db.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 any) (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 any) (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 any) (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 any, query string, args any) (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 any, query string, args ...any) (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 any, query string, args ...any) (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. // QueryNamed 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) QueryNamed(dest any, query string, args any) (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. // QueryNamedContext 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) QueryNamedContext(ctx context.Context, dest any, query string, args any) (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 any, query string, args any) (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 any, query string, args any) (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 any, query string, args ...any) (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 any, query string, args ...any) (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 any, query string, args ...any) (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 any, query string, args ...any) (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 any, query string, args any) (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 any, query string, args any) (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 ...any) (q string, params []any, 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 any) (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 any) (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 any) (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 any) (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 any) (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 any) (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 ...any) (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 ...any) (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 any) (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 any) (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. /*
  573. // MySQL limit 0, size
  574. if d.Driver == `mysql` {
  575. return fmt.Sprintf(" LIMIT %d, %d", (page-1)*pagesize, pagesize)
  576. }
  577. // // PostgreSQL limit size offset 0
  578. // */
  579. return fmt.Sprintf(" LIMIT %d OFFSET %d", pagesize, (page-1)*pagesize)
  580. }
  581. // Commit commits the transaction.
  582. func (t *Tx) Commit() error {
  583. return t.tx.Commit()
  584. }
  585. // Rollback aborts the transaction.
  586. func (t *Tx) Rollback() error {
  587. return t.tx.Rollback()
  588. }
  589. // NamedExec executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args
  590. func (t *Tx) NamedExec(query string, args any) (lastInsertId, rowsAffected int64, err error) {
  591. var rs sql.Result
  592. if rs, err = t.tx.NamedExec(query, args); err == nil {
  593. rowsAffected, _ = rs.RowsAffected()
  594. lastInsertId, _ = rs.LastInsertId()
  595. }
  596. return
  597. }
  598. // NamedExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE. with named args
  599. func (t *Tx) NamedExecContext(ctx context.Context, query string, args any) (lastInsertId, rowsAffected int64, err error) {
  600. var rs sql.Result
  601. if rs, err = t.tx.NamedExecContext(ctx, query, args); err == nil {
  602. rowsAffected, _ = rs.RowsAffected()
  603. lastInsertId, _ = rs.LastInsertId()
  604. }
  605. return
  606. }
  607. // Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.
  608. func (t *Tx) Exec(query string, args ...any) (lastInsertId, rowsAffected int64, err error) {
  609. var rs sql.Result
  610. if rs, err = t.tx.Exec(query, args...); err == nil {
  611. rowsAffected, _ = rs.RowsAffected()
  612. lastInsertId, _ = rs.LastInsertId()
  613. }
  614. return
  615. }
  616. // ExecContext executes a query that doesn't return rows. For example: an INSERT and UPDATE.
  617. func (t *Tx) ExecContext(ctx context.Context, query string, args ...any) (lastInsertId, rowsAffected int64, err error) {
  618. var rs sql.Result
  619. if rs, err = t.tx.ExecContext(ctx, query, args...); err == nil {
  620. rowsAffected, _ = rs.RowsAffected()
  621. lastInsertId, _ = rs.LastInsertId()
  622. }
  623. return
  624. }
  625. // Preparex a statement within a transaction.
  626. func (t *Tx) Preparex(query string) (stmt *Stmt, err error) {
  627. stmt, err = t.tx.Preparex(query)
  628. return
  629. }
  630. // PreparexContext a statement within a transaction.
  631. func (t *Tx) PreparexContext(ctx context.Context, query string) (stmt *Stmt, err error) {
  632. stmt, err = t.tx.PreparexContext(ctx, query)
  633. return
  634. }
  635. // PrepareNamed returns an sqlx.NamedStmt
  636. func (t *Tx) PrepareNamed(query string) (stmt *NamedStmt, err error) {
  637. stmt, err = t.tx.PrepareNamed(query)
  638. return
  639. }
  640. // PrepareNamedContext returns an sqlx.NamedStmt
  641. func (t *Tx) PrepareNamedContext(ctx context.Context, query string) (stmt *NamedStmt, err error) {
  642. stmt, err = t.tx.PrepareNamedContext(ctx, query)
  643. return
  644. }
  645. // Query executes a query that returns rows, typically a SELECT. with named args
  646. func (t *Tx) Query(dest any, query string, args any) (err error) {
  647. nstmt := &sqlx.NamedStmt{}
  648. nstmt, err = t.tx.PrepareNamed(query)
  649. if err != nil {
  650. return
  651. }
  652. defer nstmt.Close()
  653. err = nstmt.Select(dest, args)
  654. return
  655. }
  656. // QueryContext executes a query that returns rows, typically a SELECT. with named args
  657. func (t *Tx) QueryContext(ctx context.Context, dest any, query string, args any) (err error) {
  658. nstmt := &sqlx.NamedStmt{}
  659. nstmt, err = t.tx.PrepareNamedContext(ctx, query)
  660. if err != nil {
  661. return
  662. }
  663. defer nstmt.Close()
  664. err = nstmt.SelectContext(ctx, dest, args)
  665. return
  666. }
  667. // QueryRow executes a query that returns rows, typically a SELECT. with named args
  668. func (t *Tx) QueryRow(dest any, query string, args any) (err error) {
  669. nstmt := &sqlx.NamedStmt{}
  670. nstmt, err = t.tx.PrepareNamed(query)
  671. if err != nil {
  672. return err
  673. }
  674. defer nstmt.Close()
  675. err = nstmt.Get(dest, args)
  676. return
  677. }
  678. // QueryRowContext get row with named args
  679. func (t *Tx) QueryRowContext(ctx context.Context, dest any, query string, args any) (err error) {
  680. nstmt := &sqlx.NamedStmt{}
  681. nstmt, err = t.tx.PrepareNamedContext(ctx, query)
  682. if err != nil {
  683. return err
  684. }
  685. defer nstmt.Close()
  686. err = nstmt.GetContext(ctx, dest, args)
  687. return
  688. }
  689. // Stats returns database statistics.
  690. func Stats() (s sql.DBStats, err error) {
  691. defaultDB.conn, err = connect()
  692. if err != nil {
  693. return
  694. }
  695. s = defaultDB.Stats()
  696. return
  697. }
  698. // Ping ping connect
  699. func Ping() (err error) {
  700. defaultDB.conn, err = connect()
  701. if err != nil {
  702. return
  703. }
  704. err = defaultDB.Ping()
  705. return
  706. }
  707. // PingContext ping connect
  708. func PingContext(ctx context.Context) (err error) {
  709. defaultDB.conn, err = connectContext(ctx)
  710. if err != nil {
  711. return
  712. }
  713. err = defaultDB.PingContext(ctx)
  714. return
  715. }
  716. // Select select
  717. func Select(dest any, query string, args ...any) (err error) {
  718. defaultDB.conn, err = connect()
  719. if err != nil {
  720. return err
  721. }
  722. err = defaultDB.conn.Select(dest, query, args...)
  723. return
  724. }
  725. // Query get rows with named args
  726. func Query(dest any, query string, args any) (err error) {
  727. defaultDB.conn, err = connect()
  728. if err != nil {
  729. return
  730. }
  731. nstmt := &sqlx.NamedStmt{}
  732. nstmt, err = defaultDB.conn.PrepareNamed(query)
  733. if err != nil {
  734. return
  735. }
  736. defer nstmt.Close()
  737. err = nstmt.Select(dest, args)
  738. return
  739. }
  740. // QueryContext get rows with named args
  741. func QueryContext(ctx context.Context, dest any, query string, args any) (err error) {
  742. defaultDB.conn, err = connectContext(ctx)
  743. if err != nil {
  744. return
  745. }
  746. nstmt := &sqlx.NamedStmt{}
  747. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  748. if err != nil {
  749. return
  750. }
  751. defer nstmt.Close()
  752. err = nstmt.SelectContext(ctx, dest, args)
  753. return
  754. }
  755. // Rows get rows with named args
  756. func Rows(dest any, query string, args any) (err error) {
  757. defaultDB.conn, err = connect()
  758. if err != nil {
  759. return
  760. }
  761. nstmt := &sqlx.NamedStmt{}
  762. nstmt, err = defaultDB.conn.PrepareNamed(query)
  763. if err != nil {
  764. return
  765. }
  766. defer nstmt.Close()
  767. err = nstmt.Select(dest, args)
  768. return
  769. }
  770. // RowsContext get rows with named args
  771. func RowsContext(ctx context.Context, dest any, query string, args any) (err error) {
  772. defaultDB.conn, err = connectContext(ctx)
  773. if err != nil {
  774. return
  775. }
  776. nstmt := &sqlx.NamedStmt{}
  777. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  778. if err != nil {
  779. return
  780. }
  781. defer nstmt.Close()
  782. err = nstmt.SelectContext(ctx, dest, args)
  783. return
  784. }
  785. // Get get
  786. func Get(dest any, query string, args ...any) (err error) {
  787. defaultDB.conn, err = connect()
  788. if err != nil {
  789. return
  790. }
  791. err = defaultDB.conn.Get(dest, query, args...)
  792. return
  793. }
  794. // QueryRow get row with named args
  795. func QueryRow(dest any, query string, args any) (err error) {
  796. defaultDB.conn, err = connect()
  797. if err != nil {
  798. return
  799. }
  800. nstmt := &sqlx.NamedStmt{}
  801. nstmt, err = defaultDB.conn.PrepareNamed(query)
  802. if err != nil {
  803. return
  804. }
  805. defer nstmt.Close()
  806. err = nstmt.Get(dest, args)
  807. return
  808. }
  809. // QueryRowContext get row with named args
  810. func QueryRowContext(ctx context.Context, dest any, query string, args any) (err error) {
  811. defaultDB.conn, err = connectContext(ctx)
  812. if err != nil {
  813. return
  814. }
  815. nstmt := &sqlx.NamedStmt{}
  816. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  817. if err != nil {
  818. return
  819. }
  820. defer nstmt.Close()
  821. err = nstmt.GetContext(ctx, dest, args)
  822. return
  823. }
  824. // Row get row with named args
  825. func Row(dest any, query string, args any) (err error) {
  826. defaultDB.conn, err = connect()
  827. if err != nil {
  828. return
  829. }
  830. nstmt := &sqlx.NamedStmt{}
  831. nstmt, err = defaultDB.conn.PrepareNamed(query)
  832. if err != nil {
  833. return
  834. }
  835. defer nstmt.Close()
  836. err = nstmt.Get(dest, args)
  837. return
  838. }
  839. // RowContext get row with named args
  840. func RowContext(ctx context.Context, dest any, query string, args any) (err error) {
  841. defaultDB.conn, err = connectContext(ctx)
  842. if err != nil {
  843. return
  844. }
  845. nstmt := &sqlx.NamedStmt{}
  846. nstmt, err = defaultDB.conn.PrepareNamedContext(ctx, query)
  847. if err != nil {
  848. return
  849. }
  850. defer nstmt.Close()
  851. err = nstmt.GetContext(ctx, dest, args)
  852. return
  853. }
  854. /*
  855. // 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.
  856. func In(query string, args ...any) (q string, params []any, err error) {
  857. defaultDB.conn, err = connect()
  858. if err != nil {
  859. return
  860. }
  861. q, params, err = defaultDB.In(query, args...)
  862. return
  863. }
  864. // */
  865. // Exec exec
  866. func Exec(query string, args ...any) (lastInsertId, rowsAffected int64, err error) {
  867. defaultDB.conn, err = connect()
  868. if err != nil {
  869. return
  870. }
  871. lastInsertId, rowsAffected, err = defaultDB.Exec(query, args...)
  872. return
  873. }
  874. // Exec exec
  875. func ExecContext(ctx context.Context, query string, args ...any) (lastInsertId, rowsAffected int64, err error) {
  876. defaultDB.conn, err = connectContext(ctx)
  877. if err != nil {
  878. return
  879. }
  880. lastInsertId, rowsAffected, err = defaultDB.ExecContext(ctx, query, args...)
  881. return
  882. }
  883. // NamedExec exec with named args
  884. func NamedExec(query string, args any) (lastInsertId, rowsAffected int64, err error) {
  885. defaultDB.conn, err = connect()
  886. if err != nil {
  887. return
  888. }
  889. lastInsertId, rowsAffected, err = defaultDB.NamedExec(query, args)
  890. return
  891. }
  892. // NamedExecContext exec with named args
  893. func NamedExecContext(ctx context.Context, query string, args any) (lastInsertId, rowsAffected int64, err error) {
  894. defaultDB.conn, err = connectContext(ctx)
  895. if err != nil {
  896. return
  897. }
  898. lastInsertId, rowsAffected, err = defaultDB.NamedExecContext(ctx, query, args)
  899. return
  900. }