public virtual Dictionary <string, object> AppendDataScripts(GenerateScriptOption option, StringBuilder sb, Table table, List <TableColumn> columns, Dictionary <long, List <Dictionary <string, object> > > dictPagedData) { Dictionary <string, object> parameters = new Dictionary <string, object>(); bool appendString = option.ScriptOutputMode.HasFlag(GenerateScriptOutputMode.WriteToString); bool appendFile = option.ScriptOutputMode.HasFlag(GenerateScriptOutputMode.WriteToFile); List <string> excludeColumnNames = new List <string>(); if (option.GenerateIdentity && !option.InsertIdentityValue) { excludeColumnNames = columns.Where(item => item.IsIdentity).Select(item => item.ColumnName).ToList(); } foreach (var kp in dictPagedData) { StringBuilder sbFilePage = new StringBuilder(Environment.NewLine); string tableName = this.GetQuotedObjectName(table); string insert = $"{this.GetBatchInsertPrefix()} {tableName}({this.GetQuotedColumnNames(columns.Where(item => !excludeColumnNames.Contains(item.ColumnName)))})VALUES"; if (appendString) { sb.AppendLine(insert); } if (appendFile) { sbFilePage.AppendLine(insert); } int rowCount = 0; foreach (var row in kp.Value) { rowCount++; string values = this.GetInsertValues(columns, excludeColumnNames, kp.Key, rowCount - 1, row, out var insertParameters, out var valuesWithoutParameter); if (insertParameters != null) { foreach (var para in insertParameters) { parameters.Add(para.Key, para.Value); } } values = $"{this.GetBatchInsertItemBefore(tableName, rowCount == 1)}{values}{this.GetBatchInsertItemEnd(rowCount == kp.Value.Count)}"; if (option.RemoveEmoji) { values = StringHelper.RemoveEmoji(values); } if (appendString) { sb.AppendLine(values); } if (appendFile) { sbFilePage.AppendLine(valuesWithoutParameter); } } if (appendString) { sb.AppendLine(); } if (appendFile) { sbFilePage.AppendLine(); this.AppendScriptsToFile(sbFilePage.ToString(), GenerateScriptMode.Data); } } return(parameters); }
private Dictionary <long, List <Dictionary <string, object> > > GetSortedPageDatas(DbConnection connection, Table table, string primaryKeyColumns, string parentColumnName, List <TableColumn> columns, GenerateScriptOption option, string whereClause = "") { string quotedTableName = this.GetQuotedObjectName(table); int pageSize = option.DataBatchSize; long total = Convert.ToInt64(this.GetScalar(connection, $"SELECT COUNT(1) FROM {quotedTableName} {whereClause}")); var dictPagedData = this.GetPagedDataList(connection, table, columns, primaryKeyColumns, total, pageSize, whereClause); List <object> parentValues = dictPagedData.Values.SelectMany(item => item.Select(t => t[primaryKeyColumns.Trim(QuotationLeftChar, QuotationRightChar)])).ToList(); if (parentValues.Count > 0) { TableColumn parentColumn = columns.FirstOrDefault(item => item.Owner == table.Owner && item.ColumnName == parentColumnName); long parentValuesPageCount = PaginationHelper.GetPageCount(parentValues.Count, option.InQueryItemLimitCount); for (long parentValuePageNumber = 1; parentValuePageNumber <= parentValuesPageCount; parentValuePageNumber++) { IEnumerable <object> pagedParentValues = parentValues.Skip((int)(parentValuePageNumber - 1) * pageSize).Take(option.InQueryItemLimitCount); whereClause = $" WHERE {GetQuotedString(parentColumnName)} IN ({string.Join(",", pagedParentValues.Select(item => SelectDataScriptValue(parentColumn, item, true)))})"; total = Convert.ToInt64(this.GetScalar(connection, $"SELECT COUNT(1) FROM {quotedTableName} {whereClause}")); if (total > 0) { Dictionary <long, List <Dictionary <string, object> > > dictChildPagedData = this.GetSortedPageDatas(connection, table, primaryKeyColumns, parentColumnName, columns, option, whereClause); foreach (var kp in dictChildPagedData) { long pageNumber = dictPagedData.Keys.Max(item => item); dictPagedData.Add(pageNumber + 1, kp.Value); } } } } return(dictPagedData); }
public DbInterpreter(ConnectionInfo connectionInfo, GenerateScriptOption option) { this.ConnectionInfo = connectionInfo; this.Option = option; }
public OracleInterpreter(ConnectionInfo connectionInfo, GenerateScriptOption options) : base(connectionInfo, options) { }
public static DbInterpreter GetDbInterpreter(DatabaseType dbType, ConnectionInfo connectionInfo, GenerateScriptOption generateScriptOption) { DbInterpreter dbInterpreter = null; var assembly = Assembly.GetExecutingAssembly(); //var typeArray = assembly.GetTypes(); var typeArray = assembly.ExportedTypes; var types = (from type in typeArray where type.IsSubclassOf(typeof(DbInterpreter)) select type).ToList(); foreach (var type in types) { dbInterpreter = (DbInterpreter)Activator.CreateInstance(type, connectionInfo, generateScriptOption); if (dbInterpreter.DatabaseType == dbType) { return(dbInterpreter); } } return(dbInterpreter); }
public SqlServerInterpreter(ConnectionInfo connectionInfo, GenerateScriptOption options) : base(connectionInfo, options) { }