コード例 #1
0
        public List <SchemaRow> GetSchema()
        {
            if (!string.IsNullOrEmpty(this.ColumnSchemaQuery))
            {
                if (this.ColumnSchemaQuery.IndexOf(this.TableNamePlaceHolder) == -1)
                {
                    throw new Exception("Required placeholder for table name: '" + this.TableNamePlaceHolder + "'.");
                }
            }

            List <SchemaRow> Schema = new List <SchemaRow>();

            using (Npgsql.NpgsqlConnection conn = new Npgsql.NpgsqlConnection(this.ConnStr)) {
                conn.Open();

                //Using single result set
                if (!string.IsNullOrEmpty(this.SchemaQuery))
                {
                    using (Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand()) {
                        cmd.Connection  = conn;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = this.SchemaQuery;

                        DataTable dtSchema = new DataTable();
                        using (Npgsql.NpgsqlDataAdapter da = new Npgsql.NpgsqlDataAdapter(cmd)) {
                            da.Fill(dtSchema);
                        }

                        conn.Close();

                        foreach (DataRow dr in dtSchema.Rows)
                        {
                            Schema.Add(this.SetColumnAttributes(dr["TABLE_NAME"].ToString(), dr["TABLE_TYPE"].ToString(), dr));
                        }

                        return(Schema);
                    }
                }

                //Way of the Table/Column

                //Retrieve table schema first
                List <SchemaRow> TableSchema = new List <SchemaRow>();
                if (!string.IsNullOrEmpty(this.TableSchemaQuery))
                {
                    //Using table schema query
                    using (Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand()) {
                        cmd.Connection  = conn;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = this.TableSchemaQuery;

                        DataTable dtTableSchema = new DataTable();
                        using (Npgsql.NpgsqlDataAdapter da = new Npgsql.NpgsqlDataAdapter(cmd)) {
                            da.Fill(dtTableSchema);
                        }

                        TableSchema = this.GetInitialTables(dtTableSchema);
                    }
                }
                else
                {
                    //Get by default using GetSchema
                    DataTable dtTableSchema = new DataTable();
                    dtTableSchema = conn.GetSchema(System.Data.SqlClient.SqlClientMetaDataCollectionNames.Tables);
                    TableSchema   = this.GetInitialTables(dtTableSchema);
                }


                //Get columns for each table
                if (!string.IsNullOrEmpty(this.ColumnSchemaQuery))
                {
                    //Use column schema query
                    foreach (SchemaRow tsr in TableSchema)
                    {
                        DataTable dtColumnSchema = new DataTable();

                        using (Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand()) {
                            cmd.Connection  = conn;
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = this.ColumnSchemaQuery.Replace(this.TableNamePlaceHolder, tsr.Name);

                            using (Npgsql.NpgsqlDataAdapter da = new Npgsql.NpgsqlDataAdapter(cmd)) {
                                da.Fill(dtColumnSchema);
                            }
                        }

                        //Get column schema
                        foreach (DataRow dr in dtColumnSchema.Rows)
                        {
                            Schema.Add(this.SetColumnAttributes(tsr.Name, tsr.Type, dr));
                        }
                    }
                }

                conn.Close();
            }

            return(Schema);
        }