sqlx.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package db
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "sync"
  6. // PostgreSQL
  7. _ "github.com/lib/pq"
  8. // MySQL
  9. _ "github.com/go-sql-driver/mysql"
  10. "github.com/jmoiron/sqlx"
  11. )
  12. // DB db
  13. type DB struct {
  14. Driver string
  15. c *sqlx.DB
  16. tx *sqlx.Tx
  17. }
  18. var (
  19. defaultConfig Option
  20. defaultDb *DB
  21. db *sqlx.DB
  22. once sync.Once
  23. )
  24. // SetDefaultOption default connect option
  25. func SetDefaultOption(opt Option) (err error) {
  26. defaultConfig = opt
  27. defaultDb = &DB{Driver: opt.Driver}
  28. defaultDb.c, err = connect()
  29. return
  30. }
  31. // ReleaseDefault release default connect
  32. func ReleaseDefault() error {
  33. if defaultDb != nil {
  34. if defaultDb.c != nil {
  35. defaultDb.c.Close()
  36. }
  37. }
  38. return nil
  39. }
  40. // New new DB dynamic object
  41. func New(opt Option) (dbx *DB, err error) {
  42. //dbx = &DB{}
  43. dbx.Driver = opt.Driver
  44. dbx.c, err = sqlx.Connect(opt.Driver, opt.DNS)
  45. if err != nil {
  46. return
  47. }
  48. dbx.c.SetMaxOpenConns(opt.MaxOpenConns)
  49. dbx.c.SetMaxIdleConns(opt.MaxIdle)
  50. dbx.c.SetConnMaxLifetime(opt.MaxLifetime)
  51. err = dbx.c.Ping()
  52. return
  53. }
  54. // Release release connect
  55. func Release(dbx *DB) (err error) {
  56. if dbx.c != nil {
  57. err = dbx.c.Close()
  58. }
  59. return
  60. }
  61. func connect() (dbx *sqlx.DB, err error) {
  62. once.Do(func() {
  63. db, err = sqlx.Connect(defaultConfig.Driver, defaultConfig.DNS)
  64. if err == nil {
  65. db.DB.SetMaxOpenConns(defaultConfig.MaxOpenConns)
  66. db.DB.SetMaxIdleConns(defaultConfig.MaxIdle)
  67. db.DB.SetConnMaxLifetime(defaultConfig.MaxLifetime)
  68. err = db.Ping()
  69. }
  70. })
  71. dbx = db
  72. return
  73. }
  74. // Connect connect to database
  75. func (d *DB) Connect() (err error) {
  76. if d.c != nil {
  77. return
  78. }
  79. d.c, err = connect()
  80. return
  81. }
  82. // Close close database connect
  83. func (d *DB) Close() {
  84. // use pool
  85. //d.c.Close()
  86. }
  87. // BeginTrans begin trans
  88. func (d *DB) BeginTrans() (err error) {
  89. d.c, err = connect()
  90. if err != nil {
  91. return
  92. }
  93. d.tx = d.c.MustBegin()
  94. return
  95. }
  96. // Commit commit
  97. func (d *DB) Commit() error {
  98. return d.tx.Commit()
  99. }
  100. // Rollback rollback
  101. func (d *DB) Rollback() error {
  102. return d.tx.Rollback()
  103. }
  104. // TransNamedExec trans execute
  105. func (d *DB) TransExec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
  106. rs, err := d.tx.Exec(query, args...)
  107. if err != nil {
  108. return
  109. }
  110. RowsAffected, _ = rs.RowsAffected()
  111. LastInsertId, _ = rs.LastInsertId()
  112. return
  113. }
  114. // TransNamedExec trans execute, named bindvars
  115. func (d *DB) TransNamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  116. rs, err := d.tx.NamedExec(query, args)
  117. if err != nil {
  118. return
  119. }
  120. RowsAffected, _ = rs.RowsAffected()
  121. LastInsertId, _ = rs.LastInsertId()
  122. return
  123. }
  124. // TransGet trans get row
  125. func (d *DB) TransGet(dest interface{}, query string, args ...interface{}) (err error) {
  126. d.tx.Get(dest, query, args...)
  127. return
  128. }
  129. // TransNamedGet trans get row, named bindvars
  130. func (d *DB) TransNamedGet(dest interface{}, query string, args interface{}) (err error) {
  131. var nstmt *sqlx.NamedStmt
  132. nstmt, err = d.tx.PrepareNamed(query)
  133. if err != nil {
  134. return
  135. }
  136. defer nstmt.Close()
  137. err = nstmt.Get(dest, args)
  138. return
  139. }
  140. // TransSelect trans get rows
  141. func (d *DB) TransSelect(dest interface{}, query string, args ...interface{}) (err error) {
  142. d.tx.Select(dest, query, args...)
  143. return
  144. }
  145. // Get get one
  146. func (d *DB) Get(dest interface{}, query string, args ...interface{}) (err error) {
  147. err = d.Connect()
  148. if err != nil {
  149. return
  150. }
  151. defer d.Close()
  152. err = d.c.Get(dest, query, args...)
  153. return
  154. }
  155. // Get get one, named bindvars
  156. func (d *DB) NamedGet(dest interface{}, query string, args interface{}) (err error) {
  157. err = d.Connect()
  158. if err != nil {
  159. return
  160. }
  161. defer d.Close()
  162. nstmt, err := d.c.PrepareNamed(query)
  163. if err != nil {
  164. return
  165. }
  166. defer nstmt.Close()
  167. err = nstmt.Get(dest, args)
  168. return
  169. }
  170. // Select select rows
  171. func (d *DB) Select(dest interface{}, query string, args ...interface{}) error {
  172. err := d.Connect()
  173. if err != nil {
  174. return err
  175. }
  176. defer d.Close()
  177. err = d.c.Select(dest, query, args...)
  178. return err
  179. }
  180. // NamedSelect select rows, named bindvars
  181. func (d *DB) NamedSelect(dest interface{}, query string, args interface{}) (err error) {
  182. err = d.Connect()
  183. if err != nil {
  184. return err
  185. }
  186. defer d.Close()
  187. nstmt, err := d.c.PrepareNamed(query)
  188. if err != nil {
  189. return err
  190. }
  191. defer nstmt.Close()
  192. err = nstmt.Select(dest, args)
  193. return err
  194. }
  195. // Exec exec
  196. func (d *DB) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
  197. err = d.Connect()
  198. if err != nil {
  199. return
  200. }
  201. defer d.Close()
  202. var rs sql.Result
  203. rs, err = d.c.Exec(query, args...)
  204. if err != nil {
  205. return
  206. }
  207. LastInsertId, _ = rs.LastInsertId()
  208. RowsAffected, _ = rs.RowsAffected()
  209. return
  210. }
  211. // NamedExec exec, named bindvars
  212. func (d *DB) NamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  213. err = d.Connect()
  214. if err != nil {
  215. return
  216. }
  217. defer d.Close()
  218. var rs sql.Result
  219. rs, err = d.c.NamedExec(query, args)
  220. if err != nil {
  221. return
  222. }
  223. LastInsertId, _ = rs.LastInsertId()
  224. RowsAffected, _ = rs.RowsAffected()
  225. return
  226. }
  227. // Limit MySQL/PostgreSQL limit
  228. func (d *DB) Limit(page, pagesize int) string {
  229. // MySQL limit n, size
  230. if d.Driver == DriverMySQL {
  231. return fmt.Sprintf(" LIMIT %d, %d", (page-1)*pagesize, pagesize)
  232. }
  233. // // PostgreSQL limit size offset n
  234. return fmt.Sprintf(" LIMIT %d OFFSET %d", pagesize, (page-1)*pagesize)
  235. }
  236. // BeginTrans begin trans
  237. func BeginTrans() (err error) {
  238. return defaultDb.BeginTrans()
  239. }
  240. // Commit commit
  241. func Commit() error {
  242. return defaultDb.Commit()
  243. }
  244. // Rollback rollback
  245. func Rollback() error {
  246. return defaultDb.Rollback()
  247. }
  248. // TransNamedExec trans execute
  249. func TransExec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
  250. return defaultDb.TransExec(query, args...)
  251. }
  252. // TransNamedExec trans execute, named bindvars
  253. func TransNamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  254. return defaultDb.TransNamedExec(query, args)
  255. }
  256. // TransGet trans get row
  257. func TransGet(dest interface{}, query string, args interface{}) (err error) {
  258. return defaultDb.TransGet(dest, query, args)
  259. }
  260. // TransNamedGet trans get row, named bindvars
  261. func TransNamedGet(dest interface{}, query string, args interface{}) (err error) {
  262. return defaultDb.TransNamedGet(dest, query, args)
  263. }
  264. // TransSelect trans get rows
  265. func TransSelect(dest interface{}, query string, args ...interface{}) (err error) {
  266. return defaultDb.TransSelect(dest, query, args...)
  267. }
  268. // Get get one
  269. func Get(dest interface{}, query string, args ...interface{}) error {
  270. return defaultDb.Get(dest, query, args...)
  271. }
  272. // Get get one, named bindvars
  273. func NamedGet(dest interface{}, query string, args interface{}) (err error) {
  274. return defaultDb.NamedGet(dest, query, args)
  275. }
  276. // Select select rows
  277. func Select(dest interface{}, query string, args ...interface{}) error {
  278. return defaultDb.Select(dest, query, args...)
  279. }
  280. // NamedSelect select rows, named bindvars
  281. func NamedSelect(dest interface{}, query string, args interface{}) (err error) {
  282. return defaultDb.NamedSelect(dest, query, args)
  283. }
  284. // Exec execute
  285. func Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
  286. return defaultDb.Exec(query, args...)
  287. }
  288. // NamedExec exec, named bindvars
  289. func NamedExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  290. return defaultDb.NamedExec(query, args)
  291. }
  292. // Limit MySQL/PostgreSQL limit
  293. func Limit(page, pagesize int) string {
  294. return defaultDb.Limit(page, pagesize)
  295. }