engine.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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. "bufio"
  7. "bytes"
  8. "database/sql"
  9. "encoding/gob"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "os"
  14. "reflect"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "time"
  19. "github.com/go-xorm/core"
  20. )
  21. // Engine is the major struct of xorm, it means a database manager.
  22. // Commonly, an application only need one engine
  23. type Engine struct {
  24. db *core.DB
  25. dialect core.Dialect
  26. ColumnMapper core.IMapper
  27. TableMapper core.IMapper
  28. TagIdentifier string
  29. Tables map[reflect.Type]*core.Table
  30. mutex *sync.RWMutex
  31. Cacher core.Cacher
  32. showSQL bool
  33. showExecTime bool
  34. logger core.ILogger
  35. TZLocation *time.Location
  36. DatabaseTZ *time.Location // The timezone of the database
  37. disableGlobalCache bool
  38. }
  39. // ShowSQL show SQL statement or not on logger if log level is great than INFO
  40. func (engine *Engine) ShowSQL(show ...bool) {
  41. engine.logger.ShowSQL(show...)
  42. if len(show) == 0 {
  43. engine.showSQL = true
  44. } else {
  45. engine.showSQL = show[0]
  46. }
  47. }
  48. // ShowExecTime show SQL statement and execute time or not on logger if log level is great than INFO
  49. func (engine *Engine) ShowExecTime(show ...bool) {
  50. if len(show) == 0 {
  51. engine.showExecTime = true
  52. } else {
  53. engine.showExecTime = show[0]
  54. }
  55. }
  56. // Logger return the logger interface
  57. func (engine *Engine) Logger() core.ILogger {
  58. return engine.logger
  59. }
  60. // SetLogger set the new logger
  61. func (engine *Engine) SetLogger(logger core.ILogger) {
  62. engine.logger = logger
  63. engine.dialect.SetLogger(logger)
  64. }
  65. // SetDisableGlobalCache disable global cache or not
  66. func (engine *Engine) SetDisableGlobalCache(disable bool) {
  67. if engine.disableGlobalCache != disable {
  68. engine.disableGlobalCache = disable
  69. }
  70. }
  71. // DriverName return the current sql driver's name
  72. func (engine *Engine) DriverName() string {
  73. return engine.dialect.DriverName()
  74. }
  75. // DataSourceName return the current connection string
  76. func (engine *Engine) DataSourceName() string {
  77. return engine.dialect.DataSourceName()
  78. }
  79. // SetMapper set the name mapping rules
  80. func (engine *Engine) SetMapper(mapper core.IMapper) {
  81. engine.SetTableMapper(mapper)
  82. engine.SetColumnMapper(mapper)
  83. }
  84. // SetTableMapper set the table name mapping rule
  85. func (engine *Engine) SetTableMapper(mapper core.IMapper) {
  86. engine.TableMapper = mapper
  87. }
  88. // SetColumnMapper set the column name mapping rule
  89. func (engine *Engine) SetColumnMapper(mapper core.IMapper) {
  90. engine.ColumnMapper = mapper
  91. }
  92. // SupportInsertMany If engine's database support batch insert records like
  93. // "insert into user values (name, age), (name, age)".
  94. // When the return is ture, then engine.Insert(&users) will
  95. // generate batch sql and exeute.
  96. func (engine *Engine) SupportInsertMany() bool {
  97. return engine.dialect.SupportInsertMany()
  98. }
  99. // QuoteStr Engine's database use which character as quote.
  100. // mysql, sqlite use ` and postgres use "
  101. func (engine *Engine) QuoteStr() string {
  102. return engine.dialect.QuoteStr()
  103. }
  104. // Quote Use QuoteStr quote the string sql
  105. func (engine *Engine) Quote(value string) string {
  106. value = strings.TrimSpace(value)
  107. if len(value) == 0 {
  108. return value
  109. }
  110. if string(value[0]) == engine.dialect.QuoteStr() || value[0] == '`' {
  111. return value
  112. }
  113. value = strings.Replace(value, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1)
  114. return engine.dialect.QuoteStr() + value + engine.dialect.QuoteStr()
  115. }
  116. // QuoteTo quotes string and writes into the buffer
  117. func (engine *Engine) QuoteTo(buf *bytes.Buffer, value string) {
  118. if buf == nil {
  119. return
  120. }
  121. value = strings.TrimSpace(value)
  122. if value == "" {
  123. return
  124. }
  125. if string(value[0]) == engine.dialect.QuoteStr() || value[0] == '`' {
  126. buf.WriteString(value)
  127. return
  128. }
  129. value = strings.Replace(value, ".", engine.dialect.QuoteStr()+"."+engine.dialect.QuoteStr(), -1)
  130. buf.WriteString(engine.dialect.QuoteStr())
  131. buf.WriteString(value)
  132. buf.WriteString(engine.dialect.QuoteStr())
  133. }
  134. func (engine *Engine) quote(sql string) string {
  135. return engine.dialect.QuoteStr() + sql + engine.dialect.QuoteStr()
  136. }
  137. // SqlType will be depracated, please use SQLType instead
  138. //
  139. // Deprecated: use SQLType instead
  140. func (engine *Engine) SqlType(c *core.Column) string {
  141. return engine.SQLType(c)
  142. }
  143. // SQLType A simple wrapper to dialect's core.SqlType method
  144. func (engine *Engine) SQLType(c *core.Column) string {
  145. return engine.dialect.SqlType(c)
  146. }
  147. // AutoIncrStr Database's autoincrement statement
  148. func (engine *Engine) AutoIncrStr() string {
  149. return engine.dialect.AutoIncrStr()
  150. }
  151. // SetMaxOpenConns is only available for go 1.2+
  152. func (engine *Engine) SetMaxOpenConns(conns int) {
  153. engine.db.SetMaxOpenConns(conns)
  154. }
  155. // SetMaxIdleConns set the max idle connections on pool, default is 2
  156. func (engine *Engine) SetMaxIdleConns(conns int) {
  157. engine.db.SetMaxIdleConns(conns)
  158. }
  159. // SetDefaultCacher set the default cacher. Xorm's default not enable cacher.
  160. func (engine *Engine) SetDefaultCacher(cacher core.Cacher) {
  161. engine.Cacher = cacher
  162. }
  163. // NoCache If you has set default cacher, and you want temporilly stop use cache,
  164. // you can use NoCache()
  165. func (engine *Engine) NoCache() *Session {
  166. session := engine.NewSession()
  167. session.IsAutoClose = true
  168. return session.NoCache()
  169. }
  170. // NoCascade If you do not want to auto cascade load object
  171. func (engine *Engine) NoCascade() *Session {
  172. session := engine.NewSession()
  173. session.IsAutoClose = true
  174. return session.NoCascade()
  175. }
  176. // MapCacher Set a table use a special cacher
  177. func (engine *Engine) MapCacher(bean interface{}, cacher core.Cacher) {
  178. v := rValue(bean)
  179. tb := engine.autoMapType(v)
  180. tb.Cacher = cacher
  181. }
  182. // NewDB provides an interface to operate database directly
  183. func (engine *Engine) NewDB() (*core.DB, error) {
  184. return core.OpenDialect(engine.dialect)
  185. }
  186. // DB return the wrapper of sql.DB
  187. func (engine *Engine) DB() *core.DB {
  188. return engine.db
  189. }
  190. // Dialect return database dialect
  191. func (engine *Engine) Dialect() core.Dialect {
  192. return engine.dialect
  193. }
  194. // NewSession New a session
  195. func (engine *Engine) NewSession() *Session {
  196. session := &Session{Engine: engine}
  197. session.Init()
  198. return session
  199. }
  200. // Close the engine
  201. func (engine *Engine) Close() error {
  202. return engine.db.Close()
  203. }
  204. // Ping tests if database is alive
  205. func (engine *Engine) Ping() error {
  206. session := engine.NewSession()
  207. defer session.Close()
  208. engine.logger.Infof("PING DATABASE %v", engine.DriverName())
  209. return session.Ping()
  210. }
  211. // logging sql
  212. func (engine *Engine) logSQL(sqlStr string, sqlArgs ...interface{}) {
  213. if engine.showSQL && !engine.showExecTime {
  214. if len(sqlArgs) > 0 {
  215. engine.logger.Infof("[sql] %v [args] %v", sqlStr, sqlArgs)
  216. } else {
  217. engine.logger.Infof("[sql] %v", sqlStr)
  218. }
  219. }
  220. }
  221. func (engine *Engine) logSQLQueryTime(sqlStr string, args []interface{}, executionBlock func() (*core.Stmt, *core.Rows, error)) (*core.Stmt, *core.Rows, error) {
  222. if engine.showSQL && engine.showExecTime {
  223. b4ExecTime := time.Now()
  224. stmt, res, err := executionBlock()
  225. execDuration := time.Since(b4ExecTime)
  226. if len(args) > 0 {
  227. engine.logger.Infof("[sql] %s [args] %v - took: %v", sqlStr, args, execDuration)
  228. } else {
  229. engine.logger.Infof("[sql] %s - took: %v", sqlStr, execDuration)
  230. }
  231. return stmt, res, err
  232. }
  233. return executionBlock()
  234. }
  235. func (engine *Engine) logSQLExecutionTime(sqlStr string, args []interface{}, executionBlock func() (sql.Result, error)) (sql.Result, error) {
  236. if engine.showSQL && engine.showExecTime {
  237. b4ExecTime := time.Now()
  238. res, err := executionBlock()
  239. execDuration := time.Since(b4ExecTime)
  240. if len(args) > 0 {
  241. engine.logger.Infof("[sql] %s [args] %v - took: %v", sqlStr, args, execDuration)
  242. } else {
  243. engine.logger.Infof("[sql] %s - took: %v", sqlStr, execDuration)
  244. }
  245. return res, err
  246. }
  247. return executionBlock()
  248. }
  249. // Sql provides raw sql input parameter. When you have a complex SQL statement
  250. // and cannot use Where, Id, In and etc. Methods to describe, you can use SQL.
  251. //
  252. // Deprecated: use SQL instead.
  253. func (engine *Engine) Sql(querystring string, args ...interface{}) *Session {
  254. return engine.SQL(querystring, args...)
  255. }
  256. // SQL method let's you manually write raw SQL and operate
  257. // For example:
  258. //
  259. // engine.SQL("select * from user").Find(&users)
  260. //
  261. // This code will execute "select * from user" and set the records to users
  262. func (engine *Engine) SQL(query interface{}, args ...interface{}) *Session {
  263. session := engine.NewSession()
  264. session.IsAutoClose = true
  265. return session.SQL(query, args...)
  266. }
  267. // NoAutoTime Default if your struct has "created" or "updated" filed tag, the fields
  268. // will automatically be filled with current time when Insert or Update
  269. // invoked. Call NoAutoTime if you dont' want to fill automatically.
  270. func (engine *Engine) NoAutoTime() *Session {
  271. session := engine.NewSession()
  272. session.IsAutoClose = true
  273. return session.NoAutoTime()
  274. }
  275. // NoAutoCondition disable auto generate Where condition from bean or not
  276. func (engine *Engine) NoAutoCondition(no ...bool) *Session {
  277. session := engine.NewSession()
  278. session.IsAutoClose = true
  279. return session.NoAutoCondition(no...)
  280. }
  281. // DBMetas Retrieve all tables, columns, indexes' informations from database.
  282. func (engine *Engine) DBMetas() ([]*core.Table, error) {
  283. tables, err := engine.dialect.GetTables()
  284. if err != nil {
  285. return nil, err
  286. }
  287. for _, table := range tables {
  288. colSeq, cols, err := engine.dialect.GetColumns(table.Name)
  289. if err != nil {
  290. return nil, err
  291. }
  292. for _, name := range colSeq {
  293. table.AddColumn(cols[name])
  294. }
  295. indexes, err := engine.dialect.GetIndexes(table.Name)
  296. if err != nil {
  297. return nil, err
  298. }
  299. table.Indexes = indexes
  300. for _, index := range indexes {
  301. for _, name := range index.Cols {
  302. if col := table.GetColumn(name); col != nil {
  303. col.Indexes[index.Name] = index.Type
  304. } else {
  305. return nil, fmt.Errorf("Unknown col %s in index %v of table %v, columns %v", name, index.Name, table.Name, table.ColumnsSeq())
  306. }
  307. }
  308. }
  309. }
  310. return tables, nil
  311. }
  312. // DumpAllToFile dump database all table structs and data to a file
  313. func (engine *Engine) DumpAllToFile(fp string, tp ...core.DbType) error {
  314. f, err := os.Create(fp)
  315. if err != nil {
  316. return err
  317. }
  318. defer f.Close()
  319. return engine.DumpAll(f, tp...)
  320. }
  321. // DumpAll dump database all table structs and data to w
  322. func (engine *Engine) DumpAll(w io.Writer, tp ...core.DbType) error {
  323. tables, err := engine.DBMetas()
  324. if err != nil {
  325. return err
  326. }
  327. return engine.DumpTables(tables, w, tp...)
  328. }
  329. // DumpTablesToFile dump specified tables to SQL file.
  330. func (engine *Engine) DumpTablesToFile(tables []*core.Table, fp string, tp ...core.DbType) error {
  331. f, err := os.Create(fp)
  332. if err != nil {
  333. return err
  334. }
  335. defer f.Close()
  336. return engine.DumpTables(tables, f, tp...)
  337. }
  338. // DumpTables dump specify tables to io.Writer
  339. func (engine *Engine) DumpTables(tables []*core.Table, w io.Writer, tp ...core.DbType) error {
  340. return engine.dumpTables(tables, w, tp...)
  341. }
  342. // dumpTables dump database all table structs and data to w with specify db type
  343. func (engine *Engine) dumpTables(tables []*core.Table, w io.Writer, tp ...core.DbType) error {
  344. var dialect core.Dialect
  345. var distDBName string
  346. if len(tp) == 0 {
  347. dialect = engine.dialect
  348. distDBName = string(engine.dialect.DBType())
  349. } else {
  350. dialect = core.QueryDialect(tp[0])
  351. if dialect == nil {
  352. return errors.New("Unsupported database type")
  353. }
  354. dialect.Init(nil, engine.dialect.URI(), "", "")
  355. distDBName = string(tp[0])
  356. }
  357. _, err := io.WriteString(w, fmt.Sprintf("/*Generated by xorm v%s %s, from %s to %s*/\n\n",
  358. Version, time.Now().In(engine.TZLocation).Format("2006-01-02 15:04:05"), engine.dialect.DBType(), strings.ToUpper(distDBName)))
  359. if err != nil {
  360. return err
  361. }
  362. for i, table := range tables {
  363. if i > 0 {
  364. _, err = io.WriteString(w, "\n")
  365. if err != nil {
  366. return err
  367. }
  368. }
  369. _, err = io.WriteString(w, dialect.CreateTableSql(table, "", table.StoreEngine, "")+";\n")
  370. if err != nil {
  371. return err
  372. }
  373. for _, index := range table.Indexes {
  374. _, err = io.WriteString(w, dialect.CreateIndexSql(table.Name, index)+";\n")
  375. if err != nil {
  376. return err
  377. }
  378. }
  379. cols := table.ColumnsSeq()
  380. colNames := dialect.Quote(strings.Join(cols, dialect.Quote(", ")))
  381. rows, err := engine.DB().Query("SELECT " + colNames + " FROM " + engine.Quote(table.Name))
  382. if err != nil {
  383. return err
  384. }
  385. defer rows.Close()
  386. for rows.Next() {
  387. dest := make([]interface{}, len(cols))
  388. err = rows.ScanSlice(&dest)
  389. if err != nil {
  390. return err
  391. }
  392. _, err = io.WriteString(w, "INSERT INTO "+dialect.Quote(table.Name)+" ("+colNames+") VALUES (")
  393. if err != nil {
  394. return err
  395. }
  396. var temp string
  397. for i, d := range dest {
  398. col := table.GetColumn(cols[i])
  399. if col == nil {
  400. return errors.New("unknow column error")
  401. }
  402. if d == nil {
  403. temp += ", NULL"
  404. } else if col.SQLType.IsText() || col.SQLType.IsTime() {
  405. var v = fmt.Sprintf("%s", d)
  406. if strings.HasSuffix(v, " +0000 UTC") {
  407. temp += fmt.Sprintf(", '%s'", v[0:len(v)-len(" +0000 UTC")])
  408. } else {
  409. temp += ", '" + strings.Replace(v, "'", "''", -1) + "'"
  410. }
  411. } else if col.SQLType.IsBlob() {
  412. if reflect.TypeOf(d).Kind() == reflect.Slice {
  413. temp += fmt.Sprintf(", %s", dialect.FormatBytes(d.([]byte)))
  414. } else if reflect.TypeOf(d).Kind() == reflect.String {
  415. temp += fmt.Sprintf(", '%s'", d.(string))
  416. }
  417. } else if col.SQLType.IsNumeric() {
  418. switch reflect.TypeOf(d).Kind() {
  419. case reflect.Slice:
  420. temp += fmt.Sprintf(", %s", string(d.([]byte)))
  421. case reflect.Int16, reflect.Int8, reflect.Int32, reflect.Int64, reflect.Int:
  422. if col.SQLType.Name == core.Bool {
  423. temp += fmt.Sprintf(", %v", strconv.FormatBool(reflect.ValueOf(d).Int() > 0))
  424. } else {
  425. temp += fmt.Sprintf(", %v", d)
  426. }
  427. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  428. if col.SQLType.Name == core.Bool {
  429. temp += fmt.Sprintf(", %v", strconv.FormatBool(reflect.ValueOf(d).Uint() > 0))
  430. } else {
  431. temp += fmt.Sprintf(", %v", d)
  432. }
  433. default:
  434. temp += fmt.Sprintf(", %v", d)
  435. }
  436. } else {
  437. s := fmt.Sprintf("%v", d)
  438. if strings.Contains(s, ":") || strings.Contains(s, "-") {
  439. if strings.HasSuffix(s, " +0000 UTC") {
  440. temp += fmt.Sprintf(", '%s'", s[0:len(s)-len(" +0000 UTC")])
  441. } else {
  442. temp += fmt.Sprintf(", '%s'", s)
  443. }
  444. } else {
  445. temp += fmt.Sprintf(", %s", s)
  446. }
  447. }
  448. }
  449. _, err = io.WriteString(w, temp[2:]+");\n")
  450. if err != nil {
  451. return err
  452. }
  453. }
  454. // FIXME: Hack for postgres
  455. if string(dialect.DBType()) == core.POSTGRES && table.AutoIncrColumn() != nil {
  456. _, err = io.WriteString(w, "SELECT setval('table_id_seq', COALESCE((SELECT MAX("+table.AutoIncrColumn().Name+") FROM "+dialect.Quote(table.Name)+"), 1), false);\n")
  457. if err != nil {
  458. return err
  459. }
  460. }
  461. }
  462. return nil
  463. }
  464. func (engine *Engine) tableName(beanOrTableName interface{}) (string, error) {
  465. v := rValue(beanOrTableName)
  466. if v.Type().Kind() == reflect.String {
  467. return beanOrTableName.(string), nil
  468. } else if v.Type().Kind() == reflect.Struct {
  469. return engine.tbName(v), nil
  470. }
  471. return "", errors.New("bean should be a struct or struct's point")
  472. }
  473. func (engine *Engine) tbName(v reflect.Value) string {
  474. if tb, ok := v.Interface().(TableName); ok {
  475. return tb.TableName()
  476. }
  477. if v.Type().Kind() == reflect.Ptr {
  478. if tb, ok := reflect.Indirect(v).Interface().(TableName); ok {
  479. return tb.TableName()
  480. }
  481. } else if v.CanAddr() {
  482. if tb, ok := v.Addr().Interface().(TableName); ok {
  483. return tb.TableName()
  484. }
  485. }
  486. return engine.TableMapper.Obj2Table(reflect.Indirect(v).Type().Name())
  487. }
  488. // Cascade use cascade or not
  489. func (engine *Engine) Cascade(trueOrFalse ...bool) *Session {
  490. session := engine.NewSession()
  491. session.IsAutoClose = true
  492. return session.Cascade(trueOrFalse...)
  493. }
  494. // Where method provide a condition query
  495. func (engine *Engine) Where(query interface{}, args ...interface{}) *Session {
  496. session := engine.NewSession()
  497. session.IsAutoClose = true
  498. return session.Where(query, args...)
  499. }
  500. // Id will be depracated, please use ID instead
  501. func (engine *Engine) Id(id interface{}) *Session {
  502. session := engine.NewSession()
  503. session.IsAutoClose = true
  504. return session.Id(id)
  505. }
  506. // ID method provoide a condition as (id) = ?
  507. func (engine *Engine) ID(id interface{}) *Session {
  508. session := engine.NewSession()
  509. session.IsAutoClose = true
  510. return session.ID(id)
  511. }
  512. // Before apply before Processor, affected bean is passed to closure arg
  513. func (engine *Engine) Before(closures func(interface{})) *Session {
  514. session := engine.NewSession()
  515. session.IsAutoClose = true
  516. return session.Before(closures)
  517. }
  518. // After apply after insert Processor, affected bean is passed to closure arg
  519. func (engine *Engine) After(closures func(interface{})) *Session {
  520. session := engine.NewSession()
  521. session.IsAutoClose = true
  522. return session.After(closures)
  523. }
  524. // Charset set charset when create table, only support mysql now
  525. func (engine *Engine) Charset(charset string) *Session {
  526. session := engine.NewSession()
  527. session.IsAutoClose = true
  528. return session.Charset(charset)
  529. }
  530. // StoreEngine set store engine when create table, only support mysql now
  531. func (engine *Engine) StoreEngine(storeEngine string) *Session {
  532. session := engine.NewSession()
  533. session.IsAutoClose = true
  534. return session.StoreEngine(storeEngine)
  535. }
  536. // Distinct use for distinct columns. Caution: when you are using cache,
  537. // distinct will not be cached because cache system need id,
  538. // but distinct will not provide id
  539. func (engine *Engine) Distinct(columns ...string) *Session {
  540. session := engine.NewSession()
  541. session.IsAutoClose = true
  542. return session.Distinct(columns...)
  543. }
  544. // Select customerize your select columns or contents
  545. func (engine *Engine) Select(str string) *Session {
  546. session := engine.NewSession()
  547. session.IsAutoClose = true
  548. return session.Select(str)
  549. }
  550. // Cols only use the parameters as select or update columns
  551. func (engine *Engine) Cols(columns ...string) *Session {
  552. session := engine.NewSession()
  553. session.IsAutoClose = true
  554. return session.Cols(columns...)
  555. }
  556. // AllCols indicates that all columns should be use
  557. func (engine *Engine) AllCols() *Session {
  558. session := engine.NewSession()
  559. session.IsAutoClose = true
  560. return session.AllCols()
  561. }
  562. // MustCols specify some columns must use even if they are empty
  563. func (engine *Engine) MustCols(columns ...string) *Session {
  564. session := engine.NewSession()
  565. session.IsAutoClose = true
  566. return session.MustCols(columns...)
  567. }
  568. // UseBool xorm automatically retrieve condition according struct, but
  569. // if struct has bool field, it will ignore them. So use UseBool
  570. // to tell system to do not ignore them.
  571. // If no parameters, it will use all the bool field of struct, or
  572. // it will use parameters's columns
  573. func (engine *Engine) UseBool(columns ...string) *Session {
  574. session := engine.NewSession()
  575. session.IsAutoClose = true
  576. return session.UseBool(columns...)
  577. }
  578. // Omit only not use the parameters as select or update columns
  579. func (engine *Engine) Omit(columns ...string) *Session {
  580. session := engine.NewSession()
  581. session.IsAutoClose = true
  582. return session.Omit(columns...)
  583. }
  584. // Nullable set null when column is zero-value and nullable for update
  585. func (engine *Engine) Nullable(columns ...string) *Session {
  586. session := engine.NewSession()
  587. session.IsAutoClose = true
  588. return session.Nullable(columns...)
  589. }
  590. // In will generate "column IN (?, ?)"
  591. func (engine *Engine) In(column string, args ...interface{}) *Session {
  592. session := engine.NewSession()
  593. session.IsAutoClose = true
  594. return session.In(column, args...)
  595. }
  596. // Incr provides a update string like "column = column + ?"
  597. func (engine *Engine) Incr(column string, arg ...interface{}) *Session {
  598. session := engine.NewSession()
  599. session.IsAutoClose = true
  600. return session.Incr(column, arg...)
  601. }
  602. // Decr provides a update string like "column = column - ?"
  603. func (engine *Engine) Decr(column string, arg ...interface{}) *Session {
  604. session := engine.NewSession()
  605. session.IsAutoClose = true
  606. return session.Decr(column, arg...)
  607. }
  608. // SetExpr provides a update string like "column = {expression}"
  609. func (engine *Engine) SetExpr(column string, expression string) *Session {
  610. session := engine.NewSession()
  611. session.IsAutoClose = true
  612. return session.SetExpr(column, expression)
  613. }
  614. // Table temporarily change the Get, Find, Update's table
  615. func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
  616. session := engine.NewSession()
  617. session.IsAutoClose = true
  618. return session.Table(tableNameOrBean)
  619. }
  620. // Alias set the table alias
  621. func (engine *Engine) Alias(alias string) *Session {
  622. session := engine.NewSession()
  623. session.IsAutoClose = true
  624. return session.Alias(alias)
  625. }
  626. // Limit will generate "LIMIT start, limit"
  627. func (engine *Engine) Limit(limit int, start ...int) *Session {
  628. session := engine.NewSession()
  629. session.IsAutoClose = true
  630. return session.Limit(limit, start...)
  631. }
  632. // Desc will generate "ORDER BY column1 DESC, column2 DESC"
  633. func (engine *Engine) Desc(colNames ...string) *Session {
  634. session := engine.NewSession()
  635. session.IsAutoClose = true
  636. return session.Desc(colNames...)
  637. }
  638. // Asc will generate "ORDER BY column1,column2 Asc"
  639. // This method can chainable use.
  640. //
  641. // engine.Desc("name").Asc("age").Find(&users)
  642. // // SELECT * FROM user ORDER BY name DESC, age ASC
  643. //
  644. func (engine *Engine) Asc(colNames ...string) *Session {
  645. session := engine.NewSession()
  646. session.IsAutoClose = true
  647. return session.Asc(colNames...)
  648. }
  649. // OrderBy will generate "ORDER BY order"
  650. func (engine *Engine) OrderBy(order string) *Session {
  651. session := engine.NewSession()
  652. session.IsAutoClose = true
  653. return session.OrderBy(order)
  654. }
  655. // Join the join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
  656. func (engine *Engine) Join(joinOperator string, tablename interface{}, condition string, args ...interface{}) *Session {
  657. session := engine.NewSession()
  658. session.IsAutoClose = true
  659. return session.Join(joinOperator, tablename, condition, args...)
  660. }
  661. // GroupBy generate group by statement
  662. func (engine *Engine) GroupBy(keys string) *Session {
  663. session := engine.NewSession()
  664. session.IsAutoClose = true
  665. return session.GroupBy(keys)
  666. }
  667. // Having generate having statement
  668. func (engine *Engine) Having(conditions string) *Session {
  669. session := engine.NewSession()
  670. session.IsAutoClose = true
  671. return session.Having(conditions)
  672. }
  673. func (engine *Engine) autoMapType(v reflect.Value) *core.Table {
  674. t := v.Type()
  675. engine.mutex.Lock()
  676. defer engine.mutex.Unlock()
  677. table, ok := engine.Tables[t]
  678. if !ok {
  679. table = engine.mapType(v)
  680. engine.Tables[t] = table
  681. if engine.Cacher != nil {
  682. if v.CanAddr() {
  683. engine.GobRegister(v.Addr().Interface())
  684. } else {
  685. engine.GobRegister(v.Interface())
  686. }
  687. }
  688. }
  689. return table
  690. }
  691. // GobRegister register one struct to gob for cache use
  692. func (engine *Engine) GobRegister(v interface{}) *Engine {
  693. //fmt.Printf("Type: %[1]T => Data: %[1]#v\n", v)
  694. gob.Register(v)
  695. return engine
  696. }
  697. // Table table struct
  698. type Table struct {
  699. *core.Table
  700. Name string
  701. }
  702. // TableInfo get table info according to bean's content
  703. func (engine *Engine) TableInfo(bean interface{}) *Table {
  704. v := rValue(bean)
  705. return &Table{engine.autoMapType(v), engine.tbName(v)}
  706. }
  707. func addIndex(indexName string, table *core.Table, col *core.Column, indexType int) {
  708. if index, ok := table.Indexes[indexName]; ok {
  709. index.AddColumn(col.Name)
  710. col.Indexes[index.Name] = indexType
  711. } else {
  712. index := core.NewIndex(indexName, indexType)
  713. index.AddColumn(col.Name)
  714. table.AddIndex(index)
  715. col.Indexes[index.Name] = indexType
  716. }
  717. }
  718. func (engine *Engine) newTable() *core.Table {
  719. table := core.NewEmptyTable()
  720. if !engine.disableGlobalCache {
  721. table.Cacher = engine.Cacher
  722. }
  723. return table
  724. }
  725. // TableName table name interface to define customerize table name
  726. type TableName interface {
  727. TableName() string
  728. }
  729. var (
  730. tpTableName = reflect.TypeOf((*TableName)(nil)).Elem()
  731. )
  732. func (engine *Engine) mapType(v reflect.Value) *core.Table {
  733. t := v.Type()
  734. table := engine.newTable()
  735. if tb, ok := v.Interface().(TableName); ok {
  736. table.Name = tb.TableName()
  737. } else {
  738. if v.CanAddr() {
  739. if tb, ok = v.Addr().Interface().(TableName); ok {
  740. table.Name = tb.TableName()
  741. }
  742. }
  743. if table.Name == "" {
  744. table.Name = engine.TableMapper.Obj2Table(t.Name())
  745. }
  746. }
  747. table.Type = t
  748. var idFieldColName string
  749. var err error
  750. var hasCacheTag, hasNoCacheTag bool
  751. for i := 0; i < t.NumField(); i++ {
  752. tag := t.Field(i).Tag
  753. ormTagStr := tag.Get(engine.TagIdentifier)
  754. var col *core.Column
  755. fieldValue := v.Field(i)
  756. fieldType := fieldValue.Type()
  757. if ormTagStr != "" {
  758. col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
  759. IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]int)}
  760. tags := splitTag(ormTagStr)
  761. if len(tags) > 0 {
  762. if tags[0] == "-" {
  763. continue
  764. }
  765. if strings.ToUpper(tags[0]) == "EXTENDS" {
  766. switch fieldValue.Kind() {
  767. case reflect.Ptr:
  768. f := fieldValue.Type().Elem()
  769. if f.Kind() == reflect.Struct {
  770. fieldPtr := fieldValue
  771. fieldValue = fieldValue.Elem()
  772. if !fieldValue.IsValid() || fieldPtr.IsNil() {
  773. fieldValue = reflect.New(f).Elem()
  774. }
  775. }
  776. fallthrough
  777. case reflect.Struct:
  778. parentTable := engine.mapType(fieldValue)
  779. for _, col := range parentTable.Columns() {
  780. col.FieldName = fmt.Sprintf("%v.%v", t.Field(i).Name, col.FieldName)
  781. table.AddColumn(col)
  782. for indexName, indexType := range col.Indexes {
  783. addIndex(indexName, table, col, indexType)
  784. }
  785. }
  786. continue
  787. default:
  788. //TODO: warning
  789. }
  790. }
  791. indexNames := make(map[string]int)
  792. var isIndex, isUnique bool
  793. var preKey string
  794. for j, key := range tags {
  795. k := strings.ToUpper(key)
  796. switch {
  797. case k == "<-":
  798. col.MapType = core.ONLYFROMDB
  799. case k == "->":
  800. col.MapType = core.ONLYTODB
  801. case k == "PK":
  802. col.IsPrimaryKey = true
  803. col.Nullable = false
  804. case k == "NULL":
  805. if j == 0 {
  806. col.Nullable = true
  807. } else {
  808. col.Nullable = (strings.ToUpper(tags[j-1]) != "NOT")
  809. }
  810. // TODO: for postgres how add autoincr?
  811. /*case strings.HasPrefix(k, "AUTOINCR(") && strings.HasSuffix(k, ")"):
  812. col.IsAutoIncrement = true
  813. autoStart := k[len("AUTOINCR")+1 : len(k)-1]
  814. autoStartInt, err := strconv.Atoi(autoStart)
  815. if err != nil {
  816. engine.LogError(err)
  817. }
  818. col.AutoIncrStart = autoStartInt*/
  819. case k == "AUTOINCR":
  820. col.IsAutoIncrement = true
  821. //col.AutoIncrStart = 1
  822. case k == "DEFAULT":
  823. col.Default = tags[j+1]
  824. case k == "CREATED":
  825. col.IsCreated = true
  826. case k == "VERSION":
  827. col.IsVersion = true
  828. col.Default = "1"
  829. case k == "UTC":
  830. col.TimeZone = time.UTC
  831. case k == "LOCAL":
  832. col.TimeZone = time.Local
  833. case strings.HasPrefix(k, "LOCALE(") && strings.HasSuffix(k, ")"):
  834. location := k[len("LOCALE")+1 : len(k)-1]
  835. col.TimeZone, err = time.LoadLocation(location)
  836. if err != nil {
  837. engine.logger.Error(err)
  838. }
  839. case k == "UPDATED":
  840. col.IsUpdated = true
  841. case k == "DELETED":
  842. col.IsDeleted = true
  843. case strings.HasPrefix(k, "INDEX(") && strings.HasSuffix(k, ")"):
  844. indexName := k[len("INDEX")+1 : len(k)-1]
  845. indexNames[indexName] = core.IndexType
  846. case k == "INDEX":
  847. isIndex = true
  848. case strings.HasPrefix(k, "UNIQUE(") && strings.HasSuffix(k, ")"):
  849. indexName := k[len("UNIQUE")+1 : len(k)-1]
  850. indexNames[indexName] = core.UniqueType
  851. case k == "UNIQUE":
  852. isUnique = true
  853. case k == "NOTNULL":
  854. col.Nullable = false
  855. case k == "CACHE":
  856. if !hasCacheTag {
  857. hasCacheTag = true
  858. }
  859. case k == "NOCACHE":
  860. if !hasNoCacheTag {
  861. hasNoCacheTag = true
  862. }
  863. case k == "NOT":
  864. default:
  865. if strings.HasPrefix(k, "'") && strings.HasSuffix(k, "'") {
  866. if preKey != "DEFAULT" {
  867. col.Name = key[1 : len(key)-1]
  868. }
  869. } else if strings.Contains(k, "(") && strings.HasSuffix(k, ")") {
  870. fs := strings.Split(k, "(")
  871. if _, ok := core.SqlTypes[fs[0]]; !ok {
  872. preKey = k
  873. continue
  874. }
  875. col.SQLType = core.SQLType{Name: fs[0]}
  876. if fs[0] == core.Enum && fs[1][0] == '\'' { //enum
  877. options := strings.Split(fs[1][0:len(fs[1])-1], ",")
  878. col.EnumOptions = make(map[string]int)
  879. for k, v := range options {
  880. v = strings.TrimSpace(v)
  881. v = strings.Trim(v, "'")
  882. col.EnumOptions[v] = k
  883. }
  884. } else if fs[0] == core.Set && fs[1][0] == '\'' { //set
  885. options := strings.Split(fs[1][0:len(fs[1])-1], ",")
  886. col.SetOptions = make(map[string]int)
  887. for k, v := range options {
  888. v = strings.TrimSpace(v)
  889. v = strings.Trim(v, "'")
  890. col.SetOptions[v] = k
  891. }
  892. } else {
  893. fs2 := strings.Split(fs[1][0:len(fs[1])-1], ",")
  894. if len(fs2) == 2 {
  895. col.Length, err = strconv.Atoi(fs2[0])
  896. if err != nil {
  897. engine.logger.Error(err)
  898. }
  899. col.Length2, err = strconv.Atoi(fs2[1])
  900. if err != nil {
  901. engine.logger.Error(err)
  902. }
  903. } else if len(fs2) == 1 {
  904. col.Length, err = strconv.Atoi(fs2[0])
  905. if err != nil {
  906. engine.logger.Error(err)
  907. }
  908. }
  909. }
  910. } else {
  911. if _, ok := core.SqlTypes[k]; ok {
  912. col.SQLType = core.SQLType{Name: k}
  913. } else if key != col.Default {
  914. col.Name = key
  915. }
  916. }
  917. engine.dialect.SqlType(col)
  918. }
  919. preKey = k
  920. }
  921. if col.SQLType.Name == "" {
  922. col.SQLType = core.Type2SQLType(fieldType)
  923. }
  924. if col.Length == 0 {
  925. col.Length = col.SQLType.DefaultLength
  926. }
  927. if col.Length2 == 0 {
  928. col.Length2 = col.SQLType.DefaultLength2
  929. }
  930. if col.Name == "" {
  931. col.Name = engine.ColumnMapper.Obj2Table(t.Field(i).Name)
  932. }
  933. if isUnique {
  934. indexNames[col.Name] = core.UniqueType
  935. } else if isIndex {
  936. indexNames[col.Name] = core.IndexType
  937. }
  938. for indexName, indexType := range indexNames {
  939. addIndex(indexName, table, col, indexType)
  940. }
  941. }
  942. } else {
  943. var sqlType core.SQLType
  944. if fieldValue.CanAddr() {
  945. if _, ok := fieldValue.Addr().Interface().(core.Conversion); ok {
  946. sqlType = core.SQLType{Name: core.Text}
  947. }
  948. }
  949. if _, ok := fieldValue.Interface().(core.Conversion); ok {
  950. sqlType = core.SQLType{Name: core.Text}
  951. } else {
  952. sqlType = core.Type2SQLType(fieldType)
  953. }
  954. col = core.NewColumn(engine.ColumnMapper.Obj2Table(t.Field(i).Name),
  955. t.Field(i).Name, sqlType, sqlType.DefaultLength,
  956. sqlType.DefaultLength2, true)
  957. }
  958. if col.IsAutoIncrement {
  959. col.Nullable = false
  960. }
  961. table.AddColumn(col)
  962. if fieldType.Kind() == reflect.Int64 && (strings.ToUpper(col.FieldName) == "ID" || strings.HasSuffix(strings.ToUpper(col.FieldName), ".ID")) {
  963. idFieldColName = col.Name
  964. }
  965. } // end for
  966. if idFieldColName != "" && len(table.PrimaryKeys) == 0 {
  967. col := table.GetColumn(idFieldColName)
  968. col.IsPrimaryKey = true
  969. col.IsAutoIncrement = true
  970. col.Nullable = false
  971. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  972. table.AutoIncrement = col.Name
  973. }
  974. if hasCacheTag {
  975. if engine.Cacher != nil { // !nash! use engine's cacher if provided
  976. engine.logger.Info("enable cache on table:", table.Name)
  977. table.Cacher = engine.Cacher
  978. } else {
  979. engine.logger.Info("enable LRU cache on table:", table.Name)
  980. table.Cacher = NewLRUCacher2(NewMemoryStore(), time.Hour, 10000) // !nashtsai! HACK use LRU cacher for now
  981. }
  982. }
  983. if hasNoCacheTag {
  984. engine.logger.Info("no cache on table:", table.Name)
  985. table.Cacher = nil
  986. }
  987. return table
  988. }
  989. // IsTableEmpty if a table has any reocrd
  990. func (engine *Engine) IsTableEmpty(bean interface{}) (bool, error) {
  991. session := engine.NewSession()
  992. defer session.Close()
  993. return session.IsTableEmpty(bean)
  994. }
  995. // IsTableExist if a table is exist
  996. func (engine *Engine) IsTableExist(beanOrTableName interface{}) (bool, error) {
  997. session := engine.NewSession()
  998. defer session.Close()
  999. return session.IsTableExist(beanOrTableName)
  1000. }
  1001. // IdOf get id from one struct
  1002. //
  1003. // Deprecated: use IDOf instead.
  1004. func (engine *Engine) IdOf(bean interface{}) core.PK {
  1005. return engine.IDOf(bean)
  1006. }
  1007. // IDOf get id from one struct
  1008. func (engine *Engine) IDOf(bean interface{}) core.PK {
  1009. return engine.IdOfV(reflect.ValueOf(bean))
  1010. }
  1011. // IdOfV get id from one value of struct
  1012. //
  1013. // Deprecated: use IDOfV instead.
  1014. func (engine *Engine) IdOfV(rv reflect.Value) core.PK {
  1015. return engine.IDOfV(rv)
  1016. }
  1017. // IDOfV get id from one value of struct
  1018. func (engine *Engine) IDOfV(rv reflect.Value) core.PK {
  1019. v := reflect.Indirect(rv)
  1020. table := engine.autoMapType(v)
  1021. pk := make([]interface{}, len(table.PrimaryKeys))
  1022. for i, col := range table.PKColumns() {
  1023. pkField := v.FieldByName(col.FieldName)
  1024. switch pkField.Kind() {
  1025. case reflect.String:
  1026. pk[i] = pkField.String()
  1027. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1028. pk[i] = pkField.Int()
  1029. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  1030. pk[i] = pkField.Uint()
  1031. }
  1032. }
  1033. return core.PK(pk)
  1034. }
  1035. // CreateIndexes create indexes
  1036. func (engine *Engine) CreateIndexes(bean interface{}) error {
  1037. session := engine.NewSession()
  1038. defer session.Close()
  1039. return session.CreateIndexes(bean)
  1040. }
  1041. // CreateUniques create uniques
  1042. func (engine *Engine) CreateUniques(bean interface{}) error {
  1043. session := engine.NewSession()
  1044. defer session.Close()
  1045. return session.CreateUniques(bean)
  1046. }
  1047. func (engine *Engine) getCacher2(table *core.Table) core.Cacher {
  1048. return table.Cacher
  1049. }
  1050. func (engine *Engine) getCacher(v reflect.Value) core.Cacher {
  1051. if table := engine.autoMapType(v); table != nil {
  1052. return table.Cacher
  1053. }
  1054. return engine.Cacher
  1055. }
  1056. // ClearCacheBean if enabled cache, clear the cache bean
  1057. func (engine *Engine) ClearCacheBean(bean interface{}, id string) error {
  1058. v := rValue(bean)
  1059. t := v.Type()
  1060. if t.Kind() != reflect.Struct {
  1061. return errors.New("error params")
  1062. }
  1063. tableName := engine.tbName(v)
  1064. table := engine.autoMapType(v)
  1065. cacher := table.Cacher
  1066. if cacher == nil {
  1067. cacher = engine.Cacher
  1068. }
  1069. if cacher != nil {
  1070. cacher.ClearIds(tableName)
  1071. cacher.DelBean(tableName, id)
  1072. }
  1073. return nil
  1074. }
  1075. // ClearCache if enabled cache, clear some tables' cache
  1076. func (engine *Engine) ClearCache(beans ...interface{}) error {
  1077. for _, bean := range beans {
  1078. v := rValue(bean)
  1079. t := v.Type()
  1080. if t.Kind() != reflect.Struct {
  1081. return errors.New("error params")
  1082. }
  1083. tableName := engine.tbName(v)
  1084. table := engine.autoMapType(v)
  1085. cacher := table.Cacher
  1086. if cacher == nil {
  1087. cacher = engine.Cacher
  1088. }
  1089. if cacher != nil {
  1090. cacher.ClearIds(tableName)
  1091. cacher.ClearBeans(tableName)
  1092. }
  1093. }
  1094. return nil
  1095. }
  1096. // Sync the new struct changes to database, this method will automatically add
  1097. // table, column, index, unique. but will not delete or change anything.
  1098. // If you change some field, you should change the database manually.
  1099. func (engine *Engine) Sync(beans ...interface{}) error {
  1100. for _, bean := range beans {
  1101. v := rValue(bean)
  1102. tableName := engine.tbName(v)
  1103. table := engine.autoMapType(v)
  1104. s := engine.NewSession()
  1105. defer s.Close()
  1106. isExist, err := s.Table(bean).isTableExist(tableName)
  1107. if err != nil {
  1108. return err
  1109. }
  1110. if !isExist {
  1111. err = engine.CreateTables(bean)
  1112. if err != nil {
  1113. return err
  1114. }
  1115. }
  1116. /*isEmpty, err := engine.IsEmptyTable(bean)
  1117. if err != nil {
  1118. return err
  1119. }*/
  1120. var isEmpty bool
  1121. if isEmpty {
  1122. err = engine.DropTables(bean)
  1123. if err != nil {
  1124. return err
  1125. }
  1126. err = engine.CreateTables(bean)
  1127. if err != nil {
  1128. return err
  1129. }
  1130. } else {
  1131. for _, col := range table.Columns() {
  1132. isExist, err := engine.dialect.IsColumnExist(tableName, col.Name)
  1133. if err != nil {
  1134. return err
  1135. }
  1136. if !isExist {
  1137. session := engine.NewSession()
  1138. session.Statement.setRefValue(v)
  1139. defer session.Close()
  1140. err = session.addColumn(col.Name)
  1141. if err != nil {
  1142. return err
  1143. }
  1144. }
  1145. }
  1146. for name, index := range table.Indexes {
  1147. session := engine.NewSession()
  1148. session.Statement.setRefValue(v)
  1149. defer session.Close()
  1150. if index.Type == core.UniqueType {
  1151. //isExist, err := session.isIndexExist(table.Name, name, true)
  1152. isExist, err := session.isIndexExist2(tableName, index.Cols, true)
  1153. if err != nil {
  1154. return err
  1155. }
  1156. if !isExist {
  1157. session := engine.NewSession()
  1158. session.Statement.setRefValue(v)
  1159. defer session.Close()
  1160. err = session.addUnique(tableName, name)
  1161. if err != nil {
  1162. return err
  1163. }
  1164. }
  1165. } else if index.Type == core.IndexType {
  1166. isExist, err := session.isIndexExist2(tableName, index.Cols, false)
  1167. if err != nil {
  1168. return err
  1169. }
  1170. if !isExist {
  1171. session := engine.NewSession()
  1172. session.Statement.setRefValue(v)
  1173. defer session.Close()
  1174. err = session.addIndex(tableName, name)
  1175. if err != nil {
  1176. return err
  1177. }
  1178. }
  1179. } else {
  1180. return errors.New("unknow index type")
  1181. }
  1182. }
  1183. }
  1184. }
  1185. return nil
  1186. }
  1187. // Sync2 synchronize structs to database tables
  1188. func (engine *Engine) Sync2(beans ...interface{}) error {
  1189. s := engine.NewSession()
  1190. defer s.Close()
  1191. return s.Sync2(beans...)
  1192. }
  1193. func (engine *Engine) unMap(beans ...interface{}) (e error) {
  1194. engine.mutex.Lock()
  1195. defer engine.mutex.Unlock()
  1196. for _, bean := range beans {
  1197. t := rType(bean)
  1198. if _, ok := engine.Tables[t]; ok {
  1199. delete(engine.Tables, t)
  1200. }
  1201. }
  1202. return
  1203. }
  1204. // Drop all mapped table
  1205. func (engine *Engine) dropAll() error {
  1206. session := engine.NewSession()
  1207. defer session.Close()
  1208. err := session.Begin()
  1209. if err != nil {
  1210. return err
  1211. }
  1212. err = session.dropAll()
  1213. if err != nil {
  1214. session.Rollback()
  1215. return err
  1216. }
  1217. return session.Commit()
  1218. }
  1219. // CreateTables create tabls according bean
  1220. func (engine *Engine) CreateTables(beans ...interface{}) error {
  1221. session := engine.NewSession()
  1222. defer session.Close()
  1223. err := session.Begin()
  1224. if err != nil {
  1225. return err
  1226. }
  1227. for _, bean := range beans {
  1228. err = session.CreateTable(bean)
  1229. if err != nil {
  1230. session.Rollback()
  1231. return err
  1232. }
  1233. }
  1234. return session.Commit()
  1235. }
  1236. // DropTables drop specify tables
  1237. func (engine *Engine) DropTables(beans ...interface{}) error {
  1238. session := engine.NewSession()
  1239. defer session.Close()
  1240. err := session.Begin()
  1241. if err != nil {
  1242. return err
  1243. }
  1244. for _, bean := range beans {
  1245. err = session.DropTable(bean)
  1246. if err != nil {
  1247. session.Rollback()
  1248. return err
  1249. }
  1250. }
  1251. return session.Commit()
  1252. }
  1253. func (engine *Engine) createAll() error {
  1254. session := engine.NewSession()
  1255. defer session.Close()
  1256. return session.createAll()
  1257. }
  1258. // Exec raw sql
  1259. func (engine *Engine) Exec(sql string, args ...interface{}) (sql.Result, error) {
  1260. session := engine.NewSession()
  1261. defer session.Close()
  1262. return session.Exec(sql, args...)
  1263. }
  1264. // Query a raw sql and return records as []map[string][]byte
  1265. func (engine *Engine) Query(sql string, paramStr ...interface{}) (resultsSlice []map[string][]byte, err error) {
  1266. session := engine.NewSession()
  1267. defer session.Close()
  1268. return session.Query(sql, paramStr...)
  1269. }
  1270. // Insert one or more records
  1271. func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
  1272. session := engine.NewSession()
  1273. defer session.Close()
  1274. return session.Insert(beans...)
  1275. }
  1276. // InsertOne insert only one record
  1277. func (engine *Engine) InsertOne(bean interface{}) (int64, error) {
  1278. session := engine.NewSession()
  1279. defer session.Close()
  1280. return session.InsertOne(bean)
  1281. }
  1282. // Update records, bean's non-empty fields are updated contents,
  1283. // condiBean' non-empty filds are conditions
  1284. // CAUTION:
  1285. // 1.bool will defaultly be updated content nor conditions
  1286. // You should call UseBool if you have bool to use.
  1287. // 2.float32 & float64 may be not inexact as conditions
  1288. func (engine *Engine) Update(bean interface{}, condiBeans ...interface{}) (int64, error) {
  1289. session := engine.NewSession()
  1290. defer session.Close()
  1291. return session.Update(bean, condiBeans...)
  1292. }
  1293. // Delete records, bean's non-empty fields are conditions
  1294. func (engine *Engine) Delete(bean interface{}) (int64, error) {
  1295. session := engine.NewSession()
  1296. defer session.Close()
  1297. return session.Delete(bean)
  1298. }
  1299. // Get retrieve one record from table, bean's non-empty fields
  1300. // are conditions
  1301. func (engine *Engine) Get(bean interface{}) (bool, error) {
  1302. session := engine.NewSession()
  1303. defer session.Close()
  1304. return session.Get(bean)
  1305. }
  1306. // Find retrieve records from table, condiBeans's non-empty fields
  1307. // are conditions. beans could be []Struct, []*Struct, map[int64]Struct
  1308. // map[int64]*Struct
  1309. func (engine *Engine) Find(beans interface{}, condiBeans ...interface{}) error {
  1310. session := engine.NewSession()
  1311. defer session.Close()
  1312. return session.Find(beans, condiBeans...)
  1313. }
  1314. // Iterate record by record handle records from table, bean's non-empty fields
  1315. // are conditions.
  1316. func (engine *Engine) Iterate(bean interface{}, fun IterFunc) error {
  1317. session := engine.NewSession()
  1318. defer session.Close()
  1319. return session.Iterate(bean, fun)
  1320. }
  1321. // Rows return sql.Rows compatible Rows obj, as a forward Iterator object for iterating record by record, bean's non-empty fields
  1322. // are conditions.
  1323. func (engine *Engine) Rows(bean interface{}) (*Rows, error) {
  1324. session := engine.NewSession()
  1325. return session.Rows(bean)
  1326. }
  1327. // Count counts the records. bean's non-empty fields are conditions.
  1328. func (engine *Engine) Count(bean interface{}) (int64, error) {
  1329. session := engine.NewSession()
  1330. defer session.Close()
  1331. return session.Count(bean)
  1332. }
  1333. // Sum sum the records by some column. bean's non-empty fields are conditions.
  1334. func (engine *Engine) Sum(bean interface{}, colName string) (float64, error) {
  1335. session := engine.NewSession()
  1336. defer session.Close()
  1337. return session.Sum(bean, colName)
  1338. }
  1339. // Sums sum the records by some columns. bean's non-empty fields are conditions.
  1340. func (engine *Engine) Sums(bean interface{}, colNames ...string) ([]float64, error) {
  1341. session := engine.NewSession()
  1342. defer session.Close()
  1343. return session.Sums(bean, colNames...)
  1344. }
  1345. // SumsInt like Sums but return slice of int64 instead of float64.
  1346. func (engine *Engine) SumsInt(bean interface{}, colNames ...string) ([]int64, error) {
  1347. session := engine.NewSession()
  1348. defer session.Close()
  1349. return session.SumsInt(bean, colNames...)
  1350. }
  1351. // ImportFile SQL DDL file
  1352. func (engine *Engine) ImportFile(ddlPath string) ([]sql.Result, error) {
  1353. file, err := os.Open(ddlPath)
  1354. if err != nil {
  1355. return nil, err
  1356. }
  1357. defer file.Close()
  1358. return engine.Import(file)
  1359. }
  1360. // Import SQL DDL from io.Reader
  1361. func (engine *Engine) Import(r io.Reader) ([]sql.Result, error) {
  1362. var results []sql.Result
  1363. var lastError error
  1364. scanner := bufio.NewScanner(r)
  1365. semiColSpliter := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
  1366. if atEOF && len(data) == 0 {
  1367. return 0, nil, nil
  1368. }
  1369. if i := bytes.IndexByte(data, ';'); i >= 0 {
  1370. return i + 1, data[0:i], nil
  1371. }
  1372. // If we're at EOF, we have a final, non-terminated line. Return it.
  1373. if atEOF {
  1374. return len(data), data, nil
  1375. }
  1376. // Request more data.
  1377. return 0, nil, nil
  1378. }
  1379. scanner.Split(semiColSpliter)
  1380. for scanner.Scan() {
  1381. query := strings.Trim(scanner.Text(), " \t\n\r")
  1382. if len(query) > 0 {
  1383. engine.logSQL(query)
  1384. result, err := engine.DB().Exec(query)
  1385. results = append(results, result)
  1386. if err != nil {
  1387. return nil, err
  1388. //lastError = err
  1389. }
  1390. }
  1391. }
  1392. return results, lastError
  1393. }
  1394. // TZTime change one time to xorm time location
  1395. func (engine *Engine) TZTime(t time.Time) time.Time {
  1396. if !t.IsZero() { // if time is not initialized it's not suitable for Time.In()
  1397. return t.In(engine.TZLocation)
  1398. }
  1399. return t
  1400. }
  1401. // NowTime return current time
  1402. func (engine *Engine) NowTime(sqlTypeName string) interface{} {
  1403. t := time.Now()
  1404. return engine.FormatTime(sqlTypeName, t)
  1405. }
  1406. // NowTime2 return current time
  1407. func (engine *Engine) NowTime2(sqlTypeName string) (interface{}, time.Time) {
  1408. t := time.Now()
  1409. return engine.FormatTime(sqlTypeName, t), t
  1410. }
  1411. // FormatTime format time
  1412. func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}) {
  1413. return engine.formatTime(engine.TZLocation, sqlTypeName, t)
  1414. }
  1415. func (engine *Engine) formatColTime(col *core.Column, t time.Time) (v interface{}) {
  1416. if col.DisableTimeZone {
  1417. return engine.formatTime(nil, col.SQLType.Name, t)
  1418. } else if col.TimeZone != nil {
  1419. return engine.formatTime(col.TimeZone, col.SQLType.Name, t)
  1420. }
  1421. return engine.formatTime(engine.TZLocation, col.SQLType.Name, t)
  1422. }
  1423. func (engine *Engine) formatTime(tz *time.Location, sqlTypeName string, t time.Time) (v interface{}) {
  1424. if engine.dialect.DBType() == core.ORACLE {
  1425. return t
  1426. }
  1427. if tz != nil {
  1428. t = t.In(tz)
  1429. } else {
  1430. t = engine.TZTime(t)
  1431. }
  1432. switch sqlTypeName {
  1433. case core.Time:
  1434. s := t.Format("2006-01-02 15:04:05") //time.RFC3339
  1435. v = s[11:19]
  1436. case core.Date:
  1437. v = t.Format("2006-01-02")
  1438. case core.DateTime, core.TimeStamp:
  1439. if engine.dialect.DBType() == "ql" {
  1440. v = t
  1441. } else if engine.dialect.DBType() == "sqlite3" {
  1442. v = t.UTC().Format("2006-01-02 15:04:05")
  1443. } else {
  1444. v = t.Format("2006-01-02 15:04:05")
  1445. }
  1446. case core.TimeStampz:
  1447. if engine.dialect.DBType() == core.MSSQL {
  1448. v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
  1449. } else if engine.DriverName() == "mssql" {
  1450. v = t
  1451. } else {
  1452. v = t.Format(time.RFC3339Nano)
  1453. }
  1454. case core.BigInt, core.Int:
  1455. v = t.Unix()
  1456. default:
  1457. v = t
  1458. }
  1459. return
  1460. }
  1461. // Unscoped always disable struct tag "deleted"
  1462. func (engine *Engine) Unscoped() *Session {
  1463. session := engine.NewSession()
  1464. session.IsAutoClose = true
  1465. return session.Unscoped()
  1466. }