statement.go 36 KB

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