session_schema.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "database/sql"
  7. "errors"
  8. "fmt"
  9. "reflect"
  10. "strings"
  11. "github.com/go-xorm/core"
  12. )
  13. // Ping test if database is ok
  14. func (session *Session) Ping() error {
  15. defer session.resetStatement()
  16. if session.IsAutoClose {
  17. defer session.Close()
  18. }
  19. return session.DB().Ping()
  20. }
  21. // CreateTable create a table according a bean
  22. func (session *Session) CreateTable(bean interface{}) error {
  23. v := rValue(bean)
  24. if err := session.Statement.setRefValue(v); err != nil {
  25. return err
  26. }
  27. defer session.resetStatement()
  28. if session.IsAutoClose {
  29. defer session.Close()
  30. }
  31. return session.createOneTable()
  32. }
  33. // CreateIndexes create indexes
  34. func (session *Session) CreateIndexes(bean interface{}) error {
  35. v := rValue(bean)
  36. if err := session.Statement.setRefValue(v); err != nil {
  37. return err
  38. }
  39. defer session.resetStatement()
  40. if session.IsAutoClose {
  41. defer session.Close()
  42. }
  43. sqls := session.Statement.genIndexSQL()
  44. for _, sqlStr := range sqls {
  45. _, err := session.exec(sqlStr)
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. return nil
  51. }
  52. // CreateUniques create uniques
  53. func (session *Session) CreateUniques(bean interface{}) error {
  54. v := rValue(bean)
  55. if err := session.Statement.setRefValue(v); err != nil {
  56. return err
  57. }
  58. defer session.resetStatement()
  59. if session.IsAutoClose {
  60. defer session.Close()
  61. }
  62. sqls := session.Statement.genUniqueSQL()
  63. for _, sqlStr := range sqls {
  64. _, err := session.exec(sqlStr)
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. return nil
  70. }
  71. func (session *Session) createOneTable() error {
  72. sqlStr := session.Statement.genCreateTableSQL()
  73. _, err := session.exec(sqlStr)
  74. return err
  75. }
  76. // to be deleted
  77. func (session *Session) createAll() error {
  78. if session.IsAutoClose {
  79. defer session.Close()
  80. }
  81. for _, table := range session.Engine.Tables {
  82. session.Statement.RefTable = table
  83. session.Statement.tableName = table.Name
  84. err := session.createOneTable()
  85. session.resetStatement()
  86. if err != nil {
  87. return err
  88. }
  89. }
  90. return nil
  91. }
  92. // DropIndexes drop indexes
  93. func (session *Session) DropIndexes(bean interface{}) error {
  94. v := rValue(bean)
  95. if err := session.Statement.setRefValue(v); err != nil {
  96. return err
  97. }
  98. defer session.resetStatement()
  99. if session.IsAutoClose {
  100. defer session.Close()
  101. }
  102. sqls := session.Statement.genDelIndexSQL()
  103. for _, sqlStr := range sqls {
  104. _, err := session.exec(sqlStr)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. return nil
  110. }
  111. // DropTable drop table will drop table if exist, if drop failed, it will return error
  112. func (session *Session) DropTable(beanOrTableName interface{}) error {
  113. tableName, err := session.Engine.tableName(beanOrTableName)
  114. if err != nil {
  115. return err
  116. }
  117. var needDrop = true
  118. if !session.Engine.dialect.SupportDropIfExists() {
  119. sqlStr, args := session.Engine.dialect.TableCheckSql(tableName)
  120. results, err := session.query(sqlStr, args...)
  121. if err != nil {
  122. return err
  123. }
  124. needDrop = len(results) > 0
  125. }
  126. if needDrop {
  127. sqlStr := session.Engine.Dialect().DropTableSql(tableName)
  128. _, err = session.exec(sqlStr)
  129. return err
  130. }
  131. return nil
  132. }
  133. // IsTableExist if a table is exist
  134. func (session *Session) IsTableExist(beanOrTableName interface{}) (bool, error) {
  135. tableName, err := session.Engine.tableName(beanOrTableName)
  136. if err != nil {
  137. return false, err
  138. }
  139. return session.isTableExist(tableName)
  140. }
  141. func (session *Session) isTableExist(tableName string) (bool, error) {
  142. defer session.resetStatement()
  143. if session.IsAutoClose {
  144. defer session.Close()
  145. }
  146. sqlStr, args := session.Engine.dialect.TableCheckSql(tableName)
  147. results, err := session.query(sqlStr, args...)
  148. return len(results) > 0, err
  149. }
  150. // IsTableEmpty if table have any records
  151. func (session *Session) IsTableEmpty(bean interface{}) (bool, error) {
  152. v := rValue(bean)
  153. t := v.Type()
  154. if t.Kind() == reflect.String {
  155. return session.isTableEmpty(bean.(string))
  156. } else if t.Kind() == reflect.Struct {
  157. rows, err := session.Count(bean)
  158. return rows == 0, err
  159. }
  160. return false, errors.New("bean should be a struct or struct's point")
  161. }
  162. func (session *Session) isTableEmpty(tableName string) (bool, error) {
  163. defer session.resetStatement()
  164. if session.IsAutoClose {
  165. defer session.Close()
  166. }
  167. var total int64
  168. sqlStr := fmt.Sprintf("select count(*) from %s", session.Engine.Quote(tableName))
  169. err := session.DB().QueryRow(sqlStr).Scan(&total)
  170. session.saveLastSQL(sqlStr)
  171. if err != nil {
  172. if err == sql.ErrNoRows {
  173. err = nil
  174. }
  175. return true, err
  176. }
  177. return total == 0, nil
  178. }
  179. func (session *Session) isIndexExist(tableName, idxName string, unique bool) (bool, error) {
  180. defer session.resetStatement()
  181. if session.IsAutoClose {
  182. defer session.Close()
  183. }
  184. var idx string
  185. if unique {
  186. idx = uniqueName(tableName, idxName)
  187. } else {
  188. idx = indexName(tableName, idxName)
  189. }
  190. sqlStr, args := session.Engine.dialect.IndexCheckSql(tableName, idx)
  191. results, err := session.query(sqlStr, args...)
  192. return len(results) > 0, err
  193. }
  194. // find if index is exist according cols
  195. func (session *Session) isIndexExist2(tableName string, cols []string, unique bool) (bool, error) {
  196. defer session.resetStatement()
  197. if session.IsAutoClose {
  198. defer session.Close()
  199. }
  200. indexes, err := session.Engine.dialect.GetIndexes(tableName)
  201. if err != nil {
  202. return false, err
  203. }
  204. for _, index := range indexes {
  205. if sliceEq(index.Cols, cols) {
  206. if unique {
  207. return index.Type == core.UniqueType, nil
  208. }
  209. return index.Type == core.IndexType, nil
  210. }
  211. }
  212. return false, nil
  213. }
  214. func (session *Session) addColumn(colName string) error {
  215. defer session.resetStatement()
  216. if session.IsAutoClose {
  217. defer session.Close()
  218. }
  219. col := session.Statement.RefTable.GetColumn(colName)
  220. sql, args := session.Statement.genAddColumnStr(col)
  221. _, err := session.exec(sql, args...)
  222. return err
  223. }
  224. func (session *Session) addIndex(tableName, idxName string) error {
  225. defer session.resetStatement()
  226. if session.IsAutoClose {
  227. defer session.Close()
  228. }
  229. index := session.Statement.RefTable.Indexes[idxName]
  230. sqlStr := session.Engine.dialect.CreateIndexSql(tableName, index)
  231. _, err := session.exec(sqlStr)
  232. return err
  233. }
  234. func (session *Session) addUnique(tableName, uqeName string) error {
  235. defer session.resetStatement()
  236. if session.IsAutoClose {
  237. defer session.Close()
  238. }
  239. index := session.Statement.RefTable.Indexes[uqeName]
  240. sqlStr := session.Engine.dialect.CreateIndexSql(tableName, index)
  241. _, err := session.exec(sqlStr)
  242. return err
  243. }
  244. // To be deleted
  245. func (session *Session) dropAll() error {
  246. defer session.resetStatement()
  247. if session.IsAutoClose {
  248. defer session.Close()
  249. }
  250. for _, table := range session.Engine.Tables {
  251. session.Statement.Init()
  252. session.Statement.RefTable = table
  253. sqlStr := session.Engine.Dialect().DropTableSql(session.Statement.TableName())
  254. _, err := session.exec(sqlStr)
  255. if err != nil {
  256. return err
  257. }
  258. }
  259. return nil
  260. }
  261. // Sync2 synchronize structs to database tables
  262. func (session *Session) Sync2(beans ...interface{}) error {
  263. engine := session.Engine
  264. tables, err := engine.DBMetas()
  265. if err != nil {
  266. return err
  267. }
  268. var structTables []*core.Table
  269. for _, bean := range beans {
  270. v := rValue(bean)
  271. table, err := engine.mapType(v)
  272. if err != nil {
  273. return err
  274. }
  275. structTables = append(structTables, table)
  276. var tbName = session.tbNameNoSchema(table)
  277. var oriTable *core.Table
  278. for _, tb := range tables {
  279. if strings.EqualFold(tb.Name, tbName) {
  280. oriTable = tb
  281. break
  282. }
  283. }
  284. if oriTable == nil {
  285. err = session.StoreEngine(session.Statement.StoreEngine).CreateTable(bean)
  286. if err != nil {
  287. return err
  288. }
  289. err = session.CreateUniques(bean)
  290. if err != nil {
  291. return err
  292. }
  293. err = session.CreateIndexes(bean)
  294. if err != nil {
  295. return err
  296. }
  297. } else {
  298. for _, col := range table.Columns() {
  299. var oriCol *core.Column
  300. for _, col2 := range oriTable.Columns() {
  301. if strings.EqualFold(col.Name, col2.Name) {
  302. oriCol = col2
  303. break
  304. }
  305. }
  306. if oriCol != nil {
  307. expectedType := engine.dialect.SqlType(col)
  308. curType := engine.dialect.SqlType(oriCol)
  309. if expectedType != curType {
  310. if expectedType == core.Text &&
  311. strings.HasPrefix(curType, core.Varchar) {
  312. // currently only support mysql & postgres
  313. if engine.dialect.DBType() == core.MYSQL ||
  314. engine.dialect.DBType() == core.POSTGRES {
  315. engine.logger.Infof("Table %s column %s change type from %s to %s\n",
  316. tbName, col.Name, curType, expectedType)
  317. _, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col))
  318. } else {
  319. engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s\n",
  320. tbName, col.Name, curType, expectedType)
  321. }
  322. } else if strings.HasPrefix(curType, core.Varchar) && strings.HasPrefix(expectedType, core.Varchar) {
  323. if engine.dialect.DBType() == core.MYSQL {
  324. if oriCol.Length < col.Length {
  325. engine.logger.Infof("Table %s column %s change type from varchar(%d) to varchar(%d)\n",
  326. tbName, col.Name, oriCol.Length, col.Length)
  327. _, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col))
  328. }
  329. }
  330. } else {
  331. if !(strings.HasPrefix(curType, expectedType) && curType[len(expectedType)] == '(') {
  332. engine.logger.Warnf("Table %s column %s db type is %s, struct type is %s",
  333. tbName, col.Name, curType, expectedType)
  334. }
  335. }
  336. } else if expectedType == core.Varchar {
  337. if engine.dialect.DBType() == core.MYSQL {
  338. if oriCol.Length < col.Length {
  339. engine.logger.Infof("Table %s column %s change type from varchar(%d) to varchar(%d)\n",
  340. tbName, col.Name, oriCol.Length, col.Length)
  341. _, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col))
  342. }
  343. }
  344. }
  345. if col.Default != oriCol.Default {
  346. engine.logger.Warnf("Table %s Column %s db default is %s, struct default is %s",
  347. tbName, col.Name, oriCol.Default, col.Default)
  348. }
  349. if col.Nullable != oriCol.Nullable {
  350. engine.logger.Warnf("Table %s Column %s db nullable is %v, struct nullable is %v",
  351. tbName, col.Name, oriCol.Nullable, col.Nullable)
  352. }
  353. } else {
  354. session := engine.NewSession()
  355. session.Statement.RefTable = table
  356. session.Statement.tableName = tbName
  357. defer session.Close()
  358. err = session.addColumn(col.Name)
  359. }
  360. if err != nil {
  361. return err
  362. }
  363. }
  364. var foundIndexNames = make(map[string]bool)
  365. var addedNames = make(map[string]*core.Index)
  366. for name, index := range table.Indexes {
  367. var oriIndex *core.Index
  368. for name2, index2 := range oriTable.Indexes {
  369. if index.Equal(index2) {
  370. oriIndex = index2
  371. foundIndexNames[name2] = true
  372. break
  373. }
  374. }
  375. if oriIndex != nil {
  376. if oriIndex.Type != index.Type {
  377. sql := engine.dialect.DropIndexSql(tbName, oriIndex)
  378. _, err = engine.Exec(sql)
  379. if err != nil {
  380. return err
  381. }
  382. oriIndex = nil
  383. }
  384. }
  385. if oriIndex == nil {
  386. addedNames[name] = index
  387. }
  388. }
  389. for name2, index2 := range oriTable.Indexes {
  390. if _, ok := foundIndexNames[name2]; !ok {
  391. sql := engine.dialect.DropIndexSql(tbName, index2)
  392. _, err = engine.Exec(sql)
  393. if err != nil {
  394. return err
  395. }
  396. }
  397. }
  398. for name, index := range addedNames {
  399. if index.Type == core.UniqueType {
  400. session := engine.NewSession()
  401. session.Statement.RefTable = table
  402. session.Statement.tableName = tbName
  403. defer session.Close()
  404. err = session.addUnique(tbName, name)
  405. } else if index.Type == core.IndexType {
  406. session := engine.NewSession()
  407. session.Statement.RefTable = table
  408. session.Statement.tableName = tbName
  409. defer session.Close()
  410. err = session.addIndex(tbName, name)
  411. }
  412. if err != nil {
  413. return err
  414. }
  415. }
  416. }
  417. }
  418. for _, table := range tables {
  419. var oriTable *core.Table
  420. for _, structTable := range structTables {
  421. if strings.EqualFold(table.Name, session.tbNameNoSchema(structTable)) {
  422. oriTable = structTable
  423. break
  424. }
  425. }
  426. if oriTable == nil {
  427. //engine.LogWarnf("Table %s has no struct to mapping it", table.Name)
  428. continue
  429. }
  430. for _, colName := range table.ColumnsSeq() {
  431. if oriTable.GetColumn(colName) == nil {
  432. engine.logger.Warnf("Table %s has column %s but struct has not related field", table.Name, colName)
  433. }
  434. }
  435. }
  436. return nil
  437. }