public virtual string[] GetPrimaryKeys(QueryOptions queryOptions, MapRuleBasic mapRuleBasic) { List <string> columns = new List <string>(); if (string.IsNullOrEmpty(mapRuleBasic.PrimaryKeys)) { string schema = mapRuleBasic.Table.Split('.').Length == 1 ? string.Empty : mapRuleBasic.Table.Split('.')[0]; string table = mapRuleBasic.Table.Split('.').Length == 1 ? mapRuleBasic.Table.Split('.')[0] : mapRuleBasic.Table.Split('.')[1]; string[] primaryKeys = GetPrimaryKeys(queryOptions, schema, table); foreach (var primaryKey in primaryKeys) { columns.Add(primaryKey); } } else { columns = mapRuleBasic.PrimaryKeys.Split(',').Select(x => x.Trim()).ToList(); } if (columns.Count == 0) { throw new InvalidOperationException($"[{mapRuleBasic.Table}] primary keys not setted."); } return(columns.ToArray()); }
public IDataReader CreateTableReader(QueryOptions queryOptions, MapRuleBasic mapRuleBasic, out string[] primaryKeys) { primaryKeys = GetPrimaryKeys(queryOptions, mapRuleBasic); IDbCommand command = queryOptions.Connection.CreateCommand(); command.Transaction = queryOptions.Transaction; string sql = $"select * from {mapRuleBasic.Table} where 1 = 0"; command.CommandText = sql; return(command.ExecuteReader()); }
public override string[] GetPrimaryKeys(QueryOptions queryOptions, MapRuleBasic mapRuleBasic) { List <string> columns = new List <string>(); if (string.IsNullOrEmpty(mapRuleBasic.PrimaryKeys)) { using (var command = queryOptions.Connection.CreateCommand()) { command.Transaction = queryOptions.Transaction; string sql = $"pragma table_info({mapRuleBasic.Table})"; command.CommandText = sql; using (var reader = command.ExecuteReader()) { while (reader.Read()) { if (reader["pk"].ToString() == "0") { continue; } columns.Add(reader["name"].ToString()); } reader.Close(); } } } else { columns = mapRuleBasic.PrimaryKeys.Split(',').Select(x => x.Trim()).ToList(); } if (columns.Count == 0) { throw new InvalidOperationException($"[{mapRuleBasic.Table}] primary keys not setted."); } return(columns.ToArray()); }
protected Row(IDataRecord dataRecord, MapRuleBasic mapRuleBasic, bool noSetValue, string[] primaryKeys, object entity) { SetMapRuleBasic(mapRuleBasic); for (int i = 0; i < dataRecord.FieldCount; i++) { string name = dataRecord.GetName(i); _columns.Add(name); if (noSetValue) { _valueMap[name] = null; } else { _valueMap[name] = dataRecord[i]; } } PrimaryKeys = primaryKeys; Entity = entity; }
public void SetMapRuleBasic(MapRuleBasic mapRuleBasic) { _mapRuleBasic = mapRuleBasic; }
public static Row CreateWriteRow(IDataRecord dataRecord, MapRuleBasic mapRuleBasic, string[] primaryKeys, object entity) { return(new Row(dataRecord, mapRuleBasic, true, primaryKeys, entity)); }
public static Row CreateReadRow(IDataRecord dataRecord, MapRuleBasic mapRuleBasic) { return(new Row(dataRecord, mapRuleBasic, false, new string[0], null)); }
private IEnumerable <TRootEntity> Fetch(int page, int size, TotalCount totalCount, Map <TRootEntity> map) { totalCount.Value = 0; using (var reader = _databaseAccessor.CreateFetchReader(_queryOptions, out Action dispose)) { TRootEntity rootEntity = null; Row prevRow = null; int currentPage = 0; int currentSize = 0; Dictionary <MapRuleBasic, Dictionary <string, bool> > mapUniqueKeyValueCache = new Dictionary <MapRuleBasic, Dictionary <string, bool> >(); while (reader.Read()) { MapRuleBasic rootMapRuleBasic = map.RootMapRuleBasic; Row currentRow = Row.CreateReadRow(reader, rootMapRuleBasic); List <string> uniqueKeys = new List <string>(); if (currentRow.CanCreate(prevRow, uniqueKeys)) { totalCount.Value++; if (rootEntity != null) { SetReNestNodes(map, rootEntity); yield return(rootEntity); } rootEntity = null; if (currentPage == 0 || currentSize % size == 0) { currentPage++; currentSize = 0; } currentSize++; if (currentPage == page) { rootEntity = rootMapRuleBasic.GetEntity(currentRow, rootEntity) as TRootEntity; } } if (rootEntity == null) { prevRow = currentRow; continue; } if (_queryOptions.IsFlatFormat) { continue; } uniqueKeys.AddRange(currentRow.UniqueKeys); foreach (var mapRuleBasic in map.MapRuleBasicListWithoutRoot) { currentRow.SetMapRuleBasic(mapRuleBasic); // ユニークキーの値がNULL、または、依然と重複している場合は、処理をしない >> StringBuilder uniqueKeyValuesBuilder = new StringBuilder(); bool canContinue = true; foreach (var key in currentRow.UniqueKeys) { if (currentRow[key] == DBNull.Value) { canContinue = false; } uniqueKeyValuesBuilder.Append(currentRow[key].ToString() + ":"); } string uniqueKeyValues = uniqueKeyValuesBuilder.ToString(); if (mapUniqueKeyValueCache.TryGetValue(mapRuleBasic, out Dictionary <string, bool> values)) { if (values.ContainsKey(uniqueKeyValues)) { canContinue = false; } ; } else { mapUniqueKeyValueCache[mapRuleBasic] = new Dictionary <string, bool>(); } mapUniqueKeyValueCache[mapRuleBasic][uniqueKeyValues] = true; if (!canContinue) { continue; } // ユニークキーが重複している場合は、処理をしない << object parentEntity = rootEntity; PropertyInfo property = null; foreach (var section in mapRuleBasic.ExpressionSections) { if (property != null && !property.PropertyType.IsList()) { parentEntity = property.Get(parentEntity); } if (property != null && property.PropertyType.IsList()) { var list = property.Get(parentEntity) as IEnumerable; foreach (var item in list) { parentEntity = item; } } Dictionary <string, PropertyInfo> propertyMap = parentEntity.GetType().GetPropertyMap(BindingFlags.GetProperty | BindingFlags.SetProperty, PropertyTypeFilters.OnlyClass); property = propertyMap[section]; } if (mapRuleBasic.IsToMany) { object list = property.Get(parentEntity); if (list == null) { if (property.PropertyType.IsArray) { list = Array.CreateInstance(mapRuleBasic.EntityType, 0); } else if (property.PropertyType == typeof(IEnumerable <>).MakeGenericType(mapRuleBasic.EntityType)) { list = typeof(List <>).MakeGenericType(mapRuleBasic.EntityType).Create(); } else { list = property.PropertyType.Create(); } property.Set(parentEntity, list); } object entity = mapRuleBasic.GetEntity(currentRow, rootEntity); if (property.PropertyType.IsArray) { Array array = (Array)list; Array newArray = Array.CreateInstance(mapRuleBasic.EntityType, array.Length + 1); array.CopyTo(newArray, 0); newArray.SetValue(entity, array.Length); property.Set(parentEntity, newArray); } else if (property.PropertyType == typeof(IEnumerable <>).MakeGenericType(mapRuleBasic.EntityType)) { dynamic dynamicList = typeof(List <>).MakeGenericType(mapRuleBasic.EntityType).Create(); dynamicList.AddRange(list as dynamic); dynamicList.Add(entity as dynamic); property.Set(parentEntity, dynamicList as object); } else { MethodInfo method = list.GetType().GetMethod("Add", new Type[] { mapRuleBasic.EntityType }); method.Invoke(list, new object[] { entity }); } } else { object entity = mapRuleBasic.GetEntity(currentRow, rootEntity); property.Set(parentEntity, entity); } } prevRow = currentRow; } if (rootEntity != null) { SetReNestNodes(map, rootEntity); yield return(rootEntity); } reader.Close(); dispose(); } }