1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 20:10:56 +00:00

add comments for orm packages, part 2

This commit is contained in:
傅小黑 2014-01-17 17:25:17 +08:00
parent f5a5ebe16b
commit 4c527dde65
7 changed files with 59 additions and 0 deletions

View File

@ -927,6 +927,7 @@ func (d *dbBase) GenerateOperatorSql(mi *modelInfo, fi *fieldInfo, operator stri
return sql, params return sql, params
} }
// gernerate sql string with inner function, such as UPPER(text).
func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) { func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) {
// default not use // default not use
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
) )
// mysql operators.
var mysqlOperators = map[string]string{ var mysqlOperators = map[string]string{
"exact": "= ?", "exact": "= ?",
"iexact": "LIKE ?", "iexact": "LIKE ?",
@ -21,6 +22,7 @@ var mysqlOperators = map[string]string{
"iendswith": "LIKE ?", "iendswith": "LIKE ?",
} }
// mysql column field types.
var mysqlTypes = map[string]string{ var mysqlTypes = map[string]string{
"auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY", "auto": "AUTO_INCREMENT NOT NULL PRIMARY KEY",
"pk": "NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY",
@ -41,29 +43,35 @@ var mysqlTypes = map[string]string{
"float64-decimal": "numeric(%d, %d)", "float64-decimal": "numeric(%d, %d)",
} }
// mysql dbBaser implementation.
type dbBaseMysql struct { type dbBaseMysql struct {
dbBase dbBase
} }
var _ dbBaser = new(dbBaseMysql) var _ dbBaser = new(dbBaseMysql)
// get mysql operator.
func (d *dbBaseMysql) OperatorSql(operator string) string { func (d *dbBaseMysql) OperatorSql(operator string) string {
return mysqlOperators[operator] return mysqlOperators[operator]
} }
// get mysql table field types.
func (d *dbBaseMysql) DbTypes() map[string]string { func (d *dbBaseMysql) DbTypes() map[string]string {
return mysqlTypes return mysqlTypes
} }
// show table sql for mysql.
func (d *dbBaseMysql) ShowTablesQuery() string { func (d *dbBaseMysql) ShowTablesQuery() string {
return "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = DATABASE()" 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 { func (d *dbBaseMysql) ShowColumnsQuery(table string) string {
return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+ return fmt.Sprintf("SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE FROM information_schema.columns "+
"WHERE table_schema = DATABASE() AND table_name = '%s'", table) "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 { func (d *dbBaseMysql) IndexExists(db dbQuerier, table string, name string) bool {
row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+ row := db.QueryRow("SELECT count(*) FROM information_schema.statistics "+
"WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?", table, name) "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 return cnt > 0
} }
// create new mysql dbBaser.
func newdbBaseMysql() dbBaser { func newdbBaseMysql() dbBaser {
b := new(dbBaseMysql) b := new(dbBaseMysql)
b.ins = b b.ins = b

View File

@ -1,11 +1,13 @@
package orm package orm
// oracle dbBaser
type dbBaseOracle struct { type dbBaseOracle struct {
dbBase dbBase
} }
var _ dbBaser = new(dbBaseOracle) var _ dbBaser = new(dbBaseOracle)
// create oracle dbBaser.
func newdbBaseOracle() dbBaser { func newdbBaseOracle() dbBaser {
b := new(dbBaseOracle) b := new(dbBaseOracle)
b.ins = b b.ins = b

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
) )
// postgresql operators.
var postgresOperators = map[string]string{ var postgresOperators = map[string]string{
"exact": "= ?", "exact": "= ?",
"iexact": "= UPPER(?)", "iexact": "= UPPER(?)",
@ -20,6 +21,7 @@ var postgresOperators = map[string]string{
"iendswith": "LIKE UPPER(?)", "iendswith": "LIKE UPPER(?)",
} }
// postgresql column field types.
var postgresTypes = map[string]string{ var postgresTypes = map[string]string{
"auto": "serial NOT NULL PRIMARY KEY", "auto": "serial NOT NULL PRIMARY KEY",
"pk": "NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY",
@ -40,16 +42,19 @@ var postgresTypes = map[string]string{
"float64-decimal": "numeric(%d, %d)", "float64-decimal": "numeric(%d, %d)",
} }
// postgresql dbBaser.
type dbBasePostgres struct { type dbBasePostgres struct {
dbBase dbBase
} }
var _ dbBaser = new(dbBasePostgres) var _ dbBaser = new(dbBasePostgres)
// get postgresql operator.
func (d *dbBasePostgres) OperatorSql(operator string) string { func (d *dbBasePostgres) OperatorSql(operator string) string {
return postgresOperators[operator] return postgresOperators[operator]
} }
// generate functioned sql string, such as contains(text).
func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { func (d *dbBasePostgres) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) {
switch operator { switch operator {
case "contains", "startswith", "endswith": 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 { func (d *dbBasePostgres) SupportUpdateJoin() bool {
return false return false
} }
@ -67,10 +73,13 @@ func (d *dbBasePostgres) MaxLimit() uint64 {
return 0 return 0
} }
// postgresql quote is ".
func (d *dbBasePostgres) TableQuote() string { func (d *dbBasePostgres) TableQuote() string {
return `"` return `"`
} }
// postgresql value placeholder is $n.
// replace default ? to $n.
func (d *dbBasePostgres) ReplaceMarks(query *string) { func (d *dbBasePostgres) ReplaceMarks(query *string) {
q := *query q := *query
num := 0 num := 0
@ -97,6 +106,7 @@ func (d *dbBasePostgres) ReplaceMarks(query *string) {
*query = string(data) *query = string(data)
} }
// make returning sql support for postgresql.
func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) { func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool) {
if mi.fields.pk.auto { if mi.fields.pk.auto {
if query != nil { if query != nil {
@ -107,18 +117,22 @@ func (d *dbBasePostgres) HasReturningID(mi *modelInfo, query *string) (has bool)
return return
} }
// show table sql for postgresql.
func (d *dbBasePostgres) ShowTablesQuery() string { 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')" 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 { 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) 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 { func (d *dbBasePostgres) DbTypes() map[string]string {
return postgresTypes return postgresTypes
} }
// check index exist in postgresql.
func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bool { 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) query := fmt.Sprintf("SELECT COUNT(*) FROM pg_indexes WHERE tablename = '%s' AND indexname = '%s'", table, name)
row := db.QueryRow(query) row := db.QueryRow(query)
@ -127,6 +141,7 @@ func (d *dbBasePostgres) IndexExists(db dbQuerier, table string, name string) bo
return cnt > 0 return cnt > 0
} }
// create new postgresql dbBaser.
func newdbBasePostgres() dbBaser { func newdbBasePostgres() dbBaser {
b := new(dbBasePostgres) b := new(dbBasePostgres)
b.ins = b b.ins = b

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
) )
// sqlite operators.
var sqliteOperators = map[string]string{ var sqliteOperators = map[string]string{
"exact": "= ?", "exact": "= ?",
"iexact": "LIKE ? ESCAPE '\\'", "iexact": "LIKE ? ESCAPE '\\'",
@ -20,6 +21,7 @@ var sqliteOperators = map[string]string{
"iendswith": "LIKE ? ESCAPE '\\'", "iendswith": "LIKE ? ESCAPE '\\'",
} }
// sqlite column types.
var sqliteTypes = map[string]string{ var sqliteTypes = map[string]string{
"auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT", "auto": "integer NOT NULL PRIMARY KEY AUTOINCREMENT",
"pk": "NOT NULL PRIMARY KEY", "pk": "NOT NULL PRIMARY KEY",
@ -40,38 +42,47 @@ var sqliteTypes = map[string]string{
"float64-decimal": "decimal", "float64-decimal": "decimal",
} }
// sqlite dbBaser.
type dbBaseSqlite struct { type dbBaseSqlite struct {
dbBase dbBase
} }
var _ dbBaser = new(dbBaseSqlite) var _ dbBaser = new(dbBaseSqlite)
// get sqlite operator.
func (d *dbBaseSqlite) OperatorSql(operator string) string { func (d *dbBaseSqlite) OperatorSql(operator string) string {
return sqliteOperators[operator] return sqliteOperators[operator]
} }
// generate functioned sql for sqlite.
// only support DATE(text).
func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) { func (d *dbBaseSqlite) GenerateOperatorLeftCol(fi *fieldInfo, operator string, leftCol *string) {
if fi.fieldType == TypeDateField { if fi.fieldType == TypeDateField {
*leftCol = fmt.Sprintf("DATE(%s)", *leftCol) *leftCol = fmt.Sprintf("DATE(%s)", *leftCol)
} }
} }
// unable updating joined record in sqlite.
func (d *dbBaseSqlite) SupportUpdateJoin() bool { func (d *dbBaseSqlite) SupportUpdateJoin() bool {
return false return false
} }
// max int in sqlite.
func (d *dbBaseSqlite) MaxLimit() uint64 { func (d *dbBaseSqlite) MaxLimit() uint64 {
return 9223372036854775807 return 9223372036854775807
} }
// get column types in sqlite.
func (d *dbBaseSqlite) DbTypes() map[string]string { func (d *dbBaseSqlite) DbTypes() map[string]string {
return sqliteTypes return sqliteTypes
} }
// get show tables sql in sqlite.
func (d *dbBaseSqlite) ShowTablesQuery() string { func (d *dbBaseSqlite) ShowTablesQuery() string {
return "SELECT name FROM sqlite_master WHERE type = 'table'" 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) { func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]string, error) {
query := d.ins.ShowColumnsQuery(table) query := d.ins.ShowColumnsQuery(table)
rows, err := db.Query(query) rows, err := db.Query(query)
@ -92,10 +103,12 @@ func (d *dbBaseSqlite) GetColumns(db dbQuerier, table string) (map[string][3]str
return columns, nil return columns, nil
} }
// get show columns sql in sqlite.
func (d *dbBaseSqlite) ShowColumnsQuery(table string) string { func (d *dbBaseSqlite) ShowColumnsQuery(table string) string {
return fmt.Sprintf("pragma table_info('%s')", table) return fmt.Sprintf("pragma table_info('%s')", table)
} }
// check index exist in sqlite.
func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool { func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool {
query := fmt.Sprintf("PRAGMA index_list('%s')", table) query := fmt.Sprintf("PRAGMA index_list('%s')", table)
rows, err := db.Query(query) rows, err := db.Query(query)
@ -113,6 +126,7 @@ func (d *dbBaseSqlite) IndexExists(db dbQuerier, table string, name string) bool
return false return false
} }
// create new sqlite dbBaser.
func newdbBaseSqlite() dbBaser { func newdbBaseSqlite() dbBaser {
b := new(dbBaseSqlite) b := new(dbBaseSqlite)
b.ins = b b.ins = b

View File

@ -6,6 +6,7 @@ import (
"time" "time"
) )
// table info struct.
type dbTable struct { type dbTable struct {
id int id int
index string index string
@ -18,6 +19,7 @@ type dbTable struct {
jtl *dbTable jtl *dbTable
} }
// tables collection struct, contains some tables.
type dbTables struct { type dbTables struct {
tablesM map[string]*dbTable tablesM map[string]*dbTable
tables []*dbTable tables []*dbTable
@ -26,6 +28,8 @@ type dbTables struct {
skipEnd bool 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 { func (t *dbTables) set(names []string, mi *modelInfo, fi *fieldInfo, inner bool) *dbTable {
name := strings.Join(names, ExprSep) name := strings.Join(names, ExprSep)
if j, ok := t.tablesM[name]; ok { 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] return t.tablesM[name]
} }
// add table info to collection.
func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) (*dbTable, bool) { func (t *dbTables) add(names []string, mi *modelInfo, fi *fieldInfo, inner bool) (*dbTable, bool) {
name := strings.Join(names, ExprSep) name := strings.Join(names, ExprSep)
if _, ok := t.tablesM[name]; ok == false { 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 return t.tablesM[name], false
} }
// get table info in collection.
func (t *dbTables) get(name string) (*dbTable, bool) { func (t *dbTables) get(name string) (*dbTable, bool) {
j, ok := t.tablesM[name] j, ok := t.tablesM[name]
return j, ok 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 { func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related []string) []string {
if depth < 0 || fi.fieldType == RelManyToMany { if depth < 0 || fi.fieldType == RelManyToMany {
return related return related
@ -79,6 +87,7 @@ func (t *dbTables) loopDepth(depth int, prefix string, fi *fieldInfo, related []
return related return related
} }
// parse related fields.
func (t *dbTables) parseRelated(rels []string, depth int) { func (t *dbTables) parseRelated(rels []string, depth int) {
relsNum := len(rels) relsNum := len(rels)
@ -140,6 +149,7 @@ func (t *dbTables) parseRelated(rels []string, depth int) {
} }
} }
// generate join string.
func (t *dbTables) getJoinSql() (join string) { func (t *dbTables) getJoinSql() (join string) {
Q := t.base.TableQuote() Q := t.base.TableQuote()
@ -186,6 +196,7 @@ func (t *dbTables) getJoinSql() (join string) {
return return
} }
// parse orm model struct field tag expression.
func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string, info *fieldInfo, success bool) { func (t *dbTables) parseExprs(mi *modelInfo, exprs []string) (index, name string, info *fieldInfo, success bool) {
var ( var (
jtl *dbTable jtl *dbTable
@ -300,6 +311,7 @@ loopFor:
return return
} }
// generate condition sql.
func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) { func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (where string, params []interface{}) {
if cond == nil || cond.IsEmpty() { if cond == nil || cond.IsEmpty() {
return return
@ -364,6 +376,7 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe
return return
} }
// generate order sql.
func (t *dbTables) getOrderSql(orders []string) (orderSql string) { func (t *dbTables) getOrderSql(orders []string) (orderSql string) {
if len(orders) == 0 { if len(orders) == 0 {
return return
@ -392,6 +405,7 @@ func (t *dbTables) getOrderSql(orders []string) (orderSql string) {
return return
} }
// generate limit sql.
func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) { func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits string) {
if limit == 0 { if limit == 0 {
limit = int64(DefaultRowsLimit) limit = int64(DefaultRowsLimit)
@ -414,6 +428,7 @@ func (t *dbTables) getLimitSql(mi *modelInfo, offset int64, limit int64) (limits
return return
} }
// crete new tables collection.
func newDbTables(mi *modelInfo, base dbBaser) *dbTables { func newDbTables(mi *modelInfo, base dbBaser) *dbTables {
tables := &dbTables{} tables := &dbTables{}
tables.tablesM = make(map[string]*dbTable) tables.tablesM = make(map[string]*dbTable)

View File

@ -6,6 +6,7 @@ import (
"time" "time"
) )
// get table alias.
func getDbAlias(name string) *alias { func getDbAlias(name string) *alias {
if al, ok := dataBaseCache.get(name); ok { if al, ok := dataBaseCache.get(name); ok {
return al return al
@ -15,6 +16,7 @@ func getDbAlias(name string) *alias {
return nil return nil
} }
// get pk column info.
func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) { func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) {
fi := mi.fields.pk fi := mi.fields.pk
@ -37,6 +39,7 @@ func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interfac
return return
} }
// get fields description as flatted string.
func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) { func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) {
outFor: outFor: