xdominion

package module
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 13 Imported by: 34

README

XDominion for GO v0

Go Report Card GoDoc

The XDominion library is used to build object instead of queries to access any database. Queries are build on demand based on the type of database. Do not write queries anymore, but use objects.

Because you need to compile, add the database drivers directly into the code, XDominion support only posgresql and MySQL in this version. More databases will be supported later. 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.

XDominion needs Go v1.17+

Version Changes Control

v0.5.1 - 2024-04-09

  • Correction of a bug into manuals

v0.5.0 - 2023-05-02

  • XCursor is now implemented and makes a simple query to the database but return results as an *XRecord.
  • XCursor is complatible with XBase and XTransactions
  • Official documentation mounted for https://pkg.go.dev/

v0.4.3 - 2022-11-22

  • Added GetInt for a string with automatic conversion if possible from an XRecord

v0.4.2 - 2022-06-13

  • Correction of a bug on DoSelect with limits, a 0 limit is not a limit

v0.4.1 - 2022-01-19

  • XConditions, XCondition, XOrder, XOrderBy, XFieldSet can now be cloned with

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

Examples

Constants

View Source
const (
	// The distinct supported databases
	DB_Postgres  = "postgres"
	DB_MySQL     = "mysql"
	DB_Localhost = "localhost"
)
View Source
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"
)
View Source
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)
)
View Source
const (
	XField_Int      = 1
	XField_VarChar  = 2
	XField_Float    = 3
	XField_DateTime = 4
	XField_Date     = 5
	XField_Text     = 6
)
View Source
const (
	ASC  = "asc"
	DESC = "desc"
)
View Source
const VERSION = "0.5.1"

VERSION is the used version number of the XDominion library.

Variables

View Source
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

func IsAutoIncrement(f XFieldDef) bool

IsAutoIncrement returns true if the field is an auto-incremented field (with a sequence).

func IsFieldConstraint

func IsFieldConstraint(f XFieldDef, ftype string) bool

IsFieldConstraint returns true if the field checks contains a specific condition.

func IsNotNull

func IsNotNull(f XFieldDef) bool

IsNotNull returns true if the field cannot be null.

func IsPrimaryKey

func IsPrimaryKey(f XFieldDef) bool

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) Exec

func (b *XBase) Exec(query string, args ...interface{}) (*sql.Rows, error)

func (*XBase) Logoff

func (b *XBase) Logoff()

func (*XBase) Logon

func (b *XBase) Logon()

func (*XBase) NewXCursor added in v0.5.0

func (b *XBase) NewXCursor() *XCursor

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

func (c *XCondition) GetCondition(table *XTable, DB string, baseindex int) (string, interface{}, bool)

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

type XConstraint struct {
	Type string
	Data []string
}

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

func (c *XConstraints) CreateIndex(table string, prepend string, field string, DB string) []string

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
}

func (*XCursor) Close added in v0.5.0

func (c *XCursor) Close()

func (*XCursor) Exec added in v0.5.0

func (c *XCursor) Exec(query string, args ...interface{}) (err error)

func (*XCursor) Next added in v0.5.0

func (c *XCursor) Next() bool

func (*XCursor) Read added in v0.5.0

func (c *XCursor) Read() (*XRecord, error)

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

func (XFieldDate) GetName

func (f XFieldDate) GetName() string

gets the name of the field

func (XFieldDate) GetType

func (f XFieldDate) GetType() int

gets the type 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

func (XFieldDateTime) GetName

func (f XFieldDateTime) GetName() string

gets the name of the field

func (XFieldDateTime) GetType

func (f XFieldDateTime) GetType() int

gets the type 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

func (XFieldFloat) GetName

func (f XFieldFloat) GetName() string

gets the name of the field

func (XFieldFloat) GetType

func (f XFieldFloat) GetType() int

gets the type 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) GetName

func (f XFieldInteger) GetName() string

gets the name of the field

func (XFieldInteger) GetType

func (f XFieldInteger) GetType() int

gets the type of the field

func (XFieldInteger) IsAutoIncrement

func (f XFieldInteger) IsAutoIncrement() bool

Is it autoincrement

type XFieldSet

type XFieldSet []string

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

func (XFieldText) GetName

func (f XFieldText) GetName() string

gets the name of the field

func (XFieldText) GetType

func (f XFieldText) GetType() int

gets the type 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

func (XFieldVarChar) GetName

func (f XFieldVarChar) GetName() string

gets the name of the field

func (XFieldVarChar) GetType

func (f XFieldVarChar) GetType() int

gets the type of the field

type XGroup

type XGroup []XGroupBy

func (*XGroup) CreateGroup

func (g *XGroup) CreateGroup(table *XTable, DB string) string

type XGroupBy

type XGroupBy struct {
	Field string
}

func (*XGroupBy) GetGroup

func (g *XGroupBy) GetGroup(table *XTable, DB string) string

type XHaving

type XHaving []XCondition

func (*XHaving) CreateHaving

func (h *XHaving) CreateHaving(table *XTable, DB string, baseindex int) (string, []interface{})

type XOrder

type XOrder []XOrderBy

func (*XOrder) Clone added in v0.4.2

func (o *XOrder) Clone() XOrder

func (*XOrder) CreateOrder

func (o *XOrder) CreateOrder(table *XTable, DB string) string

type XOrderBy

type XOrderBy struct {
	Field    string
	Operator string
}

func NewXOrderBy

func NewXOrderBy(field string, operator string) XOrderBy

func (*XOrderBy) Clone added in v0.4.2

func (o *XOrderBy) Clone() XOrderBy

