orm_raw.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "database/sql"
  17. "fmt"
  18. "reflect"
  19. "time"
  20. )
  21. // raw sql string prepared statement
  22. type rawPrepare struct {
  23. rs *rawSet
  24. stmt stmtQuerier
  25. closed bool
  26. }
  27. func (o *rawPrepare) Exec(args ...interface{}) (sql.Result, error) {
  28. if o.closed {
  29. return nil, ErrStmtClosed
  30. }
  31. return o.stmt.Exec(args...)
  32. }
  33. func (o *rawPrepare) Close() error {
  34. o.closed = true
  35. return o.stmt.Close()
  36. }
  37. func newRawPreparer(rs *rawSet) (RawPreparer, error) {
  38. o := new(rawPrepare)
  39. o.rs = rs
  40. query := rs.query
  41. rs.orm.alias.DbBaser.ReplaceMarks(&query)
  42. st, err := rs.orm.db.Prepare(query)
  43. if err != nil {
  44. return nil, err
  45. }
  46. if Debug {
  47. o.stmt = newStmtQueryLog(rs.orm.alias, st, query)
  48. } else {
  49. o.stmt = st
  50. }
  51. return o, nil
  52. }
  53. // raw query seter
  54. type rawSet struct {
  55. query string
  56. args []interface{}
  57. orm *orm
  58. }
  59. var _ RawSeter = new(rawSet)
  60. // set args for every query
  61. func (o rawSet) SetArgs(args ...interface{}) RawSeter {
  62. o.args = args
  63. return &o
  64. }
  65. // execute raw sql and return sql.Result
  66. func (o *rawSet) Exec() (sql.Result, error) {
  67. query := o.query
  68. o.orm.alias.DbBaser.ReplaceMarks(&query)
  69. args := getFlatParams(nil, o.args, o.orm.alias.TZ)
  70. return o.orm.db.Exec(query, args...)
  71. }
  72. // set field value to row container
  73. func (o *rawSet) setFieldValue(ind reflect.Value, value interface{}) {
  74. switch ind.Kind() {
  75. case reflect.Bool:
  76. if value == nil {
  77. ind.SetBool(false)
  78. } else if v, ok := value.(bool); ok {
  79. ind.SetBool(v)
  80. } else {
  81. v, _ := StrTo(ToStr(value)).Bool()
  82. ind.SetBool(v)
  83. }
  84. case reflect.String:
  85. if value == nil {
  86. ind.SetString("")
  87. } else {
  88. ind.SetString(ToStr(value))
  89. }
  90. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  91. if value == nil {
  92. ind.SetInt(0)
  93. } else {
  94. val := reflect.ValueOf(value)
  95. switch val.Kind() {
  96. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  97. ind.SetInt(val.Int())
  98. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  99. ind.SetInt(int64(val.Uint()))
  100. default:
  101. v, _ := StrTo(ToStr(value)).Int64()
  102. ind.SetInt(v)
  103. }
  104. }
  105. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  106. if value == nil {
  107. ind.SetUint(0)
  108. } else {
  109. val := reflect.ValueOf(value)
  110. switch val.Kind() {
  111. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  112. ind.SetUint(uint64(val.Int()))
  113. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  114. ind.SetUint(val.Uint())
  115. default:
  116. v, _ := StrTo(ToStr(value)).Uint64()
  117. ind.SetUint(v)
  118. }
  119. }
  120. case reflect.Float64, reflect.Float32:
  121. if value == nil {
  122. ind.SetFloat(0)
  123. } else {
  124. val := reflect.ValueOf(value)
  125. switch val.Kind() {
  126. case reflect.Float64:
  127. ind.SetFloat(val.Float())
  128. default:
  129. v, _ := StrTo(ToStr(value)).Float64()
  130. ind.SetFloat(v)
  131. }
  132. }
  133. case reflect.Struct:
  134. if value == nil {
  135. ind.Set(reflect.Zero(ind.Type()))
  136. } else if _, ok := ind.Interface().(time.Time); ok {
  137. var str string
  138. switch d := value.(type) {
  139. case time.Time:
  140. o.orm.alias.DbBaser.TimeFromDB(&d, o.orm.alias.TZ)
  141. ind.Set(reflect.ValueOf(d))
  142. case []byte:
  143. str = string(d)
  144. case string:
  145. str = d
  146. }
  147. if str != "" {
  148. if len(str) >= 19 {
  149. str = str[:19]
  150. t, err := time.ParseInLocation(formatDateTime, str, o.orm.alias.TZ)
  151. if err == nil {
  152. t = t.In(DefaultTimeLoc)
  153. ind.Set(reflect.ValueOf(t))
  154. }
  155. } else if len(str) >= 10 {
  156. str = str[:10]
  157. t, err := time.ParseInLocation(formatDate, str, DefaultTimeLoc)
  158. if err == nil {
  159. ind.Set(reflect.ValueOf(t))
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. // set field value in loop for slice container
  167. func (o *rawSet) loopSetRefs(refs []interface{}, sInds []reflect.Value, nIndsPtr *[]reflect.Value, eTyps []reflect.Type, init bool) {
  168. nInds := *nIndsPtr
  169. cur := 0
  170. for i := 0; i < len(sInds); i++ {
  171. sInd := sInds[i]
  172. eTyp := eTyps[i]
  173. typ := eTyp
  174. isPtr := false
  175. if typ.Kind() == reflect.Ptr {
  176. isPtr = true
  177. typ = typ.Elem()
  178. }
  179. if typ.Kind() == reflect.Ptr {
  180. isPtr = true
  181. typ = typ.Elem()
  182. }
  183. var nInd reflect.Value
  184. if init {
  185. nInd = reflect.New(sInd.Type()).Elem()
  186. } else {
  187. nInd = nInds[i]
  188. }
  189. val := reflect.New(typ)
  190. ind := val.Elem()
  191. tpName := ind.Type().String()
  192. if ind.Kind() == reflect.Struct {
  193. if tpName == "time.Time" {
  194. value := reflect.ValueOf(refs[cur]).Elem().Interface()
  195. if isPtr && value == nil {
  196. val = reflect.New(val.Type()).Elem()
  197. } else {
  198. o.setFieldValue(ind, value)
  199. }
  200. cur++
  201. }
  202. } else {
  203. value := reflect.ValueOf(refs[cur]).Elem().Interface()
  204. if isPtr && value == nil {
  205. val = reflect.New(val.Type()).Elem()
  206. } else {
  207. o.setFieldValue(ind, value)
  208. }
  209. cur++
  210. }
  211. if nInd.Kind() == reflect.Slice {
  212. if isPtr {
  213. nInd = reflect.Append(nInd, val)
  214. } else {
  215. nInd = reflect.Append(nInd, ind)
  216. }
  217. } else {
  218. if isPtr {
  219. nInd.Set(val)
  220. } else {
  221. nInd.Set(ind)
  222. }
  223. }
  224. nInds[i] = nInd
  225. }
  226. }
  227. // query data and map to container
  228. func (o *rawSet) QueryRow(containers ...interface{}) error {
  229. var (
  230. refs = make([]interface{}, 0, len(containers))
  231. sInds []reflect.Value
  232. eTyps []reflect.Type
  233. sMi *modelInfo
  234. )
  235. structMode := false
  236. for _, container := range containers {
  237. val := reflect.ValueOf(container)
  238. ind := reflect.Indirect(val)
  239. if val.Kind() != reflect.Ptr {
  240. panic(fmt.Errorf("<RawSeter.QueryRow> all args must be use ptr"))
  241. }
  242. etyp := ind.Type()
  243. typ := etyp
  244. if typ.Kind() == reflect.Ptr {
  245. typ = typ.Elem()
  246. }
  247. sInds = append(sInds, ind)
  248. eTyps = append(eTyps, etyp)
  249. if typ.Kind() == reflect.Struct && typ.String() != "time.Time" {
  250. if len(containers) > 1 {
  251. panic(fmt.Errorf("<RawSeter.QueryRow> now support one struct only. see #384"))
  252. }
  253. structMode = true
  254. fn := getFullName(typ)
  255. if mi, ok := modelCache.getByFullName(fn); ok {
  256. sMi = mi
  257. }
  258. } else {
  259. var ref interface{}
  260. refs = append(refs, &ref)
  261. }
  262. }
  263. query := o.query
  264. o.orm.alias.DbBaser.ReplaceMarks(&query)
  265. args := getFlatParams(nil, o.args, o.orm.alias.TZ)
  266. rows, err := o.orm.db.Query(query, args...)
  267. if err != nil {
  268. if err == sql.ErrNoRows {
  269. return ErrNoRows
  270. }
  271. return err
  272. }
  273. defer rows.Close()
  274. if rows.Next() {
  275. if structMode {
  276. columns, err := rows.Columns()
  277. if err != nil {
  278. return err
  279. }
  280. columnsMp := make(map[string]interface{}, len(columns))
  281. refs = make([]interface{}, 0, len(columns))
  282. for _, col := range columns {
  283. var ref interface{}
  284. columnsMp[col] = &ref
  285. refs = append(refs, &ref)
  286. }
  287. if err := rows.Scan(refs...); err != nil {
  288. return err
  289. }
  290. ind := sInds[0]
  291. if ind.Kind() == reflect.Ptr {
  292. if ind.IsNil() || !ind.IsValid() {
  293. ind.Set(reflect.New(eTyps[0].Elem()))
  294. }
  295. ind = ind.Elem()
  296. }
  297. if sMi != nil {
  298. for _, col := range columns {
  299. if fi := sMi.fields.GetByColumn(col); fi != nil {
  300. value := reflect.ValueOf(columnsMp[col]).Elem().Interface()
  301. field := ind.FieldByIndex(fi.fieldIndex)
  302. if fi.fieldType&IsRelField > 0 {
  303. mf := reflect.New(fi.relModelInfo.addrField.Elem().Type())
  304. field.Set(mf)
  305. field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
  306. }
  307. o.setFieldValue(field, value)
  308. }
  309. }
  310. } else {
  311. for i := 0; i < ind.NumField(); i++ {
  312. f := ind.Field(i)
  313. fe := ind.Type().Field(i)
  314. _, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
  315. var col string
  316. if col = tags["column"]; col == "" {
  317. col = snakeString(fe.Name)
  318. }
  319. if v, ok := columnsMp[col]; ok {
  320. value := reflect.ValueOf(v).Elem().Interface()
  321. o.setFieldValue(f, value)
  322. }
  323. }
  324. }
  325. } else {
  326. if err := rows.Scan(refs...); err != nil {
  327. return err
  328. }
  329. nInds := make([]reflect.Value, len(sInds))
  330. o.loopSetRefs(refs, sInds, &nInds, eTyps, true)
  331. for i, sInd := range sInds {
  332. nInd := nInds[i]
  333. sInd.Set(nInd)
  334. }
  335. }
  336. } else {
  337. return ErrNoRows
  338. }
  339. return nil
  340. }
  341. // query data rows and map to container
  342. func (o *rawSet) QueryRows(containers ...interface{}) (int64, error) {
  343. var (
  344. refs = make([]interface{}, 0, len(containers))
  345. sInds []reflect.Value
  346. eTyps []reflect.Type
  347. sMi *modelInfo
  348. )
  349. structMode := false
  350. for _, container := range containers {
  351. val := reflect.ValueOf(container)
  352. sInd := reflect.Indirect(val)
  353. if val.Kind() != reflect.Ptr || sInd.Kind() != reflect.Slice {
  354. panic(fmt.Errorf("<RawSeter.QueryRows> all args must be use ptr slice"))
  355. }
  356. etyp := sInd.Type().Elem()
  357. typ := etyp
  358. if typ.Kind() == reflect.Ptr {
  359. typ = typ.Elem()
  360. }
  361. sInds = append(sInds, sInd)
  362. eTyps = append(eTyps, etyp)
  363. if typ.Kind() == reflect.Struct && typ.String() != "time.Time" {
  364. if len(containers) > 1 {
  365. panic(fmt.Errorf("<RawSeter.QueryRow> now support one struct only. see #384"))
  366. }
  367. structMode = true
  368. fn := getFullName(typ)
  369. if mi, ok := modelCache.getByFullName(fn); ok {
  370. sMi = mi
  371. }
  372. } else {
  373. var ref interface{}
  374. refs = append(refs, &ref)
  375. }
  376. }
  377. query := o.query
  378. o.orm.alias.DbBaser.ReplaceMarks(&query)
  379. args := getFlatParams(nil, o.args, o.orm.alias.TZ)
  380. rows, err := o.orm.db.Query(query, args...)
  381. if err != nil {
  382. return 0, err
  383. }
  384. defer rows.Close()
  385. var cnt int64
  386. nInds := make([]reflect.Value, len(sInds))
  387. sInd := sInds[0]
  388. for rows.Next() {
  389. if structMode {
  390. columns, err := rows.Columns()
  391. if err != nil {
  392. return 0, err
  393. }
  394. columnsMp := make(map[string]interface{}, len(columns))
  395. refs = make([]interface{}, 0, len(columns))
  396. for _, col := range columns {
  397. var ref interface{}
  398. columnsMp[col] = &ref
  399. refs = append(refs, &ref)
  400. }
  401. if err := rows.Scan(refs...); err != nil {
  402. return 0, err
  403. }
  404. if cnt == 0 && !sInd.IsNil() {
  405. sInd.Set(reflect.New(sInd.Type()).Elem())
  406. }
  407. var ind reflect.Value
  408. if eTyps[0].Kind() == reflect.Ptr {
  409. ind = reflect.New(eTyps[0].Elem())
  410. } else {
  411. ind = reflect.New(eTyps[0])
  412. }
  413. if ind.Kind() == reflect.Ptr {
  414. ind = ind.Elem()
  415. }
  416. if sMi != nil {
  417. for _, col := range columns {
  418. if fi := sMi.fields.GetByColumn(col); fi != nil {
  419. value := reflect.ValueOf(columnsMp[col]).Elem().Interface()
  420. field := ind.FieldByIndex(fi.fieldIndex)
  421. if fi.fieldType&IsRelField > 0 {
  422. mf := reflect.New(fi.relModelInfo.addrField.Elem().Type())
  423. field.Set(mf)
  424. field = mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
  425. }
  426. o.setFieldValue(field, value)
  427. }
  428. }
  429. } else {
  430. for i := 0; i < ind.NumField(); i++ {
  431. f := ind.Field(i)
  432. fe := ind.Type().Field(i)
  433. _, tags := parseStructTag(fe.Tag.Get(defaultStructTagName))
  434. var col string
  435. if col = tags["column"]; col == "" {
  436. col = snakeString(fe.Name)
  437. }
  438. if v, ok := columnsMp[col]; ok {
  439. value := reflect.ValueOf(v).Elem().Interface()
  440. o.setFieldValue(f, value)
  441. }
  442. }
  443. }
  444. if eTyps[0].Kind() == reflect.Ptr {
  445. ind = ind.Addr()
  446. }
  447. sInd = reflect.Append(sInd, ind)
  448. } else {
  449. if err := rows.Scan(refs...); err != nil {
  450. return 0, err
  451. }
  452. o.loopSetRefs(refs, sInds, &nInds, eTyps, cnt == 0)
  453. }
  454. cnt++
  455. }
  456. if cnt > 0 {
  457. if structMode {
  458. sInds[0].Set(sInd)
  459. } else {
  460. for i, sInd := range sInds {
  461. nInd := nInds[i]
  462. sInd.Set(nInd)
  463. }
  464. }
  465. }
  466. return cnt, nil
  467. }
  468. func (o *rawSet) readValues(container interface{}, needCols []string) (int64, error) {
  469. var (
  470. maps []Params
  471. lists []ParamsList
  472. list ParamsList
  473. )
  474. typ := 0
  475. switch container.(type) {
  476. case *[]Params:
  477. typ = 1
  478. case *[]ParamsList:
  479. typ = 2
  480. case *ParamsList:
  481. typ = 3
  482. default:
  483. panic(fmt.Errorf("<RawSeter> unsupport read values type `%T`", container))
  484. }
  485. query := o.query
  486. o.orm.alias.DbBaser.ReplaceMarks(&query)
  487. args := getFlatParams(nil, o.args, o.orm.alias.TZ)
  488. var rs *sql.Rows
  489. rs, err := o.orm.db.Query(query, args...)
  490. if err != nil {
  491. return 0, err
  492. }
  493. defer rs.Close()
  494. var (
  495. refs []interface{}
  496. cnt int64
  497. cols []string
  498. indexs []int
  499. )
  500. for rs.Next() {
  501. if cnt == 0 {
  502. columns, err := rs.Columns()
  503. if err != nil {
  504. return 0, err
  505. }
  506. if len(needCols) > 0 {
  507. indexs = make([]int, 0, len(needCols))
  508. } else {
  509. indexs = make([]int, 0, len(columns))
  510. }
  511. cols = columns
  512. refs = make([]interface{}, len(cols))
  513. for i := range refs {
  514. var ref sql.NullString
  515. refs[i] = &ref
  516. if len(needCols) > 0 {
  517. for _, c := range needCols {
  518. if c == cols[i] {
  519. indexs = append(indexs, i)
  520. }
  521. }
  522. } else {
  523. indexs = append(indexs, i)
  524. }
  525. }
  526. }
  527. if err := rs.Scan(refs...); err != nil {
  528. return 0, err
  529. }
  530. switch typ {
  531. case 1:
  532. params := make(Params, len(cols))
  533. for _, i := range indexs {
  534. ref := refs[i]
  535. value := reflect.Indirect(reflect.ValueOf(ref)).Interface().(sql.NullString)
  536. if value.Valid {
  537. params[cols[i]] = value.String
  538. } else {
  539. params[cols[i]] = nil
  540. }
  541. }
  542. maps = append(maps, params)
  543. case 2:
  544. params := make(ParamsList, 0, len(cols))
  545. for _, i := range indexs {
  546. ref := refs[i]
  547. value := reflect.Indirect(reflect.ValueOf(ref)).Interface().(sql.NullString)
  548. if value.Valid {
  549. params = append(params, value.String)
  550. } else {
  551. params = append(params, nil)
  552. }
  553. }
  554. lists = append(lists, params)
  555. case 3:
  556. for _, i := range indexs {
  557. ref := refs[i]
  558. value := reflect.Indirect(reflect.ValueOf(ref)).Interface().(sql.NullString)
  559. if value.Valid {
  560. list = append(list, value.String)
  561. } else {
  562. list = append(list, nil)
  563. }
  564. }
  565. }
  566. cnt++
  567. }
  568. switch v := container.(type) {
  569. case *[]Params:
  570. *v = maps
  571. case *[]ParamsList:
  572. *v = lists
  573. case *ParamsList:
  574. *v = list
  575. }
  576. return cnt, nil
  577. }
  578. func (o *rawSet) queryRowsTo(container interface{}, keyCol, valueCol string) (int64, error) {
  579. var (
  580. maps Params
  581. ind *reflect.Value
  582. )
  583. typ := 0
  584. switch container.(type) {
  585. case *Params:
  586. typ = 1
  587. default:
  588. typ = 2
  589. vl := reflect.ValueOf(container)
  590. id := reflect.Indirect(vl)
  591. if vl.Kind() != reflect.Ptr || id.Kind() != reflect.Struct {
  592. panic(fmt.Errorf("<RawSeter> RowsTo unsupport type `%T` need ptr struct", container))
  593. }
  594. ind = &id
  595. }
  596. query := o.query
  597. o.orm.alias.DbBaser.ReplaceMarks(&query)
  598. args := getFlatParams(nil, o.args, o.orm.alias.TZ)
  599. rs, err := o.orm.db.Query(query, args...)
  600. if err != nil {
  601. return 0, err
  602. }
  603. defer rs.Close()
  604. var (
  605. refs []interface{}
  606. cnt int64
  607. cols []string
  608. )
  609. var (
  610. keyIndex = -1
  611. valueIndex = -1
  612. )
  613. for rs.Next() {
  614. if cnt == 0 {
  615. columns, err := rs.Columns()
  616. if err != nil {
  617. return 0, err
  618. }
  619. cols = columns
  620. refs = make([]interface{}, len(cols))
  621. for i := range refs {
  622. if keyCol == cols[i] {
  623. keyIndex = i
  624. }
  625. if typ == 1 || keyIndex == i {
  626. var ref sql.NullString
  627. refs[i] = &ref
  628. } else {
  629. var ref interface{}
  630. refs[i] = &ref
  631. }
  632. if valueCol == cols[i] {
  633. valueIndex = i
  634. }
  635. }
  636. if keyIndex == -1 || valueIndex == -1 {
  637. panic(fmt.Errorf("<RawSeter> RowsTo unknown key, value column name `%s: %s`", keyCol, valueCol))
  638. }
  639. }
  640. if err := rs.Scan(refs...); err != nil {
  641. return 0, err
  642. }
  643. if cnt == 0 {
  644. switch typ {
  645. case 1:
  646. maps = make(Params)
  647. }
  648. }
  649. key := reflect.Indirect(reflect.ValueOf(refs[keyIndex])).Interface().(sql.NullString).String
  650. switch typ {
  651. case 1:
  652. value := reflect.Indirect(reflect.ValueOf(refs[valueIndex])).Interface().(sql.NullString)
  653. if value.Valid {
  654. maps[key] = value.String
  655. } else {
  656. maps[key] = nil
  657. }
  658. default:
  659. if id := ind.FieldByName(camelString(key)); id.IsValid() {
  660. o.setFieldValue(id, reflect.ValueOf(refs[valueIndex]).Elem().Interface())
  661. }
  662. }
  663. cnt++
  664. }
  665. if typ == 1 {
  666. v, _ := container.(*Params)
  667. *v = maps
  668. }
  669. return cnt, nil
  670. }
  671. // query data to []map[string]interface
  672. func (o *rawSet) Values(container *[]Params, cols ...string) (int64, error) {
  673. return o.readValues(container, cols)
  674. }
  675. // query data to [][]interface
  676. func (o *rawSet) ValuesList(container *[]ParamsList, cols ...string) (int64, error) {
  677. return o.readValues(container, cols)
  678. }
  679. // query data to []interface
  680. func (o *rawSet) ValuesFlat(container *ParamsList, cols ...string) (int64, error) {
  681. return o.readValues(container, cols)
  682. }
  683. // query all rows into map[string]interface with specify key and value column name.
  684. // keyCol = "name", valueCol = "value"
  685. // table data
  686. // name | value
  687. // total | 100
  688. // found | 200
  689. // to map[string]interface{}{
  690. // "total": 100,
  691. // "found": 200,
  692. // }
  693. func (o *rawSet) RowsToMap(result *Params, keyCol, valueCol string) (int64, error) {
  694. return o.queryRowsTo(result, keyCol, valueCol)
  695. }
  696. // query all rows into struct with specify key and value column name.
  697. // keyCol = "name", valueCol = "value"
  698. // table data
  699. // name | value
  700. // total | 100
  701. // found | 200
  702. // to struct {
  703. // Total int
  704. // Found int
  705. // }
  706. func (o *rawSet) RowsToStruct(ptrStruct interface{}, keyCol, valueCol string) (int64, error) {
  707. return o.queryRowsTo(ptrStruct, keyCol, valueCol)
  708. }
  709. // return prepared raw statement for used in times.
  710. func (o *rawSet) Prepare() (RawPreparer, error) {
  711. return newRawPreparer(o)
  712. }
  713. func newRawSet(orm *orm, query string, args []interface{}) RawSeter {
  714. o := new(rawSet)
  715. o.query = query
  716. o.args = args
  717. o.orm = orm
  718. return o
  719. }