engine.go 36 KB

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