func (*XOrderBy) GetOrder

func (c *XOrderBy) GetOrder(table *XTable, DB string) string

type XRecord

type XRecord map[string]interface{}

func NewXRecord

func NewXRecord() *XRecord

func (*XRecord) Clone

func (r *XRecord) Clone() xcore.XDatasetDef

func (*XRecord) Del

func (r *XRecord) Del(key string)

func (*XRecord) Get

func (r *XRecord) Get(key string) (interface{}, bool)

func (*XRecord) GetBool

func (r *XRecord) GetBool(key string) (bool, bool)

func (*XRecord) GetBoolCollection

func (r *XRecord) GetBoolCollection(key string) ([]bool, bool)

func (*XRecord) GetCollection

func (r *XRecord) GetCollection(key string) (xcore.XDatasetCollectionDef, bool)

func (*XRecord) GetDataset

func (r *XRecord) GetDataset(key string) (xcore.XDatasetDef, bool)

func (*XRecord) GetFloat

func (r *XRecord) GetFloat(key string) (float64, bool)

func (*XRecord) GetFloatCollection

func (r *XRecord) GetFloatCollection(key string) ([]float64, bool)

func (*XRecord) GetInt

func (r *XRecord) GetInt(key string) (int, bool)

func (*XRecord) GetIntCollection

func (r *XRecord) GetIntCollection(key string) ([]int, bool)

func (*XRecord) GetString

func (r *XRecord) GetString(key string) (string, bool)

func (*XRecord) GetStringCollection

func (r *XRecord) GetStringCollection(key string) ([]string, bool)

func (*XRecord) GetTime

func (r *XRecord) GetTime(key string) (time.Time, bool)

func (*XRecord) GetTimeCollection

func (r *XRecord) GetTimeCollection(key string) ([]time.Time, bool)

func (*XRecord) GoString

func (r *XRecord) GoString() string

func (*XRecord) Set

func (r *XRecord) Set(key string, data interface{})

func (*XRecord) String

func (r *XRecord) String() string

makes an interface of XDataset to reuse for other libraries and be sure we can call the functions

type XRecordDef

type XRecordDef interface {
	xcore.XDatasetDef
}

type XRecords

type XRecords []XRecordDef

func (*XRecords) Clone

func (r *XRecords) Clone() xcore.XDatasetCollectionDef

func (*XRecords) Count

func (r *XRecords) Count() int

func (*XRecords) Get

func (r *XRecords) Get(index int) (xcore.XDatasetDef, bool)

func (*XRecords) GetCollection

func (r *XRecords) GetCollection(key string) (xcore.XDatasetCollectionDef, bool)

func (*XRecords) GetData

func (r *XRecords) GetData(key string) (interface{}, bool)

func (*XRecords) GetDataBool

func (d *XRecords) GetDataBool(key string) (bool, bool)

func (*XRecords) GetDataFloat

func (d *XRecords) GetDataFloat(key string) (float64, bool)

func (*XRecords) GetDataInt

func (d *XRecords) GetDataInt(key string) (int, bool)

func (*XRecords) GetDataString

func (r *XRecords) GetDataString(key string) (string, bool)

func (*XRecords) GetDataTime

func (d *XRecords) GetDataTime(key string) (time.Time, bool)

func (*XRecords) GoString

func (r *XRecords) GoString() string

func (*XRecords) Pop

func (r *XRecords) Pop() xcore.XDatasetDef

func (*XRecords) Push

func (r *XRecords) Push(data xcore.XDatasetDef)

func (*XRecords) Shift

func (d *XRecords) Shift() xcore.XDatasetDef

func (*XRecords) String

func (r *XRecords) String() string

func (*XRecords) Unshift

func (d *XRecords) Unshift(data 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 NewXTable

func NewXTable(name string, prepend string) *XTable

func (*XTable) AddField

func (t *XTable) AddField(field XFieldDef)

func (*XTable) Avg

func (t *XTable) Avg(field string, args ...interface{}) (interface{}, error)

func (*XTable) Count

func (t *XTable) Count(args ...interface{}) (int, error)

func (*XTable) Delete

func (t *XTable) Delete(args ...interface{}) (int, error)

func (*XTable) GetField

func (t *XTable) GetField(name string) XFieldDef

func (*XTable) GetPrimaryKey

func (t *XTable) GetPrimaryKey() XFieldDef

func (*XTable) Insert

func (t *XTable) Insert(data interface{}, args ...interface{}) (interface{}, error)

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) Max

func (t *XTable) Max(field string, args ...interface{}) (interface{}, error)

func (*XTable) Min

func (t *XTable) Min(field string, args ...interface{}) (interface{}, error)

func (*XTable) Select

func (t *XTable) Select(args ...interface{}) (interface{}, error)

func (*XTable) SelectAll

func (t *XTable) SelectAll(args ...interface{}) (*XRecords, error)

func (*XTable) SelectOne

func (t *XTable) SelectOne(args ...interface{}) (*XRecord, error)

func (*XTable) SetBase

func (t *XTable) SetBase(base *XBase)

func (*XTable) SetLanguage

func (t *XTable) SetLanguage(lang language.Tag)

func (*XTable) Synchronize

func (t *XTable) Synchronize(args ...interface{}) error

func (*XTable) Update

func (t *XTable) Update(args ...interface{}) (int, error)

func (*XTable) Upsert

func (t *XTable) Upsert(args ...interface{}) (int, error)

type XTransaction added in v0.3.3

type XTransaction struct {
	DB *XBase
	TX *sql.Tx
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL