diff --git a/cache.go b/cache.go index bedd22a7..b9bf6e0d 100644 --- a/cache.go +++ b/cache.go @@ -20,7 +20,7 @@ const ( // CacheStore is a interface to store cache type CacheStore interface { - // key is primary key or composite primary key or unique key's value + // key is primary key or composite primary key // value is struct's pointer // key format : -p--... Put(key string, value interface{}) error diff --git a/db.go b/db.go index ed97987a..1f70a926 100644 --- a/db.go +++ b/db.go @@ -8,11 +8,6 @@ import ( "sync" ) -var ( - ErrNoMapPointer = errors.New("mp should be a map's pointer") - ErrNoStructPointer = errors.New("mp should be a map's pointer") -) - func MapToSlice(query string, mp interface{}) (string, []interface{}, error) { vv := reflect.ValueOf(mp) if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map { @@ -252,7 +247,7 @@ type Rows struct { } // scan data to a struct's pointer according field index -func (rs *Rows) ScanStruct(dest ...interface{}) error { +func (rs *Rows) ScanStructByIndex(dest ...interface{}) error { if len(dest) == 0 { return errors.New("at least one struct") } @@ -319,7 +314,7 @@ func fieldByName(v reflect.Value, name string) reflect.Value { } // scan data to a struct's pointer according field name -func (rs *Rows) ScanStruct2(dest interface{}) error { +func (rs *Rows) ScanStructByName(dest interface{}) error { vv := reflect.ValueOf(dest) if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct { return errors.New("dest should be a struct's pointer") diff --git a/dialect.go b/dialect.go index 08a4a4c5..f419cdb6 100644 --- a/dialect.go +++ b/dialect.go @@ -30,6 +30,9 @@ type Dialect interface { DBType() DbType SqlType(*Column) string + DriverName() string + DataSourceName() string + QuoteStr() string AndStr() string OrStr() string @@ -43,21 +46,28 @@ type Dialect interface { IndexOnTable() bool ShowCreateNull() bool - DropTableSql(tableName string) string IndexCheckSql(tableName, idxName string) (string, []interface{}) TableCheckSql(tableName string) (string, []interface{}) - ColumnCheckSql(tableName, colName string, isPK bool) (string, []interface{}) + //ColumnCheckSql(tableName, colName string) (string, []interface{}) + + //IsTableExist(tableName string) (bool, error) + //IsIndexExist(tableName string, idx *Index) (bool, error) + IsColumnExist(tableName string, col *Column) (bool, error) + CreateTableSql(table *Table, tableName, storeEngine, charset string) string + DropTableSql(tableName string) string CreateIndexSql(tableName string, index *Index) string GetColumns(tableName string) ([]string, map[string]*Column, error) GetTables() ([]*Table, error) GetIndexes(tableName string) (map[string]*Index, error) - Filters() []Filter + // Get data from db cell to a struct's field + //GetData(col *Column, fieldValue *reflect.Value, cellData interface{}) error + // Set field data to db + //SetData(col *Column, fieldValue *refelct.Value) (interface{}, error) - DriverName() string - DataSourceName() string + Filters() []Filter } func OpenDialect(dialect Dialect) (*DB, error) { @@ -126,6 +136,22 @@ func (db *Base) DropTableSql(tableName string) string { return fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName) } +func (db *Base) IsColumnExist(tableName string, col *Column) (bool, error) { + args := []interface{}{db.DbName, tableName, col.Name} + query := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?" + + rows, err := db.DB().Query(query, args...) + if err != nil { + return false, err + } + defer rows.Close() + + if rows.Next() { + return true, nil + } + return false, ErrNotExist +} + func (db *Base) CreateIndexSql(tableName string, index *Index) string { quote := db.Quote var unique string diff --git a/error.go b/error.go new file mode 100644 index 00000000..b79f188e --- /dev/null +++ b/error.go @@ -0,0 +1,10 @@ +package core + +import "errors" + +var ( + ErrNoMapPointer = errors.New("mp should be a map's pointer") + ErrNoStructPointer = errors.New("mp should be a map's pointer") + ErrNotExist = errors.New("Not exist") + ErrIgnore = errors.New("Ignore") +) diff --git a/table.go b/table.go index aadcc9a8..9ccae74e 100644 --- a/table.go +++ b/table.go @@ -18,6 +18,8 @@ type Table struct { Updated string Version string Cacher Cacher + storeEngine string + charset string } func (table *Table) Columns() map[string]*Column {