gdb

package
v0.0.0-...-8506bae Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 19 Imported by: 0

README

GMC DATABASE

  1. Support of MYSQL , SQLITE3.
  2. Support of Multiple database source.
  3. Support of encrypt sqlite3 databse.

Configuration

database configuration section in app.toml

########################################################
# database configuration
########################################################
# mysql,sqlite3 are both supported
# support of mutiple mysql server 
# support of mutiple sqlite3 database
# notic: each config section must have an unique id 
########################################################
[database]
default="mysql"
[[database.mysql]]
enable=true
id="default"
host="127.0.0.1"
port="3306"
username="root"
password="admin"
database="test"
prefix=""
prefix_sql_holder="__PREFIX__"
charset="utf8"
collate="utf8_general_ci"
maxidle=30
maxconns=200
timeout=3000
readtimeout=5000
writetimeout=5000
maxlifetimeseconds=1800

[[database.mysql]]
enable=false
id="news"
host="127.0.0.1"
port="3306"
username="root"
password="admin"
database="test"
prefix=""
prefix_sql_holder="__PREFIX__"
charset="utf8"
collate="utf8_general_ci"
maxidle=30
maxconns=200
timeout=3000
readtimeout=5000
writetimeout=5000
maxlifetimeseconds=1800

[[database.sqlite3]]
enable=false
id="default"
database="test.db"
# if password is not empty , database will be encrypted.
password=""
prefix=""
prefix_sql_holder="__PREFIX__"
# syncmode 0:OFF, 1:NORMAL, 2:FULL, 3:EXTRA
syncmode=0
# openmode ro,rw,rwc,memory
openmode="rw"
# shared,private
cachemode="shared"

Example

package main

import (
	"github.com/snail007/gmc"
)

func main() {
	cfg := gmc.New.Config()
	cfg.SetConfigFile("../../app.toml")
	err := cfg.ReadInConfig()
	if err != nil {
		panic(err)
	}
	// Init only using [database] section in app.toml
	gmc.DB.Init(cfg)

	// database default is mysql in app.toml
	// so gmc.DB.DB() equal to  gmc.DB.MySQL()
	// we can connect to multiple cache drivers at same time, id is the unique name of driver
	// gmc.DB.DB(id) to load `id` named default driver.
	db := gmc.DB.DB().(*gdb.MySQLDB)
	//do something with db
}

Documentation

Index

Constants

View Source
const (
	OpenModeReadOnly        = "ro"
	OpenModeReadWrite       = "rw"
	OpenModeReadWriteCreate = "rwc"
	OpenModeMemory          = "memory"
	CacheModeShared         = "shared"
	CacheModePrivate        = "private"
	SyncModeOff             = 0
	SyncModeNormal          = 1
	SyncModeFull            = 2
	SyncModeExtra           = 3
)

Variables

This section is empty.

Functions

func DB

func DB(id ...string) gcore.Database

func Init

func Init(cfg0 gcore.Config) (err error)

Init parse app.toml database configuration, `cfg` is Config object of app.toml

func InitFromFile

func InitFromFile(cfgFile string) (err error)

InitFromFile parse foo.toml database configuration, `cfg` is Config object of foo.toml

Types

type M

type M map[string]interface{}

type Model

type Model struct {
	// contains filtered or unexported fields
}

func Table

func Table(table string, db ...interface{}) *Model

func (*Model) Count

func (s *Model) Count(where gmap.M) (count int64, error error)

func (*Model) DeleteBy

func (s *Model) DeleteBy(where map[string]interface{}) (cnt int64, err error)

func (*Model) DeleteByIDs

func (s *Model) DeleteByIDs(ids []string) (cnt int64, err error)

func (*Model) ExecSQL

func (s *Model) ExecSQL(sql string, values ...interface{}) (lastInsertID, rowsAffected int64, error error)

func (*Model) GetAll

func (s *Model) GetAll(orderBy ...string) (ret []map[string]string, error error)

func (*Model) GetAllRs

func (s *Model) GetAllRs(orderBy ...string) (rs gcore.ResultSet, error error)

func (*Model) GetAllWithFields

func (s *Model) GetAllWithFields(fields string, orderBy ...string) (ret []map[string]string, error error)

func (*Model) GetAllWithFieldsRs

func (s *Model) GetAllWithFieldsRs(fields string, orderBy ...string) (rs gcore.ResultSet, error error)

func (*Model) GetBy

func (s *Model) GetBy(where map[string]interface{}) (ret map[string]string, error error)

func (*Model) GetByID

func (s *Model) GetByID(id string) (ret map[string]string, error error)

func (*Model) GetByIDWithFields

