private void GetColumnInfo(IColumnTypeConverter columnTypeConverter, string tableName, List <IColumn> columns) { var columnFactory = DbContext.PowerPlant.CreateColumnFactory(); var selectStmt = CreateSelectStatementForColumns(tableName); var cursor = DbContext.PowerPlant.CreateDataCursor(); try { var reader = cursor.ExecuteReader(selectStmt); while (reader.Read()) { var name = reader.GetString(0); var type = reader.GetString(1); int length = reader.GetInt16(2); int prec = reader.GetInt16(3); var scale = reader.GetInt32(4); var isNullable = reader.GetString(5) == "Y"; var def = reader.IsDBNull(6) ? "" : reader.GetString(6).TrimEnd(); var sourceType = type.AddParameters(); var colType = columnTypeConverter.GetDestinationType(sourceType, ref length, ref prec, ref scale).ColumnTypeName(); columns.Add(columnFactory.CreateInstance(colType, name, length, prec, scale, isNullable, false, def, "")); } } finally { cursor.Close(); } }
public void Startup() { _length = 0; _prec = 0; _scale = 0; _columnTypeConverter = new ColumnTypeConverter(new XmlConversionsReader(new TypeDescriptionFactory(new TypeConstraintFactory(new TypeOperatorFactory())))); _columnTypeConverter.Initialize(File.ReadAllText("Resources/Unit4OracleWriterConversions.xml")); }
private void ReadColumns(IColumnTypeConverter columnsTypeConverter, ITableDefinition tableDefinition, XmlDocument xmlDocument) { var rootNode = xmlDocument.DocumentElement.SelectSingleNode("/Table"); IColumnFactory columnFactory = _dbContext.PowerPlant.CreateColumnFactory(); foreach (XmlNode col in rootNode.ChildNodes[0].ChildNodes) { ReadColumn(columnFactory, columnsTypeConverter, tableDefinition.Columns, col); } }
public ITableDefinition ReadSchema(IColumnTypeConverter columnsTypeConverter, string schemaXml) { ITableDefinition tableDefinition = _dbContext.PowerPlant.CreateTableDefinition(); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(schemaXml); ReadTableInfo(tableDefinition, xmlDocument); ReadColumns(columnsTypeConverter, tableDefinition, xmlDocument); ReadIndexes(tableDefinition, xmlDocument); return(tableDefinition); }
// Write to file from DB public override ITableDefinition GetTableDefinition(IColumnTypeConverter columnTypeConverter, string tableName) { var columns = new List <IColumn>(); GetColumnInfo(columnTypeConverter, tableName, columns); var tableDefinition = DbContext.PowerPlant.CreateTableDefinition(tableName, columns, GetSegmentName(tableName)); tableDefinition.HasBlobColumn = (from col in tableDefinition.Columns where col.Type == ColumnTypeName.Blob select col).Any(); return(tableDefinition); }
private static void ReadColumn(IColumnFactory columnFactory, IColumnTypeConverter columnsTypeConverter, List <IColumn> columns, XmlNode col) { string colName = col.Attributes["Name"].InnerText; string type = col["Type"].InnerText.ToLower(); bool isNullable = Convert.ToBoolean(col["IsNullable"].InnerText); string def = col["Default"].InnerText; Dictionary <string, object> colDetails = ReadColumnDetails(col); var length = colDetails.ContainsKey("Length") ? Convert.ToInt32(colDetails["Length"]) : 0; var prec = colDetails.ContainsKey("Prec") ? Convert.ToInt32(colDetails["Prec"]) : 0; var scale = colDetails.ContainsKey("Scale") ? Convert.ToInt32(colDetails["Scale"]) : 0; var collation = colDetails.ContainsKey("Collation") ? (string)colDetails["Collation"] : ""; bool isIdentity = colDetails.ContainsKey("IsIdentity") && Convert.ToBoolean(col["IsIdentity"]); var destinationType = columnsTypeConverter.GetDestinationType(type, ref length, ref prec, ref scale).ColumnTypeName(length); columns.Add(columnFactory.CreateInstance(destinationType, colName, length, prec, scale, isNullable, isIdentity, def, collation)); }
public abstract ITableDefinition GetTableDefinition(IColumnTypeConverter columnTypeConverter, string name);
public override ITableDefinition GetTableDefinition(IColumnTypeConverter columnTypeConverter, string tableName) { string selectStmt = ""; selectStmt += "SELECT c.name, " + "\n"; selectStmt += " t.name AS t_name, " + "\n"; selectStmt += " isnull(c.max_length, 0) as length, " + "\n"; selectStmt += " isnull(c.precision, 0) as prec, " + "\n"; selectStmt += " isnull(c.scale, 0) as scale, " + "\n"; selectStmt += " c.is_nullable, " + "\n"; selectStmt += " convert(varchar(256), isnull(c.collation_name, '')) as collation, " + "\n"; selectStmt += " isnull(object_definition(c.default_object_id), '') as def, " + "\n"; selectStmt += " c.is_identity " + "\n"; selectStmt += "FROM sys.columns c " + "\n"; selectStmt += " JOIN sys.types t " + "\n"; selectStmt += " ON c.user_type_id = t.user_type_id " + "\n"; selectStmt += string.Format("WHERE c.object_id = Object_id('{0}') ", tableName) + "\n"; selectStmt += "ORDER BY c.column_id "; IColumnFactory columnFactory = DbContext.PowerPlant.CreateColumnFactory(); List <IColumn> columns = new List <IColumn>(); bool tableHasBlobColumn = false; IDataCursor cursor = null; try { cursor = DbContext.PowerPlant.CreateDataCursor(); IDataReader reader = cursor.ExecuteReader(selectStmt); while (reader.Read()) { string name = reader.GetString(0); string type = reader.GetString(1); int length = reader.GetInt16(2); if (length != -1 && (type == "nvarchar" || type == "nchar")) { length /= 2; } int prec = reader.GetByte(3); int scale = reader.GetByte(4); if (scale != 0 && type == "datetime2") { prec = 0; } bool isNullable = reader.GetBoolean(5); string collation = reader.GetString(6); string def = reader.GetString(7); bool isIdentity = reader.GetBoolean(8); string sourceType = type.AddParameters(); ColumnTypeName colType = columnTypeConverter.GetDestinationType(sourceType, ref length, ref prec, ref scale).ColumnTypeName(length); if (colType == ColumnTypeName.Blob || colType == ColumnTypeName.OldBlob) { tableHasBlobColumn = true; } columns.Add( columnFactory.CreateInstance( colType, name, length, prec, scale, isNullable, isIdentity, def, collation) ); } } catch (Exception ex) { throw new ADatabaseException("ERROR when Get TableDefinition: " + selectStmt, ex); } finally { if (cursor != null) { cursor.Close(); } } ITableDefinition tableDefinition = DbContext.PowerPlant.CreateTableDefinition(tableName, columns, GetSegmentName(tableName)); tableDefinition.HasBlobColumn = tableHasBlobColumn; tableDefinition.Columns.RemoveAll(c => c.Name == "agrtid"); return(tableDefinition); }