sql.go 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package sql provides a generic interface around SQL (or SQL-like)
  5. // databases.
  6. //
  7. // The sql package must be used in conjunction with a database driver.
  8. // See https://golang.org/s/sqldrivers for a list of drivers.
  9. //
  10. // Drivers that do not support context cancellation will not return until
  11. // after the query is completed.
  12. //
  13. // For usage examples, see the wiki page at
  14. // https://golang.org/s/sqlwiki.
  15. package sql
  16. import (
  17. "context"
  18. "database/sql/driver"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "reflect"
  23. "runtime"
  24. "sort"
  25. "strconv"
  26. "sync"
  27. "sync/atomic"
  28. "time"
  29. )
  30. var (
  31. driversMu sync.RWMutex
  32. drivers = make(map[string]driver.Driver)
  33. )
  34. // nowFunc returns the current time; it's overridden in tests.
  35. var nowFunc = time.Now
  36. // Register makes a database driver available by the provided name.
  37. // If Register is called twice with the same name or if driver is nil,
  38. // it panics.
  39. func Register(name string, driver driver.Driver) {
  40. driversMu.Lock()
  41. defer driversMu.Unlock()
  42. if driver == nil {
  43. panic("sql: Register driver is nil")
  44. }
  45. if _, dup := drivers[name]; dup {
  46. panic("sql: Register called twice for driver " + name)
  47. }
  48. drivers[name] = driver
  49. }
  50. func unregisterAllDrivers() {
  51. driversMu.Lock()
  52. defer driversMu.Unlock()
  53. // For tests.
  54. drivers = make(map[string]driver.Driver)
  55. }
  56. // Drivers returns a sorted list of the names of the registered drivers.
  57. func Drivers() []string {
  58. driversMu.RLock()
  59. defer driversMu.RUnlock()
  60. list := make([]string, 0, len(drivers))
  61. for name := range drivers {
  62. list = append(list, name)
  63. }
  64. sort.Strings(list)
  65. return list
  66. }
  67. // A NamedArg is a named argument. NamedArg values may be used as
  68. // arguments to Query or Exec and bind to the corresponding named
  69. // parameter in the SQL statement.
  70. //
  71. // For a more concise way to create NamedArg values, see
  72. // the Named function.
  73. type NamedArg struct {
  74. _Named_Fields_Required struct{}
  75. // Name is the name of the parameter placeholder.
  76. //
  77. // If empty, the ordinal position in the argument list will be
  78. // used.
  79. //
  80. // Name must omit any symbol prefix.
  81. Name string
  82. // Value is the value of the parameter.
  83. // It may be assigned the same value types as the query
  84. // arguments.
  85. Value any
  86. }
  87. // Named provides a more concise way to create NamedArg values.
  88. //
  89. // Example usage:
  90. //
  91. // db.ExecContext(ctx, `
  92. // delete from Invoice
  93. // where
  94. // TimeCreated < @end
  95. // and TimeCreated >= @start;`,
  96. // sql.Named("start", startTime),
  97. // sql.Named("end", endTime),
  98. // )
  99. func Named(name string, value any) NamedArg {
  100. // This method exists because the go1compat promise
  101. // doesn't guarantee that structs don't grow more fields,
  102. // so unkeyed struct literals are a vet error. Thus, we don't
  103. // want to allow sql.NamedArg{name, value}.
  104. return NamedArg{Name: name, Value: value}
  105. }
  106. // IsolationLevel is the transaction isolation level used in TxOptions.
  107. type IsolationLevel int
  108. // Various isolation levels that drivers may support in BeginTx.
  109. // If a driver does not support a given isolation level an error may be returned.
  110. //
  111. // See https://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.
  112. const (
  113. LevelDefault IsolationLevel = iota
  114. LevelReadUncommitted
  115. LevelReadCommitted
  116. LevelWriteCommitted
  117. LevelRepeatableRead
  118. LevelSnapshot
  119. LevelSerializable
  120. LevelLinearizable
  121. )
  122. // String returns the name of the transaction isolation level.
  123. func (i IsolationLevel) String() string {
  124. switch i {
  125. case LevelDefault:
  126. return "Default"
  127. case LevelReadUncommitted:
  128. return "Read Uncommitted"
  129. case LevelReadCommitted:
  130. return "Read Committed"
  131. case LevelWriteCommitted:
  132. return "Write Committed"
  133. case LevelRepeatableRead:
  134. return "Repeatable Read"
  135. case LevelSnapshot:
  136. return "Snapshot"
  137. case LevelSerializable:
  138. return "Serializable"
  139. case LevelLinearizable:
  140. return "Linearizable"
  141. default:
  142. return "IsolationLevel(" + strconv.Itoa(int(i)) + ")"
  143. }
  144. }
  145. var _ fmt.Stringer = LevelDefault
  146. // TxOptions holds the transaction options to be used in DB.BeginTx.
  147. type TxOptions struct {
  148. // Isolation is the transaction isolation level.
  149. // If zero, the driver or database's default level is used.
  150. Isolation IsolationLevel
  151. ReadOnly bool
  152. }
  153. // RawBytes is a byte slice that holds a reference to memory owned by
  154. // the database itself. After a Scan into a RawBytes, the slice is only
  155. // valid until the next call to Next, Scan, or Close.
  156. type RawBytes []byte
  157. // NullString represents a string that may be null.
  158. // NullString implements the Scanner interface so
  159. // it can be used as a scan destination:
  160. //
  161. // var s NullString
  162. // err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
  163. // ...
  164. // if s.Valid {
  165. // // use s.String
  166. // } else {
  167. // // NULL value
  168. // }
  169. //
  170. type NullString struct {
  171. String string
  172. Valid bool // Valid is true if String is not NULL
  173. }
  174. // Scan implements the Scanner interface.
  175. func (ns *NullString) Scan(value any) error {
  176. if value == nil {
  177. ns.String, ns.Valid = "", false
  178. return nil
  179. }
  180. ns.Valid = true
  181. return convertAssign(&ns.String, value)
  182. }
  183. // Value implements the driver Valuer interface.
  184. func (ns NullString) Value() (driver.Value, error) {
  185. if !ns.Valid {
  186. return nil, nil
  187. }
  188. return ns.String, nil
  189. }
  190. // NullInt64 represents an int64 that may be null.
  191. // NullInt64 implements the Scanner interface so
  192. // it can be used as a scan destination, similar to NullString.
  193. type NullInt64 struct {
  194. Int64 int64
  195. Valid bool // Valid is true if Int64 is not NULL
  196. }
  197. // Scan implements the Scanner interface.
  198. func (n *NullInt64) Scan(value any) error {
  199. if value == nil {
  200. n.Int64, n.Valid = 0, false
  201. return nil
  202. }
  203. n.Valid = true
  204. return convertAssign(&n.Int64, value)
  205. }
  206. // Value implements the driver Valuer interface.
  207. func (n NullInt64) Value() (driver.Value, error) {
  208. if !n.Valid {
  209. return nil, nil
  210. }
  211. return n.Int64, nil
  212. }
  213. // NullInt32 represents an int32 that may be null.
  214. // NullInt32 implements the Scanner interface so
  215. // it can be used as a scan destination, similar to NullString.
  216. type NullInt32 struct {
  217. Int32 int32
  218. Valid bool // Valid is true if Int32 is not NULL
  219. }
  220. // Scan implements the Scanner interface.
  221. func (n *NullInt32) Scan(value any) error {
  222. if value == nil {
  223. n.Int32, n.Valid = 0, false
  224. return nil
  225. }
  226. n.Valid = true
  227. return convertAssign(&n.Int32, value)
  228. }
  229. // Value implements the driver Valuer interface.
  230. func (n NullInt32) Value() (driver.Value, error) {
  231. if !n.Valid {
  232. return nil, nil
  233. }
  234. return int64(n.Int32), nil
  235. }
  236. // NullInt16 represents an int16 that may be null.
  237. // NullInt16 implements the Scanner interface so
  238. // it can be used as a scan destination, similar to NullString.
  239. type NullInt16 struct {
  240. Int16 int16
  241. Valid bool // Valid is true if Int16 is not NULL
  242. }
  243. // Scan implements the Scanner interface.
  244. func (n *NullInt16) Scan(value any) error {
  245. if value == nil {
  246. n.Int16, n.Valid = 0, false
  247. return nil
  248. }
  249. err := convertAssign(&n.Int16, value)
  250. n.Valid = err == nil
  251. return err
  252. }
  253. // Value implements the driver Valuer interface.
  254. func (n NullInt16) Value() (driver.Value, error) {
  255. if !n.Valid {
  256. return nil, nil
  257. }
  258. return int64(n.Int16), nil
  259. }
  260. // NullByte represents a byte that may be null.
  261. // NullByte implements the Scanner interface so
  262. // it can be used as a scan destination, similar to NullString.
  263. type NullByte struct {
  264. Byte byte
  265. Valid bool // Valid is true if Byte is not NULL
  266. }
  267. // Scan implements the Scanner interface.
  268. func (n *NullByte) Scan(value any) error {
  269. if value == nil {
  270. n.Byte, n.Valid = 0, false
  271. return nil
  272. }
  273. err := convertAssign(&n.Byte, value)
  274. n.Valid = err == nil
  275. return err
  276. }
  277. // Value implements the driver Valuer interface.
  278. func (n NullByte) Value() (driver.Value, error) {
  279. if !n.Valid {
  280. return nil, nil
  281. }
  282. return int64(n.Byte), nil
  283. }
  284. // NullFloat64 represents a float64 that may be null.
  285. // NullFloat64 implements the Scanner interface so
  286. // it can be used as a scan destination, similar to NullString.
  287. type NullFloat64 struct {
  288. Float64 float64
  289. Valid bool // Valid is true if Float64 is not NULL
  290. }
  291. // Scan implements the Scanner interface.
  292. func (n *NullFloat64) Scan(value any) error {
  293. if value == nil {
  294. n.Float64, n.Valid = 0, false
  295. return nil
  296. }
  297. n.Valid = true
  298. return convertAssign(&n.Float64, value)
  299. }
  300. // Value implements the driver Valuer interface.
  301. func (n NullFloat64) Value() (driver.Value, error) {
  302. if !n.Valid {
  303. return nil, nil
  304. }
  305. return n.Float64, nil
  306. }
  307. // NullBool represents a bool that may be null.
  308. // NullBool implements the Scanner interface so
  309. // it can be used as a scan destination, similar to NullString.
  310. type NullBool struct {
  311. Bool bool
  312. Valid bool // Valid is true if Bool is not NULL
  313. }
  314. // Scan implements the Scanner interface.
  315. func (n *NullBool) Scan(value any) error {
  316. if value == nil {
  317. n.Bool, n.Valid = false, false
  318. return nil
  319. }
  320. n.Valid = true
  321. return convertAssign(&n.Bool, value)
  322. }
  323. // Value implements the driver Valuer interface.
  324. func (n NullBool) Value() (driver.Value, error) {
  325. if !n.Valid {
  326. return nil, nil
  327. }
  328. return n.Bool, nil
  329. }
  330. // NullTime represents a time.Time that may be null.
  331. // NullTime implements the Scanner interface so
  332. // it can be used as a scan destination, similar to NullString.
  333. type NullTime struct {
  334. Time time.Time
  335. Valid bool // Valid is true if Time is not NULL
  336. }
  337. // Scan implements the Scanner interface.
  338. func (n *NullTime) Scan(value any) error {
  339. if value == nil {
  340. n.Time, n.Valid = time.Time{}, false
  341. return nil
  342. }
  343. n.Valid = true
  344. return convertAssign(&n.Time, value)
  345. }
  346. // Value implements the driver Valuer interface.
  347. func (n NullTime) Value() (driver.Value, error) {
  348. if !n.Valid {
  349. return nil, nil
  350. }
  351. return n.Time, nil
  352. }
  353. // Scanner is an interface used by Scan.
  354. type Scanner interface {
  355. // Scan assigns a value from a database driver.
  356. //
  357. // The src value will be of one of the following types:
  358. //
  359. // int64
  360. // float64
  361. // bool
  362. // []byte
  363. // string
  364. // time.Time
  365. // nil - for NULL values
  366. //
  367. // An error should be returned if the value cannot be stored
  368. // without loss of information.
  369. //
  370. // Reference types such as []byte are only valid until the next call to Scan
  371. // and should not be retained. Their underlying memory is owned by the driver.
  372. // If retention is necessary, copy their values before the next call to Scan.
  373. Scan(src any) error
  374. }
  375. // Out may be used to retrieve OUTPUT value parameters from stored procedures.
  376. //
  377. // Not all drivers and databases support OUTPUT value parameters.
  378. //
  379. // Example usage:
  380. //
  381. // var outArg string
  382. // _, err := db.ExecContext(ctx, "ProcName", sql.Named("Arg1", sql.Out{Dest: &outArg}))
  383. type Out struct {
  384. _Named_Fields_Required struct{}
  385. // Dest is a pointer to the value that will be set to the result of the
  386. // stored procedure's OUTPUT parameter.
  387. Dest any
  388. // In is whether the parameter is an INOUT parameter. If so, the input value to the stored
  389. // procedure is the dereferenced value of Dest's pointer, which is then replaced with
  390. // the output value.
  391. In bool
  392. }
  393. // ErrNoRows is returned by Scan when QueryRow doesn't return a
  394. // row. In such a case, QueryRow returns a placeholder *Row value that
  395. // defers this error until a Scan.
  396. var ErrNoRows = errors.New("sql: no rows in result set")
  397. // DB is a database handle representing a pool of zero or more
  398. // underlying connections. It's safe for concurrent use by multiple
  399. // goroutines.
  400. //
  401. // The sql package creates and frees connections automatically; it
  402. // also maintains a free pool of idle connections. If the database has
  403. // a concept of per-connection state, such state can be reliably observed
  404. // within a transaction (Tx) or connection (Conn). Once DB.Begin is called, the
  405. // returned Tx is bound to a single connection. Once Commit or
  406. // Rollback is called on the transaction, that transaction's
  407. // connection is returned to DB's idle connection pool. The pool size
  408. // can be controlled with SetMaxIdleConns.
  409. type DB struct {
  410. // Atomic access only. At top of struct to prevent mis-alignment
  411. // on 32-bit platforms. Of type time.Duration.
  412. waitDuration int64 // Total time waited for new connections.
  413. connector driver.Connector
  414. // numClosed is an atomic counter which represents a total number of
  415. // closed connections. Stmt.openStmt checks it before cleaning closed
  416. // connections in Stmt.css.
  417. numClosed uint64
  418. mu sync.Mutex // protects following fields
  419. freeConn []*driverConn // free connections ordered by returnedAt oldest to newest
  420. connRequests map[uint64]chan connRequest
  421. nextRequest uint64 // Next key to use in connRequests.
  422. numOpen int // number of opened and pending open connections
  423. // Used to signal the need for new connections
  424. // a goroutine running connectionOpener() reads on this chan and
  425. // maybeOpenNewConnections sends on the chan (one send per needed connection)
  426. // It is closed during db.Close(). The close tells the connectionOpener
  427. // goroutine to exit.
  428. openerCh chan struct{}
  429. closed bool
  430. dep map[finalCloser]depSet
  431. lastPut map[*driverConn]string // stacktrace of last conn's put; debug only
  432. maxIdleCount int // zero means defaultMaxIdleConns; negative means 0
  433. maxOpen int // <= 0 means unlimited
  434. maxLifetime time.Duration // maximum amount of time a connection may be reused
  435. maxIdleTime time.Duration // maximum amount of time a connection may be idle before being closed
  436. cleanerCh chan struct{}
  437. waitCount int64 // Total number of connections waited for.
  438. maxIdleClosed int64 // Total number of connections closed due to idle count.
  439. maxIdleTimeClosed int64 // Total number of connections closed due to idle time.
  440. maxLifetimeClosed int64 // Total number of connections closed due to max connection lifetime limit.
  441. stop func() // stop cancels the connection opener.
  442. }
  443. // connReuseStrategy determines how (*DB).conn returns database connections.
  444. type connReuseStrategy uint8
  445. const (
  446. // alwaysNewConn forces a new connection to the database.
  447. alwaysNewConn connReuseStrategy = iota
  448. // cachedOrNewConn returns a cached connection, if available, else waits
  449. // for one to become available (if MaxOpenConns has been reached) or
  450. // creates a new database connection.
  451. cachedOrNewConn
  452. )
  453. // driverConn wraps a driver.Conn with a mutex, to
  454. // be held during all calls into the Conn. (including any calls onto
  455. // interfaces returned via that Conn, such as calls on Tx, Stmt,
  456. // Result, Rows)
  457. type driverConn struct {
  458. db *DB
  459. createdAt time.Time
  460. sync.Mutex // guards following
  461. ci driver.Conn
  462. needReset bool // The connection session should be reset before use if true.
  463. closed bool
  464. finalClosed bool // ci.Close has been called
  465. openStmt map[*driverStmt]bool
  466. // guarded by db.mu
  467. inUse bool
  468. returnedAt time.Time // Time the connection was created or returned.
  469. onPut []func() // code (with db.mu held) run when conn is next returned
  470. dbmuClosed bool // same as closed, but guarded by db.mu, for removeClosedStmtLocked
  471. }
  472. func (dc *driverConn) releaseConn(err error) {
  473. dc.db.putConn(dc, err, true)
  474. }
  475. func (dc *driverConn) removeOpenStmt(ds *driverStmt) {
  476. dc.Lock()
  477. defer dc.Unlock()
  478. delete(dc.openStmt, ds)
  479. }
  480. func (dc *driverConn) expired(timeout time.Duration) bool {
  481. if timeout <= 0 {
  482. return false
  483. }
  484. return dc.createdAt.Add(timeout).Before(nowFunc())
  485. }
  486. // resetSession checks if the driver connection needs the
  487. // session to be reset and if required, resets it.
  488. func (dc *driverConn) resetSession(ctx context.Context) error {
  489. dc.Lock()
  490. defer dc.Unlock()
  491. if !dc.needReset {
  492. return nil
  493. }
  494. if cr, ok := dc.ci.(driver.SessionResetter); ok {
  495. return cr.ResetSession(ctx)
  496. }
  497. return nil
  498. }
  499. // validateConnection checks if the connection is valid and can
  500. // still be used. It also marks the session for reset if required.
  501. func (dc *driverConn) validateConnection(needsReset bool) bool {
  502. dc.Lock()
  503. defer dc.Unlock()
  504. if needsReset {
  505. dc.needReset = true
  506. }
  507. if cv, ok := dc.ci.(driver.Validator); ok {
  508. return cv.IsValid()
  509. }
  510. return true
  511. }
  512. // prepareLocked prepares the query on dc. When cg == nil the dc must keep track of
  513. // the prepared statements in a pool.
  514. func (dc *driverConn) prepareLocked(ctx context.Context, cg stmtConnGrabber, query string) (*driverStmt, error) {
  515. si, err := ctxDriverPrepare(ctx, dc.ci, query)
  516. if err != nil {
  517. return nil, err
  518. }
  519. ds := &driverStmt{Locker: dc, si: si}
  520. // No need to manage open statements if there is a single connection grabber.
  521. if cg != nil {
  522. return ds, nil
  523. }
  524. // Track each driverConn's open statements, so we can close them
  525. // before closing the conn.
  526. //
  527. // Wrap all driver.Stmt is *driverStmt to ensure they are only closed once.
  528. if dc.openStmt == nil {
  529. dc.openStmt = make(map[*driverStmt]bool)
  530. }
  531. dc.openStmt[ds] = true
  532. return ds, nil
  533. }
  534. // the dc.db's Mutex is held.
  535. func (dc *driverConn) closeDBLocked() func() error {
  536. dc.Lock()
  537. defer dc.Unlock()
  538. if dc.closed {
  539. return func() error { return errors.New("sql: duplicate driverConn close") }
  540. }
  541. dc.closed = true
  542. return dc.db.removeDepLocked(dc, dc)
  543. }
  544. func (dc *driverConn) Close() error {
  545. dc.Lock()
  546. if dc.closed {
  547. dc.Unlock()
  548. return errors.New("sql: duplicate driverConn close")
  549. }
  550. dc.closed = true
  551. dc.Unlock() // not defer; removeDep finalClose calls may need to lock
  552. // And now updates that require holding dc.mu.Lock.
  553. dc.db.mu.Lock()
  554. dc.dbmuClosed = true
  555. fn := dc.db.removeDepLocked(dc, dc)
  556. dc.db.mu.Unlock()
  557. return fn()
  558. }
  559. func (dc *driverConn) finalClose() error {
  560. var err error
  561. // Each *driverStmt has a lock to the dc. Copy the list out of the dc
  562. // before calling close on each stmt.
  563. var openStmt []*driverStmt
  564. withLock(dc, func() {
  565. openStmt = make([]*driverStmt, 0, len(dc.openStmt))
  566. for ds := range dc.openStmt {
  567. openStmt = append(openStmt, ds)
  568. }
  569. dc.openStmt = nil
  570. })
  571. for _, ds := range openStmt {
  572. ds.Close()
  573. }
  574. withLock(dc, func() {
  575. dc.finalClosed = true
  576. err = dc.ci.Close()
  577. dc.ci = nil
  578. })
  579. dc.db.mu.Lock()
  580. dc.db.numOpen--
  581. dc.db.maybeOpenNewConnections()
  582. dc.db.mu.Unlock()
  583. atomic.AddUint64(&dc.db.numClosed, 1)
  584. return err
  585. }
  586. // driverStmt associates a driver.Stmt with the
  587. // *driverConn from which it came, so the driverConn's lock can be
  588. // held during calls.
  589. type driverStmt struct {
  590. sync.Locker // the *driverConn
  591. si driver.Stmt
  592. closed bool
  593. closeErr error // return value of previous Close call
  594. }
  595. // Close ensures driver.Stmt is only closed once and always returns the same
  596. // result.
  597. func (ds *driverStmt) Close() error {
  598. ds.Lock()
  599. defer ds.Unlock()
  600. if ds.closed {
  601. return ds.closeErr
  602. }
  603. ds.closed = true
  604. ds.closeErr = ds.si.Close()
  605. return ds.closeErr
  606. }
  607. // depSet is a finalCloser's outstanding dependencies
  608. type depSet map[any]bool // set of true bools
  609. // The finalCloser interface is used by (*DB).addDep and related
  610. // dependency reference counting.
  611. type finalCloser interface {
  612. // finalClose is called when the reference count of an object
  613. // goes to zero. (*DB).mu is not held while calling it.
  614. finalClose() error
  615. }
  616. // addDep notes that x now depends on dep, and x's finalClose won't be
  617. // called until all of x's dependencies are removed with removeDep.
  618. func (db *DB) addDep(x finalCloser, dep any) {
  619. db.mu.Lock()
  620. defer db.mu.Unlock()
  621. db.addDepLocked(x, dep)
  622. }
  623. func (db *DB) addDepLocked(x finalCloser, dep any) {
  624. if db.dep == nil {
  625. db.dep = make(map[finalCloser]depSet)
  626. }
  627. xdep := db.dep[x]
  628. if xdep == nil {
  629. xdep = make(depSet)
  630. db.dep[x] = xdep
  631. }
  632. xdep[dep] = true
  633. }
  634. // removeDep notes that x no longer depends on dep.
  635. // If x still has dependencies, nil is returned.
  636. // If x no longer has any dependencies, its finalClose method will be
  637. // called and its error value will be returned.
  638. func (db *DB) removeDep(x finalCloser, dep any) error {
  639. db.mu.Lock()
  640. fn := db.removeDepLocked(x, dep)
  641. db.mu.Unlock()
  642. return fn()
  643. }
  644. func (db *DB) removeDepLocked(x finalCloser, dep any) func() error {
  645. xdep, ok := db.dep[x]
  646. if !ok {
  647. panic(fmt.Sprintf("unpaired removeDep: no deps for %T", x))
  648. }
  649. l0 := len(xdep)
  650. delete(xdep, dep)
  651. switch len(xdep) {
  652. case l0:
  653. // Nothing removed. Shouldn't happen.
  654. panic(fmt.Sprintf("unpaired removeDep: no %T dep on %T", dep, x))
  655. case 0:
  656. // No more dependencies.
  657. delete(db.dep, x)
  658. return x.finalClose
  659. default:
  660. // Dependencies remain.
  661. return func() error { return nil }
  662. }
  663. }
  664. // This is the size of the connectionOpener request chan (DB.openerCh).
  665. // This value should be larger than the maximum typical value
  666. // used for db.maxOpen. If maxOpen is significantly larger than
  667. // connectionRequestQueueSize then it is possible for ALL calls into the *DB
  668. // to block until the connectionOpener can satisfy the backlog of requests.
  669. var connectionRequestQueueSize = 1000000
  670. type dsnConnector struct {
  671. dsn string
  672. driver driver.Driver
  673. }
  674. func (t dsnConnector) Connect(_ context.Context) (driver.Conn, error) {
  675. return t.driver.Open(t.dsn)
  676. }
  677. func (t dsnConnector) Driver() driver.Driver {
  678. return t.driver
  679. }
  680. // OpenDB opens a database using a Connector, allowing drivers to
  681. // bypass a string based data source name.
  682. //
  683. // Most users will open a database via a driver-specific connection
  684. // helper function that returns a *DB. No database drivers are included
  685. // in the Go standard library. See https://golang.org/s/sqldrivers for
  686. // a list of third-party drivers.
  687. //
  688. // OpenDB may just validate its arguments without creating a connection
  689. // to the database. To verify that the data source name is valid, call
  690. // Ping.
  691. //
  692. // The returned DB is safe for concurrent use by multiple goroutines
  693. // and maintains its own pool of idle connections. Thus, the OpenDB
  694. // function should be called just once. It is rarely necessary to
  695. // close a DB.
  696. func OpenDB(c driver.Connector) *DB {
  697. ctx, cancel := context.WithCancel(context.Background())
  698. db := &DB{
  699. connector: c,
  700. openerCh: make(chan struct{}, connectionRequestQueueSize),
  701. lastPut: make(map[*driverConn]string),
  702. connRequests: make(map[uint64]chan connRequest),
  703. stop: cancel,
  704. }
  705. go db.connectionOpener(ctx)
  706. return db
  707. }
  708. // Open opens a database specified by its database driver name and a
  709. // driver-specific data source name, usually consisting of at least a
  710. // database name and connection information.
  711. //
  712. // Most users will open a database via a driver-specific connection
  713. // helper function that returns a *DB. No database drivers are included
  714. // in the Go standard library. See https://golang.org/s/sqldrivers for
  715. // a list of third-party drivers.
  716. //
  717. // Open may just validate its arguments without creating a connection
  718. // to the database. To verify that the data source name is valid, call
  719. // Ping.
  720. //
  721. // The returned DB is safe for concurrent use by multiple goroutines
  722. // and maintains its own pool of idle connections. Thus, the Open
  723. // function should be called just once. It is rarely necessary to
  724. // close a DB.
  725. func Open(driverName, dataSourceName string) (*DB, error) {
  726. driversMu.RLock()
  727. driveri, ok := drivers[driverName]
  728. driversMu.RUnlock()
  729. if !ok {
  730. return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
  731. }
  732. if driverCtx, ok := driveri.(driver.DriverContext); ok {
  733. connector, err := driverCtx.OpenConnector(dataSourceName)
  734. if err != nil {
  735. return nil, err
  736. }
  737. return OpenDB(connector), nil
  738. }
  739. return OpenDB(dsnConnector{dsn: dataSourceName, driver: driveri}), nil
  740. }
  741. func (db *DB) pingDC(ctx context.Context, dc *driverConn, release func(error)) error {
  742. var err error
  743. if pinger, ok := dc.ci.(driver.Pinger); ok {
  744. withLock(dc, func() {
  745. err = pinger.Ping(ctx)
  746. })
  747. }
  748. release(err)
  749. return err
  750. }
  751. // PingContext verifies a connection to the database is still alive,
  752. // establishing a connection if necessary.
  753. func (db *DB) PingContext(ctx context.Context) error {
  754. var dc *driverConn
  755. var err error
  756. var isBadConn bool
  757. for i := 0; i < maxBadConnRetries; i++ {
  758. dc, err = db.conn(ctx, cachedOrNewConn)
  759. isBadConn = errors.Is(err, driver.ErrBadConn)
  760. if !isBadConn {
  761. break
  762. }
  763. }
  764. if isBadConn {
  765. dc, err = db.conn(ctx, alwaysNewConn)
  766. }
  767. if err != nil {
  768. return err
  769. }
  770. return db.pingDC(ctx, dc, dc.releaseConn)
  771. }
  772. // Ping verifies a connection to the database is still alive,
  773. // establishing a connection if necessary.
  774. //
  775. // Ping uses context.Background internally; to specify the context, use
  776. // PingContext.
  777. func (db *DB) Ping() error {
  778. return db.PingContext(context.Background())
  779. }
  780. // Close closes the database and prevents new queries from starting.
  781. // Close then waits for all queries that have started processing on the server
  782. // to finish.
  783. //
  784. // It is rare to Close a DB, as the DB handle is meant to be
  785. // long-lived and shared between many goroutines.
  786. func (db *DB) Close() error {
  787. db.mu.Lock()
  788. if db.closed { // Make DB.Close idempotent
  789. db.mu.Unlock()
  790. return nil
  791. }
  792. if db.cleanerCh != nil {
  793. close(db.cleanerCh)
  794. }
  795. var err error
  796. fns := make([]func() error, 0, len(db.freeConn))
  797. for _, dc := range db.freeConn {
  798. fns = append(fns, dc.closeDBLocked())
  799. }
  800. db.freeConn = nil
  801. db.closed = true
  802. for _, req := range db.connRequests {
  803. close(req)
  804. }
  805. db.mu.Unlock()
  806. for _, fn := range fns {
  807. err1 := fn()
  808. if err1 != nil {
  809. err = err1
  810. }
  811. }
  812. db.stop()
  813. if c, ok := db.connector.(io.Closer); ok {
  814. err1 := c.Close()
  815. if err1 != nil {
  816. err = err1
  817. }
  818. }
  819. return err
  820. }
  821. const defaultMaxIdleConns = 2
  822. func (db *DB) maxIdleConnsLocked() int {
  823. n := db.maxIdleCount
  824. switch {
  825. case n == 0:
  826. // TODO(bradfitz): ask driver, if supported, for its default preference
  827. return defaultMaxIdleConns
  828. case n < 0:
  829. return 0
  830. default:
  831. return n
  832. }
  833. }
  834. func (db *DB) shortestIdleTimeLocked() time.Duration {
  835. if db.maxIdleTime <= 0 {
  836. return db.maxLifetime
  837. }
  838. if db.maxLifetime <= 0 {
  839. return db.maxIdleTime
  840. }
  841. min := db.maxIdleTime
  842. if min > db.maxLifetime {
  843. min = db.maxLifetime
  844. }
  845. return min
  846. }
  847. // SetMaxIdleConns sets the maximum number of connections in the idle
  848. // connection pool.
  849. //
  850. // If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
  851. // then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.
  852. //
  853. // If n <= 0, no idle connections are retained.
  854. //
  855. // The default max idle connections is currently 2. This may change in
  856. // a future release.
  857. func (db *DB) SetMaxIdleConns(n int) {
  858. db.mu.Lock()
  859. if n > 0 {
  860. db.maxIdleCount = n
  861. } else {
  862. // No idle connections.
  863. db.maxIdleCount = -1
  864. }
  865. // Make sure maxIdle doesn't exceed maxOpen
  866. if db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen {
  867. db.maxIdleCount = db.maxOpen
  868. }
  869. var closing []*driverConn
  870. idleCount := len(db.freeConn)
  871. maxIdle := db.maxIdleConnsLocked()
  872. if idleCount > maxIdle {
  873. closing = db.freeConn[maxIdle:]
  874. db.freeConn = db.freeConn[:maxIdle]
  875. }
  876. db.maxIdleClosed += int64(len(closing))
  877. db.mu.Unlock()
  878. for _, c := range closing {
  879. c.Close()
  880. }
  881. }
  882. // SetMaxOpenConns sets the maximum number of open connections to the database.
  883. //
  884. // If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
  885. // MaxIdleConns, then MaxIdleConns will be reduced to match the new
  886. // MaxOpenConns limit.
  887. //
  888. // If n <= 0, then there is no limit on the number of open connections.
  889. // The default is 0 (unlimited).
  890. func (db *DB) SetMaxOpenConns(n int) {
  891. db.mu.Lock()
  892. db.maxOpen = n
  893. if n < 0 {
  894. db.maxOpen = 0
  895. }
  896. syncMaxIdle := db.maxOpen > 0 && db.maxIdleConnsLocked() > db.maxOpen
  897. db.mu.Unlock()
  898. if syncMaxIdle {
  899. db.SetMaxIdleConns(n)
  900. }
  901. }
  902. // SetConnMaxLifetime sets the maximum amount of time a connection may be reused.
  903. //
  904. // Expired connections may be closed lazily before reuse.
  905. //
  906. // If d <= 0, connections are not closed due to a connection's age.
  907. func (db *DB) SetConnMaxLifetime(d time.Duration) {
  908. if d < 0 {
  909. d = 0
  910. }
  911. db.mu.Lock()
  912. // Wake cleaner up when lifetime is shortened.
  913. if d > 0 && d < db.maxLifetime && db.cleanerCh != nil {
  914. select {
  915. case db.cleanerCh <- struct{}{}:
  916. default:
  917. }
  918. }
  919. db.maxLifetime = d
  920. db.startCleanerLocked()
  921. db.mu.Unlock()
  922. }
  923. // SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
  924. //
  925. // Expired connections may be closed lazily before reuse.
  926. //
  927. // If d <= 0, connections are not closed due to a connection's idle time.
  928. func (db *DB) SetConnMaxIdleTime(d time.Duration) {
  929. if d < 0 {
  930. d = 0
  931. }
  932. db.mu.Lock()
  933. defer db.mu.Unlock()
  934. // Wake cleaner up when idle time is shortened.
  935. if d > 0 && d < db.maxIdleTime && db.cleanerCh != nil {
  936. select {
  937. case db.cleanerCh <- struct{}{}:
  938. default:
  939. }
  940. }
  941. db.maxIdleTime = d
  942. db.startCleanerLocked()
  943. }
  944. // startCleanerLocked starts connectionCleaner if needed.
  945. func (db *DB) startCleanerLocked() {
  946. if (db.maxLifetime > 0 || db.maxIdleTime > 0) && db.numOpen > 0 && db.cleanerCh == nil {
  947. db.cleanerCh = make(chan struct{}, 1)
  948. go db.connectionCleaner(db.shortestIdleTimeLocked())
  949. }
  950. }
  951. func (db *DB) connectionCleaner(d time.Duration) {
  952. const minInterval = time.Second
  953. if d < minInterval {
  954. d = minInterval
  955. }
  956. t := time.NewTimer(d)
  957. for {
  958. select {
  959. case <-t.C:
  960. case <-db.cleanerCh: // maxLifetime was changed or db was closed.
  961. }
  962. db.mu.Lock()
  963. d = db.shortestIdleTimeLocked()
  964. if db.closed || db.numOpen == 0 || d <= 0 {
  965. db.cleanerCh = nil
  966. db.mu.Unlock()
  967. return
  968. }
  969. d, closing := db.connectionCleanerRunLocked(d)
  970. db.mu.Unlock()
  971. for _, c := range closing {
  972. c.Close()
  973. }
  974. if d < minInterval {
  975. d = minInterval
  976. }
  977. if !t.Stop() {
  978. select {
  979. case <-t.C:
  980. default:
  981. }
  982. }
  983. t.Reset(d)
  984. }
  985. }
  986. // connectionCleanerRunLocked removes connections that should be closed from
  987. // freeConn and returns them along side an updated duration to the next check
  988. // if a quicker check is required to ensure connections are checked appropriately.
  989. func (db *DB) connectionCleanerRunLocked(d time.Duration) (time.Duration, []*driverConn) {
  990. var idleClosing int64
  991. var closing []*driverConn
  992. if db.maxIdleTime > 0 {
  993. // As freeConn is ordered by returnedAt process
  994. // in reverse order to minimise the work needed.
  995. idleSince := nowFunc().Add(-db.maxIdleTime)
  996. last := len(db.freeConn) - 1
  997. for i := last; i >= 0; i-- {
  998. c := db.freeConn[i]
  999. if c.returnedAt.Before(idleSince) {
  1000. i++
  1001. closing = db.freeConn[:i:i]
  1002. db.freeConn = db.freeConn[i:]
  1003. idleClosing = int64(len(closing))
  1004. db.maxIdleTimeClosed += idleClosing
  1005. break
  1006. }
  1007. }
  1008. if len(db.freeConn) > 0 {
  1009. c := db.freeConn[0]
  1010. if d2 := c.returnedAt.Sub(idleSince); d2 < d {
  1011. // Ensure idle connections are cleaned up as soon as
  1012. // possible.
  1013. d = d2
  1014. }
  1015. }
  1016. }
  1017. if db.maxLifetime > 0 {
  1018. expiredSince := nowFunc().Add(-db.maxLifetime)
  1019. for i := 0; i < len(db.freeConn); i++ {
  1020. c := db.freeConn[i]
  1021. if c.createdAt.Before(expiredSince) {
  1022. closing = append(closing, c)
  1023. last := len(db.freeConn) - 1
  1024. // Use slow delete as order is required to ensure
  1025. // connections are reused least idle time first.
  1026. copy(db.freeConn[i:], db.freeConn[i+1:])
  1027. db.freeConn[last] = nil
  1028. db.freeConn = db.freeConn[:last]
  1029. i--
  1030. } else if d2 := c.createdAt.Sub(expiredSince); d2 < d {
  1031. // Prevent connections sitting the freeConn when they
  1032. // have expired by updating our next deadline d.
  1033. d = d2
  1034. }
  1035. }
  1036. db.maxLifetimeClosed += int64(len(closing)) - idleClosing
  1037. }
  1038. return d, closing
  1039. }
  1040. // DBStats contains database statistics.
  1041. type DBStats struct {
  1042. MaxOpenConnections int // Maximum number of open connections to the database.
  1043. // Pool Status
  1044. OpenConnections int // The number of established connections both in use and idle.
  1045. InUse int // The number of connections currently in use.
  1046. Idle int // The number of idle connections.
  1047. // Counters
  1048. WaitCount int64 // The total number of connections waited for.
  1049. WaitDuration time.Duration // The total time blocked waiting for a new connection.
  1050. MaxIdleClosed int64 // The total number of connections closed due to SetMaxIdleConns.
  1051. MaxIdleTimeClosed int64 // The total number of connections closed due to SetConnMaxIdleTime.
  1052. MaxLifetimeClosed int64 // The total number of connections closed due to SetConnMaxLifetime.
  1053. }
  1054. // Stats returns database statistics.
  1055. func (db *DB) Stats() DBStats {
  1056. wait := atomic.LoadInt64(&db.waitDuration)
  1057. db.mu.Lock()
  1058. defer db.mu.Unlock()
  1059. stats := DBStats{
  1060. MaxOpenConnections: db.maxOpen,
  1061. Idle: len(db.freeConn),
  1062. OpenConnections: db.numOpen,
  1063. InUse: db.numOpen - len(db.freeConn),
  1064. WaitCount: db.waitCount,
  1065. WaitDuration: time.Duration(wait),
  1066. MaxIdleClosed: db.maxIdleClosed,
  1067. MaxIdleTimeClosed: db.maxIdleTimeClosed,
  1068. MaxLifetimeClosed: db.maxLifetimeClosed,
  1069. }
  1070. return stats
  1071. }
  1072. // Assumes db.mu is locked.
  1073. // If there are connRequests and the connection limit hasn't been reached,
  1074. // then tell the connectionOpener to open new connections.
  1075. func (db *DB) maybeOpenNewConnections() {
  1076. numRequests := len(db.connRequests)
  1077. if db.maxOpen > 0 {
  1078. numCanOpen := db.maxOpen - db.numOpen
  1079. if numRequests > numCanOpen {
  1080. numRequests = numCanOpen
  1081. }
  1082. }
  1083. for numRequests > 0 {
  1084. db.numOpen++ // optimistically
  1085. numRequests--
  1086. if db.closed {
  1087. return
  1088. }
  1089. db.openerCh <- struct{}{}
  1090. }
  1091. }
  1092. // Runs in a separate goroutine, opens new connections when requested.
  1093. func (db *DB) connectionOpener(ctx context.Context) {
  1094. for {
  1095. select {
  1096. case <-ctx.Done():
  1097. return
  1098. case <-db.openerCh:
  1099. db.openNewConnection(ctx)
  1100. }
  1101. }
  1102. }
  1103. // Open one new connection
  1104. func (db *DB) openNewConnection(ctx context.Context) {
  1105. // maybeOpenNewConnections has already executed db.numOpen++ before it sent
  1106. // on db.openerCh. This function must execute db.numOpen-- if the
  1107. // connection fails or is closed before returning.
  1108. ci, err := db.connector.Connect(ctx)
  1109. db.mu.Lock()
  1110. defer db.mu.Unlock()
  1111. if db.closed {
  1112. if err == nil {
  1113. ci.Close()
  1114. }
  1115. db.numOpen--
  1116. return
  1117. }
  1118. if err != nil {
  1119. db.numOpen--
  1120. db.putConnDBLocked(nil, err)
  1121. db.maybeOpenNewConnections()
  1122. return
  1123. }
  1124. dc := &driverConn{
  1125. db: db,
  1126. createdAt: nowFunc(),
  1127. returnedAt: nowFunc(),
  1128. ci: ci,
  1129. }
  1130. if db.putConnDBLocked(dc, err) {
  1131. db.addDepLocked(dc, dc)
  1132. } else {
  1133. db.numOpen--
  1134. ci.Close()
  1135. }
  1136. }
  1137. // connRequest represents one request for a new connection
  1138. // When there are no idle connections available, DB.conn will create
  1139. // a new connRequest and put it on the db.connRequests list.
  1140. type connRequest struct {
  1141. conn *driverConn
  1142. err error
  1143. }
  1144. var errDBClosed = errors.New("sql: database is closed")
  1145. // nextRequestKeyLocked returns the next connection request key.
  1146. // It is assumed that nextRequest will not overflow.
  1147. func (db *DB) nextRequestKeyLocked() uint64 {
  1148. next := db.nextRequest
  1149. db.nextRequest++
  1150. return next
  1151. }
  1152. // conn returns a newly-opened or cached *driverConn.
  1153. func (db *DB) conn(ctx context.Context, strategy connReuseStrategy) (*driverConn, error) {
  1154. db.mu.Lock()
  1155. if db.closed {
  1156. db.mu.Unlock()
  1157. return nil, errDBClosed
  1158. }
  1159. // Check if the context is expired.
  1160. select {
  1161. default:
  1162. case <-ctx.Done():
  1163. db.mu.Unlock()
  1164. return nil, ctx.Err()
  1165. }
  1166. lifetime := db.maxLifetime
  1167. // Prefer a free connection, if possible.
  1168. last := len(db.freeConn) - 1
  1169. if strategy == cachedOrNewConn && last >= 0 {
  1170. // Reuse the lowest idle time connection so we can close
  1171. // connections which remain idle as soon as possible.
  1172. conn := db.freeConn[last]
  1173. db.freeConn = db.freeConn[:last]
  1174. conn.inUse = true
  1175. if conn.expired(lifetime) {
  1176. db.maxLifetimeClosed++
  1177. db.mu.Unlock()
  1178. conn.Close()
  1179. return nil, driver.ErrBadConn
  1180. }
  1181. db.mu.Unlock()
  1182. // Reset the session if required.
  1183. if err := conn.resetSession(ctx); errors.Is(err, driver.ErrBadConn) {
  1184. conn.Close()
  1185. return nil, err
  1186. }
  1187. return conn, nil
  1188. }
  1189. // Out of free connections or we were asked not to use one. If we're not
  1190. // allowed to open any more connections, make a request and wait.
  1191. if db.maxOpen > 0 && db.numOpen >= db.maxOpen {
  1192. // Make the connRequest channel. It's buffered so that the
  1193. // connectionOpener doesn't block while waiting for the req to be read.
  1194. req := make(chan connRequest, 1)
  1195. reqKey := db.nextRequestKeyLocked()
  1196. db.connRequests[reqKey] = req
  1197. db.waitCount++
  1198. db.mu.Unlock()
  1199. waitStart := nowFunc()
  1200. // Timeout the connection request with the context.
  1201. select {
  1202. case <-ctx.Done():
  1203. // Remove the connection request and ensure no value has been sent
  1204. // on it after removing.
  1205. db.mu.Lock()
  1206. delete(db.connRequests, reqKey)
  1207. db.mu.Unlock()
  1208. atomic.AddInt64(&db.waitDuration, int64(time.Since(waitStart)))
  1209. select {
  1210. default:
  1211. case ret, ok := <-req:
  1212. if ok && ret.conn != nil {
  1213. db.putConn(ret.conn, ret.err, false)
  1214. }
  1215. }
  1216. return nil, ctx.Err()
  1217. case ret, ok := <-req:
  1218. atomic.AddInt64(&db.waitDuration, int64(time.Since(waitStart)))
  1219. if !ok {
  1220. return nil, errDBClosed
  1221. }
  1222. // Only check if the connection is expired if the strategy is cachedOrNewConns.
  1223. // If we require a new connection, just re-use the connection without looking
  1224. // at the expiry time. If it is expired, it will be checked when it is placed
  1225. // back into the connection pool.
  1226. // This prioritizes giving a valid connection to a client over the exact connection
  1227. // lifetime, which could expire exactly after this point anyway.
  1228. if strategy == cachedOrNewConn && ret.err == nil && ret.conn.expired(lifetime) {
  1229. db.mu.Lock()
  1230. db.maxLifetimeClosed++
  1231. db.mu.Unlock()
  1232. ret.conn.Close()
  1233. return nil, driver.ErrBadConn
  1234. }
  1235. if ret.conn == nil {
  1236. return nil, ret.err
  1237. }
  1238. // Reset the session if required.
  1239. if err := ret.conn.resetSession(ctx); errors.Is(err, driver.ErrBadConn) {
  1240. ret.conn.Close()
  1241. return nil, err
  1242. }
  1243. return ret.conn, ret.err
  1244. }
  1245. }
  1246. db.numOpen++ // optimistically
  1247. db.mu.Unlock()
  1248. ci, err := db.connector.Connect(ctx)
  1249. if err != nil {
  1250. db.mu.Lock()
  1251. db.numOpen-- // correct for earlier optimism
  1252. db.maybeOpenNewConnections()
  1253. db.mu.Unlock()
  1254. return nil, err
  1255. }
  1256. db.mu.Lock()
  1257. dc := &driverConn{
  1258. db: db,
  1259. createdAt: nowFunc(),
  1260. returnedAt: nowFunc(),
  1261. ci: ci,
  1262. inUse: true,
  1263. }
  1264. db.addDepLocked(dc, dc)
  1265. db.mu.Unlock()
  1266. return dc, nil
  1267. }
  1268. // putConnHook is a hook for testing.
  1269. var putConnHook func(*DB, *driverConn)
  1270. // noteUnusedDriverStatement notes that ds is no longer used and should
  1271. // be closed whenever possible (when c is next not in use), unless c is
  1272. // already closed.
  1273. func (db *DB) noteUnusedDriverStatement(c *driverConn, ds *driverStmt) {
  1274. db.mu.Lock()
  1275. defer db.mu.Unlock()
  1276. if c.inUse {
  1277. c.onPut = append(c.onPut, func() {
  1278. ds.Close()
  1279. })
  1280. } else {
  1281. c.Lock()
  1282. fc := c.finalClosed
  1283. c.Unlock()
  1284. if !fc {
  1285. ds.Close()
  1286. }
  1287. }
  1288. }
  1289. // debugGetPut determines whether getConn & putConn calls' stack traces
  1290. // are returned for more verbose crashes.
  1291. const debugGetPut = false
  1292. // putConn adds a connection to the db's free pool.
  1293. // err is optionally the last error that occurred on this connection.
  1294. func (db *DB) putConn(dc *driverConn, err error, resetSession bool) {
  1295. if !errors.Is(err, driver.ErrBadConn) {
  1296. if !dc.validateConnection(resetSession) {
  1297. err = driver.ErrBadConn
  1298. }
  1299. }
  1300. db.mu.Lock()
  1301. if !dc.inUse {
  1302. db.mu.Unlock()
  1303. if debugGetPut {
  1304. fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was: %s", dc, stack(), db.lastPut[dc])
  1305. }
  1306. panic("sql: connection returned that was never out")
  1307. }
  1308. if !errors.Is(err, driver.ErrBadConn) && dc.expired(db.maxLifetime) {
  1309. db.maxLifetimeClosed++
  1310. err = driver.ErrBadConn
  1311. }
  1312. if debugGetPut {
  1313. db.lastPut[dc] = stack()
  1314. }
  1315. dc.inUse = false
  1316. dc.returnedAt = nowFunc()
  1317. for _, fn := range dc.onPut {
  1318. fn()
  1319. }
  1320. dc.onPut = nil
  1321. if errors.Is(err, driver.ErrBadConn) {
  1322. // Don't reuse bad connections.
  1323. // Since the conn is considered bad and is being discarded, treat it
  1324. // as closed. Don't decrement the open count here, finalClose will
  1325. // take care of that.
  1326. db.maybeOpenNewConnections()
  1327. db.mu.Unlock()
  1328. dc.Close()
  1329. return
  1330. }
  1331. if putConnHook != nil {
  1332. putConnHook(db, dc)
  1333. }
  1334. added := db.putConnDBLocked(dc, nil)
  1335. db.mu.Unlock()
  1336. if !added {
  1337. dc.Close()
  1338. return
  1339. }
  1340. }
  1341. // Satisfy a connRequest or put the driverConn in the idle pool and return true
  1342. // or return false.
  1343. // putConnDBLocked will satisfy a connRequest if there is one, or it will
  1344. // return the *driverConn to the freeConn list if err == nil and the idle
  1345. // connection limit will not be exceeded.
  1346. // If err != nil, the value of dc is ignored.
  1347. // If err == nil, then dc must not equal nil.
  1348. // If a connRequest was fulfilled or the *driverConn was placed in the
  1349. // freeConn list, then true is returned, otherwise false is returned.
  1350. func (db *DB) putConnDBLocked(dc *driverConn, err error) bool {
  1351. if db.closed {
  1352. return false
  1353. }
  1354. if db.maxOpen > 0 && db.numOpen > db.maxOpen {
  1355. return false
  1356. }
  1357. if c := len(db.connRequests); c > 0 {
  1358. var req chan connRequest
  1359. var reqKey uint64
  1360. for reqKey, req = range db.connRequests {
  1361. break
  1362. }
  1363. delete(db.connRequests, reqKey) // Remove from pending requests.
  1364. if err == nil {
  1365. dc.inUse = true
  1366. }
  1367. req <- connRequest{
  1368. conn: dc,
  1369. err: err,
  1370. }
  1371. return true
  1372. } else if err == nil && !db.closed {
  1373. if db.maxIdleConnsLocked() > len(db.freeConn) {
  1374. db.freeConn = append(db.freeConn, dc)
  1375. db.startCleanerLocked()
  1376. return true
  1377. }
  1378. db.maxIdleClosed++
  1379. }
  1380. return false
  1381. }
  1382. // maxBadConnRetries is the number of maximum retries if the driver returns
  1383. // driver.ErrBadConn to signal a broken connection before forcing a new
  1384. // connection to be opened.
  1385. const maxBadConnRetries = 2
  1386. // PrepareContext creates a prepared statement for later queries or executions.
  1387. // Multiple queries or executions may be run concurrently from the
  1388. // returned statement.
  1389. // The caller must call the statement's Close method
  1390. // when the statement is no longer needed.
  1391. //
  1392. // The provided context is used for the preparation of the statement, not for the
  1393. // execution of the statement.
  1394. func (db *DB) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1395. var stmt *Stmt
  1396. var err error
  1397. var isBadConn bool
  1398. for i := 0; i < maxBadConnRetries; i++ {
  1399. stmt, err = db.prepare(ctx, query, cachedOrNewConn)
  1400. isBadConn = errors.Is(err, driver.ErrBadConn)
  1401. if !isBadConn {
  1402. break
  1403. }
  1404. }
  1405. if isBadConn {
  1406. return db.prepare(ctx, query, alwaysNewConn)
  1407. }
  1408. return stmt, err
  1409. }
  1410. // Prepare creates a prepared statement for later queries or executions.
  1411. // Multiple queries or executions may be run concurrently from the
  1412. // returned statement.
  1413. // The caller must call the statement's Close method
  1414. // when the statement is no longer needed.
  1415. //
  1416. // Prepare uses context.Background internally; to specify the context, use
  1417. // PrepareContext.
  1418. func (db *DB) Prepare(query string) (*Stmt, error) {
  1419. return db.PrepareContext(context.Background(), query)
  1420. }
  1421. func (db *DB) prepare(ctx context.Context, query string, strategy connReuseStrategy) (*Stmt, error) {
  1422. // TODO: check if db.driver supports an optional
  1423. // driver.Preparer interface and call that instead, if so,
  1424. // otherwise we make a prepared statement that's bound
  1425. // to a connection, and to execute this prepared statement
  1426. // we either need to use this connection (if it's free), else
  1427. // get a new connection + re-prepare + execute on that one.
  1428. dc, err := db.conn(ctx, strategy)
  1429. if err != nil {
  1430. return nil, err
  1431. }
  1432. return db.prepareDC(ctx, dc, dc.releaseConn, nil, query)
  1433. }
  1434. // prepareDC prepares a query on the driverConn and calls release before
  1435. // returning. When cg == nil it implies that a connection pool is used, and
  1436. // when cg != nil only a single driver connection is used.
  1437. func (db *DB) prepareDC(ctx context.Context, dc *driverConn, release func(error), cg stmtConnGrabber, query string) (*Stmt, error) {
  1438. var ds *driverStmt
  1439. var err error
  1440. defer func() {
  1441. release(err)
  1442. }()
  1443. withLock(dc, func() {
  1444. ds, err = dc.prepareLocked(ctx, cg, query)
  1445. })
  1446. if err != nil {
  1447. return nil, err
  1448. }
  1449. stmt := &Stmt{
  1450. db: db,
  1451. query: query,
  1452. cg: cg,
  1453. cgds: ds,
  1454. }
  1455. // When cg == nil this statement will need to keep track of various
  1456. // connections they are prepared on and record the stmt dependency on
  1457. // the DB.
  1458. if cg == nil {
  1459. stmt.css = []connStmt{{dc, ds}}
  1460. stmt.lastNumClosed = atomic.LoadUint64(&db.numClosed)
  1461. db.addDep(stmt, stmt)
  1462. }
  1463. return stmt, nil
  1464. }
  1465. // ExecContext executes a query without returning any rows.
  1466. // The args are for any placeholder parameters in the query.
  1467. func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
  1468. var res Result
  1469. var err error
  1470. var isBadConn bool
  1471. for i := 0; i < maxBadConnRetries; i++ {
  1472. res, err = db.exec(ctx, query, args, cachedOrNewConn)
  1473. isBadConn = errors.Is(err, driver.ErrBadConn)
  1474. if !isBadConn {
  1475. break
  1476. }
  1477. }
  1478. if isBadConn {
  1479. return db.exec(ctx, query, args, alwaysNewConn)
  1480. }
  1481. return res, err
  1482. }
  1483. // Exec executes a query without returning any rows.
  1484. // The args are for any placeholder parameters in the query.
  1485. //
  1486. // Exec uses context.Background internally; to specify the context, use
  1487. // ExecContext.
  1488. func (db *DB) Exec(query string, args ...any) (Result, error) {
  1489. return db.ExecContext(context.Background(), query, args...)
  1490. }
  1491. func (db *DB) exec(ctx context.Context, query string, args []any, strategy connReuseStrategy) (Result, error) {
  1492. dc, err := db.conn(ctx, strategy)
  1493. if err != nil {
  1494. return nil, err
  1495. }
  1496. return db.execDC(ctx, dc, dc.releaseConn, query, args)
  1497. }
  1498. func (db *DB) execDC(ctx context.Context, dc *driverConn, release func(error), query string, args []any) (res Result, err error) {
  1499. defer func() {
  1500. release(err)
  1501. }()
  1502. execerCtx, ok := dc.ci.(driver.ExecerContext)
  1503. var execer driver.Execer
  1504. if !ok {
  1505. execer, ok = dc.ci.(driver.Execer)
  1506. }
  1507. if ok {
  1508. var nvdargs []driver.NamedValue
  1509. var resi driver.Result
  1510. withLock(dc, func() {
  1511. nvdargs, err = driverArgsConnLocked(dc.ci, nil, args)
  1512. if err != nil {
  1513. return
  1514. }
  1515. resi, err = ctxDriverExec(ctx, execerCtx, execer, query, nvdargs)
  1516. })
  1517. if err != driver.ErrSkip {
  1518. if err != nil {
  1519. return nil, err
  1520. }
  1521. return driverResult{dc, resi}, nil
  1522. }
  1523. }
  1524. var si driver.Stmt
  1525. withLock(dc, func() {
  1526. si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1527. })
  1528. if err != nil {
  1529. return nil, err
  1530. }
  1531. ds := &driverStmt{Locker: dc, si: si}
  1532. defer ds.Close()
  1533. return resultFromStatement(ctx, dc.ci, ds, args...)
  1534. }
  1535. // QueryContext executes a query that returns rows, typically a SELECT.
  1536. // The args are for any placeholder parameters in the query.
  1537. func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
  1538. var rows *Rows
  1539. var err error
  1540. var isBadConn bool
  1541. for i := 0; i < maxBadConnRetries; i++ {
  1542. rows, err = db.query(ctx, query, args, cachedOrNewConn)
  1543. isBadConn = errors.Is(err, driver.ErrBadConn)
  1544. if !isBadConn {
  1545. break
  1546. }
  1547. }
  1548. if isBadConn {
  1549. return db.query(ctx, query, args, alwaysNewConn)
  1550. }
  1551. return rows, err
  1552. }
  1553. // Query executes a query that returns rows, typically a SELECT.
  1554. // The args are for any placeholder parameters in the query.
  1555. //
  1556. // Query uses context.Background internally; to specify the context, use
  1557. // QueryContext.
  1558. func (db *DB) Query(query string, args ...any) (*Rows, error) {
  1559. return db.QueryContext(context.Background(), query, args...)
  1560. }
  1561. func (db *DB) query(ctx context.Context, query string, args []any, strategy connReuseStrategy) (*Rows, error) {
  1562. dc, err := db.conn(ctx, strategy)
  1563. if err != nil {
  1564. return nil, err
  1565. }
  1566. return db.queryDC(ctx, nil, dc, dc.releaseConn, query, args)
  1567. }
  1568. // queryDC executes a query on the given connection.
  1569. // The connection gets released by the releaseConn function.
  1570. // The ctx context is from a query method and the txctx context is from an
  1571. // optional transaction context.
  1572. func (db *DB) queryDC(ctx, txctx context.Context, dc *driverConn, releaseConn func(error), query string, args []any) (*Rows, error) {
  1573. queryerCtx, ok := dc.ci.(driver.QueryerContext)
  1574. var queryer driver.Queryer
  1575. if !ok {
  1576. queryer, ok = dc.ci.(driver.Queryer)
  1577. }
  1578. if ok {
  1579. var nvdargs []driver.NamedValue
  1580. var rowsi driver.Rows
  1581. var err error
  1582. withLock(dc, func() {
  1583. nvdargs, err = driverArgsConnLocked(dc.ci, nil, args)
  1584. if err != nil {
  1585. return
  1586. }
  1587. rowsi, err = ctxDriverQuery(ctx, queryerCtx, queryer, query, nvdargs)
  1588. })
  1589. if err != driver.ErrSkip {
  1590. if err != nil {
  1591. releaseConn(err)
  1592. return nil, err
  1593. }
  1594. // Note: ownership of dc passes to the *Rows, to be freed
  1595. // with releaseConn.
  1596. rows := &Rows{
  1597. dc: dc,
  1598. releaseConn: releaseConn,
  1599. rowsi: rowsi,
  1600. }
  1601. rows.initContextClose(ctx, txctx)
  1602. return rows, nil
  1603. }
  1604. }
  1605. var si driver.Stmt
  1606. var err error
  1607. withLock(dc, func() {
  1608. si, err = ctxDriverPrepare(ctx, dc.ci, query)
  1609. })
  1610. if err != nil {
  1611. releaseConn(err)
  1612. return nil, err
  1613. }
  1614. ds := &driverStmt{Locker: dc, si: si}
  1615. rowsi, err := rowsiFromStatement(ctx, dc.ci, ds, args...)
  1616. if err != nil {
  1617. ds.Close()
  1618. releaseConn(err)
  1619. return nil, err
  1620. }
  1621. // Note: ownership of ci passes to the *Rows, to be freed
  1622. // with releaseConn.
  1623. rows := &Rows{
  1624. dc: dc,
  1625. releaseConn: releaseConn,
  1626. rowsi: rowsi,
  1627. closeStmt: ds,
  1628. }
  1629. rows.initContextClose(ctx, txctx)
  1630. return rows, nil
  1631. }
  1632. // QueryRowContext executes a query that is expected to return at most one row.
  1633. // QueryRowContext always returns a non-nil value. Errors are deferred until
  1634. // Row's Scan method is called.
  1635. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  1636. // Otherwise, the *Row's Scan scans the first selected row and discards
  1637. // the rest.
  1638. func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
  1639. rows, err := db.QueryContext(ctx, query, args...)
  1640. return &Row{rows: rows, err: err}
  1641. }
  1642. // QueryRow executes a query that is expected to return at most one row.
  1643. // QueryRow always returns a non-nil value. Errors are deferred until
  1644. // Row's Scan method is called.
  1645. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  1646. // Otherwise, the *Row's Scan scans the first selected row and discards
  1647. // the rest.
  1648. //
  1649. // QueryRow uses context.Background internally; to specify the context, use
  1650. // QueryRowContext.
  1651. func (db *DB) QueryRow(query string, args ...any) *Row {
  1652. return db.QueryRowContext(context.Background(), query, args...)
  1653. }
  1654. // BeginTx starts a transaction.
  1655. //
  1656. // The provided context is used until the transaction is committed or rolled back.
  1657. // If the context is canceled, the sql package will roll back
  1658. // the transaction. Tx.Commit will return an error if the context provided to
  1659. // BeginTx is canceled.
  1660. //
  1661. // The provided TxOptions is optional and may be nil if defaults should be used.
  1662. // If a non-default isolation level is used that the driver doesn't support,
  1663. // an error will be returned.
  1664. func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
  1665. var tx *Tx
  1666. var err error
  1667. var isBadConn bool
  1668. for i := 0; i < maxBadConnRetries; i++ {
  1669. tx, err = db.begin(ctx, opts, cachedOrNewConn)
  1670. isBadConn = errors.Is(err, driver.ErrBadConn)
  1671. if !isBadConn {
  1672. break
  1673. }
  1674. }
  1675. if isBadConn {
  1676. return db.begin(ctx, opts, alwaysNewConn)
  1677. }
  1678. return tx, err
  1679. }
  1680. // Begin starts a transaction. The default isolation level is dependent on
  1681. // the driver.
  1682. //
  1683. // Begin uses context.Background internally; to specify the context, use
  1684. // BeginTx.
  1685. func (db *DB) Begin() (*Tx, error) {
  1686. return db.BeginTx(context.Background(), nil)
  1687. }
  1688. func (db *DB) begin(ctx context.Context, opts *TxOptions, strategy connReuseStrategy) (tx *Tx, err error) {
  1689. dc, err := db.conn(ctx, strategy)
  1690. if err != nil {
  1691. return nil, err
  1692. }
  1693. return db.beginDC(ctx, dc, dc.releaseConn, opts)
  1694. }
  1695. // beginDC starts a transaction. The provided dc must be valid and ready to use.
  1696. func (db *DB) beginDC(ctx context.Context, dc *driverConn, release func(error), opts *TxOptions) (tx *Tx, err error) {
  1697. var txi driver.Tx
  1698. keepConnOnRollback := false
  1699. withLock(dc, func() {
  1700. _, hasSessionResetter := dc.ci.(driver.SessionResetter)
  1701. _, hasConnectionValidator := dc.ci.(driver.Validator)
  1702. keepConnOnRollback = hasSessionResetter && hasConnectionValidator
  1703. txi, err = ctxDriverBegin(ctx, opts, dc.ci)
  1704. })
  1705. if err != nil {
  1706. release(err)
  1707. return nil, err
  1708. }
  1709. // Schedule the transaction to rollback when the context is canceled.
  1710. // The cancel function in Tx will be called after done is set to true.
  1711. ctx, cancel := context.WithCancel(ctx)
  1712. tx = &Tx{
  1713. db: db,
  1714. dc: dc,
  1715. releaseConn: release,
  1716. txi: txi,
  1717. cancel: cancel,
  1718. keepConnOnRollback: keepConnOnRollback,
  1719. ctx: ctx,
  1720. }
  1721. go tx.awaitDone()
  1722. return tx, nil
  1723. }
  1724. // Driver returns the database's underlying driver.
  1725. func (db *DB) Driver() driver.Driver {
  1726. return db.connector.Driver()
  1727. }
  1728. // ErrConnDone is returned by any operation that is performed on a connection
  1729. // that has already been returned to the connection pool.
  1730. var ErrConnDone = errors.New("sql: connection is already closed")
  1731. // Conn returns a single connection by either opening a new connection
  1732. // or returning an existing connection from the connection pool. Conn will
  1733. // block until either a connection is returned or ctx is canceled.
  1734. // Queries run on the same Conn will be run in the same database session.
  1735. //
  1736. // Every Conn must be returned to the database pool after use by
  1737. // calling Conn.Close.
  1738. func (db *DB) Conn(ctx context.Context) (*Conn, error) {
  1739. var dc *driverConn
  1740. var err error
  1741. var isBadConn bool
  1742. for i := 0; i < maxBadConnRetries; i++ {
  1743. dc, err = db.conn(ctx, cachedOrNewConn)
  1744. isBadConn = errors.Is(err, driver.ErrBadConn)
  1745. if !isBadConn {
  1746. break
  1747. }
  1748. }
  1749. if isBadConn {
  1750. dc, err = db.conn(ctx, alwaysNewConn)
  1751. }
  1752. if err != nil {
  1753. return nil, err
  1754. }
  1755. conn := &Conn{
  1756. db: db,
  1757. dc: dc,
  1758. }
  1759. return conn, nil
  1760. }
  1761. type releaseConn func(error)
  1762. // Conn represents a single database connection rather than a pool of database
  1763. // connections. Prefer running queries from DB unless there is a specific
  1764. // need for a continuous single database connection.
  1765. //
  1766. // A Conn must call Close to return the connection to the database pool
  1767. // and may do so concurrently with a running query.
  1768. //
  1769. // After a call to Close, all operations on the
  1770. // connection fail with ErrConnDone.
  1771. type Conn struct {
  1772. db *DB
  1773. // closemu prevents the connection from closing while there
  1774. // is an active query. It is held for read during queries
  1775. // and exclusively during close.
  1776. closemu sync.RWMutex
  1777. // dc is owned until close, at which point
  1778. // it's returned to the connection pool.
  1779. dc *driverConn
  1780. // done transitions from 0 to 1 exactly once, on close.
  1781. // Once done, all operations fail with ErrConnDone.
  1782. // Use atomic operations on value when checking value.
  1783. done int32
  1784. }
  1785. // grabConn takes a context to implement stmtConnGrabber
  1786. // but the context is not used.
  1787. func (c *Conn) grabConn(context.Context) (*driverConn, releaseConn, error) {
  1788. if atomic.LoadInt32(&c.done) != 0 {
  1789. return nil, nil, ErrConnDone
  1790. }
  1791. c.closemu.RLock()
  1792. return c.dc, c.closemuRUnlockCondReleaseConn, nil
  1793. }
  1794. // PingContext verifies the connection to the database is still alive.
  1795. func (c *Conn) PingContext(ctx context.Context) error {
  1796. dc, release, err := c.grabConn(ctx)
  1797. if err != nil {
  1798. return err
  1799. }
  1800. return c.db.pingDC(ctx, dc, release)
  1801. }
  1802. // ExecContext executes a query without returning any rows.
  1803. // The args are for any placeholder parameters in the query.
  1804. func (c *Conn) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
  1805. dc, release, err := c.grabConn(ctx)
  1806. if err != nil {
  1807. return nil, err
  1808. }
  1809. return c.db.execDC(ctx, dc, release, query, args)
  1810. }
  1811. // QueryContext executes a query that returns rows, typically a SELECT.
  1812. // The args are for any placeholder parameters in the query.
  1813. func (c *Conn) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
  1814. dc, release, err := c.grabConn(ctx)
  1815. if err != nil {
  1816. return nil, err
  1817. }
  1818. return c.db.queryDC(ctx, nil, dc, release, query, args)
  1819. }
  1820. // QueryRowContext executes a query that is expected to return at most one row.
  1821. // QueryRowContext always returns a non-nil value. Errors are deferred until
  1822. // Row's Scan method is called.
  1823. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  1824. // Otherwise, the *Row's Scan scans the first selected row and discards
  1825. // the rest.
  1826. func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
  1827. rows, err := c.QueryContext(ctx, query, args...)
  1828. return &Row{rows: rows, err: err}
  1829. }
  1830. // PrepareContext creates a prepared statement for later queries or executions.
  1831. // Multiple queries or executions may be run concurrently from the
  1832. // returned statement.
  1833. // The caller must call the statement's Close method
  1834. // when the statement is no longer needed.
  1835. //
  1836. // The provided context is used for the preparation of the statement, not for the
  1837. // execution of the statement.
  1838. func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  1839. dc, release, err := c.grabConn(ctx)
  1840. if err != nil {
  1841. return nil, err
  1842. }
  1843. return c.db.prepareDC(ctx, dc, release, c, query)
  1844. }
  1845. // Raw executes f exposing the underlying driver connection for the
  1846. // duration of f. The driverConn must not be used outside of f.
  1847. //
  1848. // Once f returns and err is not driver.ErrBadConn, the Conn will continue to be usable
  1849. // until Conn.Close is called.
  1850. func (c *Conn) Raw(f func(driverConn any) error) (err error) {
  1851. var dc *driverConn
  1852. var release releaseConn
  1853. // grabConn takes a context to implement stmtConnGrabber, but the context is not used.
  1854. dc, release, err = c.grabConn(nil)
  1855. if err != nil {
  1856. return
  1857. }
  1858. fPanic := true
  1859. dc.Mutex.Lock()
  1860. defer func() {
  1861. dc.Mutex.Unlock()
  1862. // If f panics fPanic will remain true.
  1863. // Ensure an error is passed to release so the connection
  1864. // may be discarded.
  1865. if fPanic {
  1866. err = driver.ErrBadConn
  1867. }
  1868. release(err)
  1869. }()
  1870. err = f(dc.ci)
  1871. fPanic = false
  1872. return
  1873. }
  1874. // BeginTx starts a transaction.
  1875. //
  1876. // The provided context is used until the transaction is committed or rolled back.
  1877. // If the context is canceled, the sql package will roll back
  1878. // the transaction. Tx.Commit will return an error if the context provided to
  1879. // BeginTx is canceled.
  1880. //
  1881. // The provided TxOptions is optional and may be nil if defaults should be used.
  1882. // If a non-default isolation level is used that the driver doesn't support,
  1883. // an error will be returned.
  1884. func (c *Conn) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error) {
  1885. dc, release, err := c.grabConn(ctx)
  1886. if err != nil {
  1887. return nil, err
  1888. }
  1889. return c.db.beginDC(ctx, dc, release, opts)
  1890. }
  1891. // closemuRUnlockCondReleaseConn read unlocks closemu
  1892. // as the sql operation is done with the dc.
  1893. func (c *Conn) closemuRUnlockCondReleaseConn(err error) {
  1894. c.closemu.RUnlock()
  1895. if errors.Is(err, driver.ErrBadConn) {
  1896. c.close(err)
  1897. }
  1898. }
  1899. func (c *Conn) txCtx() context.Context {
  1900. return nil
  1901. }
  1902. func (c *Conn) close(err error) error {
  1903. if !atomic.CompareAndSwapInt32(&c.done, 0, 1) {
  1904. return ErrConnDone
  1905. }
  1906. // Lock around releasing the driver connection
  1907. // to ensure all queries have been stopped before doing so.
  1908. c.closemu.Lock()
  1909. defer c.closemu.Unlock()
  1910. c.dc.releaseConn(err)
  1911. c.dc = nil
  1912. c.db = nil
  1913. return err
  1914. }
  1915. // Close returns the connection to the connection pool.
  1916. // All operations after a Close will return with ErrConnDone.
  1917. // Close is safe to call concurrently with other operations and will
  1918. // block until all other operations finish. It may be useful to first
  1919. // cancel any used context and then call close directly after.
  1920. func (c *Conn) Close() error {
  1921. return c.close(nil)
  1922. }
  1923. // Tx is an in-progress database transaction.
  1924. //
  1925. // A transaction must end with a call to Commit or Rollback.
  1926. //
  1927. // After a call to Commit or Rollback, all operations on the
  1928. // transaction fail with ErrTxDone.
  1929. //
  1930. // The statements prepared for a transaction by calling
  1931. // the transaction's Prepare or Stmt methods are closed
  1932. // by the call to Commit or Rollback.
  1933. type Tx struct {
  1934. db *DB
  1935. // closemu prevents the transaction from closing while there
  1936. // is an active query. It is held for read during queries
  1937. // and exclusively during close.
  1938. closemu sync.RWMutex
  1939. // dc is owned exclusively until Commit or Rollback, at which point
  1940. // it's returned with putConn.
  1941. dc *driverConn
  1942. txi driver.Tx
  1943. // releaseConn is called once the Tx is closed to release
  1944. // any held driverConn back to the pool.
  1945. releaseConn func(error)
  1946. // done transitions from 0 to 1 exactly once, on Commit
  1947. // or Rollback. once done, all operations fail with
  1948. // ErrTxDone.
  1949. // Use atomic operations on value when checking value.
  1950. done int32
  1951. // keepConnOnRollback is true if the driver knows
  1952. // how to reset the connection's session and if need be discard
  1953. // the connection.
  1954. keepConnOnRollback bool
  1955. // All Stmts prepared for this transaction. These will be closed after the
  1956. // transaction has been committed or rolled back.
  1957. stmts struct {
  1958. sync.Mutex
  1959. v []*Stmt
  1960. }
  1961. // cancel is called after done transitions from 0 to 1.
  1962. cancel func()
  1963. // ctx lives for the life of the transaction.
  1964. ctx context.Context
  1965. }
  1966. // awaitDone blocks until the context in Tx is canceled and rolls back
  1967. // the transaction if it's not already done.
  1968. func (tx *Tx) awaitDone() {
  1969. // Wait for either the transaction to be committed or rolled
  1970. // back, or for the associated context to be closed.
  1971. <-tx.ctx.Done()
  1972. // Discard and close the connection used to ensure the
  1973. // transaction is closed and the resources are released. This
  1974. // rollback does nothing if the transaction has already been
  1975. // committed or rolled back.
  1976. // Do not discard the connection if the connection knows
  1977. // how to reset the session.
  1978. discardConnection := !tx.keepConnOnRollback
  1979. tx.rollback(discardConnection)
  1980. }
  1981. func (tx *Tx) isDone() bool {
  1982. return atomic.LoadInt32(&tx.done) != 0
  1983. }
  1984. // ErrTxDone is returned by any operation that is performed on a transaction
  1985. // that has already been committed or rolled back.
  1986. var ErrTxDone = errors.New("sql: transaction has already been committed or rolled back")
  1987. // close returns the connection to the pool and
  1988. // must only be called by Tx.rollback or Tx.Commit while
  1989. // tx is already canceled and won't be executed concurrently.
  1990. func (tx *Tx) close(err error) {
  1991. tx.releaseConn(err)
  1992. tx.dc = nil
  1993. tx.txi = nil
  1994. }
  1995. // hookTxGrabConn specifies an optional hook to be called on
  1996. // a successful call to (*Tx).grabConn. For tests.
  1997. var hookTxGrabConn func()
  1998. func (tx *Tx) grabConn(ctx context.Context) (*driverConn, releaseConn, error) {
  1999. select {
  2000. default:
  2001. case <-ctx.Done():
  2002. return nil, nil, ctx.Err()
  2003. }
  2004. // closemu.RLock must come before the check for isDone to prevent the Tx from
  2005. // closing while a query is executing.
  2006. tx.closemu.RLock()
  2007. if tx.isDone() {
  2008. tx.closemu.RUnlock()
  2009. return nil, nil, ErrTxDone
  2010. }
  2011. if hookTxGrabConn != nil { // test hook
  2012. hookTxGrabConn()
  2013. }
  2014. return tx.dc, tx.closemuRUnlockRelease, nil
  2015. }
  2016. func (tx *Tx) txCtx() context.Context {
  2017. return tx.ctx
  2018. }
  2019. // closemuRUnlockRelease is used as a func(error) method value in
  2020. // ExecContext and QueryContext. Unlocking in the releaseConn keeps
  2021. // the driver conn from being returned to the connection pool until
  2022. // the Rows has been closed.
  2023. func (tx *Tx) closemuRUnlockRelease(error) {
  2024. tx.closemu.RUnlock()
  2025. }
  2026. // Closes all Stmts prepared for this transaction.
  2027. func (tx *Tx) closePrepared() {
  2028. tx.stmts.Lock()
  2029. defer tx.stmts.Unlock()
  2030. for _, stmt := range tx.stmts.v {
  2031. stmt.Close()
  2032. }
  2033. }
  2034. // Commit commits the transaction.
  2035. func (tx *Tx) Commit() error {
  2036. // Check context first to avoid transaction leak.
  2037. // If put it behind tx.done CompareAndSwap statement, we can't ensure
  2038. // the consistency between tx.done and the real COMMIT operation.
  2039. select {
  2040. default:
  2041. case <-tx.ctx.Done():
  2042. if atomic.LoadInt32(&tx.done) == 1 {
  2043. return ErrTxDone
  2044. }
  2045. return tx.ctx.Err()
  2046. }
  2047. if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
  2048. return ErrTxDone
  2049. }
  2050. // Cancel the Tx to release any active R-closemu locks.
  2051. // This is safe to do because tx.done has already transitioned
  2052. // from 0 to 1. Hold the W-closemu lock prior to rollback
  2053. // to ensure no other connection has an active query.
  2054. tx.cancel()
  2055. tx.closemu.Lock()
  2056. tx.closemu.Unlock()
  2057. var err error
  2058. withLock(tx.dc, func() {
  2059. err = tx.txi.Commit()
  2060. })
  2061. if !errors.Is(err, driver.ErrBadConn) {
  2062. tx.closePrepared()
  2063. }
  2064. tx.close(err)
  2065. return err
  2066. }
  2067. var rollbackHook func()
  2068. // rollback aborts the transaction and optionally forces the pool to discard
  2069. // the connection.
  2070. func (tx *Tx) rollback(discardConn bool) error {
  2071. if !atomic.CompareAndSwapInt32(&tx.done, 0, 1) {
  2072. return ErrTxDone
  2073. }
  2074. if rollbackHook != nil {
  2075. rollbackHook()
  2076. }
  2077. // Cancel the Tx to release any active R-closemu locks.
  2078. // This is safe to do because tx.done has already transitioned
  2079. // from 0 to 1. Hold the W-closemu lock prior to rollback
  2080. // to ensure no other connection has an active query.
  2081. tx.cancel()
  2082. tx.closemu.Lock()
  2083. tx.closemu.Unlock()
  2084. var err error
  2085. withLock(tx.dc, func() {
  2086. err = tx.txi.Rollback()
  2087. })
  2088. if !errors.Is(err, driver.ErrBadConn) {
  2089. tx.closePrepared()
  2090. }
  2091. if discardConn {
  2092. err = driver.ErrBadConn
  2093. }
  2094. tx.close(err)
  2095. return err
  2096. }
  2097. // Rollback aborts the transaction.
  2098. func (tx *Tx) Rollback() error {
  2099. return tx.rollback(false)
  2100. }
  2101. // PrepareContext creates a prepared statement for use within a transaction.
  2102. //
  2103. // The returned statement operates within the transaction and will be closed
  2104. // when the transaction has been committed or rolled back.
  2105. //
  2106. // To use an existing prepared statement on this transaction, see Tx.Stmt.
  2107. //
  2108. // The provided context will be used for the preparation of the context, not
  2109. // for the execution of the returned statement. The returned statement
  2110. // will run in the transaction context.
  2111. func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error) {
  2112. dc, release, err := tx.grabConn(ctx)
  2113. if err != nil {
  2114. return nil, err
  2115. }
  2116. stmt, err := tx.db.prepareDC(ctx, dc, release, tx, query)
  2117. if err != nil {
  2118. return nil, err
  2119. }
  2120. tx.stmts.Lock()
  2121. tx.stmts.v = append(tx.stmts.v, stmt)
  2122. tx.stmts.Unlock()
  2123. return stmt, nil
  2124. }
  2125. // Prepare creates a prepared statement for use within a transaction.
  2126. //
  2127. // The returned statement operates within the transaction and will be closed
  2128. // when the transaction has been committed or rolled back.
  2129. //
  2130. // To use an existing prepared statement on this transaction, see Tx.Stmt.
  2131. //
  2132. // Prepare uses context.Background internally; to specify the context, use
  2133. // PrepareContext.
  2134. func (tx *Tx) Prepare(query string) (*Stmt, error) {
  2135. return tx.PrepareContext(context.Background(), query)
  2136. }
  2137. // StmtContext returns a transaction-specific prepared statement from
  2138. // an existing statement.
  2139. //
  2140. // Example:
  2141. // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  2142. // ...
  2143. // tx, err := db.Begin()
  2144. // ...
  2145. // res, err := tx.StmtContext(ctx, updateMoney).Exec(123.45, 98293203)
  2146. //
  2147. // The provided context is used for the preparation of the statement, not for the
  2148. // execution of the statement.
  2149. //
  2150. // The returned statement operates within the transaction and will be closed
  2151. // when the transaction has been committed or rolled back.
  2152. func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt {
  2153. dc, release, err := tx.grabConn(ctx)
  2154. if err != nil {
  2155. return &Stmt{stickyErr: err}
  2156. }
  2157. defer release(nil)
  2158. if tx.db != stmt.db {
  2159. return &Stmt{stickyErr: errors.New("sql: Tx.Stmt: statement from different database used")}
  2160. }
  2161. var si driver.Stmt
  2162. var parentStmt *Stmt
  2163. stmt.mu.Lock()
  2164. if stmt.closed || stmt.cg != nil {
  2165. // If the statement has been closed or already belongs to a
  2166. // transaction, we can't reuse it in this connection.
  2167. // Since tx.StmtContext should never need to be called with a
  2168. // Stmt already belonging to tx, we ignore this edge case and
  2169. // re-prepare the statement in this case. No need to add
  2170. // code-complexity for this.
  2171. stmt.mu.Unlock()
  2172. withLock(dc, func() {
  2173. si, err = ctxDriverPrepare(ctx, dc.ci, stmt.query)
  2174. })
  2175. if err != nil {
  2176. return &Stmt{stickyErr: err}
  2177. }
  2178. } else {
  2179. stmt.removeClosedStmtLocked()
  2180. // See if the statement has already been prepared on this connection,
  2181. // and reuse it if possible.
  2182. for _, v := range stmt.css {
  2183. if v.dc == dc {
  2184. si = v.ds.si
  2185. break
  2186. }
  2187. }
  2188. stmt.mu.Unlock()
  2189. if si == nil {
  2190. var ds *driverStmt
  2191. withLock(dc, func() {
  2192. ds, err = stmt.prepareOnConnLocked(ctx, dc)
  2193. })
  2194. if err != nil {
  2195. return &Stmt{stickyErr: err}
  2196. }
  2197. si = ds.si
  2198. }
  2199. parentStmt = stmt
  2200. }
  2201. txs := &Stmt{
  2202. db: tx.db,
  2203. cg: tx,
  2204. cgds: &driverStmt{
  2205. Locker: dc,
  2206. si: si,
  2207. },
  2208. parentStmt: parentStmt,
  2209. query: stmt.query,
  2210. }
  2211. if parentStmt != nil {
  2212. tx.db.addDep(parentStmt, txs)
  2213. }
  2214. tx.stmts.Lock()
  2215. tx.stmts.v = append(tx.stmts.v, txs)
  2216. tx.stmts.Unlock()
  2217. return txs
  2218. }
  2219. // Stmt returns a transaction-specific prepared statement from
  2220. // an existing statement.
  2221. //
  2222. // Example:
  2223. // updateMoney, err := db.Prepare("UPDATE balance SET money=money+? WHERE id=?")
  2224. // ...
  2225. // tx, err := db.Begin()
  2226. // ...
  2227. // res, err := tx.Stmt(updateMoney).Exec(123.45, 98293203)
  2228. //
  2229. // The returned statement operates within the transaction and will be closed
  2230. // when the transaction has been committed or rolled back.
  2231. //
  2232. // Stmt uses context.Background internally; to specify the context, use
  2233. // StmtContext.
  2234. func (tx *Tx) Stmt(stmt *Stmt) *Stmt {
  2235. return tx.StmtContext(context.Background(), stmt)
  2236. }
  2237. // ExecContext executes a query that doesn't return rows.
  2238. // For example: an INSERT and UPDATE.
  2239. func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (Result, error) {
  2240. dc, release, err := tx.grabConn(ctx)
  2241. if err != nil {
  2242. return nil, err
  2243. }
  2244. return tx.db.execDC(ctx, dc, release, query, args)
  2245. }
  2246. // Exec executes a query that doesn't return rows.
  2247. // For example: an INSERT and UPDATE.
  2248. //
  2249. // Exec uses context.Background internally; to specify the context, use
  2250. // ExecContext.
  2251. func (tx *Tx) Exec(query string, args ...any) (Result, error) {
  2252. return tx.ExecContext(context.Background(), query, args...)
  2253. }
  2254. // QueryContext executes a query that returns rows, typically a SELECT.
  2255. func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) {
  2256. dc, release, err := tx.grabConn(ctx)
  2257. if err != nil {
  2258. return nil, err
  2259. }
  2260. return tx.db.queryDC(ctx, tx.ctx, dc, release, query, args)
  2261. }
  2262. // Query executes a query that returns rows, typically a SELECT.
  2263. //
  2264. // Query uses context.Background internally; to specify the context, use
  2265. // QueryContext.
  2266. func (tx *Tx) Query(query string, args ...any) (*Rows, error) {
  2267. return tx.QueryContext(context.Background(), query, args...)
  2268. }
  2269. // QueryRowContext executes a query that is expected to return at most one row.
  2270. // QueryRowContext always returns a non-nil value. Errors are deferred until
  2271. // Row's Scan method is called.
  2272. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2273. // Otherwise, the *Row's Scan scans the first selected row and discards
  2274. // the rest.
  2275. func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *Row {
  2276. rows, err := tx.QueryContext(ctx, query, args...)
  2277. return &Row{rows: rows, err: err}
  2278. }
  2279. // QueryRow executes a query that is expected to return at most one row.
  2280. // QueryRow always returns a non-nil value. Errors are deferred until
  2281. // Row's Scan method is called.
  2282. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2283. // Otherwise, the *Row's Scan scans the first selected row and discards
  2284. // the rest.
  2285. //
  2286. // QueryRow uses context.Background internally; to specify the context, use
  2287. // QueryRowContext.
  2288. func (tx *Tx) QueryRow(query string, args ...any) *Row {
  2289. return tx.QueryRowContext(context.Background(), query, args...)
  2290. }
  2291. // connStmt is a prepared statement on a particular connection.
  2292. type connStmt struct {
  2293. dc *driverConn
  2294. ds *driverStmt
  2295. }
  2296. // stmtConnGrabber represents a Tx or Conn that will return the underlying
  2297. // driverConn and release function.
  2298. type stmtConnGrabber interface {
  2299. // grabConn returns the driverConn and the associated release function
  2300. // that must be called when the operation completes.
  2301. grabConn(context.Context) (*driverConn, releaseConn, error)
  2302. // txCtx returns the transaction context if available.
  2303. // The returned context should be selected on along with
  2304. // any query context when awaiting a cancel.
  2305. txCtx() context.Context
  2306. }
  2307. var (
  2308. _ stmtConnGrabber = &Tx{}
  2309. _ stmtConnGrabber = &Conn{}
  2310. )
  2311. // Stmt is a prepared statement.
  2312. // A Stmt is safe for concurrent use by multiple goroutines.
  2313. //
  2314. // If a Stmt is prepared on a Tx or Conn, it will be bound to a single
  2315. // underlying connection forever. If the Tx or Conn closes, the Stmt will
  2316. // become unusable and all operations will return an error.
  2317. // If a Stmt is prepared on a DB, it will remain usable for the lifetime of the
  2318. // DB. When the Stmt needs to execute on a new underlying connection, it will
  2319. // prepare itself on the new connection automatically.
  2320. type Stmt struct {
  2321. // Immutable:
  2322. db *DB // where we came from
  2323. query string // that created the Stmt
  2324. stickyErr error // if non-nil, this error is returned for all operations
  2325. closemu sync.RWMutex // held exclusively during close, for read otherwise.
  2326. // If Stmt is prepared on a Tx or Conn then cg is present and will
  2327. // only ever grab a connection from cg.
  2328. // If cg is nil then the Stmt must grab an arbitrary connection
  2329. // from db and determine if it must prepare the stmt again by
  2330. // inspecting css.
  2331. cg stmtConnGrabber
  2332. cgds *driverStmt
  2333. // parentStmt is set when a transaction-specific statement
  2334. // is requested from an identical statement prepared on the same
  2335. // conn. parentStmt is used to track the dependency of this statement
  2336. // on its originating ("parent") statement so that parentStmt may
  2337. // be closed by the user without them having to know whether or not
  2338. // any transactions are still using it.
  2339. parentStmt *Stmt
  2340. mu sync.Mutex // protects the rest of the fields
  2341. closed bool
  2342. // css is a list of underlying driver statement interfaces
  2343. // that are valid on particular connections. This is only
  2344. // used if cg == nil and one is found that has idle
  2345. // connections. If cg != nil, cgds is always used.
  2346. css []connStmt
  2347. // lastNumClosed is copied from db.numClosed when Stmt is created
  2348. // without tx and closed connections in css are removed.
  2349. lastNumClosed uint64
  2350. }
  2351. // ExecContext executes a prepared statement with the given arguments and
  2352. // returns a Result summarizing the effect of the statement.
  2353. func (s *Stmt) ExecContext(ctx context.Context, args ...any) (Result, error) {
  2354. s.closemu.RLock()
  2355. defer s.closemu.RUnlock()
  2356. var res Result
  2357. strategy := cachedOrNewConn
  2358. for i := 0; i < maxBadConnRetries+1; i++ {
  2359. if i == maxBadConnRetries {
  2360. strategy = alwaysNewConn
  2361. }
  2362. dc, releaseConn, ds, err := s.connStmt(ctx, strategy)
  2363. if err != nil {
  2364. if errors.Is(err, driver.ErrBadConn) {
  2365. continue
  2366. }
  2367. return nil, err
  2368. }
  2369. res, err = resultFromStatement(ctx, dc.ci, ds, args...)
  2370. releaseConn(err)
  2371. if !errors.Is(err, driver.ErrBadConn) {
  2372. return res, err
  2373. }
  2374. }
  2375. return nil, driver.ErrBadConn
  2376. }
  2377. // Exec executes a prepared statement with the given arguments and
  2378. // returns a Result summarizing the effect of the statement.
  2379. //
  2380. // Exec uses context.Background internally; to specify the context, use
  2381. // ExecContext.
  2382. func (s *Stmt) Exec(args ...any) (Result, error) {
  2383. return s.ExecContext(context.Background(), args...)
  2384. }
  2385. func resultFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, args ...any) (Result, error) {
  2386. ds.Lock()
  2387. defer ds.Unlock()
  2388. dargs, err := driverArgsConnLocked(ci, ds, args)
  2389. if err != nil {
  2390. return nil, err
  2391. }
  2392. resi, err := ctxDriverStmtExec(ctx, ds.si, dargs)
  2393. if err != nil {
  2394. return nil, err
  2395. }
  2396. return driverResult{ds.Locker, resi}, nil
  2397. }
  2398. // removeClosedStmtLocked removes closed conns in s.css.
  2399. //
  2400. // To avoid lock contention on DB.mu, we do it only when
  2401. // s.db.numClosed - s.lastNum is large enough.
  2402. func (s *Stmt) removeClosedStmtLocked() {
  2403. t := len(s.css)/2 + 1
  2404. if t > 10 {
  2405. t = 10
  2406. }
  2407. dbClosed := atomic.LoadUint64(&s.db.numClosed)
  2408. if dbClosed-s.lastNumClosed < uint64(t) {
  2409. return
  2410. }
  2411. s.db.mu.Lock()
  2412. for i := 0; i < len(s.css); i++ {
  2413. if s.css[i].dc.dbmuClosed {
  2414. s.css[i] = s.css[len(s.css)-1]
  2415. s.css = s.css[:len(s.css)-1]
  2416. i--
  2417. }
  2418. }
  2419. s.db.mu.Unlock()
  2420. s.lastNumClosed = dbClosed
  2421. }
  2422. // connStmt returns a free driver connection on which to execute the
  2423. // statement, a function to call to release the connection, and a
  2424. // statement bound to that connection.
  2425. func (s *Stmt) connStmt(ctx context.Context, strategy connReuseStrategy) (dc *driverConn, releaseConn func(error), ds *driverStmt, err error) {
  2426. if err = s.stickyErr; err != nil {
  2427. return
  2428. }
  2429. s.mu.Lock()
  2430. if s.closed {
  2431. s.mu.Unlock()
  2432. err = errors.New("sql: statement is closed")
  2433. return
  2434. }
  2435. // In a transaction or connection, we always use the connection that the
  2436. // stmt was created on.
  2437. if s.cg != nil {
  2438. s.mu.Unlock()
  2439. dc, releaseConn, err = s.cg.grabConn(ctx) // blocks, waiting for the connection.
  2440. if err != nil {
  2441. return
  2442. }
  2443. return dc, releaseConn, s.cgds, nil
  2444. }
  2445. s.removeClosedStmtLocked()
  2446. s.mu.Unlock()
  2447. dc, err = s.db.conn(ctx, strategy)
  2448. if err != nil {
  2449. return nil, nil, nil, err
  2450. }
  2451. s.mu.Lock()
  2452. for _, v := range s.css {
  2453. if v.dc == dc {
  2454. s.mu.Unlock()
  2455. return dc, dc.releaseConn, v.ds, nil
  2456. }
  2457. }
  2458. s.mu.Unlock()
  2459. // No luck; we need to prepare the statement on this connection
  2460. withLock(dc, func() {
  2461. ds, err = s.prepareOnConnLocked(ctx, dc)
  2462. })
  2463. if err != nil {
  2464. dc.releaseConn(err)
  2465. return nil, nil, nil, err
  2466. }
  2467. return dc, dc.releaseConn, ds, nil
  2468. }
  2469. // prepareOnConnLocked prepares the query in Stmt s on dc and adds it to the list of
  2470. // open connStmt on the statement. It assumes the caller is holding the lock on dc.
  2471. func (s *Stmt) prepareOnConnLocked(ctx context.Context, dc *driverConn) (*driverStmt, error) {
  2472. si, err := dc.prepareLocked(ctx, s.cg, s.query)
  2473. if err != nil {
  2474. return nil, err
  2475. }
  2476. cs := connStmt{dc, si}
  2477. s.mu.Lock()
  2478. s.css = append(s.css, cs)
  2479. s.mu.Unlock()
  2480. return cs.ds, nil
  2481. }
  2482. // QueryContext executes a prepared query statement with the given arguments
  2483. // and returns the query results as a *Rows.
  2484. func (s *Stmt) QueryContext(ctx context.Context, args ...any) (*Rows, error) {
  2485. s.closemu.RLock()
  2486. defer s.closemu.RUnlock()
  2487. var rowsi driver.Rows
  2488. strategy := cachedOrNewConn
  2489. for i := 0; i < maxBadConnRetries+1; i++ {
  2490. if i == maxBadConnRetries {
  2491. strategy = alwaysNewConn
  2492. }
  2493. dc, releaseConn, ds, err := s.connStmt(ctx, strategy)
  2494. if err != nil {
  2495. if errors.Is(err, driver.ErrBadConn) {
  2496. continue
  2497. }
  2498. return nil, err
  2499. }
  2500. rowsi, err = rowsiFromStatement(ctx, dc.ci, ds, args...)
  2501. if err == nil {
  2502. // Note: ownership of ci passes to the *Rows, to be freed
  2503. // with releaseConn.
  2504. rows := &Rows{
  2505. dc: dc,
  2506. rowsi: rowsi,
  2507. // releaseConn set below
  2508. }
  2509. // addDep must be added before initContextClose or it could attempt
  2510. // to removeDep before it has been added.
  2511. s.db.addDep(s, rows)
  2512. // releaseConn must be set before initContextClose or it could
  2513. // release the connection before it is set.
  2514. rows.releaseConn = func(err error) {
  2515. releaseConn(err)
  2516. s.db.removeDep(s, rows)
  2517. }
  2518. var txctx context.Context
  2519. if s.cg != nil {
  2520. txctx = s.cg.txCtx()
  2521. }
  2522. rows.initContextClose(ctx, txctx)
  2523. return rows, nil
  2524. }
  2525. releaseConn(err)
  2526. if !errors.Is(err, driver.ErrBadConn) {
  2527. return nil, err
  2528. }
  2529. }
  2530. return nil, driver.ErrBadConn
  2531. }
  2532. // Query executes a prepared query statement with the given arguments
  2533. // and returns the query results as a *Rows.
  2534. //
  2535. // Query uses context.Background internally; to specify the context, use
  2536. // QueryContext.
  2537. func (s *Stmt) Query(args ...any) (*Rows, error) {
  2538. return s.QueryContext(context.Background(), args...)
  2539. }
  2540. func rowsiFromStatement(ctx context.Context, ci driver.Conn, ds *driverStmt, args ...any) (driver.Rows, error) {
  2541. ds.Lock()
  2542. defer ds.Unlock()
  2543. dargs, err := driverArgsConnLocked(ci, ds, args)
  2544. if err != nil {
  2545. return nil, err
  2546. }
  2547. return ctxDriverStmtQuery(ctx, ds.si, dargs)
  2548. }
  2549. // QueryRowContext executes a prepared query statement with the given arguments.
  2550. // If an error occurs during the execution of the statement, that error will
  2551. // be returned by a call to Scan on the returned *Row, which is always non-nil.
  2552. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2553. // Otherwise, the *Row's Scan scans the first selected row and discards
  2554. // the rest.
  2555. func (s *Stmt) QueryRowContext(ctx context.Context, args ...any) *Row {
  2556. rows, err := s.QueryContext(ctx, args...)
  2557. if err != nil {
  2558. return &Row{err: err}
  2559. }
  2560. return &Row{rows: rows}
  2561. }
  2562. // QueryRow executes a prepared query statement with the given arguments.
  2563. // If an error occurs during the execution of the statement, that error will
  2564. // be returned by a call to Scan on the returned *Row, which is always non-nil.
  2565. // If the query selects no rows, the *Row's Scan will return ErrNoRows.
  2566. // Otherwise, the *Row's Scan scans the first selected row and discards
  2567. // the rest.
  2568. //
  2569. // Example usage:
  2570. //
  2571. // var name string
  2572. // err := nameByUseridStmt.QueryRow(id).Scan(&name)
  2573. //
  2574. // QueryRow uses context.Background internally; to specify the context, use
  2575. // QueryRowContext.
  2576. func (s *Stmt) QueryRow(args ...any) *Row {
  2577. return s.QueryRowContext(context.Background(), args...)
  2578. }
  2579. // Close closes the statement.
  2580. func (s *Stmt) Close() error {
  2581. s.closemu.Lock()
  2582. defer s.closemu.Unlock()
  2583. if s.stickyErr != nil {
  2584. return s.stickyErr
  2585. }
  2586. s.mu.Lock()
  2587. if s.closed {
  2588. s.mu.Unlock()
  2589. return nil
  2590. }
  2591. s.closed = true
  2592. txds := s.cgds
  2593. s.cgds = nil
  2594. s.mu.Unlock()
  2595. if s.cg == nil {
  2596. return s.db.removeDep(s, s)
  2597. }
  2598. if s.parentStmt != nil {
  2599. // If parentStmt is set, we must not close s.txds since it's stored
  2600. // in the css array of the parentStmt.
  2601. return s.db.removeDep(s.parentStmt, s)
  2602. }
  2603. return txds.Close()
  2604. }
  2605. func (s *Stmt) finalClose() error {
  2606. s.mu.Lock()
  2607. defer s.mu.Unlock()
  2608. if s.css != nil {
  2609. for _, v := range s.css {
  2610. s.db.noteUnusedDriverStatement(v.dc, v.ds)
  2611. v.dc.removeOpenStmt(v.ds)
  2612. }
  2613. s.css = nil
  2614. }
  2615. return nil
  2616. }
  2617. // Rows is the result of a query. Its cursor starts before the first row
  2618. // of the result set. Use Next to advance from row to row.
  2619. type Rows struct {
  2620. dc *driverConn // owned; must call releaseConn when closed to release
  2621. releaseConn func(error)
  2622. rowsi driver.Rows
  2623. cancel func() // called when Rows is closed, may be nil.
  2624. closeStmt *driverStmt // if non-nil, statement to Close on close
  2625. // closemu prevents Rows from closing while there
  2626. // is an active streaming result. It is held for read during non-close operations
  2627. // and exclusively during close.
  2628. //
  2629. // closemu guards lasterr and closed.
  2630. closemu sync.RWMutex
  2631. closed bool
  2632. lasterr error // non-nil only if closed is true
  2633. // lastcols is only used in Scan, Next, and NextResultSet which are expected
  2634. // not to be called concurrently.
  2635. lastcols []driver.Value
  2636. }
  2637. // lasterrOrErrLocked returns either lasterr or the provided err.
  2638. // rs.closemu must be read-locked.
  2639. func (rs *Rows) lasterrOrErrLocked(err error) error {
  2640. if rs.lasterr != nil && rs.lasterr != io.EOF {
  2641. return rs.lasterr
  2642. }
  2643. return err
  2644. }
  2645. // bypassRowsAwaitDone is only used for testing.
  2646. // If true, it will not close the Rows automatically from the context.
  2647. var bypassRowsAwaitDone = false
  2648. func (rs *Rows) initContextClose(ctx, txctx context.Context) {
  2649. if ctx.Done() == nil && (txctx == nil || txctx.Done() == nil) {
  2650. return
  2651. }
  2652. if bypassRowsAwaitDone {
  2653. return
  2654. }
  2655. ctx, rs.cancel = context.WithCancel(ctx)
  2656. go rs.awaitDone(ctx, txctx)
  2657. }
  2658. // awaitDone blocks until either ctx or txctx is canceled. The ctx is provided
  2659. // from the query context and is canceled when the query Rows is closed.
  2660. // If the query was issued in a transaction, the transaction's context
  2661. // is also provided in txctx to ensure Rows is closed if the Tx is closed.
  2662. func (rs *Rows) awaitDone(ctx, txctx context.Context) {
  2663. var txctxDone <-chan struct{}
  2664. if txctx != nil {
  2665. txctxDone = txctx.Done()
  2666. }
  2667. select {
  2668. case <-ctx.Done():
  2669. case <-txctxDone:
  2670. }
  2671. rs.close(ctx.Err())
  2672. }
  2673. // Next prepares the next result row for reading with the Scan method. It
  2674. // returns true on success, or false if there is no next result row or an error
  2675. // happened while preparing it. Err should be consulted to distinguish between
  2676. // the two cases.
  2677. //
  2678. // Every call to Scan, even the first one, must be preceded by a call to Next.
  2679. func (rs *Rows) Next() bool {
  2680. var doClose, ok bool
  2681. withLock(rs.closemu.RLocker(), func() {
  2682. doClose, ok = rs.nextLocked()
  2683. })
  2684. if doClose {
  2685. rs.Close()
  2686. }
  2687. return ok
  2688. }
  2689. func (rs *Rows) nextLocked() (doClose, ok bool) {
  2690. if rs.closed {
  2691. return false, false
  2692. }
  2693. // Lock the driver connection before calling the driver interface
  2694. // rowsi to prevent a Tx from rolling back the connection at the same time.
  2695. rs.dc.Lock()
  2696. defer rs.dc.Unlock()
  2697. if rs.lastcols == nil {
  2698. rs.lastcols = make([]driver.Value, len(rs.rowsi.Columns()))
  2699. }
  2700. rs.lasterr = rs.rowsi.Next(rs.lastcols)
  2701. if rs.lasterr != nil {
  2702. // Close the connection if there is a driver error.
  2703. if rs.lasterr != io.EOF {
  2704. return true, false
  2705. }
  2706. nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
  2707. if !ok {
  2708. return true, false
  2709. }
  2710. // The driver is at the end of the current result set.
  2711. // Test to see if there is another result set after the current one.
  2712. // Only close Rows if there is no further result sets to read.
  2713. if !nextResultSet.HasNextResultSet() {
  2714. doClose = true
  2715. }
  2716. return doClose, false
  2717. }
  2718. return false, true
  2719. }
  2720. // NextResultSet prepares the next result set for reading. It reports whether
  2721. // there is further result sets, or false if there is no further result set
  2722. // or if there is an error advancing to it. The Err method should be consulted
  2723. // to distinguish between the two cases.
  2724. //
  2725. // After calling NextResultSet, the Next method should always be called before
  2726. // scanning. If there are further result sets they may not have rows in the result
  2727. // set.
  2728. func (rs *Rows) NextResultSet() bool {
  2729. var doClose bool
  2730. defer func() {
  2731. if doClose {
  2732. rs.Close()
  2733. }
  2734. }()
  2735. rs.closemu.RLock()
  2736. defer rs.closemu.RUnlock()
  2737. if rs.closed {
  2738. return false
  2739. }
  2740. rs.lastcols = nil
  2741. nextResultSet, ok := rs.rowsi.(driver.RowsNextResultSet)
  2742. if !ok {
  2743. doClose = true
  2744. return false
  2745. }
  2746. // Lock the driver connection before calling the driver interface
  2747. // rowsi to prevent a Tx from rolling back the connection at the same time.
  2748. rs.dc.Lock()
  2749. defer rs.dc.Unlock()
  2750. rs.lasterr = nextResultSet.NextResultSet()
  2751. if rs.lasterr != nil {
  2752. doClose = true
  2753. return false
  2754. }
  2755. return true
  2756. }
  2757. // Err returns the error, if any, that was encountered during iteration.
  2758. // Err may be called after an explicit or implicit Close.
  2759. func (rs *Rows) Err() error {
  2760. rs.closemu.RLock()
  2761. defer rs.closemu.RUnlock()
  2762. return rs.lasterrOrErrLocked(nil)
  2763. }
  2764. var errRowsClosed = errors.New("sql: Rows are closed")
  2765. var errNoRows = errors.New("sql: no Rows available")
  2766. // Columns returns the column names.
  2767. // Columns returns an error if the rows are closed.
  2768. func (rs *Rows) Columns() ([]string, error) {
  2769. rs.closemu.RLock()
  2770. defer rs.closemu.RUnlock()
  2771. if rs.closed {
  2772. return nil, rs.lasterrOrErrLocked(errRowsClosed)
  2773. }
  2774. if rs.rowsi == nil {
  2775. return nil, rs.lasterrOrErrLocked(errNoRows)
  2776. }
  2777. rs.dc.Lock()
  2778. defer rs.dc.Unlock()
  2779. return rs.rowsi.Columns(), nil
  2780. }
  2781. // ColumnTypes returns column information such as column type, length,
  2782. // and nullable. Some information may not be available from some drivers.
  2783. func (rs *Rows) ColumnTypes() ([]*ColumnType, error) {
  2784. rs.closemu.RLock()
  2785. defer rs.closemu.RUnlock()
  2786. if rs.closed {
  2787. return nil, rs.lasterrOrErrLocked(errRowsClosed)
  2788. }
  2789. if rs.rowsi == nil {
  2790. return nil, rs.lasterrOrErrLocked(errNoRows)
  2791. }
  2792. rs.dc.Lock()
  2793. defer rs.dc.Unlock()
  2794. return rowsColumnInfoSetupConnLocked(rs.rowsi), nil
  2795. }
  2796. // ColumnType contains the name and type of a column.
  2797. type ColumnType struct {
  2798. name string
  2799. hasNullable bool
  2800. hasLength bool
  2801. hasPrecisionScale bool
  2802. nullable bool
  2803. length int64
  2804. databaseType string
  2805. precision int64
  2806. scale int64
  2807. scanType reflect.Type
  2808. }
  2809. // Name returns the name or alias of the column.
  2810. func (ci *ColumnType) Name() string {
  2811. return ci.name
  2812. }
  2813. // Length returns the column type length for variable length column types such
  2814. // as text and binary field types. If the type length is unbounded the value will
  2815. // be math.MaxInt64 (any database limits will still apply).
  2816. // If the column type is not variable length, such as an int, or if not supported
  2817. // by the driver ok is false.
  2818. func (ci *ColumnType) Length() (length int64, ok bool) {
  2819. return ci.length, ci.hasLength
  2820. }
  2821. // DecimalSize returns the scale and precision of a decimal type.
  2822. // If not applicable or if not supported ok is false.
  2823. func (ci *ColumnType) DecimalSize() (precision, scale int64, ok bool) {
  2824. return ci.precision, ci.scale, ci.hasPrecisionScale
  2825. }
  2826. // ScanType returns a Go type suitable for scanning into using Rows.Scan.
  2827. // If a driver does not support this property ScanType will return
  2828. // the type of an empty interface.
  2829. func (ci *ColumnType) ScanType() reflect.Type {
  2830. return ci.scanType
  2831. }
  2832. // Nullable reports whether the column may be null.
  2833. // If a driver does not support this property ok will be false.
  2834. func (ci *ColumnType) Nullable() (nullable, ok bool) {
  2835. return ci.nullable, ci.hasNullable
  2836. }
  2837. // DatabaseTypeName returns the database system name of the column type. If an empty
  2838. // string is returned, then the driver type name is not supported.
  2839. // Consult your driver documentation for a list of driver data types. Length specifiers
  2840. // are not included.
  2841. // Common type names include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL",
  2842. // "INT", and "BIGINT".
  2843. func (ci *ColumnType) DatabaseTypeName() string {
  2844. return ci.databaseType
  2845. }
  2846. func rowsColumnInfoSetupConnLocked(rowsi driver.Rows) []*ColumnType {
  2847. names := rowsi.Columns()
  2848. list := make([]*ColumnType, len(names))
  2849. for i := range list {
  2850. ci := &ColumnType{
  2851. name: names[i],
  2852. }
  2853. list[i] = ci
  2854. if prop, ok := rowsi.(driver.RowsColumnTypeScanType); ok {
  2855. ci.scanType = prop.ColumnTypeScanType(i)
  2856. } else {
  2857. ci.scanType = reflect.TypeOf(new(any)).Elem()
  2858. }
  2859. if prop, ok := rowsi.(driver.RowsColumnTypeDatabaseTypeName); ok {
  2860. ci.databaseType = prop.ColumnTypeDatabaseTypeName(i)
  2861. }
  2862. if prop, ok := rowsi.(driver.RowsColumnTypeLength); ok {
  2863. ci.length, ci.hasLength = prop.ColumnTypeLength(i)
  2864. }
  2865. if prop, ok := rowsi.(driver.RowsColumnTypeNullable); ok {
  2866. ci.nullable, ci.hasNullable = prop.ColumnTypeNullable(i)
  2867. }
  2868. if prop, ok := rowsi.(driver.RowsColumnTypePrecisionScale); ok {
  2869. ci.precision, ci.scale, ci.hasPrecisionScale = prop.ColumnTypePrecisionScale(i)
  2870. }
  2871. }
  2872. return list
  2873. }
  2874. // Scan copies the columns in the current row into the values pointed
  2875. // at by dest. The number of values in dest must be the same as the
  2876. // number of columns in Rows.
  2877. //
  2878. // Scan converts columns read from the database into the following
  2879. // common Go types and special types provided by the sql package:
  2880. //
  2881. // *string
  2882. // *[]byte
  2883. // *int, *int8, *int16, *int32, *int64
  2884. // *uint, *uint8, *uint16, *uint32, *uint64
  2885. // *bool
  2886. // *float32, *float64
  2887. // *interface{}
  2888. // *RawBytes
  2889. // *Rows (cursor value)
  2890. // any type implementing Scanner (see Scanner docs)
  2891. //
  2892. // In the most simple case, if the type of the value from the source
  2893. // column is an integer, bool or string type T and dest is of type *T,
  2894. // Scan simply assigns the value through the pointer.
  2895. //
  2896. // Scan also converts between string and numeric types, as long as no
  2897. // information would be lost. While Scan stringifies all numbers
  2898. // scanned from numeric database columns into *string, scans into
  2899. // numeric types are checked for overflow. For example, a float64 with
  2900. // value 300 or a string with value "300" can scan into a uint16, but
  2901. // not into a uint8, though float64(255) or "255" can scan into a
  2902. // uint8. One exception is that scans of some float64 numbers to
  2903. // strings may lose information when stringifying. In general, scan
  2904. // floating point columns into *float64.
  2905. //
  2906. // If a dest argument has type *[]byte, Scan saves in that argument a
  2907. // copy of the corresponding data. The copy is owned by the caller and
  2908. // can be modified and held indefinitely. The copy can be avoided by
  2909. // using an argument of type *RawBytes instead; see the documentation
  2910. // for RawBytes for restrictions on its use.
  2911. //
  2912. // If an argument has type *interface{}, Scan copies the value
  2913. // provided by the underlying driver without conversion. When scanning
  2914. // from a source value of type []byte to *interface{}, a copy of the
  2915. // slice is made and the caller owns the result.
  2916. //
  2917. // Source values of type time.Time may be scanned into values of type
  2918. // *time.Time, *interface{}, *string, or *[]byte. When converting to
  2919. // the latter two, time.RFC3339Nano is used.
  2920. //
  2921. // Source values of type bool may be scanned into types *bool,
  2922. // *interface{}, *string, *[]byte, or *RawBytes.
  2923. //
  2924. // For scanning into *bool, the source may be true, false, 1, 0, or
  2925. // string inputs parseable by strconv.ParseBool.
  2926. //
  2927. // Scan can also convert a cursor returned from a query, such as
  2928. // "select cursor(select * from my_table) from dual", into a
  2929. // *Rows value that can itself be scanned from. The parent
  2930. // select query will close any cursor *Rows if the parent *Rows is closed.
  2931. //
  2932. // If any of the first arguments implementing Scanner returns an error,
  2933. // that error will be wrapped in the returned error
  2934. func (rs *Rows) Scan(dest ...any) error {
  2935. rs.closemu.RLock()
  2936. if rs.lasterr != nil && rs.lasterr != io.EOF {
  2937. rs.closemu.RUnlock()
  2938. return rs.lasterr
  2939. }
  2940. if rs.closed {
  2941. err := rs.lasterrOrErrLocked(errRowsClosed)
  2942. rs.closemu.RUnlock()
  2943. return err
  2944. }
  2945. rs.closemu.RUnlock()
  2946. if rs.lastcols == nil {
  2947. return errors.New("sql: Scan called without calling Next")
  2948. }
  2949. if len(dest) != len(rs.lastcols) {
  2950. return fmt.Errorf("sql: expected %d destination arguments in Scan, not %d", len(rs.lastcols), len(dest))
  2951. }
  2952. for i, sv := range rs.lastcols {
  2953. err := convertAssignRows(dest[i], sv, rs)
  2954. if err != nil {
  2955. return fmt.Errorf(`sql: Scan error on column index %d, name %q: %w`, i, rs.rowsi.Columns()[i], err)
  2956. }
  2957. }
  2958. return nil
  2959. }
  2960. // rowsCloseHook returns a function so tests may install the
  2961. // hook through a test only mutex.
  2962. var rowsCloseHook = func() func(*Rows, *error) { return nil }
  2963. // Close closes the Rows, preventing further enumeration. If Next is called
  2964. // and returns false and there are no further result sets,
  2965. // the Rows are closed automatically and it will suffice to check the
  2966. // result of Err. Close is idempotent and does not affect the result of Err.
  2967. func (rs *Rows) Close() error {
  2968. return rs.close(nil)
  2969. }
  2970. func (rs *Rows) close(err error) error {
  2971. rs.closemu.Lock()
  2972. defer rs.closemu.Unlock()
  2973. if rs.closed {
  2974. return nil
  2975. }
  2976. rs.closed = true
  2977. if rs.lasterr == nil {
  2978. rs.lasterr = err
  2979. }
  2980. withLock(rs.dc, func() {
  2981. err = rs.rowsi.Close()
  2982. })
  2983. if fn := rowsCloseHook(); fn != nil {
  2984. fn(rs, &err)
  2985. }
  2986. if rs.cancel != nil {
  2987. rs.cancel()
  2988. }
  2989. if rs.closeStmt != nil {
  2990. rs.closeStmt.Close()
  2991. }
  2992. rs.releaseConn(err)
  2993. return err
  2994. }
  2995. // Row is the result of calling QueryRow to select a single row.
  2996. type Row struct {
  2997. // One of these two will be non-nil:
  2998. err error // deferred error for easy chaining
  2999. rows *Rows
  3000. }
  3001. // Scan copies the columns from the matched row into the values
  3002. // pointed at by dest. See the documentation on Rows.Scan for details.
  3003. // If more than one row matches the query,
  3004. // Scan uses the first row and discards the rest. If no row matches
  3005. // the query, Scan returns ErrNoRows.
  3006. func (r *Row) Scan(dest ...any) error {
  3007. if r.err != nil {
  3008. return r.err
  3009. }
  3010. // TODO(bradfitz): for now we need to defensively clone all
  3011. // []byte that the driver returned (not permitting
  3012. // *RawBytes in Rows.Scan), since we're about to close
  3013. // the Rows in our defer, when we return from this function.
  3014. // the contract with the driver.Next(...) interface is that it
  3015. // can return slices into read-only temporary memory that's
  3016. // only valid until the next Scan/Close. But the TODO is that
  3017. // for a lot of drivers, this copy will be unnecessary. We
  3018. // should provide an optional interface for drivers to
  3019. // implement to say, "don't worry, the []bytes that I return
  3020. // from Next will not be modified again." (for instance, if
  3021. // they were obtained from the network anyway) But for now we
  3022. // don't care.
  3023. defer r.rows.Close()
  3024. for _, dp := range dest {
  3025. if _, ok := dp.(*RawBytes); ok {
  3026. return errors.New("sql: RawBytes isn't allowed on Row.Scan")
  3027. }
  3028. }
  3029. if !r.rows.Next() {
  3030. if err := r.rows.Err(); err != nil {
  3031. return err
  3032. }
  3033. return ErrNoRows
  3034. }
  3035. err := r.rows.Scan(dest...)
  3036. if err != nil {
  3037. return err
  3038. }
  3039. // Make sure the query can be processed to completion with no errors.
  3040. return r.rows.Close()
  3041. }
  3042. // Err provides a way for wrapping packages to check for
  3043. // query errors without calling Scan.
  3044. // Err returns the error, if any, that was encountered while running the query.
  3045. // If this error is not nil, this error will also be returned from Scan.
  3046. func (r *Row) Err() error {
  3047. return r.err
  3048. }
  3049. // A Result summarizes an executed SQL command.
  3050. type Result interface {
  3051. // LastInsertId returns the integer generated by the database
  3052. // in response to a command. Typically this will be from an
  3053. // "auto increment" column when inserting a new row. Not all
  3054. // databases support this feature, and the syntax of such
  3055. // statements varies.
  3056. LastInsertId() (int64, error)
  3057. // RowsAffected returns the number of rows affected by an
  3058. // update, insert, or delete. Not every database or database
  3059. // driver may support this.
  3060. RowsAffected() (int64, error)
  3061. }
  3062. type driverResult struct {
  3063. sync.Locker // the *driverConn
  3064. resi driver.Result
  3065. }
  3066. func (dr driverResult) LastInsertId() (int64, error) {
  3067. dr.Lock()
  3068. defer dr.Unlock()
  3069. return dr.resi.LastInsertId()
  3070. }
  3071. func (dr driverResult) RowsAffected() (int64, error) {
  3072. dr.Lock()
  3073. defer dr.Unlock()
  3074. return dr.resi.RowsAffected()
  3075. }
  3076. func stack() string {
  3077. var buf [2 << 10]byte
  3078. return string(buf[:runtime.Stack(buf[:], false)])
  3079. }
  3080. // withLock runs while holding lk.
  3081. func withLock(lk sync.Locker, fn func()) {
  3082. lk.Lock()
  3083. defer lk.Unlock() // in case fn panics
  3084. fn()
  3085. }