func (s *Model) GetByIDWithFields(fields string, id string) (ret map[string]string, error error)

func (*Model) GetByWithFields

func (s *Model) GetByWithFields(fields string, where map[string]interface{}) (ret map[string]string, error error)

func (*Model) Insert

func (s *Model) Insert(data map[string]interface{}) (lastInsertID int64, err error)

func (*Model) InsertBatch

func (s *Model) InsertBatch(data []map[string]interface{}) (cnt, lastInsertID int64, err error)

func (*Model) List

func (s *Model) List(where map[string]interface{}, offset, length int, orderBy ...string) (ret []map[string]string, err error)

func (*Model) ListWithFields

func (s *Model) ListWithFields(fields string, where map[string]interface{}, offset, length int, orderBy ...string) (ret []map[string]string, err error)

func (*Model) MGetBy

func (s *Model) MGetBy(where map[string]interface{}, orderBy ...string) (ret []map[string]string, error error)

func (*Model) MGetByIDs

func (s *Model) MGetByIDs(ids []string, orderBy ...string) (ret []map[string]string, error error)

func (*Model) MGetByIDsRs

func (s *Model) MGetByIDsRs(ids []string, orderBy ...string) (rs gcore.ResultSet, error error)

func (*Model) MGetByIDsWithFields

func (s *Model) MGetByIDsWithFields(fields string, ids []string, orderBy ...string) (ret []map[string]string, err error)

func (*Model) MGetByIDsWithFieldsRs

func (s *Model) MGetByIDsWithFieldsRs(fields string, ids []string, orderBy ...string) (rs gcore.ResultSet, err error)

func (*Model) MGetByRs

func (s *Model) MGetByRs(where map[string]interface{}, orderBy ...string) (rs gcore.ResultSet, error error)

func (*Model) MGetByWithFields

func (s *Model) MGetByWithFields(fields string, where map[string]interface{}, orderBy ...string) (ret []map[string]string, err error)

func (*Model) MGetByWithFieldsRs

func (s *Model) MGetByWithFieldsRs(fields string, where map[string]interface{}, orderBy ...string) (rs gcore.ResultSet, err error)

func (*Model) OrderBy

func (s *Model) OrderBy(ar gcore.ActiveRecord, orderBy ...string) (ret [][]string)

func (*Model) Page

func (s *Model) Page(where map[string]interface{}, offset, length int, orderBy ...string) (ret []map[string]string, total int, err error)

func (*Model) PageWithFields

func (s *Model) PageWithFields(fields string, where map[string]interface{}, offset, length int, orderBy ...string) (ret []map[string]string, total int, err error)

func (*Model) PrimaryKey

func (s *Model) PrimaryKey() string

func (*Model) QuerySQL

func (s *Model) QuerySQL(sql string, values ...interface{}) (ret []map[string]string, error error)

func (*Model) SetPrimaryKey

func (s *Model) SetPrimaryKey(primaryKey string) *Model

func (*Model) UpdateBy

func (s *Model) UpdateBy(where, data map[string]interface{}) (cnt int64, err error)

func (*Model) UpdateByIDs

func (s *Model) UpdateByIDs(ids []string, data map[string]interface{}) (cnt int64, err error)

type MySQLActiveRecord

type MySQLActiveRecord struct {
	// contains filtered or unexported fields
}

func (*MySQLActiveRecord) Cache

func (ar *MySQLActiveRecord) Cache(key string, seconds uint) gcore.ActiveRecord

func (*MySQLActiveRecord) Delete

func (ar *MySQLActiveRecord) Delete(table string, where gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) From

func (ar *MySQLActiveRecord) From(from string) gcore.ActiveRecord

func (*MySQLActiveRecord) FromAs

func (ar *MySQLActiveRecord) FromAs(from, as string) gcore.ActiveRecord

func (*MySQLActiveRecord) GetValues

func (ar *MySQLActiveRecord) GetValues() []interface{}

func (*MySQLActiveRecord) GroupBy

func (ar *MySQLActiveRecord) GroupBy(column string) gcore.ActiveRecord

func (*MySQLActiveRecord) Having

func (ar *MySQLActiveRecord) Having(having string) gcore.ActiveRecord

func (*MySQLActiveRecord) HavingWrap

func (ar *MySQLActiveRecord) HavingWrap(having, leftWrap, rightWrap string) gcore.ActiveRecord

func (*MySQLActiveRecord) Insert

func (ar *MySQLActiveRecord) Insert(table string, data gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) InsertBatch

func (ar *MySQLActiveRecord) InsertBatch(table string, data []gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) Join

