sqlx.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. package sqlx
  2. import (
  3. "database/sql"
  4. "database/sql/driver"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "path/filepath"
  9. "reflect"
  10. "strings"
  11. "sync"
  12. "github.com/jmoiron/sqlx/reflectx"
  13. )
  14. // Although the NameMapper is convenient, in practice it should not
  15. // be relied on except for application code. If you are writing a library
  16. // that uses sqlx, you should be aware that the name mappings you expect
  17. // can be overridden by your user's application.
  18. // NameMapper is used to map column names to struct field names. By default,
  19. // it uses strings.ToLower to lowercase struct field names. It can be set
  20. // to whatever you want, but it is encouraged to be set before sqlx is used
  21. // as name-to-field mappings are cached after first use on a type.
  22. var NameMapper = strings.ToLower
  23. var origMapper = reflect.ValueOf(NameMapper)
  24. // Rather than creating on init, this is created when necessary so that
  25. // importers have time to customize the NameMapper.
  26. var mpr *reflectx.Mapper
  27. // mprMu protects mpr.
  28. var mprMu sync.Mutex
  29. // mapper returns a valid mapper using the configured NameMapper func.
  30. func mapper() *reflectx.Mapper {
  31. mprMu.Lock()
  32. defer mprMu.Unlock()
  33. if mpr == nil {
  34. mpr = reflectx.NewMapperFunc("db", NameMapper)
  35. } else if origMapper != reflect.ValueOf(NameMapper) {
  36. // if NameMapper has changed, create a new mapper
  37. mpr = reflectx.NewMapperFunc("db", NameMapper)
  38. origMapper = reflect.ValueOf(NameMapper)
  39. }
  40. return mpr
  41. }
  42. // isScannable takes the reflect.Type and the actual dest value and returns
  43. // whether or not it's Scannable. Something is scannable if:
  44. // * it is not a struct
  45. // * it implements sql.Scanner
  46. // * it has no exported fields
  47. func isScannable(t reflect.Type) bool {
  48. if reflect.PtrTo(t).Implements(_scannerInterface) {
  49. return true
  50. }
  51. if t.Kind() != reflect.Struct {
  52. return true
  53. }
  54. // it's not important that we use the right mapper for this particular object,
  55. // we're only concerned on how many exported fields this struct has
  56. return len(mapper().TypeMap(t).Index) == 0
  57. }
  58. // ColScanner is an interface used by MapScan and SliceScan
  59. type ColScanner interface {
  60. Columns() ([]string, error)
  61. Scan(dest ...interface{}) error
  62. Err() error
  63. }
  64. // Queryer is an interface used by Get and Select
  65. type Queryer interface {
  66. Query(query string, args ...interface{}) (*sql.Rows, error)
  67. Queryx(query string, args ...interface{}) (*Rows, error)
  68. QueryRowx(query string, args ...interface{}) *Row
  69. }
  70. // Execer is an interface used by MustExec and LoadFile
  71. type Execer interface {
  72. Exec(query string, args ...interface{}) (sql.Result, error)
  73. }
  74. // Binder is an interface for something which can bind queries (Tx, DB)
  75. type binder interface {
  76. DriverName() string
  77. Rebind(string) string
  78. BindNamed(string, interface{}) (string, []interface{}, error)
  79. }
  80. // Ext is a union interface which can bind, query, and exec, used by
  81. // NamedQuery and NamedExec.
  82. type Ext interface {
  83. binder
  84. Queryer
  85. Execer
  86. }
  87. // Preparer is an interface used by Preparex.
  88. type Preparer interface {
  89. Prepare(query string) (*sql.Stmt, error)
  90. }
  91. // determine if any of our extensions are unsafe
  92. func isUnsafe(i interface{}) bool {
  93. switch v := i.(type) {
  94. case Row:
  95. return v.unsafe
  96. case *Row:
  97. return v.unsafe
  98. case Rows:
  99. return v.unsafe
  100. case *Rows:
  101. return v.unsafe
  102. case NamedStmt:
  103. return v.Stmt.unsafe
  104. case *NamedStmt:
  105. return v.Stmt.unsafe
  106. case Stmt:
  107. return v.unsafe
  108. case *Stmt:
  109. return v.unsafe
  110. case qStmt:
  111. return v.unsafe
  112. case *qStmt:
  113. return v.unsafe
  114. case DB:
  115. return v.unsafe
  116. case *DB:
  117. return v.unsafe
  118. case Tx:
  119. return v.unsafe
  120. case *Tx:
  121. return v.unsafe
  122. case sql.Rows, *sql.Rows:
  123. return false
  124. default:
  125. return false
  126. }
  127. }
  128. func mapperFor(i interface{}) *reflectx.Mapper {
  129. switch i := i.(type) {
  130. case DB:
  131. return i.Mapper
  132. case *DB:
  133. return i.Mapper
  134. case Tx:
  135. return i.Mapper
  136. case *Tx:
  137. return i.Mapper
  138. default:
  139. return mapper()
  140. }
  141. }
  142. var _scannerInterface = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
  143. var _valuerInterface = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
  144. // Row is a reimplementation of sql.Row in order to gain access to the underlying
  145. // sql.Rows.Columns() data, necessary for StructScan.
  146. type Row struct {
  147. err error
  148. unsafe bool
  149. rows *sql.Rows
  150. Mapper *reflectx.Mapper
  151. }
  152. // Scan is a fixed implementation of sql.Row.Scan, which does not discard the
  153. // underlying error from the internal rows object if it exists.
  154. func (r *Row) Scan(dest ...interface{}) error {
  155. if r.err != nil {
  156. return r.err
  157. }
  158. // TODO(bradfitz): for now we need to defensively clone all
  159. // []byte that the driver returned (not permitting
  160. // *RawBytes in Rows.Scan), since we're about to close
  161. // the Rows in our defer, when we return from this function.
  162. // the contract with the driver.Next(...) interface is that it
  163. // can return slices into read-only temporary memory that's
  164. // only valid until the next Scan/Close. But the TODO is that
  165. // for a lot of drivers, this copy will be unnecessary. We
  166. // should provide an optional interface for drivers to
  167. // implement to say, "don't worry, the []bytes that I return
  168. // from Next will not be modified again." (for instance, if
  169. // they were obtained from the network anyway) But for now we
  170. // don't care.
  171. defer r.rows.Close()
  172. for _, dp := range dest {
  173. if _, ok := dp.(*sql.RawBytes); ok {
  174. return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  175. }
  176. }
  177. if !r.rows.Next() {
  178. if err := r.rows.Err(); err != nil {
  179. return err
  180. }
  181. return sql.ErrNoRows
  182. }
  183. err := r.rows.Scan(dest...)
  184. if err != nil {
  185. return err
  186. }
  187. // Make sure the query can be processed to completion with no errors.
  188. if err := r.rows.Close(); err != nil {
  189. return err
  190. }
  191. return nil
  192. }
  193. // Columns returns the underlying sql.Rows.Columns(), or the deferred error usually
  194. // returned by Row.Scan()
  195. func (r *Row) Columns() ([]string, error) {
  196. if r.err != nil {
  197. return []string{}, r.err
  198. }
  199. return r.rows.Columns()
  200. }
  201. // ColumnTypes returns the underlying sql.Rows.ColumnTypes(), or the deferred error
  202. func (r *Row) ColumnTypes() ([]*sql.ColumnType, error) {
  203. if r.err != nil {
  204. return []*sql.ColumnType{}, r.err
  205. }
  206. return r.rows.ColumnTypes()
  207. }
  208. // Err returns the error encountered while scanning.
  209. func (r *Row) Err() error {
  210. return r.err
  211. }
  212. // DB is a wrapper around sql.DB which keeps track of the driverName upon Open,
  213. // used mostly to automatically bind named queries using the right bindvars.
  214. type DB struct {
  215. *sql.DB
  216. driverName string
  217. unsafe bool
  218. Mapper *reflectx.Mapper
  219. }
  220. // NewDb returns a new sqlx DB wrapper for a pre-existing *sql.DB. The
  221. // driverName of the original database is required for named query support.
  222. func NewDb(db *sql.DB, driverName string) *DB {
  223. return &DB{DB: db, driverName: driverName, Mapper: mapper()}
  224. }
  225. // DriverName returns the driverName passed to the Open function for this DB.
  226. func (db *DB) DriverName() string {
  227. return db.driverName
  228. }
  229. // Open is the same as sql.Open, but returns an *sqlx.DB instead.
  230. func Open(driverName, dataSourceName string) (*DB, error) {
  231. db, err := sql.Open(driverName, dataSourceName)
  232. if err != nil {
  233. return nil, err
  234. }
  235. return &DB{DB: db, driverName: driverName, Mapper: mapper()}, err
  236. }
  237. // MustOpen is the same as sql.Open, but returns an *sqlx.DB instead and panics on error.
  238. func MustOpen(driverName, dataSourceName string) *DB {
  239. db, err := Open(driverName, dataSourceName)
  240. if err != nil {
  241. panic(err)
  242. }
  243. return db
  244. }
  245. // MapperFunc sets a new mapper for this db using the default sqlx struct tag
  246. // and the provided mapper function.
  247. func (db *DB) MapperFunc(mf func(string) string) {
  248. db.Mapper = reflectx.NewMapperFunc("db", mf)
  249. }
  250. // Rebind transforms a query from QUESTION to the DB driver's bindvar type.
  251. func (db *DB) Rebind(query string) string {
  252. return Rebind(BindType(db.driverName), query)
  253. }
  254. // Unsafe returns a version of DB which will silently succeed to scan when
  255. // columns in the SQL result have no fields in the destination struct.
  256. // sqlx.Stmt and sqlx.Tx which are created from this DB will inherit its
  257. // safety behavior.
  258. func (db *DB) Unsafe() *DB {
  259. return &DB{DB: db.DB, driverName: db.driverName, unsafe: true, Mapper: db.Mapper}
  260. }
  261. // BindNamed binds a query using the DB driver's bindvar type.
  262. func (db *DB) BindNamed(query string, arg interface{}) (string, []interface{}, error) {
  263. return bindNamedMapper(BindType(db.driverName), query, arg, db.Mapper)
  264. }
  265. // NamedQuery using this DB.
  266. // Any named placeholder parameters are replaced with fields from arg.
  267. func (db *DB) NamedQuery(query string, arg interface{}) (*Rows, error) {
  268. return NamedQuery(db, query, arg)
  269. }
  270. // NamedExec using this DB.
  271. // Any named placeholder parameters are replaced with fields from arg.
  272. func (db *DB) NamedExec(query string, arg interface{}) (sql.Result, error) {
  273. return NamedExec(db, query, arg)
  274. }
  275. // Select using this DB.
  276. // Any placeholder parameters are replaced with supplied args.
  277. func (db *DB) Select(dest interface{}, query string, args ...interface{}) error {
  278. return Select(db, dest, query, args...)
  279. }
  280. // Get using this DB.
  281. // Any placeholder parameters are replaced with supplied args.
  282. // An error is returned if the result set is empty.
  283. func (db *DB) Get(dest interface{}, query string, args ...interface{}) error {
  284. return Get(db, dest, query, args...)
  285. }
  286. // MustBegin starts a transaction, and panics on error. Returns an *sqlx.Tx instead
  287. // of an *sql.Tx.
  288. func (db *DB) MustBegin() *Tx {
  289. tx, err := db.Beginx()
  290. if err != nil {
  291. panic(err)
  292. }
  293. return tx
  294. }
  295. // Beginx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx.
  296. func (db *DB) Beginx() (*Tx, error) {
  297. tx, err := db.DB.Begin()
  298. if err != nil {
  299. return nil, err
  300. }
  301. return &Tx{Tx: tx, driverName: db.driverName, unsafe: db.unsafe, Mapper: db.Mapper}, err
  302. }
  303. // Queryx queries the database and returns an *sqlx.Rows.
  304. // Any placeholder parameters are replaced with supplied args.
  305. func (db *DB) Queryx(query string, args ...interface{}) (*Rows, error) {
  306. r, err := db.DB.Query(query, args...)
  307. if err != nil {
  308. return nil, err
  309. }
  310. return &Rows{Rows: r, unsafe: db.unsafe, Mapper: db.Mapper}, err
  311. }
  312. // QueryRowx queries the database and returns an *sqlx.Row.
  313. // Any placeholder parameters are replaced with supplied args.
  314. func (db *DB) QueryRowx(query string, args ...interface{}) *Row {
  315. rows, err := db.DB.Query(query, args...)
  316. return &Row{rows: rows, err: err, unsafe: db.unsafe, Mapper: db.Mapper}
  317. }
  318. // MustExec (panic) runs MustExec using this database.
  319. // Any placeholder parameters are replaced with supplied args.
  320. func (db *DB) MustExec(query string, args ...interface{}) sql.Result {
  321. return MustExec(db, query, args...)
  322. }
  323. // Preparex returns an sqlx.Stmt instead of a sql.Stmt
  324. func (db *DB) Preparex(query string) (*Stmt, error) {
  325. return Preparex(db, query)
  326. }
  327. // PrepareNamed returns an sqlx.NamedStmt
  328. func (db *DB) PrepareNamed(query string) (*NamedStmt, error) {
  329. return prepareNamed(db, query)
  330. }
  331. // Conn is a wrapper around sql.Conn with extra functionality
  332. type Conn struct {
  333. *sql.Conn
  334. driverName string
  335. unsafe bool
  336. Mapper *reflectx.Mapper
  337. }
  338. // Tx is an sqlx wrapper around sql.Tx with extra functionality
  339. type Tx struct {
  340. *sql.Tx
  341. driverName string
  342. unsafe bool
  343. Mapper *reflectx.Mapper
  344. }
  345. // DriverName returns the driverName used by the DB which began this transaction.
  346. func (tx *Tx) DriverName() string {
  347. return tx.driverName
  348. }
  349. // Rebind a query within a transaction's bindvar type.
  350. func (tx *Tx) Rebind(query string) string {
  351. return Rebind(BindType(tx.driverName), query)
  352. }
  353. // Unsafe returns a version of Tx which will silently succeed to scan when
  354. // columns in the SQL result have no fields in the destination struct.
  355. func (tx *Tx) Unsafe() *Tx {
  356. return &Tx{Tx: tx.Tx, driverName: tx.driverName, unsafe: true, Mapper: tx.Mapper}
  357. }
  358. // BindNamed binds a query within a transaction's bindvar type.
  359. func (tx *Tx) BindNamed(query string, arg interface{}) (string, []interface{}, error) {
  360. return bindNamedMapper(BindType(tx.driverName), query, arg, tx.Mapper)
  361. }
  362. // NamedQuery within a transaction.
  363. // Any named placeholder parameters are replaced with fields from arg.
  364. func (tx *Tx) NamedQuery(query string, arg interface{}) (*Rows, error) {
  365. return NamedQuery(tx, query, arg)
  366. }
  367. // NamedExec a named query within a transaction.
  368. // Any named placeholder parameters are replaced with fields from arg.
  369. func (tx *Tx) NamedExec(query string, arg interface{}) (sql.Result, error) {
  370. return NamedExec(tx, query, arg)
  371. }
  372. // Select within a transaction.
  373. // Any placeholder parameters are replaced with supplied args.
  374. func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error {
  375. return Select(tx, dest, query, args...)
  376. }
  377. // Queryx within a transaction.
  378. // Any placeholder parameters are replaced with supplied args.
  379. func (tx *Tx) Queryx(query string, args ...interface{}) (*Rows, error) {
  380. r, err := tx.Tx.Query(query, args...)
  381. if err != nil {
  382. return nil, err
  383. }
  384. return &Rows{Rows: r, unsafe: tx.unsafe, Mapper: tx.Mapper}, err
  385. }
  386. // QueryRowx within a transaction.
  387. // Any placeholder parameters are replaced with supplied args.
  388. func (tx *Tx) QueryRowx(query string, args ...interface{}) *Row {
  389. rows, err := tx.Tx.Query(query, args...)
  390. return &Row{rows: rows, err: err, unsafe: tx.unsafe, Mapper: tx.Mapper}
  391. }
  392. // Get within a transaction.
  393. // Any placeholder parameters are replaced with supplied args.
  394. // An error is returned if the result set is empty.
  395. func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error {
  396. return Get(tx, dest, query, args...)
  397. }
  398. // MustExec runs MustExec within a transaction.
  399. // Any placeholder parameters are replaced with supplied args.
  400. func (tx *Tx) MustExec(query string, args ...interface{}) sql.Result {
  401. return MustExec(tx, query, args...)
  402. }
  403. // Preparex a statement within a transaction.
  404. func (tx *Tx) Preparex(query string) (*Stmt, error) {
  405. return Preparex(tx, query)
  406. }
  407. // Stmtx returns a version of the prepared statement which runs within a transaction. Provided
  408. // stmt can be either *sql.Stmt or *sqlx.Stmt.
  409. func (tx *Tx) Stmtx(stmt interface{}) *Stmt {
  410. var s *sql.Stmt
  411. switch v := stmt.(type) {
  412. case Stmt:
  413. s = v.Stmt
  414. case *Stmt:
  415. s = v.Stmt
  416. case *sql.Stmt:
  417. s = v
  418. default:
  419. panic(fmt.Sprintf("non-statement type %v passed to Stmtx", reflect.ValueOf(stmt).Type()))
  420. }
  421. return &Stmt{Stmt: tx.Stmt(s), Mapper: tx.Mapper}
  422. }
  423. // NamedStmt returns a version of the prepared statement which runs within a transaction.
  424. func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt {
  425. return &NamedStmt{
  426. QueryString: stmt.QueryString,
  427. Params: stmt.Params,
  428. Stmt: tx.Stmtx(stmt.Stmt),
  429. }
  430. }
  431. // PrepareNamed returns an sqlx.NamedStmt
  432. func (tx *Tx) PrepareNamed(query string) (*NamedStmt, error) {
  433. return prepareNamed(tx, query)
  434. }
  435. // Stmt is an sqlx wrapper around sql.Stmt with extra functionality
  436. type Stmt struct {
  437. *sql.Stmt
  438. unsafe bool
  439. Mapper *reflectx.Mapper
  440. }
  441. // Unsafe returns a version of Stmt which will silently succeed to scan when
  442. // columns in the SQL result have no fields in the destination struct.
  443. func (s *Stmt) Unsafe() *Stmt {
  444. return &Stmt{Stmt: s.Stmt, unsafe: true, Mapper: s.Mapper}
  445. }
  446. // Select using the prepared statement.
  447. // Any placeholder parameters are replaced with supplied args.
  448. func (s *Stmt) Select(dest interface{}, args ...interface{}) error {
  449. return Select(&qStmt{s}, dest, "", args...)
  450. }
  451. // Get using the prepared statement.
  452. // Any placeholder parameters are replaced with supplied args.
  453. // An error is returned if the result set is empty.
  454. func (s *Stmt) Get(dest interface{}, args ...interface{}) error {
  455. return Get(&qStmt{s}, dest, "", args...)
  456. }
  457. // MustExec (panic) using this statement. Note that the query portion of the error
  458. // output will be blank, as Stmt does not expose its query.
  459. // Any placeholder parameters are replaced with supplied args.
  460. func (s *Stmt) MustExec(args ...interface{}) sql.Result {
  461. return MustExec(&qStmt{s}, "", args...)
  462. }
  463. // QueryRowx using this statement.
  464. // Any placeholder parameters are replaced with supplied args.
  465. func (s *Stmt) QueryRowx(args ...interface{}) *Row {
  466. qs := &qStmt{s}
  467. return qs.QueryRowx("", args...)
  468. }
  469. // Queryx using this statement.
  470. // Any placeholder parameters are replaced with supplied args.
  471. func (s *Stmt) Queryx(args ...interface{}) (*Rows, error) {
  472. qs := &qStmt{s}
  473. return qs.Queryx("", args...)
  474. }
  475. // qStmt is an unexposed wrapper which lets you use a Stmt as a Queryer & Execer by
  476. // implementing those interfaces and ignoring the `query` argument.
  477. type qStmt struct{ *Stmt }
  478. func (q *qStmt) Query(query string, args ...interface{}) (*sql.Rows, error) {
  479. return q.Stmt.Query(args...)
  480. }
  481. func (q *qStmt) Queryx(query string, args ...interface{}) (*Rows, error) {
  482. r, err := q.Stmt.Query(args...)
  483. if err != nil {
  484. return nil, err
  485. }
  486. return &Rows{Rows: r, unsafe: q.Stmt.unsafe, Mapper: q.Stmt.Mapper}, err
  487. }
  488. func (q *qStmt) QueryRowx(query string, args ...interface{}) *Row {
  489. rows, err := q.Stmt.Query(args...)
  490. return &Row{rows: rows, err: err, unsafe: q.Stmt.unsafe, Mapper: q.Stmt.Mapper}
  491. }
  492. func (q *qStmt) Exec(query string, args ...interface{}) (sql.Result, error) {
  493. return q.Stmt.Exec(args...)
  494. }
  495. // Rows is a wrapper around sql.Rows which caches costly reflect operations
  496. // during a looped StructScan
  497. type Rows struct {
  498. *sql.Rows
  499. unsafe bool
  500. Mapper *reflectx.Mapper
  501. // these fields cache memory use for a rows during iteration w/ structScan
  502. started bool
  503. fields [][]int
  504. values []interface{}
  505. }
  506. // SliceScan using this Rows.
  507. func (r *Rows) SliceScan() ([]interface{}, error) {
  508. return SliceScan(r)
  509. }
  510. // MapScan using this Rows.
  511. func (r *Rows) MapScan(dest map[string]interface{}) error {
  512. return MapScan(r, dest)
  513. }
  514. // StructScan is like sql.Rows.Scan, but scans a single Row into a single Struct.
  515. // Use this and iterate over Rows manually when the memory load of Select() might be
  516. // prohibitive. *Rows.StructScan caches the reflect work of matching up column
  517. // positions to fields to avoid that overhead per scan, which means it is not safe
  518. // to run StructScan on the same Rows instance with different struct types.
  519. func (r *Rows) StructScan(dest interface{}) error {
  520. v := reflect.ValueOf(dest)
  521. if v.Kind() != reflect.Ptr {
  522. return errors.New("must pass a pointer, not a value, to StructScan destination")
  523. }
  524. v = v.Elem()
  525. if !r.started {
  526. columns, err := r.Columns()
  527. if err != nil {
  528. return err
  529. }
  530. m := r.Mapper
  531. r.fields = m.TraversalsByName(v.Type(), columns)
  532. // if we are not unsafe and are missing fields, return an error
  533. if f, err := missingFields(r.fields); err != nil && !r.unsafe {
  534. return fmt.Errorf("missing destination name %s in %T", columns[f], dest)
  535. }
  536. r.values = make([]interface{}, len(columns))
  537. r.started = true
  538. }
  539. err := fieldsByTraversal(v, r.fields, r.values, true)
  540. if err != nil {
  541. return err
  542. }
  543. // scan into the struct field pointers and append to our results
  544. err = r.Scan(r.values...)
  545. if err != nil {
  546. return err
  547. }
  548. return r.Err()
  549. }
  550. // Connect to a database and verify with a ping.
  551. func Connect(driverName, dataSourceName string) (*DB, error) {
  552. db, err := Open(driverName, dataSourceName)
  553. if err != nil {
  554. return nil, err
  555. }
  556. err = db.Ping()
  557. if err != nil {
  558. db.Close()
  559. return nil, err
  560. }
  561. return db, nil
  562. }
  563. // MustConnect connects to a database and panics on error.
  564. func MustConnect(driverName, dataSourceName string) *DB {
  565. db, err := Connect(driverName, dataSourceName)
  566. if err != nil {
  567. panic(err)
  568. }
  569. return db
  570. }
  571. // Preparex prepares a statement.
  572. func Preparex(p Preparer, query string) (*Stmt, error) {
  573. s, err := p.Prepare(query)
  574. if err != nil {
  575. return nil, err
  576. }
  577. return &Stmt{Stmt: s, unsafe: isUnsafe(p), Mapper: mapperFor(p)}, err
  578. }
  579. // Select executes a query using the provided Queryer, and StructScans each row
  580. // into dest, which must be a slice. If the slice elements are scannable, then
  581. // the result set must have only one column. Otherwise, StructScan is used.
  582. // The *sql.Rows are closed automatically.
  583. // Any placeholder parameters are replaced with supplied args.
  584. func Select(q Queryer, dest interface{}, query string, args ...interface{}) error {
  585. rows, err := q.Queryx(query, args...)
  586. if err != nil {
  587. return err
  588. }
  589. // if something happens here, we want to make sure the rows are Closed
  590. defer rows.Close()
  591. return scanAll(rows, dest, false)
  592. }
  593. // Get does a QueryRow using the provided Queryer, and scans the resulting row
  594. // to dest. If dest is scannable, the result must only have one column. Otherwise,
  595. // StructScan is used. Get will return sql.ErrNoRows like row.Scan would.
  596. // Any placeholder parameters are replaced with supplied args.
  597. // An error is returned if the result set is empty.
  598. func Get(q Queryer, dest interface{}, query string, args ...interface{}) error {
  599. r := q.QueryRowx(query, args...)
  600. return r.scanAny(dest, false)
  601. }
  602. // LoadFile exec's every statement in a file (as a single call to Exec).
  603. // LoadFile may return a nil *sql.Result if errors are encountered locating or
  604. // reading the file at path. LoadFile reads the entire file into memory, so it
  605. // is not suitable for loading large data dumps, but can be useful for initializing
  606. // schemas or loading indexes.
  607. //
  608. // FIXME: this does not really work with multi-statement files for mattn/go-sqlite3
  609. // or the go-mysql-driver/mysql drivers; pq seems to be an exception here. Detecting
  610. // this by requiring something with DriverName() and then attempting to split the
  611. // queries will be difficult to get right, and its current driver-specific behavior
  612. // is deemed at least not complex in its incorrectness.
  613. func LoadFile(e Execer, path string) (*sql.Result, error) {
  614. realpath, err := filepath.Abs(path)
  615. if err != nil {
  616. return nil, err
  617. }
  618. contents, err := ioutil.ReadFile(realpath)
  619. if err != nil {
  620. return nil, err
  621. }
  622. res, err := e.Exec(string(contents))
  623. return &res, err
  624. }
  625. // MustExec execs the query using e and panics if there was an error.
  626. // Any placeholder parameters are replaced with supplied args.
  627. func MustExec(e Execer, query string, args ...interface{}) sql.Result {
  628. res, err := e.Exec(query, args...)
  629. if err != nil {
  630. panic(err)
  631. }
  632. return res
  633. }
  634. // SliceScan using this Rows.
  635. func (r *Row) SliceScan() ([]interface{}, error) {
  636. return SliceScan(r)
  637. }
  638. // MapScan using this Rows.
  639. func (r *Row) MapScan(dest map[string]interface{}) error {
  640. return MapScan(r, dest)
  641. }
  642. func (r *Row) scanAny(dest interface{}, structOnly bool) error {
  643. if r.err != nil {
  644. return r.err
  645. }
  646. if r.rows == nil {
  647. r.err = sql.ErrNoRows
  648. return r.err
  649. }
  650. defer r.rows.Close()
  651. v := reflect.ValueOf(dest)
  652. if v.Kind() != reflect.Ptr {
  653. return errors.New("must pass a pointer, not a value, to StructScan destination")
  654. }
  655. if v.IsNil() {
  656. return errors.New("nil pointer passed to StructScan destination")
  657. }
  658. base := reflectx.Deref(v.Type())
  659. scannable := isScannable(base)
  660. if structOnly && scannable {
  661. return structOnlyError(base)
  662. }
  663. columns, err := r.Columns()
  664. if err != nil {
  665. return err
  666. }
  667. if scannable && len(columns) > 1 {
  668. return fmt.Errorf("scannable dest type %s with >1 columns (%d) in result", base.Kind(), len(columns))
  669. }
  670. if scannable {
  671. return r.Scan(dest)
  672. }
  673. m := r.Mapper
  674. fields := m.TraversalsByName(v.Type(), columns)
  675. // if we are not unsafe and are missing fields, return an error
  676. if f, err := missingFields(fields); err != nil && !r.unsafe {
  677. return fmt.Errorf("missing destination name %s in %T", columns[f], dest)
  678. }
  679. values := make([]interface{}, len(columns))
  680. err = fieldsByTraversal(v, fields, values, true)
  681. if err != nil {
  682. return err
  683. }
  684. // scan into the struct field pointers and append to our results
  685. return r.Scan(values...)
  686. }
  687. // StructScan a single Row into dest.
  688. func (r *Row) StructScan(dest interface{}) error {
  689. return r.scanAny(dest, true)
  690. }
  691. // SliceScan a row, returning a []interface{} with values similar to MapScan.
  692. // This function is primarily intended for use where the number of columns
  693. // is not known. Because you can pass an []interface{} directly to Scan,
  694. // it's recommended that you do that as it will not have to allocate new
  695. // slices per row.
  696. func SliceScan(r ColScanner) ([]interface{}, error) {
  697. // ignore r.started, since we needn't use reflect for anything.
  698. columns, err := r.Columns()
  699. if err != nil {
  700. return []interface{}{}, err
  701. }
  702. values := make([]interface{}, len(columns))
  703. for i := range values {
  704. values[i] = new(interface{})
  705. }
  706. err = r.Scan(values...)
  707. if err != nil {
  708. return values, err
  709. }
  710. for i := range columns {
  711. values[i] = *(values[i].(*interface{}))
  712. }
  713. return values, r.Err()
  714. }
  715. // MapScan scans a single Row into the dest map[string]interface{}.
  716. // Use this to get results for SQL that might not be under your control
  717. // (for instance, if you're building an interface for an SQL server that
  718. // executes SQL from input). Please do not use this as a primary interface!
  719. // This will modify the map sent to it in place, so reuse the same map with
  720. // care. Columns which occur more than once in the result will overwrite
  721. // each other!
  722. func MapScan(r ColScanner, dest map[string]interface{}) error {
  723. // ignore r.started, since we needn't use reflect for anything.
  724. columns, err := r.Columns()
  725. if err != nil {
  726. return err
  727. }
  728. values := make([]interface{}, len(columns))
  729. for i := range values {
  730. values[i] = new(interface{})
  731. }
  732. err = r.Scan(values...)
  733. if err != nil {
  734. return err
  735. }
  736. for i, column := range columns {
  737. dest[column] = *(values[i].(*interface{}))
  738. }
  739. return r.Err()
  740. }
  741. type rowsi interface {
  742. Close() error
  743. Columns() ([]string, error)
  744. Err() error
  745. Next() bool
  746. Scan(...interface{}) error
  747. }
  748. // structOnlyError returns an error appropriate for type when a non-scannable
  749. // struct is expected but something else is given
  750. func structOnlyError(t reflect.Type) error {
  751. isStruct := t.Kind() == reflect.Struct
  752. isScanner := reflect.PtrTo(t).Implements(_scannerInterface)
  753. if !isStruct {
  754. return fmt.Errorf("expected %s but got %s", reflect.Struct, t.Kind())
  755. }
  756. if isScanner {
  757. return fmt.Errorf("structscan expects a struct dest but the provided struct type %s implements scanner", t.Name())
  758. }
  759. return fmt.Errorf("expected a struct, but struct %s has no exported fields", t.Name())
  760. }
  761. // scanAll scans all rows into a destination, which must be a slice of any
  762. // type. It resets the slice length to zero before appending each element to
  763. // the slice. If the destination slice type is a Struct, then StructScan will
  764. // be used on each row. If the destination is some other kind of base type,
  765. // then each row must only have one column which can scan into that type. This
  766. // allows you to do something like:
  767. //
  768. // rows, _ := db.Query("select id from people;")
  769. // var ids []int
  770. // scanAll(rows, &ids, false)
  771. //
  772. // and ids will be a list of the id results. I realize that this is a desirable
  773. // interface to expose to users, but for now it will only be exposed via changes
  774. // to `Get` and `Select`. The reason that this has been implemented like this is
  775. // this is the only way to not duplicate reflect work in the new API while
  776. // maintaining backwards compatibility.
  777. func scanAll(rows rowsi, dest interface{}, structOnly bool) error {
  778. var v, vp reflect.Value
  779. value := reflect.ValueOf(dest)
  780. // json.Unmarshal returns errors for these
  781. if value.Kind() != reflect.Ptr {
  782. return errors.New("must pass a pointer, not a value, to StructScan destination")
  783. }
  784. if value.IsNil() {
  785. return errors.New("nil pointer passed to StructScan destination")
  786. }
  787. direct := reflect.Indirect(value)
  788. slice, err := baseType(value.Type(), reflect.Slice)
  789. if err != nil {
  790. return err
  791. }
  792. direct.SetLen(0)
  793. isPtr := slice.Elem().Kind() == reflect.Ptr
  794. base := reflectx.Deref(slice.Elem())
  795. scannable := isScannable(base)
  796. if structOnly && scannable {
  797. return structOnlyError(base)
  798. }
  799. columns, err := rows.Columns()
  800. if err != nil {
  801. return err
  802. }
  803. // if it's a base type make sure it only has 1 column; if not return an error
  804. if scannable && len(columns) > 1 {
  805. return fmt.Errorf("non-struct dest type %s with >1 columns (%d)", base.Kind(), len(columns))
  806. }
  807. if !scannable {
  808. var values []interface{}
  809. var m *reflectx.Mapper
  810. switch rows.(type) {
  811. case *Rows:
  812. m = rows.(*Rows).Mapper
  813. default:
  814. m = mapper()
  815. }
  816. fields := m.TraversalsByName(base, columns)
  817. // if we are not unsafe and are missing fields, return an error
  818. if f, err := missingFields(fields); err != nil && !isUnsafe(rows) {
  819. return fmt.Errorf("missing destination name %s in %T", columns[f], dest)
  820. }
  821. values = make([]interface{}, len(columns))
  822. for rows.Next() {
  823. // create a new struct type (which returns PtrTo) and indirect it
  824. vp = reflect.New(base)
  825. v = reflect.Indirect(vp)
  826. err = fieldsByTraversal(v, fields, values, true)
  827. if err != nil {
  828. return err
  829. }
  830. // scan into the struct field pointers and append to our results
  831. err = rows.Scan(values...)
  832. if err != nil {
  833. return err
  834. }
  835. if isPtr {
  836. direct.Set(reflect.Append(direct, vp))
  837. } else {
  838. direct.Set(reflect.Append(direct, v))
  839. }
  840. }
  841. } else {
  842. for rows.Next() {
  843. vp = reflect.New(base)
  844. err = rows.Scan(vp.Interface())
  845. if err != nil {
  846. return err
  847. }
  848. // append
  849. if isPtr {
  850. direct.Set(reflect.Append(direct, vp))
  851. } else {
  852. direct.Set(reflect.Append(direct, reflect.Indirect(vp)))
  853. }
  854. }
  855. }
  856. return rows.Err()
  857. }
  858. // FIXME: StructScan was the very first bit of API in sqlx, and now unfortunately
  859. // it doesn't really feel like it's named properly. There is an incongruency
  860. // between this and the way that StructScan (which might better be ScanStruct
  861. // anyway) works on a rows object.
  862. // StructScan all rows from an sql.Rows or an sqlx.Rows into the dest slice.
  863. // StructScan will scan in the entire rows result, so if you do not want to
  864. // allocate structs for the entire result, use Queryx and see sqlx.Rows.StructScan.
  865. // If rows is sqlx.Rows, it will use its mapper, otherwise it will use the default.
  866. func StructScan(rows rowsi, dest interface{}) error {
  867. return scanAll(rows, dest, true)
  868. }
  869. // reflect helpers
  870. func baseType(t reflect.Type, expected reflect.Kind) (reflect.Type, error) {
  871. t = reflectx.Deref(t)
  872. if t.Kind() != expected {
  873. return nil, fmt.Errorf("expected %s but got %s", expected, t.Kind())
  874. }
  875. return t, nil
  876. }
  877. // fieldsByName fills a values interface with fields from the passed value based
  878. // on the traversals in int. If ptrs is true, return addresses instead of values.
  879. // We write this instead of using FieldsByName to save allocations and map lookups
  880. // when iterating over many rows. Empty traversals will get an interface pointer.
  881. // Because of the necessity of requesting ptrs or values, it's considered a bit too
  882. // specialized for inclusion in reflectx itself.
  883. func fieldsByTraversal(v reflect.Value, traversals [][]int, values []interface{}, ptrs bool) error {
  884. v = reflect.Indirect(v)
  885. if v.Kind() != reflect.Struct {
  886. return errors.New("argument not a struct")
  887. }
  888. for i, traversal := range traversals {
  889. if len(traversal) == 0 {
  890. values[i] = new(interface{})
  891. continue
  892. }
  893. f := reflectx.FieldByIndexes(v, traversal)
  894. if ptrs {
  895. values[i] = f.Addr().Interface()
  896. } else {
  897. values[i] = f.Interface()
  898. }
  899. }
  900. return nil
  901. }
  902. func missingFields(transversals [][]int) (field int, err error) {
  903. for i, t := range transversals {
  904. if len(t) == 0 {
  905. return i, errors.New("missing field")
  906. }
  907. }
  908. return 0, nil
  909. }