f5a5ebe1 by 傅小黑

add comments for orm packages, part 1

1 parent 32799bc2
...@@ -16,6 +16,7 @@ var ( ...@@ -16,6 +16,7 @@ var (
16 commands = make(map[string]commander) 16 commands = make(map[string]commander)
17 ) 17 )
18 18
19 // print help.
19 func printHelp(errs ...string) { 20 func printHelp(errs ...string) {
20 content := `orm command usage: 21 content := `orm command usage:
21 22
...@@ -31,6 +32,7 @@ func printHelp(errs ...string) { ...@@ -31,6 +32,7 @@ func printHelp(errs ...string) {
31 os.Exit(2) 32 os.Exit(2)
32 } 33 }
33 34
35 // listen for orm command and then run it if command arguments passed.
34 func RunCommand() { 36 func RunCommand() {
35 if len(os.Args) < 2 || os.Args[1] != "orm" { 37 if len(os.Args) < 2 || os.Args[1] != "orm" {
36 return 38 return
...@@ -58,6 +60,7 @@ func RunCommand() { ...@@ -58,6 +60,7 @@ func RunCommand() {
58 } 60 }
59 } 61 }
60 62
63 // sync database struct command interface.
61 type commandSyncDb struct { 64 type commandSyncDb struct {
62 al *alias 65 al *alias
63 force bool 66 force bool
...@@ -66,6 +69,7 @@ type commandSyncDb struct { ...@@ -66,6 +69,7 @@ type commandSyncDb struct {
66 rtOnError bool 69 rtOnError bool
67 } 70 }
68 71
72 // parse orm command line arguments.
69 func (d *commandSyncDb) Parse(args []string) { 73 func (d *commandSyncDb) Parse(args []string) {
70 var name string 74 var name string
71 75
...@@ -78,6 +82,7 @@ func (d *commandSyncDb) Parse(args []string) { ...@@ -78,6 +82,7 @@ func (d *commandSyncDb) Parse(args []string) {
78 d.al = getDbAlias(name) 82 d.al = getDbAlias(name)
79 } 83 }
80 84
85 // run orm line command.
81 func (d *commandSyncDb) Run() error { 86 func (d *commandSyncDb) Run() error {
82 var drops []string 87 var drops []string
83 if d.force { 88 if d.force {
...@@ -208,10 +213,12 @@ func (d *commandSyncDb) Run() error { ...@@ -208,10 +213,12 @@ func (d *commandSyncDb) Run() error {
208 return nil 213 return nil
209 } 214 }
210 215
216 // database creation commander interface implement.
211 type commandSqlAll struct { 217 type commandSqlAll struct {
212 al *alias 218 al *alias
213 } 219 }
214 220
221 // parse orm command line arguments.
215 func (d *commandSqlAll) Parse(args []string) { 222 func (d *commandSqlAll) Parse(args []string) {
216 var name string 223 var name string
217 224
...@@ -222,6 +229,7 @@ func (d *commandSqlAll) Parse(args []string) { ...@@ -222,6 +229,7 @@ func (d *commandSqlAll) Parse(args []string) {
222 d.al = getDbAlias(name) 229 d.al = getDbAlias(name)
223 } 230 }
224 231
232 // run orm line command.
225 func (d *commandSqlAll) Run() error { 233 func (d *commandSqlAll) Run() error {
226 sqls, indexes := getDbCreateSql(d.al) 234 sqls, indexes := getDbCreateSql(d.al)
227 var all []string 235 var all []string
...@@ -243,6 +251,10 @@ func init() { ...@@ -243,6 +251,10 @@ func init() {
243 commands["sqlall"] = new(commandSqlAll) 251 commands["sqlall"] = new(commandSqlAll)
244 } 252 }
245 253
254 // run syncdb command line.
255 // name means table's alias name. default is "default".
256 // force means run next sql if the current is error.
257 // verbose means show all info when running command or not.
246 func RunSyncdb(name string, force bool, verbose bool) error { 258 func RunSyncdb(name string, force bool, verbose bool) error {
247 BootStrap() 259 BootStrap()
248 260
......
...@@ -12,6 +12,7 @@ type dbIndex struct { ...@@ -12,6 +12,7 @@ type dbIndex struct {
12 Sql string 12 Sql string
13 } 13 }
14 14
15 // create database drop sql.
15 func getDbDropSql(al *alias) (sqls []string) { 16 func getDbDropSql(al *alias) (sqls []string) {
16 if len(modelCache.cache) == 0 { 17 if len(modelCache.cache) == 0 {
17 fmt.Println("no Model found, need register your model") 18 fmt.Println("no Model found, need register your model")
...@@ -26,6 +27,7 @@ func getDbDropSql(al *alias) (sqls []string) { ...@@ -26,6 +27,7 @@ func getDbDropSql(al *alias) (sqls []string) {
26 return sqls 27 return sqls
27 } 28 }
28 29
30 // get database column type string.
29 func getColumnTyp(al *alias, fi *fieldInfo) (col string) { 31 func getColumnTyp(al *alias, fi *fieldInfo) (col string) {
30 T := al.DbBaser.DbTypes() 32 T := al.DbBaser.DbTypes()
31 fieldType := fi.fieldType 33 fieldType := fi.fieldType
...@@ -79,6 +81,7 @@ checkColumn: ...@@ -79,6 +81,7 @@ checkColumn:
79 return 81 return
80 } 82 }
81 83
84 // create alter sql string.
82 func getColumnAddQuery(al *alias, fi *fieldInfo) string { 85 func getColumnAddQuery(al *alias, fi *fieldInfo) string {
83 Q := al.DbBaser.TableQuote() 86 Q := al.DbBaser.TableQuote()
84 typ := getColumnTyp(al, fi) 87 typ := getColumnTyp(al, fi)
...@@ -90,6 +93,7 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string { ...@@ -90,6 +93,7 @@ func getColumnAddQuery(al *alias, fi *fieldInfo) string {
90 return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ) 93 return fmt.Sprintf("ALTER TABLE %s%s%s ADD COLUMN %s%s%s %s", Q, fi.mi.table, Q, Q, fi.column, Q, typ)
91 } 94 }
92 95
96 // create database creation string.
93 func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex) { 97 func getDbCreateSql(al *alias) (sqls []string, tableIndexes map[string][]dbIndex) {
94 if len(modelCache.cache) == 0 { 98 if len(modelCache.cache) == 0 {
95 fmt.Println("no Model found, need register your model") 99 fmt.Println("no Model found, need register your model")
......
...@@ -15,7 +15,7 @@ const ( ...@@ -15,7 +15,7 @@ const (
15 ) 15 )
16 16
17 var ( 17 var (
18 ErrMissPK = errors.New("missed pk value") 18 ErrMissPK = errors.New("missed pk value") // missing pk error
19 ) 19 )
20 20
21 var ( 21 var (
...@@ -45,12 +45,15 @@ var ( ...@@ -45,12 +45,15 @@ var (
45 } 45 }
46 ) 46 )
47 47
48 // an instance of dbBaser interface/
48 type dbBase struct { 49 type dbBase struct {
49 ins dbBaser 50 ins dbBaser
50 } 51 }
51 52
53 // check dbBase implements dbBaser interface.
52 var _ dbBaser = new(dbBase) 54 var _ dbBaser = new(dbBase)
53 55
56 // get struct columns values as interface slice.
54 func (d *dbBase) collectValues(mi *modelInfo, ind reflect.Value, cols []string, skipAuto bool, insert bool, names *[]string, tz *time.Location) (values []interface{}, err error) { 57 func (d *dbBase) collectValues(mi *modelInfo, ind reflect.Value, cols []string, skipAuto bool, insert bool, names *[]string, tz *time.Location) (values []interface{}, err error) {
55 var columns []string 58 var columns []string
56 59
...@@ -87,6 +90,7 @@ func (d *dbBase) collectValues(mi *modelInfo, ind reflect.Value, cols []string, ...@@ -87,6 +90,7 @@ func (d *dbBase) collectValues(mi *modelInfo, ind reflect.Value, cols []string,
87 return 90 return
88 } 91 }
89 92
93 // get one field value in struct column as interface.
90 func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Value, insert bool, tz *time.Location) (interface{}, error) { 94 func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Value, insert bool, tz *time.Location) (interface{}, error) {
91 var value interface{} 95 var value interface{}
92 if fi.pk { 96 if fi.pk {
...@@ -155,6 +159,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val ...@@ -155,6 +159,7 @@ func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Val
155 return value, nil 159 return value, nil
156 } 160 }
157 161
162 // create insert sql preparation statement object.
158 func (d *dbBase) PrepareInsert(q dbQuerier, mi *modelInfo) (stmtQuerier, string, error) { 163 func (d *dbBase) PrepareInsert(q dbQuerier, mi *modelInfo) (stmtQuerier, string, error) {
159 Q := d.ins.TableQuote() 164 Q := d.ins.TableQuote()
160 165
...@@ -180,6 +185,7 @@ func (d *dbBase) PrepareInsert(q dbQuerier, mi *modelInfo) (stmtQuerier, string, ...@@ -180,6 +185,7 @@ func (d *dbBase) PrepareInsert(q dbQuerier, mi *modelInfo) (stmtQuerier, string,
180 return stmt, query, err 185 return stmt, query, err
181 } 186 }
182 187
188 // insert struct with prepared statement and given struct reflect value.
183 func (d *dbBase) InsertStmt(stmt stmtQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) { 189 func (d *dbBase) InsertStmt(stmt stmtQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) {
184 values, err := d.collectValues(mi, ind, mi.fields.dbcols, true, true, nil, tz) 190 values, err := d.collectValues(mi, ind, mi.fields.dbcols, true, true, nil, tz)
185 if err != nil { 191 if err != nil {
...@@ -200,6 +206,7 @@ func (d *dbBase) InsertStmt(stmt stmtQuerier, mi *modelInfo, ind reflect.Value, ...@@ -200,6 +206,7 @@ func (d *dbBase) InsertStmt(stmt stmtQuerier, mi *modelInfo, ind reflect.Value,
200 } 206 }
201 } 207 }
202 208
209 // query sql ,read records and persist in dbBaser.
203 func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string) error { 210 func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string) error {
204 var whereCols []string 211 var whereCols []string
205 var args []interface{} 212 var args []interface{}
...@@ -259,6 +266,7 @@ func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Lo ...@@ -259,6 +266,7 @@ func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Lo
259 return nil 266 return nil
260 } 267 }
261 268
269 // execute insert sql dbQuerier with given struct reflect.Value.
262 func (d *dbBase) Insert(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) { 270 func (d *dbBase) Insert(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) {
263 names := make([]string, 0, len(mi.fields.dbcols)-1) 271 names := make([]string, 0, len(mi.fields.dbcols)-1)
264 values, err := d.collectValues(mi, ind, mi.fields.dbcols, true, true, &names, tz) 272 values, err := d.collectValues(mi, ind, mi.fields.dbcols, true, true, &names, tz)
...@@ -269,6 +277,7 @@ func (d *dbBase) Insert(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. ...@@ -269,6 +277,7 @@ func (d *dbBase) Insert(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
269 return d.InsertValue(q, mi, false, names, values) 277 return d.InsertValue(q, mi, false, names, values)
270 } 278 }
271 279
280 // multi-insert sql with given slice struct reflect.Value.
272 func (d *dbBase) InsertMulti(q dbQuerier, mi *modelInfo, sind reflect.Value, bulk int, tz *time.Location) (int64, error) { 281 func (d *dbBase) InsertMulti(q dbQuerier, mi *modelInfo, sind reflect.Value, bulk int, tz *time.Location) (int64, error) {
273 var ( 282 var (
274 cnt int64 283 cnt int64
...@@ -325,6 +334,8 @@ func (d *dbBase) InsertMulti(q dbQuerier, mi *modelInfo, sind reflect.Value, bul ...@@ -325,6 +334,8 @@ func (d *dbBase) InsertMulti(q dbQuerier, mi *modelInfo, sind reflect.Value, bul
325 return cnt, nil 334 return cnt, nil
326 } 335 }
327 336
337 // execute insert sql with given struct and given values.
338 // insert the given values, not the field values in struct.
328 func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []string, values []interface{}) (int64, error) { 339 func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []string, values []interface{}) (int64, error) {
329 Q := d.ins.TableQuote() 340 Q := d.ins.TableQuote()
330 341
...@@ -364,6 +375,7 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s ...@@ -364,6 +375,7 @@ func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []s
364 } 375 }
365 } 376 }
366 377
378 // execute update sql dbQuerier with given struct reflect.Value.
367 func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string) (int64, error) { 379 func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string) (int64, error) {
368 pkName, pkValue, ok := getExistPk(mi, ind) 380 pkName, pkValue, ok := getExistPk(mi, ind)
369 if ok == false { 381 if ok == false {
...@@ -404,6 +416,8 @@ func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. ...@@ -404,6 +416,8 @@ func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
404 return 0, nil 416 return 0, nil
405 } 417 }
406 418
419 // execute delete sql dbQuerier with given struct reflect.Value.
420 // delete index is pk.
407 func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) { 421 func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) {
408 pkName, pkValue, ok := getExistPk(mi, ind) 422 pkName, pkValue, ok := getExistPk(mi, ind)
409 if ok == false { 423 if ok == false {
...@@ -445,6 +459,8 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time. ...@@ -445,6 +459,8 @@ func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.
445 return 0, nil 459 return 0, nil
446 } 460 }
447 461
462 // update table-related record by querySet.
463 // need querySet not struct reflect.Value to update related records.
448 func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, params Params, tz *time.Location) (int64, error) { 464 func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, params Params, tz *time.Location) (int64, error) {
449 columns := make([]string, 0, len(params)) 465 columns := make([]string, 0, len(params))
450 values := make([]interface{}, 0, len(params)) 466 values := make([]interface{}, 0, len(params))
...@@ -520,6 +536,8 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con ...@@ -520,6 +536,8 @@ func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
520 return 0, nil 536 return 0, nil
521 } 537 }
522 538
539 // delete related records.
540 // do UpdateBanch or DeleteBanch by condition of tables' relationship.
523 func (d *dbBase) deleteRels(q dbQuerier, mi *modelInfo, args []interface{}, tz *time.Location) error { 541 func (d *dbBase) deleteRels(q dbQuerier, mi *modelInfo, args []interface{}, tz *time.Location) error {
524 for _, fi := range mi.fields.fieldsReverse { 542 for _, fi := range mi.fields.fieldsReverse {
525 fi = fi.reverseFieldInfo 543 fi = fi.reverseFieldInfo
...@@ -546,6 +564,7 @@ func (d *dbBase) deleteRels(q dbQuerier, mi *modelInfo, args []interface{}, tz * ...@@ -546,6 +564,7 @@ func (d *dbBase) deleteRels(q dbQuerier, mi *modelInfo, args []interface{}, tz *
546 return nil 564 return nil
547 } 565 }
548 566
567 // delete table-related records.
549 func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, tz *time.Location) (int64, error) { 568 func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, tz *time.Location) (int64, error) {
550 tables := newDbTables(mi, d.ins) 569 tables := newDbTables(mi, d.ins)
551 tables.skipEnd = true 570 tables.skipEnd = true
...@@ -623,6 +642,7 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con ...@@ -623,6 +642,7 @@ func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Con
623 return 0, nil 642 return 0, nil
624 } 643 }
625 644
645 // read related records.
626 func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, container interface{}, tz *time.Location, cols []string) (int64, error) { 646 func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, container interface{}, tz *time.Location, cols []string) (int64, error) {
627 647
628 val := reflect.ValueOf(container) 648 val := reflect.ValueOf(container)
...@@ -832,6 +852,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi ...@@ -832,6 +852,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
832 return cnt, nil 852 return cnt, nil
833 } 853 }
834 854
855 // excute count sql and return count result int64.
835 func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, tz *time.Location) (cnt int64, err error) { 856 func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, tz *time.Location) (cnt int64, err error) {
836 tables := newDbTables(mi, d.ins) 857 tables := newDbTables(mi, d.ins)
837 tables.parseRelated(qs.related, qs.relDepth) 858 tables.parseRelated(qs.related, qs.relDepth)
...@@ -852,6 +873,7 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition ...@@ -852,6 +873,7 @@ func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition
852 return 873 return
853 } 874 }
854 875
876 // generate sql with replacing operator string placeholders and replaced values.
855 func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator string, args []interface{}, tz *time.Location) (string, []interface{}) { 877 func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator string, args []interface{}, tz *time.Location) (string, []interface{}) {
856 sql := "" 878 sql := ""
857 params := getFlatParams(fi, args, tz) 879 params := getFlatParams(fi, args, tz)
...@@ -909,6 +931,7 @@ func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) { ...@@ -909,6 +931,7 @@ func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) {
909 // default not use 931 // default not use
910 } 932 }
911 933
934 // set values to struct column.
912 func (d *dbBase) setColsValues(mi *modelInfo, ind *reflect.Value, cols []string, values []interface{}, tz *time.Location) { 935 func (d *dbBase) setColsValues(mi *modelInfo, ind *reflect.Value, cols []string, values []interface{}, tz *time.Location) {
913 for i, column := range cols { 936 for i, column := range cols {
914 val := reflect.Indirect(reflect.ValueOf(values[i])).Interface() 937 val := reflect.Indirect(reflect.ValueOf(values[i])).Interface()
...@@ -930,6 +953,7 @@ func (d *dbBase) setColsValues(mi *modelInfo, ind *reflect.Value, cols []string, ...@@ -930,6 +953,7 @@ func (d *dbBase) setColsValues(mi *modelInfo, ind *reflect.Value, cols []string,
930 } 953 }
931 } 954 }
932 955
956 // convert value from database result to value following in field type.
933 func (d *dbBase) convertValueFromDB(fi *fieldInfo, val interface{}, tz *time.Location) (interface{}, error) { 957 func (d *dbBase) convertValueFromDB(fi *fieldInfo, val interface{}, tz *time.Location) (interface{}, error) {
934 if val == nil { 958 if val == nil {
935 return nil, nil 959 return nil, nil
...@@ -1082,6 +1106,7 @@ end: ...@@ -1082,6 +1106,7 @@ end:
1082 1106
1083 } 1107 }
1084 1108
1109 // set one value to struct column field.
1085 func (d *dbBase) setFieldValue(fi *fieldInfo, value interface{}, field reflect.Value) (interface{}, error) { 1110 func (d *dbBase) setFieldValue(fi *fieldInfo, value interface{}, field reflect.Value) (interface{}, error) {
1086 1111
1087 fieldType := fi.fieldType 1112 fieldType := fi.fieldType
...@@ -1156,6 +1181,7 @@ setValue: ...@@ -1156,6 +1181,7 @@ setValue:
1156 return value, nil 1181 return value, nil
1157 } 1182 }
1158 1183
1184 // query sql, read values , save to *[]ParamList.
1159 func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, exprs []string, container interface{}, tz *time.Location) (int64, error) { 1185 func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, exprs []string, container interface{}, tz *time.Location) (int64, error) {
1160 1186
1161 var ( 1187 var (
...@@ -1323,6 +1349,7 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond ...@@ -1323,6 +1349,7 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
1323 return cnt, nil 1349 return cnt, nil
1324 } 1350 }
1325 1351
1352 // flag of update joined record.
1326 func (d *dbBase) SupportUpdateJoin() bool { 1353 func (d *dbBase) SupportUpdateJoin() bool {
1327 return true 1354 return true
1328 } 1355 }
...@@ -1331,30 +1358,37 @@ func (d *dbBase) MaxLimit() uint64 { ...@@ -1331,30 +1358,37 @@ func (d *dbBase) MaxLimit() uint64 {
1331 return 18446744073709551615 1358 return 18446744073709551615
1332 } 1359 }
1333 1360
1361 // return quote.
1334 func (d *dbBase) TableQuote() string { 1362 func (d *dbBase) TableQuote() string {
1335 return "`" 1363 return "`"
1336 } 1364 }
1337 1365
1366 // replace value placeholer in parametered sql string.
1338 func (d *dbBase) ReplaceMarks(query *string) { 1367 func (d *dbBase) ReplaceMarks(query *string) {
1339 // default use `?` as mark, do nothing 1368 // default use `?` as mark, do nothing
1340 } 1369 }
1341 1370
1371 // flag of RETURNING sql.
1342 func (d *dbBase) HasReturningID(*modelInfo, *string) bool { 1372 func (d *dbBase) HasReturningID(*modelInfo, *string) bool {
1343 return false 1373 return false
1344 } 1374 }
1345 1375
1376 // convert time from db.
1346 func (d *dbBase) TimeFromDB(t *time.Time, tz *time.Location) { 1377 func (d *dbBase) TimeFromDB(t *time.Time, tz *time.Location) {
1347 *t = t.In(tz) 1378 *t = t.In(tz)
1348 } 1379 }
1349 1380
1381 // convert time to db.
1350 func (d *dbBase) TimeToDB(t *time.Time, tz *time.Location) { 1382 func (d *dbBase) TimeToDB(t *time.Time, tz *time.Location) {
1351 *t = t.In(tz) 1383 *t = t.In(tz)
1352 } 1384 }
1353 1385
1386 // get database types.
1354 func (d *dbBase) DbTypes() map[string]string { 1387 func (d *dbBase) DbTypes() map[string]string {
1355 return nil 1388 return nil
1356 } 1389 }
1357 1390
1391 // gt all tables.
1358 func (d *dbBase) GetTables(db dbQuerier) (map[string]bool, error) { 1392 func (d *dbBase) GetTables(db dbQuerier) (map[string]bool, error) {
1359 tables := make(map[string]bool) 1393 tables := make(map[string]bool)
1360 query := d.ins.ShowTablesQuery() 1394 query := d.ins.ShowTablesQuery()
...@@ -1379,6 +1413,7 @@ func (d *dbBase) GetTables(db dbQuerier) (map[string]bool, error) { ...@@ -1379,6 +1413,7 @@ func (d *dbBase) GetTables(db dbQuerier) (map[string]bool, error) {
1379 return tables, nil 1413 return tables, nil
1380 } 1414 }
1381 1415
1416 // get all cloumns in table.
1382 func (d *dbBase) GetColumns(db dbQuerier, table string) (map[string][3]string, error) { 1417 func (d *dbBase) GetColumns(db dbQuerier, table string) (map[string][3]string, error) {
1383 columns := make(map[string][3]string) 1418 columns := make(map[string][3]string)
1384 query := d.ins.ShowColumnsQuery(table) 1419 query := d.ins.ShowColumnsQuery(table)
...@@ -1405,18 +1440,22 @@ func (d *dbBase) GetColumns(db dbQuerier, table string) (map[string][3]string, e ...@@ -1405,18 +1440,22 @@ func (d *dbBase) GetColumns(db dbQuerier, table string) (map[string][3]string, e
1405 return columns, nil 1440 return columns, nil
1406 } 1441 }
1407 1442
1443 // not implement.
1408 func (d *dbBase) OperatorSql(operator string) string { 1444 func (d *dbBase) OperatorSql(operator string) string {
1409 panic(ErrNotImplement) 1445 panic(ErrNotImplement)
1410 } 1446 }
1411 1447
1448 // not implement.
1412 func (d *dbBase) ShowTablesQuery() string { 1449 func (d *dbBase) ShowTablesQuery() string {
1413 panic(ErrNotImplement) 1450 panic(ErrNotImplement)
1414 } 1451 }
1415 1452
1453 // not implement.
1416 func (d *dbBase) ShowColumnsQuery(table string) string { 1454 func (d *dbBase) ShowColumnsQuery(table string) string {
1417 panic(ErrNotImplement) 1455 panic(ErrNotImplement)
1418 } 1456 }
1419 1457
1458 // not implement.
1420 func (d *dbBase) IndexExists(dbQuerier, string, string) bool { 1459 func (d *dbBase) IndexExists(dbQuerier, string, string) bool {
1421 panic(ErrNotImplement) 1460 panic(ErrNotImplement)
1422 } 1461 }
......
...@@ -9,27 +9,32 @@ import ( ...@@ -9,27 +9,32 @@ import (
9 "time" 9 "time"
10 ) 10 )
11 11
12 // database driver constant int.
12 type DriverType int 13 type DriverType int
13 14
14 const ( 15 const (
15 _ DriverType = iota 16 _ DriverType = iota // int enum type
16 DR_MySQL 17 DR_MySQL // mysql
17 DR_Sqlite 18 DR_Sqlite // sqlite
18 DR_Oracle 19 DR_Oracle // oracle
19 DR_Postgres 20 DR_Postgres // pgsql
20 ) 21 )
21 22
23 // database driver string.
22 type driver string 24 type driver string
23 25
26 // get type constant int of current driver..
24 func (d driver) Type() DriverType { 27 func (d driver) Type() DriverType {
25 a, _ := dataBaseCache.get(string(d)) 28 a, _ := dataBaseCache.get(string(d))
26 return a.Driver 29 return a.Driver
27 } 30 }
28 31
32 // get name of current driver
29 func (d driver) Name() string { 33 func (d driver) Name() string {
30 return string(d) 34 return string(d)
31 } 35 }
32 36
37 // check driver iis implemented Driver interface or not.
33 var _ Driver = new(driver) 38 var _ Driver = new(driver)
34 39
35 var ( 40 var (
...@@ -47,11 +52,13 @@ var ( ...@@ -47,11 +52,13 @@ var (
47 } 52 }
48 ) 53 )
49 54
55 // database alias cacher.
50 type _dbCache struct { 56 type _dbCache struct {
51 mux sync.RWMutex 57 mux sync.RWMutex
52 cache map[string]*alias 58 cache map[string]*alias
53 } 59 }
54 60
61 // add database alias with original name.
55 func (ac *_dbCache) add(name string, al *alias) (added bool) { 62 func (ac *_dbCache) add(name string, al *alias) (added bool) {
56 ac.mux.Lock() 63 ac.mux.Lock()
57 defer ac.mux.Unlock() 64 defer ac.mux.Unlock()
...@@ -62,6 +69,7 @@ func (ac *_dbCache) add(name string, al *alias) (added bool) { ...@@ -62,6 +69,7 @@ func (ac *_dbCache) add(name string, al *alias) (added bool) {
62 return 69 return
63 } 70 }
64 71
72 // get database alias if cached.
65 func (ac *_dbCache) get(name string) (al *alias, ok bool) { 73 func (ac *_dbCache) get(name string) (al *alias, ok bool) {
66 ac.mux.RLock() 74 ac.mux.RLock()
67 defer ac.mux.RUnlock() 75 defer ac.mux.RUnlock()
...@@ -69,6 +77,7 @@ func (ac *_dbCache) get(name string) (al *alias, ok bool) { ...@@ -69,6 +77,7 @@ func (ac *_dbCache) get(name string) (al *alias, ok bool) {
69 return 77 return
70 } 78 }
71 79
80 // get default alias.
72 func (ac *_dbCache) getDefault() (al *alias) { 81 func (ac *_dbCache) getDefault() (al *alias) {
73 al, _ = ac.get("default") 82 al, _ = ac.get("default")
74 return 83 return
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!