statement.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "bytes"
  7. "database/sql/driver"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "reflect"
  12. "strings"
  13. "time"
  14. "github.com/go-xorm/builder"
  15. "github.com/go-xorm/core"
  16. )
  17. type incrParam struct {
  18. colName string
  19. arg interface{}
  20. }
  21. type decrParam struct {
  22. colName string
  23. arg interface{}
  24. }
  25. type exprParam struct {
  26. colName string
  27. expr string
  28. }
  29. // Statement save all the sql info for executing SQL
  30. type Statement struct {
  31. RefTable *core.Table
  32. Engine *Engine
  33. Start int
  34. LimitN int
  35. idParam *core.PK
  36. OrderStr string
  37. JoinStr string
  38. joinArgs []interface{}
  39. GroupByStr string
  40. HavingStr string
  41. ColumnStr string
  42. selectStr string
  43. columnMap map[string]bool
  44. useAllCols bool
  45. OmitStr string
  46. AltTableName string
  47. tableName string
  48. RawSQL string
  49. RawParams []interface{}
  50. UseCascade bool
  51. UseAutoJoin bool
  52. StoreEngine string
  53. Charset string
  54. UseCache bool
  55. UseAutoTime bool
  56. noAutoCondition bool
  57. IsDistinct bool
  58. IsForUpdate bool
  59. TableAlias string
  60. allUseBool bool
  61. checkVersion bool
  62. unscoped bool
  63. mustColumnMap map[string]bool
  64. nullableMap map[string]bool
  65. incrColumns map[string]incrParam
  66. decrColumns map[string]decrParam
  67. exprColumns map[string]exprParam
  68. cond builder.Cond
  69. bufferSize int
  70. }
  71. // Init reset all the statement's fields
  72. func (statement *Statement) Init() {
  73. statement.RefTable = nil
  74. statement.Start = 0
  75. statement.LimitN = 0
  76. statement.OrderStr = ""
  77. statement.UseCascade = true
  78. statement.JoinStr = ""
  79. statement.joinArgs = make([]interface{}, 0)
  80. statement.GroupByStr = ""
  81. statement.HavingStr = ""
  82. statement.ColumnStr = ""
  83. statement.OmitStr = ""
  84. statement.columnMap = make(map[string]bool)
  85. statement.AltTableName = ""
  86. statement.tableName = ""
  87. statement.idParam = nil
  88. statement.RawSQL = ""
  89. statement.RawParams = make([]interface{}, 0)
  90. statement.UseCache = true
  91. statement.UseAutoTime = true
  92. statement.noAutoCondition = false
  93. statement.IsDistinct = false
  94. statement.IsForUpdate = false
  95. statement.TableAlias = ""
  96. statement.selectStr = ""
  97. statement.allUseBool = false
  98. statement.useAllCols = false
  99. statement.mustColumnMap = make(map[string]bool)
  100. statement.nullableMap = make(map[string]bool)
  101. statement.checkVersion = true
  102. statement.unscoped = false
  103. statement.incrColumns = make(map[string]incrParam)
  104. statement.decrColumns = make(map[string]decrParam)
  105. statement.exprColumns = make(map[string]exprParam)
  106. statement.cond = builder.NewCond()
  107. statement.bufferSize = 0
  108. }
  109. // NoAutoCondition if you do not want convert bean's field as query condition, then use this function
  110. func (statement *Statement) NoAutoCondition(no ...bool) *Statement {
  111. statement.noAutoCondition = true
  112. if len(no) > 0 {
  113. statement.noAutoCondition = no[0]
  114. }
  115. return statement
  116. }
  117. // Alias set the table alias
  118. func (statement *Statement) Alias(alias string) *Statement {
  119. statement.TableAlias = alias
  120. return statement
  121. }
  122. // SQL adds raw sql statement
  123. func (statement *Statement) SQL(query interface{}, args ...interface{}) *Statement {
  124. switch query.(type) {
  125. case (*builder.Builder):
  126. var err error
  127. statement.RawSQL, statement.RawParams, err = query.(*builder.Builder).ToSQL()
  128. if err != nil {
  129. statement.Engine.logger.Error(err)
  130. }
  131. case string:
  132. statement.RawSQL = query.(string)
  133. statement.RawParams = args
  134. default:
  135. statement.Engine.logger.Error("unsupported sql type")
  136. }
  137. return statement
  138. }
  139. // Where add Where statement
  140. func (statement *Statement) Where(query interface{}, args ...interface{}) *Statement {
  141. return statement.And(query, args...)
  142. }
  143. // And add Where & and statement
  144. func (statement *Statement) And(query interface{}, args ...interface{}) *Statement {
  145. switch query.(type) {
  146. case string:
  147. cond := builder.Expr(query.(string), args...)
  148. statement.cond = statement.cond.And(cond)
  149. case map[string]interface{}:
  150. cond := builder.Eq(query.(map[string]interface{}))
  151. statement.cond = statement.cond.And(cond)
  152. case builder.Cond:
  153. cond := query.(builder.Cond)
  154. statement.cond = statement.cond.And(cond)
  155. for _, v := range args {
  156. if vv, ok := v.(builder.Cond); ok {
  157. statement.cond = statement.cond.And(vv)
  158. }
  159. }
  160. default:
  161. // TODO: not support condition type
  162. }
  163. return statement
  164. }
  165. // Or add Where & Or statement
  166. func (statement *Statement) Or(query interface{}, args ...interface{}) *Statement {
  167. switch query.(type) {
  168. case string:
  169. cond := builder.Expr(query.(string), args...)
  170. statement.cond = statement.cond.Or(cond)
  171. case map[string]interface{}:
  172. cond := builder.Eq(query.(map[string]interface{}))
  173. statement.cond = statement.cond.Or(cond)
  174. case builder.Cond:
  175. cond := query.(builder.Cond)
  176. statement.cond = statement.cond.Or(cond)
  177. for _, v := range args {
  178. if vv, ok := v.(builder.Cond); ok {
  179. statement.cond = statement.cond.Or(vv)
  180. }
  181. }
  182. default:
  183. // TODO: not support condition type
  184. }
  185. return statement
  186. }
  187. // In generate "Where column IN (?) " statement
  188. func (statement *Statement) In(column string, args ...interface{}) *Statement {
  189. in := builder.In(statement.Engine.Quote(column), args...)
  190. statement.cond = statement.cond.And(in)
  191. return statement
  192. }
  193. // NotIn generate "Where column NOT IN (?) " statement
  194. func (statement *Statement) NotIn(column string, args ...interface{}) *Statement {
  195. notIn := builder.NotIn(statement.Engine.Quote(column), args...)
  196. statement.cond = statement.cond.And(notIn)
  197. return statement
  198. }
  199. func (statement *Statement) setRefValue(v reflect.Value) error {
  200. var err error
  201. statement.RefTable, err = statement.Engine.autoMapType(reflect.Indirect(v))
  202. if err != nil {
  203. return err
  204. }
  205. statement.tableName = statement.Engine.tbName(v)
  206. return nil
  207. }
  208. // Table tempororily set table name, the parameter could be a string or a pointer of struct
  209. func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
  210. v := rValue(tableNameOrBean)
  211. t := v.Type()
  212. if t.Kind() == reflect.String {
  213. statement.AltTableName = tableNameOrBean.(string)
  214. } else if t.Kind() == reflect.Struct {
  215. var err error
  216. statement.RefTable, err = statement.Engine.autoMapType(v)
  217. if err != nil {
  218. statement.Engine.logger.Error(err)
  219. return statement
  220. }
  221. statement.AltTableName = statement.Engine.tbName(v)
  222. }
  223. return statement
  224. }
  225. // Auto generating update columnes and values according a struct
  226. func buildUpdates(engine *Engine, table *core.Table, bean interface{},
  227. includeVersion bool, includeUpdated bool, includeNil bool,
  228. includeAutoIncr bool, allUseBool bool, useAllCols bool,
  229. mustColumnMap map[string]bool, nullableMap map[string]bool,
  230. columnMap map[string]bool, update, unscoped bool) ([]string, []interface{}) {
  231. var colNames = make([]string, 0)
  232. var args = make([]interface{}, 0)
  233. for _, col := range table.Columns() {
  234. if !includeVersion && col.IsVersion {
  235. continue
  236. }
  237. if col.IsCreated {
  238. continue
  239. }
  240. if !includeUpdated && col.IsUpdated {
  241. continue
  242. }
  243. if !includeAutoIncr && col.IsAutoIncrement {
  244. continue
  245. }
  246. if col.IsDeleted && !unscoped {
  247. continue
  248. }
  249. if use, ok := columnMap[strings.ToLower(col.Name)]; ok && !use {
  250. continue
  251. }
  252. fieldValuePtr, err := col.ValueOf(bean)
  253. if err != nil {
  254. engine.logger.Error(err)
  255. continue
  256. }
  257. fieldValue := *fieldValuePtr
  258. fieldType := reflect.TypeOf(fieldValue.Interface())
  259. if fieldType == nil {
  260. continue
  261. }
  262. requiredField := useAllCols
  263. includeNil := useAllCols
  264. if b, ok := getFlagForColumn(mustColumnMap, col); ok {
  265. if b {
  266. requiredField = true
  267. } else {
  268. continue
  269. }
  270. }
  271. // !evalphobia! set fieldValue as nil when column is nullable and zero-value
  272. if b, ok := getFlagForColumn(nullableMap, col); ok {
  273. if b && col.Nullable && isZero(fieldValue.Interface()) {
  274. var nilValue *int
  275. fieldValue = reflect.ValueOf(nilValue)
  276. fieldType = reflect.TypeOf(fieldValue.Interface())
  277. includeNil = true
  278. }
  279. }
  280. var val interface{}
  281. if fieldValue.CanAddr() {
  282. if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
  283. data, err := structConvert.ToDB()
  284. if err != nil {
  285. engine.logger.Error(err)
  286. } else {
  287. val = data
  288. }
  289. goto APPEND
  290. }
  291. }
  292. if structConvert, ok := fieldValue.Interface().(core.Conversion); ok {
  293. data, err := structConvert.ToDB()
  294. if err != nil {
  295. engine.logger.Error(err)
  296. } else {
  297. val = data
  298. }
  299. goto APPEND
  300. }
  301. if fieldType.Kind() == reflect.Ptr {
  302. if fieldValue.IsNil() {
  303. if includeNil {
  304. args = append(args, nil)
  305. colNames = append(colNames, fmt.Sprintf("%v=?", engine.Quote(col.Name)))
  306. }
  307. continue
  308. } else if !fieldValue.IsValid() {
  309. continue
  310. } else {
  311. // dereference ptr type to instance type
  312. fieldValue = fieldValue.Elem()
  313. fieldType = reflect.TypeOf(fieldValue.Interface())
  314. requiredField = true
  315. }
  316. }
  317. switch fieldType.Kind() {
  318. case reflect.Bool:
  319. if allUseBool || requiredField {
  320. val = fieldValue.Interface()
  321. } else {
  322. // if a bool in a struct, it will not be as a condition because it default is false,
  323. // please use Where() instead
  324. continue
  325. }
  326. case reflect.String:
  327. if !requiredField && fieldValue.String() == "" {
  328. continue
  329. }
  330. // for MyString, should convert to string or panic
  331. if fieldType.String() != reflect.String.String() {
  332. val = fieldValue.String()
  333. } else {
  334. val = fieldValue.Interface()
  335. }
  336. case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64:
  337. if !requiredField && fieldValue.Int() == 0 {
  338. continue
  339. }
  340. val = fieldValue.Interface()
  341. case reflect.Float32, reflect.Float64:
  342. if !requiredField && fieldValue.Float() == 0.0 {
  343. continue
  344. }
  345. val = fieldValue.Interface()
  346. case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
  347. if !requiredField && fieldValue.Uint() == 0 {
  348. continue
  349. }
  350. t := int64(fieldValue.Uint())
  351. val = reflect.ValueOf(&t).Interface()
  352. case reflect.Struct:
  353. if fieldType.ConvertibleTo(core.TimeType) {
  354. t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
  355. if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
  356. continue
  357. }
  358. val = engine.formatColTime(col, t)
  359. } else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
  360. val, _ = nulType.Value()
  361. } else {
  362. if !col.SQLType.IsJson() {
  363. engine.autoMapType(fieldValue)
  364. if table, ok := engine.Tables[fieldValue.Type()]; ok {
  365. if len(table.PrimaryKeys) == 1 {
  366. pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
  367. // fix non-int pk issues
  368. if pkField.IsValid() && (!requiredField && !isZero(pkField.Interface())) {
  369. val = pkField.Interface()
  370. } else {
  371. continue
  372. }
  373. } else {
  374. //TODO: how to handler?
  375. panic("not supported")
  376. }
  377. } else {
  378. val = fieldValue.Interface()
  379. }
  380. } else {
  381. // Blank struct could not be as update data
  382. if requiredField || !isStructZero(fieldValue) {
  383. bytes, err := json.Marshal(fieldValue.Interface())
  384. if err != nil {
  385. panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface()))
  386. }
  387. if col.SQLType.IsText() {
  388. val = string(bytes)
  389. } else if col.SQLType.IsBlob() {
  390. val = bytes
  391. }
  392. } else {
  393. continue
  394. }
  395. }
  396. }
  397. case reflect.Array, reflect.Slice, reflect.Map:
  398. if !requiredField {
  399. if fieldValue == reflect.Zero(fieldType) {
  400. continue
  401. }
  402. if fieldType.Kind() == reflect.Array {
  403. if isArrayValueZero(fieldValue) {
  404. continue
  405. }
  406. } else if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 {
  407. continue
  408. }
  409. }
  410. if col.SQLType.IsText() {
  411. bytes, err := json.Marshal(fieldValue.Interface())
  412. if err != nil {
  413. engine.logger.Error(err)
  414. continue
  415. }
  416. val = string(bytes)
  417. } else if col.SQLType.IsBlob() {
  418. var bytes []byte
  419. var err error
  420. if fieldType.Kind() == reflect.Slice &&
  421. fieldType.Elem().Kind() == reflect.Uint8 {
  422. if fieldValue.Len() > 0 {
  423. val = fieldValue.Bytes()
  424. } else {
  425. continue
  426. }
  427. } else if fieldType.Kind() == reflect.Array &&
  428. fieldType.Elem().Kind() == reflect.Uint8 {
  429. val = fieldValue.Slice(0, 0).Interface()
  430. } else {
  431. bytes, err = json.Marshal(fieldValue.Interface())
  432. if err != nil {
  433. engine.logger.Error(err)
  434. continue
  435. }
  436. val = bytes
  437. }
  438. } else {
  439. continue
  440. }
  441. default:
  442. val = fieldValue.Interface()
  443. }
  444. APPEND:
  445. args = append(args, val)
  446. if col.IsPrimaryKey && engine.dialect.DBType() == "ql" {
  447. continue
  448. }
  449. colNames = append(colNames, fmt.Sprintf("%v = ?", engine.Quote(col.Name)))
  450. }
  451. return colNames, args
  452. }
  453. func (statement *Statement) needTableName() bool {
  454. return len(statement.JoinStr) > 0
  455. }
  456. func (statement *Statement) colName(col *core.Column, tableName string) string {
  457. if statement.needTableName() {
  458. var nm = tableName
  459. if len(statement.TableAlias) > 0 {
  460. nm = statement.TableAlias
  461. }
  462. return statement.Engine.Quote(nm) + "." + statement.Engine.Quote(col.Name)
  463. }
  464. return statement.Engine.Quote(col.Name)
  465. }
  466. // TableName return current tableName
  467. func (statement *Statement) TableName() string {
  468. if statement.AltTableName != "" {
  469. return statement.AltTableName
  470. }
  471. return statement.tableName
  472. }
  473. // ID generate "where id = ? " statement or for composite key "where key1 = ? and key2 = ?"
  474. func (statement *Statement) ID(id interface{}) *Statement {
  475. idValue := reflect.ValueOf(id)
  476. idType := reflect.TypeOf(idValue.Interface())
  477. switch idType {
  478. case ptrPkType:
  479. if pkPtr, ok := (id).(*core.PK); ok {
  480. statement.idParam = pkPtr
  481. return statement
  482. }
  483. case pkType:
  484. if pk, ok := (id).(core.PK); ok {
  485. statement.idParam = &pk
  486. return statement
  487. }
  488. }
  489. switch idType.Kind() {
  490. case reflect.String:
  491. statement.idParam = &core.PK{idValue.Convert(reflect.TypeOf("")).Interface()}
  492. return statement
  493. }
  494. statement.idParam = &core.PK{id}
  495. return statement
  496. }
  497. // Incr Generate "Update ... Set column = column + arg" statement
  498. func (statement *Statement) Incr(column string, arg ...interface{}) *Statement {
  499. k := strings.ToLower(column)
  500. if len(arg) > 0 {
  501. statement.incrColumns[k] = incrParam{column, arg[0]}
  502. } else {
  503. statement.incrColumns[k] = incrParam{column, 1}
  504. }
  505. return statement
  506. }
  507. // Decr Generate "Update ... Set column = column - arg" statement
  508. func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
  509. k := strings.ToLower(column)
  510. if len(arg) > 0 {
  511. statement.decrColumns[k] = decrParam{column, arg[0]}
  512. } else {
  513. statement.decrColumns[k] = decrParam{column, 1}
  514. }
  515. return statement
  516. }
  517. // SetExpr Generate "Update ... Set column = {expression}" statement
  518. func (statement *Statement) SetExpr(column string, expression string) *Statement {
  519. k := strings.ToLower(column)
  520. statement.exprColumns[k] = exprParam{column, expression}
  521. return statement
  522. }
  523. // Generate "Update ... Set column = column + arg" statement
  524. func (statement *Statement) getInc() map[string]incrParam {
  525. return statement.incrColumns
  526. }
  527. // Generate "Update ... Set column = column - arg" statement
  528. func (statement *Statement) getDec() map[string]decrParam {
  529. return statement.decrColumns
  530. }
  531. // Generate "Update ... Set column = {expression}" statement
  532. func (statement *Statement) getExpr() map[string]exprParam {
  533. return statement.exprColumns
  534. }
  535. func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
  536. newColumns := make([]string, 0)
  537. for _, col := range columns {
  538. col = strings.Replace(col, "`", "", -1)
  539. col = strings.Replace(col, statement.Engine.QuoteStr(), "", -1)
  540. ccols := strings.Split(col, ",")
  541. for _, c := range ccols {
  542. fields := strings.Split(strings.TrimSpace(c), ".")
  543. if len(fields) == 1 {
  544. newColumns = append(newColumns, statement.Engine.quote(fields[0]))
  545. } else if len(fields) == 2 {
  546. newColumns = append(newColumns, statement.Engine.quote(fields[0])+"."+
  547. statement.Engine.quote(fields[1]))
  548. } else {
  549. panic(errors.New("unwanted colnames"))
  550. }
  551. }
  552. }
  553. return newColumns
  554. }
  555. func (statement *Statement) colmap2NewColsWithQuote() []string {
  556. newColumns := make([]string, 0, len(statement.columnMap))
  557. for col := range statement.columnMap {
  558. fields := strings.Split(strings.TrimSpace(col), ".")
  559. if len(fields) == 1 {
  560. newColumns = append(newColumns, statement.Engine.quote(fields[0]))
  561. } else if len(fields) == 2 {
  562. newColumns = append(newColumns, statement.Engine.quote(fields[0])+"."+
  563. statement.Engine.quote(fields[1]))
  564. } else {
  565. panic(errors.New("unwanted colnames"))
  566. }
  567. }
  568. return newColumns
  569. }
  570. // Distinct generates "DISTINCT col1, col2 " statement
  571. func (statement *Statement) Distinct(columns ...string) *Statement {
  572. statement.IsDistinct = true
  573. statement.Cols(columns...)
  574. return statement
  575. }
  576. // ForUpdate generates "SELECT ... FOR UPDATE" statement
  577. func (statement *Statement) ForUpdate() *Statement {
  578. statement.IsForUpdate = true
  579. return statement
  580. }
  581. // Select replace select
  582. func (statement *Statement) Select(str string) *Statement {
  583. statement.selectStr = str
  584. return statement
  585. }
  586. // Cols generate "col1, col2" statement
  587. func (statement *Statement) Cols(columns ...string) *Statement {
  588. cols := col2NewCols(columns...)
  589. for _, nc := range cols {
  590. statement.columnMap[strings.ToLower(nc)] = true
  591. }
  592. newColumns := statement.colmap2NewColsWithQuote()
  593. statement.ColumnStr = strings.Join(newColumns, ", ")
  594. statement.ColumnStr = strings.Replace(statement.ColumnStr, statement.Engine.quote("*"), "*", -1)
  595. return statement
  596. }
  597. // AllCols update use only: update all columns
  598. func (statement *Statement) AllCols() *Statement {
  599. statement.useAllCols = true
  600. return statement
  601. }
  602. // MustCols update use only: must update columns
  603. func (statement *Statement) MustCols(columns ...string) *Statement {
  604. newColumns := col2NewCols(columns...)
  605. for _, nc := range newColumns {
  606. statement.mustColumnMap[strings.ToLower(nc)] = true
  607. }
  608. return statement
  609. }
  610. // UseBool indicates that use bool fields as update contents and query contiditions
  611. func (statement *Statement) UseBool(columns ...string) *Statement {
  612. if len(columns) > 0 {
  613. statement.MustCols(columns...)
  614. } else {
  615. statement.allUseBool = true
  616. }
  617. return statement
  618. }
  619. // Omit do not use the columns
  620. func (statement *Statement) Omit(columns ...string) {
  621. newColumns := col2NewCols(columns...)
  622. for _, nc := range newColumns {
  623. statement.columnMap[strings.ToLower(nc)] = false
  624. }
  625. statement.OmitStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
  626. }
  627. // Nullable Update use only: update columns to null when value is nullable and zero-value
  628. func (statement *Statement) Nullable(columns ...string) {
  629. newColumns := col2NewCols(columns...)
  630. for _, nc := range newColumns {
  631. statement.nullableMap[strings.ToLower(nc)] = true
  632. }
  633. }
  634. // Top generate LIMIT limit statement
  635. func (statement *Statement) Top(limit int) *Statement {
  636. statement.Limit(limit)
  637. return statement
  638. }
  639. // Limit generate LIMIT start, limit statement
  640. func (statement *Statement) Limit(limit int, start ...int) *Statement {
  641. statement.LimitN = limit
  642. if len(start) > 0 {
  643. statement.Start = start[0]
  644. }
  645. return statement
  646. }
  647. // OrderBy generate "Order By order" statement
  648. func (statement *Statement) OrderBy(order string) *Statement {
  649. if len(statement.OrderStr) > 0 {
  650. statement.OrderStr += ", "
  651. }
  652. statement.OrderStr += order
  653. return statement
  654. }
  655. // Desc generate `ORDER BY xx DESC`
  656. func (statement *Statement) Desc(colNames ...string) *Statement {
  657. var buf bytes.Buffer
  658. fmt.Fprintf(&buf, statement.OrderStr)
  659. if len(statement.OrderStr) > 0 {
  660. fmt.Fprint(&buf, ", ")
  661. }
  662. newColNames := statement.col2NewColsWithQuote(colNames...)
  663. fmt.Fprintf(&buf, "%v DESC", strings.Join(newColNames, " DESC, "))
  664. statement.OrderStr = buf.String()
  665. return statement
  666. }
  667. // Asc provide asc order by query condition, the input parameters are columns.
  668. func (statement *Statement) Asc(colNames ...string) *Statement {
  669. var buf bytes.Buffer
  670. fmt.Fprintf(&buf, statement.OrderStr)
  671. if len(statement.OrderStr) > 0 {
  672. fmt.Fprint(&buf, ", ")
  673. }
  674. newColNames := statement.col2NewColsWithQuote(colNames...)
  675. fmt.Fprintf(&buf, "%v ASC", strings.Join(newColNames, " ASC, "))
  676. statement.OrderStr = buf.String()
  677. return statement
  678. }
  679. // Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
  680. func (statement *Statement) Join(joinOP string, tablename interface{}, condition string, args ...interface{}) *Statement {
  681. var buf bytes.Buffer
  682. if len(statement.JoinStr) > 0 {
  683. fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, joinOP)
  684. } else {
  685. fmt.Fprintf(&buf, "%v JOIN ", joinOP)
  686. }
  687. switch tablename.(type) {
  688. case []string:
  689. t := tablename.([]string)
  690. if len(t) > 1 {
  691. fmt.Fprintf(&buf, "%v AS %v", statement.Engine.Quote(t[0]), statement.Engine.Quote(t[1]))
  692. } else if len(t) == 1 {
  693. fmt.Fprintf(&buf, statement.Engine.Quote(t[0]))
  694. }
  695. case []interface{}:
  696. t := tablename.([]interface{})
  697. l := len(t)
  698. var table string
  699. if l > 0 {
  700. f := t[0]
  701. v := rValue(f)
  702. t := v.Type()
  703. if t.Kind() == reflect.String {
  704. table = f.(string)
  705. } else if t.Kind() == reflect.Struct {
  706. table = statement.Engine.tbName(v)
  707. }
  708. }
  709. if l > 1 {
  710. fmt.Fprintf(&buf, "%v AS %v", statement.Engine.Quote(table),
  711. statement.Engine.Quote(fmt.Sprintf("%v", t[1])))
  712. } else if l == 1 {
  713. fmt.Fprintf(&buf, statement.Engine.Quote(table))
  714. }
  715. default:
  716. fmt.Fprintf(&buf, statement.Engine.Quote(fmt.Sprintf("%v", tablename)))
  717. }
  718. fmt.Fprintf(&buf, " ON %v", condition)
  719. statement.JoinStr = buf.String()
  720. statement.joinArgs = append(statement.joinArgs, args...)
  721. return statement
  722. }
  723. // GroupBy generate "Group By keys" statement
  724. func (statement *Statement) GroupBy(keys string) *Statement {
  725. statement.GroupByStr = keys
  726. return statement
  727. }
  728. // Having generate "Having conditions" statement
  729. func (statement *Statement) Having(conditions string) *Statement {
  730. statement.HavingStr = fmt.Sprintf("HAVING %v", conditions)
  731. return statement
  732. }
  733. // Unscoped always disable struct tag "deleted"
  734. func (statement *Statement) Unscoped() *Statement {
  735. statement.unscoped = true
  736. return statement
  737. }
  738. func (statement *Statement) genColumnStr() string {
  739. var buf bytes.Buffer
  740. if statement.RefTable == nil {
  741. return ""
  742. }
  743. columns := statement.RefTable.Columns()
  744. for _, col := range columns {
  745. if statement.OmitStr != "" {
  746. if _, ok := getFlagForColumn(statement.columnMap, col); ok {
  747. continue
  748. }
  749. }
  750. if col.MapType == core.ONLYTODB {
  751. continue
  752. }
  753. if buf.Len() != 0 {
  754. buf.WriteString(", ")
  755. }
  756. if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
  757. buf.WriteString("id() AS ")
  758. }
  759. if statement.JoinStr != "" {
  760. if statement.TableAlias != "" {
  761. buf.WriteString(statement.TableAlias)
  762. } else {
  763. buf.WriteString(statement.TableName())
  764. }
  765. buf.WriteString(".")
  766. }
  767. statement.Engine.QuoteTo(&buf, col.Name)
  768. }
  769. return buf.String()
  770. }
  771. func (statement *Statement) genCreateTableSQL() string {
  772. return statement.Engine.dialect.CreateTableSql(statement.RefTable, statement.TableName(),
  773. statement.StoreEngine, statement.Charset)
  774. }
  775. func (statement *Statement) genIndexSQL() []string {
  776. var sqls []string
  777. tbName := statement.TableName()
  778. quote := statement.Engine.Quote
  779. for idxName, index := range statement.RefTable.Indexes {
  780. if index.Type == core.IndexType {
  781. sql := fmt.Sprintf("CREATE INDEX %v ON %v (%v);", quote(indexName(tbName, idxName)),
  782. quote(tbName), quote(strings.Join(index.Cols, quote(","))))
  783. sqls = append(sqls, sql)
  784. }
  785. }
  786. return sqls
  787. }
  788. func uniqueName(tableName, uqeName string) string {
  789. return fmt.Sprintf("UQE_%v_%v", tableName, uqeName)
  790. }
  791. func (statement *Statement) genUniqueSQL() []string {
  792. var sqls []string
  793. tbName := statement.TableName()
  794. for _, index := range statement.RefTable.Indexes {
  795. if index.Type == core.UniqueType {
  796. sql := statement.Engine.dialect.CreateIndexSql(tbName, index)
  797. sqls = append(sqls, sql)
  798. }
  799. }
  800. return sqls
  801. }
  802. func (statement *Statement) genDelIndexSQL() []string {
  803. var sqls []string
  804. tbName := statement.TableName()
  805. for idxName, index := range statement.RefTable.Indexes {
  806. var rIdxName string
  807. if index.Type == core.UniqueType {
  808. rIdxName = uniqueName(tbName, idxName)
  809. } else if index.Type == core.IndexType {
  810. rIdxName = indexName(tbName, idxName)
  811. }
  812. sql := fmt.Sprintf("DROP INDEX %v", statement.Engine.Quote(rIdxName))
  813. if statement.Engine.dialect.IndexOnTable() {
  814. sql += fmt.Sprintf(" ON %v", statement.Engine.Quote(statement.TableName()))
  815. }
  816. sqls = append(sqls, sql)
  817. }
  818. return sqls
  819. }
  820. func (statement *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) {
  821. quote := statement.Engine.Quote
  822. sql := fmt.Sprintf("ALTER TABLE %v ADD %v", quote(statement.TableName()),
  823. col.String(statement.Engine.dialect))
  824. if statement.Engine.dialect.DBType() == core.MYSQL && len(col.Comment) > 0 {
  825. sql += " COMMENT '" + col.Comment + "'"
  826. }
  827. sql += ";"
  828. return sql, []interface{}{}
  829. }
  830. func (statement *Statement) buildConds(table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) (builder.Cond, error) {
  831. return statement.Engine.buildConds(table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols,
  832. statement.unscoped, statement.mustColumnMap, statement.TableName(), statement.TableAlias, addedTableName)
  833. }
  834. func (statement *Statement) mergeConds(bean interface{}) error {
  835. if !statement.noAutoCondition {
  836. var addedTableName = (len(statement.JoinStr) > 0)
  837. autoCond, err := statement.buildConds(statement.RefTable, bean, true, true, false, true, addedTableName)
  838. if err != nil {
  839. return err
  840. }
  841. statement.cond = statement.cond.And(autoCond)
  842. }
  843. if err := statement.processIDParam(); err != nil {
  844. return err
  845. }
  846. return nil
  847. }
  848. func (statement *Statement) genConds(bean interface{}) (string, []interface{}, error) {
  849. if err := statement.mergeConds(bean); err != nil {
  850. return "", nil, err
  851. }
  852. return builder.ToSQL(statement.cond)
  853. }
  854. func (statement *Statement) genGetSQL(bean interface{}) (string, []interface{}, error) {
  855. v := rValue(bean)
  856. isStruct := v.Kind() == reflect.Struct
  857. if isStruct {
  858. statement.setRefValue(v)
  859. }
  860. var columnStr = statement.ColumnStr
  861. if len(statement.selectStr) > 0 {
  862. columnStr = statement.selectStr
  863. } else {
  864. // TODO: always generate column names, not use * even if join
  865. if len(statement.JoinStr) == 0 {
  866. if len(columnStr) == 0 {
  867. if len(statement.GroupByStr) > 0 {
  868. columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1))
  869. } else {
  870. columnStr = statement.genColumnStr()
  871. }
  872. }
  873. } else {
  874. if len(columnStr) == 0 {
  875. if len(statement.GroupByStr) > 0 {
  876. columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1))
  877. }
  878. }
  879. }
  880. }
  881. if len(columnStr) == 0 {
  882. columnStr = "*"
  883. }
  884. if isStruct {
  885. if err := statement.mergeConds(bean); err != nil {
  886. return "", nil, err
  887. }
  888. }
  889. condSQL, condArgs, err := builder.ToSQL(statement.cond)
  890. if err != nil {
  891. return "", nil, err
  892. }
  893. sqlStr, err := statement.genSelectSQL(columnStr, condSQL, true)
  894. if err != nil {
  895. return "", nil, err
  896. }
  897. return sqlStr, append(statement.joinArgs, condArgs...), nil
  898. }
  899. func (statement *Statement) genCountSQL(beans ...interface{}) (string, []interface{}, error) {
  900. var condSQL string
  901. var condArgs []interface{}
  902. var err error
  903. if len(beans) > 0 {
  904. statement.setRefValue(rValue(beans[0]))
  905. condSQL, condArgs, err = statement.genConds(beans[0])
  906. } else {
  907. condSQL, condArgs, err = builder.ToSQL(statement.cond)
  908. }
  909. if err != nil {
  910. return "", nil, err
  911. }
  912. var selectSQL = statement.selectStr
  913. if len(selectSQL) <= 0 {
  914. if statement.IsDistinct {
  915. selectSQL = fmt.Sprintf("count(DISTINCT %s)", statement.ColumnStr)
  916. } else {
  917. selectSQL = "count(*)"
  918. }
  919. }
  920. sqlStr, err := statement.genSelectSQL(selectSQL, condSQL, false)
  921. if err != nil {
  922. return "", nil, err
  923. }
  924. return sqlStr, append(statement.joinArgs, condArgs...), nil
  925. }
  926. func (statement *Statement) genSumSQL(bean interface{}, columns ...string) (string, []interface{}, error) {
  927. statement.setRefValue(rValue(bean))
  928. var sumStrs = make([]string, 0, len(columns))
  929. for _, colName := range columns {
  930. if !strings.Contains(colName, " ") && !strings.Contains(colName, "(") {
  931. colName = statement.Engine.Quote(colName)
  932. }
  933. sumStrs = append(sumStrs, fmt.Sprintf("COALESCE(sum(%s),0)", colName))
  934. }
  935. sumSelect := strings.Join(sumStrs, ", ")
  936. condSQL, condArgs, err := statement.genConds(bean)
  937. if err != nil {
  938. return "", nil, err
  939. }
  940. sqlStr, err := statement.genSelectSQL(sumSelect, condSQL, true)
  941. if err != nil {
  942. return "", nil, err
  943. }
  944. return sqlStr, append(statement.joinArgs, condArgs...), nil
  945. }
  946. func (statement *Statement) genSelectSQL(columnStr, condSQL string, needLimit bool) (a string, err error) {
  947. var distinct string
  948. if statement.IsDistinct && !strings.HasPrefix(columnStr, "count") {
  949. distinct = "DISTINCT "
  950. }
  951. var dialect = statement.Engine.Dialect()
  952. var quote = statement.Engine.Quote
  953. var top string
  954. var mssqlCondi string
  955. if err := statement.processIDParam(); err != nil {
  956. return "", err
  957. }
  958. var buf bytes.Buffer
  959. if len(condSQL) > 0 {
  960. fmt.Fprintf(&buf, " WHERE %v", condSQL)
  961. }
  962. var whereStr = buf.String()
  963. var fromStr = " FROM "
  964. if dialect.DBType() == core.MSSQL && strings.Contains(statement.TableName(), "..") {
  965. fromStr += statement.TableName()
  966. } else {
  967. fromStr += quote(statement.TableName())
  968. }
  969. if statement.TableAlias != "" {
  970. if dialect.DBType() == core.ORACLE {
  971. fromStr += " " + quote(statement.TableAlias)
  972. } else {
  973. fromStr += " AS " + quote(statement.TableAlias)
  974. }
  975. }
  976. if statement.JoinStr != "" {
  977. fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
  978. }
  979. if dialect.DBType() == core.MSSQL {
  980. if statement.LimitN > 0 {
  981. top = fmt.Sprintf(" TOP %d ", statement.LimitN)
  982. }
  983. if statement.Start > 0 {
  984. var column string
  985. if len(statement.RefTable.PKColumns()) == 0 {
  986. for _, index := range statement.RefTable.Indexes {
  987. if len(index.Cols) == 1 {
  988. column = index.Cols[0]
  989. break
  990. }
  991. }
  992. if len(column) == 0 {
  993. column = statement.RefTable.ColumnsSeq()[0]
  994. }
  995. } else {
  996. column = statement.RefTable.PKColumns()[0].Name
  997. }
  998. if statement.needTableName() {
  999. if len(statement.TableAlias) > 0 {
  1000. column = statement.TableAlias + "." + column
  1001. } else {
  1002. column = statement.TableName() + "." + column
  1003. }
  1004. }
  1005. var orderStr string
  1006. if len(statement.OrderStr) > 0 {
  1007. orderStr = " ORDER BY " + statement.OrderStr
  1008. }
  1009. var groupStr string
  1010. if len(statement.GroupByStr) > 0 {
  1011. groupStr = " GROUP BY " + statement.GroupByStr
  1012. }
  1013. mssqlCondi = fmt.Sprintf("(%s NOT IN (SELECT TOP %d %s%s%s%s%s))",
  1014. column, statement.Start, column, fromStr, whereStr, orderStr, groupStr)
  1015. }
  1016. }
  1017. // !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
  1018. a = fmt.Sprintf("SELECT %v%v%v%v%v", distinct, top, columnStr, fromStr, whereStr)
  1019. if len(mssqlCondi) > 0 {
  1020. if len(whereStr) > 0 {
  1021. a += " AND " + mssqlCondi
  1022. } else {
  1023. a += " WHERE " + mssqlCondi
  1024. }
  1025. }
  1026. if statement.GroupByStr != "" {
  1027. a = fmt.Sprintf("%v GROUP BY %v", a, statement.GroupByStr)
  1028. }
  1029. if statement.HavingStr != "" {
  1030. a = fmt.Sprintf("%v %v", a, statement.HavingStr)
  1031. }
  1032. if statement.OrderStr != "" {
  1033. a = fmt.Sprintf("%v ORDER BY %v", a, statement.OrderStr)
  1034. }
  1035. if needLimit {
  1036. if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE {
  1037. if statement.Start > 0 {
  1038. a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start)
  1039. } else if statement.LimitN > 0 {
  1040. a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
  1041. }
  1042. } else if dialect.DBType() == core.ORACLE {
  1043. if statement.Start != 0 || statement.LimitN != 0 {
  1044. a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start)
  1045. }
  1046. }
  1047. }
  1048. if statement.IsForUpdate {
  1049. a = dialect.ForUpdateSql(a)
  1050. }
  1051. return
  1052. }
  1053. func (statement *Statement) processIDParam() error {
  1054. if statement.idParam == nil {
  1055. return nil
  1056. }
  1057. if len(statement.RefTable.PrimaryKeys) != len(*statement.idParam) {
  1058. return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d",
  1059. len(statement.RefTable.PrimaryKeys),
  1060. len(*statement.idParam),
  1061. )
  1062. }
  1063. for i, col := range statement.RefTable.PKColumns() {
  1064. var colName = statement.colName(col, statement.TableName())
  1065. statement.cond = statement.cond.And(builder.Eq{colName: (*(statement.idParam))[i]})
  1066. }
  1067. return nil
  1068. }
  1069. func (statement *Statement) joinColumns(cols []*core.Column, includeTableName bool) string {
  1070. var colnames = make([]string, len(cols))
  1071. for i, col := range cols {
  1072. if includeTableName {
  1073. colnames[i] = statement.Engine.Quote(statement.TableName()) +
  1074. "." + statement.Engine.Quote(col.Name)
  1075. } else {
  1076. colnames[i] = statement.Engine.Quote(col.Name)
  1077. }
  1078. }
  1079. return strings.Join(colnames, ", ")
  1080. }
  1081. func (statement *Statement) convertIDSQL(sqlStr string) string {
  1082. if statement.RefTable != nil {
  1083. cols := statement.RefTable.PKColumns()
  1084. if len(cols) == 0 {
  1085. return ""
  1086. }
  1087. colstrs := statement.joinColumns(cols, false)
  1088. sqls := splitNNoCase(sqlStr, " from ", 2)
  1089. if len(sqls) != 2 {
  1090. return ""
  1091. }
  1092. var top string
  1093. if statement.LimitN > 0 && statement.Engine.dialect.DBType() == core.MSSQL {
  1094. top = fmt.Sprintf("TOP %d ", statement.LimitN)
  1095. }
  1096. newsql := fmt.Sprintf("SELECT %s%s FROM %v", top, colstrs, sqls[1])
  1097. return newsql
  1098. }
  1099. return ""
  1100. }
  1101. func (statement *Statement) convertUpdateSQL(sqlStr string) (string, string) {
  1102. if statement.RefTable == nil || len(statement.RefTable.PrimaryKeys) != 1 {
  1103. return "", ""
  1104. }
  1105. colstrs := statement.joinColumns(statement.RefTable.PKColumns(), true)
  1106. sqls := splitNNoCase(sqlStr, "where", 2)
  1107. if len(sqls) != 2 {
  1108. if len(sqls) == 1 {
  1109. return sqls[0], fmt.Sprintf("SELECT %v FROM %v",
  1110. colstrs, statement.Engine.Quote(statement.TableName()))
  1111. }
  1112. return "", ""
  1113. }
  1114. var whereStr = sqls[1]
  1115. //TODO: for postgres only, if any other database?
  1116. var paraStr string
  1117. if statement.Engine.dialect.DBType() == core.POSTGRES {
  1118. paraStr = "$"
  1119. } else if statement.Engine.dialect.DBType() == core.MSSQL {
  1120. paraStr = ":"
  1121. }
  1122. if paraStr != "" {
  1123. if strings.Contains(sqls[1], paraStr) {
  1124. dollers := strings.Split(sqls[1], paraStr)
  1125. whereStr = dollers[0]
  1126. for i, c := range dollers[1:] {
  1127. ccs := strings.SplitN(c, " ", 2)
  1128. whereStr += fmt.Sprintf(paraStr+"%v %v", i+1, ccs[1])
  1129. }
  1130. }
  1131. }
  1132. return sqls[0], fmt.Sprintf("SELECT %v FROM %v WHERE %v",
  1133. colstrs, statement.Engine.Quote(statement.TableName()),
  1134. whereStr)
  1135. }