Пример #1
0
 public void TestSchema()
 {
     MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
     conn.ConnectionString = "database=nhibernate;server=localhost;user id=root;password=mysqlbjdxcm2018;pooling=true;port=3306;allow zero datetime=true";
     conn.Open();
     var       table  = conn.GetSchema();
     DataTable tables = conn.GetSchema(DbMetaDataCollectionNames.ReservedWords);
 }
Пример #2
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 (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr)) {
                conn.Open();

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

                        DataTable dtSchema = new DataTable();
                        using (MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(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 (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand()) {
                        cmd.Connection  = conn;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = this.TableSchemaQuery;

                        DataTable dtTableSchema = new DataTable();
                        using (MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(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 (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand()) {
                            cmd.Connection  = conn;
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = this.ColumnSchemaQuery.Replace(this.TableNamePlaceHolder, tsr.Name);

                            using (MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(cmd)) {
                                da.Fill(dtColumnSchema);
                            }
                        }

                        //Get column schema
                        foreach (DataRow dr in dtColumnSchema.Rows)
                        {
                            Schema.Add(this.SetColumnAttributes(tsr.Name, tsr.Type, dr));
                        }
                    }
                }
                else
                {
                    //Get by default using DataReader GetSchemaTable
                    //I can't figure this out either. We will not use generic get schema.
                }

                conn.Close();
            }

            return(Schema);
        }