new dialect interface

This commit is contained in:
Lunny Xiao 2014-04-23 15:23:03 +08:00
parent 05943b9679
commit aafdd49780
5 changed files with 46 additions and 13 deletions

View File

@ -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 : <tablename>-p-<pk1>-<pk2>...
Put(key string, value interface{}) error

9
db.go
View File

@ -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")

View File

@ -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

10
error.go Normal file
View File

@ -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")
)

View File

@ -18,6 +18,8 @@ type Table struct {
Updated string
Version string
Cacher Cacher
storeEngine string
charset string
}
func (table *Table) Columns() map[string]*Column {