protected void AddMasterDataTable() { var metadata = (TMappingInfo)MappingFactory.GetMapping(typeof(TMappingInfo)); // Add main data table AddDataTable(metadata.TableName, metadata.EntityName, metadata); }
protected virtual TEntity ReadEntity(IDataReader reader) { var entity = new TEntity(); var items = _metadata.Columns; foreach (var item in items) { if (IgnoreMemoFields && item.IsMemo) { continue; } var fieldIndex = reader.GetOrdinal(item.Name); if (reader.IsDBNull(fieldIndex)) { continue; } entity[item.Name] = reader.GetValue(fieldIndex); } if (_associations == null) { return(entity); } foreach (var belong in _associations) { var fieldIndex = reader.GetOrdinal(belong.ThisKeys[0]); if (reader.IsDBNull(fieldIndex)) { continue; } var propEntity = entity[belong.Name] as Entity; if (propEntity == null) { continue; } var omapping = MappingFactory.GetMapping(belong.OtherMappingType); for (var x = 0; x < omapping.Columns.Count; x++) { var item = omapping.Columns[x]; var itemName = belong.Name + "." + item.Name; fieldIndex = reader.GetOrdinal(itemName); if (!reader.IsDBNull(fieldIndex)) { propEntity[item.Name] = reader.GetValue(fieldIndex); } } } return(entity); }
public Repository() { _metadata = MappingFactory.GetMapping <TMappingInfo>(); _tableName = _metadata.TableName; _tableAlias = _metadata.EntityName; _entityName = _metadata.EntityName; _associations = _metadata.Associations.Where(x => x.GetType().Name == "MakeReferenceToMapping`2"); _hasManyAssociations = _metadata.Associations.Where(x => x.GetType().Name == "HasManyMapping`2"); }
public void Will_returnAttributeDataTypeMapping_When_Requested() { ValueMapping mapping = MappingFactory.GetMapping(MappingTypes.ATTRIBUTE_DATA_TYPE); Assert.IsNotNull(mapping); Assert.AreEqual(MappingTypes.ATTRIBUTE_DATA_TYPE, mapping.MappingType); Assert.AreEqual(typeof(char), mapping.ValueType); Assert.IsTrue(mapping.PossibleValues.ContainsKey('A')); Assert.AreEqual("Alpha numeric", mapping.PossibleValues['A']); Assert.IsTrue(mapping.PossibleValues.ContainsKey('N')); Assert.AreEqual("Numeric", mapping.PossibleValues['N']); }
protected virtual SelectStatement GetSelectStatement(ICondition criteria, IOrderByExpression order_by_expression) { var colNames = new List <string>(); var select = new SelectStatement { ConnectionStringName = _connectionStringName }; select.From(GetBaseSelectStatement(), _entityName); for (var x = 0; x < _metadata.Columns.Count; x++) { string colName = _metadata.Columns[x].Name; colNames.Add(colName); select.Column(new SqlColumnName(_metadata.Columns[x].Name), colName); } if (_associations != null) { foreach (var belong in _associations) { var omapping = MappingFactory.GetMapping(belong.OtherMappingType); for (var x = 0; x < omapping.Columns.Count; x++) { string colName = belong.Name + "." + omapping.Columns[x].Name; if (colNames.Contains(colName)) { continue; } else { colNames.Add(colName); } select.Column(new SqlColumnName(colName), colName); } } } if (criteria != null) { select.Where(criteria); } if (order_by_expression != null) { select.OrderBy(order_by_expression); } return(select); }
public void Will_ReturnStatusMapping_When_Requested() { ValueMapping mapping = MappingFactory.GetMapping(MappingTypes.STATUS); Assert.IsNotNull(mapping); Assert.AreEqual(MappingTypes.STATUS, mapping.MappingType); Assert.AreEqual(typeof(char), mapping.ValueType); Assert.IsTrue(mapping.PossibleValues.ContainsKey('N')); Assert.AreEqual("Normal", mapping.PossibleValues['N']); Assert.IsTrue(mapping.PossibleValues.ContainsKey('P')); Assert.AreEqual("Parked", mapping.PossibleValues['P']); Assert.IsTrue(mapping.PossibleValues.ContainsKey('C')); Assert.AreEqual("Closed", mapping.PossibleValues['C']); Assert.IsTrue(mapping.PossibleValues.ContainsKey('T')); Assert.AreEqual("Terminated", mapping.PossibleValues['T']); }
public void Will_ReturnBflagMapping_When_Requested() { ValueMapping mapping = MappingFactory.GetMapping(MappingTypes.BFLAG); Assert.IsNotNull(mapping); Assert.AreEqual(MappingTypes.BFLAG, mapping.MappingType); Assert.AreEqual(typeof(int), mapping.ValueType); Assert.IsTrue(mapping.PossibleValues.ContainsKey(1)); Assert.AreEqual("Address(A)", mapping.PossibleValues[1]); Assert.IsTrue(mapping.PossibleValues.ContainsKey(2)); Assert.AreEqual("Memory/cache(M)", mapping.PossibleValues[2]); Assert.IsTrue(mapping.PossibleValues.ContainsKey(4)); Assert.AreEqual("Workflow(W)", mapping.PossibleValues[4]); Assert.IsTrue(mapping.PossibleValues.ContainsKey(8)); Assert.AreEqual("Master file(MF)", mapping.PossibleValues[8]); Assert.IsTrue(mapping.PossibleValues.ContainsKey(16)); Assert.AreEqual("Dates on relations", mapping.PossibleValues[16]); Assert.IsTrue(mapping.PossibleValues.ContainsKey(32)); Assert.AreEqual("Form", mapping.PossibleValues[32]); }
private IDataReader GetRelatedTableReader(string table_name) { var metadata = (TMappingInfo)MappingFactory.GetMapping(typeof(TMappingInfo)); var item = metadata.Associations.Where(x => x.Name == table_name).FirstOrDefault(); if (item == null) { return(null); } var omapping = MappingFactory.GetMapping(item.OtherMappingType); var qry = (new SelectStatement()) .Column(omapping.Columns.Where(x => x.Name == item.OtherKeys[0]).Single().ColumnName) .From(omapping.TableName); var lblCount = item.OtherLabels.Count; if (lblCount == 0) { return(null); } else { if (lblCount == 1) { qry.Column(omapping.Columns.Where(x => x.Name == item.OtherLabels[0]).Single().ColumnName); } else { var lblConcat = new ConcatFunction(" | "); foreach (var lbl in item.OtherLabels) { lblConcat.Append(new SqlColumnName(omapping.Columns.Where(x => x.Name == lbl).Single().ColumnName)); } qry.Column(lblConcat); } } return(qry.ExecuteReader()); }
protected void AddDetailsDataTables(TMappingInfo mapping_info) { // Add details data tables var items = mapping_info.Associations; foreach (var item in items) { var mapping = MappingFactory.GetMapping(item.OtherMappingType); var tableName = mapping.TableName; var tableAlias = item.Name; var associationName = item.GetType().Name; AddDataTable(tableName, tableAlias, mapping); var thisColumns = new DataColumn[item.ThisKeys.Count]; var otherColumns = new DataColumn[item.OtherKeys.Count]; for (int z = 0; z < item.ThisKeys.Count; z++) { thisColumns[z] = this.Tables[0].Columns[item.ThisKeys[z]]; } for (int z = 0; z < item.OtherKeys.Count; z++) { var key = item.OtherKeys[z]; var ccol = this.Tables[tableAlias].Columns[item.OtherKeys[z]]; otherColumns[z] = ccol; } if (associationName.Contains("Has")) { this.Tables[tableAlias].ExtendedProperties["IsChild"] = true; this.Relations.Add(new DataRelation(tableAlias, thisColumns, otherColumns)); } } }
protected SelectStatement GetBaseSelectStatement() { var colNames = new List <string>(); var select = new SelectStatement { ConnectionStringName = _connectionStringName }; select.From(_tableName, _tableAlias); for (var x = 0; x < _metadata.Columns.Count; x++) { var col = _metadata.Columns[x]; if (col.Name.IndexOf(".") < 0) { select.Column(new SqlColumnName(_tableAlias, col.ColumnName), col.Name); } } //if (_associations == null) // _associations = _metadata.Associations.Where(x => x.GetType().Name == "BelongsToMapping`2" || x.GetType().Name == "HasOneMapping`2"); if (_associations.Count() == 0) { return(select); } foreach (var item in _associations) { var omapping = MappingFactory.GetMapping(item.OtherMappingType); // extra columns for criteria support for (var x = 0; x < omapping.Columns.Count; x++) { var col = omapping.Columns[x]; string colName = item.Name + "." + col.Name; if (colNames.Contains(colName)) { continue; } else { colNames.Add(colName); } select.Column(new SqlColumnName(item.Name, col.ColumnName), item.Name + "." + col.Name); } var keyName = item.ThisKeys[0]; var otherKeyName = item.OtherKeys[0]; var tcol = _metadata.Columns.Where(x => x.Name == keyName).Single(); var ocol = omapping.Columns.Where(x => x.Name == otherKeyName).Single(); if (tcol.IsNullable) { select.LeftJoin(omapping.TableName, item.Name, Condition.IsEqual(new SqlColumnName(_entityName, tcol.ColumnName), new SqlColumnName(item.Name, ocol.ColumnName))); } else { select.InnerJoin(omapping.TableName, item.Name, Condition.IsEqual(new SqlColumnName(_entityName, tcol.ColumnName), new SqlColumnName(item.Name, ocol.ColumnName))); } } return(select); }
public virtual void Update(TEntity entity) { ICondition criteria = IdentityInsert ? Conditions.IsEqual(Entity.ID, new SqlNumber(entity.Id.ToString())) : Conditions.IsEqual(Entity.ID, new SqlString(entity.Id.ToString())); var current = GetOne(criteria); if (current == null) { return; } bool hasChanges = false; foreach (var item in _metadata.Columns) { if (current[item.Name] != entity[item.Name]) { hasChanges = true; break; } } if (hasChanges) { var update = SqlUtils.CreateUpdateStatement(_metadata); update.ConnectionStringName = _connectionStringName; foreach (var item in _metadata.Columns) { update.SetParamValue(item.Name, entity[item.Name]); } update.Execute(); } if (_hasManyAssociations.Count() == 0) { return; } foreach (IAssociationMapping hma in _hasManyAssociations) { var items = entity[hma.Name] as IEnumerable <ValueObject>; if (items == null) { continue; } var storedItems = current[hma.Name] as IEnumerable <ValueObject>; // if (storedItems == null) // continue; var omapping = MappingFactory.GetMapping(hma.OtherMappingType); //var primKeys = omapping.Columns.Where(o => o.IsPrimaryKey || o.IsDbGenerated).ToList(); DeleteStatement deleteStament = SqlUtils.CreateDeleteStatement(omapping, true); deleteStament.ConnectionStringName = _connectionStringName; InsertStatement insertStament = SqlUtils.CreateInsertStatement(omapping); insertStament.ConnectionStringName = _connectionStringName; if (storedItems != null) { // Delete non existent foreach (ValueObject storedItem in storedItems) { var itemFound = items.FirstOrDefault(ci => ci.Equals(storedItem)); if (itemFound == null) { foreach (ColumnMapping cm in omapping.Columns) { deleteStament.SetParamValue(cm.Name, storedItem[cm.Name]); } deleteStament.Execute(); } } // Insert non existent foreach (ValueObject item in items) { var itemFound = storedItems.FirstOrDefault(si => si.Equals(item)); if (itemFound == null) { foreach (ColumnMapping cm in omapping.Columns) { insertStament.SetParamValue(cm.Name, item[cm.Name]); } insertStament.Execute(); } } } else { foreach (ValueObject item in items) { foreach (ColumnMapping cm in omapping.Columns) { insertStament.SetParamValue(cm.Name, item[cm.Name]); } insertStament.Execute(); } } } }
protected void AddDetailsDataTables() { var metadata = (TMappingInfo)MappingFactory.GetMapping(typeof(TMappingInfo)); AddDetailsDataTables(metadata); }
/// <summary> /// Função resposável por retornar o tipo de mapeamento do arquivo enviado /// </summary> /// <exception cref="FileFormatException">Arquivo não suportado</exception> /// <returns>Nome do mapeamento</returns> public string MappingName() { var format = MappingFactory.GetMapping(_fileExtension); return(format?.GetMappingType()); }
public static SelectStatement CreateSelectStatement(IMapping mapping) { var colNames = new List <string>(); var baseSelect = new SelectStatement(); baseSelect.From(mapping.TableName, mapping.EntityName); for (var x = 0; x < mapping.Columns.Count; x++) { var col = mapping.Columns[x]; if (col.Name.IndexOf(".") < 0) { baseSelect.Column(new SqlColumnName(mapping.EntityName, col.ColumnName), col.Name); } } var _associations = mapping.Associations.Where(x => x.GetType().Name == "MakeReferenceToMapping`2"); foreach (var item in _associations) { var omapping = MappingFactory.GetMapping(item.OtherMappingType); // extra columns for criteria support for (var x = 0; x < omapping.Columns.Count; x++) { var col = omapping.Columns[x]; string colName = item.Name + "." + col.Name; if (colNames.Contains(colName)) { continue; } else { colNames.Add(colName); } baseSelect.Column(new SqlColumnName(item.Name, col.ColumnName), item.Name + "." + col.Name); } var keyName = item.ThisKeys[0]; var otherKeyName = item.OtherKeys[0]; var tcol = mapping.Columns.Where(x => x.Name == keyName).Single(); var ocol = omapping.Columns.Where(x => x.Name == otherKeyName).Single(); if (tcol.IsNullable) { baseSelect.LeftJoin(omapping.TableName, item.Name, Condition.IsEqual(new SqlColumnName(mapping.EntityName, tcol.ColumnName), new SqlColumnName(item.Name, ocol.ColumnName))); } else { baseSelect.InnerJoin(omapping.TableName, item.Name, Condition.IsEqual(new SqlColumnName(mapping.EntityName, tcol.ColumnName), new SqlColumnName(item.Name, ocol.ColumnName))); } } colNames = new List <string>(); var select = new SelectStatement(); select.From(baseSelect, mapping.EntityName); for (var x = 0; x < mapping.Columns.Count; x++) { string colName = mapping.Columns[x].Name; colNames.Add(colName); select.Column(new SqlColumnName(mapping.Columns[x].Name), colName); } foreach (var belong in _associations) { var omapping = MappingFactory.GetMapping(belong.OtherMappingType); for (var x = 0; x < omapping.Columns.Count; x++) { string colName = belong.Name + "." + omapping.Columns[x].Name; if (colNames.Contains(colName)) { continue; } else { colNames.Add(colName); } select.Column(new SqlColumnName(colName), colName); } } return(select); }
public virtual TEntity GetOne(ICondition filter, IOrderByExpression order_by) { var select = GetSelectStatement(filter, order_by); var reader = select.ExecuteReader(); TEntity entity = default(TEntity); if (reader.Read()) { entity = ReadEntity(reader); } reader.Close(); if (entity == null) { return(default(TEntity)); } if (_associations.Count() > 0) { foreach (IAssociationMapping _association in _associations) { var amapping = MappingFactory.GetMapping(_association.OtherMappingType); object val = entity[_association.ThisKeys[0]]; if (val == null) { continue; } int colCount = amapping.Columns.Count; var select2 = SqlUtils.CreateSelectStatement(amapping); select2.ConnectionStringName = select.ConnectionStringName; select2.Where(Condition.IsEqual(_association.OtherKeys[0], new SqlNumber((int)val))); var values = new Dictionary <string, object>(); var reader2 = select2.ExecuteReader(); if (reader2.Read()) { if (entity[_association.Name] == null) { entity[_association.Name] = Activator.CreateInstance(entity.GetProperty(_association.Name).PropertyType, (int)val); } Entity _prop = entity[_association.Name] as Entity; for (int f = 0; f < reader2.FieldCount; f++) { _prop[reader2.GetName(f)] = reader2.GetValue(f); } } reader2.Close(); } } if (_hasManyAssociations.Count() == 0) { return(entity); } foreach (IAssociationMapping hma in _hasManyAssociations) { var omapping = MappingFactory.GetMapping(hma.OtherMappingType); int colCount = omapping.Columns.Count; var select2 = SqlUtils.CreateSelectStatement(omapping); select2.ConnectionStringName = select.ConnectionStringName; ICondition idCriteria = IdentityInsert ? Conditions.IsEqual(hma.OtherKeys[0], new SqlNumber(entity.Id.ToString())) : Conditions.IsEqual(hma.OtherKeys[0], new SqlString(entity.Id.ToString())); select2.Where(idCriteria); var reader2 = select2.ExecuteReader(); while (reader2.Read()) { var values = new Dictionary <string, object>(); for (int f = 0; f < reader2.FieldCount; f++) { values.Add(reader2.GetName(f), reader2.GetValue(f)); } entity.AddValueObject(hma.Name, values); } reader2.Close(); } return(entity); }
public virtual void Insert(TEntity entity) { var insert = SqlUtils.CreateInsertStatement(_metadata, IdentityInsert); insert.ConnectionStringName = _connectionStringName; foreach (var item in _metadata.Columns) { if (IdentityInsert || !item.IsDbGenerated) { insert.SetParamValue(item.Name, entity[item.Name]); } } if (!String.IsNullOrEmpty(insert.IdentityColumnName)) { object id; insert.Execute(out id); entity[insert.IdentityColumnName] = Int32.Parse(id.ToString()); } else { insert.Execute(); } if (_hasManyAssociations.Count() == 0) { return; } foreach (IAssociationMapping hma in _hasManyAssociations) { var items = entity[hma.Name] as IEnumerable <IIndexable>; if (items == null || items.Count() == 0) { continue; } var omapping = MappingFactory.GetMapping(hma.OtherMappingType); InsertStatement insert2 = SqlUtils.CreateInsertStatement(omapping); insert2.ConnectionStringName = insert.ConnectionStringName; int colCount = omapping.Columns.Count; foreach (TEntity item in items) { if (!String.IsNullOrEmpty(insert.IdentityColumnName)) { item[hma.OtherKeys[0]] = entity[insert.IdentityColumnName]; } for (var x = 0; x < colCount; x++) { var propName = omapping.Columns[x].Name; insert2.SetParamValue(propName, item[propName]); } if (!String.IsNullOrEmpty(insert2.IdentityColumnName)) { object id; insert2.Execute(out id); item[insert2.IdentityColumnName] = Int32.Parse(id.ToString()); } else { insert2.Execute(); } } } }
private void AddDataTable(string table_name, string table_alias, IMapping mapping) { if (this.Tables.Contains(table_alias)) { return; } var table = new DataTable(table_alias); var keys = new List <DataColumn>(); this.Tables.Add(table); var columns = table.Columns; var select = (new SelectStatement()).From(table_name, "t0"); table.ExtendedProperties["Storage"] = table_name; foreach (var item in mapping.Columns) { if (IgnoreMemoFields && item.IsMemo) { continue; } var pinfo = (PropertyInfo)item.Property; DataColumn column; if (pinfo.PropertyType.IsGenericType && pinfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { column = columns.Add(item.Name, pinfo.PropertyType.GetGenericArguments()[0]); } else if (pinfo.PropertyType.IsEnum) { column = columns.Add(item.Name, typeof(string)); } else { column = columns.Add(item.Name, pinfo.PropertyType); } select.Column(new SqlColumnName("t0", item.ColumnName), item.Name); if (item.IsDbGenerated) { column.AutoIncrement = true; column.AutoIncrementSeed = 1; column.AutoIncrementStep = 1; column.Unique = true; } if (item.IsPrimaryKey) { column.ExtendedProperties["PrimaryKey"] = true; keys.Add(column); } column.AllowDBNull = item.IsNullable; //if (item.IsNullable && item.DefaultValue != null) // column.DefaultValue = item.DefaultValue; if (!item.IsDbGenerated) { if (item.DefaultFunctionValue == null) { column.DefaultValue = item.DefaultValue; } else { column.DefaultValue = item.DefaultFunctionValue(); } } column.ExtendedProperties["Storage"] = item.ColumnName; if (item.IsMemo) { column.ExtendedProperties["Memo"] = true; } } table.ExtendedProperties["SelectStatement"] = select; if (keys.Count > 0) { table.PrimaryKey = keys.ToArray(); } /* Procedimiento para añadir las columnas descriptivas de las columnas foreanas */ var items = mapping.Associations.Where(x => x.GetType().Name == "MakeReferenceToMapping`2" || x.GetType().Name == "HasOneMapping`2"); if (items.Count() == 0) { return; } //Debes poner las otras columnas con el nombre fisico alias el nombre de la propiedad int t = 1; string tp = "t" + t; foreach (var item in items) { var omapping = MappingFactory.GetMapping(item.OtherMappingType); // extra columns for criteria support for (var x = 0; x < omapping.Columns.Count; x++) { var col = omapping.Columns[x]; select.Column(new SqlColumnName(tp, col.ColumnName), item.Name + "." + col.Name); } /* build joins */ //for (var k = 0; k < item.ThisKeys.Count; k++ ) //{ var keyName = item.ThisKeys[0]; var otherKeyName = item.OtherKeys[0]; var tcol = mapping.Columns.Where(x => x.Name == keyName).Single(); var ocol = omapping.Columns.Where(x => x.Name == otherKeyName).Single(); if (tcol.IsNullable || item.GetType().Name == "HasOneMapping`2") { select.LeftJoin(omapping.TableName, tp, Condition.IsEqual(new SqlColumnName("t0", tcol.ColumnName), new SqlColumnName(tp, ocol.ColumnName))); } else { select.InnerJoin(omapping.TableName, tp, Condition.IsEqual(new SqlColumnName("t0", tcol.ColumnName), new SqlColumnName(tp, ocol.ColumnName))); } //} /* build foreign row name */ var lblCount = item.OtherLabels.Count; if (lblCount > 0) { var lbl0 = item.OtherLabels[0]; //var colName = omapping.Columns.Where(x => x.Name == lbl0).Single().ColumnName; ISqlExpression lblExpr; if (lblCount == 1) { lblExpr = new SqlColumnName(item.Name + "." + item.OtherLabels[0]); } else { var lblConcat = new ConcatFunction(" | "); foreach (var lbl in item.OtherLabels) { lblConcat.Append(new SqlColumnName(item.Name, lbl)); } lblExpr = lblConcat; } var column = columns.Add(item.Name, typeof(string)); column.ExtendedProperties["SelectExpression"] = lblExpr; column.ExtendedProperties["NonStorable"] = true; } // Include OtherColumns in DataTable foreach (string ocn in item.OtherColumns) { var column = columns.Add(omapping.EntityName + "." + ocn, typeof(string)); column.ExtendedProperties["NonStorable"] = true; } t++; tp = "t" + t; } }