db.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package db
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "sync"
  7. "time"
  8. // PostgreSQL
  9. _ "github.com/lib/pq"
  10. // MySQL
  11. //_ "github.com/go-sql-driver/mysql"
  12. "github.com/jmoiron/sqlx"
  13. )
  14. var (
  15. config Config
  16. db *sqlx.DB
  17. //err error
  18. once sync.Once
  19. defaultDB *DB
  20. errNoneConnect = errors.New(`数据库连接错误`)
  21. )
  22. // DB define
  23. type DB struct {
  24. Driver string
  25. conn *sqlx.DB
  26. tx *sqlx.Tx
  27. }
  28. // SetConfig set
  29. func SetConfig(cfg Config) {
  30. config.Driver = cfg.Driver
  31. config.DNS = cfg.DNS
  32. config.MaxOpenConns = cfg.MaxOpenConns
  33. config.MaxIdle = cfg.MaxIdle
  34. config.MaxLifetime = cfg.MaxLifetime * time.Second
  35. defaultDB = &DB{Driver: config.Driver}
  36. }
  37. // New new DB object
  38. func New() *DB {
  39. return &DB{Driver: config.Driver}
  40. }
  41. // Release free db connect
  42. func Release() {
  43. if db != nil {
  44. db.Close()
  45. }
  46. }
  47. // NewConfig new DB dynamic object
  48. func NewConfig(config Config) (dbx *DB, err error) {
  49. //dbx = &DB{}
  50. dbx.Driver = config.Driver
  51. dbx.conn, err = sqlx.Connect(config.Driver, config.DNS)
  52. if err != nil {
  53. return
  54. }
  55. dbx.conn.SetMaxOpenConns(config.MaxOpenConns)
  56. dbx.conn.SetMaxIdleConns(config.MaxIdle)
  57. dbx.conn.SetConnMaxLifetime(config.MaxLifetime)
  58. err = dbx.conn.Ping()
  59. return
  60. }
  61. // ReleaseConfig free db connect
  62. func ReleaseConfig(dbx *DB) {
  63. if dbx.conn != nil {
  64. dbx.conn.Close()
  65. }
  66. }
  67. func connect() (dbx *sqlx.DB, err error) {
  68. once.Do(func() {
  69. db, err = sqlx.Connect(config.Driver, config.DNS)
  70. if err != nil {
  71. fmt.Println("Connect ERR", err)
  72. return
  73. }
  74. db.DB.SetMaxOpenConns(config.MaxOpenConns)
  75. db.DB.SetMaxIdleConns(config.MaxIdle)
  76. db.DB.SetConnMaxLifetime(config.MaxLifetime)
  77. err = db.Ping()
  78. if err != nil {
  79. fmt.Println("Connect Ping", err)
  80. return
  81. }
  82. })
  83. if db == nil {
  84. err = errNoneConnect
  85. return
  86. }
  87. dbx = db
  88. return
  89. }
  90. // Stats Stats returns database statistics.
  91. func (d *DB) Stats() (s sql.DBStats) {
  92. s = d.conn.DB.Stats()
  93. return
  94. }
  95. // Connect connect to database
  96. func (d *DB) Connect() (err error) {
  97. if d.conn != nil {
  98. return
  99. }
  100. d.conn, err = connect()
  101. return
  102. }
  103. // Close close database connect
  104. func (d *DB) Close() {
  105. // use pool
  106. //d.conn.Close()
  107. }
  108. // Ping Ping connect
  109. func (d *DB) Ping() (err error) {
  110. d.conn, err = connect()
  111. if err != nil {
  112. return
  113. }
  114. return d.conn.Ping()
  115. }
  116. // BeginTrans begin trans
  117. func (d *DB) BeginTrans() (err error) {
  118. d.conn, err = connect()
  119. if err != nil {
  120. return
  121. }
  122. d.tx, err = d.conn.Beginx()
  123. return
  124. }
  125. // Commit commit
  126. func (d *DB) Commit() (err error) {
  127. return d.tx.Commit()
  128. }
  129. // Rollback rollback
  130. func (d *DB) Rollback() (err error) {
  131. return d.tx.Rollback()
  132. }
  133. // TransExec trans execute
  134. func (d *DB) TransExec(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  135. if rs, err := d.tx.NamedExec(query, args); err == nil {
  136. RowsAffected, _ = rs.RowsAffected()
  137. LastInsertId, _ = rs.LastInsertId()
  138. }
  139. return
  140. }
  141. // TransUpdate trans update
  142. func (d *DB) TransUpdate(query string, args interface{}) (reply Reply) {
  143. var (
  144. err error
  145. rs sql.Result
  146. )
  147. if rs, err = d.tx.NamedExec(query, args); err == nil {
  148. a, _ := rs.RowsAffected()
  149. reply = ReplyOk(a, 0)
  150. } else {
  151. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  152. }
  153. return
  154. }
  155. // TransRow trans get row
  156. func (d *DB) TransRow(dest interface{}, query string, args interface{}) (err error) {
  157. nstmt, err := d.tx.PrepareNamed(query)
  158. if err != nil {
  159. return err
  160. }
  161. defer nstmt.Close()
  162. err = nstmt.Get(dest, args)
  163. //err = d.tx.Get(dest, query, args)
  164. return err
  165. }
  166. // Select select
  167. func (d *DB) Select(dest interface{}, query string, args ...interface{}) error {
  168. err := d.Connect()
  169. if err != nil {
  170. return err
  171. }
  172. defer d.Close()
  173. err = d.conn.Select(dest, query, args...)
  174. return err
  175. }
  176. // Rows get rows
  177. func (d *DB) Rows(dest interface{}, query string, args interface{}) error {
  178. err := d.Connect()
  179. if err != nil {
  180. return err
  181. }
  182. defer d.Close()
  183. nstmt, err := d.conn.PrepareNamed(query)
  184. if err != nil {
  185. return err
  186. }
  187. defer nstmt.Close()
  188. err = nstmt.Select(dest, args)
  189. return err
  190. }
  191. // Get get
  192. func (d *DB) Get(dest interface{}, query string, args ...interface{}) error {
  193. err := d.Connect()
  194. if err != nil {
  195. return err
  196. }
  197. defer d.Close()
  198. err = d.conn.Get(dest, query, args...)
  199. return err
  200. }
  201. // Row get row
  202. func (d *DB) Row(dest interface{}, query string, args interface{}) error {
  203. err := d.Connect()
  204. if err != nil {
  205. return err
  206. }
  207. defer d.Close()
  208. nstmt, err := d.conn.PrepareNamed(query)
  209. if err != nil {
  210. return err
  211. }
  212. defer nstmt.Close()
  213. err = nstmt.Get(dest, args)
  214. return err
  215. }
  216. // InsertReply insert and return DbReply
  217. func (d *DB) InsertReply(query string, args interface{}) (reply Reply) {
  218. var (
  219. err error
  220. rs sql.Result
  221. )
  222. err = d.Connect()
  223. if err != nil {
  224. reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
  225. return
  226. }
  227. defer d.Close()
  228. if rs, err = d.conn.NamedExec(query, args); err == nil {
  229. a, _ := rs.RowsAffected()
  230. n, _ := rs.LastInsertId()
  231. reply = ReplyOk(a, n)
  232. } else {
  233. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  234. }
  235. return
  236. }
  237. // UpdateReply update/delete and return DbReply
  238. func (d *DB) UpdateReply(query string, args interface{}) (reply Reply) {
  239. var (
  240. err error
  241. rs sql.Result
  242. )
  243. err = d.Connect()
  244. if err != nil {
  245. reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
  246. return
  247. }
  248. defer d.Close()
  249. if rs, err = d.conn.NamedExec(query, args); err == nil {
  250. a, _ := rs.RowsAffected()
  251. reply = ReplyOk(a, 0)
  252. } else {
  253. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  254. }
  255. return
  256. }
  257. // Insert insert into
  258. func (d *DB) Insert(query string, args interface{}) (LastInsertId, RowsAffected int64, err error) {
  259. err = d.Connect()
  260. if err != nil {
  261. return
  262. }
  263. defer d.Close()
  264. var rs sql.Result
  265. if rs, err = d.conn.NamedExec(query, args); err == nil {
  266. LastInsertId, _ = rs.LastInsertId()
  267. RowsAffected, _ = rs.RowsAffected()
  268. }
  269. return
  270. }
  271. // Update update/delete
  272. func (d *DB) Update(query string, args interface{}) (RowsAffected int64, err error) {
  273. err = d.Connect()
  274. if err != nil {
  275. return
  276. }
  277. defer d.Close()
  278. var rs sql.Result
  279. if rs, err = d.conn.NamedExec(query, args); err == nil {
  280. RowsAffected, _ = rs.RowsAffected()
  281. }
  282. return
  283. }
  284. // Exec exec
  285. func (d *DB) Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
  286. err = d.Connect()
  287. if err != nil {
  288. return
  289. }
  290. defer d.Close()
  291. var rs sql.Result
  292. if rs, err = d.conn.Exec(query, args...); err == nil {
  293. LastInsertId, _ = rs.LastInsertId()
  294. RowsAffected, _ = rs.RowsAffected()
  295. }
  296. return
  297. }
  298. // Limit MySQL limit
  299. func (d *DB) Limit(page, pagesize int) string {
  300. // MySQL limit 0, size
  301. if d.Driver == `mysql` {
  302. return fmt.Sprintf(" limit %d, %d", (page-1)*pagesize, pagesize)
  303. }
  304. // // PostgreSQL limit size offset 0
  305. return fmt.Sprintf(" limit %d offset %d", pagesize, (page-1)*pagesize)
  306. }
  307. // Ping ping connect
  308. func Ping() (err error) {
  309. defaultDB.conn, err = connect()
  310. if err != nil {
  311. return
  312. }
  313. err = defaultDB.Ping()
  314. return
  315. }
  316. // Select select
  317. func Select(dest interface{}, query string, args ...interface{}) (err error) {
  318. defaultDB.conn, err = connect()
  319. if err != nil {
  320. return err
  321. }
  322. err = defaultDB.conn.Select(dest, query, args...)
  323. return
  324. }
  325. // Rows get rows
  326. func Rows(dest interface{}, query string, args interface{}) (err error) {
  327. defaultDB.conn, err = connect()
  328. if err != nil {
  329. return err
  330. }
  331. nstmt, err := defaultDB.conn.PrepareNamed(query)
  332. if err != nil {
  333. return
  334. }
  335. defer nstmt.Close()
  336. err = nstmt.Select(dest, args)
  337. return
  338. }
  339. // Get get
  340. func Get(dest interface{}, query string, args ...interface{}) (err error) {
  341. defaultDB.conn, err = connect()
  342. if err != nil {
  343. return
  344. }
  345. err = defaultDB.conn.Get(dest, query, args...)
  346. return
  347. }
  348. // Row get row
  349. func Row(dest interface{}, query string, args interface{}) (err error) {
  350. defaultDB.conn, err = connect()
  351. if err != nil {
  352. return
  353. }
  354. nstmt, err := defaultDB.conn.PrepareNamed(query)
  355. if err != nil {
  356. return err
  357. }
  358. defer nstmt.Close()
  359. err = nstmt.Get(dest, args)
  360. return
  361. }
  362. func Exec(query string, args ...interface{}) (LastInsertId, RowsAffected int64, err error) {
  363. defaultDB.conn, err = connect()
  364. if err != nil {
  365. return
  366. }
  367. LastInsertId, RowsAffected, err = defaultDB.Exec(query, args...)
  368. return
  369. }