func (ar *MySQLActiveRecord) Join(table, as, on, typ string) gcore.ActiveRecord

func (*MySQLActiveRecord) Limit

func (ar *MySQLActiveRecord) Limit(limit ...int) gcore.ActiveRecord

Limit Limit(offset,count) or Limit(count)

func (*MySQLActiveRecord) OrderBy

func (ar *MySQLActiveRecord) OrderBy(column, typ string) gcore.ActiveRecord

func (*MySQLActiveRecord) Raw

func (ar *MySQLActiveRecord) Raw(sql string, values ...interface{}) gcore.ActiveRecord

func (*MySQLActiveRecord) Replace

func (ar *MySQLActiveRecord) Replace(table string, data gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) ReplaceBatch

func (ar *MySQLActiveRecord) ReplaceBatch(table string, data []gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) Reset

func (ar *MySQLActiveRecord) Reset()

func (*MySQLActiveRecord) SQL

func (ar *MySQLActiveRecord) SQL() string

func (*MySQLActiveRecord) Select

func (ar *MySQLActiveRecord) Select(columns string) gcore.ActiveRecord

func (*MySQLActiveRecord) SelectNoWrap

func (ar *MySQLActiveRecord) SelectNoWrap(columns string) gcore.ActiveRecord

func (*MySQLActiveRecord) Set

func (ar *MySQLActiveRecord) Set(column string, value interface{}) gcore.ActiveRecord

func (*MySQLActiveRecord) SetNoWrap

func (ar *MySQLActiveRecord) SetNoWrap(column string, value interface{}) gcore.ActiveRecord

func (*MySQLActiveRecord) Update

func (ar *MySQLActiveRecord) Update(table string, data, where gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) UpdateBatch

func (ar *MySQLActiveRecord) UpdateBatch(table string, values []gmap.M, whereColumn []string) gcore.ActiveRecord

func (*MySQLActiveRecord) Values

func (ar *MySQLActiveRecord) Values() []interface{}

func (*MySQLActiveRecord) Where

func (ar *MySQLActiveRecord) Where(where gmap.M) gcore.ActiveRecord

func (*MySQLActiveRecord) WhereRaw

func (ar *MySQLActiveRecord) WhereRaw(where string) gcore.ActiveRecord

func (*MySQLActiveRecord) WhereWrap

func (ar *MySQLActiveRecord) WhereWrap(where gmap.M, leftWrap, rightWrap string) gcore.ActiveRecord

func (*MySQLActiveRecord) Wrap

func (ar *MySQLActiveRecord) Wrap(v string) string

type MySQLDB

type MySQLDB struct {
	Config   MySQLDBConfig
	ConnPool *sql.DB
	DSN      string
}

func DBMySQL

func DBMySQL(id ...string) *MySQLDB

DBMySQL acquires a mysql db object associated the id, id default is : `default`

func NewMySQLDB

func NewMySQLDB(config MySQLDBConfig) (db *MySQLDB, err error)

func (*MySQLDB) AR

func (db *MySQLDB) AR() (ar gcore.ActiveRecord)

func (*MySQLDB) Begin

func (db *MySQLDB) Begin() (tx *sql.Tx, err error)

func (*MySQLDB) Exec

func (db *MySQLDB) Exec(ar gcore.ActiveRecord) (rs gcore.ResultSet, err error)

func (*MySQLDB) ExecSQL

func (db *MySQLDB) ExecSQL(sqlStr string, values ...interface{}) (rs gcore.ResultSet, err error)

func (*MySQLDB) ExecSQLTx

func (db *MySQLDB) ExecSQLTx(tx *sql.Tx, sqlStr string, values ...interface{}) (rs gcore.ResultSet, err error)

func (*MySQLDB) ExecTx

func (db *MySQLDB) ExecTx(ar0 gcore.ActiveRecord, tx *sql.Tx) (rs gcore.ResultSet, err error)

func (*MySQLDB) Query

func (db *MySQLDB) Query(ar0 gcore.ActiveRecord) (rs gcore.ResultSet, err error)

func (*MySQLDB) QuerySQL

func (db *MySQLDB) QuerySQL(sqlStr string, values ...interface{}) (rs gcore.ResultSet, err error)

func (*MySQLDB) Stats

func (db *MySQLDB) Stats() sql.DBStats

type MySQLDBConfig

type MySQLDBConfig struct {
	Charset                  string
	Collate                  string
	Database                 string
	Host                     string
	Port                     int
	Username                 string
	Password                 string
	TablePrefix              string
	TablePrefixSQLIdentifier string
	Timeout                  int
	ReadTimeout              int
	WriteTimeout             int
	MaxIdleConns             int
	MaxOpenConns             int
	Cache                    gcore.DBCache
}

