Documentation
¶
Index ¶
- Constants
- func BindDataSource(db *sql.DB, dbtype DBType, tableNames ...string)
- func BindDataSourceWithClass[T gdaoStruct.TableClass](db *sql.DB, dbtype DBType)
- func BindMapperDataSource(namespace string, db *sql.DB, dbtype DBType)
- func BindMapperIdDataSource(namespace, id string, db *sql.DB, dbtype DBType)
- func ExecuteBatch(sql string, args [][]any) ([]sql.Result, error)
- func ExecuteQuery[T any](sql string, args ...any) (r *T, err error)
- func ExecuteQueryBean(sql string, args ...any) *base.DataBean
- func ExecuteQueryBeans(sql string, args ...any) *base.DataBeans
- func ExecuteQueryList[T any](sql string, args ...any) (r []*T, err error)
- func ExecuteUpdate(sql string, args ...any) (sql.Result, error)
- func GetDefaultDBHandle() DBhandle
- func Init(db *sql.DB, dbtype DBType)
- func NewDBHandle(db *sql.DB, dbtype DBType) DBhandle
- func NewTransaction() (r Transaction, err error)
- func NewTransactionWithDBhandle(db DBhandle) (r Transaction, err error)
- func PreCompile(limit uint32)
- func SetLogger(on bool)
- func UnbindDataSource(tableNames ...string)
- func UnbindDataSourceWithClass[T gdaoStruct.TableClass]()
- func UnbindMapperDataSource(namespace string)
- func UnbindMapperIdDataSource(namespace, id string)
- type GStruct
- type Table
- func (t *Table[T]) AddBatch()
- func (*Table[T]) Decode(data []byte) (map[string]any, error)
- func (t *Table[T]) Delete() (sql.Result, error)
- func (*Table[T]) Encode(m map[string]any) ([]byte, error)
- func (t *Table[T]) ExecBatch() ([]sql.Result, error)
- func (t *Table[T]) GroupBy(columns ...Column[T]) *Table[T]
- func (t *Table[T]) Having(havings ...*Having[T]) *Table[T]
- func (t *Table[T]) Init(s string, columns []Column[T])
- func (t *Table[T]) Insert() (sql.Result, error)
- func (t *Table[T]) IsInit() bool
- func (t *Table[T]) Limit(limit int64)
- func (t *Table[T]) Limit2(offset, limit int64)
- func (t *Table[T]) MustMaster(must bool)
- func (t *Table[T]) OrderBy(sorts ...*Sort[T]) *Table[T]
- func (t *Table[T]) Put0(k string, v any)
- func (t *Table[T]) Select(columns ...Column[T]) (_r *T, err error)
- func (t *Table[T]) Selects(columns ...Column[T]) (_r []*T, err error)
- func (t *Table[T]) TableName() string
- func (t *Table[T]) Update() (sql.Result, error)
- func (t *Table[T]) UseCache(use bool)
- func (t *Table[T]) UseCommentLine(commentline string)
- func (t *Table[T]) UseDBHandle(db DBhandle) *Table[T]
- func (t *Table[T]) UseTransaction(transaction Transaction)
- func (t *Table[T]) Where(wheres ...*Where[T]) *Table[T]
Constants ¶
const ( MYSQL base.DBType POSTGRESQL MARIADB SQLITE ORACLE SQLSERVER DB2 SYBASE DERBY FIREBIRD INGRES GREENPLUM TERADATA NETEZZA VERTICA TIDB OCEANBASE OPENGAUSS HSQLDB ENTERPRISEDB SAPHANA COCKROACHDB INFORMIX )
Variables ¶
This section is empty.
Functions ¶
func BindDataSource ¶
BindDataSource binds an open database connection to a specified data source type and list of table names.
Parameters:
db (*sql.DB): An open database connection. dbtype (DBType): The data source type, such as gdao.MYSQL, gdao.POSTGRESQL, etc. tableNames (...string): A list of table names to be bound to the data source.
Description:
This method initializes and configures a data source to be used for subsequent data operations. It takes an open database connection, a data source type, and one or more table names that need to be bound. This allows the data source to be used for CRUD operations within the application.
Example:
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
if err != nil {
log.Fatal(err)
}
defer db.Close()
BindDataSource(db, gdao.MYSQL, "users", "orders")
func BindDataSourceWithClass ¶
func BindDataSourceWithClass[T gdaoStruct.TableClass](db *sql.DB, dbtype DBType)
BindDataSourceWithClass binds the provided database connection and data source type to a generic table base class.
Parameters:
db (*sql.DB): An open database connection. dbtype (DBType): The data source type, such as gdao.MYSQL, gdao.POSTGRESQL, etc.
Description:
This function initializes the generic table base class to work with the specified database connection and data source type.
Type Parameters:
T: The type of the table base class that is the standardized entity class generated by gdao
Example:
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// assuming dao.Hstest is the standardized entity class generated by gdao
BindDataSourceWithClass[dao.Hstest](db, gdao.MYSQL)
func BindMapperDataSource ¶
BindMapperDataSource binds the provided database connection and data source type to a specific namespace in the XML mapping files.
Parameters:
namespace (string): The namespace in the XML mapping files that corresponds to the CRUD to bind. db (*sql.DB): An open database connection. dbtype (DBType): The data source type, such as gdao.MYSQL, gdao.POSTGRESQL, etc.
Description:
If there is only one data source, the call to the gdao.Init function has already set the data source for all operations. If there are multiple data sources and the xml needs to be set to specify the data source, you can call this function for binding
Example:
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// assuming users is the namespace of XML mapping
BindMapperDataSource("users", db, gdao.MYSQL)
func BindMapperIdDataSource ¶
BindMapperIdDataSource is similar to the BindMapperDataSource function, but it needs to specify the id of a CURD property
// assuming users is the namespace of XML mapping and selectHstest is the id of select tag BindMapperIdDataSource("users","selectHstest", db, gdao.MYSQL)
func ExecuteBatch ¶
ExecuteBatch executes a batch of SQL statements. sql is the SQL statement to execute for each batch item. args is a slice of slices, where each inner slice contains the arguments for a single SQL statement. The function returns a slice of int64 values representing the number of rows affected by each SQL statement and any error encountered. If there's an error, it returns nil and the specific error information; otherwise, it returns the slice of affected rows and nil.
func ExecuteQuery ¶
ExecuteQuery executes an SQL query and returns the parsed results. T is a generic type that represents the specific struct or type the query result should be converted to. sql is the SQL query statement to execute. args is an optional list of parameters to substitute placeholders in the SQL query. The function returns a pointer to a value of type *T, which is typically a pointer to a struct that holds the query results. If there's an error, it returns nil and the specific error information; otherwise, it returns a filled result object and nil.
func ExecuteQueryBean ¶
ExecuteQueryBean executes an SQL query and returns a single DataBean object. sql is the SQL query statement to execute. args is an optional list of parameters to substitute placeholders in the SQL query. The function returns a pointer to a DataBean object, which typically holds the data retrieved from a single row in the query results. If there's an error, it returns nil and the specific error information; otherwise, it returns a filled DataBean object and nil.
func ExecuteQueryBeans ¶
ExecuteQueryBeans executes an SQL query and returns a list of DataBean objects. sql is the SQL query statement to execute. args is an optional list of parameters to substitute placeholders in the SQL query. The function returns a slice of pointers to DataBean objects, where each element represents one row of the query results. If there's an error, it returns nil and the specific error information; otherwise, it returns a slice of filled DataBean objects and nil.
func ExecuteQueryList ¶
ExecuteQueryList executes an SQL query and returns a list of parsed results. T is a generic type that represents the specific struct or type the query results should be converted to. sql is the SQL query statement to execute. args is an optional list of parameters to substitute placeholders in the SQL query. The function returns a slice of pointers to values of type T, where each element represents one row of the query results. If there's an error, it returns nil and the specific error information; otherwise, it returns a slice of filled result objects and nil.
func ExecuteUpdate ¶
ExecuteUpdate executes an SQL update, insert, or delete statement. sql is the SQL statement to execute. args is an optional list of parameters to substitute placeholders in the SQL statement. The function returns the number of rows affected by the SQL statement and any error encountered. If there's an error, it returns -1 and the specific error information; otherwise, it returns the number of affected rows and nil.
func GetDefaultDBHandle ¶
func GetDefaultDBHandle() DBhandle
func Init ¶
Init initializes the gdao package by binding the provided database connection and data source type.
Parameters:
db (*sql.DB): An open database connection. dbtype (DBType): The data source type, such as gdao.MYSQL, gdao.POSTGRESQL, etc.
Description:
This function sets up gdao to work with the specified database connection and data source type. It is typically called at the beginning of the application to configure the gdao package for use with a specific database.
Example:
db, err := sql.Open("mysql", "user:password@tcp(127.0.0.1:3306)/dbname")
if err != nil {
log.Fatal(err)
}
defer db.Close()
gdao.Init(db, gdao.MYSQL)
func NewDBHandle ¶
func NewTransaction ¶
func NewTransaction() (r Transaction, err error)
func NewTransactionWithDBhandle ¶
func NewTransactionWithDBhandle(db DBhandle) (r Transaction, err error)
func PreCompile ¶ added in v1.1.2
func PreCompile(limit uint32)
func UnbindDataSource ¶
func UnbindDataSource(tableNames ...string)
func UnbindDataSourceWithClass ¶
func UnbindDataSourceWithClass[T gdaoStruct.TableClass]()
func UnbindMapperDataSource ¶
func UnbindMapperDataSource(namespace string)
func UnbindMapperIdDataSource ¶
func UnbindMapperIdDataSource(namespace, id string)
Types ¶
type GStruct ¶
type GStruct[P any, T any] interface { Scanner // UseCache use default gdaoCache or not use cache UseCache(use bool) // UseTransaction use specified transaction UseTransaction(transaction Transaction) // UseDBHandle use specified DBhandle UseDBHandle(db DBhandle) *Table[T] // UseCommentLine set annotations for sql UseCommentLine(commentline string) MustMaster(must bool) // Where adds a WHERE clause to the query with one or more conditions. // // Parameters: // // wheres: Variable length argument list of *Where[T] objects representing the conditions to add to the WHERE clause. // // Returns: // // A pointer to the Table[T] instance to allow method chaining. // // Description: // // This function allows you to specify one or more conditions that will be added to the WHERE clause of the SQL query. // Each *Where[T] object represents a condition that must be satisfied by the rows returned by the query. // Multiple conditions can be combined to form complex queries. // // Example: // // // Assuming "hs" is an instance of a Table struct that represents a table named "hstest" // // And "Rowname" and "Id" are columns in the "hstest" table // hs := dao.NewHstest() // hs = hs.Where(hs.Rowname.RLIKE(1)).GroupBy(hs.Id).Having(hs.Id.Count().LT(2)).Limit(2) // hslist, _ := hs.Selects() Where(wheres ...*Where[T]) *Table[T] // OrderBy sql: order by OrderBy(sorts ...*Sort[T]) *Table[T] // GroupBy sql: group by GroupBy(columns ...Column[T]) *Table[T] // Having sql: having Having(havings ...*Having[T]) *Table[T] Limit2(offset, limit int64) Limit(limit int64) // Selects sql:select from table and Return data slice Selects(columns ...Column[T]) (_r []P, err error) // Select sql:select from table and Return first data Select(columns ...Column[T]) (_r P, err error) // Update sql: update Update() (sql.Result, error) // Insert sql: insert Insert() (sql.Result, error) // Delete sql: delete Delete() (sql.Result, error) // AddBatch sql: add data to batch sql AddBatch() // ExecBatch sql:database batch operation ExecBatch() ([]sql.Result, error) //Copy object data Copy(h P) P // Encode Serialized object Encode() ([]byte, error) // Decode deserialization Decode(bs []byte) (err error) String() string // TableName return table name TableName() string }
type Table ¶
type Table[T any] struct { //GStruct[*T, T] gdaoStruct.TableClass // contains filtered or unexported fields }
func (*Table[T]) MustMaster ¶
func (*Table[T]) UseCommentLine ¶
func (*Table[T]) UseDBHandle ¶
func (*Table[T]) UseTransaction ¶
func (t *Table[T]) UseTransaction(transaction Transaction)
func (*Table[T]) Where ¶
Where adds a WHERE clause to the query with one or more conditions.
Parameters:
wheres: Variable length argument list of *Where[T] objects representing the conditions to add to the WHERE clause.
Returns:
A pointer to the Table[T] instance to allow method chaining.
Description:
This function allows you to specify one or more conditions that will be added to the WHERE clause of the SQL query. Each *Where[T] object represents a condition that must be satisfied by the rows returned by the query. Multiple conditions can be combined to form complex queries.
Example:
// Assuming "hs" is an instance of a Table struct that represents a table named "hstest" // And "Rowname" and "Id" are columns in the "hstest" table hs := dao.NewHstest() hs = hs.Where(hs.Rowname.RLIKE(1)).GroupBy(hs.Id).Having(hs.Id.Count().LT(2)).Limit(2) hslist, _ := hs.Selects()