public async Task LoadSchemaAsync(Db2SourceContext dataSet) { try { await dataSet.LoadSchemaAsync(); GC.Collect(); } catch (Exception t) { App.HandleThreadException(t); } return; }
//#pragma warning disable 1998 public async Task LoadSchemaAsync(Db2SourceContext dataSet, IDbConnection connection) { try { await dataSet.LoadSchemaAsync(connection); GC.Collect(); } catch (Exception t) { App.HandleThreadException(t); } finally { connection.Close(); connection.Dispose(); } return; }
private async Task ExportConfigAsync(Db2SourceContext dataSet, List <string> schemas, List <string> excludedSchemas, string configFile, string ruleFile, Encoding encoding) { LoadFromRuleFile(ruleFile, encoding); await dataSet.LoadSchemaAsync(); Dictionary <string, bool> activeSchemas = GetActiveSchemaDict(dataSet, schemas, excludedSchemas); StringBuilder buf = new StringBuilder(); IDbConnection conn = dataSet.NewConnection(true); IDbCommand cmdTbl = dataSet.GetSqlCommand(_tableSql, null, conn); IDbCommand cmdCol = dataSet.GetSqlCommand(Properties.Resources.ColumnSQL, null, conn); IDbCommand cmdKey = dataSet.GetSqlCommand(Properties.Resources.KeyConsSQL, null, conn); try { IDbDataParameter pC = cmdCol.Parameters[0] as IDbDataParameter; //pC.ParameterName = "oid"; pC.DbType = DbType.Int64; //cmdCol.Parameters.Add(pC); IDbDataParameter pK = cmdKey.Parameters[0] as IDbDataParameter; //pK.ParameterName = "oid"; pK.DbType = DbType.Int64; //cmdKey.Parameters.Add(pK); List <TableInfo> l = new List <TableInfo>(); using (IDataReader reader = cmdTbl.ExecuteReader()) { while (reader.Read()) { uint id = (uint)reader.GetValue(0); string sch = reader.GetString(1); string tbl = reader.GetString(2); if (!activeSchemas.ContainsKey(sch.ToLower())) { continue; } l.Add(new TableInfo() { Oid = id, Schema = sch, TableName = tbl }); } } for (int i = 0; i < l.Count; i++) { TableInfo info = l[i]; SortedList <int, string> lCol = new SortedList <int, string>(); List <string> ignoreCols = new List <string>(); List <ValueInfo> fixedCols = new List <ValueInfo>(); pC.Value = (Int64)info.Oid; using (IDataReader readerC = cmdCol.ExecuteReader()) { while (readerC.Read()) { string col = readerC.GetString(0); string[] keys = new string[] { (info.Schema + "." + info.TableName + "." + col).ToLower(), (info.TableName + "." + col).ToLower(), col.ToLower() }; int idx = readerC.GetInt32(1); lCol.Add(idx, col); if (_ignoreFields.ContainsKey(keys[0]) || _ignoreFields.ContainsKey(keys[1]) || _ignoreFields.ContainsKey(keys[2])) { ignoreCols.Add(col); } foreach (string k in keys) { if (_fixedFields.ContainsKey(k)) { fixedCols.Add(new ValueInfo() { FieldName = col, Value = _fixedFields[k] }); break; } } } } info.IgnoreFields = ignoreCols.ToArray(); List <string> keyCols = new List <string>(); pK.Value = (Int64)info.Oid; using (IDataReader readerK = cmdKey.ExecuteReader()) { if (readerK.Read()) { Array a = readerK.GetValue(1) as Array; foreach (object o in a) { int p = Convert.ToInt32(o); keyCols.Add(lCol[p]); } } } info.OrderBy = keyCols.ToArray(); buf.AppendLine(string.Format("[{0}.{1}]", info.Schema, info.TableName)); buf.Append("ORDER="); buf.AppendLine(ToCsv(info.OrderBy)); buf.Append("WHERE="); buf.AppendLine(); buf.Append("IGNORE="); buf.AppendLine(ToCsv(info.IgnoreFields)); buf.AppendLine(string.Format("[{0}.{1}\\FIXED]", info.Schema, info.TableName)); List <string> fixVals = new List <string>(); foreach (ValueInfo f in fixedCols) { buf.Append(f.FieldName); buf.Append('='); buf.AppendLine(f.Value); } buf.AppendLine(); } } finally { cmdTbl.Dispose(); cmdCol.Dispose(); cmdKey.Dispose(); conn.Dispose(); } File.WriteAllText(_configFileName, buf.ToString(), encoding); }
private async Task ExportAsync(Db2SourceContext dataSet, IEnumerable <Schema> schemas, string baseDir) { Dictionary <string, bool> exported = new Dictionary <string, bool>(); await dataSet.LoadSchemaAsync(); foreach (Schema s in schemas) { string schemaDir = Path.Combine(baseDir, s.Name); foreach (SchemaObject obj in s.Objects) { StringBuilder buf = new StringBuilder(); if (obj is Table) { ExportTable(buf, (Table)obj); } else if (obj is View) { ExportView(buf, (View)obj); } else if (obj is StoredFunction) { ExportStoredFunction(buf, (StoredFunction)obj); } else if (obj is Sequence) { ExportSequence(buf, (Sequence)obj); } else if (obj is ComplexType) { ExportComplexType(buf, (ComplexType)obj); } else if (obj is PgsqlEnumType) { ExportEnumType(buf, (PgsqlEnumType)obj); } else if (obj is PgsqlBasicType) { ExportBasicType(buf, (PgsqlBasicType)obj); } else if (obj is PgsqlRangeType) { ExportRangeType(buf, (PgsqlRangeType)obj); } if (buf.Length != 0) { string dir = Path.Combine(schemaDir, obj.GetExportFolderName()); string path = Path.Combine(dir, obj.Name + ".sql"); Directory.CreateDirectory(dir); bool append = exported.ContainsKey(path); Encoding encoding = (comboBoxEncoding.SelectedItem as ComboBoxItem)?.Tag as Encoding; encoding = encoding ?? Encoding.UTF8; using (StreamWriter sw = new StreamWriter(path, append, encoding)) { if (append) { sw.WriteLine(); } sw.Write(buf.ToString()); } exported[path] = true; } } } }
private async Task ExportAsync(Db2SourceContext dataSet, List <string> schemas, List <string> excludedSchemas, string baseDir, Encoding encoding) { Dictionary <string, bool> exported = new Dictionary <string, bool>(); await dataSet.LoadSchemaAsync(); foreach (Schema s in dataSet.Schemas) { string sn = s.Name.ToLower(); if (_schemas.Count != 0) { if (schemas.IndexOf(sn) == -1) { continue; } } else { if (s.IsHidden) { continue; } if (excludedSchemas.IndexOf(sn) != -1) { continue; } } Console.Error.WriteLine(s.Name + "を出力しています"); string schemaDir = Path.Combine(baseDir, s.Name); foreach (SchemaObject obj in s.Objects) { StringBuilder buf = new StringBuilder(); if (obj is Table) { ExportTable(buf, (Table)obj); } else if (obj is View) { ExportView(buf, (View)obj); } else if (obj is StoredFunction) { ExportStoredFunction(buf, (StoredFunction)obj); } else if (obj is Sequence) { ExportSequence(buf, (Sequence)obj); } else if (obj is ComplexType) { ExportComplexType(buf, (ComplexType)obj); } else if (obj is PgsqlEnumType) { ExportEnumType(buf, (PgsqlEnumType)obj); } else if (obj is PgsqlBasicType) { ExportBasicType(buf, (PgsqlBasicType)obj); } else if (obj is PgsqlRangeType) { ExportRangeType(buf, (PgsqlRangeType)obj); } if (buf.Length != 0) { string dir = Path.Combine(schemaDir, obj.GetExportFolderName()); string path = Path.Combine(dir, obj.Name + ".sql"); Directory.CreateDirectory(dir); bool append = exported.ContainsKey(path); encoding = encoding ?? Encoding.UTF8; using (StreamWriter sw = new StreamWriter(path, append, encoding)) { if (append) { sw.Write(DataSet.GetNewLine()); } sw.Write(DataSet.NormalizeNewLine(buf)); } exported[path] = true; } } } }