func NewMySQLDBConfig

func NewMySQLDBConfig() MySQLDBConfig

func NewMySQLDBConfigWith

func NewMySQLDBConfigWith(host string, port int, dbName, user, pass string) (cfg MySQLDBConfig)

type MySQLDBGroup

type MySQLDBGroup struct {
	// contains filtered or unexported fields
}

func NewMySQLDBGroup

func NewMySQLDBGroup(defaultConfigName string) (group *MySQLDBGroup)

func NewMySQLDBGroupCache

func NewMySQLDBGroupCache(defaultConfigName string, cache gcore.DBCache) (group *MySQLDBGroup)

func (*MySQLDBGroup) DB

func (g *MySQLDBGroup) DB(name ...string) (db gcore.Database)

func (*MySQLDBGroup) Regist

func (g *MySQLDBGroup) Regist(name string, cfgI interface{}) (err error)

func (*MySQLDBGroup) RegistGroup

func (g *MySQLDBGroup) RegistGroup(cfg interface{}) (err error)

type ResultSet

type ResultSet struct {
	// contains filtered or unexported fields
}

func NewResultSet

func NewResultSet(rawRows *[]map[string][]byte) (rs *ResultSet)

func (*ResultSet) LastInsertID

func (rs *ResultSet) LastInsertID() int64

func (*ResultSet) Len

func (rs *ResultSet) Len() int

func (*ResultSet) MapRows

func (rs *ResultSet) MapRows(keyColumn string) (rowsMap map[string]map[string]string)

func (*ResultSet) MapStructs

func (rs *ResultSet) MapStructs(keyColumn string, strucT interface{}, tagName ...string) (structsMap map[string]interface{}, err error)

func (*ResultSet) MapValues

func (rs *ResultSet) MapValues(keyColumn, valueColumn string) (values map[string]string)

func (*ResultSet) Row

func (rs *ResultSet) Row() (row map[string]string)

func (*ResultSet) Rows

func (rs *ResultSet) Rows() (rows []map[string]string)

func (*ResultSet) RowsAffected

func (rs *ResultSet) RowsAffected() int64

func (*ResultSet) SQL

func (rs *ResultSet) SQL() string

func (*ResultSet) Struct

func (rs *ResultSet) Struct(strucT interface{}, tagName ...string) (Struct interface{}, err error)

func (*ResultSet) Structs

func (rs *ResultSet) Structs(strucT interface{}, tagName ...string) (structs []interface{}, err error)

func (*ResultSet) TimeUsed

func (rs *ResultSet) TimeUsed() time.Duration

func (*ResultSet) Value

func (rs *ResultSet) Value(column string) (value string)

func (*ResultSet) Values

func (rs *ResultSet) Values(column string) (values []string)

type SQLite3ActiveRecord

type SQLite3ActiveRecord struct {
	// contains filtered or unexported fields
}

func (*SQLite3ActiveRecord) Cache

func (ar *SQLite3ActiveRecord) Cache(key string, seconds uint) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Delete

func (ar *SQLite3ActiveRecord) Delete(table string, where gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) From

func (ar *SQLite3ActiveRecord) From(from string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) FromAs

func (ar *SQLite3ActiveRecord) FromAs(from, as string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) GetValues

func (ar *SQLite3ActiveRecord) GetValues() []interface{}

func (*SQLite3ActiveRecord) GroupBy

