using System; using System.Data; using System.Data.Common; using Iesi.Collections.Generic; namespace NHibernate.Dialect.Schema { /// /// Metadata support for connections using the iAnywhere.Data.SQLAnywhere /// ADO.NET provider for SQL Anywhere 11 - for the NHibernate 3.2.0 distribution /// /// Contact: http://iablog.sybase.com/paulley /// /// This NHibernate dialect for SQL Anywhere 12 is a contribution to the NHibernate /// open-source project. It is intended to be included in the NHibernate /// distribution and is licensed under LGPL. /// /// This library is free software; you can redistribute it and/or /// modify it under the terms of the GNU Lesser General Public /// License as published by the Free Software Foundation; either /// version 2.1 of the License, or (at your option) any later version. /// /// This library is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU /// Lesser General Public License for more details. /// /// You should have received a copy of the GNU Lesser General Public /// License along with this library; if not, write to the Free Software /// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA /// /// /// /// The SQLAnywhere11DataBaseMetaData, SQLAnywhere11TableMetaData, and the like offer /// support for the .NET metadata APIs from within NHibernate. This file is now versioned /// with "SQL Anywhere 11" to accomodate API changes with later SQL Anywhere releases. /// public class SybaseSQLAnywhere11DataBaseMetaData : AbstractDataBaseSchema { public SybaseSQLAnywhere11DataBaseMetaData(DbConnection connection) : base( connection ) { } public override ITableMetadata GetTableMetadata(DataRow rs, bool extras) { return new SybaseSQLAnywhere11TableMetaData(rs, this, extras); } public override DataTable GetTables(string catalog, string schemaPattern, string tableNamePattern, string[] types) { var restrictions = new[] {schemaPattern, tableNamePattern, null}; DataTable objTbl = Connection.GetSchema("Tables", restrictions); return objTbl; } public override DataTable GetIndexInfo(string catalog, string schemaPattern, string tableName) { var restrictions = new[] {schemaPattern, tableName, null}; DataTable objTbl = Connection.GetSchema("Indexes", restrictions); return objTbl; } public override DataTable GetIndexColumns(string catalog, string schemaPattern, string tableName, string indexName) { var restrictions = new[] {schemaPattern, tableName, indexName, null}; DataTable objTbl = Connection.GetSchema("IndexColumns", restrictions); return objTbl; } public override DataTable GetColumns(string catalog, string schemaPattern, string tableNamePattern, string columnNamePattern) { var restrictions = new[] {schemaPattern, tableNamePattern, null}; DataTable objTbl = Connection.GetSchema("Columns", restrictions); return objTbl; } public override DataTable GetForeignKeys(string catalog, string schema, string table) { var restrictions = new[] {schema, table, null}; DataTable objTbl = Connection.GetSchema("ForeignKeys", restrictions); return objTbl; } public override ISet GetReservedWords() { var result = new HashedSet(); DataTable dtReservedWords = Connection.GetSchema(DbMetaDataCollectionNames.ReservedWords); foreach (DataRow row in dtReservedWords.Rows) { result.Add(row["reserved_word"].ToString()); } return result; } } public class SybaseSQLAnywhere11TableMetaData : AbstractTableMetadata { public SybaseSQLAnywhere11TableMetaData(DataRow rs, IDataBaseSchema meta, bool extras) : base(rs, meta, extras) {} protected override IColumnMetadata GetColumnMetadata(DataRow rs) { return new SybaseSQLAnywhere11ColumnMetaData(rs); } protected override string GetColumnName(DataRow rs) { return Convert.ToString(rs["COLUMN_NAME"]); } protected override string GetConstraintName(DataRow rs) { // The ADO provider for SQL Anywhere 11 does not support constraint names. // Hence we simply return the column name here ... return Convert.ToString(rs["COLUMN_NAME"]); } protected override IForeignKeyMetadata GetForeignKeyMetadata(DataRow rs) { return new SybaseSQLAnywhere11ForeignKeyMetaData(rs); } protected override IIndexMetadata GetIndexMetadata(DataRow rs) { return new SybaseSQLAnywhere11IndexMetaData(rs); } protected override string GetIndexName(DataRow rs) { return (string) rs["INDEX_NAME"]; } protected override void ParseTableInfo(DataRow rs) { Catalog = null; Schema = Convert.ToString(rs["TABLE_SCHEMA"]); if (string.IsNullOrEmpty(Schema)) { Schema = null; } Name = Convert.ToString(rs["TABLE_NAME"]); } } public class SybaseSQLAnywhere11ColumnMetaData : AbstractColumnMetaData { public SybaseSQLAnywhere11ColumnMetaData(DataRow rs) : base(rs) { Name = Convert.ToString(rs["COLUMN_NAME"]); object objValue = rs["COLUMN_SIZE"]; if (objValue != DBNull.Value) { ColumnSize = Convert.ToInt32(objValue); } objValue = rs["PRECISION"]; if (objValue != DBNull.Value) { NumericalPrecision = Convert.ToInt32(objValue); } Nullable = Convert.ToString(rs["IS_NULLABLE"]); TypeName = Convert.ToString(rs["DATA_TYPE"]); } } public class SybaseSQLAnywhere11IndexMetaData : AbstractIndexMetadata { public SybaseSQLAnywhere11IndexMetaData(DataRow rs) : base(rs) { Name = (string) rs["INDEX_NAME"]; } } public class SybaseSQLAnywhere11ForeignKeyMetaData : AbstractForeignKeyMetadata { public SybaseSQLAnywhere11ForeignKeyMetaData(DataRow rs) : base(rs) { // SQL Anywhere 11 does support constraint names but the version 11 ADO.NET // provider does not offer an ability to retrieve them... so we merely // return the column name. Name = (string) rs["COLUMN_NAME"]; } } }