xdominion

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2022 License: MIT Imports: 13 Imported by: 34

README

@UTF-8

XDominion for GO v0

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 in this version. More databases will be supported later.

XDominion is the Go adaptation of PHP7-Dominion libraries: a full database abstraction layer

TO DO:

  • text, float, time, date, lob fields
  • Joins
  • Sub Queries
  • Group and report functions
  • Synchro to upgrade DB tables and fields
  • Oracle, Informix, Mongo, other DBs

Manual:

  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

Examples:

Some code to start working:

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 query:

  q, err := base.Exec("drop table test")
  if (err != nil) {
    fmt.Println(err)
  }
  q.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()
  1. Reference

XBase

XTable

XRecord

Version Changes Control

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

Index

Examples

Constants

View Source
const (
	// Version of XDominion
	VERSION = "0.4.2"

	// 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"
	NN = "nn"
	AI = "ai"
	FK = "fk"
	IN = "in"
	UI = "ui"
	MI = "mi"
	MU = "mu"
	DC = "dc"
	TR = "tr"
)
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"
)

Variables

View Source
var DEBUG bool = false

Functions

func IsAutoIncrement

func IsAutoIncrement(f XFieldDef) bool

func IsFieldConstraint

func IsFieldConstraint(f XFieldDef, ftype string) bool

func IsNotNull

func IsNotNull(f XFieldDef) bool

func IsPrimaryKey

func IsPrimaryKey(f XFieldDef) bool

returns true if the field is a primary key for the table

Types

type Cursor

type Cursor struct {
	Base *XBase
}

func (*Cursor) Close

func (c *Cursor) Close()

func (*Cursor) Query

func (c *Cursor) Query() interface{}

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

func (b *XBase) Cursor() *Cursor

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

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
}

type XConstraints

type XConstraints []XConstraint

func (*XConstraints) CreateConstraints

func (c *XConstraints) CreateConstraints(prepend string, name string, DB string) string

func (*XConstraints) CreateIndex added in v0.3.3

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

func (*XConstraints) Get

func (c *XConstraints) Get(ctype string) *XConstraint

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 {
	// creates the name of the field with its type (to create the table)
	CreateField(prepend string, DB string, ifText *bool) string
	// creates a string representation of the value of the field for insert/update with ' for text
	CreateValue(v interface{}, table string, DB string, id string) string
	// creates the sequence used by the field (only autoincrement fields)
	CreateSequence(table string) string
	// creates the index used by the field (normal, unique, multi, multi unique)
	CreateIndex(table string, id string, DB string) []string
	// gets the name of the field
	GetName() string
	// gets the type of the field
	GetType() int
	// gets the checks of the field
	GetConstraints() XConstraints
}

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