http urls monitor.

mysql.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 // import "upper.io/db.v3/mysql"
  22. import (
  23. "database/sql"
  24. "upper.io/db.v3"
  25. "upper.io/db.v3/internal/sqladapter"
  26. "upper.io/db.v3/lib/sqlbuilder"
  27. )
  28. const sqlDriver = `mysql`
  29. // Adapter is the public name of the adapter.
  30. const Adapter = sqlDriver
  31. func init() {
  32. sqlbuilder.RegisterAdapter(Adapter, &sqlbuilder.AdapterFuncMap{
  33. New: New,
  34. NewTx: NewTx,
  35. Open: Open,
  36. })
  37. }
  38. // Open stablishes a new connection with the SQL server.
  39. func Open(settings db.ConnectionURL) (sqlbuilder.Database, error) {
  40. d := newDatabase(settings)
  41. if err := d.Open(settings); err != nil {
  42. return nil, err
  43. }
  44. return d, nil
  45. }
  46. // NewTx wraps a regular *sql.Tx transaction and returns a new upper-db
  47. // transaction backed by it.
  48. func NewTx(sqlTx *sql.Tx) (sqlbuilder.Tx, error) {
  49. d := newDatabase(nil)
  50. // Binding with sqladapter's logic.
  51. d.BaseDatabase = sqladapter.NewBaseDatabase(d)
  52. // Binding with sqlbuilder.
  53. d.SQLBuilder = sqlbuilder.WithSession(d.BaseDatabase, template)
  54. if err := d.BaseDatabase.BindTx(d.Context(), sqlTx); err != nil {
  55. return nil, err
  56. }
  57. newTx := sqladapter.NewDatabaseTx(d)
  58. return &tx{DatabaseTx: newTx}, nil
  59. }
  60. // New wraps the given *sql.DB session and creates a new db session.
  61. func New(sess *sql.DB) (sqlbuilder.Database, error) {
  62. d := newDatabase(nil)
  63. // Binding with sqladapter's logic.
  64. d.BaseDatabase = sqladapter.NewBaseDatabase(d)
  65. // Binding with sqlbuilder.
  66. d.SQLBuilder = sqlbuilder.WithSession(d.BaseDatabase, template)
  67. if err := d.BaseDatabase.BindSession(sess); err != nil {
  68. return nil, err
  69. }
  70. return d, nil
  71. }