add comments for orm packages, part 2
Showing
7 changed files
with
59 additions
and
0 deletions
| ... | @@ -927,6 +927,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri | ... | @@ -927,6 +927,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri |
| 927 | return sql, params | 927 | return sql, params |
| 928 | } | 928 | } |
| 929 | 929 | ||
| 930 | // gernerate sql string with inner function, such as UPPER(text). | ||
| 930 | func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) { | 931 | func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) { |
| 931 | // default not use | 932 | // default not use |
| 932 | } | 933 | } | ... | ... |
| ... | @@ -4,6 +4,7 @@ import ( | ... | @@ -4,6 +4,7 @@ import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | ) | 5 | ) |
| 6 | 6 | ||
| 7 | // mysql operators. | ||
| 7 | var mysqlOperators = map[string]string{ | 8 | var mysqlOperators = map[string]string{ |
| 8 | "exact": "= ?", | 9 | "exact": "= ?", |
| 9 | "iexact": "LIKE ?", | 10 | "iexact": "LIKE ?", |
| ... | @@ -21,6 +22,7 @@ var mysqlOperators = map[string]string{ | ... | @@ -21,6 +22,7 @@ var mysqlOperators = map[string]string{ |
| 21 | "iendswith": "LIKE ?", | 22 | "iendswith": "LIKE ?", |
| 22 | } | 23 | } |
| 23 | 24 | ||
| 25 | // mysql column field types. | ||
| 24 | var mysqlTypes = map[string]string{ | 26 | var mysqlTypes = map[string]string{ |
| 25 | "auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY", | 27 | "auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY", |
| 26 | "pk": "NOT NULL PRIMARY KEY", | 28 | "pk": "NOT NULL PRIMARY KEY", |
| ... | @@ -41,29 +43,35 @@ var mysqlTypes = map[string]string{ | ... | @@ -41,29 +43,35 @@ var mysqlTypes = map[string]string{ |
| 41 | "float64-decimal": "numeric(%d, %d)", | 43 | "float64-decimal": "numeric(%d, %d)", |
| 42 | } | 44 | } |
| 43 | 45 | ||
| 46 | // mysql dbBaser implementation. | ||
| 44 | type dbBaseMysql struct { | 47 | type dbBaseMysql struct { |
| 45 | dbBase | 48 | dbBase |
| 46 | } | 49 | } |
| 47 | 50 | ||
| 48 | var _ dbBaser = new(dbBaseMysql) | 51 | var _ dbBaser = new(dbBaseMysql) |
| 49 | 52 | ||
| 53 | // get mysql operator. | ||
| 50 | func (d *dbBaseMysql) OperatorSql(operator string) string { | 54 | func (d *dbBaseMysql) OperatorSql(operator string) string { |
| 51 | return mysqlOperators[operator] | 55 | return mysqlOperators[operator] |
| 52 | } | 56 | } |
| 53 | 57 | ||
| 58 | // get mysql table field types. | ||
| 54 | func (d *dbBaseMysql) DbTypes() map[string]string { | 59 | func (d *dbBaseMysql) DbTypes() map[string]string { |
| 55 | return mysqlTypes | 60 | return mysqlTypes |
| 56 | } | 61 | } |
| 57 | 62 | ||
| 63 | // show table sql for mysql. | ||
| 58 | func (d *dbBaseMysql) ShowTablesQuery() string { | 64 | func (d *dbBaseMysql) ShowTablesQuery() string { |
| 59 | return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()" | 65 | return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()" |
| 60 | } | 66 | } |
| 61 | 67 | ||
| 68 | // show columns sql of table for mysql. | ||
| 62 | func (d *dbBaseMysql) ShowColumnsQuery(table string) string { | 69 | func (d *dbBaseMysql) ShowColumnsQuery(table string) string { |
| 63 | return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+ | 70 | return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+ |
| 64 | "WHERE table_schema = DATABASE() AND table_name = '%s'", table) | 71 | "WHERE table_schema = DATABASE() AND table_name = '%s'", table) |
| 65 | } | 72 | } |
| 66 | 73 | ||
| 74 | // execute sql to check index exist. | ||
| 67 | func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool { | 75 | func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool { |
| 68 | row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+ | 76 | row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+ |
| 69 | "WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name) | 77 | "WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name) |
| ... | @@ -72,6 +80,7 @@ func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool | ... | @@ -72,6 +80,7 @@ func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool |
| 72 | return cnt > 0 | 80 | return cnt > 0 |
| 73 | } | 81 | } |
| 74 | 82 | ||
| 83 | // create new mysql dbBaser. | ||
| 75 | func newdbBaseMysql() dbBaser { | 84 | func newdbBaseMysql() dbBaser { |
| 76 | b := new(dbBaseMysql) | 85 | b := new(dbBaseMysql) |
| 77 | b.ins = b | 86 | b.ins = b | ... | ... |
| 1 | package orm | 1 | package orm |
| 2 | 2 | ||
| 3 | // oracle dbBaser | ||
| 3 | type dbBaseOracle struct { | 4 | type dbBaseOracle struct { |
| 4 | dbBase | 5 | dbBase |
| 5 | } | 6 | } |
| 6 | 7 | ||
| 7 | var _ dbBaser = new(dbBaseOracle) | 8 | var _ dbBaser = new(dbBaseOracle) |
| 8 | 9 | ||
| 10 | // create oracle dbBaser. | ||
| 9 | func newdbBaseOracle() dbBaser { | 11 | func newdbBaseOracle() dbBaser { |
| 10 | b := new(dbBaseOracle) | 12 | b := new(dbBaseOracle) |
| 11 | b.ins = b | 13 | b.ins = b | ... | ... |
| ... | @@ -5,6 +5,7 @@ import ( | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | "strconv" | 5 | "strconv" |
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | // postgresql operators. | ||
| 8 | var postgresOperators = map[string]string{ | 9 | var postgresOperators = map[string]string{ |
| 9 | "exact": "= ?", | 10 | "exact": "= ?", |
| 10 | "iexact": "= UPPER(?)", | 11 | "iexact": "= UPPER(?)", |
| ... | @@ -20,6 +21,7 @@ var postgresOperators = map[string]string{ | ... | @@ -20,6 +21,7 @@ var postgresOperators = map[string]string{ |
| 20 | "iendswith": "LIKE UPPER(?)", | 21 | "iendswith": "LIKE UPPER(?)", |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 24 | // postgresql column field types. | ||
| 23 | var postgresTypes = map[string]string{ | 25 | var postgresTypes = map[string]string{ |
| 24 | "auto": "serial NOT NULL PRIMARY KEY", | 26 | "auto": "serial NOT NULL PRIMARY KEY", |
| 25 | "pk": "NOT NULL PRIMARY KEY", | 27 | "pk": "NOT NULL PRIMARY KEY", |
| ... | @@ -40,16 +42,19 @@ var postgresTypes = map[string]string{ | ... | @@ -40,16 +42,19 @@ var postgresTypes = map[string]string{ |
| 40 | "float64-decimal": "numeric(%d, %d)", | 42 | "float64-decimal": "numeric(%d, %d)", |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 45 | // postgresql dbBaser. | ||
| 43 | type dbBasePostgres struct { | 46 | type dbBasePostgres struct { |
| 44 | dbBase | 47 | dbBase |
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | var _ dbBaser = new(dbBasePostgres) | 50 | var _ dbBaser = new(dbBasePostgres) |
| 48 | 51 | ||
| 52 | // get postgresql operator. | ||
| 49 | func (d *dbBasePostgres) OperatorSql(operator string) string { | 53 | func (d *dbBasePostgres) OperatorSql(operator string) string { |
| 50 | return postgresOperators[operator] | 54 | return postgresOperators[operator] |
| 51 | } | 55 | } |
| 52 | 56 | ||
| 57 | // generate functioned sql string, such as contains(text). | ||
| 53 | func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { | 58 | func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { |
| 54 | switch operator { | 59 | switch operator { |
| 55 | case "contains", "startswith", "endswith": | 60 | case "contains", "startswith", "endswith": |
| ... | @@ -59,6 +64,7 @@ func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, | ... | @@ -59,6 +64,7 @@ func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, |
| 59 | } | 64 | } |
| 60 | } | 65 | } |
| 61 | 66 | ||
| 67 | // postgresql unsupports updating joined record. | ||
| 62 | func (d *dbBasePostgres) SupportUpdateJoin() bool { | 68 | func (d *dbBasePostgres) SupportUpdateJoin() bool { |
| 63 | return false | 69 | return false |
| 64 | } | 70 | } |
| ... | @@ -67,10 +73,13 @@ func (d *dbBasePostgres) MaxLimit() uint64 { | ... | @@ -67,10 +73,13 @@ func (d *dbBasePostgres) MaxLimit() uint64 { |
| 67 | return 0 | 73 | return 0 |
| 68 | } | 74 | } |
| 69 | 75 | ||
| 76 | // postgresql quote is ". | ||
| 70 | func (d *dbBasePostgres) TableQuote() string { | 77 | func (d *dbBasePostgres) TableQuote() string { |
| 71 | return `"` | 78 | return `"` |
| 72 | } | 79 | } |
| 73 | 80 | ||
| 81 | // postgresql value placeholder is $n. | ||
| 82 | // replace default ? to $n. | ||
| 74 | func (d *dbBasePostgres) ReplaceMarks(query *string) { | 83 | func (d *dbBasePostgres) ReplaceMarks(query *string) { |
| 75 | q := *query | 84 | q := *query |
| 76 | num := 0 | 85 | num := 0 |
| ... | @@ -97,6 +106,7 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) { | ... | @@ -97,6 +106,7 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) { |
| 97 | *query = string(data) | 106 | *query = string(data) |
| 98 | } | 107 | } |
| 99 | 108 | ||
| 109 | // make returning sql support for postgresql. | ||
| 100 | func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) { | 110 | func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) { |
| 101 | if mi.fields.pk.auto { | 111 | if mi.fields.pk.auto { |
| 102 | if query != nil { | 112 | if query != nil { |
| ... | @@ -107,18 +117,22 @@ func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) | ... | @@ -107,18 +117,22 @@ func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) |
| 107 | return | 117 | return |
| 108 | } | 118 | } |
| 109 | 119 | ||
| 120 | // show table sql for postgresql. | ||
| 110 | func (d *dbBasePostgres) ShowTablesQuery() string { | 121 | func (d *dbBasePostgres) ShowTablesQuery() string { |
| 111 | return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')" | 122 | return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')" |
| 112 | } | 123 | } |
| 113 | 124 | ||
| 125 | // show table columns sql for postgresql. | ||
| 114 | func (d *dbBasePostgres) ShowColumnsQuery(table string) string { | 126 | func (d *dbBasePostgres) ShowColumnsQuery(table string) string { |
| 115 | return fmt.Sprintf("SELECT column_name, data_type, is_nullable FROM information_schema.columns where table_schema NOT IN ('pg_catalog', 'information_schema') and table_name = '%s'", table) | 127 | return fmt.Sprintf("SELECT column_name, data_type, is_nullable FROM information_schema.columns where table_schema NOT IN ('pg_catalog', 'information_schema') and table_name = '%s'", table) |
| 116 | } | 128 | } |
| 117 | 129 | ||
| 130 | // get column types of postgresql. | ||
| 118 | func (d *dbBasePostgres) DbTypes() map[string]string { | 131 | func (d *dbBasePostgres) DbTypes() map[string]string { |
| 119 | return postgresTypes | 132 | return postgresTypes |
| 120 | } | 133 | } |
| 121 | 134 | ||
| 135 | // check index exist in postgresql. | ||
| 122 | func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bool { | 136 | func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bool { |
| 123 | query := fmt.Sprintf("SELECT COUNT(*) FROM pg_indexes WHERE tablename = '%s' AND indexname = '%s'", table, name) | 137 | query := fmt.Sprintf("SELECT COUNT(*) FROM pg_indexes WHERE tablename = '%s' AND indexname = '%s'", table, name) |
| 124 | row := db.QueryRow(query) | 138 | row := db.QueryRow(query) |
| ... | @@ -127,6 +141,7 @@ func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bo | ... | @@ -127,6 +141,7 @@ func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bo |
| 127 | return cnt > 0 | 141 | return cnt > 0 |
| 128 | } | 142 | } |
| 129 | 143 | ||
| 144 | // create new postgresql dbBaser. | ||
| 130 | func newdbBasePostgres() dbBaser { | 145 | func newdbBasePostgres() dbBaser { |
| 131 | b := new(dbBasePostgres) | 146 | b := new(dbBasePostgres) |
| 132 | b.ins = b | 147 | b.ins = b | ... | ... |
| ... | @@ -5,6 +5,7 @@ import ( | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | "fmt" | 5 | "fmt" |
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | // sqlite operators. | ||
| 8 | var sqliteOperators = map[string]string{ | 9 | var sqliteOperators = map[string]string{ |
| 9 | "exact": "= ?", | 10 | "exact": "= ?", |
| 10 | "iexact": "LIKE ? ESCAPE '\\'", | 11 | "iexact": "LIKE ? ESCAPE '\\'", |
| ... | @@ -20,6 +21,7 @@ var sqliteOperators = map[string]string{ | ... | @@ -20,6 +21,7 @@ var sqliteOperators = map[string]string{ |
| 20 | "iendswith": "LIKE ? ESCAPE '\\'", | 21 | "iendswith": "LIKE ? ESCAPE '\\'", |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 24 | // sqlite column types. | ||
| 23 | var sqliteTypes = map[string]string{ | 25 | var sqliteTypes = map[string]string{ |
| 24 | "auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT", | 26 | "auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT", |
| 25 | "pk": "NOT NULL PRIMARY KEY", | 27 | "pk": "NOT NULL PRIMARY KEY", |
| ... | @@ -40,38 +42,47 @@ var sqliteTypes = map[string]string{ | ... | @@ -40,38 +42,47 @@ var sqliteTypes = map[string]string{ |
| 40 | "float64-decimal": "decimal", | 42 | "float64-decimal": "decimal", |
| 41 | } | 43 | } |
| 42 | 44 | ||
| 45 | // sqlite dbBaser. | ||
| 43 | type dbBaseSqlite struct { | 46 | type dbBaseSqlite struct { |
| 44 | dbBase | 47 | dbBase |
| 45 | } | 48 | } |
| 46 | 49 | ||
| 47 | var _ dbBaser = new(dbBaseSqlite) | 50 | var _ dbBaser = new(dbBaseSqlite) |
| 48 | 51 | ||
| 52 | // get sqlite operator. | ||
| 49 | func (d *dbBaseSqlite) OperatorSql(operator string) string { | 53 | func (d *dbBaseSqlite) OperatorSql(operator string) string { |
| 50 | return sqliteOperators[operator] | 54 | return sqliteOperators[operator] |
| 51 | } | 55 | } |
| 52 | 56 | ||
| 57 | // generate functioned sql for sqlite. | ||
| 58 | // only support DATE(text). | ||
| 53 | func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { | 59 | func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { |
| 54 | if fi.fieldType == TypeDateField { | 60 | if fi.fieldType == TypeDateField { |
| 55 | *leftCol = fmt.Sprintf("DATE(%s)", *leftCol) | 61 | *leftCol = fmt.Sprintf("DATE(%s)", *leftCol) |
| 56 | } | 62 | } |
| 57 | } | 63 | } |
| 58 | 64 | ||
| 65 | // unable updating joined record in sqlite. | ||
| 59 | func (d *dbBaseSqlite) SupportUpdateJoin() bool { | 66 | func (d *dbBaseSqlite) SupportUpdateJoin() bool { |
| 60 | return false | 67 | return false |
| 61 | } | 68 | } |
| 62 | 69 | ||
| 70 | // max int in sqlite. | ||
| 63 | func (d *dbBaseSqlite) MaxLimit() uint64 { | 71 | func (d *dbBaseSqlite) MaxLimit() uint64 { |
| 64 | return 9223372036854775807 | 72 | return 9223372036854775807 |
| 65 | } | 73 | } |
| 66 | 74 | ||
| 75 | // get column types in sqlite. | ||
| 67 | func (d *dbBaseSqlite) DbTypes() map[string]string { | 76 | func (d *dbBaseSqlite) DbTypes() map[string]string { |
| 68 | return sqliteTypes | 77 | return sqliteTypes |
| 69 | } | 78 | } |
| 70 | 79 | ||
| 80 | // get show tables sql in sqlite. | ||
| 71 | func (d *dbBaseSqlite) ShowTablesQuery() string { | 81 | func (d *dbBaseSqlite) ShowTablesQuery() string { |
| 72 | return "SELECT name FROM sqlite_master WHERE type = 'table'" | 82 | return "SELECT name FROM sqlite_master WHERE type = 'table'" |
| 73 | } | 83 | } |
| 74 | 84 | ||
| 85 | // get columns in sqlite. | ||
| 75 | func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) { | 86 | func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) { |
| 76 | query := d.ins.ShowColumnsQuery(table) | 87 | query := d.ins.ShowColumnsQuery(table) |
| 77 | rows, err := db.Query(query) | 88 | rows, err := db.Query(query) |
| ... | @@ -92,10 +103,12 @@ func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]str | ... | @@ -92,10 +103,12 @@ func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]str |
| 92 | return columns, nil | 103 | return columns, nil |
| 93 | } | 104 | } |
| 94 | 105 | ||
| 106 | // get show columns sql in sqlite. | ||
| 95 | func (d *dbBaseSqlite) ShowColumnsQuery(table string) string { | 107 | func (d *dbBaseSqlite) ShowColumnsQuery(table string) string { |
| 96 | return fmt.Sprintf("pragma table_info('%s')", table) | 108 | return fmt.Sprintf("pragma table_info('%s')", table) |
| 97 | } | 109 | } |
| 98 | 110 | ||
| 111 | // check index exist in sqlite. | ||
| 99 | func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool { | 112 | func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool { |
| 100 | query := fmt.Sprintf("PRAGMA index_list('%s')", table) | 113 | query := fmt.Sprintf("PRAGMA index_list('%s')", table) |
| 101 | rows, err := db.Query(query) | 114 | rows, err := db.Query(query) |
| ... | @@ -113,6 +126,7 @@ func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool | ... | @@ -113,6 +126,7 @@ func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool |
| 113 | return false | 126 | return false |
| 114 | } | 127 | } |
| 115 | 128 | ||
| 129 | // create new sqlite dbBaser. | ||
| 116 | func newdbBaseSqlite() dbBaser { | 130 | func newdbBaseSqlite() dbBaser { |
| 117 | b := new(dbBaseSqlite) | 131 | b := new(dbBaseSqlite) |
| 118 | b.ins = b | 132 | b.ins = b | ... | ... |
| ... | @@ -6,6 +6,7 @@ import ( | ... | @@ -6,6 +6,7 @@ import ( |
| 6 | "time" | 6 | "time" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | // table info struct. | ||
| 9 | type dbTable struct { | 10 | type dbTable struct { |
| 10 | id int | 11 | id int |
| 11 | index string | 12 | index string |
| ... | @@ -18,6 +19,7 @@ type dbTable struct { | ... | @@ -18,6 +19,7 @@ type dbTable struct { |
| 18 | jtl *dbTable | 19 | jtl *dbTable |
| 19 | } | 20 | } |
| 20 | 21 | ||
| 22 | // tables collection struct, contains some tables. | ||
| 21 | type dbTables struct { | 23 | type dbTables struct { |
| 22 | tablesM map[string]*dbTable | 24 | tablesM map[string]*dbTable |
| 23 | tables []*dbTable | 25 | tables []*dbTable |
| ... | @@ -26,6 +28,8 @@ type dbTables struct { | ... | @@ -26,6 +28,8 @@ type dbTables struct { |
| 26 | skipEnd bool | 28 | skipEnd bool |
| 27 | } | 29 | } |
| 28 | 30 | ||
| 31 | // set table info to collection. | ||
| 32 | // if not exist, create new. | ||
| 29 | func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) *dbTable { | 33 | func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) *dbTable { |
| 30 | name := strings.Join(names, ExprSep) | 34 | name := strings.Join(names, ExprSep) |
| 31 | if j, ok := t.tablesM[name]; ok { | 35 | if j, ok := t.tablesM[name]; ok { |
| ... | @@ -42,6 +46,7 @@ func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) | ... | @@ -42,6 +46,7 @@ func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) |
| 42 | return t.tablesM[name] | 46 | return t.tablesM[name] |
| 43 | } | 47 | } |
| 44 | 48 | ||
| 49 | // add table info to collection. | ||
| 45 | func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) (*dbTable, bool) { | 50 | func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) (*dbTable, bool) { |
| 46 | name := strings.Join(names, ExprSep) | 51 | name := strings.Join(names, ExprSep) |
| 47 | if _, ok := t.tablesM[name]; ok == false { | 52 | if _, ok := t.tablesM[name]; ok == false { |
| ... | @@ -54,11 +59,14 @@ func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) | ... | @@ -54,11 +59,14 @@ func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) |
| 54 | return t.tablesM[name], false | 59 | return t.tablesM[name], false |
| 55 | } | 60 | } |
| 56 | 61 | ||
| 62 | // get table info in collection. | ||
| 57 | func (t *dbTables) get(name string) (*dbTable, bool) { | 63 | func (t *dbTables) get(name string) (*dbTable, bool) { |
| 58 | j, ok := t.tablesM[name] | 64 | j, ok := t.tablesM[name] |
| 59 | return j, ok | 65 | return j, ok |
| 60 | } | 66 | } |
| 61 | 67 | ||
| 68 | // get related fields info in recursive depth loop. | ||
| 69 | // loop once, depth decreases one. | ||
| 62 | func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related []string) []string { | 70 | func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related []string) []string { |
| 63 | if depth < 0 || fi.fieldType == RelManyToMany { | 71 | if depth < 0 || fi.fieldType == RelManyToMany { |
| 64 | return related | 72 | return related |
| ... | @@ -79,6 +87,7 @@ func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related [] | ... | @@ -79,6 +87,7 @@ func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related [] |
| 79 | return related | 87 | return related |
| 80 | } | 88 | } |
| 81 | 89 | ||
| 90 | // parse related fields. | ||
| 82 | func (t *dbTables) parseRelated(rels []string, depth int) { | 91 | func (t *dbTables) parseRelated(rels []string, depth int) { |
| 83 | 92 | ||
| 84 | relsNum := len(rels) | 93 | relsNum := len(rels) |
| ... | @@ -140,6 +149,7 @@ func (t *dbTables) parseRelated(rels []string, depth int) { | ... | @@ -140,6 +149,7 @@ func (t *dbTables) parseRelated(rels []string, depth int) { |
| 140 | } | 149 | } |
| 141 | } | 150 | } |
| 142 | 151 | ||
| 152 | // generate join string. | ||
| 143 | func (t *dbTables) getJoinSql() (join string) { | 153 | func (t *dbTables) getJoinSql() (join string) { |
| 144 | Q := t.base.TableQuote() | 154 | Q := t.base.TableQuote() |
| 145 | 155 | ||
| ... | @@ -186,6 +196,7 @@ func (t *dbTables) getJoinSql() (join string) { | ... | @@ -186,6 +196,7 @@ func (t *dbTables) getJoinSql() (join string) { |
| 186 | return | 196 | return |
| 187 | } | 197 | } |
| 188 | 198 | ||
| 199 | // parse orm model struct field tag expression. | ||
| 189 | func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string, info *fieldInfo, success bool) { | 200 | func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string, info *fieldInfo, success bool) { |
| 190 | var ( | 201 | var ( |
| 191 | jtl *dbTable | 202 | jtl *dbTable |
| ... | @@ -300,6 +311,7 @@ loopFor: | ... | @@ -300,6 +311,7 @@ loopFor: |
| 300 | return | 311 | return |
| 301 | } | 312 | } |
| 302 | 313 | ||
| 314 | // generate condition sql. | ||
| 303 | func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) { | 315 | func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) { |
| 304 | if cond == nil || cond.IsEmpty() { | 316 | if cond == nil || cond.IsEmpty() { |
| 305 | return | 317 | return |
| ... | @@ -364,6 +376,7 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe | ... | @@ -364,6 +376,7 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe |
| 364 | return | 376 | return |
| 365 | } | 377 | } |
| 366 | 378 | ||
| 379 | // generate order sql. | ||
| 367 | func (t *dbTables) getOrderSql(orders []string) (orderSql string) { | 380 | func (t *dbTables) getOrderSql(orders []string) (orderSql string) { |
| 368 | if len(orders) == 0 { | 381 | if len(orders) == 0 { |
| 369 | return | 382 | return |
| ... | @@ -392,6 +405,7 @@ func (t *dbTables) getOrderSql(orders []string) (orderSql string) { | ... | @@ -392,6 +405,7 @@ func (t *dbTables) getOrderSql(orders []string) (orderSql string) { |
| 392 | return | 405 | return |
| 393 | } | 406 | } |
| 394 | 407 | ||
| 408 | // generate limit sql. | ||
| 395 | func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) { | 409 | func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) { |
| 396 | if limit == 0 { | 410 | if limit == 0 { |
| 397 | limit = int64(DefaultRowsLimit) | 411 | limit = int64(DefaultRowsLimit) |
| ... | @@ -414,6 +428,7 @@ func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits | ... | @@ -414,6 +428,7 @@ func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits |
| 414 | return | 428 | return |
| 415 | } | 429 | } |
| 416 | 430 | ||
| 431 | // crete new tables collection. | ||
| 417 | func newDbTables(mi *modelInfo, base dbBaser) *dbTables { | 432 | func newDbTables(mi *modelInfo, base dbBaser) *dbTables { |
| 418 | tables := &dbTables{} | 433 | tables := &dbTables{} |
| 419 | tables.tablesM = make(map[string]*dbTable) | 434 | tables.tablesM = make(map[string]*dbTable) | ... | ... |
| ... | @@ -6,6 +6,7 @@ import ( | ... | @@ -6,6 +6,7 @@ import ( |
| 6 | "time" | 6 | "time" |
| 7 | ) | 7 | ) |
| 8 | 8 | ||
| 9 | // get table alias. | ||
| 9 | func getDbAlias(name string) *alias { | 10 | func getDbAlias(name string) *alias { |
| 10 | if al, ok := dataBaseCache.get(name); ok { | 11 | if al, ok := dataBaseCache.get(name); ok { |
| 11 | return al | 12 | return al |
| ... | @@ -15,6 +16,7 @@ func getDbAlias(name string) *alias { | ... | @@ -15,6 +16,7 @@ func getDbAlias(name string) *alias { |
| 15 | return nil | 16 | return nil |
| 16 | } | 17 | } |
| 17 | 18 | ||
| 19 | // get pk column info. | ||
| 18 | func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) { | 20 | func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) { |
| 19 | fi := mi.fields.pk | 21 | fi := mi.fields.pk |
| 20 | 22 | ||
| ... | @@ -37,6 +39,7 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac | ... | @@ -37,6 +39,7 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac |
| 37 | return | 39 | return |
| 38 | } | 40 | } |
| 39 | 41 | ||
| 42 | // get fields description as flatted string. | ||
| 40 | func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) { | 43 | func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) { |
| 41 | 44 | ||
| 42 | outFor: | 45 | outFor: | ... | ... |
-
Please register or sign in to post a comment