http urls monitor.

collection.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  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. // table is the actual implementation of a collection.
  29. type table struct {
  30. sqladapter.BaseCollection // Leveraged by sqladapter
  31. d *database
  32. name string
  33. }
  34. var (
  35. _ = sqladapter.Collection(&table{})
  36. _ = db.Collection(&table{})
  37. )
  38. // newTable binds *table with sqladapter.
  39. func newTable(d *database, name string) *table {
  40. t := &table{
  41. name: name,
  42. d: d,
  43. }
  44. t.BaseCollection = sqladapter.NewBaseCollection(t)
  45. return t
  46. }
  47. func (t *table) Name() string {
  48. return t.name
  49. }
  50. func (t *table) Database() sqladapter.Database {
  51. return t.d
  52. }
  53. // Insert inserts an item (map or struct) into the collection.
  54. func (t *table) Insert(item interface{}) (interface{}, error) {
  55. columnNames, columnValues, err := sqlbuilder.Map(item, nil)
  56. if err != nil {
  57. return nil, err
  58. }
  59. pKey := t.BaseCollection.PrimaryKeys()
  60. q := t.d.InsertInto(t.Name()).
  61. Columns(columnNames...).
  62. Values(columnValues...)
  63. var res sql.Result
  64. if res, err = q.Exec(); err != nil {
  65. return nil, err
  66. }
  67. lastID, err := res.LastInsertId()
  68. if err == nil && len(pKey) <= 1 {
  69. return lastID, nil
  70. }
  71. keyMap := db.Cond{}
  72. for i := range columnNames {
  73. for j := 0; j < len(pKey); j++ {
  74. if pKey[j] == columnNames[i] {
  75. keyMap[pKey[j]] = columnValues[i]
  76. }
  77. }
  78. }
  79. // There was an auto column among primary keys, let's search for it.
  80. if lastID > 0 {
  81. for j := 0; j < len(pKey); j++ {
  82. if keyMap[pKey[j]] == nil {
  83. keyMap[pKey[j]] = lastID
  84. }
  85. }
  86. }
  87. return keyMap, nil
  88. }