| @@ -124,27 +124,47 @@ func (engine *Engine) QuoteStr() string { | |||||
| } | } | ||||
| // Quote Use QuoteStr quote the string sql | // Quote Use QuoteStr quote the string sql | ||||
| func (engine *Engine) Quote(sql string) string { | |||||
| return engine.quoteTable(sql) | |||||
| } | |||||
| func (engine *Engine) Quote(value string) string { | |||||
| value = strings.TrimSpace(value) | |||||
| if len(value) == 0 { | |||||
| return value | |||||
| } | |||||
| func (engine *Engine) quote(sql string) string { | |||||
| return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr() | |||||
| if string(value[0]) == engine.dialect.QuoteStr() || value[0] == '`' { | |||||
| return value | |||||
| } | |||||
| value = strings.Replace(value, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1) | |||||
| return engine.dialect.QuoteStr() + value + engine.dialect.QuoteStr() | |||||
| } | } | ||||
| func (engine *Engine) quoteTable(keyName string) string { | |||||
| keyName = strings.TrimSpace(keyName) | |||||
| if len(keyName) == 0 { | |||||
| return keyName | |||||
| // QuoteTo quotes string and writes into the buffer | |||||
| func (engine *Engine) QuoteTo(buf *bytes.Buffer, value string) { | |||||
| if buf == nil { | |||||
| return | |||||
| } | |||||
| value = strings.TrimSpace(value) | |||||
| if value == "" { | |||||
| return | |||||
| } | } | ||||
| if string(keyName[0]) == engine.dialect.QuoteStr() || keyName[0] == '`' { | |||||
| return keyName | |||||
| if string(value[0]) == engine.dialect.QuoteStr() || value[0] == '`' { | |||||
| buf.WriteString(value) | |||||
| return | |||||
| } | } | ||||
| keyName = strings.Replace(keyName, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1) | |||||
| value = strings.Replace(value, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1) | |||||
| return engine.dialect.QuoteStr() + keyName + engine.dialect.QuoteStr() | |||||
| buf.WriteString(engine.dialect.QuoteStr()) | |||||
| buf.WriteString(value) | |||||
| buf.WriteString(engine.dialect.QuoteStr()) | |||||
| } | |||||
| func (engine *Engine) quote(sql string) string { | |||||
| return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr() | |||||
| } | } | ||||
| // SqlType will be depracated, please use SQLType instead | // SqlType will be depracated, please use SQLType instead | ||||
| @@ -92,7 +92,7 @@ func (m *LRUCacher) GC() { | |||||
| } | } | ||||
| } | } | ||||
| // Get all bean's ids according to sql and parameter from cache | |||||
| // GetIds returns all bean's ids according to sql and parameter from cache | |||||
| func (m *LRUCacher) GetIds(tableName, sql string) interface{} { | func (m *LRUCacher) GetIds(tableName, sql string) interface{} { | ||||
| m.mutex.Lock() | m.mutex.Lock() | ||||
| defer m.mutex.Unlock() | defer m.mutex.Unlock() | ||||
| @@ -121,7 +121,7 @@ func (m *LRUCacher) GetIds(tableName, sql string) interface{} { | |||||
| return nil | return nil | ||||
| } | } | ||||
| // Get bean according tableName and id from cache | |||||
| // GetBean returns bean according tableName and id from cache | |||||
| func (m *LRUCacher) GetBean(tableName string, id string) interface{} { | func (m *LRUCacher) GetBean(tableName string, id string) interface{} { | ||||
| m.mutex.Lock() | m.mutex.Lock() | ||||
| defer m.mutex.Unlock() | defer m.mutex.Unlock() | ||||
| @@ -12,7 +12,7 @@ import ( | |||||
| var _ core.CacheStore = NewMemoryStore() | var _ core.CacheStore = NewMemoryStore() | ||||
| // memory store | |||||
| // MemoryStore represents in-memory store | |||||
| type MemoryStore struct { | type MemoryStore struct { | ||||
| store map[interface{}]interface{} | store map[interface{}]interface{} | ||||
| mutex sync.RWMutex | mutex sync.RWMutex | ||||
| @@ -5,7 +5,6 @@ | |||||
| package xorm | package xorm | ||||
| import ( | import ( | ||||
| "errors" | |||||
| "fmt" | "fmt" | ||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| @@ -258,8 +257,9 @@ func (db *mssql) SqlType(c *core.Column) string { | |||||
| return core.Int | return core.Int | ||||
| } | } | ||||
| var hasLen1 bool = (c.Length > 0) | |||||
| var hasLen2 bool = (c.Length2 > 0) | |||||
| hasLen1 := (c.Length > 0) | |||||
| hasLen2 := (c.Length2 > 0) | |||||
| if hasLen2 { | if hasLen2 { | ||||
| res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | ||||
| } else if hasLen1 { | } else if hasLen1 { | ||||
| @@ -371,17 +371,16 @@ func (db *mssql) GetColumns(tableName string) ([]string, map[string]*core.Column | |||||
| } | } | ||||
| switch ct { | switch ct { | ||||
| case "DATETIMEOFFSET": | case "DATETIMEOFFSET": | ||||
| col.SQLType = core.SQLType{core.TimeStampz, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.TimeStampz, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "NVARCHAR": | case "NVARCHAR": | ||||
| col.SQLType = core.SQLType{core.NVarchar, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.NVarchar, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "IMAGE": | case "IMAGE": | ||||
| col.SQLType = core.SQLType{core.VarBinary, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.VarBinary, DefaultLength: 0, DefaultLength2: 0} | |||||
| default: | default: | ||||
| if _, ok := core.SqlTypes[ct]; ok { | if _, ok := core.SqlTypes[ct]; ok { | ||||
| col.SQLType = core.SQLType{ct, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: ct, DefaultLength: 0, DefaultLength2: 0} | |||||
| } else { | } else { | ||||
| return nil, nil, errors.New(fmt.Sprintf("unknow colType %v for %v - %v", | |||||
| ct, tableName, col.Name)) | |||||
| return nil, nil, fmt.Errorf("Unknown colType %v for %v - %v", ct, tableName, col.Name) | |||||
| } | } | ||||
| } | } | ||||
| @@ -6,7 +6,6 @@ package xorm | |||||
| import ( | import ( | ||||
| "crypto/tls" | "crypto/tls" | ||||
| "errors" | |||||
| "fmt" | "fmt" | ||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| @@ -202,7 +201,7 @@ func (db *mysql) SqlType(c *core.Column) string { | |||||
| res = core.Enum | res = core.Enum | ||||
| res += "(" | res += "(" | ||||
| opts := "" | opts := "" | ||||
| for v, _ := range c.EnumOptions { | |||||
| for v := range c.EnumOptions { | |||||
| opts += fmt.Sprintf(",'%v'", v) | opts += fmt.Sprintf(",'%v'", v) | ||||
| } | } | ||||
| res += strings.TrimLeft(opts, ",") | res += strings.TrimLeft(opts, ",") | ||||
| @@ -211,7 +210,7 @@ func (db *mysql) SqlType(c *core.Column) string { | |||||
| res = core.Set | res = core.Set | ||||
| res += "(" | res += "(" | ||||
| opts := "" | opts := "" | ||||
| for v, _ := range c.SetOptions { | |||||
| for v := range c.SetOptions { | |||||
| opts += fmt.Sprintf(",'%v'", v) | opts += fmt.Sprintf(",'%v'", v) | ||||
| } | } | ||||
| res += strings.TrimLeft(opts, ",") | res += strings.TrimLeft(opts, ",") | ||||
| @@ -227,8 +226,8 @@ func (db *mysql) SqlType(c *core.Column) string { | |||||
| res = t | res = t | ||||
| } | } | ||||
| var hasLen1 bool = (c.Length > 0) | |||||
| var hasLen2 bool = (c.Length2 > 0) | |||||
| hasLen1 := (c.Length > 0) | |||||
| hasLen2 := (c.Length2 > 0) | |||||
| if res == core.BigInt && !hasLen1 && !hasLen2 { | if res == core.BigInt && !hasLen1 && !hasLen2 { | ||||
| c.Length = 20 | c.Length = 20 | ||||
| @@ -373,9 +372,9 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column | |||||
| col.Length = len1 | col.Length = len1 | ||||
| col.Length2 = len2 | col.Length2 = len2 | ||||
| if _, ok := core.SqlTypes[colType]; ok { | if _, ok := core.SqlTypes[colType]; ok { | ||||
| col.SQLType = core.SQLType{colType, len1, len2} | |||||
| col.SQLType = core.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2} | |||||
| } else { | } else { | ||||
| return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v", colType)) | |||||
| return nil, nil, fmt.Errorf("Unknown colType %v", colType) | |||||
| } | } | ||||
| if colKey == "PRI" { | if colKey == "PRI" { | ||||
| @@ -5,7 +5,6 @@ | |||||
| package xorm | package xorm | ||||
| import ( | import ( | ||||
| "errors" | |||||
| "fmt" | "fmt" | ||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| @@ -526,8 +525,9 @@ func (db *oracle) SqlType(c *core.Column) string { | |||||
| res = t | res = t | ||||
| } | } | ||||
| var hasLen1 bool = (c.Length > 0) | |||||
| var hasLen2 bool = (c.Length2 > 0) | |||||
| hasLen1 := (c.Length > 0) | |||||
| hasLen2 := (c.Length2 > 0) | |||||
| if hasLen2 { | if hasLen2 { | ||||
| res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | ||||
| } else if hasLen1 { | } else if hasLen1 { | ||||
| @@ -577,14 +577,14 @@ func (db *oracle) DropTableSql(tableName string) string { | |||||
| return fmt.Sprintf("DROP TABLE `%s`", tableName) | return fmt.Sprintf("DROP TABLE `%s`", tableName) | ||||
| } | } | ||||
| func (b *oracle) CreateTableSql(table *core.Table, tableName, storeEngine, charset string) string { | |||||
| func (db *oracle) CreateTableSql(table *core.Table, tableName, storeEngine, charset string) string { | |||||
| var sql string | var sql string | ||||
| sql = "CREATE TABLE " | sql = "CREATE TABLE " | ||||
| if tableName == "" { | if tableName == "" { | ||||
| tableName = table.Name | tableName = table.Name | ||||
| } | } | ||||
| sql += b.Quote(tableName) + " (" | |||||
| sql += db.Quote(tableName) + " (" | |||||
| pkList := table.PrimaryKeys | pkList := table.PrimaryKeys | ||||
| @@ -593,7 +593,7 @@ func (b *oracle) CreateTableSql(table *core.Table, tableName, storeEngine, chars | |||||
| /*if col.IsPrimaryKey && len(pkList) == 1 { | /*if col.IsPrimaryKey && len(pkList) == 1 { | ||||
| sql += col.String(b.dialect) | sql += col.String(b.dialect) | ||||
| } else {*/ | } else {*/ | ||||
| sql += col.StringNoPk(b) | |||||
| sql += col.StringNoPk(db) | |||||
| //} | //} | ||||
| sql = strings.TrimSpace(sql) | sql = strings.TrimSpace(sql) | ||||
| sql += ", " | sql += ", " | ||||
| @@ -601,17 +601,17 @@ func (b *oracle) CreateTableSql(table *core.Table, tableName, storeEngine, chars | |||||
| if len(pkList) > 0 { | if len(pkList) > 0 { | ||||
| sql += "PRIMARY KEY ( " | sql += "PRIMARY KEY ( " | ||||
| sql += b.Quote(strings.Join(pkList, b.Quote(","))) | |||||
| sql += db.Quote(strings.Join(pkList, db.Quote(","))) | |||||
| sql += " ), " | sql += " ), " | ||||
| } | } | ||||
| sql = sql[:len(sql)-2] + ")" | sql = sql[:len(sql)-2] + ")" | ||||
| if b.SupportEngine() && storeEngine != "" { | |||||
| if db.SupportEngine() && storeEngine != "" { | |||||
| sql += " ENGINE=" + storeEngine | sql += " ENGINE=" + storeEngine | ||||
| } | } | ||||
| if b.SupportCharset() { | |||||
| if db.SupportCharset() { | |||||
| if len(charset) == 0 { | if len(charset) == 0 { | ||||
| charset = b.URI().Charset | |||||
| charset = db.URI().Charset | |||||
| } | } | ||||
| if len(charset) > 0 { | if len(charset) > 0 { | ||||
| sql += " DEFAULT CHARSET " + charset | sql += " DEFAULT CHARSET " + charset | ||||
| @@ -733,23 +733,23 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Colum | |||||
| switch dt { | switch dt { | ||||
| case "VARCHAR2": | case "VARCHAR2": | ||||
| col.SQLType = core.SQLType{core.Varchar, len1, len2} | |||||
| col.SQLType = core.SQLType{Name: core.Varchar, DefaultLength: len1, DefaultLength2: len2} | |||||
| case "NVARCHAR2": | case "NVARCHAR2": | ||||
| col.SQLType = core.SQLType{core.NVarchar, len1, len2} | |||||
| col.SQLType = core.SQLType{Name: core.NVarchar, DefaultLength: len1, DefaultLength2: len2} | |||||
| case "TIMESTAMP WITH TIME ZONE": | case "TIMESTAMP WITH TIME ZONE": | ||||
| col.SQLType = core.SQLType{core.TimeStampz, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.TimeStampz, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "NUMBER": | case "NUMBER": | ||||
| col.SQLType = core.SQLType{core.Double, len1, len2} | |||||
| col.SQLType = core.SQLType{Name: core.Double, DefaultLength: len1, DefaultLength2: len2} | |||||
| case "LONG", "LONG RAW": | case "LONG", "LONG RAW": | ||||
| col.SQLType = core.SQLType{core.Text, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Text, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "RAW": | case "RAW": | ||||
| col.SQLType = core.SQLType{core.Binary, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Binary, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "ROWID": | case "ROWID": | ||||
| col.SQLType = core.SQLType{core.Varchar, 18, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Varchar, DefaultLength: 18, DefaultLength2: 0} | |||||
| case "AQ$_SUBSCRIBERS": | case "AQ$_SUBSCRIBERS": | ||||
| ignore = true | ignore = true | ||||
| default: | default: | ||||
| col.SQLType = core.SQLType{strings.ToUpper(dt), len1, len2} | |||||
| col.SQLType = core.SQLType{Name: strings.ToUpper(dt), DefaultLength: len1, DefaultLength2: len2} | |||||
| } | } | ||||
| if ignore { | if ignore { | ||||
| @@ -757,7 +757,7 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Colum | |||||
| } | } | ||||
| if _, ok := core.SqlTypes[col.SQLType.Name]; !ok { | if _, ok := core.SqlTypes[col.SQLType.Name]; !ok { | ||||
| return nil, nil, errors.New(fmt.Sprintf("unkonw colType %v %v", *dataType, col.SQLType)) | |||||
| return nil, nil, fmt.Errorf("Unknown colType %v %v", *dataType, col.SQLType) | |||||
| } | } | ||||
| col.Length = dataLen | col.Length = dataLen | ||||
| @@ -842,5 +842,5 @@ func (db *oracle) GetIndexes(tableName string) (map[string]*core.Index, error) { | |||||
| } | } | ||||
| func (db *oracle) Filters() []core.Filter { | func (db *oracle) Filters() []core.Filter { | ||||
| return []core.Filter{&core.QuoteFilter{}, &core.SeqFilter{":", 1}, &core.IdFilter{}} | |||||
| return []core.Filter{&core.QuoteFilter{}, &core.SeqFilter{Prefix: ":", Start: 1}, &core.IdFilter{}} | |||||
| } | } | ||||
| @@ -5,7 +5,6 @@ | |||||
| package xorm | package xorm | ||||
| import ( | import ( | ||||
| "errors" | |||||
| "fmt" | "fmt" | ||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| @@ -813,8 +812,9 @@ func (db *postgres) SqlType(c *core.Column) string { | |||||
| res = t | res = t | ||||
| } | } | ||||
| var hasLen1 bool = (c.Length > 0) | |||||
| var hasLen2 bool = (c.Length2 > 0) | |||||
| hasLen1 := (c.Length > 0) | |||||
| hasLen2 := (c.Length2 > 0) | |||||
| if hasLen2 { | if hasLen2 { | ||||
| res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" | ||||
| } else if hasLen1 { | } else if hasLen1 { | ||||
| @@ -880,9 +880,10 @@ func (db *postgres) ModifyColumnSql(tableName string, col *core.Column) string { | |||||
| } | } | ||||
| func (db *postgres) DropIndexSql(tableName string, index *core.Index) string { | func (db *postgres) DropIndexSql(tableName string, index *core.Index) string { | ||||
| quote := db.Quote | |||||
| //var unique string | //var unique string | ||||
| var idxName string = index.Name | |||||
| quote := db.Quote | |||||
| idxName := index.Name | |||||
| if !strings.HasPrefix(idxName, "UQE_") && | if !strings.HasPrefix(idxName, "UQE_") && | ||||
| !strings.HasPrefix(idxName, "IDX_") { | !strings.HasPrefix(idxName, "IDX_") { | ||||
| if index.Type == core.UniqueType { | if index.Type == core.UniqueType { | ||||
| @@ -973,24 +974,24 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND s.table_schema = $2 AND f.att | |||||
| switch dataType { | switch dataType { | ||||
| case "character varying", "character": | case "character varying", "character": | ||||
| col.SQLType = core.SQLType{core.Varchar, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Varchar, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "timestamp without time zone": | case "timestamp without time zone": | ||||
| col.SQLType = core.SQLType{core.DateTime, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.DateTime, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "timestamp with time zone": | case "timestamp with time zone": | ||||
| col.SQLType = core.SQLType{core.TimeStampz, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.TimeStampz, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "double precision": | case "double precision": | ||||
| col.SQLType = core.SQLType{core.Double, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Double, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "boolean": | case "boolean": | ||||
| col.SQLType = core.SQLType{core.Bool, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Bool, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "time without time zone": | case "time without time zone": | ||||
| col.SQLType = core.SQLType{core.Time, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.Time, DefaultLength: 0, DefaultLength2: 0} | |||||
| case "oid": | case "oid": | ||||
| col.SQLType = core.SQLType{core.BigInt, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: core.BigInt, DefaultLength: 0, DefaultLength2: 0} | |||||
| default: | default: | ||||
| col.SQLType = core.SQLType{strings.ToUpper(dataType), 0, 0} | |||||
| col.SQLType = core.SQLType{Name: strings.ToUpper(dataType), DefaultLength: 0, DefaultLength2: 0} | |||||
| } | } | ||||
| if _, ok := core.SqlTypes[col.SQLType.Name]; !ok { | if _, ok := core.SqlTypes[col.SQLType.Name]; !ok { | ||||
| return nil, nil, errors.New(fmt.Sprintf("unknow colType: %v", dataType)) | |||||
| return nil, nil, fmt.Errorf("Unknown colType: %v", dataType) | |||||
| } | } | ||||
| col.Length = maxLen | col.Length = maxLen | ||||
| @@ -1087,5 +1088,5 @@ func (db *postgres) GetIndexes(tableName string) (map[string]*core.Index, error) | |||||
| } | } | ||||
| func (db *postgres) Filters() []core.Filter { | func (db *postgres) Filters() []core.Filter { | ||||
| return []core.Filter{&core.IdFilter{}, &core.QuoteFilter{}, &core.SeqFilter{"$", 1}} | |||||
| return []core.Filter{&core.IdFilter{}, &core.QuoteFilter{}, &core.SeqFilter{Prefix: "$", Start: 1}} | |||||
| } | } | ||||
| @@ -4,17 +4,17 @@ | |||||
| package xorm | package xorm | ||||
| // Executed before an object is initially persisted to the database | |||||
| // BeforeInsertProcessor executed before an object is initially persisted to the database | |||||
| type BeforeInsertProcessor interface { | type BeforeInsertProcessor interface { | ||||
| BeforeInsert() | BeforeInsert() | ||||
| } | } | ||||
| // Executed before an object is updated | |||||
| // BeforeUpdateProcessor executed before an object is updated | |||||
| type BeforeUpdateProcessor interface { | type BeforeUpdateProcessor interface { | ||||
| BeforeUpdate() | BeforeUpdate() | ||||
| } | } | ||||
| // Executed before an object is deleted | |||||
| // BeforeDeleteProcessor executed before an object is deleted | |||||
| type BeforeDeleteProcessor interface { | type BeforeDeleteProcessor interface { | ||||
| BeforeDelete() | BeforeDelete() | ||||
| } | } | ||||
| @@ -34,17 +34,17 @@ type AfterSetProcessor interface { | |||||
| //} | //} | ||||
| // -- | // -- | ||||
| // Executed after an object is persisted to the database | |||||
| // AfterInsertProcessor executed after an object is persisted to the database | |||||
| type AfterInsertProcessor interface { | type AfterInsertProcessor interface { | ||||
| AfterInsert() | AfterInsert() | ||||
| } | } | ||||
| // Executed after an object has been updated | |||||
| // AfterUpdateProcessor executed after an object has been updated | |||||
| type AfterUpdateProcessor interface { | type AfterUpdateProcessor interface { | ||||
| AfterUpdate() | AfterUpdate() | ||||
| } | } | ||||
| // Executed after an object has been deleted | |||||
| // AfterDeleteProcessor executed after an object has been deleted | |||||
| type AfterDeleteProcessor interface { | type AfterDeleteProcessor interface { | ||||
| AfterDelete() | AfterDelete() | ||||
| } | } | ||||
| @@ -371,7 +371,7 @@ func (session *Session) DB() *core.DB { | |||||
| return session.db | return session.db | ||||
| } | } | ||||
| // Cond return session's conditions | |||||
| // Conds returns session query conditions | |||||
| func (session *Session) Conds() builder.Cond { | func (session *Session) Conds() builder.Cond { | ||||
| return session.Statement.cond | return session.Statement.cond | ||||
| } | } | ||||
| @@ -1119,11 +1119,12 @@ func (session *Session) Count(bean interface{}) (int64, error) { | |||||
| } else { | } else { | ||||
| err = session.Tx.QueryRow(sqlStr, args...).Scan(&total) | err = session.Tx.QueryRow(sqlStr, args...).Scan(&total) | ||||
| } | } | ||||
| if err != nil { | |||||
| return 0, err | |||||
| if err == sql.ErrNoRows || err == nil { | |||||
| return total, nil | |||||
| } | } | ||||
| return total, nil | |||||
| return 0, err | |||||
| } | } | ||||
| // Sum call sum some column. bean's non-empty fields are conditions. | // Sum call sum some column. bean's non-empty fields are conditions. | ||||
| @@ -1151,11 +1152,11 @@ func (session *Session) Sum(bean interface{}, columnName string) (float64, error | |||||
| } else { | } else { | ||||
| err = session.Tx.QueryRow(sqlStr, args...).Scan(&res) | err = session.Tx.QueryRow(sqlStr, args...).Scan(&res) | ||||
| } | } | ||||
| if err != nil { | |||||
| return 0, err | |||||
| } | |||||
| return res, nil | |||||
| if err == sql.ErrNoRows || err == nil { | |||||
| return res, nil | |||||
| } | |||||
| return 0, err | |||||
| } | } | ||||
| // Sums call sum some columns. bean's non-empty fields are conditions. | // Sums call sum some columns. bean's non-empty fields are conditions. | ||||
| @@ -1183,11 +1184,11 @@ func (session *Session) Sums(bean interface{}, columnNames ...string) ([]float64 | |||||
| } else { | } else { | ||||
| err = session.Tx.QueryRow(sqlStr, args...).ScanSlice(&res) | err = session.Tx.QueryRow(sqlStr, args...).ScanSlice(&res) | ||||
| } | } | ||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| return res, nil | |||||
| if err == sql.ErrNoRows || err == nil { | |||||
| return res, nil | |||||
| } | |||||
| return nil, err | |||||
| } | } | ||||
| // SumsInt sum specify columns and return as []int64 instead of []float64 | // SumsInt sum specify columns and return as []int64 instead of []float64 | ||||
| @@ -1215,11 +1216,11 @@ func (session *Session) SumsInt(bean interface{}, columnNames ...string) ([]int6 | |||||
| } else { | } else { | ||||
| err = session.Tx.QueryRow(sqlStr, args...).ScanSlice(&res) | err = session.Tx.QueryRow(sqlStr, args...).ScanSlice(&res) | ||||
| } | } | ||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| return res, nil | |||||
| if err == sql.ErrNoRows || err == nil { | |||||
| return res, nil | |||||
| } | |||||
| return nil, err | |||||
| } | } | ||||
| func (session *Session) noCacheFind(sliceValue reflect.Value, sqlStr string, args ...interface{}) error { | func (session *Session) noCacheFind(sliceValue reflect.Value, sqlStr string, args ...interface{}) error { | ||||
| @@ -1499,10 +1500,13 @@ func (session *Session) isTableEmpty(tableName string) (bool, error) { | |||||
| } | } | ||||
| var total int64 | var total int64 | ||||
| sql := fmt.Sprintf("select count(*) from %s", session.Engine.Quote(tableName)) | |||||
| err := session.DB().QueryRow(sql).Scan(&total) | |||||
| session.saveLastSQL(sql) | |||||
| sqlStr := fmt.Sprintf("select count(*) from %s", session.Engine.Quote(tableName)) | |||||
| err := session.DB().QueryRow(sqlStr).Scan(&total) | |||||
| session.saveLastSQL(sqlStr) | |||||
| if err != nil { | if err != nil { | ||||
| if err == sql.ErrNoRows { | |||||
| err = nil | |||||
| } | |||||
| return true, err | return true, err | ||||
| } | } | ||||
| @@ -237,9 +237,10 @@ func (db *sqlite3) TableCheckSql(tableName string) (string, []interface{}) { | |||||
| } | } | ||||
| func (db *sqlite3) DropIndexSql(tableName string, index *core.Index) string { | func (db *sqlite3) DropIndexSql(tableName string, index *core.Index) string { | ||||
| quote := db.Quote | |||||
| //var unique string | //var unique string | ||||
| var idxName string = index.Name | |||||
| quote := db.Quote | |||||
| idxName := index.Name | |||||
| if !strings.HasPrefix(idxName, "UQE_") && | if !strings.HasPrefix(idxName, "UQE_") && | ||||
| !strings.HasPrefix(idxName, "IDX_") { | !strings.HasPrefix(idxName, "IDX_") { | ||||
| if index.Type == core.UniqueType { | if index.Type == core.UniqueType { | ||||
| @@ -319,7 +320,7 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu | |||||
| col.Name = strings.Trim(field, "`[] ") | col.Name = strings.Trim(field, "`[] ") | ||||
| continue | continue | ||||
| } else if idx == 1 { | } else if idx == 1 { | ||||
| col.SQLType = core.SQLType{field, 0, 0} | |||||
| col.SQLType = core.SQLType{Name: field, DefaultLength: 0, DefaultLength2: 0} | |||||
| } | } | ||||
| switch field { | switch field { | ||||
| case "PRIMARY": | case "PRIMARY": | ||||
| @@ -128,7 +128,7 @@ func (statement *Statement) Alias(alias string) *Statement { | |||||
| return statement | return statement | ||||
| } | } | ||||
| // Sql add the raw sql statement | |||||
| // SQL adds raw sql statement | |||||
| func (statement *Statement) SQL(query interface{}, args ...interface{}) *Statement { | func (statement *Statement) SQL(query interface{}, args ...interface{}) *Statement { | ||||
| switch query.(type) { | switch query.(type) { | ||||
| case (*builder.Builder): | case (*builder.Builder): | ||||
| @@ -795,23 +795,23 @@ func (statement *Statement) col2NewColsWithQuote(columns ...string) []string { | |||||
| return newColumns | return newColumns | ||||
| } | } | ||||
| // Generate "Distince col1, col2 " statment | |||||
| // Distinct generates "DISTINCT col1, col2 " statement | |||||
| func (statement *Statement) Distinct(columns ...string) *Statement { | func (statement *Statement) Distinct(columns ...string) *Statement { | ||||
| statement.IsDistinct = true | statement.IsDistinct = true | ||||
| statement.Cols(columns...) | statement.Cols(columns...) | ||||
| return statement | return statement | ||||
| } | } | ||||
| // Generate "SELECT ... FOR UPDATE" statment | |||||
| // ForUpdate generates "SELECT ... FOR UPDATE" statement | |||||
| func (statement *Statement) ForUpdate() *Statement { | func (statement *Statement) ForUpdate() *Statement { | ||||
| statement.IsForUpdate = true | statement.IsForUpdate = true | ||||
| return statement | return statement | ||||
| } | } | ||||
| // Select replace select | // Select replace select | ||||
| func (s *Statement) Select(str string) *Statement { | |||||
| s.selectStr = str | |||||
| return s | |||||
| func (statement *Statement) Select(str string) *Statement { | |||||
| statement.selectStr = str | |||||
| return statement | |||||
| } | } | ||||
| // Cols generate "col1, col2" statement | // Cols generate "col1, col2" statement | ||||
| @@ -985,41 +985,45 @@ func (statement *Statement) Unscoped() *Statement { | |||||
| } | } | ||||
| func (statement *Statement) genColumnStr() string { | func (statement *Statement) genColumnStr() string { | ||||
| table := statement.RefTable | |||||
| var colNames []string | |||||
| for _, col := range table.Columns() { | |||||
| var buf bytes.Buffer | |||||
| columns := statement.RefTable.Columns() | |||||
| for _, col := range columns { | |||||
| if statement.OmitStr != "" { | if statement.OmitStr != "" { | ||||
| if _, ok := statement.columnMap[strings.ToLower(col.Name)]; ok { | if _, ok := statement.columnMap[strings.ToLower(col.Name)]; ok { | ||||
| continue | continue | ||||
| } | } | ||||
| } | } | ||||
| if col.MapType == core.ONLYTODB { | if col.MapType == core.ONLYTODB { | ||||
| continue | continue | ||||
| } | } | ||||
| if buf.Len() != 0 { | |||||
| buf.WriteString(", ") | |||||
| } | |||||
| if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" { | |||||
| buf.WriteString("id() AS ") | |||||
| } | |||||
| if statement.JoinStr != "" { | if statement.JoinStr != "" { | ||||
| var name string | |||||
| if statement.TableAlias != "" { | if statement.TableAlias != "" { | ||||
| name = statement.Engine.Quote(statement.TableAlias) | |||||
| } else { | |||||
| name = statement.Engine.Quote(statement.TableName()) | |||||
| } | |||||
| name += "." + statement.Engine.Quote(col.Name) | |||||
| if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" { | |||||
| colNames = append(colNames, "id() AS "+name) | |||||
| } else { | |||||
| colNames = append(colNames, name) | |||||
| } | |||||
| } else { | |||||
| name := statement.Engine.Quote(col.Name) | |||||
| if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" { | |||||
| colNames = append(colNames, "id() AS "+name) | |||||
| buf.WriteString(statement.TableAlias) | |||||
| } else { | } else { | ||||
| colNames = append(colNames, name) | |||||
| buf.WriteString(statement.TableName()) | |||||
| } | } | ||||
| buf.WriteString(".") | |||||
| } | } | ||||
| statement.Engine.QuoteTo(&buf, col.Name) | |||||
| } | } | ||||
| return strings.Join(colNames, ", ") | |||||
| return buf.String() | |||||
| } | } | ||||
| func (statement *Statement) genCreateTableSQL() string { | func (statement *Statement) genCreateTableSQL() string { | ||||
| @@ -1027,11 +1031,11 @@ func (statement *Statement) genCreateTableSQL() string { | |||||
| statement.StoreEngine, statement.Charset) | statement.StoreEngine, statement.Charset) | ||||
| } | } | ||||
| func (s *Statement) genIndexSQL() []string { | |||||
| func (statement *Statement) genIndexSQL() []string { | |||||
| var sqls []string | var sqls []string | ||||
| tbName := s.TableName() | |||||
| quote := s.Engine.Quote | |||||
| for idxName, index := range s.RefTable.Indexes { | |||||
| tbName := statement.TableName() | |||||
| quote := statement.Engine.Quote | |||||
| for idxName, index := range statement.RefTable.Indexes { | |||||
| if index.Type == core.IndexType { | if index.Type == core.IndexType { | ||||
| sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)), | sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)), | ||||
| quote(tbName), quote(strings.Join(index.Cols, quote(",")))) | quote(tbName), quote(strings.Join(index.Cols, quote(",")))) | ||||
| @@ -1045,41 +1049,41 @@ func uniqueName(tableName, uqeName string) string { | |||||
| return fmt.Sprintf("UQE_%v_%v", tableName, uqeName) | return fmt.Sprintf("UQE_%v_%v", tableName, uqeName) | ||||
| } | } | ||||
| func (s *Statement) genUniqueSQL() []string { | |||||
| func (statement *Statement) genUniqueSQL() []string { | |||||
| var sqls []string | var sqls []string | ||||
| tbName := s.TableName() | |||||
| for _, index := range s.RefTable.Indexes { | |||||
| tbName := statement.TableName() | |||||
| for _, index := range statement.RefTable.Indexes { | |||||
| if index.Type == core.UniqueType { | if index.Type == core.UniqueType { | ||||
| sql := s.Engine.dialect.CreateIndexSql(tbName, index) | |||||
| sql := statement.Engine.dialect.CreateIndexSql(tbName, index) | |||||
| sqls = append(sqls, sql) | sqls = append(sqls, sql) | ||||
| } | } | ||||
| } | } | ||||
| return sqls | return sqls | ||||
| } | } | ||||
| func (s *Statement) genDelIndexSQL() []string { | |||||
| func (statement *Statement) genDelIndexSQL() []string { | |||||
| var sqls []string | var sqls []string | ||||
| tbName := s.TableName() | |||||
| for idxName, index := range s.RefTable.Indexes { | |||||
| tbName := statement.TableName() | |||||
| for idxName, index := range statement.RefTable.Indexes { | |||||
| var rIdxName string | var rIdxName string | ||||
| if index.Type == core.UniqueType { | if index.Type == core.UniqueType { | ||||
| rIdxName = uniqueName(tbName, idxName) | rIdxName = uniqueName(tbName, idxName) | ||||
| } else if index.Type == core.IndexType { | } else if index.Type == core.IndexType { | ||||
| rIdxName = indexName(tbName, idxName) | rIdxName = indexName(tbName, idxName) | ||||
| } | } | ||||
| sql := fmt.Sprintf("DROP INDEX %v", s.Engine.Quote(rIdxName)) | |||||
| if s.Engine.dialect.IndexOnTable() { | |||||
| sql += fmt.Sprintf(" ON %v", s.Engine.Quote(s.TableName())) | |||||
| sql := fmt.Sprintf("DROP INDEX %v", statement.Engine.Quote(rIdxName)) | |||||
| if statement.Engine.dialect.IndexOnTable() { | |||||
| sql += fmt.Sprintf(" ON %v", statement.Engine.Quote(statement.TableName())) | |||||
| } | } | ||||
| sqls = append(sqls, sql) | sqls = append(sqls, sql) | ||||
| } | } | ||||
| return sqls | return sqls | ||||
| } | } | ||||
| func (s *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) { | |||||
| quote := s.Engine.Quote | |||||
| sql := fmt.Sprintf("ALTER TABLE %v ADD %v;", quote(s.TableName()), | |||||
| col.String(s.Engine.dialect)) | |||||
| func (statement *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) { | |||||
| quote := statement.Engine.Quote | |||||
| sql := fmt.Sprintf("ALTER TABLE %v ADD %v;", quote(statement.TableName()), | |||||
| col.String(statement.Engine.dialect)) | |||||
| return sql, []interface{}{} | return sql, []interface{}{} | ||||
| } | } | ||||
| @@ -2,6 +2,18 @@ | |||||
| "comment": "", | "comment": "", | ||||
| "ignore": "test", | "ignore": "test", | ||||
| "package": [ | "package": [ | ||||
| { | |||||
| "checksumSHA1": "K3Gp8Tv/B8otlbsOfQp3UpJGaaE=", | |||||
| "path": "code.gitea.io/git", | |||||
| "revision": "766747ef8b271a2b1142edd0a40735f556ec2c1d", | |||||
| "revisionTime": "2016-11-06T09:52:37Z" | |||||
| }, | |||||
| { | |||||
| "checksumSHA1": "/uhZZppDeb3Rbp3h8C0ALR3hdrA=", | |||||
| "path": "code.gitea.io/sdk/gitea", | |||||
| "revision": "0a0a04ccf7a5e6b93d9a5507701635330cf4579c", | |||||
| "revisionTime": "2016-11-07T15:06:50Z" | |||||
| }, | |||||
| { | { | ||||
| "checksumSHA1": "IyfS7Rbl6OgR83QR7TOfKdDCq+M=", | "checksumSHA1": "IyfS7Rbl6OgR83QR7TOfKdDCq+M=", | ||||
| "path": "github.com/Unknwon/cae", | "path": "github.com/Unknwon/cae", | ||||
| @@ -38,18 +50,6 @@ | |||||
| "revision": "fb1f79c6b65acda83063cbc69f6bba1522558bfc", | "revision": "fb1f79c6b65acda83063cbc69f6bba1522558bfc", | ||||
| "revisionTime": "2016-01-17T19:21:50Z" | "revisionTime": "2016-01-17T19:21:50Z" | ||||
| }, | }, | ||||
| { | |||||
| "checksumSHA1": "K3Gp8Tv/B8otlbsOfQp3UpJGaaE=", | |||||
| "path": "code.gitea.io/git", | |||||
| "revision": "766747ef8b271a2b1142edd0a40735f556ec2c1d", | |||||
| "revisionTime": "2016-11-06T09:52:37Z" | |||||
| }, | |||||
| { | |||||
| "checksumSHA1": "/uhZZppDeb3Rbp3h8C0ALR3hdrA=", | |||||
| "path": "code.gitea.io/sdk/gitea", | |||||
| "revision": "0a0a04ccf7a5e6b93d9a5507701635330cf4579c", | |||||
| "revisionTime": "2016-11-07T15:06:50Z" | |||||
| }, | |||||
| { | { | ||||
| "checksumSHA1": "Lf3uUXTkKK5DJ37BxQvxO1Fq+K8=", | "checksumSHA1": "Lf3uUXTkKK5DJ37BxQvxO1Fq+K8=", | ||||
| "path": "github.com/davecgh/go-spew/spew", | "path": "github.com/davecgh/go-spew/spew", | ||||
| @@ -147,10 +147,10 @@ | |||||
| "revisionTime": "2016-11-01T01:36:51Z" | "revisionTime": "2016-11-01T01:36:51Z" | ||||
| }, | }, | ||||
| { | { | ||||
| "checksumSHA1": "+h0UQPxV/rdGOIL3vqwNU6++TyM=", | |||||
| "checksumSHA1": "eOdqyQnmqdF0VZpBgYoOaYQDaiw=", | |||||
| "path": "github.com/go-xorm/xorm", | "path": "github.com/go-xorm/xorm", | ||||
| "revision": "2b4faaaeabd118dfa541e751564004db98a6e995", | |||||
| "revisionTime": "2016-11-01T14:44:45Z" | |||||
| "revision": "f2610e02a1a1240d663ebe5bcde6c9ad4f1372cb", | |||||
| "revisionTime": "2016-11-11T16:13:12Z" | |||||
| }, | }, | ||||
| { | { | ||||
| "checksumSHA1": "1ft/4j5MFa7C9dPI9whL03HSUzk=", | "checksumSHA1": "1ft/4j5MFa7C9dPI9whL03HSUzk=", | ||||