gdb.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gdb provides ORM features for popular relationship databases.
  7. package gdb
  8. import (
  9. "context"
  10. "database/sql"
  11. "fmt"
  12. "github.com/gogf/gf/errors/gerror"
  13. "github.com/gogf/gf/os/gcmd"
  14. "time"
  15. "github.com/gogf/gf/container/gvar"
  16. "github.com/gogf/gf/internal/intlog"
  17. "github.com/gogf/gf/os/glog"
  18. "github.com/gogf/gf/container/gmap"
  19. "github.com/gogf/gf/container/gtype"
  20. "github.com/gogf/gf/os/gcache"
  21. "github.com/gogf/gf/util/grand"
  22. )
  23. // DB defines the interfaces for ORM operations.
  24. type DB interface {
  25. // ===========================================================================
  26. // Model creation.
  27. // ===========================================================================
  28. // The DB interface is designed not only for
  29. // relational databases but also for NoSQL databases in the future. The name
  30. // "Table" is not proper for that purpose any more.
  31. Table(table ...string) *Model
  32. Model(table ...string) *Model
  33. Schema(schema string) *Schema
  34. // Open creates a raw connection object for database with given node configuration.
  35. // Note that it is not recommended using the this function manually.
  36. Open(config *ConfigNode) (*sql.DB, error)
  37. // Ctx is a chaining function, which creates and returns a new DB that is a shallow copy
  38. // of current DB object and with given context in it.
  39. // Note that this returned DB object can be used only once, so do not assign it to
  40. // a global or package variable for long using.
  41. Ctx(ctx context.Context) DB
  42. // ===========================================================================
  43. // Query APIs.
  44. // ===========================================================================
  45. Query(sql string, args ...interface{}) (*sql.Rows, error)
  46. Exec(sql string, args ...interface{}) (sql.Result, error)
  47. Prepare(sql string, execOnMaster ...bool) (*sql.Stmt, error)
  48. // ===========================================================================
  49. // Common APIs for CURD.
  50. // ===========================================================================
  51. Insert(table string, data interface{}, batch ...int) (sql.Result, error)
  52. InsertIgnore(table string, data interface{}, batch ...int) (sql.Result, error)
  53. Replace(table string, data interface{}, batch ...int) (sql.Result, error)
  54. Save(table string, data interface{}, batch ...int) (sql.Result, error)
  55. BatchInsert(table string, list interface{}, batch ...int) (sql.Result, error)
  56. BatchReplace(table string, list interface{}, batch ...int) (sql.Result, error)
  57. BatchSave(table string, list interface{}, batch ...int) (sql.Result, error)
  58. Update(table string, data interface{}, condition interface{}, args ...interface{}) (sql.Result, error)
  59. Delete(table string, condition interface{}, args ...interface{}) (sql.Result, error)
  60. // ===========================================================================
  61. // Internal APIs for CURD, which can be overwrote for custom CURD implements.
  62. // ===========================================================================
  63. DoQuery(link Link, sql string, args ...interface{}) (rows *sql.Rows, err error)
  64. DoGetAll(link Link, sql string, args ...interface{}) (result Result, err error)
  65. DoExec(link Link, sql string, args ...interface{}) (result sql.Result, err error)
  66. DoPrepare(link Link, sql string) (*sql.Stmt, error)
  67. DoInsert(link Link, table string, data interface{}, option int, batch ...int) (result sql.Result, err error)
  68. DoBatchInsert(link Link, table string, list interface{}, option int, batch ...int) (result sql.Result, err error)
  69. DoUpdate(link Link, table string, data interface{}, condition string, args ...interface{}) (result sql.Result, err error)
  70. DoDelete(link Link, table string, condition string, args ...interface{}) (result sql.Result, err error)
  71. // ===========================================================================
  72. // Query APIs for convenience purpose.
  73. // ===========================================================================
  74. GetAll(sql string, args ...interface{}) (Result, error)
  75. GetOne(sql string, args ...interface{}) (Record, error)
  76. GetValue(sql string, args ...interface{}) (Value, error)
  77. GetArray(sql string, args ...interface{}) ([]Value, error)
  78. GetCount(sql string, args ...interface{}) (int, error)
  79. GetStruct(objPointer interface{}, sql string, args ...interface{}) error
  80. GetStructs(objPointerSlice interface{}, sql string, args ...interface{}) error
  81. GetScan(objPointer interface{}, sql string, args ...interface{}) error
  82. // ===========================================================================
  83. // Master/Slave specification support.
  84. // ===========================================================================
  85. Master() (*sql.DB, error)
  86. Slave() (*sql.DB, error)
  87. // ===========================================================================
  88. // Ping-Pong.
  89. // ===========================================================================
  90. PingMaster() error
  91. PingSlave() error
  92. // ===========================================================================
  93. // Transaction.
  94. // ===========================================================================
  95. Begin() (*TX, error)
  96. Transaction(f func(tx *TX) error) (err error)
  97. // ===========================================================================
  98. // Configuration methods.
  99. // ===========================================================================
  100. GetCache() *gcache.Cache
  101. SetDebug(debug bool)
  102. GetDebug() bool
  103. SetSchema(schema string)
  104. GetSchema() string
  105. GetPrefix() string
  106. GetGroup() string
  107. SetDryRun(dryrun bool)
  108. GetDryRun() bool
  109. SetLogger(logger *glog.Logger)
  110. GetLogger() *glog.Logger
  111. GetConfig() *ConfigNode
  112. SetMaxIdleConnCount(n int)
  113. SetMaxOpenConnCount(n int)
  114. SetMaxConnLifetime(d time.Duration)
  115. // ===========================================================================
  116. // Utility methods.
  117. // ===========================================================================
  118. GetCtx() context.Context
  119. GetChars() (charLeft string, charRight string)
  120. GetMaster(schema ...string) (*sql.DB, error)
  121. GetSlave(schema ...string) (*sql.DB, error)
  122. QuoteWord(s string) string
  123. QuoteString(s string) string
  124. QuotePrefixTableName(table string) string
  125. Tables(schema ...string) (tables []string, err error)
  126. TableFields(table string, schema ...string) (map[string]*TableField, error)
  127. HasTable(name string) (bool, error)
  128. // HandleSqlBeforeCommit is a hook function, which deals with the sql string before
  129. // it's committed to underlying driver. The parameter <link> specifies the current
  130. // database connection operation object. You can modify the sql string <sql> and its
  131. // arguments <args> as you wish before they're committed to driver.
  132. HandleSqlBeforeCommit(link Link, sql string, args []interface{}) (string, []interface{})
  133. // ===========================================================================
  134. // Internal methods, for internal usage purpose, you do not need consider it.
  135. // ===========================================================================
  136. mappingAndFilterData(schema, table string, data map[string]interface{}, filter bool) (map[string]interface{}, error)
  137. convertFieldValueToLocalValue(fieldValue interface{}, fieldType string) interface{}
  138. convertRowsToResult(rows *sql.Rows) (Result, error)
  139. }
  140. // Core is the base struct for database management.
  141. type Core struct {
  142. DB DB // DB interface object.
  143. group string // Configuration group name.
  144. debug *gtype.Bool // Enable debug mode for the database, which can be changed in runtime.
  145. cache *gcache.Cache // Cache manager, SQL result cache only.
  146. schema *gtype.String // Custom schema for this object.
  147. logger *glog.Logger // Logger.
  148. config *ConfigNode // Current config node.
  149. ctx context.Context // Context for chaining operation only.
  150. }
  151. // Driver is the interface for integrating sql drivers into package gdb.
  152. type Driver interface {
  153. // New creates and returns a database object for specified database server.
  154. New(core *Core, node *ConfigNode) (DB, error)
  155. }
  156. // Sql is the sql recording struct.
  157. type Sql struct {
  158. Sql string // SQL string(may contain reserved char '?').
  159. Args []interface{} // Arguments for this sql.
  160. Format string // Formatted sql which contains arguments in the sql.
  161. Error error // Execution result.
  162. Start int64 // Start execution timestamp in milliseconds.
  163. End int64 // End execution timestamp in milliseconds.
  164. Group string // Group is the group name of the configuration that the sql is executed from.
  165. }
  166. // TableField is the struct for table field.
  167. type TableField struct {
  168. Index int // For ordering purpose as map is unordered.
  169. Name string // Field name.
  170. Type string // Field type.
  171. Null bool // Field can be null or not.
  172. Key string // The index information(empty if it's not a index).
  173. Default interface{} // Default value for the field.
  174. Extra string // Extra information.
  175. Comment string // Comment.
  176. }
  177. // Link is a common database function wrapper interface.
  178. type Link interface {
  179. Query(sql string, args ...interface{}) (*sql.Rows, error)
  180. Exec(sql string, args ...interface{}) (sql.Result, error)
  181. Prepare(sql string) (*sql.Stmt, error)
  182. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
  183. ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
  184. PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
  185. }
  186. // Counter is the type for update count.
  187. type Counter struct {
  188. Field string
  189. Value float64
  190. }
  191. type (
  192. Raw string // Raw is a raw sql that will not be treated as argument but as a direct sql part.
  193. Value = *gvar.Var // Value is the field value type.
  194. Record map[string]Value // Record is the row record of the table.
  195. Result []Record // Result is the row record array.
  196. Map = map[string]interface{} // Map is alias of map[string]interface{}, which is the most common usage map type.
  197. List = []Map // List is type of map array.
  198. )
  199. const (
  200. insertOptionDefault = 0
  201. insertOptionReplace = 1
  202. insertOptionSave = 2
  203. insertOptionIgnore = 3
  204. defaultBatchNumber = 10 // Per count for batch insert/replace/save.
  205. defaultMaxIdleConnCount = 10 // Max idle connection count in pool.
  206. defaultMaxOpenConnCount = 100 // Max open connection count in pool.
  207. defaultMaxConnLifeTime = 30 * time.Second // Max life time for per connection in pool in seconds.
  208. )
  209. var (
  210. // ErrNoRows is alias of sql.ErrNoRows.
  211. ErrNoRows = sql.ErrNoRows
  212. // instances is the management map for instances.
  213. instances = gmap.NewStrAnyMap(true)
  214. // driverMap manages all custom registered driver.
  215. driverMap = map[string]Driver{
  216. "mysql": &DriverMysql{},
  217. "mssql": &DriverMssql{},
  218. "pgsql": &DriverPgsql{},
  219. "oracle": &DriverOracle{},
  220. "sqlite": &DriverSqlite{},
  221. }
  222. // lastOperatorRegPattern is the regular expression pattern for a string
  223. // which has operator at its tail.
  224. lastOperatorRegPattern = `[<>=]+\s*$`
  225. // regularFieldNameRegPattern is the regular expression pattern for a string
  226. // which is a regular field name of table.
  227. regularFieldNameRegPattern = `^[\w\.\-\_]+$`
  228. // internalCache is the memory cache for internal usage.
  229. internalCache = gcache.New()
  230. // allDryRun sets dry-run feature for all database connections.
  231. // It is commonly used for command options for convenience.
  232. allDryRun = false
  233. )
  234. func init() {
  235. // allDryRun is initialized from environment or command options.
  236. allDryRun = gcmd.GetWithEnv("gf.gdb.dryrun", false).Bool()
  237. }
  238. // Register registers custom database driver to gdb.
  239. func Register(name string, driver Driver) error {
  240. driverMap[name] = driver
  241. return nil
  242. }
  243. // New creates and returns an ORM object with global configurations.
  244. // The parameter <name> specifies the configuration group name,
  245. // which is DefaultGroupName in default.
  246. func New(group ...string) (db DB, err error) {
  247. groupName := configs.group
  248. if len(group) > 0 && group[0] != "" {
  249. groupName = group[0]
  250. }
  251. configs.RLock()
  252. defer configs.RUnlock()
  253. if len(configs.config) < 1 {
  254. return nil, gerror.New("empty database configuration")
  255. }
  256. if _, ok := configs.config[groupName]; ok {
  257. if node, err := getConfigNodeByGroup(groupName, true); err == nil {
  258. c := &Core{
  259. group: groupName,
  260. debug: gtype.NewBool(),
  261. cache: gcache.New(),
  262. schema: gtype.NewString(),
  263. logger: glog.New(),
  264. config: node,
  265. }
  266. if v, ok := driverMap[node.Type]; ok {
  267. c.DB, err = v.New(c, node)
  268. if err != nil {
  269. return nil, err
  270. }
  271. return c.DB, nil
  272. } else {
  273. return nil, gerror.New(fmt.Sprintf(`unsupported database type "%s"`, node.Type))
  274. }
  275. } else {
  276. return nil, err
  277. }
  278. } else {
  279. return nil, gerror.New(fmt.Sprintf(`database configuration node "%s" is not found`, groupName))
  280. }
  281. }
  282. // Instance returns an instance for DB operations.
  283. // The parameter <name> specifies the configuration group name,
  284. // which is DefaultGroupName in default.
  285. func Instance(name ...string) (db DB, err error) {
  286. group := configs.group
  287. if len(name) > 0 && name[0] != "" {
  288. group = name[0]
  289. }
  290. v := instances.GetOrSetFuncLock(group, func() interface{} {
  291. db, err = New(group)
  292. return db
  293. })
  294. if v != nil {
  295. return v.(DB), nil
  296. }
  297. return
  298. }
  299. // getConfigNodeByGroup calculates and returns a configuration node of given group. It
  300. // calculates the value internally using weight algorithm for load balance.
  301. //
  302. // The parameter <master> specifies whether retrieving a master node, or else a slave node
  303. // if master-slave configured.
  304. func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
  305. if list, ok := configs.config[group]; ok {
  306. // Separates master and slave configuration nodes array.
  307. masterList := make(ConfigGroup, 0)
  308. slaveList := make(ConfigGroup, 0)
  309. for i := 0; i < len(list); i++ {
  310. if list[i].Role == "slave" {
  311. slaveList = append(slaveList, list[i])
  312. } else {
  313. masterList = append(masterList, list[i])
  314. }
  315. }
  316. if len(masterList) < 1 {
  317. return nil, gerror.New("at least one master node configuration's need to make sense")
  318. }
  319. if len(slaveList) < 1 {
  320. slaveList = masterList
  321. }
  322. if master {
  323. return getConfigNodeByWeight(masterList), nil
  324. } else {
  325. return getConfigNodeByWeight(slaveList), nil
  326. }
  327. } else {
  328. return nil, gerror.New(fmt.Sprintf("empty database configuration for item name '%s'", group))
  329. }
  330. }
  331. // getConfigNodeByWeight calculates the configuration weights and randomly returns a node.
  332. //
  333. // Calculation algorithm brief:
  334. // 1. If we have 2 nodes, and their weights are both 1, then the weight range is [0, 199];
  335. // 2. Node1 weight range is [0, 99], and node2 weight range is [100, 199], ratio is 1:1;
  336. // 3. If the random number is 99, it then chooses and returns node1;
  337. func getConfigNodeByWeight(cg ConfigGroup) *ConfigNode {
  338. if len(cg) < 2 {
  339. return &cg[0]
  340. }
  341. var total int
  342. for i := 0; i < len(cg); i++ {
  343. total += cg[i].Weight * 100
  344. }
  345. // If total is 0 means all of the nodes have no weight attribute configured.
  346. // It then defaults each node's weight attribute to 1.
  347. if total == 0 {
  348. for i := 0; i < len(cg); i++ {
  349. cg[i].Weight = 1
  350. total += cg[i].Weight * 100
  351. }
  352. }
  353. // Exclude the right border value.
  354. r := grand.N(0, total-1)
  355. min := 0
  356. max := 0
  357. for i := 0; i < len(cg); i++ {
  358. max = min + cg[i].Weight*100
  359. //fmt.Printf("r: %d, min: %d, max: %d\n", r, min, max)
  360. if r >= min && r < max {
  361. return &cg[i]
  362. } else {
  363. min = max
  364. }
  365. }
  366. return nil
  367. }
  368. // getSqlDb retrieves and returns a underlying database connection object.
  369. // The parameter <master> specifies whether retrieves master node connection if
  370. // master-slave nodes are configured.
  371. func (c *Core) getSqlDb(master bool, schema ...string) (sqlDb *sql.DB, err error) {
  372. // Load balance.
  373. node, err := getConfigNodeByGroup(c.group, master)
  374. if err != nil {
  375. return nil, err
  376. }
  377. // Default value checks.
  378. if node.Charset == "" {
  379. node.Charset = "utf8"
  380. }
  381. // Changes the schema.
  382. nodeSchema := c.schema.Val()
  383. if len(schema) > 0 && schema[0] != "" {
  384. nodeSchema = schema[0]
  385. }
  386. if nodeSchema != "" {
  387. // Value copy.
  388. n := *node
  389. n.Name = nodeSchema
  390. node = &n
  391. }
  392. // Cache the underlying connection pool object by node.
  393. v, _ := internalCache.GetOrSetFuncLock(node.String(), func() (interface{}, error) {
  394. sqlDb, err = c.DB.Open(node)
  395. if err != nil {
  396. intlog.Printf("DB open failed: %v, %+v", err, node)
  397. return nil, err
  398. }
  399. if c.config.MaxIdleConnCount > 0 {
  400. sqlDb.SetMaxIdleConns(c.config.MaxIdleConnCount)
  401. } else {
  402. sqlDb.SetMaxIdleConns(defaultMaxIdleConnCount)
  403. }
  404. if c.config.MaxOpenConnCount > 0 {
  405. sqlDb.SetMaxOpenConns(c.config.MaxOpenConnCount)
  406. } else {
  407. sqlDb.SetMaxOpenConns(defaultMaxOpenConnCount)
  408. }
  409. if c.config.MaxConnLifetime > 0 {
  410. // Automatically checks whether MaxConnLifetime is configured using string like: "30s", "60s", etc.
  411. // Or else it is configured just using number, which means value in seconds.
  412. if c.config.MaxConnLifetime > time.Second {
  413. sqlDb.SetConnMaxLifetime(c.config.MaxConnLifetime)
  414. } else {
  415. sqlDb.SetConnMaxLifetime(c.config.MaxConnLifetime * time.Second)
  416. }
  417. } else {
  418. sqlDb.SetConnMaxLifetime(defaultMaxConnLifeTime)
  419. }
  420. return sqlDb, nil
  421. }, 0)
  422. if v != nil && sqlDb == nil {
  423. sqlDb = v.(*sql.DB)
  424. }
  425. if node.Debug {
  426. c.DB.SetDebug(node.Debug)
  427. }
  428. if node.Debug {
  429. c.DB.SetDryRun(node.DryRun)
  430. }
  431. return
  432. }