示例#1
0
        private static IList <DbTable> GetDbTables(string tableName, DbConnection dbConnection, string schema = "public")
        {
            IList <DbTable> list;

            using (DbCommand command = Transaction.CreateCommand(dbConnection, null))
            {
                string str1 = " where tables.table_schema = '" + schema + "'" + (!string.IsNullOrEmpty(tableName) ? " and tables.table_name ILIKE '" + tableName + "' " : string.Empty);
                command.CommandText = @"select tables.table_name, tables.table_schema, columns.column_name, columns.is_nullable, columns.udt_name, columns.column_default, 
key_usage.ordinal_position from information_schema.tables as tables join information_schema.columns as columns
on tables.table_catalog = columns.table_catalog and tables.table_schema = columns.table_schema and 
tables.table_name = columns.table_name left join information_schema.key_column_usage as key_usage on 
tables.table_catalog = key_usage.table_catalog and tables.table_schema = key_usage.table_schema and 
tables.table_name = key_usage.table_name and columns.column_name = key_usage.column_name "
                                      + str1 + "order by table_schema, table_name, columns.ordinal_position";
                command.Connection = dbConnection;
                DbDataReader dbDataReader = command.ExecuteReader();
                list = (IList <DbTable>) new List <DbTable>();
                Dictionary <string, DbTable>  dictionary1 = new Dictionary <string, DbTable>();
                Dictionary <string, DbColumn> dictionary2 = new Dictionary <string, DbColumn>();
                while (dbDataReader.Read())
                {
                    string  string1      = dbDataReader.GetString(0);
                    string  string2      = dbDataReader.GetString(1);
                    string  string3      = dbDataReader.GetString(2);
                    string  string4      = dbDataReader.GetString(3);
                    string  string5      = dbDataReader.GetString(4);
                    string  str2         = !dbDataReader.IsDBNull(5) ? dbDataReader.GetString(5) : string.Empty;
                    bool    isPrimaryKey = !dbDataReader.IsDBNull(6);
                    DbTable dbTable      = null;
                    if (!dictionary1.ContainsKey(string1))
                    {
                        dbTable = new DbTable(string1, string2, (IList <DbColumn>) new List <DbColumn>());
                        dictionary1.Add(string1, dbTable);
                        list.Add(dbTable);
                    }
                    else
                    {
                        dbTable = dictionary1[string1];
                    }
                    string key = string1 + "*" + string3;
                    if (!dictionary2.ContainsKey(key))
                    {
                        bool             pIsAutoGenerated = str2.StartsWith("nextval");
                        IList <DbColumn> columns          = dictionary1[string1].Columns;
                        DbColumn         dbColumn         = new DbColumn(string3, PostgresSchemaProvider.GetDataType(string5), string4 == "YES", isPrimaryKey, pIsAutoGenerated, false);
                        dbColumn.ColumnTypeName = DbColumnFactory.GetColumnTypeName(dbColumn.DbType, dbTable.Name, dbColumn.IsPrimaryKey, dbColumn.Nullable);
                        dbColumn.ReturnType     = DbColumnFactory.GetReturnType(dbColumn.DbType, dbColumn.Nullable);
                        columns.Add(dbColumn);
                        dictionary2.Add(key, dbColumn);
                    }
                    else if (isPrimaryKey)
                    {
                        dictionary2[key].IsPrimaryKey = true;
                    }
                }
            }
            dbConnection.Close();
            return(list);
        }
示例#2
0
 public IList <DbTable> GetTables(DbConnection dbConnection)
 {
     return(PostgresSchemaProvider.GetDbTables((string)null, dbConnection));
 }
示例#3
0
        public DbTable GetTable(DatabaseBase database, TableBase table)
        {
            IList <DbTable> tables = PostgresSchemaProvider.GetDbTables(table.Name, database.GetConnection(false));

            return(tables.Count > 0 ? tables[0] : null);
        }