public async Task <IActionResult> GetPropiedadesPorTipo([FromRoute] decimal idPais) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } SqlConnection conn = new SqlConnection("Data Source=192.168.1.10\\SQLMASTER;Initial Catalog=Propiedades;Persist Security Info=True;User ID=propiedades;Password=propiedades"); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT pa.idPais, pa.nbrPais, pr.tipoPropiedad, count(*) FROM Pais pa INNER JOIN Propiedad pr ON(pa.idPais = pr.codigoPais) WHERE pa.idPais = " + idPais + " GROUP BY pa.idPais, pa.nbrPais, pr.tipoPropiedad", conn); cmd.CommandTimeout = 0; SqlDataReader dr = cmd.ExecuteReader(); List <PropiedadTipo> lista = new List <PropiedadTipo>(); while (dr.Read()) { PropiedadTipo p = new PropiedadTipo(); p.IdPais = dr.GetDecimal(0);; p.NbrPais = dr.GetString(1); p.TipoPropiedad = dr.GetString(2); p.Cantidad = (decimal)dr.GetInt32(3); lista.Add(p); } conn.Close(); return(Ok(lista)); }
public static PropiedadTipo GetColumnPropiedadTipo(this Type type, string sqlNameColumn) { PropiedadTipo propiedad = type.GetPropiedadesTipos().Filtra((t) => t.GetNameSQL().Equals(sqlNameColumn)).FirstOrDefault(); if (propiedad == null) { throw new Exception("El tipo " + type.Name + " no contiene la columnaSQL " + sqlNameColumn); } return(propiedad); }
public static string[] GetForeignKeySQL(this PropiedadTipo propiedad) { string[] primaryKeysTipoReference = propiedad.Tipo.GetPrimaryKeyColumnsSQL(); string[] foreignKey = new string[primaryKeysTipoReference.Length]; string nameSQL = propiedad.Tipo.GetNameSQL(); for (int i = 0; i < primaryKeysTipoReference.Length; i++) { foreignKey[i] = nameSQL + primaryKeysTipoReference[i]; } return(foreignKey); }
public static bool AccesibilidadOk(PropiedadTipo p) { bool correcto = !p.Atributos.Contains(new IgnoreSerialitzer()); if (correcto) { //miro get correcto = p.Uso.HasFlag(UsoPropiedad.Get); if (correcto && p.Tipo.IsArray || !(p.Tipo.ImplementInterficie(typeof(IList <>)) || p.Tipo.ImplementInterficie(typeof(IDictionary <,>)))) {//si no es una lista o un diccionario miro si tiene el set porque no es obligatorio correcto = p.Uso.HasFlag(UsoPropiedad.Set); } } return(correcto); }
public static string[] GetPrimaryKeyColumnsSQL(this Type type) { List <System.Reflection.PropertyInfo> columnsObjPrimaryKey = type.GetProperties().Filtra((column) => new PropiedadTipo(column).Atributos.Filtra((atr) => atr is PrimaryKeySQL).Count != 0); string[] columnsPrimaryKey = new string[columnsObjPrimaryKey.Count]; //ahora tengo todas las columnas de la tabla que harán de PrimaryKey for (int i = 0; i < columnsObjPrimaryKey.Count; i++) { columnsPrimaryKey[i] = new PropiedadTipo(columnsObjPrimaryKey[i]).GetNameSQL(); } if (columnsObjPrimaryKey.Count == 0) { columnsPrimaryKey = new string[] { "Id" } } ; return(columnsPrimaryKey); }
public static NavigationProperty GetPropertyNavigation(this Type tipo, PropiedadTipo propiedadFrom) { PropiedadTipo[] foreignKey; Type tipoFrom; string nombrePropiedadDetino; NavigationProperty navigation = new NavigationProperty(); //la propiedad del tipo tiene un Tipo y este puede o no tener navegación hacia esa propiedad, //contiene el nombre en caso de 1-n al revés ya es otro tema pero se podria mirar //si hay el nombre del tipo en la propiedad del n y si es así es probable que la otra sea el otro tipo //tener en cuenta el atributo ForeignKey para saber el nombre ya que puede ser que haya más de una lista y para no tener esa ambiguedad navigation.IsFromMany = propiedadFrom.Tipo.IsGenericType; if (navigation.IsFromMany) { tipoFrom = propiedadFrom.Tipo.GetGenericArguments()[0]; } else { tipoFrom = propiedadFrom.Tipo; } nombrePropiedadDetino = propiedadFrom.Atributos.Where(a => a is ForeignKeyAttribute).Select(a => (a as ForeignKeyAttribute).Name).FirstOrDefault(); if (String.IsNullOrEmpty(nombrePropiedadDetino)) { nombrePropiedadDetino = propiedadFrom.Nombre; foreignKey = tipoFrom.GetPropiedadesTipo() .Where(p => p.Atributos.Where(a => a is ForeignKeyAttribute).Any(attrPropiedadDestino => (attrPropiedadDestino as ForeignKeyAttribute).Name == nombrePropiedadDetino)) .Where(p => p.Tipo.Equals(tipo)) .ToArray(); if (foreignKey.Length == 0) { foreignKey = tipoFrom.GetPropiedadesTipo() .Where(p => p.Nombre.Contains(tipo.Name) && (p.Tipo.IsGenericType ? !p.Tipo.GetGenericArguments()[0].FullName.Contains(nameof(System)) : !p.Tipo.FullName.Contains(nameof(System)))) .Where(p => !p.Atributos.Any(a => a is ForeignKeyAttribute)) .ToArray(); } } else { foreignKey = tipoFrom.GetPropiedadesTipo().Where(p => p.Nombre.Equals(nombrePropiedadDetino)).ToArray(); } if (foreignKey.Length != 0) { navigation.Nombre = foreignKey.Length == 1 ? foreignKey[0].Nombre : string.Empty; navigation.IsValid = true; navigation.IsToMany = foreignKey.Length == 1 ? foreignKey[0].Tipo.IsGenericType : true; if (navigation.HasNavigationTo) { navigation.NombreId = tipoFrom.GetPropiedadesTipo().Where(p => p.Nombre.ToLower() == navigation.CalcNombreId).Select(p => p.Nombre).FirstOrDefault(); } } //tener en cuenta en el IsValid HasNavigationFrom osea que return(navigation); }
public PropiedadNoCompatibleException(PropiedadTipo propiedad, Exception ex) : base(string.Format("Propiedad '{0}' del tipo '{1}' no es compatible: '{2}'", propiedad.Nombre, propiedad.Tipo, ex.Message)) { }
public static string CreateTableSQL(this Type obj, BaseDeDatos bd) { StringBuilder strCreate = new StringBuilder("create or replace table "); List <System.Reflection.PropertyInfo> columnsObj = obj.GetProperties().Filtra((p) => p.CanRead && p.CanWrite && p.GetAttributes().Filtra((atr) => atr is IgnoreSQL).Count == 0); List <System.Reflection.PropertyInfo> columnsObjForeignKey; PropiedadTipo propiedad; PropiedadTipo propiedadReference; string[] foreignKeys; IList <Attribute> attributes; bool containsPrimaryKey = false; Constraint constraint; int endName; strCreate.Append(obj.GetNameSQL()); strCreate.Append('('); //columns //name typeSQL, for (int i = 0; i < columnsObj.Count; i++) { propiedad = new PropiedadTipo(columnsObj[i]); if (propiedad.Tipo.IsSimpleType()) { if (!containsPrimaryKey && propiedad.Atributos.Filtra((atr) => atr is PrimaryKeySQL).FirstOrDefault() as PrimaryKeySQL != null) { containsPrimaryKey = true; } strCreate.Append(propiedad.GetNameSQL()); strCreate.Append(' '); strCreate.Append(bd.GetDeclaracionType(propiedad.Tipo, propiedad.Atributos.Filtra((atr) => atr is LimiteCampoSQL).FirstOrDefault() as LimiteCampoSQL)); strCreate.Append(','); } } //si tiene una foreignkey compuesta necesito hacer esto... for (int i = 0; i < columnsObj.Count; i++) { propiedad = new PropiedadTipo(columnsObj[i]); if (!propiedad.Tipo.IsSimpleType()) { endName = propiedad.GetNameSQL().Length; foreignKeys = propiedad.GetForeignKeySQL(); if (!containsPrimaryKey && propiedad.Atributos.Filtra((atr) => atr is PrimaryKeySQL).FirstOrDefault() as PrimaryKeySQL != null) { containsPrimaryKey = true; } for (int j = 0; j < foreignKeys.Length; i++) { strCreate.Append(foreignKeys[j]); strCreate.Append(' '); propiedadReference = propiedad.Tipo.GetColumnPropiedadTipo(foreignKeys[j].Substring(endName));//cojo la propiedad de la columna primaryKey del tipo a hacer referencia strCreate.Append(bd.GetDeclaracionType(propiedadReference.Tipo, propiedadReference.Atributos.Filtra((atr) => atr is LimiteCampoSQL).FirstOrDefault() as LimiteCampoSQL)); strCreate.Append(','); } } } if (!containsPrimaryKey) { //se le tiene que poner una columna IdAutoIncrement strCreate.Append("Id"); strCreate.Append(' '); strCreate.Append(bd.GetAutoIncrementColumnDeclaration()); strCreate.Append(','); } //añado las constrains de cada columna, le pongo un nombre único y calculable for (int i = 0; i < columnsObj.Count; i++) { attributes = new PropiedadTipo(columnsObj[i]).Atributos.Filtra((atr) => atr is Constraint && !(atr is PrimaryKeySQL)); for (int j = 0; j < attributes.Count; j++) { //constrains de la columna constraint = attributes[j] as Constraint; //falta hacer strCreate.Append(constraint.GetDeclarationConstraintLine(obj.GetNameSQL(), new PropiedadTipo(columnsObj[i]).GetNameSQL())); strCreate.Append(","); } } // la PrimaryKey que puede ser compuesta //ahora tengo todas las columnas de la tabla que harán de PrimaryKey strCreate.Append("Primary Key("); strCreate.Append(String.Join(",", obj.GetPrimaryKeyColumnsSQL())); if (strCreate[strCreate.Length - 1] == ',') { strCreate.Remove(strCreate.Length - 1, 1);//quito la ',' } strCreate.Append("),"); // los ForeignKey //las columnas que sean clases que no sean basicas columnsObjForeignKey = columnsObj.Filtra((column) => !(new PropiedadTipo(column).Tipo.IsSimpleType())); for (int i = 0; i < columnsObjForeignKey.Count; i++) { propiedad = new PropiedadTipo(columnsObjForeignKey[i]); strCreate.Append("Foreign Key("); strCreate.Append(String.Join(",", propiedad.GetForeignKeySQL()));//hacer extension para Property que coja el nombre del tipo if (strCreate[strCreate.Length - 1] == ',') { strCreate.Remove(strCreate.Length - 1, 1);//quito la ',' } strCreate.Append(") References "); strCreate.Append(propiedad.Tipo.GetNameSQL()); strCreate.Append("("); strCreate.Append(String.Join(",", propiedad.Tipo.GetPrimaryKeyColumnsSQL())); if (strCreate[strCreate.Length - 1] == ',') { strCreate.Remove(strCreate.Length - 1, 1);//quito la ',' } strCreate.Append("),"); } //falta el ondelete y esas cosas if (strCreate[strCreate.Length - 1] == ',') { strCreate.Remove(strCreate.Length - 1, 1); } strCreate.Append(");"); return(strCreate.ToString()); }