func (ar *SQLite3ActiveRecord) GroupBy(column string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Having

func (ar *SQLite3ActiveRecord) Having(having string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) HavingWrap

func (ar *SQLite3ActiveRecord) HavingWrap(having, leftWrap, rightWrap string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Insert

func (ar *SQLite3ActiveRecord) Insert(table string, data gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) InsertBatch

func (ar *SQLite3ActiveRecord) InsertBatch(table string, data []gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Join

func (ar *SQLite3ActiveRecord) Join(table, as, on, typ string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Limit

func (ar *SQLite3ActiveRecord) Limit(limit ...int) gcore.ActiveRecord

Limit Limit(offset,count) or Limit(count)

func (*SQLite3ActiveRecord) OrderBy

func (ar *SQLite3ActiveRecord) OrderBy(column, typ string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Raw

func (ar *SQLite3ActiveRecord) Raw(sql string, values ...interface{}) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Replace

func (ar *SQLite3ActiveRecord) Replace(table string, data gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) ReplaceBatch

func (ar *SQLite3ActiveRecord) ReplaceBatch(table string, data []gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Reset

func (ar *SQLite3ActiveRecord) Reset()

func (*SQLite3ActiveRecord) SQL

func (ar *SQLite3ActiveRecord) SQL() string

func (*SQLite3ActiveRecord) Select

func (ar *SQLite3ActiveRecord) Select(columns string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) SelectNoWrap

func (ar *SQLite3ActiveRecord) SelectNoWrap(columns string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Set

func (ar *SQLite3ActiveRecord) Set(column string, value interface{}) gcore.ActiveRecord

func (*SQLite3ActiveRecord) SetNoWrap

func (ar *SQLite3ActiveRecord) SetNoWrap(column string, value interface{}) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Update

func (ar *SQLite3ActiveRecord) Update(table string, data, where gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) UpdateBatch

func (ar *SQLite3ActiveRecord) UpdateBatch(table string, values []gmap.M, whereColumn []string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Values

func (ar *SQLite3ActiveRecord) Values() []interface{}

func (*SQLite3ActiveRecord) Where

func (ar *SQLite3ActiveRecord) Where(where gmap.M) gcore.ActiveRecord

func (*SQLite3ActiveRecord) WhereRaw

func (ar *SQLite3ActiveRecord) WhereRaw(where string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) WhereWrap

func (ar *SQLite3ActiveRecord) WhereWrap(where gmap.M, leftWrap, rightWrap string) gcore.ActiveRecord

func (*SQLite3ActiveRecord) Wrap

func (ar *SQLite3ActiveRecord) Wrap(v string) string

type SQLite3DB

type SQLite3DB struct {
	Config   SQLite3DBConfig
	ConnPool *sql.DB
	DSN      string
}

func DBSQLite3

func DBSQLite3(id ...string) *SQLite3DB

DBSQLite3 acquires a sqlite3 db object associated the id, id default is : `default`

func NewSQLite3DB

func NewSQLite3DB(config SQLite3DBConfig) (db SQLite3DB, err error)

func (*SQLite3DB) AR

func (db *SQLite3DB) AR() gcore.ActiveRecord

func (*SQLite3DB) Begin

func (db *SQLite3DB) Begin() (tx *sql.Tx, err error)

func (*SQLite3DB) Exec

func (db *SQLite3DB) Exec(ar0 gcore.ActiveRecord) (rs gcore.ResultSet, err error)

func (*SQLite3DB) ExecSQL

func (db *SQLite3DB) ExecSQL(sqlStr string, values ...interface{}) (rs gcore.ResultSet, err error)

func (*SQLite3DB) ExecSQLTx

func (db *SQLite3DB) ExecSQLTx(tx *sql.Tx, sqlStr string, values ...interface{}) (rs gcore.ResultSet, err error)

func (*SQLite3DB) ExecTx

func (db *SQLite3DB) ExecTx(ar0 gcore.ActiveRecord, tx *sql.Tx) (rs gcore.ResultSet, err error)

func (*SQLite3DB) Query

func (db *SQLite3DB) Query(ar0 gcore.ActiveRecord) (rs gcore.ResultSet, err error)

func (*SQLite3DB) QuerySQL

func (db *SQLite3DB) QuerySQL(sqlStr string, values ...interface{}) (rs gcore.ResultSet, err error)

func (*SQLite3DB) Stats

func (db *SQLite3DB) Stats() sql.DBStats

type SQLite3DBConfig

type SQLite3DBConfig struct {
	Database                 string
	TablePrefix              string
	TablePrefixSQLIdentifier string
	Cache                    gcore.DBCache
	SyncMode                 int
	OpenMode                 string
	CacheMode                string
}

func NewSQLite3DBConfig

func NewSQLite3DBConfig() SQLite3DBConfig

func NewSQLite3DBConfigWith

func NewSQLite3DBConfigWith(dbfilename, openMode, cacheMode string, syncMode int) (cfg SQLite3DBConfig)

type SQLite3DBGroup

type SQLite3DBGroup struct {
	// contains filtered or unexported fields
}

func NewSQLite3DBGroup

func NewSQLite3DBGroup(defaultConfigName string) (group *SQLite3DBGroup)

func NewSQLite3DBGroupCache

func NewSQLite3DBGroupCache(defaultConfigName string, cache gcore.DBCache) (group *SQLite3DBGroup)

func (*SQLite3DBGroup) DB

func (g *SQLite3DBGroup) DB(name ...string) (db gcore.Database)

func (*SQLite3DBGroup) Regist

func (g *SQLite3DBGroup) Regist(name string, cfgI interface{}) (err error)

func (*SQLite3DBGroup) RegistGroup

func (g *SQLite3DBGroup) RegistGroup(cfg interface{}) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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