public PrimaryKeySchema GetTablePrimaryKey(string connectionString, TableSchema table) { using (IVistaDBDatabase vistaDb = GetDatabase(connectionString)) { if (vistaDb == null) { return(null); } IVistaDBTableSchema vistaTable = vistaDb.TableSchema(table.Name); if (vistaTable == null) { return(null); } foreach (IVistaDBIndexInformation vistaIndex in vistaTable.Indexes) { if (!vistaIndex.Primary) { continue; } var key = new PrimaryKeySchema( table, vistaIndex.Name, vistaIndex.KeyExpression.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)); return(key); } } return(null); }
public PrimaryKeySchema GetTablePrimaryKey(string connectionString, TableSchema table) { PrimaryKeySchema result = null; using (NpgsqlConnection npgsqlConnection = new NpgsqlConnection(connectionString)) { var npgsqlCommand = npgsqlConnection.CreateCommand(); npgsqlCommand.CommandText = string.Format("select constraint_name from information_schema.table_constraints where constraint_schema='public' and table_name='{0}' and constraint_type='PRIMARY KEY'", table.Name); if (npgsqlConnection.State != ConnectionState.Open) { npgsqlConnection.Open(); } var constraint_name = npgsqlCommand.ExecuteScalar().ToString(); string ColumnName_sql = string.Format("select px.conname as ConstraintName, att.attname as ColumnName from pg_constraint px inner join pg_class home on (home.oid = px.conrelid) left join pg_attribute att on (att.attrelid = px.conrelid AND att.attnum = ANY(px.conkey)) where (home.relname = '{0}') and px.contype = 'p'", table.Name); using (NpgsqlCommand npgsqlCommand2 = new NpgsqlCommand(ColumnName_sql, npgsqlConnection)) { using (NpgsqlDataReader npgsqlDataReader2 = npgsqlCommand2.ExecuteReader()) { List <string> list = new List <string>(); while (npgsqlDataReader2.Read()) { list.Add(npgsqlDataReader2.IsDBNull(1) ? string.Empty : npgsqlDataReader2.GetString(1)); } result = new PrimaryKeySchema(table, constraint_name, list.ToArray()); } } if (npgsqlConnection.State != ConnectionState.Closed) { npgsqlConnection.Close(); } } return(result); }
private void CheckPrimaryKey() { // This item is obfuscated and can not be translated. PrimaryKeySchema tablePrimaryKey; this.Database.ValidateProvider(); tablePrimaryKey = this.Database.Provider.GetTablePrimaryKey(this); this._primaryKey = tablePrimaryKey; if (this._primaryKeyChecked) { return; } }
public override void Refresh() { base.Refresh(); this._primaryKey = null; this._keys = null; this._indexes = null; this._columns = null; this._nonPrimaryKeyColumns = null; this._nonKeyColumns = null; this._foreignKeys = null; this._primaryKeys = null; this._primaryKeyChecked = false; base._extendedProperties = new ExtendedPropertyCollection(this._defaultExtendedProperties); }
/// <summary> /// 得到表中的主键数据表 /// </summary> /// <param name="PrimaryKey"></param> /// <returns></returns> private DataTable ConvertMemberColumnSchemaCollectionToDataTable(PrimaryKeySchema PrimaryKey) { DataTable dt = new DataTable(); dt.Columns.Add("IsPrimayColumns", typeof(bool)); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("AllowDBNull", typeof(bool)); dt.Columns.Add("DataType", typeof(DbType)); dt.Columns.Add("NativeType", typeof(string)); dt.Columns.Add("Precision", typeof(byte)); dt.Columns.Add("Scale", typeof(int)); dt.Columns.Add("Size", typeof(int)); dt.Columns.Add("SystemType", typeof(Type)); if (PrimaryKey != null) { foreach (MemberColumnSchema memberColumnSchema in PrimaryKey.MemberColumns) { DataRow row = dt.NewRow(); if (memberColumnSchema.IsPrimaryKeyMember) { row["IsPrimayColumns"] = true; } else { row["IsPrimayColumns"] = false; } row["Name"] = memberColumnSchema.Name; row["AllowDBNull"] = memberColumnSchema.AllowDBNull; row["DataType"] = memberColumnSchema.DataType; row["NativeType"] = memberColumnSchema.NativeType; row["Precision"] = memberColumnSchema.Precision; row["Scale"] = memberColumnSchema.Scale; row["Size"] = memberColumnSchema.Size; row["SystemType"] = memberColumnSchema.SystemType; dt.Rows.Add(row); } } return(dt); }