db.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. package db
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. // PostgreSQL
  8. _ "github.com/lib/pq"
  9. // MySQL
  10. _ "github.com/go-sql-driver/mysql"
  11. "github.com/jmoiron/sqlx"
  12. _ "github.com/mattn/go-sqlite3"
  13. )
  14. // DB define
  15. type DB struct {
  16. Driver string
  17. conn *sqlx.DB
  18. tx *sqlx.Tx
  19. }
  20. // Tx is an in-progress database transaction.
  21. type Tx struct {
  22. tx *sqlx.Tx
  23. }
  24. // New new DB object
  25. func New() *DB {
  26. return &DB{Driver: config.Driver}
  27. }
  28. // Release free db connect
  29. func Release() {
  30. if db != nil {
  31. db.Close()
  32. }
  33. }
  34. // NewConfig new DB dynamic object
  35. func NewConfig(config Config) (dbx *DB, err error) {
  36. dbx = &DB{}
  37. dbx.Driver = config.Driver
  38. dbx.conn, err = sqlx.Connect(config.Driver, config.DNS)
  39. if err != nil {
  40. return
  41. }
  42. dbx.conn.SetMaxOpenConns(config.MaxOpenConns)
  43. dbx.conn.SetMaxIdleConns(config.MaxIdle)
  44. dbx.conn.SetConnMaxIdleTime(config.MaxIdleTime)
  45. dbx.conn.SetConnMaxLifetime(config.MaxLifeTime)
  46. //err = dbx.conn.Ping()
  47. return
  48. }
  49. // ReleaseConfig free db connect
  50. func ReleaseConfig(dbx *DB) {
  51. if dbx.conn != nil {
  52. dbx.conn.Close()
  53. }
  54. }
  55. func connect() (dbx *sqlx.DB, err error) {
  56. once.Do(func() {
  57. db, err = sqlx.Open(config.Driver, config.DNS) // sqlx.Connect(config.Driver, config.DNS)
  58. if err != nil {
  59. return
  60. }
  61. db.DB.SetMaxOpenConns(config.MaxOpenConns)
  62. db.DB.SetMaxIdleConns(config.MaxIdle)
  63. db.DB.SetConnMaxIdleTime(config.MaxIdleTime)
  64. db.DB.SetConnMaxLifetime(config.MaxLifeTime)
  65. /*
  66. err = db.Ping()
  67. if err != nil {
  68. return
  69. }
  70. // */
  71. })
  72. if db == nil {
  73. err = ErrNoneConnect
  74. return
  75. }
  76. dbx = db
  77. return
  78. }
  79. func connectContext(ctx context.Context) (dbx *sqlx.DB, err error) {
  80. once.Do(func() {
  81. db, err = sqlx.ConnectContext(ctx, config.Driver, config.DNS)
  82. if err != nil {
  83. return
  84. }
  85. db.DB.SetMaxOpenConns(config.MaxOpenConns)
  86. db.DB.SetMaxIdleConns(config.MaxIdle)
  87. db.DB.SetConnMaxIdleTime(config.MaxIdleTime)
  88. db.DB.SetConnMaxLifetime(config.MaxLifeTime)
  89. })
  90. if db == nil {
  91. err = ErrNoneConnect
  92. return
  93. }
  94. dbx = db
  95. return
  96. }
  97. // Stats Stats returns database statistics.
  98. func (d *DB) Stats() (s sql.DBStats) {
  99. s = d.conn.DB.Stats()
  100. return
  101. }
  102. // Connect connect to database
  103. func (d *DB) Connect() (err error) {
  104. if d.conn != nil {
  105. return
  106. }
  107. d.conn, err = connect()
  108. return
  109. }
  110. // ConnectContext connect to database
  111. func (d *DB) ConnectContext(ctx context.Context) (err error) {
  112. if d.conn != nil {
  113. return
  114. }
  115. d.conn, err = connectContext(ctx)
  116. return
  117. }
  118. // Close close database connect
  119. func (d *DB) Close() {
  120. // use pool
  121. //d.conn.Close()
  122. }
  123. // Ping Ping connect
  124. func (d *DB) Ping() (err error) {
  125. d.conn, err = connect()
  126. if err != nil {
  127. return
  128. }
  129. return d.conn.Ping()
  130. }
  131. // PingContext Ping connect
  132. func (d *DB) PingContext(ctx context.Context) (err error) {
  133. d.conn, err = connectContext(ctx)
  134. if err != nil {
  135. return
  136. }
  137. return d.conn.PingContext(ctx)
  138. }
  139. // BeginTrans begin trans
  140. func (d *DB) BeginTrans() (err error) {
  141. d.conn, err = connect()
  142. if err != nil {
  143. return
  144. }
  145. d.tx, err = d.conn.Beginx()
  146. return
  147. }
  148. // Begin starts a transaction. The default isolation level is dependent on the driver.
  149. func (d *DB) Begin() (tx *Tx, err error) {
  150. d.conn, err = connect()
  151. if err != nil {
  152. return
  153. }
  154. tx = &Tx{}
  155. tx.tx, err = d.conn.Beginx()
  156. return
  157. }
  158. // BeginTransx begin trans
  159. func (d *DB) BeginTransx(ctx context.Context, opts *sql.TxOptions) (err error) {
  160. d.conn, err = connectContext(ctx)
  161. if err != nil {
  162. return
  163. }
  164. d.tx, err = d.conn.BeginTxx(ctx, opts)
  165. return
  166. }
  167. // BeginTx starts a transaction.
  168. func (d *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (tx *Tx, err error) {
  169. d.conn, err = connectContext(ctx)
  170. if err != nil {
  171. return
  172. }
  173. tx = &Tx{}
  174. tx.tx, err = d.conn.BeginTxx(ctx, opts)
  175. return
  176. }
  177. // Commit commit
  178. func (d *DB) Commit() (err error) {
  179. return d.tx.Commit()
  180. }
  181. // Rollback rollback
  182. func (d *DB) Rollback() (err error) {
  183. return d.tx.Rollback()
  184. }
  185. // TransExec trans execute with named args
  186. func (d *DB) TransExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  187. var rs sql.Result
  188. if rs, err = d.tx.NamedExec(query, args); err == nil {
  189. rowsAffected, _ = rs.RowsAffected()
  190. lastInsertId, _ = rs.LastInsertId()
  191. }
  192. return
  193. }
  194. // TransExec trans execute with named args
  195. func (d *DB) TransExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  196. var rs sql.Result
  197. if rs, err = d.tx.NamedExecContext(ctx, query, args); err == nil {
  198. rowsAffected, _ = rs.RowsAffected()
  199. lastInsertId, _ = rs.LastInsertId()
  200. }
  201. return
  202. }
  203. // TransUpdate trans update
  204. func (d *DB) TransUpdate(query string, args interface{}) (reply Reply) {
  205. var (
  206. err error
  207. rs sql.Result
  208. )
  209. if rs, err = d.tx.NamedExec(query, args); err == nil {
  210. a, _ := rs.RowsAffected()
  211. reply = ReplyOk(a, 0)
  212. } else {
  213. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  214. }
  215. return
  216. }
  217. // TransRow trans get row
  218. func (d *DB) TransRow(dest interface{}, query string, args interface{}) (err error) {
  219. nstmt := &sqlx.NamedStmt{}
  220. nstmt, err = d.tx.PrepareNamed(query)
  221. if err != nil {
  222. return err
  223. }
  224. defer nstmt.Close()
  225. err = nstmt.Get(dest, args)
  226. //err = d.tx.Get(dest, query, args)
  227. return err
  228. }
  229. // Preparex a statement within a transaction.
  230. func (d *DB) Preparex(query string) (stmt *Stmt, err error) {
  231. stmt, err = d.conn.Preparex(query)
  232. return
  233. }
  234. // PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.
  235. func (d *DB) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error) {
  236. stmt, err = d.conn.PreparexContext(ctx, query)
  237. return
  238. }
  239. // PrepareNamed returns an sqlx.NamedStmt
  240. func (d *DB) PrepareNamed(query string) (stmt *NamedStmt, err error) {
  241. stmt, err = d.conn.PrepareNamed(query)
  242. return
  243. }
  244. // PrepareNamedContext returns an sqlx.NamedStmt
  245. func (d *DB) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error) {
  246. stmt, err = d.conn.PrepareNamedContext(ctx, query)
  247. return
  248. }
  249. // Select select
  250. func (d *DB) Select(dest interface{}, query string, args ...interface{}) (err error) {
  251. err = d.Connect()
  252. if err != nil {
  253. return err
  254. }
  255. defer d.Close()
  256. err = d.conn.Select(dest, query, args...)
  257. return
  258. }
  259. // SelectContext select
  260. func (d *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
  261. err = d.ConnectContext(ctx)
  262. if err != nil {
  263. return
  264. }
  265. defer d.Close()
  266. err = d.conn.SelectContext(ctx, dest, query, args...)
  267. return
  268. }
  269. // Query get rows with named args, Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
  270. func (d *DB) Query(dest interface{}, query string, args interface{}) (err error) {
  271. err = d.Connect()
  272. if err != nil {
  273. return err
  274. }
  275. defer d.Close()
  276. nstmt := &sqlx.NamedStmt{}
  277. nstmt, err = d.conn.PrepareNamed(query)
  278. if err != nil {
  279. return
  280. }
  281. defer nstmt.Close()
  282. err = nstmt.Select(dest, args)
  283. return
  284. }
  285. // QueryContext get rows with named args, QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
  286. func (d *DB) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  287. err = d.ConnectContext(ctx)
  288. if err != nil {
  289. return
  290. }
  291. defer d.Close()
  292. nstmt := &sqlx.NamedStmt{}
  293. nstmt, err = d.conn.PrepareNamedContext(ctx, query)
  294. if err != nil {
  295. return
  296. }
  297. defer nstmt.Close()
  298. err = nstmt.SelectContext(ctx, dest, args)
  299. return
  300. }
  301. // Rows get rows with named args
  302. func (d *DB) Rows(dest interface{}, query string, args interface{}) (err error) {
  303. err = d.Connect()
  304. if err != nil {
  305. return
  306. }
  307. defer d.Close()
  308. nstmt := &sqlx.NamedStmt{}
  309. nstmt, err = d.conn.PrepareNamed(query)
  310. if err != nil {
  311. return
  312. }
  313. defer nstmt.Close()
  314. err = nstmt.Select(dest, args)
  315. return
  316. }
  317. // RowsContext get rows with named args
  318. func (d *DB) RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  319. err = d.ConnectContext(ctx)
  320. if err != nil {
  321. return
  322. }
  323. defer d.Close()
  324. nstmt := &sqlx.NamedStmt{}
  325. nstmt, err = d.conn.PrepareNamedContext(ctx, query)
  326. if err != nil {
  327. return err
  328. }
  329. defer nstmt.Close()
  330. err = nstmt.SelectContext(ctx, dest, args)
  331. return
  332. }
  333. // QueryRow get row, QueryRow executes a query that is expected to return at most one row.
  334. func (d *DB) QueryRow(dest interface{}, query string, args ...interface{}) (err error) {
  335. err = d.Connect()
  336. if err != nil {
  337. return err
  338. }
  339. defer d.Close()
  340. err = d.conn.Get(dest, query, args...)
  341. return
  342. }
  343. // QueryRowContext get row, QueryRowContext executes a query that is expected to return at most one row.
  344. func (d *DB) QueryRowContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
  345. err = d.ConnectContext(ctx)
  346. if err != nil {
  347. return
  348. }
  349. defer d.Close()
  350. err = d.conn.GetContext(ctx, dest, query, args...)
  351. return
  352. }
  353. // Get get row, QueryRow executes a query that is expected to return at most one row.
  354. func (d *DB) Get(dest interface{}, query string, args ...interface{}) (err error) {
  355. err = d.Connect()
  356. if err != nil {
  357. return
  358. }
  359. defer d.Close()
  360. err = d.conn.Get(dest, query, args...)
  361. return
  362. }
  363. // GetContext get
  364. func (d *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) (err error) {
  365. err = d.ConnectContext(ctx)
  366. if err != nil {
  367. return
  368. }
  369. defer d.Close()
  370. err = d.conn.GetContext(ctx, dest, query, args...)
  371. return
  372. }
  373. // Row get row with named args
  374. func (d *DB) Row(dest interface{}, query string, args interface{}) (err error) {
  375. err = d.Connect()
  376. if err != nil {
  377. return
  378. }
  379. defer d.Close()
  380. nstmt := &sqlx.NamedStmt{}
  381. nstmt, err = d.conn.PrepareNamed(query)
  382. if err != nil {
  383. return
  384. }
  385. defer nstmt.Close()
  386. err = nstmt.Get(dest, args)
  387. return
  388. }
  389. // RowContext get row with named args
  390. func (d *DB) RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  391. err = d.ConnectContext(ctx)
  392. if err != nil {
  393. return
  394. }
  395. defer d.Close()
  396. nstmt := &sqlx.NamedStmt{}
  397. nstmt, err = d.conn.PrepareNamedContext(ctx, query)
  398. if err != nil {
  399. return
  400. }
  401. defer nstmt.Close()
  402. err = nstmt.GetContext(ctx, dest, args)
  403. return
  404. }
  405. // *
  406. // In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.
  407. func (d *DB) In(query string, args ...interface{}) (q string, params []interface{}, err error) {
  408. err = d.Connect()
  409. if err != nil {
  410. return
  411. }
  412. defer d.Close()
  413. var s string
  414. s, params, err = sqlx.In(query, args)
  415. q = d.conn.Rebind(s)
  416. return
  417. }
  418. //*/
  419. // InsertReply insert and return DbReply
  420. func (d *DB) InsertReply(query string, args interface{}) (reply Reply) {
  421. var (
  422. err error
  423. rs sql.Result
  424. )
  425. err = d.Connect()
  426. if err != nil {
  427. reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
  428. return
  429. }
  430. defer d.Close()
  431. if rs, err = d.conn.NamedExec(query, args); err == nil {
  432. a, _ := rs.RowsAffected()
  433. n, _ := rs.LastInsertId()
  434. reply = ReplyOk(a, n)
  435. } else {
  436. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  437. }
  438. return
  439. }
  440. // UpdateReply update/delete and return DbReply
  441. func (d *DB) UpdateReply(query string, args interface{}) (reply Reply) {
  442. var (
  443. err error
  444. rs sql.Result
  445. )
  446. err = d.Connect()
  447. if err != nil {
  448. reply = ReplyFaild(ErrNotConnect, err, errors.New(`数据库连接错误`))
  449. return
  450. }
  451. defer d.Close()
  452. if rs, err = d.conn.NamedExec(query, args); err == nil {
  453. a, _ := rs.RowsAffected()
  454. reply = ReplyOk(a, 0)
  455. } else {
  456. reply = ReplyFaild(ErrException, err, errors.New(`数据执行错误`))
  457. }
  458. return
  459. }
  460. // Insert insert into with named args
  461. func (d *DB) Insert(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  462. err = d.Connect()
  463. if err != nil {
  464. return
  465. }
  466. defer d.Close()
  467. var rs sql.Result
  468. if rs, err = d.conn.NamedExec(query, args); err == nil {
  469. lastInsertId, _ = rs.LastInsertId()
  470. rowsAffected, _ = rs.RowsAffected()
  471. }
  472. return
  473. }
  474. // InsertContext insert into with named args
  475. func (d *DB) InsertContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  476. err = d.ConnectContext(ctx)
  477. if err != nil {
  478. return
  479. }
  480. defer d.Close()
  481. var rs sql.Result
  482. if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
  483. lastInsertId, _ = rs.LastInsertId()
  484. rowsAffected, _ = rs.RowsAffected()
  485. }
  486. return
  487. }
  488. // Update update/delete with named args
  489. func (d *DB) Update(query string, args interface{}) (rowsAffected int64, err error) {
  490. err = d.Connect()
  491. if err != nil {
  492. return
  493. }
  494. defer d.Close()
  495. var rs sql.Result
  496. if rs, err = d.conn.NamedExec(query, args); err == nil {
  497. rowsAffected, _ = rs.RowsAffected()
  498. }
  499. return
  500. }
  501. // Update update/delete with named args
  502. func (d *DB) UpdateContext(ctx context.Context, query string, args interface{}) (rowsAffected int64, err error) {
  503. err = d.ConnectContext(ctx)
  504. if err != nil {
  505. return
  506. }
  507. defer d.Close()
  508. var rs sql.Result
  509. if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
  510. rowsAffected, _ = rs.RowsAffected()
  511. }
  512. return
  513. }
  514. // Exec exec
  515. func (d *DB) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
  516. err = d.Connect()
  517. if err != nil {
  518. return
  519. }
  520. defer d.Close()
  521. var rs sql.Result
  522. if rs, err = d.conn.Exec(query, args...); err == nil {
  523. lastInsertId, _ = rs.LastInsertId()
  524. rowsAffected, _ = rs.RowsAffected()
  525. }
  526. return
  527. }
  528. // ExecContext exec
  529. func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
  530. err = d.ConnectContext(ctx)
  531. if err != nil {
  532. return
  533. }
  534. defer d.Close()
  535. var rs sql.Result
  536. if rs, err = d.conn.ExecContext(ctx, query, args...); err == nil {
  537. lastInsertId, _ = rs.LastInsertId()
  538. rowsAffected, _ = rs.RowsAffected()
  539. }
  540. return
  541. }
  542. // Exec exec, with named args
  543. func (d *DB) NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  544. err = d.Connect()
  545. if err != nil {
  546. return
  547. }
  548. defer d.Close()
  549. var rs sql.Result
  550. if rs, err = d.conn.NamedExec(query, args); err == nil {
  551. lastInsertId, _ = rs.LastInsertId()
  552. rowsAffected, _ = rs.RowsAffected()
  553. }
  554. return
  555. }
  556. // NamedExecContext exec, with named args
  557. func (d *DB) NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  558. err = d.ConnectContext(ctx)
  559. if err != nil {
  560. return
  561. }
  562. defer d.Close()
  563. var rs sql.Result
  564. if rs, err = d.conn.NamedExecContext(ctx, query, args); err == nil {
  565. lastInsertId, _ = rs.LastInsertId()
  566. rowsAffected, _ = rs.RowsAffected()
  567. }
  568. return
  569. }
  570. // Limit MySQL limit
  571. func (d *DB) Limit(page, pagesize int) string {
  572. // MySQL limit 0, size
  573. if d.Driver == `mysql` {
  574. return fmt.Sprintf(" LIMIT %d, %d", (page-1)*pagesize, pagesize)
  575. }
  576. // // PostgreSQL limit size offset 0
  577. return fmt.Sprintf(" LIMIT %d OFFSET %d", pagesize, (page-1)*pagesize)
  578. }
  579. // Commit commits the transaction.
  580. func (t *Tx) Commit() error {
  581. return t.tx.Commit()
  582. }
  583. // Rollback aborts the transaction.
  584. func (t *Tx) Rollback() error {
  585. return t.tx.Rollback()
  586. }
  587. // NamedExec executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args
  588. func (t *Tx) NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  589. var rs sql.Result
  590. if rs, err = t.tx.NamedExec(query, args); err == nil {
  591. rowsAffected, _ = rs.RowsAffected()
  592. lastInsertId, _ = rs.LastInsertId()
  593. }
  594. return
  595. }
  596. // NamedExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args
  597. func (t *Tx) NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  598. var rs sql.Result
  599. if rs, err = t.tx.NamedExecContext(ctx, query, args); err == nil {
  600. rowsAffected, _ = rs.RowsAffected()
  601. lastInsertId, _ = rs.LastInsertId()
  602. }
  603. return
  604. }
  605. // Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.
  606. func (t *Tx) Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
  607. var rs sql.Result
  608. if rs, err = t.tx.Exec(query, args...); err == nil {
  609. rowsAffected, _ = rs.RowsAffected()
  610. lastInsertId, _ = rs.LastInsertId()
  611. }
  612. return
  613. }
  614. // ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.
  615. func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
  616. var rs sql.Result
  617. if rs, err = t.tx.ExecContext(ctx, query, args...); err == nil {
  618. rowsAffected, _ = rs.RowsAffected()
  619. lastInsertId, _ = rs.LastInsertId()
  620. }
  621. return
  622. }
  623. // Preparex a statement within a transaction.
  624. func (t *Tx) Preparex(query string) (stmt *Stmt, err error) {
  625. stmt, err = t.tx.Preparex(query)
  626. return
  627. }
  628. // PreparexContext a statement within a transaction.
  629. func (t *Tx) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error) {
  630. stmt, err = t.tx.PreparexContext(ctx, query)
  631. return
  632. }
  633. // PrepareNamed returns an sqlx.NamedStmt
  634. func (t *Tx) PrepareNamed(query string) (stmt *NamedStmt, err error) {
  635. stmt, err = t.tx.PrepareNamed(query)
  636. return
  637. }
  638. // PrepareNamedContext returns an sqlx.NamedStmt
  639. func (t *Tx) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error) {
  640. stmt, err = t.tx.PrepareNamedContext(ctx, query)
  641. return
  642. }
  643. // Query executes a query that returns rows, typically a SELECT. with named args
  644. func (t *Tx) Query(dest interface{}, query string, args interface{}) (err error) {
  645. nstmt := &sqlx.NamedStmt{}
  646. nstmt, err = t.tx.PrepareNamed(query)
  647. if err != nil {
  648. return
  649. }
  650. defer nstmt.Close()
  651. err = nstmt.Select(dest, args)
  652. return
  653. }
  654. // QueryContext executes a query that returns rows, typically a SELECT. with named args
  655. func (t *Tx) QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  656. nstmt := &sqlx.NamedStmt{}
  657. nstmt, err = t.tx.PrepareNamedContext(ctx, query)
  658. if err != nil {
  659. return
  660. }
  661. defer nstmt.Close()
  662. err = nstmt.SelectContext(ctx, dest, args)
  663. return
  664. }
  665. // QueryRow executes a query that returns rows, typically a SELECT. with named args
  666. func (t *Tx) QueryRow(dest interface{}, query string, args interface{}) (err error) {
  667. nstmt := &sqlx.NamedStmt{}
  668. nstmt, err = t.tx.PrepareNamed(query)
  669. if err != nil {
  670. return err
  671. }
  672. defer nstmt.Close()
  673. err = nstmt.Get(dest, args)
  674. return
  675. }
  676. // QueryRowContext get row with named args
  677. func (t *Tx) QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  678. nstmt := &sqlx.NamedStmt{}
  679. nstmt, err = t.tx.PrepareNamedContext(ctx, query)
  680. if err != nil {
  681. return err
  682. }
  683. defer nstmt.Close()
  684. err = nstmt.GetContext(ctx, dest, args)
  685. return
  686. }
  687. // Stats returns database statistics.
  688. func Stats() (s sql.DBStats, err error) {
  689. defaultDB.conn, err = connect()
  690. if err != nil {
  691. return
  692. }
  693. s = defaultDB.Stats()
  694. return
  695. }
  696. // Ping ping connect
  697. func Ping() (err error) {
  698. defaultDB.conn, err = connect()
  699. if err != nil {
  700. return
  701. }
  702. err = defaultDB.Ping()
  703. return
  704. }
  705. // PingContext ping connect
  706. func PingContext(ctx context.Context) (err error) {
  707. defaultDB.conn, err = connectContext(ctx)
  708. if err != nil {
  709. return
  710. }
  711. err = defaultDB.PingContext(ctx)
  712. return
  713. }
  714. // Select select
  715. func Select(dest interface{}, query string, args ...interface{}) (err error) {
  716. defaultDB.conn, err = connect()
  717. if err != nil {
  718. return err
  719. }
  720. err = defaultDB.conn.Select(dest, query, args...)
  721. return
  722. }
  723. // Query get rows with named args
  724. func Query(dest interface{}, query string, args interface{}) (err error) {
  725. defaultDB.conn, err = connect()
  726. if err != nil {
  727. return
  728. }
  729. nstmt := &sqlx.NamedStmt{}
  730. nstmt, err = defaultDB.conn.PrepareNamed(query)
  731. if err != nil {
  732. return
  733. }
  734. defer nstmt.Close()
  735. err = nstmt.Select(dest, args)
  736. return
  737. }
  738. // QueryContext get rows with named args
  739. func QueryContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  740. defaultDB.conn, err = connectContext(ctx)
  741. if err != nil {
  742. return
  743. }
  744. nstmt := &sqlx.NamedStmt{}
  745. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  746. if err != nil {
  747. return
  748. }
  749. defer nstmt.Close()
  750. err = nstmt.SelectContext(ctx, dest, args)
  751. return
  752. }
  753. // Rows get rows with named args
  754. func Rows(dest interface{}, query string, args interface{}) (err error) {
  755. defaultDB.conn, err = connect()
  756. if err != nil {
  757. return
  758. }
  759. nstmt := &sqlx.NamedStmt{}
  760. nstmt, err = defaultDB.conn.PrepareNamed(query)
  761. if err != nil {
  762. return
  763. }
  764. defer nstmt.Close()
  765. err = nstmt.Select(dest, args)
  766. return
  767. }
  768. // RowsContext get rows with named args
  769. func RowsContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  770. defaultDB.conn, err = connectContext(ctx)
  771. if err != nil {
  772. return
  773. }
  774. nstmt := &sqlx.NamedStmt{}
  775. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  776. if err != nil {
  777. return
  778. }
  779. defer nstmt.Close()
  780. err = nstmt.SelectContext(ctx, dest, args)
  781. return
  782. }
  783. // Get get
  784. func Get(dest interface{}, query string, args ...interface{}) (err error) {
  785. defaultDB.conn, err = connect()
  786. if err != nil {
  787. return
  788. }
  789. err = defaultDB.conn.Get(dest, query, args...)
  790. return
  791. }
  792. // QueryRow get row with named args
  793. func QueryRow(dest interface{}, query string, args interface{}) (err error) {
  794. defaultDB.conn, err = connect()
  795. if err != nil {
  796. return
  797. }
  798. nstmt := &sqlx.NamedStmt{}
  799. nstmt, err = defaultDB.conn.PrepareNamed(query)
  800. if err != nil {
  801. return
  802. }
  803. defer nstmt.Close()
  804. err = nstmt.Get(dest, args)
  805. return
  806. }
  807. // QueryRowContext get row with named args
  808. func QueryRowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  809. defaultDB.conn, err = connectContext(ctx)
  810. if err != nil {
  811. return
  812. }
  813. nstmt := &sqlx.NamedStmt{}
  814. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  815. if err != nil {
  816. return
  817. }
  818. defer nstmt.Close()
  819. err = nstmt.GetContext(ctx, dest, args)
  820. return
  821. }
  822. // Row get row with named args
  823. func Row(dest interface{}, query string, args interface{}) (err error) {
  824. defaultDB.conn, err = connect()
  825. if err != nil {
  826. return
  827. }
  828. nstmt := &sqlx.NamedStmt{}
  829. nstmt, err = defaultDB.conn.PrepareNamed(query)
  830. if err != nil {
  831. return
  832. }
  833. defer nstmt.Close()
  834. err = nstmt.Get(dest, args)
  835. return
  836. }
  837. // RowContext get row with named args
  838. func RowContext(ctx context.Context, dest interface{}, query string, args interface{}) (err error) {
  839. defaultDB.conn, err = connectContext(ctx)
  840. if err != nil {
  841. return
  842. }
  843. nstmt := &sqlx.NamedStmt{}
  844. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  845. if err != nil {
  846. return
  847. }
  848. defer nstmt.Close()
  849. err = nstmt.GetContext(ctx, dest, args)
  850. return
  851. }
  852. /*
  853. // In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.
  854. func In(query string, args ...interface{}) (q string, params []interface{}, err error) {
  855. defaultDB.conn, err = connect()
  856. if err != nil {
  857. return
  858. }
  859. q, params, err = defaultDB.In(query, args...)
  860. return
  861. }
  862. // */
  863. // Exec exec
  864. func Exec(query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
  865. defaultDB.conn, err = connect()
  866. if err != nil {
  867. return
  868. }
  869. lastInsertId, rowsAffected, err = defaultDB.Exec(query, args...)
  870. return
  871. }
  872. // Exec exec
  873. func ExecContext(ctx context.Context, query string, args ...interface{}) (lastInsertId, rowsAffected int64, err error) {
  874. defaultDB.conn, err = connectContext(ctx)
  875. if err != nil {
  876. return
  877. }
  878. lastInsertId, rowsAffected, err = defaultDB.ExecContext(ctx, query, args...)
  879. return
  880. }
  881. // NamedExec exec with named args
  882. func NamedExec(query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  883. defaultDB.conn, err = connect()
  884. if err != nil {
  885. return
  886. }
  887. lastInsertId, rowsAffected, err = defaultDB.NamedExec(query, args)
  888. return
  889. }
  890. // NamedExecContext exec with named args
  891. func NamedExecContext(ctx context.Context, query string, args interface{}) (lastInsertId, rowsAffected int64, err error) {
  892. defaultDB.conn, err = connectContext(ctx)
  893. if err != nil {
  894. return
  895. }
  896. lastInsertId, rowsAffected, err = defaultDB.NamedExecContext(ctx, query, args)
  897. return
  898. }