DataTable IPlugin.GetTableIndexes(string database, string table) { DataTable metaData = new DataTable(); IVistaDBDatabase db = null; try { metaData = context.CreateIndexesDataTable(); using (VistaDBConnection cn = new VistaDBConnection(context.ConnectionString)) { db = DDA.OpenDatabase(this.GetFullDatabaseName(), VistaDBDatabaseOpenMode.NonexclusiveReadOnly, GetPassword(cn)); } IVistaDBTableNameCollection tables = db.GetTableNames(); IVistaDBTableSchema tblStructure = db.TableSchema(table); foreach (IVistaDBIndexInformation indexInfo in tblStructure.Indexes) { string[] pks = indexInfo.KeyExpression.Split(';'); int index = 0; foreach (string colName in pks) { DataRow row = metaData.NewRow(); metaData.Rows.Add(row); row["TABLE_CATALOG"] = GetDatabaseName(); row["TABLE_NAME"] = tblStructure.Name; row["INDEX_CATALOG"] = GetDatabaseName(); row["INDEX_NAME"] = indexInfo.Name; row["UNIQUE"] = indexInfo.Unique; row["COLLATION"] = indexInfo.KeyStructure[index++].Descending ? 2 : 1; row["COLUMN_NAME"] = colName; } } } finally { if (db != null) { db.Close(); } } return(metaData); }
DataTable IPlugin.GetTables(string database) { DataTable metaData = new DataTable(); IVistaDBDatabase db = null; try { metaData = context.CreateTablesDataTable(); using (VistaDBConnection cn = new VistaDBConnection(context.ConnectionString)) { db = DDA.OpenDatabase(this.GetFullDatabaseName(), VistaDBDatabaseOpenMode.NonexclusiveReadOnly, GetPassword(cn)); } IVistaDBTableNameCollection tables = db.GetTableNames(); foreach (string table in tables) { IVistaDBTableSchema tblStructure = db.TableSchema(table); DataRow row = metaData.NewRow(); metaData.Rows.Add(row); row["TABLE_NAME"] = tblStructure.Name; row["DESCRIPTION"] = tblStructure.Description; } } finally { if (db != null) { db.Close(); } } return(metaData); }
DataTable IPlugin.GetForeignKeys(string database, string tableName) { DataTable metaData = new DataTable(); IVistaDBDatabase db = null; try { metaData = context.CreateForeignKeysDataTable(); using (VistaDBConnection cn = new VistaDBConnection(context.ConnectionString)) { db = DDA.OpenDatabase(this.GetFullDatabaseName(), VistaDBDatabaseOpenMode.NonexclusiveReadOnly, GetPassword(cn)); } IVistaDBTableNameCollection tables = db.GetTableNames(); foreach (string table in tables) { IVistaDBTableSchema tblStructure = db.TableSchema(table); //================================================================== // This works around a change that was made to the VistaDB 4 provider // It's ugly, we know //================================================================== IEnumerator enumerator = null; if (useOldForeignKeyWay) { enumerator = tblStructure.ForeignKeys.GetEnumerator(); } else { try { enumerator = tblStructure.ForeignKeys.Values.GetEnumerator(); } catch { enumerator = tblStructure.ForeignKeys.GetEnumerator(); useOldForeignKeyWay = true; } } // Okay, now that the version issues are over we just use the 'enumerator' while (enumerator.MoveNext()) { IVistaDBRelationshipInformation relInfo = enumerator.Current as IVistaDBRelationshipInformation; if (relInfo.ForeignTable != tableName && relInfo.PrimaryTable != tableName) { continue; } string fCols = relInfo.ForeignKey; string pCols = String.Empty; string primaryTbl = relInfo.PrimaryTable; string pkName = ""; using (IVistaDBTableSchema pkTableStruct = db.TableSchema(primaryTbl)) { foreach (IVistaDBIndexInformation idxInfo in pkTableStruct.Indexes) { if (!idxInfo.Primary) { continue; } pkName = idxInfo.Name; pCols = idxInfo.KeyExpression; break; } } string [] fColumns = fCols.Split(';'); string [] pColumns = pCols.Split(';'); for (int i = 0; i < fColumns.GetLength(0); i++) { DataRow row = metaData.NewRow(); metaData.Rows.Add(row); row["PK_TABLE_CATALOG"] = GetDatabaseName(); row["PK_TABLE_SCHEMA"] = DBNull.Value; row["FK_TABLE_CATALOG"] = DBNull.Value; row["FK_TABLE_SCHEMA"] = DBNull.Value; row["FK_TABLE_NAME"] = tblStructure.Name; row["PK_TABLE_NAME"] = relInfo.PrimaryTable; row["ORDINAL"] = 0; row["FK_NAME"] = relInfo.Name; row["PK_NAME"] = pkName; row["PK_COLUMN_NAME"] = pColumns[i]; row["FK_COLUMN_NAME"] = fColumns[i]; row["UPDATE_RULE"] = relInfo.UpdateIntegrity; row["DELETE_RULE"] = relInfo.DeleteIntegrity; } } } } finally { if (db != null) { db.Close(); } } return(metaData); }
DataTable IPlugin.GetTableColumns(string database, string table) { DataTable metaData = new DataTable(); IVistaDBDatabase db = null; try { metaData = context.CreateColumnsDataTable(); using (VistaDBConnection cn = new VistaDBConnection(context.ConnectionString)) { db = DDA.OpenDatabase(this.GetFullDatabaseName(), VistaDBDatabaseOpenMode.NonexclusiveReadOnly, GetPassword(cn)); } IVistaDBTableNameCollection tables = db.GetTableNames(); IVistaDBTableSchema tblStructure = db.TableSchema(table); foreach (IVistaDBColumnAttributes c in tblStructure) { string colName = c.Name; string def = ""; if (tblStructure.DefaultValues[colName] != null) { def = tblStructure.DefaultValues[colName].Expression; } int width = c.MaxLength; //c.ColumnWidth; int dec = 0; //c.ColumnDecimals; int length = 0; int octLength = width; IVistaDBIdentityInformation identity = null; if (tblStructure.Identities[colName] != null) { identity = tblStructure.Identities[colName]; } string[] pks = null; if (tblStructure.Indexes["PrimaryKey"] != null) { pks = tblStructure.Indexes["PrimaryKey"].KeyExpression.Split(';'); } else { foreach (IVistaDBIndexInformation pk in tblStructure.Indexes) { if (pk.Primary) { pks = pk.KeyExpression.Split(';'); break; } } } System.Collections.Hashtable pkCols = null; if (pks != null) { pkCols = new Hashtable(); foreach (string pkColName in pks) { pkCols[pkColName] = true; } } switch (c.Type) { case VistaDBType.Char: case VistaDBType.NChar: case VistaDBType.NText: case VistaDBType.NVarChar: case VistaDBType.Text: case VistaDBType.VarChar: length = width; width = 0; dec = 0; break; case VistaDBType.Money: case VistaDBType.Float: case VistaDBType.Decimal: case VistaDBType.Real: break; default: width = 0; dec = 0; break; } DataRow row = metaData.NewRow(); metaData.Rows.Add(row); row["TABLE_NAME"] = tblStructure.Name; row["COLUMN_NAME"] = c.Name; row["ORDINAL_POSITION"] = c.RowIndex; row["IS_NULLABLE"] = c.AllowNull; row["COLUMN_HASDEFAULT"] = def == string.Empty ? false : true; row["COLUMN_DEFAULT"] = def; row["IS_AUTO_KEY"] = identity == null ? false : true; row["AUTO_KEY_SEED"] = 1; row["AUTO_KEY_INCREMENT"] = identity == null ? 0 : Convert.ToInt32(identity.StepExpression); row["TYPE_NAME"] = c.Type.ToString(); row["NUMERIC_PRECISION"] = width; row["NUMERIC_SCALE"] = dec; row["CHARACTER_MAXIMUM_LENGTH"] = length; row["CHARACTER_OCTET_LENGTH"] = octLength; row["DESCRIPTION"] = c.Description; string type = (string)row["TYPE_NAME"]; row["TYPE_NAME_COMPLETE"] = this.GetDataTypeNameComplete(type, length, (short)width, (short)dec); if (c.Type == VistaDBType.Timestamp) { row["IS_COMPUTED"] = true; } row["IS_CONCURRENCY"] = type == "Timestamp" ? true : false; } } finally { if (db != null) { db.Close(); } } return(metaData); }