1
0

db.go 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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. "errors"
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "time"
  22. )
  23. const (
  24. formatTime = "15:04:05"
  25. formatDate = "2006-01-02"
  26. formatDateTime = "2006-01-02 15:04:05"
  27. )
  28. var (
  29. // ErrMissPK missing pk error
  30. ErrMissPK = errors.New("missed pk value")
  31. )
  32. var (
  33. operators = map[string]bool{
  34. "exact": true,
  35. "iexact": true,
  36. "contains": true,
  37. "icontains": true,
  38. // "regex": true,
  39. // "iregex": true,
  40. "gt": true,
  41. "gte": true,
  42. "lt": true,
  43. "lte": true,
  44. "eq": true,
  45. "nq": true,
  46. "ne": true,
  47. "startswith": true,
  48. "endswith": true,
  49. "istartswith": true,
  50. "iendswith": true,
  51. "in": true,
  52. "between": true,
  53. // "year": true,
  54. // "month": true,
  55. // "day": true,
  56. // "week_day": true,
  57. "isnull": true,
  58. // "search": true,
  59. }
  60. )
  61. // an instance of dbBaser interface/
  62. type dbBase struct {
  63. ins dbBaser
  64. }
  65. // check dbBase implements dbBaser interface.
  66. var _ dbBaser = new(dbBase)
  67. // get struct columns values as interface slice.
  68. func (d *dbBase) collectValues(mi *modelInfo, ind reflect.Value, cols []string, skipAuto bool, insert bool, names *[]string, tz *time.Location) (values []interface{}, autoFields []string, err error) {
  69. if names == nil {
  70. ns := make([]string, 0, len(cols))
  71. names = &ns
  72. }
  73. values = make([]interface{}, 0, len(cols))
  74. for _, column := range cols {
  75. var fi *fieldInfo
  76. if fi, _ = mi.fields.GetByAny(column); fi != nil {
  77. column = fi.column
  78. } else {
  79. panic(fmt.Errorf("wrong db field/column name `%s` for model `%s`", column, mi.fullName))
  80. }
  81. if fi.dbcol == false || fi.auto && skipAuto {
  82. continue
  83. }
  84. value, err := d.collectFieldValue(mi, fi, ind, insert, tz)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. // ignore empty value auto field
  89. if insert && fi.auto {
  90. if fi.fieldType&IsPositiveIntegerField > 0 {
  91. if vu, ok := value.(uint64); !ok || vu == 0 {
  92. continue
  93. }
  94. } else {
  95. if vu, ok := value.(int64); !ok || vu == 0 {
  96. continue
  97. }
  98. }
  99. autoFields = append(autoFields, fi.column)
  100. }
  101. *names, values = append(*names, column), append(values, value)
  102. }
  103. return
  104. }
  105. // get one field value in struct column as interface.
  106. func (d *dbBase) collectFieldValue(mi *modelInfo, fi *fieldInfo, ind reflect.Value, insert bool, tz *time.Location) (interface{}, error) {
  107. var value interface{}
  108. if fi.pk {
  109. _, value, _ = getExistPk(mi, ind)
  110. } else {
  111. field := ind.FieldByIndex(fi.fieldIndex)
  112. if fi.isFielder {
  113. f := field.Addr().Interface().(Fielder)
  114. value = f.RawValue()
  115. } else {
  116. switch fi.fieldType {
  117. case TypeBooleanField:
  118. if nb, ok := field.Interface().(sql.NullBool); ok {
  119. value = nil
  120. if nb.Valid {
  121. value = nb.Bool
  122. }
  123. } else if field.Kind() == reflect.Ptr {
  124. if field.IsNil() {
  125. value = nil
  126. } else {
  127. value = field.Elem().Bool()
  128. }
  129. } else {
  130. value = field.Bool()
  131. }
  132. case TypeCharField, TypeTextField, TypeJSONField, TypeJsonbField:
  133. if ns, ok := field.Interface().(sql.NullString); ok {
  134. value = nil
  135. if ns.Valid {
  136. value = ns.String
  137. }
  138. } else if field.Kind() == reflect.Ptr {
  139. if field.IsNil() {
  140. value = nil
  141. } else {
  142. value = field.Elem().String()
  143. }
  144. } else {
  145. value = field.String()
  146. }
  147. case TypeFloatField, TypeDecimalField:
  148. if nf, ok := field.Interface().(sql.NullFloat64); ok {
  149. value = nil
  150. if nf.Valid {
  151. value = nf.Float64
  152. }
  153. } else if field.Kind() == reflect.Ptr {
  154. if field.IsNil() {
  155. value = nil
  156. } else {
  157. value = field.Elem().Float()
  158. }
  159. } else {
  160. vu := field.Interface()
  161. if _, ok := vu.(float32); ok {
  162. value, _ = StrTo(ToStr(vu)).Float64()
  163. } else {
  164. value = field.Float()
  165. }
  166. }
  167. case TypeTimeField, TypeDateField, TypeDateTimeField:
  168. value = field.Interface()
  169. if t, ok := value.(time.Time); ok {
  170. d.ins.TimeToDB(&t, tz)
  171. if t.IsZero() {
  172. value = nil
  173. } else {
  174. value = t
  175. }
  176. }
  177. default:
  178. switch {
  179. case fi.fieldType&IsPositiveIntegerField > 0:
  180. if field.Kind() == reflect.Ptr {
  181. if field.IsNil() {
  182. value = nil
  183. } else {
  184. value = field.Elem().Uint()
  185. }
  186. } else {
  187. value = field.Uint()
  188. }
  189. case fi.fieldType&IsIntegerField > 0:
  190. if ni, ok := field.Interface().(sql.NullInt64); ok {
  191. value = nil
  192. if ni.Valid {
  193. value = ni.Int64
  194. }
  195. } else if field.Kind() == reflect.Ptr {
  196. if field.IsNil() {
  197. value = nil
  198. } else {
  199. value = field.Elem().Int()
  200. }
  201. } else {
  202. value = field.Int()
  203. }
  204. case fi.fieldType&IsRelField > 0:
  205. if field.IsNil() {
  206. value = nil
  207. } else {
  208. if _, vu, ok := getExistPk(fi.relModelInfo, reflect.Indirect(field)); ok {
  209. value = vu
  210. } else {
  211. value = nil
  212. }
  213. }
  214. if fi.null == false && value == nil {
  215. return nil, fmt.Errorf("field `%s` cannot be NULL", fi.fullName)
  216. }
  217. }
  218. }
  219. }
  220. switch fi.fieldType {
  221. case TypeTimeField, TypeDateField, TypeDateTimeField:
  222. if fi.autoNow || fi.autoNowAdd && insert {
  223. if insert {
  224. if t, ok := value.(time.Time); ok && !t.IsZero() {
  225. break
  226. }
  227. }
  228. tnow := time.Now()
  229. d.ins.TimeToDB(&tnow, tz)
  230. value = tnow
  231. if fi.isFielder {
  232. f := field.Addr().Interface().(Fielder)
  233. f.SetRaw(tnow.In(DefaultTimeLoc))
  234. } else if field.Kind() == reflect.Ptr {
  235. v := tnow.In(DefaultTimeLoc)
  236. field.Set(reflect.ValueOf(&v))
  237. } else {
  238. field.Set(reflect.ValueOf(tnow.In(DefaultTimeLoc)))
  239. }
  240. }
  241. case TypeJSONField, TypeJsonbField:
  242. if s, ok := value.(string); (ok && len(s) == 0) || value == nil {
  243. if fi.colDefault && fi.initial.Exist() {
  244. value = fi.initial.String()
  245. } else {
  246. value = nil
  247. }
  248. }
  249. }
  250. }
  251. return value, nil
  252. }
  253. // create insert sql preparation statement object.
  254. func (d *dbBase) PrepareInsert(q dbQuerier, mi *modelInfo) (stmtQuerier, string, error) {
  255. Q := d.ins.TableQuote()
  256. dbcols := make([]string, 0, len(mi.fields.dbcols))
  257. marks := make([]string, 0, len(mi.fields.dbcols))
  258. for _, fi := range mi.fields.fieldsDB {
  259. if fi.auto == false {
  260. dbcols = append(dbcols, fi.column)
  261. marks = append(marks, "?")
  262. }
  263. }
  264. qmarks := strings.Join(marks, ", ")
  265. sep := fmt.Sprintf("%s, %s", Q, Q)
  266. columns := strings.Join(dbcols, sep)
  267. query := fmt.Sprintf("INSERT INTO %s%s%s (%s%s%s) VALUES (%s)", Q, mi.table, Q, Q, columns, Q, qmarks)
  268. d.ins.ReplaceMarks(&query)
  269. d.ins.HasReturningID(mi, &query)
  270. stmt, err := q.Prepare(query)
  271. return stmt, query, err
  272. }
  273. // insert struct with prepared statement and given struct reflect value.
  274. func (d *dbBase) InsertStmt(stmt stmtQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) {
  275. values, _, err := d.collectValues(mi, ind, mi.fields.dbcols, true, true, nil, tz)
  276. if err != nil {
  277. return 0, err
  278. }
  279. if d.ins.HasReturningID(mi, nil) {
  280. row := stmt.QueryRow(values...)
  281. var id int64
  282. err := row.Scan(&id)
  283. return id, err
  284. }
  285. res, err := stmt.Exec(values...)
  286. if err == nil {
  287. return res.LastInsertId()
  288. }
  289. return 0, err
  290. }
  291. // query sql ,read records and persist in dbBaser.
  292. func (d *dbBase) Read(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string, isForUpdate bool) error {
  293. var whereCols []string
  294. var args []interface{}
  295. // if specify cols length > 0, then use it for where condition.
  296. if len(cols) > 0 {
  297. var err error
  298. whereCols = make([]string, 0, len(cols))
  299. args, _, err = d.collectValues(mi, ind, cols, false, false, &whereCols, tz)
  300. if err != nil {
  301. return err
  302. }
  303. } else {
  304. // default use pk value as where condtion.
  305. pkColumn, pkValue, ok := getExistPk(mi, ind)
  306. if ok == false {
  307. return ErrMissPK
  308. }
  309. whereCols = []string{pkColumn}
  310. args = append(args, pkValue)
  311. }
  312. Q := d.ins.TableQuote()
  313. sep := fmt.Sprintf("%s, %s", Q, Q)
  314. sels := strings.Join(mi.fields.dbcols, sep)
  315. colsNum := len(mi.fields.dbcols)
  316. sep = fmt.Sprintf("%s = ? AND %s", Q, Q)
  317. wheres := strings.Join(whereCols, sep)
  318. forUpdate := ""
  319. if isForUpdate {
  320. forUpdate = "FOR UPDATE"
  321. }
  322. query := fmt.Sprintf("SELECT %s%s%s FROM %s%s%s WHERE %s%s%s = ? %s", Q, sels, Q, Q, mi.table, Q, Q, wheres, Q, forUpdate)
  323. refs := make([]interface{}, colsNum)
  324. for i := range refs {
  325. var ref interface{}
  326. refs[i] = &ref
  327. }
  328. d.ins.ReplaceMarks(&query)
  329. row := q.QueryRow(query, args...)
  330. if err := row.Scan(refs...); err != nil {
  331. if err == sql.ErrNoRows {
  332. return ErrNoRows
  333. }
  334. return err
  335. }
  336. elm := reflect.New(mi.addrField.Elem().Type())
  337. mind := reflect.Indirect(elm)
  338. d.setColsValues(mi, &mind, mi.fields.dbcols, refs, tz)
  339. ind.Set(mind)
  340. return nil
  341. }
  342. // execute insert sql dbQuerier with given struct reflect.Value.
  343. func (d *dbBase) Insert(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location) (int64, error) {
  344. names := make([]string, 0, len(mi.fields.dbcols))
  345. values, autoFields, err := d.collectValues(mi, ind, mi.fields.dbcols, false, true, &names, tz)
  346. if err != nil {
  347. return 0, err
  348. }
  349. id, err := d.InsertValue(q, mi, false, names, values)
  350. if err != nil {
  351. return 0, err
  352. }
  353. if len(autoFields) > 0 {
  354. err = d.ins.setval(q, mi, autoFields)
  355. }
  356. return id, err
  357. }
  358. // multi-insert sql with given slice struct reflect.Value.
  359. func (d *dbBase) InsertMulti(q dbQuerier, mi *modelInfo, sind reflect.Value, bulk int, tz *time.Location) (int64, error) {
  360. var (
  361. cnt int64
  362. nums int
  363. values []interface{}
  364. names []string
  365. )
  366. // typ := reflect.Indirect(mi.addrField).Type()
  367. length, autoFields := sind.Len(), make([]string, 0, 1)
  368. for i := 1; i <= length; i++ {
  369. ind := reflect.Indirect(sind.Index(i - 1))
  370. // Is this needed ?
  371. // if !ind.Type().AssignableTo(typ) {
  372. // return cnt, ErrArgs
  373. // }
  374. if i == 1 {
  375. var (
  376. vus []interface{}
  377. err error
  378. )
  379. vus, autoFields, err = d.collectValues(mi, ind, mi.fields.dbcols, false, true, &names, tz)
  380. if err != nil {
  381. return cnt, err
  382. }
  383. values = make([]interface{}, bulk*len(vus))
  384. nums += copy(values, vus)
  385. } else {
  386. vus, _, err := d.collectValues(mi, ind, mi.fields.dbcols, false, true, nil, tz)
  387. if err != nil {
  388. return cnt, err
  389. }
  390. if len(vus) != len(names) {
  391. return cnt, ErrArgs
  392. }
  393. nums += copy(values[nums:], vus)
  394. }
  395. if i > 1 && i%bulk == 0 || length == i {
  396. num, err := d.InsertValue(q, mi, true, names, values[:nums])
  397. if err != nil {
  398. return cnt, err
  399. }
  400. cnt += num
  401. nums = 0
  402. }
  403. }
  404. var err error
  405. if len(autoFields) > 0 {
  406. err = d.ins.setval(q, mi, autoFields)
  407. }
  408. return cnt, err
  409. }
  410. // execute insert sql with given struct and given values.
  411. // insert the given values, not the field values in struct.
  412. func (d *dbBase) InsertValue(q dbQuerier, mi *modelInfo, isMulti bool, names []string, values []interface{}) (int64, error) {
  413. Q := d.ins.TableQuote()
  414. marks := make([]string, len(names))
  415. for i := range marks {
  416. marks[i] = "?"
  417. }
  418. sep := fmt.Sprintf("%s, %s", Q, Q)
  419. qmarks := strings.Join(marks, ", ")
  420. columns := strings.Join(names, sep)
  421. multi := len(values) / len(names)
  422. if isMulti {
  423. qmarks = strings.Repeat(qmarks+"), (", multi-1) + qmarks
  424. }
  425. query := fmt.Sprintf("INSERT INTO %s%s%s (%s%s%s) VALUES (%s)", Q, mi.table, Q, Q, columns, Q, qmarks)
  426. d.ins.ReplaceMarks(&query)
  427. if isMulti || !d.ins.HasReturningID(mi, &query) {
  428. res, err := q.Exec(query, values...)
  429. if err == nil {
  430. if isMulti {
  431. return res.RowsAffected()
  432. }
  433. return res.LastInsertId()
  434. }
  435. return 0, err
  436. }
  437. row := q.QueryRow(query, values...)
  438. var id int64
  439. err := row.Scan(&id)
  440. return id, err
  441. }
  442. // InsertOrUpdate a row
  443. // If your primary key or unique column conflict will update
  444. // If no will insert
  445. func (d *dbBase) InsertOrUpdate(q dbQuerier, mi *modelInfo, ind reflect.Value, a *alias, args ...string) (int64, error) {
  446. args0 := ""
  447. iouStr := ""
  448. argsMap := map[string]string{}
  449. switch a.Driver {
  450. case DRMySQL:
  451. iouStr = "ON DUPLICATE KEY UPDATE"
  452. case DRPostgres:
  453. if len(args) == 0 {
  454. return 0, fmt.Errorf("`%s` use InsertOrUpdate must have a conflict column", a.DriverName)
  455. } else {
  456. args0 = strings.ToLower(args[0])
  457. iouStr = fmt.Sprintf("ON CONFLICT (%s) DO UPDATE SET", args0)
  458. }
  459. default:
  460. return 0, fmt.Errorf("`%s` nonsupport InsertOrUpdate in beego", a.DriverName)
  461. }
  462. //Get on the key-value pairs
  463. for _, v := range args {
  464. kv := strings.Split(v, "=")
  465. if len(kv) == 2 {
  466. argsMap[strings.ToLower(kv[0])] = kv[1]
  467. }
  468. }
  469. isMulti := false
  470. names := make([]string, 0, len(mi.fields.dbcols)-1)
  471. Q := d.ins.TableQuote()
  472. values, _, err := d.collectValues(mi, ind, mi.fields.dbcols, true, true, &names, a.TZ)
  473. if err != nil {
  474. return 0, err
  475. }
  476. marks := make([]string, len(names))
  477. updateValues := make([]interface{}, 0)
  478. updates := make([]string, len(names))
  479. var conflitValue interface{}
  480. for i, v := range names {
  481. marks[i] = "?"
  482. valueStr := argsMap[strings.ToLower(v)]
  483. if v == args0 {
  484. conflitValue = values[i]
  485. }
  486. if valueStr != "" {
  487. switch a.Driver {
  488. case DRMySQL:
  489. updates[i] = v + "=" + valueStr
  490. case DRPostgres:
  491. if conflitValue != nil {
  492. //postgres ON CONFLICT DO UPDATE SET can`t use colu=colu+values
  493. updates[i] = fmt.Sprintf("%s=(select %s from %s where %s = ? )", v, valueStr, mi.table, args0)
  494. updateValues = append(updateValues, conflitValue)
  495. } else {
  496. return 0, fmt.Errorf("`%s` must be in front of `%s` in your struct", args0, v)
  497. }
  498. }
  499. } else {
  500. updates[i] = v + "=?"
  501. updateValues = append(updateValues, values[i])
  502. }
  503. }
  504. values = append(values, updateValues...)
  505. sep := fmt.Sprintf("%s, %s", Q, Q)
  506. qmarks := strings.Join(marks, ", ")
  507. qupdates := strings.Join(updates, ", ")
  508. columns := strings.Join(names, sep)
  509. multi := len(values) / len(names)
  510. if isMulti {
  511. qmarks = strings.Repeat(qmarks+"), (", multi-1) + qmarks
  512. }
  513. //conflitValue maybe is a int,can`t use fmt.Sprintf
  514. query := fmt.Sprintf("INSERT INTO %s%s%s (%s%s%s) VALUES (%s) %s "+qupdates, Q, mi.table, Q, Q, columns, Q, qmarks, iouStr)
  515. d.ins.ReplaceMarks(&query)
  516. if isMulti || !d.ins.HasReturningID(mi, &query) {
  517. res, err := q.Exec(query, values...)
  518. if err == nil {
  519. if isMulti {
  520. return res.RowsAffected()
  521. }
  522. return res.LastInsertId()
  523. }
  524. return 0, err
  525. }
  526. row := q.QueryRow(query, values...)
  527. var id int64
  528. err = row.Scan(&id)
  529. if err.Error() == `pq: syntax error at or near "ON"` {
  530. err = fmt.Errorf("postgres version must 9.5 or higher")
  531. }
  532. return id, err
  533. }
  534. // execute update sql dbQuerier with given struct reflect.Value.
  535. func (d *dbBase) Update(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string) (int64, error) {
  536. pkName, pkValue, ok := getExistPk(mi, ind)
  537. if ok == false {
  538. return 0, ErrMissPK
  539. }
  540. var setNames []string
  541. // if specify cols length is zero, then commit all columns.
  542. if len(cols) == 0 {
  543. cols = mi.fields.dbcols
  544. setNames = make([]string, 0, len(mi.fields.dbcols)-1)
  545. } else {
  546. setNames = make([]string, 0, len(cols))
  547. }
  548. setValues, _, err := d.collectValues(mi, ind, cols, true, false, &setNames, tz)
  549. if err != nil {
  550. return 0, err
  551. }
  552. setValues = append(setValues, pkValue)
  553. Q := d.ins.TableQuote()
  554. sep := fmt.Sprintf("%s = ?, %s", Q, Q)
  555. setColumns := strings.Join(setNames, sep)
  556. query := fmt.Sprintf("UPDATE %s%s%s SET %s%s%s = ? WHERE %s%s%s = ?", Q, mi.table, Q, Q, setColumns, Q, Q, pkName, Q)
  557. d.ins.ReplaceMarks(&query)
  558. res, err := q.Exec(query, setValues...)
  559. if err == nil {
  560. return res.RowsAffected()
  561. }
  562. return 0, err
  563. }
  564. // execute delete sql dbQuerier with given struct reflect.Value.
  565. // delete index is pk.
  566. func (d *dbBase) Delete(q dbQuerier, mi *modelInfo, ind reflect.Value, tz *time.Location, cols []string) (int64, error) {
  567. var whereCols []string
  568. var args []interface{}
  569. // if specify cols length > 0, then use it for where condition.
  570. if len(cols) > 0 {
  571. var err error
  572. whereCols = make([]string, 0, len(cols))
  573. args, _, err = d.collectValues(mi, ind, cols, false, false, &whereCols, tz)
  574. if err != nil {
  575. return 0, err
  576. }
  577. } else {
  578. // default use pk value as where condtion.
  579. pkColumn, pkValue, ok := getExistPk(mi, ind)
  580. if ok == false {
  581. return 0, ErrMissPK
  582. }
  583. whereCols = []string{pkColumn}
  584. args = append(args, pkValue)
  585. }
  586. Q := d.ins.TableQuote()
  587. sep := fmt.Sprintf("%s = ? AND %s", Q, Q)
  588. wheres := strings.Join(whereCols, sep)
  589. query := fmt.Sprintf("DELETE FROM %s%s%s WHERE %s%s%s = ?", Q, mi.table, Q, Q, wheres, Q)
  590. d.ins.ReplaceMarks(&query)
  591. res, err := q.Exec(query, args...)
  592. if err == nil {
  593. num, err := res.RowsAffected()
  594. if err != nil {
  595. return 0, err
  596. }
  597. if num > 0 {
  598. if mi.fields.pk.auto {
  599. if mi.fields.pk.fieldType&IsPositiveIntegerField > 0 {
  600. ind.FieldByIndex(mi.fields.pk.fieldIndex).SetUint(0)
  601. } else {
  602. ind.FieldByIndex(mi.fields.pk.fieldIndex).SetInt(0)
  603. }
  604. }
  605. err := d.deleteRels(q, mi, args, tz)
  606. if err != nil {
  607. return num, err
  608. }
  609. }
  610. return num, err
  611. }
  612. return 0, err
  613. }
  614. // update table-related record by querySet.
  615. // need querySet not struct reflect.Value to update related records.
  616. func (d *dbBase) UpdateBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, params Params, tz *time.Location) (int64, error) {
  617. columns := make([]string, 0, len(params))
  618. values := make([]interface{}, 0, len(params))
  619. for col, val := range params {
  620. if fi, ok := mi.fields.GetByAny(col); ok == false || fi.dbcol == false {
  621. panic(fmt.Errorf("wrong field/column name `%s`", col))
  622. } else {
  623. columns = append(columns, fi.column)
  624. values = append(values, val)
  625. }
  626. }
  627. if len(columns) == 0 {
  628. panic(fmt.Errorf("update params cannot empty"))
  629. }
  630. tables := newDbTables(mi, d.ins)
  631. if qs != nil {
  632. tables.parseRelated(qs.related, qs.relDepth)
  633. }
  634. where, args := tables.getCondSQL(cond, false, tz)
  635. values = append(values, args...)
  636. join := tables.getJoinSQL()
  637. var query, T string
  638. Q := d.ins.TableQuote()
  639. if d.ins.SupportUpdateJoin() {
  640. T = "T0."
  641. }
  642. cols := make([]string, 0, len(columns))
  643. for i, v := range columns {
  644. col := fmt.Sprintf("%s%s%s%s", T, Q, v, Q)
  645. if c, ok := values[i].(colValue); ok {
  646. switch c.opt {
  647. case ColAdd:
  648. cols = append(cols, col+" = "+col+" + ?")
  649. case ColMinus:
  650. cols = append(cols, col+" = "+col+" - ?")
  651. case ColMultiply:
  652. cols = append(cols, col+" = "+col+" * ?")
  653. case ColExcept:
  654. cols = append(cols, col+" = "+col+" / ?")
  655. }
  656. values[i] = c.value
  657. } else {
  658. cols = append(cols, col+" = ?")
  659. }
  660. }
  661. sets := strings.Join(cols, ", ") + " "
  662. if d.ins.SupportUpdateJoin() {
  663. query = fmt.Sprintf("UPDATE %s%s%s T0 %sSET %s%s", Q, mi.table, Q, join, sets, where)
  664. } else {
  665. supQuery := fmt.Sprintf("SELECT T0.%s%s%s FROM %s%s%s T0 %s%s", Q, mi.fields.pk.column, Q, Q, mi.table, Q, join, where)
  666. query = fmt.Sprintf("UPDATE %s%s%s SET %sWHERE %s%s%s IN ( %s )", Q, mi.table, Q, sets, Q, mi.fields.pk.column, Q, supQuery)
  667. }
  668. d.ins.ReplaceMarks(&query)
  669. res, err := q.Exec(query, values...)
  670. if err == nil {
  671. return res.RowsAffected()
  672. }
  673. return 0, err
  674. }
  675. // delete related records.
  676. // do UpdateBanch or DeleteBanch by condition of tables' relationship.
  677. func (d *dbBase) deleteRels(q dbQuerier, mi *modelInfo, args []interface{}, tz *time.Location) error {
  678. for _, fi := range mi.fields.fieldsReverse {
  679. fi = fi.reverseFieldInfo
  680. switch fi.onDelete {
  681. case odCascade:
  682. cond := NewCondition().And(fmt.Sprintf("%s__in", fi.name), args...)
  683. _, err := d.DeleteBatch(q, nil, fi.mi, cond, tz)
  684. if err != nil {
  685. return err
  686. }
  687. case odSetDefault, odSetNULL:
  688. cond := NewCondition().And(fmt.Sprintf("%s__in", fi.name), args...)
  689. params := Params{fi.column: nil}
  690. if fi.onDelete == odSetDefault {
  691. params[fi.column] = fi.initial.String()
  692. }
  693. _, err := d.UpdateBatch(q, nil, fi.mi, cond, params, tz)
  694. if err != nil {
  695. return err
  696. }
  697. case odDoNothing:
  698. }
  699. }
  700. return nil
  701. }
  702. // delete table-related records.
  703. func (d *dbBase) DeleteBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, tz *time.Location) (int64, error) {
  704. tables := newDbTables(mi, d.ins)
  705. tables.skipEnd = true
  706. if qs != nil {
  707. tables.parseRelated(qs.related, qs.relDepth)
  708. }
  709. if cond == nil || cond.IsEmpty() {
  710. panic(fmt.Errorf("delete operation cannot execute without condition"))
  711. }
  712. Q := d.ins.TableQuote()
  713. where, args := tables.getCondSQL(cond, false, tz)
  714. join := tables.getJoinSQL()
  715. cols := fmt.Sprintf("T0.%s%s%s", Q, mi.fields.pk.column, Q)
  716. query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s", cols, Q, mi.table, Q, join, where)
  717. d.ins.ReplaceMarks(&query)
  718. var rs *sql.Rows
  719. r, err := q.Query(query, args...)
  720. if err != nil {
  721. return 0, err
  722. }
  723. rs = r
  724. defer rs.Close()
  725. var ref interface{}
  726. args = make([]interface{}, 0)
  727. cnt := 0
  728. for rs.Next() {
  729. if err := rs.Scan(&ref); err != nil {
  730. return 0, err
  731. }
  732. args = append(args, reflect.ValueOf(ref).Interface())
  733. cnt++
  734. }
  735. if cnt == 0 {
  736. return 0, nil
  737. }
  738. marks := make([]string, len(args))
  739. for i := range marks {
  740. marks[i] = "?"
  741. }
  742. sql := fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
  743. query = fmt.Sprintf("DELETE FROM %s%s%s WHERE %s%s%s %s", Q, mi.table, Q, Q, mi.fields.pk.column, Q, sql)
  744. d.ins.ReplaceMarks(&query)
  745. res, err := q.Exec(query, args...)
  746. if err == nil {
  747. num, err := res.RowsAffected()
  748. if err != nil {
  749. return 0, err
  750. }
  751. if num > 0 {
  752. err := d.deleteRels(q, mi, args, tz)
  753. if err != nil {
  754. return num, err
  755. }
  756. }
  757. return num, nil
  758. }
  759. return 0, err
  760. }
  761. // read related records.
  762. func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, container interface{}, tz *time.Location, cols []string) (int64, error) {
  763. val := reflect.ValueOf(container)
  764. ind := reflect.Indirect(val)
  765. errTyp := true
  766. one := true
  767. isPtr := true
  768. if val.Kind() == reflect.Ptr {
  769. fn := ""
  770. if ind.Kind() == reflect.Slice {
  771. one = false
  772. typ := ind.Type().Elem()
  773. switch typ.Kind() {
  774. case reflect.Ptr:
  775. fn = getFullName(typ.Elem())
  776. case reflect.Struct:
  777. isPtr = false
  778. fn = getFullName(typ)
  779. }
  780. } else {
  781. fn = getFullName(ind.Type())
  782. }
  783. errTyp = fn != mi.fullName
  784. }
  785. if errTyp {
  786. if one {
  787. panic(fmt.Errorf("wrong object type `%s` for rows scan, need *%s", val.Type(), mi.fullName))
  788. } else {
  789. panic(fmt.Errorf("wrong object type `%s` for rows scan, need *[]*%s or *[]%s", val.Type(), mi.fullName, mi.fullName))
  790. }
  791. }
  792. rlimit := qs.limit
  793. offset := qs.offset
  794. Q := d.ins.TableQuote()
  795. var tCols []string
  796. if len(cols) > 0 {
  797. hasRel := len(qs.related) > 0 || qs.relDepth > 0
  798. tCols = make([]string, 0, len(cols))
  799. var maps map[string]bool
  800. if hasRel {
  801. maps = make(map[string]bool)
  802. }
  803. for _, col := range cols {
  804. if fi, ok := mi.fields.GetByAny(col); ok {
  805. tCols = append(tCols, fi.column)
  806. if hasRel {
  807. maps[fi.column] = true
  808. }
  809. } else {
  810. panic(fmt.Errorf("wrong field/column name `%s`", col))
  811. }
  812. }
  813. if hasRel {
  814. for _, fi := range mi.fields.fieldsDB {
  815. if fi.fieldType&IsRelField > 0 {
  816. if maps[fi.column] == false {
  817. tCols = append(tCols, fi.column)
  818. }
  819. }
  820. }
  821. }
  822. } else {
  823. tCols = mi.fields.dbcols
  824. }
  825. colsNum := len(tCols)
  826. sep := fmt.Sprintf("%s, T0.%s", Q, Q)
  827. sels := fmt.Sprintf("T0.%s%s%s", Q, strings.Join(tCols, sep), Q)
  828. tables := newDbTables(mi, d.ins)
  829. tables.parseRelated(qs.related, qs.relDepth)
  830. where, args := tables.getCondSQL(cond, false, tz)
  831. groupBy := tables.getGroupSQL(qs.groups)
  832. orderBy := tables.getOrderSQL(qs.orders)
  833. limit := tables.getLimitSQL(mi, offset, rlimit)
  834. join := tables.getJoinSQL()
  835. for _, tbl := range tables.tables {
  836. if tbl.sel {
  837. colsNum += len(tbl.mi.fields.dbcols)
  838. sep := fmt.Sprintf("%s, %s.%s", Q, tbl.index, Q)
  839. sels += fmt.Sprintf(", %s.%s%s%s", tbl.index, Q, strings.Join(tbl.mi.fields.dbcols, sep), Q)
  840. }
  841. }
  842. sqlSelect := "SELECT"
  843. if qs.distinct {
  844. sqlSelect += " DISTINCT"
  845. }
  846. query := fmt.Sprintf("%s %s FROM %s%s%s T0 %s%s%s%s%s", sqlSelect, sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit)
  847. d.ins.ReplaceMarks(&query)
  848. var rs *sql.Rows
  849. r, err := q.Query(query, args...)
  850. if err != nil {
  851. return 0, err
  852. }
  853. rs = r
  854. refs := make([]interface{}, colsNum)
  855. for i := range refs {
  856. var ref interface{}
  857. refs[i] = &ref
  858. }
  859. defer rs.Close()
  860. slice := ind
  861. var cnt int64
  862. for rs.Next() {
  863. if one && cnt == 0 || one == false {
  864. if err := rs.Scan(refs...); err != nil {
  865. return 0, err
  866. }
  867. elm := reflect.New(mi.addrField.Elem().Type())
  868. mind := reflect.Indirect(elm)
  869. cacheV := make(map[string]*reflect.Value)
  870. cacheM := make(map[string]*modelInfo)
  871. trefs := refs
  872. d.setColsValues(mi, &mind, tCols, refs[:len(tCols)], tz)
  873. trefs = refs[len(tCols):]
  874. for _, tbl := range tables.tables {
  875. // loop selected tables
  876. if tbl.sel {
  877. last := mind
  878. names := ""
  879. mmi := mi
  880. // loop cascade models
  881. for _, name := range tbl.names {
  882. names += name
  883. if val, ok := cacheV[names]; ok {
  884. last = *val
  885. mmi = cacheM[names]
  886. } else {
  887. fi := mmi.fields.GetByName(name)
  888. lastm := mmi
  889. mmi = fi.relModelInfo
  890. field := last
  891. if last.Kind() != reflect.Invalid {
  892. field = reflect.Indirect(last.FieldByIndex(fi.fieldIndex))
  893. if field.IsValid() {
  894. d.setColsValues(mmi, &field, mmi.fields.dbcols, trefs[:len(mmi.fields.dbcols)], tz)
  895. for _, fi := range mmi.fields.fieldsReverse {
  896. if fi.inModel && fi.reverseFieldInfo.mi == lastm {
  897. if fi.reverseFieldInfo != nil {
  898. f := field.FieldByIndex(fi.fieldIndex)
  899. if f.Kind() == reflect.Ptr {
  900. f.Set(last.Addr())
  901. }
  902. }
  903. }
  904. }
  905. last = field
  906. }
  907. }
  908. cacheV[names] = &field
  909. cacheM[names] = mmi
  910. }
  911. }
  912. trefs = trefs[len(mmi.fields.dbcols):]
  913. }
  914. }
  915. if one {
  916. ind.Set(mind)
  917. } else {
  918. if cnt == 0 {
  919. // you can use a empty & caped container list
  920. // orm will not replace it
  921. if ind.Len() != 0 {
  922. // if container is not empty
  923. // create a new one
  924. slice = reflect.New(ind.Type()).Elem()
  925. }
  926. }
  927. if isPtr {
  928. slice = reflect.Append(slice, mind.Addr())
  929. } else {
  930. slice = reflect.Append(slice, mind)
  931. }
  932. }
  933. }
  934. cnt++
  935. }
  936. if one == false {
  937. if cnt > 0 {
  938. ind.Set(slice)
  939. } else {
  940. // when a result is empty and container is nil
  941. // to set a empty container
  942. if ind.IsNil() {
  943. ind.Set(reflect.MakeSlice(ind.Type(), 0, 0))
  944. }
  945. }
  946. }
  947. return cnt, nil
  948. }
  949. // excute count sql and return count result int64.
  950. func (d *dbBase) Count(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, tz *time.Location) (cnt int64, err error) {
  951. tables := newDbTables(mi, d.ins)
  952. tables.parseRelated(qs.related, qs.relDepth)
  953. where, args := tables.getCondSQL(cond, false, tz)
  954. groupBy := tables.getGroupSQL(qs.groups)
  955. tables.getOrderSQL(qs.orders)
  956. join := tables.getJoinSQL()
  957. Q := d.ins.TableQuote()
  958. query := fmt.Sprintf("SELECT COUNT(*) FROM %s%s%s T0 %s%s%s", Q, mi.table, Q, join, where, groupBy)
  959. if groupBy != "" {
  960. query = fmt.Sprintf("SELECT COUNT(*) FROM (%s) AS T", query)
  961. }
  962. d.ins.ReplaceMarks(&query)
  963. row := q.QueryRow(query, args...)
  964. err = row.Scan(&cnt)
  965. return
  966. }
  967. // generate sql with replacing operator string placeholders and replaced values.
  968. func (d *dbBase) GenerateOperatorSQL(mi *modelInfo, fi *fieldInfo, operator string, args []interface{}, tz *time.Location) (string, []interface{}) {
  969. sql := ""
  970. params := getFlatParams(fi, args, tz)
  971. if len(params) == 0 {
  972. panic(fmt.Errorf("operator `%s` need at least one args", operator))
  973. }
  974. arg := params[0]
  975. switch operator {
  976. case "in":
  977. marks := make([]string, len(params))
  978. for i := range marks {
  979. marks[i] = "?"
  980. }
  981. sql = fmt.Sprintf("IN (%s)", strings.Join(marks, ", "))
  982. case "between":
  983. if len(params) != 2 {
  984. panic(fmt.Errorf("operator `%s` need 2 args not %d", operator, len(params)))
  985. }
  986. sql = "BETWEEN ? AND ?"
  987. default:
  988. if len(params) > 1 {
  989. panic(fmt.Errorf("operator `%s` need 1 args not %d", operator, len(params)))
  990. }
  991. sql = d.ins.OperatorSQL(operator)
  992. switch operator {
  993. case "exact":
  994. if arg == nil {
  995. params[0] = "IS NULL"
  996. }
  997. case "iexact", "contains", "icontains", "startswith", "endswith", "istartswith", "iendswith":
  998. param := strings.Replace(ToStr(arg), `%`, `\%`, -1)
  999. switch operator {
  1000. case "iexact":
  1001. case "contains", "icontains":
  1002. param = fmt.Sprintf("%%%s%%", param)
  1003. case "startswith", "istartswith":
  1004. param = fmt.Sprintf("%s%%", param)
  1005. case "endswith", "iendswith":
  1006. param = fmt.Sprintf("%%%s", param)
  1007. }
  1008. params[0] = param
  1009. case "isnull":
  1010. if b, ok := arg.(bool); ok {
  1011. if b {
  1012. sql = "IS NULL"
  1013. } else {
  1014. sql = "IS NOT NULL"
  1015. }
  1016. params = nil
  1017. } else {
  1018. panic(fmt.Errorf("operator `%s` need a bool value not `%T`", operator, arg))
  1019. }
  1020. }
  1021. }
  1022. return sql, params
  1023. }
  1024. // gernerate sql string with inner function, such as UPPER(text).
  1025. func (d *dbBase) GenerateOperatorLeftCol(*fieldInfo, string, *string) {
  1026. // default not use
  1027. }
  1028. // set values to struct column.
  1029. func (d *dbBase) setColsValues(mi *modelInfo, ind *reflect.Value, cols []string, values []interface{}, tz *time.Location) {
  1030. for i, column := range cols {
  1031. val := reflect.Indirect(reflect.ValueOf(values[i])).Interface()
  1032. fi := mi.fields.GetByColumn(column)
  1033. field := ind.FieldByIndex(fi.fieldIndex)
  1034. value, err := d.convertValueFromDB(fi, val, tz)
  1035. if err != nil {
  1036. panic(fmt.Errorf("Raw value: `%v` %s", val, err.Error()))
  1037. }
  1038. _, err = d.setFieldValue(fi, value, field)
  1039. if err != nil {
  1040. panic(fmt.Errorf("Raw value: `%v` %s", val, err.Error()))
  1041. }
  1042. }
  1043. }
  1044. // convert value from database result to value following in field type.
  1045. func (d *dbBase) convertValueFromDB(fi *fieldInfo, val interface{}, tz *time.Location) (interface{}, error) {
  1046. if val == nil {
  1047. return nil, nil
  1048. }
  1049. var value interface{}
  1050. var tErr error
  1051. var str *StrTo
  1052. switch v := val.(type) {
  1053. case []byte:
  1054. s := StrTo(string(v))
  1055. str = &s
  1056. case string:
  1057. s := StrTo(v)
  1058. str = &s
  1059. }
  1060. fieldType := fi.fieldType
  1061. setValue:
  1062. switch {
  1063. case fieldType == TypeBooleanField:
  1064. if str == nil {
  1065. switch v := val.(type) {
  1066. case int64:
  1067. b := v == 1
  1068. value = b
  1069. default:
  1070. s := StrTo(ToStr(v))
  1071. str = &s
  1072. }
  1073. }
  1074. if str != nil {
  1075. b, err := str.Bool()
  1076. if err != nil {
  1077. tErr = err
  1078. goto end
  1079. }
  1080. value = b
  1081. }
  1082. case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField:
  1083. if str == nil {
  1084. value = ToStr(val)
  1085. } else {
  1086. value = str.String()
  1087. }
  1088. case fieldType == TypeTimeField || fieldType == TypeDateField || fieldType == TypeDateTimeField:
  1089. if str == nil {
  1090. switch t := val.(type) {
  1091. case time.Time:
  1092. d.ins.TimeFromDB(&t, tz)
  1093. value = t
  1094. default:
  1095. s := StrTo(ToStr(t))
  1096. str = &s
  1097. }
  1098. }
  1099. if str != nil {
  1100. s := str.String()
  1101. var (
  1102. t time.Time
  1103. err error
  1104. )
  1105. if len(s) >= 19 {
  1106. s = s[:19]
  1107. t, err = time.ParseInLocation(formatDateTime, s, tz)
  1108. } else if len(s) >= 10 {
  1109. if len(s) > 10 {
  1110. s = s[:10]
  1111. }
  1112. t, err = time.ParseInLocation(formatDate, s, tz)
  1113. } else if len(s) >= 8 {
  1114. if len(s) > 8 {
  1115. s = s[:8]
  1116. }
  1117. t, err = time.ParseInLocation(formatTime, s, tz)
  1118. }
  1119. t = t.In(DefaultTimeLoc)
  1120. if err != nil && s != "00:00:00" && s != "0000-00-00" && s != "0000-00-00 00:00:00" {
  1121. tErr = err
  1122. goto end
  1123. }
  1124. value = t
  1125. }
  1126. case fieldType&IsIntegerField > 0:
  1127. if str == nil {
  1128. s := StrTo(ToStr(val))
  1129. str = &s
  1130. }
  1131. if str != nil {
  1132. var err error
  1133. switch fieldType {
  1134. case TypeBitField:
  1135. _, err = str.Int8()
  1136. case TypeSmallIntegerField:
  1137. _, err = str.Int16()
  1138. case TypeIntegerField:
  1139. _, err = str.Int32()
  1140. case TypeBigIntegerField:
  1141. _, err = str.Int64()
  1142. case TypePositiveBitField:
  1143. _, err = str.Uint8()
  1144. case TypePositiveSmallIntegerField:
  1145. _, err = str.Uint16()
  1146. case TypePositiveIntegerField:
  1147. _, err = str.Uint32()
  1148. case TypePositiveBigIntegerField:
  1149. _, err = str.Uint64()
  1150. }
  1151. if err != nil {
  1152. tErr = err
  1153. goto end
  1154. }
  1155. if fieldType&IsPositiveIntegerField > 0 {
  1156. v, _ := str.Uint64()
  1157. value = v
  1158. } else {
  1159. v, _ := str.Int64()
  1160. value = v
  1161. }
  1162. }
  1163. case fieldType == TypeFloatField || fieldType == TypeDecimalField:
  1164. if str == nil {
  1165. switch v := val.(type) {
  1166. case float64:
  1167. value = v
  1168. default:
  1169. s := StrTo(ToStr(v))
  1170. str = &s
  1171. }
  1172. }
  1173. if str != nil {
  1174. v, err := str.Float64()
  1175. if err != nil {
  1176. tErr = err
  1177. goto end
  1178. }
  1179. value = v
  1180. }
  1181. case fieldType&IsRelField > 0:
  1182. fi = fi.relModelInfo.fields.pk
  1183. fieldType = fi.fieldType
  1184. goto setValue
  1185. }
  1186. end:
  1187. if tErr != nil {
  1188. err := fmt.Errorf("convert to `%s` failed, field: %s err: %s", fi.addrValue.Type(), fi.fullName, tErr)
  1189. return nil, err
  1190. }
  1191. return value, nil
  1192. }
  1193. // set one value to struct column field.
  1194. func (d *dbBase) setFieldValue(fi *fieldInfo, value interface{}, field reflect.Value) (interface{}, error) {
  1195. fieldType := fi.fieldType
  1196. isNative := fi.isFielder == false
  1197. setValue:
  1198. switch {
  1199. case fieldType == TypeBooleanField:
  1200. if isNative {
  1201. if nb, ok := field.Interface().(sql.NullBool); ok {
  1202. if value == nil {
  1203. nb.Valid = false
  1204. } else {
  1205. nb.Bool = value.(bool)
  1206. nb.Valid = true
  1207. }
  1208. field.Set(reflect.ValueOf(nb))
  1209. } else if field.Kind() == reflect.Ptr {
  1210. if value != nil {
  1211. v := value.(bool)
  1212. field.Set(reflect.ValueOf(&v))
  1213. }
  1214. } else {
  1215. if value == nil {
  1216. value = false
  1217. }
  1218. field.SetBool(value.(bool))
  1219. }
  1220. }
  1221. case fieldType == TypeCharField || fieldType == TypeTextField || fieldType == TypeJSONField || fieldType == TypeJsonbField:
  1222. if isNative {
  1223. if ns, ok := field.Interface().(sql.NullString); ok {
  1224. if value == nil {
  1225. ns.Valid = false
  1226. } else {
  1227. ns.String = value.(string)
  1228. ns.Valid = true
  1229. }
  1230. field.Set(reflect.ValueOf(ns))
  1231. } else if field.Kind() == reflect.Ptr {
  1232. if value != nil {
  1233. v := value.(string)
  1234. field.Set(reflect.ValueOf(&v))
  1235. }
  1236. } else {
  1237. if value == nil {
  1238. value = ""
  1239. }
  1240. field.SetString(value.(string))
  1241. }
  1242. }
  1243. case fieldType == TypeTimeField || fieldType == TypeDateField || fieldType == TypeDateTimeField:
  1244. if isNative {
  1245. if value == nil {
  1246. value = time.Time{}
  1247. } else if field.Kind() == reflect.Ptr {
  1248. if value != nil {
  1249. v := value.(time.Time)
  1250. field.Set(reflect.ValueOf(&v))
  1251. }
  1252. } else {
  1253. field.Set(reflect.ValueOf(value))
  1254. }
  1255. }
  1256. case fieldType == TypePositiveBitField && field.Kind() == reflect.Ptr:
  1257. if value != nil {
  1258. v := uint8(value.(uint64))
  1259. field.Set(reflect.ValueOf(&v))
  1260. }
  1261. case fieldType == TypePositiveSmallIntegerField && field.Kind() == reflect.Ptr:
  1262. if value != nil {
  1263. v := uint16(value.(uint64))
  1264. field.Set(reflect.ValueOf(&v))
  1265. }
  1266. case fieldType == TypePositiveIntegerField && field.Kind() == reflect.Ptr:
  1267. if value != nil {
  1268. if field.Type() == reflect.TypeOf(new(uint)) {
  1269. v := uint(value.(uint64))
  1270. field.Set(reflect.ValueOf(&v))
  1271. } else {
  1272. v := uint32(value.(uint64))
  1273. field.Set(reflect.ValueOf(&v))
  1274. }
  1275. }
  1276. case fieldType == TypePositiveBigIntegerField && field.Kind() == reflect.Ptr:
  1277. if value != nil {
  1278. v := value.(uint64)
  1279. field.Set(reflect.ValueOf(&v))
  1280. }
  1281. case fieldType == TypeBitField && field.Kind() == reflect.Ptr:
  1282. if value != nil {
  1283. v := int8(value.(int64))
  1284. field.Set(reflect.ValueOf(&v))
  1285. }
  1286. case fieldType == TypeSmallIntegerField && field.Kind() == reflect.Ptr:
  1287. if value != nil {
  1288. v := int16(value.(int64))
  1289. field.Set(reflect.ValueOf(&v))
  1290. }
  1291. case fieldType == TypeIntegerField && field.Kind() == reflect.Ptr:
  1292. if value != nil {
  1293. if field.Type() == reflect.TypeOf(new(int)) {
  1294. v := int(value.(int64))
  1295. field.Set(reflect.ValueOf(&v))
  1296. } else {
  1297. v := int32(value.(int64))
  1298. field.Set(reflect.ValueOf(&v))
  1299. }
  1300. }
  1301. case fieldType == TypeBigIntegerField && field.Kind() == reflect.Ptr:
  1302. if value != nil {
  1303. v := value.(int64)
  1304. field.Set(reflect.ValueOf(&v))
  1305. }
  1306. case fieldType&IsIntegerField > 0:
  1307. if fieldType&IsPositiveIntegerField > 0 {
  1308. if isNative {
  1309. if value == nil {
  1310. value = uint64(0)
  1311. }
  1312. field.SetUint(value.(uint64))
  1313. }
  1314. } else {
  1315. if isNative {
  1316. if ni, ok := field.Interface().(sql.NullInt64); ok {
  1317. if value == nil {
  1318. ni.Valid = false
  1319. } else {
  1320. ni.Int64 = value.(int64)
  1321. ni.Valid = true
  1322. }
  1323. field.Set(reflect.ValueOf(ni))
  1324. } else {
  1325. if value == nil {
  1326. value = int64(0)
  1327. }
  1328. field.SetInt(value.(int64))
  1329. }
  1330. }
  1331. }
  1332. case fieldType == TypeFloatField || fieldType == TypeDecimalField:
  1333. if isNative {
  1334. if nf, ok := field.Interface().(sql.NullFloat64); ok {
  1335. if value == nil {
  1336. nf.Valid = false
  1337. } else {
  1338. nf.Float64 = value.(float64)
  1339. nf.Valid = true
  1340. }
  1341. field.Set(reflect.ValueOf(nf))
  1342. } else if field.Kind() == reflect.Ptr {
  1343. if value != nil {
  1344. if field.Type() == reflect.TypeOf(new(float32)) {
  1345. v := float32(value.(float64))
  1346. field.Set(reflect.ValueOf(&v))
  1347. } else {
  1348. v := value.(float64)
  1349. field.Set(reflect.ValueOf(&v))
  1350. }
  1351. }
  1352. } else {
  1353. if value == nil {
  1354. value = float64(0)
  1355. }
  1356. field.SetFloat(value.(float64))
  1357. }
  1358. }
  1359. case fieldType&IsRelField > 0:
  1360. if value != nil {
  1361. fieldType = fi.relModelInfo.fields.pk.fieldType
  1362. mf := reflect.New(fi.relModelInfo.addrField.Elem().Type())
  1363. field.Set(mf)
  1364. f := mf.Elem().FieldByIndex(fi.relModelInfo.fields.pk.fieldIndex)
  1365. field = f
  1366. goto setValue
  1367. }
  1368. }
  1369. if isNative == false {
  1370. fd := field.Addr().Interface().(Fielder)
  1371. err := fd.SetRaw(value)
  1372. if err != nil {
  1373. err = fmt.Errorf("converted value `%v` set to Fielder `%s` failed, err: %s", value, fi.fullName, err)
  1374. return nil, err
  1375. }
  1376. }
  1377. return value, nil
  1378. }
  1379. // query sql, read values , save to *[]ParamList.
  1380. func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condition, exprs []string, container interface{}, tz *time.Location) (int64, error) {
  1381. var (
  1382. maps []Params
  1383. lists []ParamsList
  1384. list ParamsList
  1385. )
  1386. typ := 0
  1387. switch v := container.(type) {
  1388. case *[]Params:
  1389. d := *v
  1390. if len(d) == 0 {
  1391. maps = d
  1392. }
  1393. typ = 1
  1394. case *[]ParamsList:
  1395. d := *v
  1396. if len(d) == 0 {
  1397. lists = d
  1398. }
  1399. typ = 2
  1400. case *ParamsList:
  1401. d := *v
  1402. if len(d) == 0 {
  1403. list = d
  1404. }
  1405. typ = 3
  1406. default:
  1407. panic(fmt.Errorf("unsupport read values type `%T`", container))
  1408. }
  1409. tables := newDbTables(mi, d.ins)
  1410. var (
  1411. cols []string
  1412. infos []*fieldInfo
  1413. )
  1414. hasExprs := len(exprs) > 0
  1415. Q := d.ins.TableQuote()
  1416. if hasExprs {
  1417. cols = make([]string, 0, len(exprs))
  1418. infos = make([]*fieldInfo, 0, len(exprs))
  1419. for _, ex := range exprs {
  1420. index, name, fi, suc := tables.parseExprs(mi, strings.Split(ex, ExprSep))
  1421. if suc == false {
  1422. panic(fmt.Errorf("unknown field/column name `%s`", ex))
  1423. }
  1424. cols = append(cols, fmt.Sprintf("%s.%s%s%s %s%s%s", index, Q, fi.column, Q, Q, name, Q))
  1425. infos = append(infos, fi)
  1426. }
  1427. } else {
  1428. cols = make([]string, 0, len(mi.fields.dbcols))
  1429. infos = make([]*fieldInfo, 0, len(exprs))
  1430. for _, fi := range mi.fields.fieldsDB {
  1431. cols = append(cols, fmt.Sprintf("T0.%s%s%s %s%s%s", Q, fi.column, Q, Q, fi.name, Q))
  1432. infos = append(infos, fi)
  1433. }
  1434. }
  1435. where, args := tables.getCondSQL(cond, false, tz)
  1436. groupBy := tables.getGroupSQL(qs.groups)
  1437. orderBy := tables.getOrderSQL(qs.orders)
  1438. limit := tables.getLimitSQL(mi, qs.offset, qs.limit)
  1439. join := tables.getJoinSQL()
  1440. sels := strings.Join(cols, ", ")
  1441. sqlSelect := "SELECT"
  1442. if qs.distinct {
  1443. sqlSelect += " DISTINCT"
  1444. }
  1445. query := fmt.Sprintf("%s %s FROM %s%s%s T0 %s%s%s%s%s", sqlSelect, sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit)
  1446. d.ins.ReplaceMarks(&query)
  1447. rs, err := q.Query(query, args...)
  1448. if err != nil {
  1449. return 0, err
  1450. }
  1451. refs := make([]interface{}, len(cols))
  1452. for i := range refs {
  1453. var ref interface{}
  1454. refs[i] = &ref
  1455. }
  1456. defer rs.Close()
  1457. var (
  1458. cnt int64
  1459. columns []string
  1460. )
  1461. for rs.Next() {
  1462. if cnt == 0 {
  1463. cols, err := rs.Columns()
  1464. if err != nil {
  1465. return 0, err
  1466. }
  1467. columns = cols
  1468. }
  1469. if err := rs.Scan(refs...); err != nil {
  1470. return 0, err
  1471. }
  1472. switch typ {
  1473. case 1:
  1474. params := make(Params, len(cols))
  1475. for i, ref := range refs {
  1476. fi := infos[i]
  1477. val := reflect.Indirect(reflect.ValueOf(ref)).Interface()
  1478. value, err := d.convertValueFromDB(fi, val, tz)
  1479. if err != nil {
  1480. panic(fmt.Errorf("db value convert failed `%v` %s", val, err.Error()))
  1481. }
  1482. params[columns[i]] = value
  1483. }
  1484. maps = append(maps, params)
  1485. case 2:
  1486. params := make(ParamsList, 0, len(cols))
  1487. for i, ref := range refs {
  1488. fi := infos[i]
  1489. val := reflect.Indirect(reflect.ValueOf(ref)).Interface()
  1490. value, err := d.convertValueFromDB(fi, val, tz)
  1491. if err != nil {
  1492. panic(fmt.Errorf("db value convert failed `%v` %s", val, err.Error()))
  1493. }
  1494. params = append(params, value)
  1495. }
  1496. lists = append(lists, params)
  1497. case 3:
  1498. for i, ref := range refs {
  1499. fi := infos[i]
  1500. val := reflect.Indirect(reflect.ValueOf(ref)).Interface()
  1501. value, err := d.convertValueFromDB(fi, val, tz)
  1502. if err != nil {
  1503. panic(fmt.Errorf("db value convert failed `%v` %s", val, err.Error()))
  1504. }
  1505. list = append(list, value)
  1506. }
  1507. }
  1508. cnt++
  1509. }
  1510. switch v := container.(type) {
  1511. case *[]Params:
  1512. *v = maps
  1513. case *[]ParamsList:
  1514. *v = lists
  1515. case *ParamsList:
  1516. *v = list
  1517. }
  1518. return cnt, nil
  1519. }
  1520. func (d *dbBase) RowsTo(dbQuerier, *querySet, *modelInfo, *Condition, interface{}, string, string, *time.Location) (int64, error) {
  1521. return 0, nil
  1522. }
  1523. // flag of update joined record.
  1524. func (d *dbBase) SupportUpdateJoin() bool {
  1525. return true
  1526. }
  1527. func (d *dbBase) MaxLimit() uint64 {
  1528. return 18446744073709551615
  1529. }
  1530. // return quote.
  1531. func (d *dbBase) TableQuote() string {
  1532. return "`"
  1533. }
  1534. // replace value placeholer in parametered sql string.
  1535. func (d *dbBase) ReplaceMarks(query *string) {
  1536. // default use `?` as mark, do nothing
  1537. }
  1538. // flag of RETURNING sql.
  1539. func (d *dbBase) HasReturningID(*modelInfo, *string) bool {
  1540. return false
  1541. }
  1542. // sync auto key
  1543. func (d *dbBase) setval(db dbQuerier, mi *modelInfo, autoFields []string) error {
  1544. return nil
  1545. }
  1546. // convert time from db.
  1547. func (d *dbBase) TimeFromDB(t *time.Time, tz *time.Location) {
  1548. *t = t.In(tz)
  1549. }
  1550. // convert time to db.
  1551. func (d *dbBase) TimeToDB(t *time.Time, tz *time.Location) {
  1552. *t = t.In(tz)
  1553. }
  1554. // get database types.
  1555. func (d *dbBase) DbTypes() map[string]string {
  1556. return nil
  1557. }
  1558. // gt all tables.
  1559. func (d *dbBase) GetTables(db dbQuerier) (map[string]bool, error) {
  1560. tables := make(map[string]bool)
  1561. query := d.ins.ShowTablesQuery()
  1562. rows, err := db.Query(query)
  1563. if err != nil {
  1564. return tables, err
  1565. }
  1566. defer rows.Close()
  1567. for rows.Next() {
  1568. var table string
  1569. err := rows.Scan(&table)
  1570. if err != nil {
  1571. return tables, err
  1572. }
  1573. if table != "" {
  1574. tables[table] = true
  1575. }
  1576. }
  1577. return tables, nil
  1578. }
  1579. // get all cloumns in table.
  1580. func (d *dbBase) GetColumns(db dbQuerier, table string) (map[string][3]string, error) {
  1581. columns := make(map[string][3]string)
  1582. query := d.ins.ShowColumnsQuery(table)
  1583. rows, err := db.Query(query)
  1584. if err != nil {
  1585. return columns, err
  1586. }
  1587. defer rows.Close()
  1588. for rows.Next() {
  1589. var (
  1590. name string
  1591. typ string
  1592. null string
  1593. )
  1594. err := rows.Scan(&name, &typ, &null)
  1595. if err != nil {
  1596. return columns, err
  1597. }
  1598. columns[name] = [3]string{name, typ, null}
  1599. }
  1600. return columns, nil
  1601. }
  1602. // not implement.
  1603. func (d *dbBase) OperatorSQL(operator string) string {
  1604. panic(ErrNotImplement)
  1605. }
  1606. // not implement.
  1607. func (d *dbBase) ShowTablesQuery() string {
  1608. panic(ErrNotImplement)
  1609. }
  1610. // not implement.
  1611. func (d *dbBase) ShowColumnsQuery(table string) string {
  1612. panic(ErrNotImplement)
  1613. }
  1614. // not implement.
  1615. func (d *dbBase) IndexExists(dbQuerier, string, string) bool {
  1616. panic(ErrNotImplement)
  1617. }