github.com/gogf/gf@v1.16.9/database/gdb/gdb_schema.go (about)

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