public void ValidateColumns(Dialect.Dialect dialect, IMapping mapping, ITableMetadata tableInfo) { IEnumerable <Column> iter = ColumnIterator; foreach (Column column in iter) { IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name); if (columnInfo == null) { throw new HibernateException(string.Format("Missing column: {0} in {1}", column.Name, Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name))); } else { //TODO: Add new method to ColumnMetadata :getTypeCode bool typesMatch = column.GetSqlType(dialect, mapping).ToLower().StartsWith(columnInfo.TypeName.ToLower()); //|| columnInfo.get() == column.GetSqlTypeCode(mapping); if (!typesMatch) { throw new HibernateException(string.Format("Wrong column type in {0} for column {1}. Found: {2}, Expected {3}", Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name), column.Name, columnInfo.TypeName.ToLower(), column.GetSqlType(dialect, mapping))); } } } }
public string[] SqlAlterStrings(Dialect.Dialect dialect, IMapping p, ITableMetadata tableInfo, string defaultCatalog, string defaultSchema) { StringBuilder root = new StringBuilder("alter table ").Append(GetQualifiedName(dialect, defaultCatalog, defaultSchema)).Append(' '). Append(dialect.AddColumnString); var results = new List <string>(ColumnSpan); foreach (Column column in ColumnIterator) { IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name); if (columnInfo != null) { continue; } // the column doesnt exist at all. StringBuilder alter = new StringBuilder(root.ToString()).Append(' ').Append(column.GetQuotedName(dialect)).Append(' ').Append( column.GetSqlType(dialect, p)); string defaultValue = column.DefaultValue; if (!string.IsNullOrEmpty(defaultValue)) { alter.Append(" default ").Append(defaultValue); if (column.IsNullable) { alter.Append(dialect.NullColumnString); } else { alter.Append(" not null"); } } bool useUniqueConstraint = column.Unique && dialect.SupportsUnique && (!column.IsNullable || dialect.SupportsNotNullUnique); if (useUniqueConstraint) { alter.Append(" unique"); } if (column.HasCheckConstraint && dialect.SupportsColumnCheck) { alter.Append(" check(").Append(column.CheckConstraint).Append(") "); } string columnComment = column.Comment; if (columnComment != null) { alter.Append(dialect.GetColumnComment(columnComment)); } results.Add(alter.ToString()); } return(results.ToArray()); }
private void AddProperties(@class clazz, string entity, ITableMetadata tableMetaData) { var columnSet = currentContext.Schema.GetColumns(tableMetaData.Catalog, tableMetaData.Schema, tableMetaData.Name, null); int nameOrdinal = columnSet.Columns.IndexOf(COLUMN_NAME); int typeOrdinal = columnSet.Columns.IndexOf(DATA_TYPE); foreach (DataRow row in columnSet.Rows) { var cInfo = tableMetaData.GetColumnMetadata(row.ItemArray[nameOrdinal].ToString()); property p = currentContext.Model.AddPropertyToEntity(entity , currentContext.NamingStrategy.GetPropertyNameFromColumnName(cInfo.Name)); p.notnull = !true.ParseFromDb(cInfo.Nullable); p.notnullSpecified = !true.ParseFromDb(cInfo.Nullable); p.column = 0 == string.Compare(p.name, cInfo.Name, true)?null:cInfo.Name; p.type1 = typeConverter.GetNHType(cInfo); if (p.type1 == null) { logger.Warn(string.Format("No NHibernate type defined for dbtype:{0} len:{1}", cInfo.TypeName, cInfo.ColumnSize)); } if (cInfo.ColumnSize > 0) { p.length = cInfo.ColumnSize.ToString(); } if (cInfo.NumericalPrecision > 0) { p.precision = cInfo.NumericalPrecision.ToString(); } } }
private void AddProperties(@class clazz,string entity, ITableMetadata tableMetaData) { var columnSet = currentContext.Schema.GetColumns(tableMetaData.Catalog, tableMetaData.Schema, tableMetaData.Name, null); int nameOrdinal = columnSet.Columns.IndexOf(COLUMN_NAME); int typeOrdinal = columnSet.Columns.IndexOf(DATA_TYPE); foreach (DataRow row in columnSet.Rows) { var cInfo = tableMetaData.GetColumnMetadata(row.ItemArray[nameOrdinal].ToString()); property p = currentContext.Model.AddPropertyToEntity(entity ,currentContext.NamingStrategy.GetPropertyNameFromColumnName(cInfo.Name)); p.notnull = !true.ParseFromDb(cInfo.Nullable); p.notnullSpecified = !true.ParseFromDb(cInfo.Nullable); p.column = 0 == string.Compare(p.name,cInfo.Name,true)?null:cInfo.Name; p.type1 = typeConverter.GetNHType(cInfo); if (p.type1 == null) { logger.Warn(string.Format("No NHibernate type defined for dbtype:{0} len:{1}", cInfo.TypeName, cInfo.ColumnSize)); } if (cInfo.ColumnSize > 0) { p.length = cInfo.ColumnSize.ToString(); } if (cInfo.NumericalPrecision > 0) { p.precision = cInfo.NumericalPrecision.ToString(); } } }
public IEnumerable <string> ValidateColumns(Dialect.Dialect dialect, IMapping mapping, ITableMetadata tableInfo) { var validationErrors = new List <string>(); IEnumerable <Column> iter = ColumnIterator; foreach (Column column in iter) { IColumnMetadata columnInfo = tableInfo.GetColumnMetadata(column.Name); if (columnInfo == null) { validationErrors.Add( string.Format( "Missing column: {0} in {1}", column.Name, dialect.Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name))); continue; } //TODO: Add new method to ColumnMetadata :getTypeCode bool typesMatch = column.GetSqlType(dialect, mapping).StartsWith(columnInfo.TypeName, StringComparison.OrdinalIgnoreCase); //|| columnInfo.get() == column.GetSqlTypeCode(mapping); if (!typesMatch) { validationErrors.Add( string.Format( "Wrong column type in {0} for column {1}. Found: {2}, Expected {3}", dialect.Qualify(tableInfo.Catalog, tableInfo.Schema, tableInfo.Name), column.Name, columnInfo.TypeName.ToLowerInvariant(), column.GetSqlType(dialect, mapping))); } } return(validationErrors); }
public void InquireSchema() { Dialect.MsSql2005Dialect dialect = new MsSql2005Dialect(); DbConnection conn = new SqlConnection(@""); conn.Open(); DatabaseMetadata meta = new DatabaseMetadata(conn, dialect); IDataBaseSchema schema = dialect.GetDataBaseSchema(conn); var dt = schema.GetTables(null, null, null, new string[0]); var cols = schema.GetColumns(null, null, null, null); var keys = schema.GetForeignKeys(null, null, null); foreach (DataRow r in dt.Rows) { var tableMeta = schema.GetTableMetadata(r, true); Console.WriteLine(string.Format("Table {2}:[{0}].[{1}]", tableMeta.Schema, tableMeta.Name, tableMeta.Catalog)); ITableMetadata tm = meta.GetTableMetadata(tableMeta.Name, tableMeta.Schema, tableMeta.Catalog, false); IColumnMetadata col = tm.GetColumnMetadata(cols.Rows[0].ItemArray[2] as string); } }