Documentation ¶
Overview ¶
Copyright Philippe Thomassigny 2004-2023. Use of this source code is governed by a MIT licence. license that can be found in the LICENSE file.
XDominion for GO v0 =============================
xdominion is a Go library for creating a database layer that abstracts the underlying database implementation and allows developers to interact with the database using objects rather than SQL statements. It supports multiple database backends, including PostgreSQL, MySQL, SQLite, and Microsoft SQL Server, among others.
If you need a not yet supported database, please open a ticket on github.com.
The library provides a set of high-level APIs for interacting with databases. It allows developers to map database tables to Go structs, allowing them to interact with the database using objects. The library also provides an intuitive and chainable API for querying the database, similar to the structure of SQL statements, but without requiring developers to write SQL code directly.
xdominion uses a set of interfaces to abstract the database operations, making it easy to use different database backends with the same code. The library supports transactions, allowing developers to perform multiple database operations in a single transaction.
The xdominion library uses reflection to map Go structs to database tables, and also allows developers to specify custom column names and relationships between tables.
Overall, xdominion provides a simple and intuitive way to interact with databases using objects and abstracts the underlying database implementation. It is a well-designed library with a clear API and support for multiple database backends.
1. Overview ------------------------
XDominion is a database abstraction layer, to build and use objects of data instead of building SQL queries. The code is portable between databases with changing the implementation, since you don't use direct incompatible SQL sentences.
The library is build over 3 main objects: - XBase: database connector and cursors to build queries and manipulation language - - Other included objects: XCursor - XTable: the table definition, data access function/structures and definition manipulation language - - Other included objects: XField*, XConstraints, XContraint, XOrderby, XConditions, XCondition - XRecord: the results and data to interchange with the database - - Other included objects: XRecords
2. Some example code to start working rapidly: ------------------------
Creates the connector to the database and connect:
```
base := &xdominion.XBase{ DBType: xdominion.DB_Postgres, Username: "username", Password: "password", Database: "test", Host: xdominion.DB_Localhost, SSL: false, } base.Logon()
```
Executes a direct query:
```
q, err := base.Exec("drop table test") if (err != nil) { fmt.Println(err) } defer q.Close() // With a select and an XCursor: // 1. Create a cursor cursor := base.NewXCursor() err := cursor.Exec("select * from test") fmt.Println(err, cursor) for cursor.Next() { data, err := cursor.Read() // data is an *XRecord fmt.Println(err, data) } cursor.Close()
```
Creates a table definition:
``` t := xdominion.NewXTable("test", "t_")
t.AddField(xdominion.XFieldInteger{Name: "f1", Constraints: xdominion.XConstraints{ xdominion.XConstraint{Type: xdominion.PK}, xdominion.XConstraint{Type: xdominion.AI}, } }) // ai, pk
t.AddField(xdominion.XFieldVarChar{Name: "f2", Size: 20, Constraints: xdominion.XConstraints{ xdominion.XConstraint{Type: xdominion.NN}, } })
t.AddField(xdominion.XFieldText{Name: "f3"}) t.AddField(xdominion.XFieldDate{Name: "f4"}) t.AddField(xdominion.XFieldDateTime{Name: "f5"}) t.AddField(xdominion.XFieldFloat{Name: "f6"}) t.SetBase(base) ```
Synchronize the table with DB (create it if it does not exist)
```
err = t.Synchronize() if (err != nil) { fmt.Println(err) }
```
Some Insert:
```
res1, err := tb.Insert(xdominion.XRecord{"f1": 1, "f2": "Data line 1",}) if (err != nil) { fmt.Println(err) } fmt.Println(res1) // res1 is the primary key
```
With an error (f2 is mandatory based on table definition):
```
res21, err := tb.Insert(xdominion.XRecord{"f1": 2, "f3": "test",}) if (err != nil) { fmt.Println(err) } fmt.Println(res21)
```
General query (select ALL): ```
res3, err := tb.Select() if err != nil { fmt.Println(err) } else { for _, x := range res3.(xdominion.XRecords) { fmt.Println(x) } }
```
Query by Key:
```
res4, err := tb.Select(1) if err != nil { fmt.Println(err) } else { switch res4.(type) { case xdominion.XRecord: fmt.Println(res4) case xdominion.XRecords: for _, x := range res4.(xdominion.XRecords) { fmt.Println(x) } } }
```
Query by Where:
```
res5, err := tb.Select(xdominion.XConditions{xdominion.NewXCondition("f1", "=", 1), xdominion.NewXCondition("f2", "like", "lin", "and")}) if err != nil { fmt.Println(err) } else { switch res5.(type) { case xdominion.XRecord: fmt.Println(res5) case xdominion.XRecords: for _, x := range res5.(xdominion.XRecords) { fmt.Println(x) } } }
```
Transactions:
``` tx, err := base.BeginTransaction() res1, err := tb.Insert(XRecord{"f1": 5, "f2": "Data line 1"}, tx) res2, err := tb.Update(2, XRecord{"f1": 5, "f2": "Data line 1"}, tx) res3, err := tb.Delete(3, tx) // Note that the transaction is always passed as a parameter to the insert, update, delete operations
if err != nil { tx.Rollback() return err }
tx.Commit() ```
3. Reference ------------------------
XBase -----
The xbase package in xdominion provides a set of functions for working with relational databases in Go. Here is a reference manual for the package:
Constants VERSION: A constant string that represents the version of XDominion. DB_Postgres: A constant string that represents the PostgreSQL database. DB_MySQL: A constant string that represents the MySQL database. DB_Localhost: A constant string that represents the local host. Variables DEBUG: A boolean variable used to enable/disable debug mode. Structs XBase DB: A pointer to an instance of sql.DB, representing the database connection. Logged: A boolean indicating whether the database connection has been established. DBType: A string representing the type of database being used. Username: A string representing the username for the database connection. Password: A string representing the password for the database connection. Database: A string representing the name of the database being connected to. Host: A string representing the host for the database connection. SSL: A boolean indicating whether to use SSL for the database connection. Logger: A pointer to a logger for debugging purposes. XTransaction DB: A pointer to an instance of XBase, representing the database connection. TX: A pointer to an instance of sql.Tx, representing a transaction. Functions Logon() The Logon() function establishes a connection to the database.
go Copy code func (b *XBase) Logon() Logoff() The Logoff() function closes the database connection.
go Copy code func (b *XBase) Logoff() Exec() The Exec() function executes a SQL query on the database and returns a cursor.
go Copy code func (b *XBase) Exec(query string, args ...interface{}) (*sql.Rows, error) Cursor() The Cursor() function returns a new instance of Cursor, which provides methods for working with database records.
go Copy code package main
import (
"fmt" "github.com/webability-go/xdominion"
)
func main() { // Create a new database connection base := &xdominion.XBase{ DBType: xdominion.DB_Postgres, Username: "username", Password: "password", Database: "test", Host: xdominion.DB_Localhost, SSL: false, } // Connect to the database base.Logon() // Execute a query query := "INSERT INTO users (name, email) VALUES ($1, $2)" _, err := base.Exec(query, "John Doe", "john.doe@example.com") if err != nil { fmt.Println("Error executing query:", err) } // Close the database connection base.Logoff() }
In this example, we first create a new instance of the xdominion.XBase struct with the connection details to the database we want to connect to. We then call the Logon() method of the XBase struct to establish a connection to the database.
Next, we define an SQL query to insert a new user into the users table, and then call the Exec() method of the XBase struct with the query and the values we want to insert. The Exec() function returns a cursor, which we don't need in this example, so we ignore it using the blank identifier (_).
If there's an error executing the query, we print an error message to the console. Finally, we close the database connection by calling the Logoff() method of the XBase struct.
Note that this is just a simple example, and you should always make sure to properly handle errors and sanitize user input when working with databases.
package main
import (
"fmt" "github.com/webability-go/xdominion"
)
func main() { // Create a new database connection base := &xdominion.XBase{ DBType: xdominion.DB_Postgres, Username: "username", Password: "password", Database: "test", Host: xdominion.DB_Localhost, SSL: false, } // Connect to the database base.Logon() // Execute a query query := "SELECT name, email FROM users WHERE id=$1" rows, err := base.Exec(query, 1) if err != nil { fmt.Println("Error executing query:", err) } // Iterate over the rows and print the results for rows.Next() { var name, email string err := rows.Scan(&name, &email) if err != nil { fmt.Println("Error reading row:", err) continue } fmt.Println("Name:", name) fmt.Println("Email:", email) } // Close the rows and database connection rows.Close() base.Logoff() }
In this example, we first create a new instance of the xdominion.XBase struct with the connection details to the database we want to connect to. We then call the Logon() method of the XBase struct to establish a connection to the database.
Next, we define an SQL query to select a user from the users table with the id equal to 1. We then call the Exec() method of the XBase struct with the query and the value we want to use for the id parameter. The Exec() function returns a cursor that we can iterate over to get the results of the query.
We use a for loop to iterate over the rows returned by the Exec() function. Inside the loop, we use the Scan() method of the rows object to read the values of the name and email columns into variables. We then print the values of these variables to the console.
If there's an error executing the query or reading a row, we print an error message to the console. Finally, we close the rows object and the database connection by calling the Close() and Logoff() methods of the XBase struct, respectively.
Note that this is just a simple example, and you should always make sure to properly handle errors and sanitize user input when working with databases.
go Copy code func (b *XBase) Cursor() *Cursor BeginTransaction() The BeginTransaction() function starts a new transaction on the database.
go Copy code func (b *XBase) BeginTransaction() (*XTransaction, error) Commit() The Commit() function commits a transaction to the database.
go Copy code func (t *XTransaction) Commit() error Rollback() The Rollback() function rolls back a transaction on the database.
go Copy code func (t *XTransaction) Rollback() error Notes The Logon() function must be called before using any other functions in the xbase package. The Logoff() function should be called when finished using the database connection. The Exec() function should be used for executing arbitrary SQL queries. The Cursor() function should be used for performing CRUD operations on database records. The BeginTransaction(), Commit(), and Rollback() functions should be used for transactions. Note that this is just a brief overview of the xbase package. For more information and examples, please refer to the documentation in the xdominion GitHub repository: https://github.com/webability-go/xdominion.
Create a new instance of the xdominion.XBase struct, which represents a database connection. The XBase struct provides methods for interacting with the database, such as querying, inserting, updating, and deleting records.
base := &xdominion.XBase{ DBType: xdominion.DB_Postgres, Username: "username", Password: "password", Database: "test", Host: xdominion.DB_Localhost, SSL: false, }
In this example, &xdominion.XBase{} is the instance of the XBase struct, and the properties of the struct are set to the database connection details. The DBType property specifies the type of database being used, Username and Password specify the username and password for the database connection, Database specifies the name of the database being connected to, Host specifies the host for the database connection, and SSL specifies whether to use SSL for the database connection.
Use the Logon() method of the XBase struct to connect to the database.
base.Logon()
The Logon() method establishes a connection to the database using the details provided in the XBase struct.
Note that this is just a simple example, and the XBase library provides many more features for working with databases using objects. You can find more information and examples in the xdominion GitHub repository: https://github.com/webability-go/xdominion.
XTable definition -----------------
XTable operations -----------------
XRecord -------
XRecords --------
Conditions ----------
Orderby -------
Fields ------
Limits ------
Groupby -------
Having ------
*/
Index ¶
- Constants
- Variables
- func IsAutoIncrement(f XFieldDef) bool
- func IsFieldConstraint(f XFieldDef, ftype string) bool
- func IsNotNull(f XFieldDef) bool
- func IsPrimaryKey(f XFieldDef) bool
- type XBase
- type XCondition
- type XConditions
- type XConstraint
- type XConstraints
- type XCursor
- type XFieldDate
- func (f XFieldDate) CreateField(prepend string, DB string, ifText *bool) string
- func (f XFieldDate) CreateIndex(table string, id string, DB string) []string
- func (f XFieldDate) CreateSequence(table string) string
- func (f XFieldDate) CreateValue(v interface{}, table string, DB string, id string) string
- func (f XFieldDate) GetConstraints() XConstraints
- func (f XFieldDate) GetName() string
- func (f XFieldDate) GetType() int
- type XFieldDateTime
- func (f XFieldDateTime) CreateField(prepend string, DB string, ifText *bool) string
- func (f XFieldDateTime) CreateIndex(table string, id string, DB string) []string
- func (f XFieldDateTime) CreateSequence(table string) string
- func (f XFieldDateTime) CreateValue(v interface{}, table string, DB string, id string) string
- func (f XFieldDateTime) GetConstraints() XConstraints
- func (f XFieldDateTime) GetName() string
- func (f XFieldDateTime) GetType() int
- type XFieldDef
- type XFieldFloat
- func (f XFieldFloat) CreateField(prepend string, DB string, ifText *bool) string
- func (f XFieldFloat) CreateIndex(table string, id string, DB string) []string
- func (f XFieldFloat) CreateSequence(table string) string
- func (f XFieldFloat) CreateValue(v interface{}, table string, DB string, id string) string
- func (f XFieldFloat) GetConstraints() XConstraints
- func (f XFieldFloat) GetName() string
- func (f XFieldFloat) GetType() int
- type XFieldInteger
- func (f XFieldInteger) CreateField(prepend string, DB string, ifText *bool) string
- func (f XFieldInteger) CreateIndex(table string, id string, DB string) []string
- func (f XFieldInteger) CreateSequence(table string) string
- func (f XFieldInteger) CreateValue(v interface{}, table string, DB string, id string) string
- func (f XFieldInteger) GetConstraints() XConstraints
- func (f XFieldInteger) GetName() string
- func (f XFieldInteger) GetType() int
- func (f XFieldInteger) IsAutoIncrement() bool
- type XFieldSet
- type XFieldText
- func (f XFieldText) CreateField(prepend string, DB string, ifText *bool) string
- func (f XFieldText) CreateIndex(table string, id string, DB string) []string
- func (f XFieldText) CreateSequence(table string) string
- func (f XFieldText) CreateValue(v interface{}, table string, DB string, id string) string
- func (f XFieldText) GetConstraints() XConstraints
- func (f XFieldText) GetName() string
- func (f XFieldText) GetType() int
- type XFieldVarChar
- func (f XFieldVarChar) CreateField(prepend string, DB string, ifText *bool) string
- func (f XFieldVarChar) CreateIndex(table string, id string, DB string) []string
- func (f XFieldVarChar) CreateSequence(table string) string
- func (f XFieldVarChar) CreateValue(v interface{}, table string, DB string, id string) string
- func (f XFieldVarChar) GetConstraints() XConstraints
- func (f XFieldVarChar) GetName() string
- func (f XFieldVarChar) GetType() int
- type XGroup
- type XGroupBy
- type XHaving
- type XOrder
- type XOrderBy
- type XRecord
- func (r *XRecord) Clone() xcore.XDatasetDef
- func (r *XRecord) Del(key string)
- func (r *XRecord) Get(key string) (interface{}, bool)
- func (r *XRecord) GetBool(key string) (bool, bool)
- func (r *XRecord) GetBoolCollection(key string) ([]bool, bool)
- func (r *XRecord) GetCollection(key string) (xcore.XDatasetCollectionDef, bool)
- func (r *XRecord) GetDataset(key string) (xcore.XDatasetDef, bool)
- func (r *XRecord) GetFloat(key string) (float64, bool)
- func (r *XRecord) GetFloatCollection(key string) ([]float64, bool)
- func (r *XRecord) GetInt(key string) (int, bool)
- func (r *XRecord) GetIntCollection(key string) ([]int, bool)
- func (r *XRecord) GetString(key string) (string, bool)
- func (r *XRecord) GetStringCollection(key string) ([]string, bool)
- func (r *XRecord) GetTime(key string) (time.Time, bool)
- func (r *XRecord) GetTimeCollection(key string) ([]time.Time, bool)
- func (r *XRecord) GoString() string
- func (r *XRecord) Set(key string, data interface{})
- func (r *XRecord) String() string
- type XRecordDef
- type XRecords
- func (r *XRecords) Clone() xcore.XDatasetCollectionDef
- func (r *XRecords) Count() int
- func (r *XRecords) Get(index int) (xcore.XDatasetDef, bool)
- func (r *XRecords) GetCollection(key string) (xcore.XDatasetCollectionDef, bool)
- func (r *XRecords) GetData(key string) (interface{}, bool)
- func (d *XRecords) GetDataBool(key string) (bool, bool)
- func (d *XRecords) GetDataFloat(key string) (float64, bool)
- func (d *XRecords) GetDataInt(key string) (int, bool)
- func (r *XRecords) GetDataString(key string) (string, bool)
- func (d *XRecords) GetDataTime(key string) (time.Time, bool)
- func (r *XRecords) GoString() string
- func (r *XRecords) Pop() xcore.XDatasetDef
- func (r *XRecords) Push(data xcore.XDatasetDef)
- func (d *XRecords) Shift() xcore.XDatasetDef
- func (r *XRecords) String() string
- func (d *XRecords) Unshift(data xcore.XDatasetDef)
- type XRecordsDef
- type XTable
- func (t *XTable) AddField(field XFieldDef)
- func (t *XTable) Avg(field string, args ...interface{}) (interface{}, error)
- func (t *XTable) Count(args ...interface{}) (int, error)
- func (t *XTable) Delete(args ...interface{}) (int, error)
- func (t *XTable) GetField(name string) XFieldDef
- func (t *XTable) GetPrimaryKey() XFieldDef
- func (t *XTable) Insert(data interface{}, args ...interface{}) (interface{}, error)
- func (t *XTable) Max(field string, args ...interface{}) (interface{}, error)
- func (t *XTable) Min(field string, args ...interface{}) (interface{}, error)
- func (t *XTable) Select(args ...interface{}) (interface{}, error)
- func (t *XTable) SelectAll(args ...interface{}) (*XRecords, error)
- func (t *XTable) SelectOne(args ...interface{}) (*XRecord, error)
- func (t *XTable) SetBase(base *XBase)
- func (t *XTable) SetLanguage(lang language.Tag)
- func (t *XTable) Synchronize(args ...interface{}) error
- func (t *XTable) Update(args ...interface{}) (int, error)
- func (t *XTable) Upsert(args ...interface{}) (int, error)
- type XTransaction
Examples ¶
Constants ¶
const ( // The distinct supported databases DB_Postgres = "postgres" DB_MySQL = "mysql" DB_Localhost = "localhost" )
const ( OP_Equal = "=" OP_NotEqual = "!=" OP_Inferior = "<=" OP_StrictInferior = "<" OP_Superior = ">=" OP_StrictSuperior = ">" OP_Between = "between" OP_In = "in" OP_NotIn = "not in" OP_Like = "like" OP_NotLike = "not like" OP_iLike = "ilike" OP_NotiLike = "not ilike" )
const ( PK = "pk" // Primary Key Constraint type NN = "nn" // Not Null Constraint type AI = "ai" // Auto Increment Constraint type FK = "fk" // Foreign Key Constraint type IN = "in" // Index Constraint type UI = "ui" // Unique Index Constraint type MI = "mi" // Multiple Index Constraint type MU = "mu" // Multiple Unique Index Constraint type DC = "dc" // Drop cascade Constraint type TR = "tr" // Transfer to another PK before deleting (is not drop cascade) )
const ( XField_Int = 1 XField_VarChar = 2 XField_Float = 3 XField_DateTime = 4 XField_Date = 5 XField_Text = 6 )
const ( ASC = "asc" DESC = "desc" )
const VERSION = "0.5.1"
VERSION is the used version number of the XDominion library.
Variables ¶
var DEBUG bool = false
DEBUG is the flag to activate debugging on the library. if DEBUG is set to TRUE, DEBUG indicates to the XDominion libraries to log a trace of queries and functions called, with most important parameters. DEBUG can be set to true or false dynamically to trace only parts of code on demand.
Functions ¶
func IsAutoIncrement ¶
IsAutoIncrement returns true if the field is an auto-incremented field (with a sequence).
func IsFieldConstraint ¶
IsFieldConstraint returns true if the field checks contains a specific condition.
func IsPrimaryKey ¶
IsPrimaryKey returns true if the field is a primary key for the table.
Types ¶
type XBase ¶
type XBase struct { DB *sql.DB Logged bool DBType string Username string Password string Database string Host string SSL bool Logger *log.Logger }
Example ¶
base := &XBase{ DBType: DB_Postgres, Username: "username", Password: "password", Database: "test", Host: DB_Localhost, SSL: false, } base.Logon()
Output:
func (*XBase) BeginTransaction ¶ added in v0.3.3
func (b *XBase) BeginTransaction() (*XTransaction, error)
func (*XBase) NewXCursor ¶ added in v0.5.0
type XCondition ¶
type XCondition struct { Field string Operator string Limit interface{} LimitExtra interface{} OperatorGlobal string AtomOpen int AtomClose int }
func NewXCondition ¶
func NewXCondition(field string, operator string, limit interface{}, args ...interface{}) XCondition
func (*XCondition) Clone ¶ added in v0.4.2
func (c *XCondition) Clone() XCondition
func (*XCondition) GetCondition ¶
type XConditions ¶
type XConditions []XCondition
func (*XConditions) Clone ¶ added in v0.4.2
func (c *XConditions) Clone() XConditions
func (*XConditions) CreateConditions ¶
func (c *XConditions) CreateConditions(table *XTable, DB string, baseindex int) (string, []interface{})
type XConstraint ¶
XConstraint is a structure representing a database constraint. Type: the type of constraint, one of PK, NN, AI, FK, IN, UI, MI, MU, DC and TR. Data: an array of strings containing the data related to the constraint.
type XConstraints ¶
type XConstraints []XConstraint
XConstraints is a collection of XConstraint structures.
func (*XConstraints) CreateConstraints ¶
func (c *XConstraints) CreateConstraints(prepend string, name string, DB string) string
CreateConstraints function returns a string of constraints for a database field. prepend: a string to be prepended before the field name. name: the name of the field. DB: the database type.
func (*XConstraints) CreateIndex ¶ added in v0.3.3
CreateIndex function returns an array of strings of SQL index creation queries. table: the name of the table. prepend: a string to be prepended before the field name. field: the name of the field. DB: the database type.
func (*XConstraints) Get ¶
func (c *XConstraints) Get(ctype string) *XConstraint
Get function returns a pointer to an XConstraint structure whose type matches the ctype string.
type XCursor ¶ added in v0.5.0
type XCursor struct { Base *XBase Transaction *XTransaction Query string Cursor *sql.Rows Columns []*sql.ColumnType // contains filtered or unexported fields }
type XFieldDate ¶
type XFieldDate struct { Name string Constraints XConstraints }
func (XFieldDate) CreateField ¶
func (f XFieldDate) CreateField(prepend string, DB string, ifText *bool) string
creates the name of the field with its type (to create the table)
func (XFieldDate) CreateIndex ¶
func (f XFieldDate) CreateIndex(table string, id string, DB string) []string
creates the index used by the field (normal, unique, multi, multi unique)
func (XFieldDate) CreateSequence ¶
func (f XFieldDate) CreateSequence(table string) string
creates the sequence used by the field (only autoincrement fields)
func (XFieldDate) CreateValue ¶
func (f XFieldDate) CreateValue(v interface{}, table string, DB string, id string) string
creates a string representation of the value of the field for insert/update and queries where
func (XFieldDate) GetConstraints ¶
func (f XFieldDate) GetConstraints() XConstraints
gets the checks of the field
type XFieldDateTime ¶
type XFieldDateTime struct { Name string Constraints XConstraints }
func (XFieldDateTime) CreateField ¶
func (f XFieldDateTime) CreateField(prepend string, DB string, ifText *bool) string
creates the name of the field with its type (to create the table)
func (XFieldDateTime) CreateIndex ¶
func (f XFieldDateTime) CreateIndex(table string, id string, DB string) []string
creates the index used by the field (normal, unique, multi, multi unique)
func (XFieldDateTime) CreateSequence ¶
func (f XFieldDateTime) CreateSequence(table string) string
creates the sequence used by the field (only autoincrement fields)
func (XFieldDateTime) CreateValue ¶
func (f XFieldDateTime) CreateValue(v interface{}, table string, DB string, id string) string
creates a string representation of the value of the field for insert/update and queries where
func (XFieldDateTime) GetConstraints ¶
func (f XFieldDateTime) GetConstraints() XConstraints
gets the checks of the field
type XFieldDef ¶
type XFieldDef interface { // CreateField creates the name of the field with its type (to create the table). // The `prepend` argument is used to add a prefix to the field name, and // the `DB` argument is used to specify the database type (postgres or mysql). // The `ifText` argument is a boolean pointer that is used to track whether // the field is a text field or not. CreateField(prepend string, DB string, ifText *bool) string // CreateValue creates a string representation of the value of the field for // insert/update with ' for text. The `v` argument is the value of the field, // the `table` argument is the name of the table, the `DB` argument is used to // specify the database type (postgres or mysql), and the `id` argument is used // to specify the ID of the row (for updates). CreateValue(v interface{}, table string, DB string, id string) string // CreateSequence creates the sequence used by the field (only autoincrement fields). // The `table` argument is the name of the table. CreateSequence(table string) string // CreateIndex creates the index used by the field (normal, unique, multi, multi unique). // The `table` argument is the name of the table, the `id` argument is the ID of the row, // and the `DB` argument is used to specify the database type (postgres or mysql). CreateIndex(table string, id string, DB string) []string // GetName gets the name of the field. GetName() string // GetType gets the type of the field. GetType() int // GetConstraints gets the checks of the field. GetConstraints() XConstraints }
XFieldDef is an interface representing a field definition. It provides methods for creating the field, its value, its sequence, and its index. It also provides methods for getting the field's name, type, and constraints.
type XFieldFloat ¶
type XFieldFloat struct { Name string Constraints XConstraints }
func (XFieldFloat) CreateField ¶
func (f XFieldFloat) CreateField(prepend string, DB string, ifText *bool) string
creates the name of the field with its type (to create the table)
func (XFieldFloat) CreateIndex ¶
func (f XFieldFloat) CreateIndex(table string, id string, DB string) []string
creates the index used by the field (normal, unique, multi, multi unique)
func (XFieldFloat) CreateSequence ¶
func (f XFieldFloat) CreateSequence(table string) string
creates the sequence used by the field (only autoincrement fields)
func (XFieldFloat) CreateValue ¶
func (f XFieldFloat) CreateValue(v interface{}, table string, DB string, id string) string
creates a string representation of the value of the field for insert/update
func (XFieldFloat) GetConstraints ¶
func (f XFieldFloat) GetConstraints() XConstraints
gets the checks of the field
type XFieldInteger ¶
type XFieldInteger struct { Name string Constraints XConstraints }
func (XFieldInteger) CreateField ¶
func (f XFieldInteger) CreateField(prepend string, DB string, ifText *bool) string
creates the name of the field with its type (to create the table)
func (XFieldInteger) CreateIndex ¶
func (f XFieldInteger) CreateIndex(table string, id string, DB string) []string
creates the index used by the field (normal, unique, multi, multi unique)
func (XFieldInteger) CreateSequence ¶
func (f XFieldInteger) CreateSequence(table string) string
creates the sequence used by the field (only autoincrement fields)
func (XFieldInteger) CreateValue ¶
func (f XFieldInteger) CreateValue(v interface{}, table string, DB string, id string) string
creates a string representation of the value of the field for insert/update
func (XFieldInteger) GetConstraints ¶
func (f XFieldInteger) GetConstraints() XConstraints
gets the checks of the field
func (XFieldInteger) IsAutoIncrement ¶
func (f XFieldInteger) IsAutoIncrement() bool
Is it autoincrement
type XFieldText ¶
type XFieldText struct { Name string Constraints XConstraints }
func (XFieldText) CreateField ¶
func (f XFieldText) CreateField(prepend string, DB string, ifText *bool) string
creates the name of the field with its type (to create the table)
func (XFieldText) CreateIndex ¶
func (f XFieldText) CreateIndex(table string, id string, DB string) []string
creates the index used by the field (normal, unique, multi, multi unique)
func (XFieldText) CreateSequence ¶
func (f XFieldText) CreateSequence(table string) string
creates the sequence used by the field (only autoincrement fields)
func (XFieldText) CreateValue ¶
func (f XFieldText) CreateValue(v interface{}, table string, DB string, id string) string
creates a string representation of the value of the field for insert/update and queries where
func (XFieldText) GetConstraints ¶
func (f XFieldText) GetConstraints() XConstraints
gets the checks of the field
type XFieldVarChar ¶
type XFieldVarChar struct { Name string Size int Constraints XConstraints }
func (XFieldVarChar) CreateField ¶
func (f XFieldVarChar) CreateField(prepend string, DB string, ifText *bool) string
creates the name of the field with its type (to create the table)
func (XFieldVarChar) CreateIndex ¶
func (f XFieldVarChar) CreateIndex(table string, id string, DB string) []string
creates the index used by the field (normal, unique, multi, multi unique)
func (XFieldVarChar) CreateSequence ¶
func (f XFieldVarChar) CreateSequence(table string) string
creates the sequence used by the field (only autoincrement fields)
func (XFieldVarChar) CreateValue ¶
func (f XFieldVarChar) CreateValue(v interface{}, table string, DB string, id string) string
creates a string representation of the value of the field for insert/update and queries where
func (XFieldVarChar) GetConstraints ¶
func (f XFieldVarChar) GetConstraints() XConstraints
gets the checks of the field
type XHaving ¶
type XHaving []XCondition
type XOrderBy ¶
func NewXOrderBy ¶
type XRecord ¶
type XRecord map[string]interface{}
func NewXRecord ¶
func NewXRecord() *XRecord
func (*XRecord) GetCollection ¶
func (*XRecord) GetDataset ¶
func (*XRecord) GetFloatCollection ¶
func (*XRecord) GetStringCollection ¶
func (*XRecord) GetTimeCollection ¶
type XRecordDef ¶
type XRecordDef interface { xcore.XDatasetDef }
type XRecordsDef ¶
type XRecordsDef interface { xcore.XDatasetCollectionDef }
type XTable ¶
type XTable struct { Base *XBase Name string Prepend string // content of table language, informative Language language.Tag Fields []XFieldDef InsertedKey interface{} }
func (*XTable) GetPrimaryKey ¶
func (*XTable) Insert ¶
Insert things into database: If data is XRecord, insert the record. Returns the key (same type as field type) interface{} If data is XRecords, insert the collection of XRecord. Returns an array of keys (same type as field type) []interface{} If data is SubQuery, intert the result of the sub query, return ?
func (*XTable) SetLanguage ¶
func (*XTable) Synchronize ¶
type XTransaction ¶ added in v0.3.3
func (*XTransaction) Commit ¶ added in v0.3.3
func (t *XTransaction) Commit() error
func (*XTransaction) Exec ¶ added in v0.3.3
func (t *XTransaction) Exec(query string, args ...interface{}) (*sql.Rows, error)
func (*XTransaction) NewXCursor ¶ added in v0.5.0
func (t *XTransaction) NewXCursor() *XCursor
func (*XTransaction) Rollback ¶ added in v0.3.3
func (t *XTransaction) Rollback() error