github.com/eden-framework/sqlx@v0.0.2/mysqlconnector/schemas.go (about)

     1  package mysqlconnector
     2  
     3  import (
     4  	"github.com/eden-framework/sqlx"
     5  	"github.com/eden-framework/sqlx/builder"
     6  )
     7  
     8  func toInterfaces(list ...string) []interface{} {
     9  	s := make([]interface{}, len(list))
    10  	for i, v := range list {
    11  		s[i] = v
    12  	}
    13  	return s
    14  }
    15  
    16  func dbFromInformationSchema(db sqlx.DBExecutor) *sqlx.Database {
    17  	d := db.D()
    18  	tableNames := d.Tables.TableNames()
    19  
    20  	database := sqlx.NewDatabase(d.Name)
    21  
    22  	tableColumnSchema := SchemaDatabase.T(&ColumnSchema{})
    23  	columnSchemaList := make([]ColumnSchema, 0)
    24  
    25  	err := db.QueryExprAndScan(
    26  		builder.Select(tableColumnSchema.Columns.Clone()).
    27  			From(tableColumnSchema,
    28  				builder.Where(
    29  					builder.And(
    30  						tableColumnSchema.F("TABLE_SCHEMA").Eq(database.Name),
    31  						tableColumnSchema.F("TABLE_NAME").In(toInterfaces(tableNames...)...),
    32  					),
    33  				),
    34  			),
    35  		&columnSchemaList,
    36  	)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  
    41  	for _, columnSchema := range columnSchemaList {
    42  		table := database.Table(columnSchema.TABLE_NAME)
    43  		if table == nil {
    44  			table = builder.T(columnSchema.TABLE_NAME)
    45  			database.AddTable(table)
    46  		}
    47  		col := builder.Col(columnSchema.COLUMN_NAME)
    48  		table.AddCol(col)
    49  	}
    50  
    51  	if tableColumnSchema.Columns.Len() != 0 {
    52  		tableIndexSchema := SchemaDatabase.T(&IndexSchema{})
    53  
    54  		indexList := make([]IndexSchema, 0)
    55  
    56  		err = db.QueryExprAndScan(
    57  			builder.Select(tableIndexSchema.Columns.Clone()).
    58  				From(
    59  					tableIndexSchema,
    60  					builder.Where(
    61  						builder.And(
    62  							tableIndexSchema.F("TABLE_SCHEMA").Eq(database.Name),
    63  							tableIndexSchema.F("TABLE_NAME").In(toInterfaces(tableNames...)...),
    64  						),
    65  					),
    66  					builder.OrderBy(
    67  						builder.AscOrder(tableIndexSchema.F("INDEX_NAME")),
    68  						builder.AscOrder(tableIndexSchema.F("SEQ_IN_INDEX")),
    69  					),
    70  				),
    71  			&indexList,
    72  		)
    73  
    74  		if err != nil {
    75  			panic(err)
    76  		}
    77  
    78  		for _, indexSchema := range indexList {
    79  			table := database.Table(indexSchema.TABLE_NAME)
    80  
    81  			if key := table.Keys.Key(indexSchema.INDEX_NAME); key != nil {
    82  				key.Columns.Add(table.Col(indexSchema.COLUMN_NAME))
    83  			} else {
    84  				key := &builder.Key{}
    85  				key.Name = indexSchema.INDEX_NAME
    86  				key.Method = indexSchema.INDEX_TYPE
    87  				key.IsUnique = indexSchema.NON_UNIQUE == 0
    88  				key.Columns, _ = table.Cols(indexSchema.COLUMN_NAME)
    89  				table.AddKey(key)
    90  			}
    91  		}
    92  	}
    93  
    94  	return database
    95  }
    96  
    97  var SchemaDatabase = sqlx.NewDatabase("INFORMATION_SCHEMA")
    98  
    99  func init() {
   100  	SchemaDatabase.Register(&ColumnSchema{})
   101  	SchemaDatabase.Register(&IndexSchema{})
   102  }
   103  
   104  type ColumnSchema struct {
   105  	TABLE_SCHEMA string `db:"TABLE_SCHEMA"`
   106  	TABLE_NAME   string `db:"TABLE_NAME"`
   107  	COLUMN_NAME  string `db:"COLUMN_NAME"`
   108  }
   109  
   110  func (ColumnSchema) TableName() string {
   111  	return "INFORMATION_SCHEMA.COLUMNS"
   112  }
   113  
   114  type IndexSchema struct {
   115  	TABLE_SCHEMA string `db:"TABLE_SCHEMA"`
   116  	TABLE_NAME   string `db:"TABLE_NAME"`
   117  	NON_UNIQUE   int32  `db:"NON_UNIQUE"`
   118  	INDEX_NAME   string `db:"INDEX_NAME"`
   119  	SEQ_IN_INDEX int32  `db:"SEQ_IN_INDEX"`
   120  	COLUMN_NAME  string `db:"COLUMN_NAME"`
   121  	INDEX_TYPE   string `db:"INDEX_TYPE"`
   122  }
   123  
   124  func (IndexSchema) TableName() string {
   125  	return "INFORMATION_SCHEMA.STATISTICS"
   126  }