db.go 21 KB

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