From 4c527dde65b78d61a0fc9b190dabe9272a309fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=82=85=E5=B0=8F=E9=BB=91?= Date: Fri, 17 Jan 2014 17:25:17 +0800 Subject: [PATCH] add comments for orm packages, part 2 --- orm/db.go | 1 + orm/db_mysql.go | 9 +++++++++ orm/db_oracle.go | 2 ++ orm/db_postgres.go | 15 +++++++++++++++ orm/db_sqlite.go | 14 ++++++++++++++ orm/db_tables.go | 15 +++++++++++++++ orm/db_utils.go | 3 +++ 7 files changed, 59 insertions(+) diff --git a/orm/db.go b/orm/db.go index 10967fc5..f12e76fb 100644 --- a/orm/db.go +++ b/orm/db.go @@ -927,6 +927,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri return sql, params } +// gernerate sql string with inner function, such as UPPER(text). func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) { // default not use } diff --git a/orm/db_mysql.go b/orm/db_mysql.go index da123079..566f2992 100644 --- a/orm/db_mysql.go +++ b/orm/db_mysql.go @@ -4,6 +4,7 @@ import ( "fmt" ) +// mysql operators. var mysqlOperators = map[string]string{ "exact": "= ?", "iexact": "LIKE ?", @@ -21,6 +22,7 @@ var mysqlOperators = map[string]string{ "iendswith": "LIKE ?", } +// mysql column field types. var mysqlTypes = map[string]string{ "auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY", @@ -41,29 +43,35 @@ var mysqlTypes = map[string]string{ "float64-decimal": "numeric(%d, %d)", } +// mysql dbBaser implementation. type dbBaseMysql struct { dbBase } var _ dbBaser = new(dbBaseMysql) +// get mysql operator. func (d *dbBaseMysql) OperatorSql(operator string) string { return mysqlOperators[operator] } +// get mysql table field types. func (d *dbBaseMysql) DbTypes() map[string]string { return mysqlTypes } +// show table sql for mysql. func (d *dbBaseMysql) ShowTablesQuery() string { return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()" } +// show columns sql of table for mysql. func (d *dbBaseMysql) ShowColumnsQuery(table string) string { return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+ "WHERE table_schema = DATABASE() AND table_name = '%s'", table) } +// execute sql to check index exist. func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool { row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+ "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 return cnt > 0 } +// create new mysql dbBaser. func newdbBaseMysql() dbBaser { b := new(dbBaseMysql) b.ins = b diff --git a/orm/db_oracle.go b/orm/db_oracle.go index ca1715ef..8e374122 100644 --- a/orm/db_oracle.go +++ b/orm/db_oracle.go @@ -1,11 +1,13 @@ package orm +// oracle dbBaser type dbBaseOracle struct { dbBase } var _ dbBaser = new(dbBaseOracle) +// create oracle dbBaser. func newdbBaseOracle() dbBaser { b := new(dbBaseOracle) b.ins = b diff --git a/orm/db_postgres.go b/orm/db_postgres.go index 4058fc10..d26511c0 100644 --- a/orm/db_postgres.go +++ b/orm/db_postgres.go @@ -5,6 +5,7 @@ import ( "strconv" ) +// postgresql operators. var postgresOperators = map[string]string{ "exact": "= ?", "iexact": "= UPPER(?)", @@ -20,6 +21,7 @@ var postgresOperators = map[string]string{ "iendswith": "LIKE UPPER(?)", } +// postgresql column field types. var postgresTypes = map[string]string{ "auto": "serial NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY", @@ -40,16 +42,19 @@ var postgresTypes = map[string]string{ "float64-decimal": "numeric(%d, %d)", } +// postgresql dbBaser. type dbBasePostgres struct { dbBase } var _ dbBaser = new(dbBasePostgres) +// get postgresql operator. func (d *dbBasePostgres) OperatorSql(operator string) string { return postgresOperators[operator] } +// generate functioned sql string, such as contains(text). func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { switch operator { case "contains", "startswith", "endswith": @@ -59,6 +64,7 @@ func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, } } +// postgresql unsupports updating joined record. func (d *dbBasePostgres) SupportUpdateJoin() bool { return false } @@ -67,10 +73,13 @@ func (d *dbBasePostgres) MaxLimit() uint64 { return 0 } +// postgresql quote is ". func (d *dbBasePostgres) TableQuote() string { return `"` } +// postgresql value placeholder is $n. +// replace default ? to $n. func (d *dbBasePostgres) ReplaceMarks(query *string) { q := *query num := 0 @@ -97,6 +106,7 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) { *query = string(data) } +// make returning sql support for postgresql. func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) { if mi.fields.pk.auto { if query != nil { @@ -107,18 +117,22 @@ func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) return } +// show table sql for postgresql. func (d *dbBasePostgres) ShowTablesQuery() string { return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')" } +// show table columns sql for postgresql. func (d *dbBasePostgres) ShowColumnsQuery(table string) string { 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) } +// get column types of postgresql. func (d *dbBasePostgres) DbTypes() map[string]string { return postgresTypes } +// check index exist in postgresql. func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bool { query := fmt.Sprintf("SELECT COUNT(*) FROM pg_indexes WHERE tablename = '%s' AND indexname = '%s'", table, name) row := db.QueryRow(query) @@ -127,6 +141,7 @@ func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bo return cnt > 0 } +// create new postgresql dbBaser. func newdbBasePostgres() dbBaser { b := new(dbBasePostgres) b.ins = b diff --git a/orm/db_sqlite.go b/orm/db_sqlite.go index 7711ded0..81692e2c 100644 --- a/orm/db_sqlite.go +++ b/orm/db_sqlite.go @@ -5,6 +5,7 @@ import ( "fmt" ) +// sqlite operators. var sqliteOperators = map[string]string{ "exact": "= ?", "iexact": "LIKE ? ESCAPE '\\'", @@ -20,6 +21,7 @@ var sqliteOperators = map[string]string{ "iendswith": "LIKE ? ESCAPE '\\'", } +// sqlite column types. var sqliteTypes = map[string]string{ "auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT", "pk": "NOT NULL PRIMARY KEY", @@ -40,38 +42,47 @@ var sqliteTypes = map[string]string{ "float64-decimal": "decimal", } +// sqlite dbBaser. type dbBaseSqlite struct { dbBase } var _ dbBaser = new(dbBaseSqlite) +// get sqlite operator. func (d *dbBaseSqlite) OperatorSql(operator string) string { return sqliteOperators[operator] } +// generate functioned sql for sqlite. +// only support DATE(text). func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { if fi.fieldType == TypeDateField { *leftCol = fmt.Sprintf("DATE(%s)", *leftCol) } } +// unable updating joined record in sqlite. func (d *dbBaseSqlite) SupportUpdateJoin() bool { return false } +// max int in sqlite. func (d *dbBaseSqlite) MaxLimit() uint64 { return 9223372036854775807 } +// get column types in sqlite. func (d *dbBaseSqlite) DbTypes() map[string]string { return sqliteTypes } +// get show tables sql in sqlite. func (d *dbBaseSqlite) ShowTablesQuery() string { return "SELECT name FROM sqlite_master WHERE type = 'table'" } +// get columns in sqlite. func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) { query := d.ins.ShowColumnsQuery(table) rows, err := db.Query(query) @@ -92,10 +103,12 @@ func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]str return columns, nil } +// get show columns sql in sqlite. func (d *dbBaseSqlite) ShowColumnsQuery(table string) string { return fmt.Sprintf("pragma table_info('%s')", table) } +// check index exist in sqlite. func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool { query := fmt.Sprintf("PRAGMA index_list('%s')", table) rows, err := db.Query(query) @@ -113,6 +126,7 @@ func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool return false } +// create new sqlite dbBaser. func newdbBaseSqlite() dbBaser { b := new(dbBaseSqlite) b.ins = b diff --git a/orm/db_tables.go b/orm/db_tables.go index f5cacf38..854c4214 100644 --- a/orm/db_tables.go +++ b/orm/db_tables.go @@ -6,6 +6,7 @@ import ( "time" ) +// table info struct. type dbTable struct { id int index string @@ -18,6 +19,7 @@ type dbTable struct { jtl *dbTable } +// tables collection struct, contains some tables. type dbTables struct { tablesM map[string]*dbTable tables []*dbTable @@ -26,6 +28,8 @@ type dbTables struct { skipEnd bool } +// set table info to collection. +// if not exist, create new. func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) *dbTable { name := strings.Join(names, ExprSep) if j, ok := t.tablesM[name]; ok { @@ -42,6 +46,7 @@ func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) return t.tablesM[name] } +// add table info to collection. func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) (*dbTable, bool) { name := strings.Join(names, ExprSep) if _, ok := t.tablesM[name]; ok == false { @@ -54,11 +59,14 @@ func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) return t.tablesM[name], false } +// get table info in collection. func (t *dbTables) get(name string) (*dbTable, bool) { j, ok := t.tablesM[name] return j, ok } +// get related fields info in recursive depth loop. +// loop once, depth decreases one. func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related []string) []string { if depth < 0 || fi.fieldType == RelManyToMany { return related @@ -79,6 +87,7 @@ func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related [] return related } +// parse related fields. func (t *dbTables) parseRelated(rels []string, depth int) { relsNum := len(rels) @@ -140,6 +149,7 @@ func (t *dbTables) parseRelated(rels []string, depth int) { } } +// generate join string. func (t *dbTables) getJoinSql() (join string) { Q := t.base.TableQuote() @@ -186,6 +196,7 @@ func (t *dbTables) getJoinSql() (join string) { return } +// parse orm model struct field tag expression. func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string, info *fieldInfo, success bool) { var ( jtl *dbTable @@ -300,6 +311,7 @@ loopFor: return } +// generate condition sql. func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) { if cond == nil || cond.IsEmpty() { return @@ -364,6 +376,7 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe return } +// generate order sql. func (t *dbTables) getOrderSql(orders []string) (orderSql string) { if len(orders) == 0 { return @@ -392,6 +405,7 @@ func (t *dbTables) getOrderSql(orders []string) (orderSql string) { return } +// generate limit sql. func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) { if limit == 0 { limit = int64(DefaultRowsLimit) @@ -414,6 +428,7 @@ func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits return } +// crete new tables collection. func newDbTables(mi *modelInfo, base dbBaser) *dbTables { tables := &dbTables{} tables.tablesM = make(map[string]*dbTable) diff --git a/orm/db_utils.go b/orm/db_utils.go index e2178294..34de8186 100644 --- a/orm/db_utils.go +++ b/orm/db_utils.go @@ -6,6 +6,7 @@ import ( "time" ) +// get table alias. func getDbAlias(name string) *alias { if al, ok := dataBaseCache.get(name); ok { return al @@ -15,6 +16,7 @@ func getDbAlias(name string) *alias { return nil } +// get pk column info. func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) { fi := mi.fields.pk @@ -37,6 +39,7 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac return } +// get fields description as flatted string. func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) { outFor: