Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle https://xorm.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1340 lines
38 KiB

  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. "context"
  7. "database/sql"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "os"
  12. "reflect"
  13. "runtime"
  14. "strconv"
  15. "strings"
  16. "time"
  17. "xorm.io/xorm/caches"
  18. "xorm.io/xorm/core"
  19. "xorm.io/xorm/dialects"
  20. "xorm.io/xorm/internal/utils"
  21. "xorm.io/xorm/log"
  22. "xorm.io/xorm/names"
  23. "xorm.io/xorm/schemas"
  24. "xorm.io/xorm/tags"
  25. )
  26. // Engine is the major struct of xorm, it means a database manager.
  27. // Commonly, an application only need one engine
  28. type Engine struct {
  29. cacherMgr *caches.Manager
  30. defaultContext context.Context
  31. dialect dialects.Dialect
  32. engineGroup *EngineGroup
  33. logger log.ContextLogger
  34. tagParser *tags.Parser
  35. db *core.DB
  36. driverName string
  37. dataSourceName string
  38. TZLocation *time.Location // The timezone of the application
  39. DatabaseTZ *time.Location // The timezone of the database
  40. logSessionID bool // create session id
  41. }
  42. // NewEngine new a db manager according to the parameter. Currently support four
  43. // drivers
  44. func NewEngine(driverName string, dataSourceName string) (*Engine, error) {
  45. dialect, err := dialects.OpenDialect(driverName, dataSourceName)
  46. if err != nil {
  47. return nil, err
  48. }
  49. db, err := core.Open(driverName, dataSourceName)
  50. if err != nil {
  51. return nil, err
  52. }
  53. cacherMgr := caches.NewManager()
  54. mapper := names.NewCacheMapper(new(names.SnakeMapper))
  55. tagParser := tags.NewParser("xorm", dialect, mapper, mapper, cacherMgr)
  56. engine := &Engine{
  57. dialect: dialect,
  58. TZLocation: time.Local,
  59. defaultContext: context.Background(),
  60. cacherMgr: cacherMgr,
  61. tagParser: tagParser,
  62. driverName: driverName,
  63. dataSourceName: dataSourceName,
  64. db: db,
  65. logSessionID: false,
  66. }
  67. if dialect.URI().DBType == schemas.SQLITE {
  68. engine.DatabaseTZ = time.UTC
  69. } else {
  70. engine.DatabaseTZ = time.Local
  71. }
  72. logger := log.NewSimpleLogger(os.Stdout)
  73. logger.SetLevel(log.LOG_INFO)
  74. engine.SetLogger(log.NewLoggerAdapter(logger))
  75. runtime.SetFinalizer(engine, func(engine *Engine) {
  76. engine.Close()
  77. })
  78. return engine, nil
  79. }
  80. // NewEngineWithParams new a db manager with params. The params will be passed to dialects.
  81. func NewEngineWithParams(driverName string, dataSourceName string, params map[string]string) (*Engine, error) {
  82. engine, err := NewEngine(driverName, dataSourceName)
  83. engine.dialect.SetParams(params)
  84. return engine, err
  85. }
  86. // EnableSessionID if enable session id
  87. func (engine *Engine) EnableSessionID(enable bool) {
  88. engine.logSessionID = enable
  89. }
  90. // SetCacher sets cacher for the table
  91. func (engine *Engine) SetCacher(tableName string, cacher caches.Cacher) {
  92. engine.cacherMgr.SetCacher(tableName, cacher)
  93. }
  94. // GetCacher returns the cachher of the special table
  95. func (engine *Engine) GetCacher(tableName string) caches.Cacher {
  96. return engine.cacherMgr.GetCacher(tableName)
  97. }
  98. // SetQuotePolicy sets the special quote policy
  99. func (engine *Engine) SetQuotePolicy(quotePolicy dialects.QuotePolicy) {
  100. engine.dialect.SetQuotePolicy(quotePolicy)
  101. }
  102. // BufferSize sets buffer size for iterate
  103. func (engine *Engine) BufferSize(size int) *Session {
  104. session := engine.NewSession()
  105. session.isAutoClose = true
  106. return session.BufferSize(size)
  107. }
  108. // ShowSQL show SQL statement or not on logger if log level is great than INFO
  109. func (engine *Engine) ShowSQL(show ...bool) {
  110. engine.logger.ShowSQL(show...)
  111. engine.DB().Logger = engine.logger
  112. }
  113. // Logger return the logger interface
  114. func (engine *Engine) Logger() log.ContextLogger {
  115. return engine.logger
  116. }
  117. // SetLogger set the new logger
  118. func (engine *Engine) SetLogger(logger interface{}) {
  119. var realLogger log.ContextLogger
  120. switch t := logger.(type) {
  121. case log.Logger:
  122. realLogger = log.NewLoggerAdapter(t)
  123. case log.ContextLogger:
  124. realLogger = t
  125. }
  126. engine.logger = realLogger
  127. engine.DB().Logger = realLogger
  128. }
  129. // SetLogLevel sets the logger level
  130. func (engine *Engine) SetLogLevel(level log.LogLevel) {
  131. engine.logger.SetLevel(level)
  132. }
  133. // SetDisableGlobalCache disable global cache or not
  134. func (engine *Engine) SetDisableGlobalCache(disable bool) {
  135. engine.cacherMgr.SetDisableGlobalCache(disable)
  136. }
  137. // DriverName return the current sql driver's name
  138. func (engine *Engine) DriverName() string {
  139. return engine.driverName
  140. }
  141. // DataSourceName return the current connection string
  142. func (engine *Engine) DataSourceName() string {
  143. return engine.dataSourceName
  144. }
  145. // SetMapper set the name mapping rules
  146. func (engine *Engine) SetMapper(mapper names.Mapper) {
  147. engine.SetTableMapper(mapper)
  148. engine.SetColumnMapper(mapper)
  149. }
  150. // SetTableMapper set the table name mapping rule
  151. func (engine *Engine) SetTableMapper(mapper names.Mapper) {
  152. engine.tagParser.SetTableMapper(mapper)
  153. }
  154. // SetColumnMapper set the column name mapping rule
  155. func (engine *Engine) SetColumnMapper(mapper names.Mapper) {
  156. engine.tagParser.SetColumnMapper(mapper)
  157. }
  158. // Quote Use QuoteStr quote the string sql
  159. func (engine *Engine) Quote(value string) string {
  160. value = strings.TrimSpace(value)
  161. if len(value) == 0 {
  162. return value
  163. }
  164. buf := strings.Builder{}
  165. engine.QuoteTo(&buf, value)
  166. return buf.String()
  167. }
  168. // QuoteTo quotes string and writes into the buffer
  169. func (engine *Engine) QuoteTo(buf *strings.Builder, value string) {
  170. if buf == nil {
  171. return
  172. }
  173. value = strings.TrimSpace(value)
  174. if value == "" {
  175. return
  176. }
  177. engine.dialect.Quoter().QuoteTo(buf, value)
  178. }
  179. // SQLType A simple wrapper to dialect's core.SqlType method
  180. func (engine *Engine) SQLType(c *schemas.Column) string {
  181. return engine.dialect.SQLType(c)
  182. }
  183. // AutoIncrStr Database's autoincrement statement
  184. func (engine *Engine) AutoIncrStr() string {
  185. return engine.dialect.AutoIncrStr()
  186. }
  187. // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
  188. func (engine *Engine) SetConnMaxLifetime(d time.Duration) {
  189. engine.DB().SetConnMaxLifetime(d)
  190. }
  191. // SetMaxOpenConns is only available for go 1.2+
  192. func (engine *Engine) SetMaxOpenConns(conns int) {
  193. engine.DB().SetMaxOpenConns(conns)
  194. }
  195. // SetMaxIdleConns set the max idle connections on pool, default is 2
  196. func (engine *Engine) SetMaxIdleConns(conns int) {
  197. engine.DB().SetMaxIdleConns(conns)
  198. }
  199. // SetDefaultCacher set the default cacher. Xorm's default not enable cacher.
  200. func (engine *Engine) SetDefaultCacher(cacher caches.Cacher) {
  201. engine.cacherMgr.SetDefaultCacher(cacher)
  202. }
  203. // GetDefaultCacher returns the default cacher
  204. func (engine *Engine) GetDefaultCacher() caches.Cacher {
  205. return engine.cacherMgr.GetDefaultCacher()
  206. }
  207. // NoCache If you has set default cacher, and you want temporilly stop use cache,
  208. // you can use NoCache()
  209. func (engine *Engine) NoCache() *Session {
  210. session := engine.NewSession()
  211. session.isAutoClose = true
  212. return session.NoCache()
  213. }
  214. // NoCascade If you do not want to auto cascade load object
  215. func (engine *Engine) NoCascade() *Session {
  216. session := engine.NewSession()
  217. session.isAutoClose = true
  218. return session.NoCascade()
  219. }
  220. // MapCacher Set a table use a special cacher
  221. func (engine *Engine) MapCacher(bean interface{}, cacher caches.Cacher) error {
  222. engine.SetCacher(dialects.FullTableName(engine.dialect, engine.GetTableMapper(), bean, true), cacher)
  223. return nil
  224. }
  225. // NewDB provides an interface to operate database directly
  226. func (engine *Engine) NewDB() (*core.DB, error) {
  227. return core.Open(engine.driverName, engine.dataSourceName)
  228. }
  229. // DB return the wrapper of sql.DB
  230. func (engine *Engine) DB() *core.DB {
  231. return engine.db
  232. }
  233. // Dialect return database dialect
  234. func (engine *Engine) Dialect() dialects.Dialect {
  235. return engine.dialect
  236. }
  237. // NewSession New a session
  238. func (engine *Engine) NewSession() *Session {
  239. return newSession(engine)
  240. }
  241. // Close the engine
  242. func (engine *Engine) Close() error {
  243. return engine.DB().Close()
  244. }
  245. // Ping tests if database is alive
  246. func (engine *Engine) Ping() error {
  247. session := engine.NewSession()
  248. defer session.Close()
  249. return session.Ping()
  250. }
  251. // SQL method let's you manually write raw SQL and operate
  252. // For example:
  253. //
  254. // engine.SQL("select * from user").Find(&users)
  255. //
  256. // This code will execute "select * from user" and set the records to users
  257. func (engine *Engine) SQL(query interface{}, args ...interface{}) *Session {
  258. session := engine.NewSession()
  259. session.isAutoClose = true
  260. return session.SQL(query, args...)
  261. }
  262. // NoAutoTime Default if your struct has "created" or "updated" filed tag, the fields
  263. // will automatically be filled with current time when Insert or Update
  264. // invoked. Call NoAutoTime if you dont' want to fill automatically.
  265. func (engine *Engine) NoAutoTime() *Session {
  266. session := engine.NewSession()
  267. session.isAutoClose = true
  268. return session.NoAutoTime()
  269. }
  270. // NoAutoCondition disable auto generate Where condition from bean or not
  271. func (engine *Engine) NoAutoCondition(no ...bool) *Session {
  272. session := engine.NewSession()
  273. session.isAutoClose = true
  274. return session.NoAutoCondition(no...)
  275. }
  276. func (engine *Engine) loadTableInfo(table *schemas.Table) error {
  277. colSeq, cols, err := engine.dialect.GetColumns(engine.db, engine.defaultContext, table.Name)
  278. if err != nil {
  279. return err
  280. }
  281. for _, name := range colSeq {
  282. table.AddColumn(cols[name])
  283. }
  284. indexes, err := engine.dialect.GetIndexes(engine.db, engine.defaultContext, table.Name)
  285. if err != nil {
  286. return err
  287. }
  288. table.Indexes = indexes
  289. var seq int
  290. for _, index := range indexes {
  291. for _, name := range index.Cols {
  292. parts := strings.Split(name, " ")
  293. if len(parts) > 1 {
  294. if parts[1] == "DESC" {
  295. seq = 1
  296. }
  297. }
  298. if col := table.GetColumn(parts[0]); col != nil {
  299. col.Indexes[index.Name] = index.Type
  300. } else {
  301. return fmt.Errorf("Unknown col %s seq %d, in index %v of table %v, columns %v", name, seq, index.Name, table.Name, table.ColumnsSeq())
  302. }
  303. }
  304. }
  305. return nil
  306. }
  307. // DBMetas Retrieve all tables, columns, indexes' informations from database.
  308. func (engine *Engine) DBMetas() ([]*schemas.Table, error) {
  309. tables, err := engine.dialect.GetTables(engine.db, engine.defaultContext)
  310. if err != nil {
  311. return nil, err
  312. }
  313. for _, table := range tables {
  314. if err = engine.loadTableInfo(table); err != nil {
  315. return nil, err
  316. }
  317. }
  318. return tables, nil
  319. }
  320. // DumpAllToFile dump database all table structs and data to a file
  321. func (engine *Engine) DumpAllToFile(fp string, tp ...schemas.DBType) error {
  322. f, err := os.Create(fp)
  323. if err != nil {
  324. return err
  325. }
  326. defer f.Close()
  327. return engine.DumpAll(f, tp...)
  328. }
  329. // DumpAll dump database all table structs and data to w
  330. func (engine *Engine) DumpAll(w io.Writer, tp ...schemas.DBType) error {
  331. tables, err := engine.DBMetas()
  332. if err != nil {
  333. return err
  334. }
  335. return engine.DumpTables(tables, w, tp...)
  336. }
  337. // DumpTablesToFile dump specified tables to SQL file.
  338. func (engine *Engine) DumpTablesToFile(tables []*schemas.Table, fp string, tp ...schemas.DBType) error {
  339. f, err := os.Create(fp)
  340. if err != nil {
  341. return err
  342. }
  343. defer f.Close()
  344. return engine.DumpTables(tables, f, tp...)
  345. }
  346. // DumpTables dump specify tables to io.Writer
  347. func (engine *Engine) DumpTables(tables []*schemas.Table, w io.Writer, tp ...schemas.DBType) error {
  348. return engine.dumpTables(tables, w, tp...)
  349. }
  350. // dumpTables dump database all table structs and data to w with specify db type
  351. func (engine *Engine) dumpTables(tables []*schemas.Table, w io.Writer, tp ...schemas.DBType) error {
  352. var dstDialect dialects.Dialect
  353. if len(tp) == 0 {
  354. dstDialect = engine.dialect
  355. } else {
  356. dstDialect = dialects.QueryDialect(tp[0])
  357. if dstDialect == nil {
  358. return errors.New("Unsupported database type")
  359. }
  360. uri := engine.dialect.URI()
  361. destURI := *uri
  362. dstDialect.Init(&destURI)
  363. }
  364. _, err := io.WriteString(w, fmt.Sprintf("/*Generated by xorm %s, from %s to %s*/\n\n",
  365. time.Now().In(engine.TZLocation).Format("2006-01-02 15:04:05"), engine.dialect.URI().DBType, dstDialect.URI().DBType))
  366. if err != nil {
  367. return err
  368. }
  369. for i, table := range tables {
  370. tableName := table.Name
  371. if dstDialect.URI().Schema != "" {
  372. tableName = fmt.Sprintf("%s.%s", dstDialect.URI().Schema, table.Name)
  373. }
  374. originalTableName := table.Name
  375. if engine.dialect.URI().Schema != "" {
  376. originalTableName = fmt.Sprintf("%s.%s", engine.dialect.URI().Schema, table.Name)
  377. }
  378. if i > 0 {
  379. _, err = io.WriteString(w, "\n")
  380. if err != nil {
  381. return err
  382. }
  383. }
  384. sqls, _ := dstDialect.CreateTableSQL(table, tableName)
  385. for _, s := range sqls {
  386. _, err = io.WriteString(w, s+";\n")
  387. if err != nil {
  388. return err
  389. }
  390. }
  391. if len(table.PKColumns()) > 0 && dstDialect.URI().DBType == schemas.MSSQL {
  392. fmt.Fprintf(w, "SET IDENTITY_INSERT [%s] ON;\n", table.Name)
  393. }
  394. for _, index := range table.Indexes {
  395. _, err = io.WriteString(w, dstDialect.CreateIndexSQL(table.Name, index)+";\n")
  396. if err != nil {
  397. return err
  398. }
  399. }
  400. cols := table.ColumnsSeq()
  401. colNames := engine.dialect.Quoter().Join(cols, ", ")
  402. destColNames := dstDialect.Quoter().Join(cols, ", ")
  403. rows, err := engine.DB().QueryContext(engine.defaultContext, "SELECT "+colNames+" FROM "+engine.Quote(originalTableName))
  404. if err != nil {
  405. return err
  406. }
  407. defer rows.Close()
  408. for rows.Next() {
  409. dest := make([]interface{}, len(cols))
  410. err = rows.ScanSlice(&dest)
  411. if err != nil {
  412. return err
  413. }
  414. _, err = io.WriteString(w, "INSERT INTO "+dstDialect.Quoter().Quote(tableName)+" ("+destColNames+") VALUES (")
  415. if err != nil {
  416. return err
  417. }
  418. var temp string
  419. for i, d := range dest {
  420. col := table.GetColumn(cols[i])
  421. if col == nil {
  422. return errors.New("unknow column error")
  423. }
  424. if d == nil {
  425. temp += ", NULL"
  426. } else if col.SQLType.IsText() || col.SQLType.IsTime() {
  427. var v = fmt.Sprintf("%s", d)
  428. if strings.HasSuffix(v, " +0000 UTC") {
  429. temp += fmt.Sprintf(", '%s'", v[0:len(v)-len(" +0000 UTC")])
  430. } else {
  431. temp += ", '" + strings.Replace(v, "'", "''", -1) + "'"
  432. }
  433. } else if col.SQLType.IsBlob() {
  434. if reflect.TypeOf(d).Kind() == reflect.Slice {
  435. temp += fmt.Sprintf(", %s", dstDialect.FormatBytes(d.([]byte)))
  436. } else if reflect.TypeOf(d).Kind() == reflect.String {
  437. temp += fmt.Sprintf(", '%s'", d.(string))
  438. }
  439. } else if col.SQLType.IsNumeric() {
  440. switch reflect.TypeOf(d).Kind() {
  441. case reflect.Slice:
  442. if col.SQLType.Name == schemas.Bool {
  443. temp += fmt.Sprintf(", %v", strconv.FormatBool(d.([]byte)[0] != byte('0')))
  444. } else {
  445. temp += fmt.Sprintf(", %s", string(d.([]byte)))
  446. }
  447. case reflect.Int16, reflect.Int8, reflect.Int32, reflect.Int64, reflect.Int:
  448. if col.SQLType.Name == schemas.Bool {
  449. temp += fmt.Sprintf(", %v", strconv.FormatBool(reflect.ValueOf(d).Int() > 0))
  450. } else {
  451. temp += fmt.Sprintf(", %v", d)
  452. }
  453. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  454. if col.SQLType.Name == schemas.Bool {
  455. temp += fmt.Sprintf(", %v", strconv.FormatBool(reflect.ValueOf(d).Uint() > 0))
  456. } else {
  457. temp += fmt.Sprintf(", %v", d)
  458. }
  459. default:
  460. temp += fmt.Sprintf(", %v", d)
  461. }
  462. } else {
  463. s := fmt.Sprintf("%v", d)
  464. if strings.Contains(s, ":") || strings.Contains(s, "-") {
  465. if strings.HasSuffix(s, " +0000 UTC") {
  466. temp += fmt.Sprintf(", '%s'", s[0:len(s)-len(" +0000 UTC")])
  467. } else {
  468. temp += fmt.Sprintf(", '%s'", s)
  469. }
  470. } else {
  471. temp += fmt.Sprintf(", %s", s)
  472. }
  473. }
  474. }
  475. _, err = io.WriteString(w, temp[2:]+");\n")
  476. if err != nil {
  477. return err
  478. }
  479. }
  480. // FIXME: Hack for postgres
  481. if dstDialect.URI().DBType == schemas.POSTGRES && table.AutoIncrColumn() != nil {
  482. _, err = io.WriteString(w, "SELECT setval('"+tableName+"_id_seq', COALESCE((SELECT MAX("+table.AutoIncrColumn().Name+") + 1 FROM "+dstDialect.Quoter().Quote(tableName)+"), 1), false);\n")
  483. if err != nil {
  484. return err
  485. }
  486. }
  487. }
  488. return nil
  489. }
  490. // Cascade use cascade or not
  491. func (engine *Engine) Cascade(trueOrFalse ...bool) *Session {
  492. session := engine.NewSession()
  493. session.isAutoClose = true
  494. return session.Cascade(trueOrFalse...)
  495. }
  496. // Where method provide a condition query
  497. func (engine *Engine) Where(query interface{}, args ...interface{}) *Session {
  498. session := engine.NewSession()
  499. session.isAutoClose = true
  500. return session.Where(query, args...)
  501. }
  502. // ID method provoide a condition as (id) = ?
  503. func (engine *Engine) ID(id interface{}) *Session {
  504. session := engine.NewSession()
  505. session.isAutoClose = true
  506. return session.ID(id)
  507. }
  508. // Before apply before Processor, affected bean is passed to closure arg
  509. func (engine *Engine) Before(closures func(interface{})) *Session {
  510. session := engine.NewSession()
  511. session.isAutoClose = true
  512. return session.Before(closures)
  513. }
  514. // After apply after insert Processor, affected bean is passed to closure arg
  515. func (engine *Engine) After(closures func(interface{})) *Session {
  516. session := engine.NewSession()
  517. session.isAutoClose = true
  518. return session.After(closures)
  519. }
  520. // Charset set charset when create table, only support mysql now
  521. func (engine *Engine) Charset(charset string) *Session {
  522. session := engine.NewSession()
  523. session.isAutoClose = true
  524. return session.Charset(charset)
  525. }
  526. // StoreEngine set store engine when create table, only support mysql now
  527. func (engine *Engine) StoreEngine(storeEngine string) *Session {
  528. session := engine.NewSession()
  529. session.isAutoClose = true
  530. return session.StoreEngine(storeEngine)
  531. }
  532. // Distinct use for distinct columns. Caution: when you are using cache,
  533. // distinct will not be cached because cache system need id,
  534. // but distinct will not provide id
  535. func (engine *Engine) Distinct(columns ...string) *Session {
  536. session := engine.NewSession()
  537. session.isAutoClose = true
  538. return session.Distinct(columns...)
  539. }
  540. // Select customerize your select columns or contents
  541. func (engine *Engine) Select(str string) *Session {
  542. session := engine.NewSession()
  543. session.isAutoClose = true
  544. return session.Select(str)
  545. }
  546. // Cols only use the parameters as select or update columns
  547. func (engine *Engine) Cols(columns ...string) *Session {
  548. session := engine.NewSession()
  549. session.isAutoClose = true
  550. return session.Cols(columns...)
  551. }
  552. // AllCols indicates that all columns should be use
  553. func (engine *Engine) AllCols() *Session {
  554. session := engine.NewSession()
  555. session.isAutoClose = true
  556. return session.AllCols()
  557. }
  558. // MustCols specify some columns must use even if they are empty
  559. func (engine *Engine) MustCols(columns ...string) *Session {
  560. session := engine.NewSession()
  561. session.isAutoClose = true
  562. return session.MustCols(columns...)
  563. }
  564. // UseBool xorm automatically retrieve condition according struct, but
  565. // if struct has bool field, it will ignore them. So use UseBool
  566. // to tell system to do not ignore them.
  567. // If no parameters, it will use all the bool field of struct, or
  568. // it will use parameters's columns
  569. func (engine *Engine) UseBool(columns ...string) *Session {
  570. session := engine.NewSession()
  571. session.isAutoClose = true
  572. return session.UseBool(columns...)
  573. }
  574. // Omit only not use the parameters as select or update columns
  575. func (engine *Engine) Omit(columns ...string) *Session {
  576. session := engine.NewSession()
  577. session.isAutoClose = true
  578. return session.Omit(columns...)
  579. }
  580. // Nullable set null when column is zero-value and nullable for update
  581. func (engine *Engine) Nullable(columns ...string) *Session {
  582. session := engine.NewSession()
  583. session.isAutoClose = true
  584. return session.Nullable(columns...)
  585. }
  586. // In will generate "column IN (?, ?)"
  587. func (engine *Engine) In(column string, args ...interface{}) *Session {
  588. session := engine.NewSession()
  589. session.isAutoClose = true
  590. return session.In(column, args...)
  591. }
  592. // NotIn will generate "column NOT IN (?, ?)"
  593. func (engine *Engine) NotIn(column string, args ...interface{}) *Session {
  594. session := engine.NewSession()
  595. session.isAutoClose = true
  596. return session.NotIn(column, args...)
  597. }
  598. // Incr provides a update string like "column = column + ?"
  599. func (engine *Engine) Incr(column string, arg ...interface{}) *Session {
  600. session := engine.NewSession()
  601. session.isAutoClose = true
  602. return session.Incr(column, arg...)
  603. }
  604. // Decr provides a update string like "column = column - ?"
  605. func (engine *Engine) Decr(column string, arg ...interface{}) *Session {
  606. session := engine.NewSession()
  607. session.isAutoClose = true
  608. return session.Decr(column, arg...)
  609. }
  610. // SetExpr provides a update string like "column = {expression}"
  611. func (engine *Engine) SetExpr(column string, expression interface{}) *Session {
  612. session := engine.NewSession()
  613. session.isAutoClose = true
  614. return session.SetExpr(column, expression)
  615. }
  616. // Table temporarily change the Get, Find, Update's table
  617. func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
  618. session := engine.NewSession()
  619. session.isAutoClose = true
  620. return session.Table(tableNameOrBean)
  621. }
  622. // Alias set the table alias
  623. func (engine *Engine) Alias(alias string) *Session {
  624. session := engine.NewSession()
  625. session.isAutoClose = true
  626. return session.Alias(alias)
  627. }
  628. // Limit will generate "LIMIT start, limit"
  629. func (engine *Engine) Limit(limit int, start ...int) *Session {
  630. session := engine.NewSession()
  631. session.isAutoClose = true
  632. return session.Limit(limit, start...)
  633. }
  634. // Desc will generate "ORDER BY column1 DESC, column2 DESC"
  635. func (engine *Engine) Desc(colNames ...string) *Session {
  636. session := engine.NewSession()
  637. session.isAutoClose = true
  638. return session.Desc(colNames...)
  639. }
  640. // Asc will generate "ORDER BY column1,column2 Asc"
  641. // This method can chainable use.
  642. //
  643. // engine.Desc("name").Asc("age").Find(&users)
  644. // // SELECT * FROM user ORDER BY name DESC, age ASC
  645. //
  646. func (engine *Engine) Asc(colNames ...string) *Session {
  647. session := engine.NewSession()
  648. session.isAutoClose = true
  649. return session.Asc(colNames...)
  650. }
  651. // OrderBy will generate "ORDER BY order"
  652. func (engine *Engine) OrderBy(order string) *Session {
  653. session := engine.NewSession()
  654. session.isAutoClose = true
  655. return session.OrderBy(order)
  656. }
  657. // Prepare enables prepare statement
  658. func (engine *Engine) Prepare() *Session {
  659. session := engine.NewSession()
  660. session.isAutoClose = true
  661. return session.Prepare()
  662. }
  663. // Join the join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
  664. func (engine *Engine) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session {
  665. session := engine.NewSession()
  666. session.isAutoClose = true
  667. return session.Join(joinOperator, tablename, condition, args...)
  668. }
  669. // GroupBy generate group by statement
  670. func (engine *Engine) GroupBy(keys string) *Session {
  671. session := engine.NewSession()
  672. session.isAutoClose = true
  673. return session.GroupBy(keys)
  674. }
  675. // Having generate having statement
  676. func (engine *Engine) Having(conditions string) *Session {
  677. session := engine.NewSession()
  678. session.isAutoClose = true
  679. return session.Having(conditions)
  680. }
  681. // Table table struct
  682. type Table struct {
  683. *schemas.Table
  684. Name string
  685. }
  686. // IsValid if table is valid
  687. func (t *Table) IsValid() bool {
  688. return t.Table != nil && len(t.Name) > 0
  689. }
  690. // TableInfo get table info according to bean's content
  691. func (engine *Engine) TableInfo(bean interface{}) (*schemas.Table, error) {
  692. v := utils.ReflectValue(bean)
  693. return engine.tagParser.ParseWithCache(v)
  694. }
  695. // IsTableEmpty if a table has any reocrd
  696. func (engine *Engine) IsTableEmpty(bean interface{}) (bool, error) {
  697. session := engine.NewSession()
  698. defer session.Close()
  699. return session.IsTableEmpty(bean)
  700. }
  701. // IsTableExist if a table is exist
  702. func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error) {
  703. session := engine.NewSession()
  704. defer session.Close()
  705. return session.IsTableExist(beanOrTableName)
  706. }
  707. // IDOf get id from one struct
  708. func (engine *Engine) IDOf(bean interface{}) (schemas.PK, error) {
  709. return engine.IDOfV(reflect.ValueOf(bean))
  710. }
  711. // TableName returns table name with schema prefix if has
  712. func (engine *Engine) TableName(bean interface{}, includeSchema ...bool) string {
  713. return dialects.FullTableName(engine.dialect, engine.GetTableMapper(), bean, includeSchema...)
  714. }
  715. // IDOfV get id from one value of struct
  716. func (engine *Engine) IDOfV(rv reflect.Value) (schemas.PK, error) {
  717. return engine.idOfV(rv)
  718. }
  719. func (engine *Engine) idOfV(rv reflect.Value) (schemas.PK, error) {
  720. v := reflect.Indirect(rv)
  721. table, err := engine.tagParser.ParseWithCache(v)
  722. if err != nil {
  723. return nil, err
  724. }
  725. pk := make([]interface{}, len(table.PrimaryKeys))
  726. for i, col := range table.PKColumns() {
  727. var err error
  728. fieldName := col.FieldName
  729. for {
  730. parts := strings.SplitN(fieldName, ".", 2)
  731. if len(parts) == 1 {
  732. break
  733. }
  734. v = v.FieldByName(parts[0])
  735. if v.Kind() == reflect.Ptr {
  736. v = v.Elem()
  737. }
  738. if v.Kind() != reflect.Struct {
  739. return nil, ErrUnSupportedType
  740. }
  741. fieldName = parts[1]
  742. }
  743. pkField := v.FieldByName(fieldName)
  744. switch pkField.Kind() {
  745. case reflect.String:
  746. pk[i], err = engine.idTypeAssertion(col, pkField.String())
  747. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  748. pk[i], err = engine.idTypeAssertion(col, strconv.FormatInt(pkField.Int(), 10))
  749. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  750. // id of uint will be converted to int64
  751. pk[i], err = engine.idTypeAssertion(col, strconv.FormatUint(pkField.Uint(), 10))
  752. }
  753. if err != nil {
  754. return nil, err
  755. }
  756. }
  757. return schemas.PK(pk), nil
  758. }
  759. func (engine *Engine) idTypeAssertion(col *schemas.Column, sid string) (interface{}, error) {
  760. if col.SQLType.IsNumeric() {
  761. n, err := strconv.ParseInt(sid, 10, 64)
  762. if err != nil {
  763. return nil, err
  764. }
  765. return n, nil
  766. } else if col.SQLType.IsText() {
  767. return sid, nil
  768. } else {
  769. return nil, errors.New("not supported")
  770. }
  771. }
  772. // CreateIndexes create indexes
  773. func (engine *Engine) CreateIndexes(bean interface{}) error {
  774. session := engine.NewSession()
  775. defer session.Close()
  776. return session.CreateIndexes(bean)
  777. }
  778. // CreateUniques create uniques
  779. func (engine *Engine) CreateUniques(bean interface{}) error {
  780. session := engine.NewSession()
  781. defer session.Close()
  782. return session.CreateUniques(bean)
  783. }
  784. // ClearCacheBean if enabled cache, clear the cache bean
  785. func (engine *Engine) ClearCacheBean(bean interface{}, id string) error {
  786. tableName := dialects.FullTableName(engine.dialect, engine.GetTableMapper(), bean)
  787. cacher := engine.GetCacher(tableName)
  788. if cacher != nil {
  789. cacher.ClearIds(tableName)
  790. cacher.DelBean(tableName, id)
  791. }
  792. return nil
  793. }
  794. // ClearCache if enabled cache, clear some tables' cache
  795. func (engine *Engine) ClearCache(beans ...interface{}) error {
  796. for _, bean := range beans {
  797. tableName := dialects.FullTableName(engine.dialect, engine.GetTableMapper(), bean)
  798. cacher := engine.GetCacher(tableName)
  799. if cacher != nil {
  800. cacher.ClearIds(tableName)
  801. cacher.ClearBeans(tableName)
  802. }
  803. }
  804. return nil
  805. }
  806. // UnMapType remove table from tables cache
  807. func (engine *Engine) UnMapType(t reflect.Type) {
  808. engine.tagParser.ClearCacheTable(t)
  809. }
  810. // Sync the new struct changes to database, this method will automatically add
  811. // table, column, index, unique. but will not delete or change anything.
  812. // If you change some field, you should change the database manually.
  813. func (engine *Engine) Sync(beans ...interface{}) error {
  814. session := engine.NewSession()
  815. defer session.Close()
  816. for _, bean := range beans {
  817. v := utils.ReflectValue(bean)
  818. tableNameNoSchema := dialects.FullTableName(engine.dialect, engine.GetTableMapper(), bean)
  819. table, err := engine.tagParser.ParseWithCache(v)
  820. if err != nil {
  821. return err
  822. }
  823. isExist, err := session.Table(bean).isTableExist(tableNameNoSchema)
  824. if err != nil {
  825. return err
  826. }
  827. if !isExist {
  828. err = session.createTable(bean)
  829. if err != nil {
  830. return err
  831. }
  832. }
  833. /*isEmpty, err := engine.IsEmptyTable(bean)
  834. if err != nil {
  835. return err
  836. }*/
  837. var isEmpty bool
  838. if isEmpty {
  839. err = session.dropTable(bean)
  840. if err != nil {
  841. return err
  842. }
  843. err = session.createTable(bean)
  844. if err != nil {
  845. return err
  846. }
  847. } else {
  848. for _, col := range table.Columns() {
  849. isExist, err := engine.dialect.IsColumnExist(engine.db, session.ctx, tableNameNoSchema, col.Name)
  850. if err != nil {
  851. return err
  852. }
  853. if !isExist {
  854. if err := session.statement.SetRefBean(bean); err != nil {
  855. return err
  856. }
  857. err = session.addColumn(col.Name)
  858. if err != nil {
  859. return err
  860. }
  861. }
  862. }
  863. for name, index := range table.Indexes {
  864. if err := session.statement.SetRefBean(bean); err != nil {
  865. return err
  866. }
  867. if index.Type == schemas.UniqueType {
  868. isExist, err := session.isIndexExist2(tableNameNoSchema, index.Cols, true)
  869. if err != nil {
  870. return err
  871. }
  872. if !isExist {
  873. if err := session.statement.SetRefBean(bean); err != nil {
  874. return err
  875. }
  876. err = session.addUnique(tableNameNoSchema, name)
  877. if err != nil {
  878. return err
  879. }
  880. }
  881. } else if index.Type == schemas.IndexType {
  882. isExist, err := session.isIndexExist2(tableNameNoSchema, index.Cols, false)
  883. if err != nil {
  884. return err
  885. }
  886. if !isExist {
  887. if err := session.statement.SetRefBean(bean); err != nil {
  888. return err
  889. }
  890. err = session.addIndex(tableNameNoSchema, name)
  891. if err != nil {
  892. return err
  893. }
  894. }
  895. } else {
  896. return errors.New("unknow index type")
  897. }
  898. }
  899. }
  900. }
  901. return nil
  902. }
  903. // Sync2 synchronize structs to database tables
  904. func (engine *Engine) Sync2(beans ...interface{}) error {
  905. s := engine.NewSession()
  906. defer s.Close()
  907. return s.Sync2(beans...)
  908. }
  909. // CreateTables create tabls according bean
  910. func (engine *Engine) CreateTables(beans ...interface{}) error {
  911. session := engine.NewSession()
  912. defer session.Close()
  913. err := session.Begin()
  914. if err != nil {
  915. return err
  916. }
  917. for _, bean := range beans {
  918. err = session.createTable(bean)
  919. if err != nil {
  920. session.Rollback()
  921. return err
  922. }
  923. }
  924. return session.Commit()
  925. }
  926. // DropTables drop specify tables
  927. func (engine *Engine) DropTables(beans ...interface{}) error {
  928. session := engine.NewSession()
  929. defer session.Close()
  930. err := session.Begin()
  931. if err != nil {
  932. return err
  933. }
  934. for _, bean := range beans {
  935. err = session.dropTable(bean)
  936. if err != nil {
  937. session.Rollback()
  938. return err
  939. }
  940. }
  941. return session.Commit()
  942. }
  943. // DropIndexes drop indexes of a table
  944. func (engine *Engine) DropIndexes(bean interface{}) error {
  945. session := engine.NewSession()
  946. defer session.Close()
  947. return session.DropIndexes(bean)
  948. }
  949. // Exec raw sql
  950. func (engine *Engine) Exec(sqlOrArgs ...interface{}) (sql.Result, error) {
  951. session := engine.NewSession()
  952. defer session.Close()
  953. return session.Exec(sqlOrArgs...)
  954. }
  955. // Query a raw sql and return records as []map[string][]byte
  956. func (engine *Engine) Query(sqlOrArgs ...interface{}) (resultsSlice []map[string][]byte, err error) {
  957. session := engine.NewSession()
  958. defer session.Close()
  959. return session.Query(sqlOrArgs...)
  960. }
  961. // QueryString runs a raw sql and return records as []map[string]string
  962. func (engine *Engine) QueryString(sqlOrArgs ...interface{}) ([]map[string]string, error) {
  963. session := engine.NewSession()
  964. defer session.Close()
  965. return session.QueryString(sqlOrArgs...)
  966. }
  967. // QueryInterface runs a raw sql and return records as []map[string]interface{}
  968. func (engine *Engine) QueryInterface(sqlOrArgs ...interface{}) ([]map[string]interface{}, error) {
  969. session := engine.NewSession()
  970. defer session.Close()
  971. return session.QueryInterface(sqlOrArgs...)
  972. }
  973. // Insert one or more records
  974. func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
  975. session := engine.NewSession()
  976. defer session.Close()
  977. return session.Insert(beans...)
  978. }
  979. // InsertOne insert only one record
  980. func (engine *Engine) InsertOne(bean interface{}) (int64, error) {
  981. session := engine.NewSession()
  982. defer session.Close()
  983. return session.InsertOne(bean)
  984. }
  985. // Update records, bean's non-empty fields are updated contents,
  986. // condiBean' non-empty filds are conditions
  987. // CAUTION:
  988. // 1.bool will defaultly be updated content nor conditions
  989. // You should call UseBool if you have bool to use.
  990. // 2.float32 & float64 may be not inexact as conditions
  991. func (engine *Engine) Update(bean interface{}, condiBeans ...interface{}) (int64, error) {
  992. session := engine.NewSession()
  993. defer session.Close()
  994. return session.Update(bean, condiBeans...)
  995. }
  996. // Delete records, bean's non-empty fields are conditions
  997. func (engine *Engine) Delete(bean interface{}) (int64, error) {
  998. session := engine.NewSession()
  999. defer session.Close()
  1000. return session.Delete(bean)
  1001. }
  1002. // Get retrieve one record from table, bean's non-empty fields
  1003. // are conditions
  1004. func (engine *Engine) Get(bean interface{}) (bool, error) {
  1005. session := engine.NewSession()
  1006. defer session.Close()
  1007. return session.Get(bean)
  1008. }
  1009. // Exist returns true if the record exist otherwise return false
  1010. func (engine *Engine) Exist(bean ...interface{}) (bool, error) {
  1011. session := engine.NewSession()
  1012. defer session.Close()
  1013. return session.Exist(bean...)
  1014. }
  1015. // Find retrieve records from table, condiBeans's non-empty fields
  1016. // are conditions. beans could be []Struct, []*Struct, map[int64]Struct
  1017. // map[int64]*Struct
  1018. func (engine *Engine) Find(beans interface{}, condiBeans ...interface{}) error {
  1019. session := engine.NewSession()
  1020. defer session.Close()
  1021. return session.Find(beans, condiBeans...)
  1022. }
  1023. // FindAndCount find the results and also return the counts
  1024. func (engine *Engine) FindAndCount(rowsSlicePtr interface{}, condiBean ...interface{}) (int64, error) {
  1025. session := engine.NewSession()
  1026. defer session.Close()
  1027. return session.FindAndCount(rowsSlicePtr, condiBean...)
  1028. }
  1029. // Iterate record by record handle records from table, bean's non-empty fields
  1030. // are conditions.
  1031. func (engine *Engine) Iterate(bean interface{}, fun IterFunc) error {
  1032. session := engine.NewSession()
  1033. defer session.Close()
  1034. return session.Iterate(bean, fun)
  1035. }
  1036. // Rows return sql.Rows compatible Rows obj, as a forward Iterator object for iterating record by record, bean's non-empty fields
  1037. // are conditions.
  1038. func (engine *Engine) Rows(bean interface{}) (*Rows, error) {
  1039. session := engine.NewSession()
  1040. return session.Rows(bean)
  1041. }
  1042. // Count counts the records. bean's non-empty fields are conditions.
  1043. func (engine *Engine) Count(bean ...interface{}) (int64, error) {
  1044. session := engine.NewSession()
  1045. defer session.Close()
  1046. return session.Count(bean...)
  1047. }
  1048. // Sum sum the records by some column. bean's non-empty fields are conditions.
  1049. func (engine *Engine) Sum(bean interface{}, colName string) (float64, error) {
  1050. session := engine.NewSession()
  1051. defer session.Close()
  1052. return session.Sum(bean, colName)
  1053. }
  1054. // SumInt sum the records by some column. bean's non-empty fields are conditions.
  1055. func (engine *Engine) SumInt(bean interface{}, colName string) (int64, error) {
  1056. session := engine.NewSession()
  1057. defer session.Close()
  1058. return session.SumInt(bean, colName)
  1059. }
  1060. // Sums sum the records by some columns. bean's non-empty fields are conditions.
  1061. func (engine *Engine) Sums(bean interface{}, colNames ...string) ([]float64, error) {
  1062. session := engine.NewSession()
  1063. defer session.Close()
  1064. return session.Sums(bean, colNames...)
  1065. }
  1066. // SumsInt like Sums but return slice of int64 instead of float64.
  1067. func (engine *Engine) SumsInt(bean interface{}, colNames ...string) ([]int64, error) {
  1068. session := engine.NewSession()
  1069. defer session.Close()
  1070. return session.SumsInt(bean, colNames...)
  1071. }
  1072. // ImportFile SQL DDL file
  1073. func (engine *Engine) ImportFile(ddlPath string) ([]sql.Result, error) {
  1074. session := engine.NewSession()
  1075. defer session.Close()
  1076. return session.ImportFile(ddlPath)
  1077. }
  1078. // Import SQL DDL from io.Reader
  1079. func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) {
  1080. session := engine.NewSession()
  1081. defer session.Close()
  1082. return session.Import(r)
  1083. }
  1084. // nowTime return current time
  1085. func (engine *Engine) nowTime(col *schemas.Column) (interface{}, time.Time) {
  1086. t := time.Now()
  1087. var tz = engine.DatabaseTZ
  1088. if !col.DisableTimeZone && col.TimeZone != nil {
  1089. tz = col.TimeZone
  1090. }
  1091. return dialects.FormatTime(engine.dialect, col.SQLType.Name, t.In(tz)), t.In(engine.TZLocation)
  1092. }
  1093. // GetColumnMapper returns the column name mapper
  1094. func (engine *Engine) GetColumnMapper() names.Mapper {
  1095. return engine.tagParser.GetColumnMapper()
  1096. }
  1097. // GetTableMapper returns the table name mapper
  1098. func (engine *Engine) GetTableMapper() names.Mapper {
  1099. return engine.tagParser.GetTableMapper()
  1100. }
  1101. // GetTZLocation returns time zone of the application
  1102. func (engine *Engine) GetTZLocation() *time.Location {
  1103. return engine.TZLocation
  1104. }
  1105. // SetTZLocation sets time zone of the application
  1106. func (engine *Engine) SetTZLocation(tz *time.Location) {
  1107. engine.TZLocation = tz
  1108. }
  1109. // GetTZDatabase returns time zone of the database
  1110. func (engine *Engine) GetTZDatabase() *time.Location {
  1111. return engine.DatabaseTZ
  1112. }
  1113. // SetTZDatabase sets time zone of the database
  1114. func (engine *Engine) SetTZDatabase(tz *time.Location) {
  1115. engine.DatabaseTZ = tz
  1116. }
  1117. // SetSchema sets the schema of database
  1118. func (engine *Engine) SetSchema(schema string) {
  1119. engine.dialect.URI().SetSchema(schema)
  1120. }
  1121. // Unscoped always disable struct tag "deleted"
  1122. func (engine *Engine) Unscoped() *Session {
  1123. session := engine.NewSession()
  1124. session.isAutoClose = true
  1125. return session.Unscoped()
  1126. }
  1127. func (engine *Engine) tbNameWithSchema(v string) string {
  1128. return dialects.TableNameWithSchema(engine.dialect, v)
  1129. }
  1130. // Context creates a session with the context
  1131. func (engine *Engine) Context(ctx context.Context) *Session {
  1132. session := engine.NewSession()
  1133. session.isAutoClose = true
  1134. return session.Context(ctx)
  1135. }
  1136. // SetDefaultContext set the default context
  1137. func (engine *Engine) SetDefaultContext(ctx context.Context) {
  1138. engine.defaultContext = ctx
  1139. }
  1140. // PingContext tests if database is alive
  1141. func (engine *Engine) PingContext(ctx context.Context) error {
  1142. session := engine.NewSession()
  1143. defer session.Close()
  1144. return session.PingContext(ctx)
  1145. }
  1146. // Transaction Execute sql wrapped in a transaction(abbr as tx), tx will automatic commit if no errors occurred
  1147. func (engine *Engine) Transaction(f func(*Session) (interface{}, error)) (interface{}, error) {
  1148. session := engine.NewSession()
  1149. defer session.Close()
  1150. if err := session.Begin(); err != nil {
  1151. return nil, err
  1152. }
  1153. result, err := f(session)
  1154. if err != nil {
  1155. return nil, err
  1156. }
  1157. if err := session.Commit(); err != nil {
  1158. return nil, err
  1159. }
  1160. return result, nil
  1161. }