session.go 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  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. "database/sql"
  7. "database/sql/driver"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "hash/crc32"
  12. "reflect"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/go-xorm/builder"
  17. "github.com/go-xorm/core"
  18. )
  19. // Session keep a pointer to sql.DB and provides all execution of all
  20. // kind of database operations.
  21. type Session struct {
  22. db *core.DB
  23. Engine *Engine
  24. Tx *core.Tx
  25. Statement Statement
  26. IsAutoCommit bool
  27. IsCommitedOrRollbacked bool
  28. TransType string
  29. IsAutoClose bool
  30. // Automatically reset the statement after operations that execute a SQL
  31. // query such as Count(), Find(), Get(), ...
  32. AutoResetStatement bool
  33. // !nashtsai! storing these beans due to yet committed tx
  34. afterInsertBeans map[interface{}]*[]func(interface{})
  35. afterUpdateBeans map[interface{}]*[]func(interface{})
  36. afterDeleteBeans map[interface{}]*[]func(interface{})
  37. // --
  38. beforeClosures []func(interface{})
  39. afterClosures []func(interface{})
  40. prepareStmt bool
  41. stmtCache map[uint32]*core.Stmt //key: hash.Hash32 of (queryStr, len(queryStr))
  42. cascadeDeep int
  43. // !evalphobia! stored the last executed query on this session
  44. //beforeSQLExec func(string, ...interface{})
  45. lastSQL string
  46. lastSQLArgs []interface{}
  47. }
  48. // Clone copy all the session's content and return a new session
  49. func (session *Session) Clone() *Session {
  50. var sess = *session
  51. return &sess
  52. }
  53. // Init reset the session as the init status.
  54. func (session *Session) Init() {
  55. session.Statement.Init()
  56. session.Statement.Engine = session.Engine
  57. session.IsAutoCommit = true
  58. session.IsCommitedOrRollbacked = false
  59. session.IsAutoClose = false
  60. session.AutoResetStatement = true
  61. session.prepareStmt = false
  62. // !nashtsai! is lazy init better?
  63. session.afterInsertBeans = make(map[interface{}]*[]func(interface{}), 0)
  64. session.afterUpdateBeans = make(map[interface{}]*[]func(interface{}), 0)
  65. session.afterDeleteBeans = make(map[interface{}]*[]func(interface{}), 0)
  66. session.beforeClosures = make([]func(interface{}), 0)
  67. session.afterClosures = make([]func(interface{}), 0)
  68. session.lastSQL = ""
  69. session.lastSQLArgs = []interface{}{}
  70. }
  71. // Close release the connection from pool
  72. func (session *Session) Close() {
  73. for _, v := range session.stmtCache {
  74. v.Close()
  75. }
  76. if session.db != nil {
  77. // When Close be called, if session is a transaction and do not call
  78. // Commit or Rollback, then call Rollback.
  79. if session.Tx != nil && !session.IsCommitedOrRollbacked {
  80. session.Rollback()
  81. }
  82. session.Tx = nil
  83. session.stmtCache = nil
  84. session.Init()
  85. session.db = nil
  86. }
  87. }
  88. func (session *Session) resetStatement() {
  89. if session.AutoResetStatement {
  90. session.Statement.Init()
  91. }
  92. }
  93. // Prepare set a flag to session that should be prepare statement before execute query
  94. func (session *Session) Prepare() *Session {
  95. session.prepareStmt = true
  96. return session
  97. }
  98. // Sql provides raw sql input parameter. When you have a complex SQL statement
  99. // and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
  100. //
  101. // Deprecated: use SQL instead.
  102. func (session *Session) Sql(query string, args ...interface{}) *Session {
  103. return session.SQL(query, args...)
  104. }
  105. // SQL provides raw sql input parameter. When you have a complex SQL statement
  106. // and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
  107. func (session *Session) SQL(query interface{}, args ...interface{}) *Session {
  108. session.Statement.SQL(query, args...)
  109. return session
  110. }
  111. // Where provides custom query condition.
  112. func (session *Session) Where(query interface{}, args ...interface{}) *Session {
  113. session.Statement.Where(query, args...)
  114. return session
  115. }
  116. // And provides custom query condition.
  117. func (session *Session) And(query interface{}, args ...interface{}) *Session {
  118. session.Statement.And(query, args...)
  119. return session
  120. }
  121. // Or provides custom query condition.
  122. func (session *Session) Or(query interface{}, args ...interface{}) *Session {
  123. session.Statement.Or(query, args...)
  124. return session
  125. }
  126. // Id provides converting id as a query condition
  127. //
  128. // Deprecated: use ID instead
  129. func (session *Session) Id(id interface{}) *Session {
  130. return session.ID(id)
  131. }
  132. // ID provides converting id as a query condition
  133. func (session *Session) ID(id interface{}) *Session {
  134. session.Statement.ID(id)
  135. return session
  136. }
  137. // Before Apply before Processor, affected bean is passed to closure arg
  138. func (session *Session) Before(closures func(interface{})) *Session {
  139. if closures != nil {
  140. session.beforeClosures = append(session.beforeClosures, closures)
  141. }
  142. return session
  143. }
  144. // After Apply after Processor, affected bean is passed to closure arg
  145. func (session *Session) After(closures func(interface{})) *Session {
  146. if closures != nil {
  147. session.afterClosures = append(session.afterClosures, closures)
  148. }
  149. return session
  150. }
  151. // Table can input a string or pointer to struct for special a table to operate.
  152. func (session *Session) Table(tableNameOrBean interface{}) *Session {
  153. session.Statement.Table(tableNameOrBean)
  154. return session
  155. }
  156. // Alias set the table alias
  157. func (session *Session) Alias(alias string) *Session {
  158. session.Statement.Alias(alias)
  159. return session
  160. }
  161. // In provides a query string like "id in (1, 2, 3)"
  162. func (session *Session) In(column string, args ...interface{}) *Session {
  163. session.Statement.In(column, args...)
  164. return session
  165. }
  166. // NotIn provides a query string like "id in (1, 2, 3)"
  167. func (session *Session) NotIn(column string, args ...interface{}) *Session {
  168. session.Statement.NotIn(column, args...)
  169. return session
  170. }
  171. // Incr provides a query string like "count = count + 1"
  172. func (session *Session) Incr(column string, arg ...interface{}) *Session {
  173. session.Statement.Incr(column, arg...)
  174. return session
  175. }
  176. // Decr provides a query string like "count = count - 1"
  177. func (session *Session) Decr(column string, arg ...interface{}) *Session {
  178. session.Statement.Decr(column, arg...)
  179. return session
  180. }
  181. // SetExpr provides a query string like "column = {expression}"
  182. func (session *Session) SetExpr(column string, expression string) *Session {
  183. session.Statement.SetExpr(column, expression)
  184. return session
  185. }
  186. // Select provides some columns to special
  187. func (session *Session) Select(str string) *Session {
  188. session.Statement.Select(str)
  189. return session
  190. }
  191. // Cols provides some columns to special
  192. func (session *Session) Cols(columns ...string) *Session {
  193. session.Statement.Cols(columns...)
  194. return session
  195. }
  196. // AllCols ask all columns
  197. func (session *Session) AllCols() *Session {
  198. session.Statement.AllCols()
  199. return session
  200. }
  201. // MustCols specify some columns must use even if they are empty
  202. func (session *Session) MustCols(columns ...string) *Session {
  203. session.Statement.MustCols(columns...)
  204. return session
  205. }
  206. // NoCascade indicate that no cascade load child object
  207. func (session *Session) NoCascade() *Session {
  208. session.Statement.UseCascade = false
  209. return session
  210. }
  211. // UseBool automatically retrieve condition according struct, but
  212. // if struct has bool field, it will ignore them. So use UseBool
  213. // to tell system to do not ignore them.
  214. // If no parameters, it will use all the bool field of struct, or
  215. // it will use parameters's columns
  216. func (session *Session) UseBool(columns ...string) *Session {
  217. session.Statement.UseBool(columns...)
  218. return session
  219. }
  220. // Distinct use for distinct columns. Caution: when you are using cache,
  221. // distinct will not be cached because cache system need id,
  222. // but distinct will not provide id
  223. func (session *Session) Distinct(columns ...string) *Session {
  224. session.Statement.Distinct(columns...)
  225. return session
  226. }
  227. // ForUpdate Set Read/Write locking for UPDATE
  228. func (session *Session) ForUpdate() *Session {
  229. session.Statement.IsForUpdate = true
  230. return session
  231. }
  232. // Omit Only not use the parameters as select or update columns
  233. func (session *Session) Omit(columns ...string) *Session {
  234. session.Statement.Omit(columns...)
  235. return session
  236. }
  237. // Nullable Set null when column is zero-value and nullable for update
  238. func (session *Session) Nullable(columns ...string) *Session {
  239. session.Statement.Nullable(columns...)
  240. return session
  241. }
  242. // NoAutoTime means do not automatically give created field and updated field
  243. // the current time on the current session temporarily
  244. func (session *Session) NoAutoTime() *Session {
  245. session.Statement.UseAutoTime = false
  246. return session
  247. }
  248. // NoAutoCondition disable generate SQL condition from beans
  249. func (session *Session) NoAutoCondition(no ...bool) *Session {
  250. session.Statement.NoAutoCondition(no...)
  251. return session
  252. }
  253. // Limit provide limit and offset query condition
  254. func (session *Session) Limit(limit int, start ...int) *Session {
  255. session.Statement.Limit(limit, start...)
  256. return session
  257. }
  258. // OrderBy provide order by query condition, the input parameter is the content
  259. // after order by on a sql statement.
  260. func (session *Session) OrderBy(order string) *Session {
  261. session.Statement.OrderBy(order)
  262. return session
  263. }
  264. // Desc provide desc order by query condition, the input parameters are columns.
  265. func (session *Session) Desc(colNames ...string) *Session {
  266. session.Statement.Desc(colNames...)
  267. return session
  268. }
  269. // Asc provide asc order by query condition, the input parameters are columns.
  270. func (session *Session) Asc(colNames ...string) *Session {
  271. session.Statement.Asc(colNames...)
  272. return session
  273. }
  274. // StoreEngine is only avialble mysql dialect currently
  275. func (session *Session) StoreEngine(storeEngine string) *Session {
  276. session.Statement.StoreEngine = storeEngine
  277. return session
  278. }
  279. // Charset is only avialble mysql dialect currently
  280. func (session *Session) Charset(charset string) *Session {
  281. session.Statement.Charset = charset
  282. return session
  283. }
  284. // Cascade indicates if loading sub Struct
  285. func (session *Session) Cascade(trueOrFalse ...bool) *Session {
  286. if len(trueOrFalse) >= 1 {
  287. session.Statement.UseCascade = trueOrFalse[0]
  288. }
  289. return session
  290. }
  291. // NoCache ask this session do not retrieve data from cache system and
  292. // get data from database directly.
  293. func (session *Session) NoCache() *Session {
  294. session.Statement.UseCache = false
  295. return session
  296. }
  297. // Join join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
  298. func (session *Session) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session {
  299. session.Statement.Join(joinOperator, tablename, condition, args...)
  300. return session
  301. }
  302. // GroupBy Generate Group By statement
  303. func (session *Session) GroupBy(keys string) *Session {
  304. session.Statement.GroupBy(keys)
  305. return session
  306. }
  307. // Having Generate Having statement
  308. func (session *Session) Having(conditions string) *Session {
  309. session.Statement.Having(conditions)
  310. return session
  311. }
  312. // DB db return the wrapper of sql.DB
  313. func (session *Session) DB() *core.DB {
  314. if session.db == nil {
  315. session.db = session.Engine.db
  316. session.stmtCache = make(map[uint32]*core.Stmt, 0)
  317. }
  318. return session.db
  319. }
  320. // Conds returns session query conditions
  321. func (session *Session) Conds() builder.Cond {
  322. return session.Statement.cond
  323. }
  324. func cleanupProcessorsClosures(slices *[]func(interface{})) {
  325. if len(*slices) > 0 {
  326. *slices = make([]func(interface{}), 0)
  327. }
  328. }
  329. func (session *Session) scanMapIntoStruct(obj interface{}, objMap map[string][]byte) error {
  330. dataStruct := rValue(obj)
  331. if dataStruct.Kind() != reflect.Struct {
  332. return errors.New("Expected a pointer to a struct")
  333. }
  334. var col *core.Column
  335. session.Statement.setRefValue(dataStruct)
  336. table := session.Statement.RefTable
  337. tableName := session.Statement.tableName
  338. for key, data := range objMap {
  339. if col = table.GetColumn(key); col == nil {
  340. session.Engine.logger.Warnf("struct %v's has not field %v. %v",
  341. table.Type.Name(), key, table.ColumnsSeq())
  342. continue
  343. }
  344. fieldName := col.FieldName
  345. fieldPath := strings.Split(fieldName, ".")
  346. var fieldValue reflect.Value
  347. if len(fieldPath) > 2 {
  348. session.Engine.logger.Error("Unsupported mutliderive", fieldName)
  349. continue
  350. } else if len(fieldPath) == 2 {
  351. parentField := dataStruct.FieldByName(fieldPath[0])
  352. if parentField.IsValid() {
  353. fieldValue = parentField.FieldByName(fieldPath[1])
  354. }
  355. } else {
  356. fieldValue = dataStruct.FieldByName(fieldName)
  357. }
  358. if !fieldValue.IsValid() || !fieldValue.CanSet() {
  359. session.Engine.logger.Warnf("table %v's column %v is not valid or cannot set", tableName, key)
  360. continue
  361. }
  362. err := session.bytes2Value(col, &fieldValue, data)
  363. if err != nil {
  364. return err
  365. }
  366. }
  367. return nil
  368. }
  369. func (session *Session) canCache() bool {
  370. if session.Statement.RefTable == nil ||
  371. session.Statement.JoinStr != "" ||
  372. session.Statement.RawSQL != "" ||
  373. !session.Statement.UseCache ||
  374. session.Statement.IsForUpdate ||
  375. session.Tx != nil ||
  376. len(session.Statement.selectStr) > 0 {
  377. return false
  378. }
  379. return true
  380. }
  381. func (session *Session) doPrepare(sqlStr string) (stmt *core.Stmt, err error) {
  382. crc := crc32.ChecksumIEEE([]byte(sqlStr))
  383. // TODO try hash(sqlStr+len(sqlStr))
  384. var has bool
  385. stmt, has = session.stmtCache[crc]
  386. if !has {
  387. stmt, err = session.DB().Prepare(sqlStr)
  388. if err != nil {
  389. return nil, err
  390. }
  391. session.stmtCache[crc] = stmt
  392. }
  393. return
  394. }
  395. func (session *Session) getField(dataStruct *reflect.Value, key string, table *core.Table, idx int) *reflect.Value {
  396. var col *core.Column
  397. if col = table.GetColumnIdx(key, idx); col == nil {
  398. //session.Engine.logger.Warnf("table %v has no column %v. %v", table.Name, key, table.ColumnsSeq())
  399. return nil
  400. }
  401. fieldValue, err := col.ValueOfV(dataStruct)
  402. if err != nil {
  403. session.Engine.logger.Error(err)
  404. return nil
  405. }
  406. if !fieldValue.IsValid() || !fieldValue.CanSet() {
  407. session.Engine.logger.Warnf("table %v's column %v is not valid or cannot set", table.Name, key)
  408. return nil
  409. }
  410. return fieldValue
  411. }
  412. // Cell cell is a result of one column field
  413. type Cell *interface{}
  414. func (session *Session) rows2Beans(rows *core.Rows, fields []string, fieldsCount int,
  415. table *core.Table, newElemFunc func() reflect.Value,
  416. sliceValueSetFunc func(*reflect.Value)) error {
  417. for rows.Next() {
  418. var newValue = newElemFunc()
  419. bean := newValue.Interface()
  420. dataStruct := rValue(bean)
  421. err := session._row2Bean(rows, fields, fieldsCount, bean, &dataStruct, table)
  422. if err != nil {
  423. return err
  424. }
  425. sliceValueSetFunc(&newValue)
  426. }
  427. return nil
  428. }
  429. func (session *Session) row2Bean(rows *core.Rows, fields []string, fieldsCount int, bean interface{}) error {
  430. dataStruct := rValue(bean)
  431. if dataStruct.Kind() != reflect.Struct {
  432. return errors.New("Expected a pointer to a struct")
  433. }
  434. session.Statement.setRefValue(dataStruct)
  435. return session._row2Bean(rows, fields, fieldsCount, bean, &dataStruct, session.Statement.RefTable)
  436. }
  437. func (session *Session) _row2Bean(rows *core.Rows, fields []string, fieldsCount int, bean interface{}, dataStruct *reflect.Value, table *core.Table) error {
  438. scanResults := make([]interface{}, fieldsCount)
  439. for i := 0; i < len(fields); i++ {
  440. var cell interface{}
  441. scanResults[i] = &cell
  442. }
  443. if err := rows.Scan(scanResults...); err != nil {
  444. return err
  445. }
  446. if b, hasBeforeSet := bean.(BeforeSetProcessor); hasBeforeSet {
  447. for ii, key := range fields {
  448. b.BeforeSet(key, Cell(scanResults[ii].(*interface{})))
  449. }
  450. }
  451. defer func() {
  452. if b, hasAfterSet := bean.(AfterSetProcessor); hasAfterSet {
  453. for ii, key := range fields {
  454. b.AfterSet(key, Cell(scanResults[ii].(*interface{})))
  455. }
  456. }
  457. }()
  458. var tempMap = make(map[string]int)
  459. for ii, key := range fields {
  460. var idx int
  461. var ok bool
  462. var lKey = strings.ToLower(key)
  463. if idx, ok = tempMap[lKey]; !ok {
  464. idx = 0
  465. } else {
  466. idx = idx + 1
  467. }
  468. tempMap[lKey] = idx
  469. if fieldValue := session.getField(dataStruct, key, table, idx); fieldValue != nil {
  470. rawValue := reflect.Indirect(reflect.ValueOf(scanResults[ii]))
  471. // if row is null then ignore
  472. if rawValue.Interface() == nil {
  473. continue
  474. }
  475. if fieldValue.CanAddr() {
  476. if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
  477. if data, err := value2Bytes(&rawValue); err == nil {
  478. structConvert.FromDB(data)
  479. } else {
  480. session.Engine.logger.Error(err)
  481. }
  482. continue
  483. }
  484. }
  485. if _, ok := fieldValue.Interface().(core.Conversion); ok {
  486. if data, err := value2Bytes(&rawValue); err == nil {
  487. if fieldValue.Kind() == reflect.Ptr && fieldValue.IsNil() {
  488. fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
  489. }
  490. fieldValue.Interface().(core.Conversion).FromDB(data)
  491. } else {
  492. session.Engine.logger.Error(err)
  493. }
  494. continue
  495. }
  496. rawValueType := reflect.TypeOf(rawValue.Interface())
  497. vv := reflect.ValueOf(rawValue.Interface())
  498. fieldType := fieldValue.Type()
  499. hasAssigned := false
  500. col := table.GetColumnIdx(key, idx)
  501. if col.SQLType.IsJson() {
  502. var bs []byte
  503. if rawValueType.Kind() == reflect.String {
  504. bs = []byte(vv.String())
  505. } else if rawValueType.ConvertibleTo(core.BytesType) {
  506. bs = vv.Bytes()
  507. } else {
  508. return fmt.Errorf("unsupported database data type: %s %v", key, rawValueType.Kind())
  509. }
  510. hasAssigned = true
  511. if len(bs) > 0 {
  512. if fieldValue.CanAddr() {
  513. err := json.Unmarshal(bs, fieldValue.Addr().Interface())
  514. if err != nil {
  515. session.Engine.logger.Error(key, err)
  516. return err
  517. }
  518. } else {
  519. x := reflect.New(fieldType)
  520. err := json.Unmarshal(bs, x.Interface())
  521. if err != nil {
  522. session.Engine.logger.Error(key, err)
  523. return err
  524. }
  525. fieldValue.Set(x.Elem())
  526. }
  527. }
  528. continue
  529. }
  530. switch fieldType.Kind() {
  531. case reflect.Complex64, reflect.Complex128:
  532. // TODO: reimplement this
  533. var bs []byte
  534. if rawValueType.Kind() == reflect.String {
  535. bs = []byte(vv.String())
  536. } else if rawValueType.ConvertibleTo(core.BytesType) {
  537. bs = vv.Bytes()
  538. }
  539. hasAssigned = true
  540. if len(bs) > 0 {
  541. if fieldValue.CanAddr() {
  542. err := json.Unmarshal(bs, fieldValue.Addr().Interface())
  543. if err != nil {
  544. session.Engine.logger.Error(err)
  545. return err
  546. }
  547. } else {
  548. x := reflect.New(fieldType)
  549. err := json.Unmarshal(bs, x.Interface())
  550. if err != nil {
  551. session.Engine.logger.Error(err)
  552. return err
  553. }
  554. fieldValue.Set(x.Elem())
  555. }
  556. }
  557. case reflect.Slice, reflect.Array:
  558. switch rawValueType.Kind() {
  559. case reflect.Slice, reflect.Array:
  560. switch rawValueType.Elem().Kind() {
  561. case reflect.Uint8:
  562. if fieldType.Elem().Kind() == reflect.Uint8 {
  563. hasAssigned = true
  564. fieldValue.Set(vv)
  565. }
  566. }
  567. }
  568. case reflect.String:
  569. if rawValueType.Kind() == reflect.String {
  570. hasAssigned = true
  571. fieldValue.SetString(vv.String())
  572. }
  573. case reflect.Bool:
  574. if rawValueType.Kind() == reflect.Bool {
  575. hasAssigned = true
  576. fieldValue.SetBool(vv.Bool())
  577. }
  578. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  579. switch rawValueType.Kind() {
  580. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  581. hasAssigned = true
  582. fieldValue.SetInt(vv.Int())
  583. }
  584. case reflect.Float32, reflect.Float64:
  585. switch rawValueType.Kind() {
  586. case reflect.Float32, reflect.Float64:
  587. hasAssigned = true
  588. fieldValue.SetFloat(vv.Float())
  589. }
  590. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  591. switch rawValueType.Kind() {
  592. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  593. hasAssigned = true
  594. fieldValue.SetUint(vv.Uint())
  595. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  596. hasAssigned = true
  597. fieldValue.SetUint(uint64(vv.Int()))
  598. }
  599. case reflect.Struct:
  600. if fieldType.ConvertibleTo(core.TimeType) {
  601. if rawValueType == core.TimeType {
  602. hasAssigned = true
  603. t := vv.Convert(core.TimeType).Interface().(time.Time)
  604. z, _ := t.Zone()
  605. dbTZ := session.Engine.DatabaseTZ
  606. if dbTZ == nil {
  607. if session.Engine.dialect.DBType() == core.SQLITE {
  608. dbTZ = time.UTC
  609. } else {
  610. dbTZ = time.Local
  611. }
  612. }
  613. // set new location if database don't save timezone or give an incorrect timezone
  614. if len(z) == 0 || t.Year() == 0 || t.Location().String() != dbTZ.String() { // !nashtsai! HACK tmp work around for lib/pq doesn't properly time with location
  615. session.Engine.logger.Debugf("empty zone key[%v] : %v | zone: %v | location: %+v\n", key, t, z, *t.Location())
  616. t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(),
  617. t.Minute(), t.Second(), t.Nanosecond(), dbTZ)
  618. }
  619. // !nashtsai! convert to engine location
  620. if col.TimeZone == nil {
  621. t = t.In(session.Engine.TZLocation)
  622. } else {
  623. t = t.In(col.TimeZone)
  624. }
  625. fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
  626. // t = fieldValue.Interface().(time.Time)
  627. // z, _ = t.Zone()
  628. // session.Engine.LogDebug("fieldValue key[%v]: %v | zone: %v | location: %+v\n", key, t, z, *t.Location())
  629. } else if rawValueType == core.IntType || rawValueType == core.Int64Type ||
  630. rawValueType == core.Int32Type {
  631. hasAssigned = true
  632. var tz *time.Location
  633. if col.TimeZone == nil {
  634. tz = session.Engine.TZLocation
  635. } else {
  636. tz = col.TimeZone
  637. }
  638. t := time.Unix(vv.Int(), 0).In(tz)
  639. //vv = reflect.ValueOf(t)
  640. fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
  641. } else {
  642. if d, ok := vv.Interface().([]uint8); ok {
  643. hasAssigned = true
  644. t, err := session.byte2Time(col, d)
  645. if err != nil {
  646. session.Engine.logger.Error("byte2Time error:", err.Error())
  647. hasAssigned = false
  648. } else {
  649. fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
  650. }
  651. } else if d, ok := vv.Interface().(string); ok {
  652. hasAssigned = true
  653. t, err := session.str2Time(col, d)
  654. if err != nil {
  655. session.Engine.logger.Error("byte2Time error:", err.Error())
  656. hasAssigned = false
  657. } else {
  658. fieldValue.Set(reflect.ValueOf(t).Convert(fieldType))
  659. }
  660. } else {
  661. panic(fmt.Sprintf("rawValueType is %v, value is %v", rawValueType, vv.Interface()))
  662. }
  663. }
  664. } else if nulVal, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
  665. // !<winxxp>! 增加支持sql.Scanner接口的结构,如sql.NullString
  666. hasAssigned = true
  667. if err := nulVal.Scan(vv.Interface()); err != nil {
  668. session.Engine.logger.Error("sql.Sanner error:", err.Error())
  669. hasAssigned = false
  670. }
  671. } else if col.SQLType.IsJson() {
  672. if rawValueType.Kind() == reflect.String {
  673. hasAssigned = true
  674. x := reflect.New(fieldType)
  675. if len([]byte(vv.String())) > 0 {
  676. err := json.Unmarshal([]byte(vv.String()), x.Interface())
  677. if err != nil {
  678. session.Engine.logger.Error(err)
  679. return err
  680. }
  681. fieldValue.Set(x.Elem())
  682. }
  683. } else if rawValueType.Kind() == reflect.Slice {
  684. hasAssigned = true
  685. x := reflect.New(fieldType)
  686. if len(vv.Bytes()) > 0 {
  687. err := json.Unmarshal(vv.Bytes(), x.Interface())
  688. if err != nil {
  689. session.Engine.logger.Error(err)
  690. return err
  691. }
  692. fieldValue.Set(x.Elem())
  693. }
  694. }
  695. } else if session.Statement.UseCascade {
  696. table := session.Engine.autoMapType(*fieldValue)
  697. if table != nil {
  698. hasAssigned = true
  699. if len(table.PrimaryKeys) != 1 {
  700. panic("unsupported non or composited primary key cascade")
  701. }
  702. var pk = make(core.PK, len(table.PrimaryKeys))
  703. switch rawValueType.Kind() {
  704. case reflect.Int64:
  705. pk[0] = vv.Int()
  706. case reflect.Int:
  707. pk[0] = int(vv.Int())
  708. case reflect.Int32:
  709. pk[0] = int32(vv.Int())
  710. case reflect.Int16:
  711. pk[0] = int16(vv.Int())
  712. case reflect.Int8:
  713. pk[0] = int8(vv.Int())
  714. case reflect.Uint64:
  715. pk[0] = vv.Uint()
  716. case reflect.Uint:
  717. pk[0] = uint(vv.Uint())
  718. case reflect.Uint32:
  719. pk[0] = uint32(vv.Uint())
  720. case reflect.Uint16:
  721. pk[0] = uint16(vv.Uint())
  722. case reflect.Uint8:
  723. pk[0] = uint8(vv.Uint())
  724. case reflect.String:
  725. pk[0] = vv.String()
  726. case reflect.Slice:
  727. pk[0], _ = strconv.ParseInt(string(rawValue.Interface().([]byte)), 10, 64)
  728. default:
  729. panic(fmt.Sprintf("unsupported primary key type: %v, %v", rawValueType, fieldValue))
  730. }
  731. if !isPKZero(pk) {
  732. // !nashtsai! TODO for hasOne relationship, it's preferred to use join query for eager fetch
  733. // however, also need to consider adding a 'lazy' attribute to xorm tag which allow hasOne
  734. // property to be fetched lazily
  735. structInter := reflect.New(fieldValue.Type())
  736. newsession := session.Engine.NewSession()
  737. defer newsession.Close()
  738. has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface())
  739. if err != nil {
  740. return err
  741. }
  742. if has {
  743. //v := structInter.Elem().Interface()
  744. //fieldValue.Set(reflect.ValueOf(v))
  745. fieldValue.Set(structInter.Elem())
  746. } else {
  747. return errors.New("cascade obj is not exist")
  748. }
  749. }
  750. } else {
  751. session.Engine.logger.Error("unsupported struct type in Scan: ", fieldValue.Type().String())
  752. }
  753. }
  754. case reflect.Ptr:
  755. // !nashtsai! TODO merge duplicated codes above
  756. //typeStr := fieldType.String()
  757. switch fieldType {
  758. // following types case matching ptr's native type, therefore assign ptr directly
  759. case core.PtrStringType:
  760. if rawValueType.Kind() == reflect.String {
  761. x := vv.String()
  762. hasAssigned = true
  763. fieldValue.Set(reflect.ValueOf(&x))
  764. }
  765. case core.PtrBoolType:
  766. if rawValueType.Kind() == reflect.Bool {
  767. x := vv.Bool()
  768. hasAssigned = true
  769. fieldValue.Set(reflect.ValueOf(&x))
  770. }
  771. case core.PtrTimeType:
  772. if rawValueType == core.PtrTimeType {
  773. hasAssigned = true
  774. var x = rawValue.Interface().(time.Time)
  775. fieldValue.Set(reflect.ValueOf(&x))
  776. }
  777. case core.PtrFloat64Type:
  778. if rawValueType.Kind() == reflect.Float64 {
  779. x := vv.Float()
  780. hasAssigned = true
  781. fieldValue.Set(reflect.ValueOf(&x))
  782. }
  783. case core.PtrUint64Type:
  784. if rawValueType.Kind() == reflect.Int64 {
  785. var x = uint64(vv.Int())
  786. hasAssigned = true
  787. fieldValue.Set(reflect.ValueOf(&x))
  788. }
  789. case core.PtrInt64Type:
  790. if rawValueType.Kind() == reflect.Int64 {
  791. x := vv.Int()
  792. hasAssigned = true
  793. fieldValue.Set(reflect.ValueOf(&x))
  794. }
  795. case core.PtrFloat32Type:
  796. if rawValueType.Kind() == reflect.Float64 {
  797. var x = float32(vv.Float())
  798. hasAssigned = true
  799. fieldValue.Set(reflect.ValueOf(&x))
  800. }
  801. case core.PtrIntType:
  802. if rawValueType.Kind() == reflect.Int64 {
  803. var x = int(vv.Int())
  804. hasAssigned = true
  805. fieldValue.Set(reflect.ValueOf(&x))
  806. }
  807. case core.PtrInt32Type:
  808. if rawValueType.Kind() == reflect.Int64 {
  809. var x = int32(vv.Int())
  810. hasAssigned = true
  811. fieldValue.Set(reflect.ValueOf(&x))
  812. }
  813. case core.PtrInt8Type:
  814. if rawValueType.Kind() == reflect.Int64 {
  815. var x = int8(vv.Int())
  816. hasAssigned = true
  817. fieldValue.Set(reflect.ValueOf(&x))
  818. }
  819. case core.PtrInt16Type:
  820. if rawValueType.Kind() == reflect.Int64 {
  821. var x = int16(vv.Int())
  822. hasAssigned = true
  823. fieldValue.Set(reflect.ValueOf(&x))
  824. }
  825. case core.PtrUintType:
  826. if rawValueType.Kind() == reflect.Int64 {
  827. var x = uint(vv.Int())
  828. hasAssigned = true
  829. fieldValue.Set(reflect.ValueOf(&x))
  830. }
  831. case core.PtrUint32Type:
  832. if rawValueType.Kind() == reflect.Int64 {
  833. var x = uint32(vv.Int())
  834. hasAssigned = true
  835. fieldValue.Set(reflect.ValueOf(&x))
  836. }
  837. case core.Uint8Type:
  838. if rawValueType.Kind() == reflect.Int64 {
  839. var x = uint8(vv.Int())
  840. hasAssigned = true
  841. fieldValue.Set(reflect.ValueOf(&x))
  842. }
  843. case core.Uint16Type:
  844. if rawValueType.Kind() == reflect.Int64 {
  845. var x = uint16(vv.Int())
  846. hasAssigned = true
  847. fieldValue.Set(reflect.ValueOf(&x))
  848. }
  849. case core.Complex64Type:
  850. var x complex64
  851. if len([]byte(vv.String())) > 0 {
  852. err := json.Unmarshal([]byte(vv.String()), &x)
  853. if err != nil {
  854. session.Engine.logger.Error(err)
  855. } else {
  856. fieldValue.Set(reflect.ValueOf(&x))
  857. }
  858. }
  859. hasAssigned = true
  860. case core.Complex128Type:
  861. var x complex128
  862. if len([]byte(vv.String())) > 0 {
  863. err := json.Unmarshal([]byte(vv.String()), &x)
  864. if err != nil {
  865. session.Engine.logger.Error(err)
  866. } else {
  867. fieldValue.Set(reflect.ValueOf(&x))
  868. }
  869. }
  870. hasAssigned = true
  871. } // switch fieldType
  872. // default:
  873. // session.Engine.LogError("unsupported type in Scan: ", reflect.TypeOf(v).String())
  874. } // switch fieldType.Kind()
  875. // !nashtsai! for value can't be assigned directly fallback to convert to []byte then back to value
  876. if !hasAssigned {
  877. data, err := value2Bytes(&rawValue)
  878. if err == nil {
  879. session.bytes2Value(col, fieldValue, data)
  880. } else {
  881. session.Engine.logger.Error(err.Error())
  882. }
  883. }
  884. }
  885. }
  886. return nil
  887. }
  888. func (session *Session) queryPreprocess(sqlStr *string, paramStr ...interface{}) {
  889. for _, filter := range session.Engine.dialect.Filters() {
  890. *sqlStr = filter.Do(*sqlStr, session.Engine.dialect, session.Statement.RefTable)
  891. }
  892. session.saveLastSQL(*sqlStr, paramStr...)
  893. }
  894. func (session *Session) str2Time(col *core.Column, data string) (outTime time.Time, outErr error) {
  895. sdata := strings.TrimSpace(data)
  896. var x time.Time
  897. var err error
  898. if sdata == "0000-00-00 00:00:00" ||
  899. sdata == "0001-01-01 00:00:00" {
  900. } else if !strings.ContainsAny(sdata, "- :") { // !nashtsai! has only found that mymysql driver is using this for time type column
  901. // time stamp
  902. sd, err := strconv.ParseInt(sdata, 10, 64)
  903. if err == nil {
  904. x = time.Unix(sd, 0)
  905. // !nashtsai! HACK mymysql driver is causing Local location being change to CHAT and cause wrong time conversion
  906. if col.TimeZone == nil {
  907. x = x.In(session.Engine.TZLocation)
  908. } else {
  909. x = x.In(col.TimeZone)
  910. }
  911. session.Engine.logger.Debugf("time(0) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  912. } else {
  913. session.Engine.logger.Debugf("time(0) err key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  914. }
  915. } else if len(sdata) > 19 && strings.Contains(sdata, "-") {
  916. x, err = time.ParseInLocation(time.RFC3339Nano, sdata, session.Engine.TZLocation)
  917. session.Engine.logger.Debugf("time(1) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  918. if err != nil {
  919. x, err = time.ParseInLocation("2006-01-02 15:04:05.999999999", sdata, session.Engine.TZLocation)
  920. session.Engine.logger.Debugf("time(2) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  921. }
  922. if err != nil {
  923. x, err = time.ParseInLocation("2006-01-02 15:04:05.9999999 Z07:00", sdata, session.Engine.TZLocation)
  924. session.Engine.logger.Debugf("time(3) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  925. }
  926. } else if len(sdata) == 19 && strings.Contains(sdata, "-") {
  927. x, err = time.ParseInLocation("2006-01-02 15:04:05", sdata, session.Engine.TZLocation)
  928. session.Engine.logger.Debugf("time(4) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  929. } else if len(sdata) == 10 && sdata[4] == '-' && sdata[7] == '-' {
  930. x, err = time.ParseInLocation("2006-01-02", sdata, session.Engine.TZLocation)
  931. session.Engine.logger.Debugf("time(5) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  932. } else if col.SQLType.Name == core.Time {
  933. if strings.Contains(sdata, " ") {
  934. ssd := strings.Split(sdata, " ")
  935. sdata = ssd[1]
  936. }
  937. sdata = strings.TrimSpace(sdata)
  938. if session.Engine.dialect.DBType() == core.MYSQL && len(sdata) > 8 {
  939. sdata = sdata[len(sdata)-8:]
  940. }
  941. st := fmt.Sprintf("2006-01-02 %v", sdata)
  942. x, err = time.ParseInLocation("2006-01-02 15:04:05", st, session.Engine.TZLocation)
  943. session.Engine.logger.Debugf("time(6) key[%v]: %+v | sdata: [%v]\n", col.FieldName, x, sdata)
  944. } else {
  945. outErr = fmt.Errorf("unsupported time format %v", sdata)
  946. return
  947. }
  948. if err != nil {
  949. outErr = fmt.Errorf("unsupported time format %v: %v", sdata, err)
  950. return
  951. }
  952. outTime = x
  953. return
  954. }
  955. func (session *Session) byte2Time(col *core.Column, data []byte) (outTime time.Time, outErr error) {
  956. return session.str2Time(col, string(data))
  957. }
  958. // convert a db data([]byte) to a field value
  959. func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value, data []byte) error {
  960. if structConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
  961. return structConvert.FromDB(data)
  962. }
  963. if structConvert, ok := fieldValue.Interface().(core.Conversion); ok {
  964. return structConvert.FromDB(data)
  965. }
  966. var v interface{}
  967. key := col.Name
  968. fieldType := fieldValue.Type()
  969. switch fieldType.Kind() {
  970. case reflect.Complex64, reflect.Complex128:
  971. x := reflect.New(fieldType)
  972. if len(data) > 0 {
  973. err := json.Unmarshal(data, x.Interface())
  974. if err != nil {
  975. session.Engine.logger.Error(err)
  976. return err
  977. }
  978. fieldValue.Set(x.Elem())
  979. }
  980. case reflect.Slice, reflect.Array, reflect.Map:
  981. v = data
  982. t := fieldType.Elem()
  983. k := t.Kind()
  984. if col.SQLType.IsText() {
  985. x := reflect.New(fieldType)
  986. if len(data) > 0 {
  987. err := json.Unmarshal(data, x.Interface())
  988. if err != nil {
  989. session.Engine.logger.Error(err)
  990. return err
  991. }
  992. fieldValue.Set(x.Elem())
  993. }
  994. } else if col.SQLType.IsBlob() {
  995. if k == reflect.Uint8 {
  996. fieldValue.Set(reflect.ValueOf(v))
  997. } else {
  998. x := reflect.New(fieldType)
  999. if len(data) > 0 {
  1000. err := json.Unmarshal(data, x.Interface())
  1001. if err != nil {
  1002. session.Engine.logger.Error(err)
  1003. return err
  1004. }
  1005. fieldValue.Set(x.Elem())
  1006. }
  1007. }
  1008. } else {
  1009. return ErrUnSupportedType
  1010. }
  1011. case reflect.String:
  1012. fieldValue.SetString(string(data))
  1013. case reflect.Bool:
  1014. d := string(data)
  1015. v, err := strconv.ParseBool(d)
  1016. if err != nil {
  1017. return fmt.Errorf("arg %v as bool: %s", key, err.Error())
  1018. }
  1019. fieldValue.Set(reflect.ValueOf(v))
  1020. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1021. sdata := string(data)
  1022. var x int64
  1023. var err error
  1024. // for mysql, when use bit, it returned \x01
  1025. if col.SQLType.Name == core.Bit &&
  1026. session.Engine.dialect.DBType() == core.MYSQL { // !nashtsai! TODO dialect needs to provide conversion interface API
  1027. if len(data) == 1 {
  1028. x = int64(data[0])
  1029. } else {
  1030. x = 0
  1031. }
  1032. } else if strings.HasPrefix(sdata, "0x") {
  1033. x, err = strconv.ParseInt(sdata, 16, 64)
  1034. } else if strings.HasPrefix(sdata, "0") {
  1035. x, err = strconv.ParseInt(sdata, 8, 64)
  1036. } else if strings.EqualFold(sdata, "true") {
  1037. x = 1
  1038. } else if strings.EqualFold(sdata, "false") {
  1039. x = 0
  1040. } else {
  1041. x, err = strconv.ParseInt(sdata, 10, 64)
  1042. }
  1043. if err != nil {
  1044. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1045. }
  1046. fieldValue.SetInt(x)
  1047. case reflect.Float32, reflect.Float64:
  1048. x, err := strconv.ParseFloat(string(data), 64)
  1049. if err != nil {
  1050. return fmt.Errorf("arg %v as float64: %s", key, err.Error())
  1051. }
  1052. fieldValue.SetFloat(x)
  1053. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  1054. x, err := strconv.ParseUint(string(data), 10, 64)
  1055. if err != nil {
  1056. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1057. }
  1058. fieldValue.SetUint(x)
  1059. //Currently only support Time type
  1060. case reflect.Struct:
  1061. // !<winxxp>! 增加支持sql.Scanner接口的结构,如sql.NullString
  1062. if nulVal, ok := fieldValue.Addr().Interface().(sql.Scanner); ok {
  1063. if err := nulVal.Scan(data); err != nil {
  1064. return fmt.Errorf("sql.Scan(%v) failed: %s ", data, err.Error())
  1065. }
  1066. } else {
  1067. if fieldType.ConvertibleTo(core.TimeType) {
  1068. x, err := session.byte2Time(col, data)
  1069. if err != nil {
  1070. return err
  1071. }
  1072. v = x
  1073. fieldValue.Set(reflect.ValueOf(v).Convert(fieldType))
  1074. } else if session.Statement.UseCascade {
  1075. table := session.Engine.autoMapType(*fieldValue)
  1076. if table != nil {
  1077. // TODO: current only support 1 primary key
  1078. if len(table.PrimaryKeys) > 1 {
  1079. panic("unsupported composited primary key cascade")
  1080. }
  1081. var pk = make(core.PK, len(table.PrimaryKeys))
  1082. rawValueType := table.ColumnType(table.PKColumns()[0].FieldName)
  1083. var err error
  1084. pk[0], err = str2PK(string(data), rawValueType)
  1085. if err != nil {
  1086. return err
  1087. }
  1088. if !isPKZero(pk) {
  1089. // !nashtsai! TODO for hasOne relationship, it's preferred to use join query for eager fetch
  1090. // however, also need to consider adding a 'lazy' attribute to xorm tag which allow hasOne
  1091. // property to be fetched lazily
  1092. structInter := reflect.New(fieldValue.Type())
  1093. newsession := session.Engine.NewSession()
  1094. defer newsession.Close()
  1095. has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface())
  1096. if err != nil {
  1097. return err
  1098. }
  1099. if has {
  1100. v = structInter.Elem().Interface()
  1101. fieldValue.Set(reflect.ValueOf(v))
  1102. } else {
  1103. return errors.New("cascade obj is not exist")
  1104. }
  1105. }
  1106. } else {
  1107. return fmt.Errorf("unsupported struct type in Scan: %s", fieldValue.Type().String())
  1108. }
  1109. }
  1110. }
  1111. case reflect.Ptr:
  1112. // !nashtsai! TODO merge duplicated codes above
  1113. //typeStr := fieldType.String()
  1114. switch fieldType.Elem().Kind() {
  1115. // case "*string":
  1116. case core.StringType.Kind():
  1117. x := string(data)
  1118. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1119. // case "*bool":
  1120. case core.BoolType.Kind():
  1121. d := string(data)
  1122. v, err := strconv.ParseBool(d)
  1123. if err != nil {
  1124. return fmt.Errorf("arg %v as bool: %s", key, err.Error())
  1125. }
  1126. fieldValue.Set(reflect.ValueOf(&v).Convert(fieldType))
  1127. // case "*complex64":
  1128. case core.Complex64Type.Kind():
  1129. var x complex64
  1130. if len(data) > 0 {
  1131. err := json.Unmarshal(data, &x)
  1132. if err != nil {
  1133. session.Engine.logger.Error(err)
  1134. return err
  1135. }
  1136. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1137. }
  1138. // case "*complex128":
  1139. case core.Complex128Type.Kind():
  1140. var x complex128
  1141. if len(data) > 0 {
  1142. err := json.Unmarshal(data, &x)
  1143. if err != nil {
  1144. session.Engine.logger.Error(err)
  1145. return err
  1146. }
  1147. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1148. }
  1149. // case "*float64":
  1150. case core.Float64Type.Kind():
  1151. x, err := strconv.ParseFloat(string(data), 64)
  1152. if err != nil {
  1153. return fmt.Errorf("arg %v as float64: %s", key, err.Error())
  1154. }
  1155. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1156. // case "*float32":
  1157. case core.Float32Type.Kind():
  1158. var x float32
  1159. x1, err := strconv.ParseFloat(string(data), 32)
  1160. if err != nil {
  1161. return fmt.Errorf("arg %v as float32: %s", key, err.Error())
  1162. }
  1163. x = float32(x1)
  1164. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1165. // case "*uint64":
  1166. case core.Uint64Type.Kind():
  1167. var x uint64
  1168. x, err := strconv.ParseUint(string(data), 10, 64)
  1169. if err != nil {
  1170. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1171. }
  1172. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1173. // case "*uint":
  1174. case core.UintType.Kind():
  1175. var x uint
  1176. x1, err := strconv.ParseUint(string(data), 10, 64)
  1177. if err != nil {
  1178. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1179. }
  1180. x = uint(x1)
  1181. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1182. // case "*uint32":
  1183. case core.Uint32Type.Kind():
  1184. var x uint32
  1185. x1, err := strconv.ParseUint(string(data), 10, 64)
  1186. if err != nil {
  1187. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1188. }
  1189. x = uint32(x1)
  1190. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1191. // case "*uint8":
  1192. case core.Uint8Type.Kind():
  1193. var x uint8
  1194. x1, err := strconv.ParseUint(string(data), 10, 64)
  1195. if err != nil {
  1196. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1197. }
  1198. x = uint8(x1)
  1199. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1200. // case "*uint16":
  1201. case core.Uint16Type.Kind():
  1202. var x uint16
  1203. x1, err := strconv.ParseUint(string(data), 10, 64)
  1204. if err != nil {
  1205. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1206. }
  1207. x = uint16(x1)
  1208. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1209. // case "*int64":
  1210. case core.Int64Type.Kind():
  1211. sdata := string(data)
  1212. var x int64
  1213. var err error
  1214. // for mysql, when use bit, it returned \x01
  1215. if col.SQLType.Name == core.Bit &&
  1216. strings.Contains(session.Engine.DriverName(), "mysql") {
  1217. if len(data) == 1 {
  1218. x = int64(data[0])
  1219. } else {
  1220. x = 0
  1221. }
  1222. } else if strings.HasPrefix(sdata, "0x") {
  1223. x, err = strconv.ParseInt(sdata, 16, 64)
  1224. } else if strings.HasPrefix(sdata, "0") {
  1225. x, err = strconv.ParseInt(sdata, 8, 64)
  1226. } else {
  1227. x, err = strconv.ParseInt(sdata, 10, 64)
  1228. }
  1229. if err != nil {
  1230. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1231. }
  1232. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1233. // case "*int":
  1234. case core.IntType.Kind():
  1235. sdata := string(data)
  1236. var x int
  1237. var x1 int64
  1238. var err error
  1239. // for mysql, when use bit, it returned \x01
  1240. if col.SQLType.Name == core.Bit &&
  1241. strings.Contains(session.Engine.DriverName(), "mysql") {
  1242. if len(data) == 1 {
  1243. x = int(data[0])
  1244. } else {
  1245. x = 0
  1246. }
  1247. } else if strings.HasPrefix(sdata, "0x") {
  1248. x1, err = strconv.ParseInt(sdata, 16, 64)
  1249. x = int(x1)
  1250. } else if strings.HasPrefix(sdata, "0") {
  1251. x1, err = strconv.ParseInt(sdata, 8, 64)
  1252. x = int(x1)
  1253. } else {
  1254. x1, err = strconv.ParseInt(sdata, 10, 64)
  1255. x = int(x1)
  1256. }
  1257. if err != nil {
  1258. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1259. }
  1260. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1261. // case "*int32":
  1262. case core.Int32Type.Kind():
  1263. sdata := string(data)
  1264. var x int32
  1265. var x1 int64
  1266. var err error
  1267. // for mysql, when use bit, it returned \x01
  1268. if col.SQLType.Name == core.Bit &&
  1269. session.Engine.dialect.DBType() == core.MYSQL {
  1270. if len(data) == 1 {
  1271. x = int32(data[0])
  1272. } else {
  1273. x = 0
  1274. }
  1275. } else if strings.HasPrefix(sdata, "0x") {
  1276. x1, err = strconv.ParseInt(sdata, 16, 64)
  1277. x = int32(x1)
  1278. } else if strings.HasPrefix(sdata, "0") {
  1279. x1, err = strconv.ParseInt(sdata, 8, 64)
  1280. x = int32(x1)
  1281. } else {
  1282. x1, err = strconv.ParseInt(sdata, 10, 64)
  1283. x = int32(x1)
  1284. }
  1285. if err != nil {
  1286. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1287. }
  1288. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1289. // case "*int8":
  1290. case core.Int8Type.Kind():
  1291. sdata := string(data)
  1292. var x int8
  1293. var x1 int64
  1294. var err error
  1295. // for mysql, when use bit, it returned \x01
  1296. if col.SQLType.Name == core.Bit &&
  1297. strings.Contains(session.Engine.DriverName(), "mysql") {
  1298. if len(data) == 1 {
  1299. x = int8(data[0])
  1300. } else {
  1301. x = 0
  1302. }
  1303. } else if strings.HasPrefix(sdata, "0x") {
  1304. x1, err = strconv.ParseInt(sdata, 16, 64)
  1305. x = int8(x1)
  1306. } else if strings.HasPrefix(sdata, "0") {
  1307. x1, err = strconv.ParseInt(sdata, 8, 64)
  1308. x = int8(x1)
  1309. } else {
  1310. x1, err = strconv.ParseInt(sdata, 10, 64)
  1311. x = int8(x1)
  1312. }
  1313. if err != nil {
  1314. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1315. }
  1316. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1317. // case "*int16":
  1318. case core.Int16Type.Kind():
  1319. sdata := string(data)
  1320. var x int16
  1321. var x1 int64
  1322. var err error
  1323. // for mysql, when use bit, it returned \x01
  1324. if col.SQLType.Name == core.Bit &&
  1325. strings.Contains(session.Engine.DriverName(), "mysql") {
  1326. if len(data) == 1 {
  1327. x = int16(data[0])
  1328. } else {
  1329. x = 0
  1330. }
  1331. } else if strings.HasPrefix(sdata, "0x") {
  1332. x1, err = strconv.ParseInt(sdata, 16, 64)
  1333. x = int16(x1)
  1334. } else if strings.HasPrefix(sdata, "0") {
  1335. x1, err = strconv.ParseInt(sdata, 8, 64)
  1336. x = int16(x1)
  1337. } else {
  1338. x1, err = strconv.ParseInt(sdata, 10, 64)
  1339. x = int16(x1)
  1340. }
  1341. if err != nil {
  1342. return fmt.Errorf("arg %v as int: %s", key, err.Error())
  1343. }
  1344. fieldValue.Set(reflect.ValueOf(&x).Convert(fieldType))
  1345. // case "*SomeStruct":
  1346. case reflect.Struct:
  1347. switch fieldType {
  1348. // case "*.time.Time":
  1349. case core.PtrTimeType:
  1350. x, err := session.byte2Time(col, data)
  1351. if err != nil {
  1352. return err
  1353. }
  1354. v = x
  1355. fieldValue.Set(reflect.ValueOf(&x))
  1356. default:
  1357. if session.Statement.UseCascade {
  1358. structInter := reflect.New(fieldType.Elem())
  1359. table := session.Engine.autoMapType(structInter.Elem())
  1360. if table != nil {
  1361. if len(table.PrimaryKeys) > 1 {
  1362. panic("unsupported composited primary key cascade")
  1363. }
  1364. var pk = make(core.PK, len(table.PrimaryKeys))
  1365. var err error
  1366. rawValueType := table.ColumnType(table.PKColumns()[0].FieldName)
  1367. pk[0], err = str2PK(string(data), rawValueType)
  1368. if err != nil {
  1369. return err
  1370. }
  1371. if !isPKZero(pk) {
  1372. // !nashtsai! TODO for hasOne relationship, it's preferred to use join query for eager fetch
  1373. // however, also need to consider adding a 'lazy' attribute to xorm tag which allow hasOne
  1374. // property to be fetched lazily
  1375. newsession := session.Engine.NewSession()
  1376. defer newsession.Close()
  1377. has, err := newsession.Id(pk).NoCascade().Get(structInter.Interface())
  1378. if err != nil {
  1379. return err
  1380. }
  1381. if has {
  1382. v = structInter.Interface()
  1383. fieldValue.Set(reflect.ValueOf(v))
  1384. } else {
  1385. return errors.New("cascade obj is not exist")
  1386. }
  1387. }
  1388. }
  1389. } else {
  1390. return fmt.Errorf("unsupported struct type in Scan: %s", fieldValue.Type().String())
  1391. }
  1392. }
  1393. default:
  1394. return fmt.Errorf("unsupported type in Scan: %s", fieldValue.Type().String())
  1395. }
  1396. default:
  1397. return fmt.Errorf("unsupported type in Scan: %s", fieldValue.Type().String())
  1398. }
  1399. return nil
  1400. }
  1401. // convert a field value of a struct to interface for put into db
  1402. func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Value) (interface{}, error) {
  1403. if fieldValue.CanAddr() {
  1404. if fieldConvert, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
  1405. data, err := fieldConvert.ToDB()
  1406. if err != nil {
  1407. return 0, err
  1408. }
  1409. if col.SQLType.IsBlob() {
  1410. return data, nil
  1411. }
  1412. return string(data), nil
  1413. }
  1414. }
  1415. if fieldConvert, ok := fieldValue.Interface().(core.Conversion); ok {
  1416. data, err := fieldConvert.ToDB()
  1417. if err != nil {
  1418. return 0, err
  1419. }
  1420. if col.SQLType.IsBlob() {
  1421. return data, nil
  1422. }
  1423. return string(data), nil
  1424. }
  1425. fieldType := fieldValue.Type()
  1426. k := fieldType.Kind()
  1427. if k == reflect.Ptr {
  1428. if fieldValue.IsNil() {
  1429. return nil, nil
  1430. } else if !fieldValue.IsValid() {
  1431. session.Engine.logger.Warn("the field[", col.FieldName, "] is invalid")
  1432. return nil, nil
  1433. } else {
  1434. // !nashtsai! deference pointer type to instance type
  1435. fieldValue = fieldValue.Elem()
  1436. fieldType = fieldValue.Type()
  1437. k = fieldType.Kind()
  1438. }
  1439. }
  1440. switch k {
  1441. case reflect.Bool:
  1442. return fieldValue.Bool(), nil
  1443. case reflect.String:
  1444. return fieldValue.String(), nil
  1445. case reflect.Struct:
  1446. if fieldType.ConvertibleTo(core.TimeType) {
  1447. t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
  1448. if session.Engine.dialect.DBType() == core.MSSQL {
  1449. if t.IsZero() {
  1450. return nil, nil
  1451. }
  1452. }
  1453. tf := session.Engine.FormatTime(col.SQLType.Name, t)
  1454. return tf, nil
  1455. }
  1456. if !col.SQLType.IsJson() {
  1457. // !<winxxp>! 增加支持driver.Valuer接口的结构,如sql.NullString
  1458. if v, ok := fieldValue.Interface().(driver.Valuer); ok {
  1459. return v.Value()
  1460. }
  1461. fieldTable := session.Engine.autoMapType(fieldValue)
  1462. if len(fieldTable.PrimaryKeys) == 1 {
  1463. pkField := reflect.Indirect(fieldValue).FieldByName(fieldTable.PKColumns()[0].FieldName)
  1464. return pkField.Interface(), nil
  1465. }
  1466. return 0, fmt.Errorf("no primary key for col %v", col.Name)
  1467. }
  1468. if col.SQLType.IsText() {
  1469. bytes, err := json.Marshal(fieldValue.Interface())
  1470. if err != nil {
  1471. session.Engine.logger.Error(err)
  1472. return 0, err
  1473. }
  1474. return string(bytes), nil
  1475. } else if col.SQLType.IsBlob() {
  1476. bytes, err := json.Marshal(fieldValue.Interface())
  1477. if err != nil {
  1478. session.Engine.logger.Error(err)
  1479. return 0, err
  1480. }
  1481. return bytes, nil
  1482. }
  1483. return nil, fmt.Errorf("Unsupported type %v", fieldValue.Type())
  1484. case reflect.Complex64, reflect.Complex128:
  1485. bytes, err := json.Marshal(fieldValue.Interface())
  1486. if err != nil {
  1487. session.Engine.logger.Error(err)
  1488. return 0, err
  1489. }
  1490. return string(bytes), nil
  1491. case reflect.Array, reflect.Slice, reflect.Map:
  1492. if !fieldValue.IsValid() {
  1493. return fieldValue.Interface(), nil
  1494. }
  1495. if col.SQLType.IsText() {
  1496. bytes, err := json.Marshal(fieldValue.Interface())
  1497. if err != nil {
  1498. session.Engine.logger.Error(err)
  1499. return 0, err
  1500. }
  1501. return string(bytes), nil
  1502. } else if col.SQLType.IsBlob() {
  1503. var bytes []byte
  1504. var err error
  1505. if (k == reflect.Array || k == reflect.Slice) &&
  1506. (fieldValue.Type().Elem().Kind() == reflect.Uint8) {
  1507. bytes = fieldValue.Bytes()
  1508. } else {
  1509. bytes, err = json.Marshal(fieldValue.Interface())
  1510. if err != nil {
  1511. session.Engine.logger.Error(err)
  1512. return 0, err
  1513. }
  1514. }
  1515. return bytes, nil
  1516. }
  1517. return nil, ErrUnSupportedType
  1518. case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
  1519. return int64(fieldValue.Uint()), nil
  1520. default:
  1521. return fieldValue.Interface(), nil
  1522. }
  1523. }
  1524. // saveLastSQL stores executed query information
  1525. func (session *Session) saveLastSQL(sql string, args ...interface{}) {
  1526. session.lastSQL = sql
  1527. session.lastSQLArgs = args
  1528. session.Engine.logSQL(sql, args...)
  1529. }
  1530. // LastSQL returns last query information
  1531. func (session *Session) LastSQL() (string, []interface{}) {
  1532. return session.lastSQL, session.lastSQLArgs
  1533. }
  1534. // tbName get some table's table name
  1535. func (session *Session) tbNameNoSchema(table *core.Table) string {
  1536. if len(session.Statement.AltTableName) > 0 {
  1537. return session.Statement.AltTableName
  1538. }
  1539. return table.Name
  1540. }
  1541. // Unscoped always disable struct tag "deleted"
  1542. func (session *Session) Unscoped() *Session {
  1543. session.Statement.Unscoped()
  1544. return session
  1545. }