gdb_schema.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright GoFrame Author(https://goframe.org). 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
  7. // Schema is a schema object from which it can then create a Model.
  8. type Schema struct {
  9. db DB
  10. tx *TX
  11. schema string
  12. }
  13. // Schema creates and returns a schema.
  14. func (c *Core) Schema(schema string) *Schema {
  15. return &Schema{
  16. db: c.db,
  17. schema: schema,
  18. }
  19. }
  20. // Schema creates and returns a initialization model from schema,
  21. // from which it can then create a Model.
  22. func (tx *TX) Schema(schema string) *Schema {
  23. return &Schema{
  24. tx: tx,
  25. schema: schema,
  26. }
  27. }
  28. // Table creates and returns a new ORM model.
  29. // The parameter `tables` can be more than one table names, like :
  30. // "user", "user u", "user, user_detail", "user u, user_detail ud"
  31. func (s *Schema) Table(table string) *Model {
  32. var m *Model
  33. if s.tx != nil {
  34. m = s.tx.Model(table)
  35. } else {
  36. m = s.db.Model(table)
  37. }
  38. // Do not change the schema of the original db,
  39. // it here creates a new db and changes its schema.
  40. db, err := New(m.db.GetGroup())
  41. if err != nil {
  42. panic(err)
  43. }
  44. db.SetSchema(s.schema)
  45. m.db = db
  46. m.schema = s.schema
  47. return m
  48. }
  49. // Model is alias of Core.Table.
  50. // See Core.Table.
  51. func (s *Schema) Model(table string) *Model {
  52. return s.Table(table)
  53. }