http urls monitor.

database.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. // Package mysql wraps the github.com/go-sql-driver/mysql MySQL driver. See
  22. // https://upper.io/db.v3/mysql for documentation, particularities and usage
  23. // examples.
  24. package mysql
  25. import (
  26. "context"
  27. "database/sql/driver"
  28. "reflect"
  29. "strings"
  30. "sync"
  31. "time"
  32. "database/sql"
  33. _ "github.com/go-sql-driver/mysql" // MySQL driver.
  34. "upper.io/db.v3"
  35. "upper.io/db.v3/internal/sqladapter"
  36. "upper.io/db.v3/internal/sqladapter/compat"
  37. "upper.io/db.v3/internal/sqladapter/exql"
  38. "upper.io/db.v3/lib/sqlbuilder"
  39. )
  40. // database is the actual implementation of Database
  41. type database struct {
  42. sqladapter.BaseDatabase
  43. sqlbuilder.SQLBuilder
  44. connURL db.ConnectionURL
  45. mu sync.Mutex
  46. }
  47. var (
  48. _ = sqlbuilder.Database(&database{})
  49. _ = sqlbuilder.Database(&database{})
  50. )
  51. // newDatabase creates a new *database session for internal use.
  52. func newDatabase(settings db.ConnectionURL) *database {
  53. return &database{
  54. connURL: settings,
  55. }
  56. }
  57. // ConnectionURL returns this database session's connection URL, if any.
  58. func (d *database) ConnectionURL() db.ConnectionURL {
  59. return d.connURL
  60. }
  61. // Open attempts to open a connection with the database server.
  62. func (d *database) Open(connURL db.ConnectionURL) error {
  63. if connURL == nil {
  64. return db.ErrMissingConnURL
  65. }
  66. d.connURL = connURL
  67. return d.open()
  68. }
  69. // NewTx begins a transaction block with the given context.
  70. func (d *database) NewTx(ctx context.Context) (sqlbuilder.Tx, error) {
  71. if ctx == nil {
  72. ctx = d.Context()
  73. }
  74. nTx, err := d.NewDatabaseTx(ctx)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &tx{DatabaseTx: nTx}, nil
  79. }
  80. // Collections returns a list of non-system tables from the database.
  81. func (d *database) Collections() (collections []string, err error) {
  82. q := d.Select("table_name").
  83. From("information_schema.tables").
  84. Where("table_schema = ?", d.BaseDatabase.Name())
  85. iter := q.Iterator()
  86. defer iter.Close()
  87. for iter.Next() {
  88. var tableName string
  89. if err := iter.Scan(&tableName); err != nil {
  90. return nil, err
  91. }
  92. collections = append(collections, tableName)
  93. }
  94. return collections, nil
  95. }
  96. // open attempts to establish a connection with the MySQL server.
  97. func (d *database) open() error {
  98. // Binding with sqladapter's logic.
  99. d.BaseDatabase = sqladapter.NewBaseDatabase(d)
  100. // Binding with sqlbuilder.
  101. d.SQLBuilder = sqlbuilder.WithSession(d.BaseDatabase, template)
  102. connFn := func() error {
  103. sess, err := sql.Open("mysql", d.ConnectionURL().String())
  104. if err == nil {
  105. sess.SetConnMaxLifetime(db.DefaultSettings.ConnMaxLifetime())
  106. sess.SetMaxIdleConns(db.DefaultSettings.MaxIdleConns())
  107. sess.SetMaxOpenConns(db.DefaultSettings.MaxOpenConns())
  108. return d.BaseDatabase.BindSession(sess)
  109. }
  110. return err
  111. }
  112. if err := d.BaseDatabase.WaitForConnection(connFn); err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. // Clone creates a copy of the database session on the given context.
  118. func (d *database) clone(ctx context.Context, checkConn bool) (*database, error) {
  119. clone := newDatabase(d.connURL)
  120. var err error
  121. clone.BaseDatabase, err = d.NewClone(clone, checkConn)
  122. if err != nil {
  123. return nil, err
  124. }
  125. clone.SetContext(ctx)
  126. clone.SQLBuilder = sqlbuilder.WithSession(clone.BaseDatabase, template)
  127. return clone, nil
  128. }
  129. func (d *database) ConvertValues(values []interface{}) []interface{} {
  130. for i := range values {
  131. switch v := values[i].(type) {
  132. case *string, *bool, *int, *uint, *int64, *uint64, *int32, *uint32, *int16, *uint16, *int8, *uint8, *float32, *float64, *[]uint8, sql.Scanner, *sql.Scanner, *time.Time:
  133. case string, bool, int, uint, int64, uint64, int32, uint32, int16, uint16, int8, uint8, float32, float64, []uint8, driver.Valuer, *driver.Valuer, time.Time:
  134. case JSONMap, JSON:
  135. // Already with scanner/valuer.
  136. case *JSONMap, *JSON:
  137. // Already with scanner/valuer.
  138. case *map[string]interface{}:
  139. values[i] = (*JSONMap)(v)
  140. case map[string]interface{}:
  141. values[i] = (*JSONMap)(&v)
  142. case sqlbuilder.ValueWrapper:
  143. values[i] = v.WrapValue(v)
  144. default:
  145. values[i] = autoWrap(reflect.ValueOf(values[i]), values[i])
  146. }
  147. }
  148. return values
  149. }
  150. // CompileStatement compiles a *exql.Statement into arguments that sql/database
  151. // accepts.
  152. func (d *database) CompileStatement(stmt *exql.Statement, args []interface{}) (string, []interface{}) {
  153. compiled, err := stmt.Compile(template)
  154. if err != nil {
  155. panic(err.Error())
  156. }
  157. return sqlbuilder.Preprocess(compiled, args)
  158. }
  159. // Err allows sqladapter to translate specific MySQL string errors into custom
  160. // error values.
  161. func (d *database) Err(err error) error {
  162. if err != nil {
  163. // This error is not exported so we have to check it by its string value.
  164. s := err.Error()
  165. if strings.Contains(s, `many connections`) {
  166. return db.ErrTooManyClients
  167. }
  168. }
  169. return err
  170. }
  171. // NewCollection creates a db.Collection by name.
  172. func (d *database) NewCollection(name string) db.Collection {
  173. return newTable(d, name)
  174. }
  175. // Tx creates a transaction block on the given context and passes it to the
  176. // function fn. If fn returns no error the transaction is commited, else the
  177. // transaction is rolled back. After being commited or rolled back the
  178. // transaction is closed automatically.
  179. func (d *database) Tx(ctx context.Context, fn func(tx sqlbuilder.Tx) error) error {
  180. return sqladapter.RunTx(d, ctx, fn)
  181. }
  182. // NewDatabaseTx begins a transaction block.
  183. func (d *database) NewDatabaseTx(ctx context.Context) (sqladapter.DatabaseTx, error) {
  184. clone, err := d.clone(ctx, true)
  185. if err != nil {
  186. return nil, err
  187. }
  188. clone.mu.Lock()
  189. defer clone.mu.Unlock()
  190. connFn := func() error {
  191. sqlTx, err := compat.BeginTx(clone.BaseDatabase.Session(), ctx, clone.TxOptions())
  192. if err == nil {
  193. return clone.BindTx(ctx, sqlTx)
  194. }
  195. return err
  196. }
  197. if err := d.BaseDatabase.WaitForConnection(connFn); err != nil {
  198. return nil, err
  199. }
  200. return sqladapter.NewDatabaseTx(clone), nil
  201. }
  202. // LookupName looks for the name of the database and it's often used as a
  203. // test to determine if the connection settings are valid.
  204. func (d *database) LookupName() (string, error) {
  205. q := d.Select(db.Raw("DATABASE() AS name"))
  206. iter := q.Iterator()
  207. defer iter.Close()
  208. if iter.Next() {
  209. var name string
  210. err := iter.Scan(&name)
  211. return name, err
  212. }
  213. return "", iter.Err()
  214. }
  215. // TableExists returns an error if the given table name does not exist on the
  216. // database.
  217. func (d *database) TableExists(name string) error {
  218. q := d.Select("table_name").
  219. From("information_schema.tables").
  220. Where("table_schema = ? AND table_name = ?", d.BaseDatabase.Name(), name)
  221. iter := q.Iterator()
  222. defer iter.Close()
  223. if iter.Next() {
  224. var name string
  225. if err := iter.Scan(&name); err != nil {
  226. return err
  227. }
  228. return nil
  229. }
  230. return db.ErrCollectionDoesNotExist
  231. }
  232. // PrimaryKeys returns the names of all the primary keys on the table.
  233. func (d *database) PrimaryKeys(tableName string) ([]string, error) {
  234. q := d.Select("k.column_name").
  235. From("information_schema.key_column_usage AS k").
  236. Where(`
  237. k.constraint_name = 'PRIMARY'
  238. AND k.table_schema = ?
  239. AND k.table_name = ?
  240. `, d.BaseDatabase.Name(), tableName).
  241. OrderBy("k.ordinal_position")
  242. iter := q.Iterator()
  243. defer iter.Close()
  244. pk := []string{}
  245. for iter.Next() {
  246. var k string
  247. if err := iter.Scan(&k); err != nil {
  248. return nil, err
  249. }
  250. pk = append(pk, k)
  251. }
  252. return pk, nil
  253. }
  254. // WithContext creates a copy of the session on the given context.
  255. func (d *database) WithContext(ctx context.Context) sqlbuilder.Database {
  256. newDB, _ := d.clone(ctx, false)
  257. return newDB
  258. }