README
¶
Goloquent
The only sequel ORM that respect google datastore.
Why Goloquent?
Few years back, we required to migrate our database to local due to compliance issue, and there have no way to map back the data to SQL database. For sake, we introduce goloquent for whoever want to migrate their google datastore to SQL database.
Database Support
- MySQL (version 5.7 and above)
- Postgres (version 9.4 and above)
This package is not compactible with native package database/sql
, if you want the support of it, you may go for sqlike
Sequel Datastore ORM
Inspired by Laravel Eloquent and Google Cloud Datastore
This repo still under development. We accept any pull request. ^_^
Installation
// dependency
$ go get -u github.com/go-sql-driver/mysql // Mysql
$ go get -u github.com/lib/pq // Postgres
$ go get -u cloud.google.com/go/datastore
$ go get -u github.com/Oskang09/goloquent
- Import the library
import "github.com/Oskang09/goloquent"
Quick Start
Connect to database
import "github.com/Oskang09/goloquent/db"
conn, err := db.Open("mysql", db.Config{
Username: "root",
Password: "",
Host: "localhost",
Port: "3306",
Database: "test",
Logger: func(stmt *goloquent.Stmt) {
log.Println(stmt.TimeElapse()) // elapse time in time.Duration
log.Println(stmt.String()) // Sql string without any ?
log.Println(stmt.Raw()) // Sql prepare statement
log.Println(stmt.Arguments()) // Sql prepare statement's arguments
log.Println(fmt.Sprintf("[%.3fms] %s", stmt.TimeElapse().Seconds()*1000, stmt.String()))
},
})
defer conn.Close()
if err != nil {
panic("Connection error: ", err)
}
User Table
// Address :
type Address struct {
Line1 string
Line2 string
Country string
PostCode uint
Region struct {
TimeZone time.Time
Keys []*datastore.Key `goloquent:"keys"`
CountryCode string `goloquent:"regionCode"`
Geolocation datastore.GeoPoint `goloquent:"geo"`
} `goloquent:"region"`
}
// User : User kind parent is Merchant
type User struct {
Key *datastore.Key `goloquent:"__key__"` // load table key
Name string
Nicknames []string
CountryCode string
PhoneNumber string
Email string
BirthDate goloquent.Date
Address Address
Age uint8
ExtraInfo json.RawMessage
CreatedDateTime time.Time
UpdatedDateTime time.Time
}
// Load : load func will execute after load
func (x *User) Load() error {
return nil
}
// Save : save func will execute before save
func (x *User) Save() (error) {
return nil
}
Helper
// StringPrimaryKey is to get key value in string form
key := datastore.NameKey("Merchant", "mjfFgYnxBS", nil)
fmt.Println(goloquent.StringKey(key)) // "mjfFgYnxBS"
key = datastore.IDKey("User", int64(2305297334603281546), nil)
fmt.Println(goloquent.StringKey(key)) // "2305297334603281546"
key = datastore.NameKey("User", int64(2305297334603281546), datastore.NameKey("Merchant", "mjfFgYnxBS", nil))
fmt.Println(goloquent.StringifyKey(key)) // Merchant,'mjfFgYnxBS'/User,2305297334603281546
Table
import "github.com/Oskang09/goloquent/db"
// Check table exists
db.Table("User").Exists() // true
// Drop table if exists
if err := db.Table("User").DropIfExists(); err != nil {
log.Fatal(err)
}
// Add index
if err := db.Table("User").AddIndex("Name", "Email"); err != nil {
log.Fatal(err)
}
// Add unique index
if err := db.Table("User").AddUniqueIndex("Email"); err != nil {
log.Fatal(err)
}
Create Record
import "github.com/Oskang09/goloquent/db"
// Example
user := new(User)
user.Name = "Hello World"
user.Age = 18
// OR
var user *User
user.Name = "Hello World"
user.Age = 18
// Create without parent key
if err := db.Create(user, nil); err != nil {
log.Println(err) // fail to create record
}
// Create with parent key
parentKey := datastore.NameKey("Parent", "value", nil)
if err := db.Create(user, parentKey); err != nil {
log.Println(err) // fail to create record
}
// Create without key, goloquent will auto generate primary key
if err := db.Create(user); err != nil {
log.Println(err) // fail to create record
}
// Create with self generate key
user.Key = datastore.NameKey("User", "uniqueID", nil)
if err := db.Create(user); err != nil {
log.Println(err) // fail to create record
}
Upsert Record
import "github.com/Oskang09/goloquent/db"
// Example
user := new(User)
user.Name = "Hello World"
user.Age = 18
// Update if key exists, else create the user record
parentKey := datastore.NameKey("Parent", "value", nil)
if err := db.Upsert(user, parentKey); err != nil {
log.Println(err) // fail
}
// Upsert with self generate key
user := new(User)
user.Key = datastore.NameKey("User", "uniqueID", nil)
user.Name = "Hello World"
user.Age = 18
if err := db.Upsert(user); err != nil {
log.Println(err) // fail
}
Retrieve Record
- Get Single Record using Primary Key
// Example
primaryKey := datastore.IDKey("User", int64(2305297334603281546), nil)
user := new(User)
if err := db.Find(primaryKey, user); err != nil {
log.Println(err) // error while retrieving record
}
if err := db.Where("Status", "=", "ACTIVE").
Find(primaryKey, user); err != goloquent.ErrNoSuchEntity {
// if no record found using primary key, error `ErrNoSuchEntity` will throw instead
log.Println(err) // error while retrieving record
}
- Get Single Record
import "github.com/Oskang09/goloquent/db"
// Example 1
user := new(User)
if err := db.First(user); err != nil {
log.Println(err) // error while retrieving record
}
if user.Key != nil { // if have record
fmt.Println("Have record")
} else { // no record
fmt.Println("Doesnt't have record")
}
// Example 2
user := new(User)
if err := db.Where("Email", "=", "admin@hotmail.com").
First(user); err != nil {
log.Println(err) // error while retrieving record
}
// Example 3
age := 22
parentKey := datastore.IDKey("Parent", 1093, nil)
user := new(User)
if err := db.Ancestor(parentKey).
WhereEqual("Age", &age).
OrderBy("-CreatedDateTime").
First(user); err != nil {
log.Println(err) // error while retrieving record
}
- Get Multiple Record
import "github.com/Oskang09/goloquent/db"
// Example 1
users := new([]User)
if err := db.Limit(10).Get(ctx, users); err != nil {
log.Println(err) // error while retrieving record
}
// Example 2
users := new([]*User)
if err := db.WhereEqual("Name", "Hello World").
Get(ctx, users); err != nil {
log.Println(err) // error while retrieving record
}
// Example 3
users := new([]User)
if err := db.Ancestor(parentKey).
WhereEqual("Name", "myz").
WhereEqual("Age", 22).
Get(ctx, users); err != nil {
log.Println(err) // error while retrieving record
}
- Get Record with OrderBying
import "github.com/Oskang09/goloquent/db"
// Ascending OrderBy
users := new([]*User)
if err := db.OrderBy("CreatedDateTime").
Get(ctx, users); err != nil {
log.Println(err) // error while retrieving record
}
// Descending OrderBy
if err := db.Table("User").
OrderBy("-CreatedDateTime").
Get(ctx, users); err != nil {
log.Println(err) // error while retrieving record
}
- Pagination Record
import "github.com/Oskang09/goloquent/db"
p := goloquent.Pagination{
Limit: 10,
Cursor: "", // pass the cursor that generate by the query so that it will display the next record
}
// Example
users := new([]*User)
if err := db.Ancestor(parentKey).
OrderBy("-CreatedDateTime").
Paginate(&p, users); err != nil {
log.Println(err) // error while retrieving record
}
// ***************** OR ********************
p := &goloquent.Pagination{
Limit: 10, // number of records in each page
Cursor: "EhQKCE1lcmNoYW50EK3bueKni5eNIxIWCgxMb3lhbHR5UG9pbnQaBkZrUUc4eA", // pass the cursor to get next record set
}
users := new([]*User)
if err := db.Ancestor(parentKey).
OrderBy("-CreatedDateTime").
Paginate(p, users); err != nil {
log.Println(err) // error while retrieving record
}
log.Println(p.NextCursor()) // next page cursor
log.Println(p.Count()) // record count
Save Record
import "github.com/Oskang09/goloquent/db"
// Example
if err := db.Save(user); err != nil {
log.Println(err) // fail to delete record
}
Delete Record
- Delete using Primary Key
import "github.com/Oskang09/goloquent/db"
// Example
if err := db.Delete(user); err != nil {
log.Println(err) // fail to delete record
}
- Delete using Where statement
// Delete user table record which account type not equal to "PREMIUM" or "MONTLY"
if err := db.Table("User").
WhereNotIn("AccountType", []string{
"PREMIUM", "MONTLY",
}).Flush(); err != nil {
log.Println(err) // fail to delete record
}
Transaction
// Example
if err := db.RunInTransaction(func(txn *goloquent.DB) error {
user := new(User)
if err := txn.Create(user, nil); err != nil {
return err // return any err to rollback the transaction
}
return nil // return nil to commit the transaction
}); err != nil {
log.Println(err)
}
- Table Locking (only effective inside RunInTransaction)
// Example
merchantKey := datastore.IDKey("Merchant", "mjfFgYnxBS", nil)
userKey := datastore.IDKey("User", int64(4645436182170916864), nil)
if err := db.RunInTransaction(func(txn *goloquent.DB) error {
user := new(User)
if err := txn.NewQuery().
WLock(). // Lock record for update
Find(userKey, user); err != nil {
return err
}
merchant := new(Merchant)
if err := txn.NewQuery().
RLock(). // Lock record for read
Find(merchantKey, merchant); err != nil {
return err
}
user.Age = 30
if err := txn.Save(user); err != nil {
return err // return any err to rollback the transaction
}
return nil // return nil to commit the transaction
}); err != nil {
log.Println(err)
}
- Database Migration
import "github.com/Oskang09/goloquent/db"
// Example
user := new(User)
if err := db.Migrate(
new(user),
Merchant{},
&Log{},
); err != nil {
log.Println(err)
}
- Filter Query
import "github.com/Oskang09/goloquent/db"
// Update single record
user := new(User)
if err := db.NewQuery().
WhereIn("Status", []interface{}{"active", "pending"}).
First(user); err != nil {
log.Println(err) // error while retrieving record or record not found
}
// Get record with like
if err := db.NewQuery().
WhereLike("Name", "%name%").
First(user); err != nil {
log.Println(err) // error while retrieving record or record not found
}
- Update Query
import "github.com/Oskang09/goloquent/db"
// Update multiple record
if err := db.Table("User").
Where("Age", ">", 10).
Update(map[string]interface{}{
"Name": "New Name",
"Email": "email@gmail.com",
"UpdatedDateTime": time.Now().UTC(),
}); err != nil {
log.Println(err) // error while retrieving record or record not found
}
if err := db.Table("User").
Omit("Name").
Where("Age", ">", 10).
Update(User{
Name: "New Name",
Email: "test@gmail.com",
UpdatedDateTime: time.Now().UTC(),
}); err != nil {
log.Println(err) // error while retrieving record or record not found
}
- JSON Filter
import "github.com/Oskang09/goloquent/db"
// JSON equal
users := new([]User)
postCode := uint32(63000)
if err := db.NewQuery().
WhereJSONEqual("Address>PostCode", &postCode).
Get(ctx, users); err != nil {
log.Println(err)
}
// JSON not equal
var timeZone *time.Time
if err := db.NewQuery().
WhereJSONNotEqual("Address>region.TimeZone", timeZone).
Get(ctx, users); err != nil {
log.Println(err)
}
// JSON contains any
if err := db.NewQuery().
WhereJSONContainAny("Nicknames", []string{
"Joe", "John", "Robert",
}).Get(ctx, users); err != nil {
log.Println(err)
}
// JSON check type
if err := db.NewQuery().
WhereJSONType("Address>region", "Object").
Get(ctx, users); err != nil {
log.Println(err)
}
// JSON check is object type
if err := db.NewQuery().
WhereJSONIsObject("Address>region").
Get(ctx, users); err != nil {
log.Println(err)
}
// JSON check is array type
if err := db.NewQuery().
WhereJSONIsArray("Address>region.keys").
Get(ctx, users); err != nil {
log.Println(err)
}
- Data Type Support for Where Filtering
The supported data type are :
- string
- bool
- int, int8, int16, int32 and int64 (signed integers)
- uint, uint8, uint16, uint32 and uint64
- float32 and float64
- []byte
- datastore.GeoPoint
- goloquent.Date
- json.RawMessage
- time.Time
- pointers to any one of the above
- *datastore.Key
- slices of any of the above
- Extra Schema Option
Available shorthand:
- longtext (only applicable for
string
data type) - index
- unsigned (only applicable for
float32
andfloat64
data type) - flatten (only applicable for struct or []struct)
type model struct {
CreatedDateTime time.Time // `CreatedDateTime`
UpdatedDateTime time.Time // `UpdatedDateTime`
}
// Fields may have a `goloquent:"name,options"` tag.
type User struct {
Key *datastore.Key `goloquent:"__key__"` // Primary Key
Name string `goloquent:",longtext"` // Using `TEXT` datatype instead of `VARCHAR(255)` by default
CreditLimit float64 `goloquent:",unsigned"` // Unsigned option only applicable for float32 & float64 data type
PhoneNumber string `goloquent:",charset=utf8,collate=utf8_bin,datatype=char(20)"`
Email string
Skip string `goloquent:"-"` // Skip this field to store in db
DefaultAddress struct {
AddressLine1 string // `DefaultAddress.AddressLine1`
AddressLine2 string // `DefaultAddress.AddressLine2`
PostCode int // `DefaultAddress.PostCode`
City string // `DefaultAddress.City`
State string // `DefaultAddress.State`
Country string
} `goloquent:",flatten"` // Flatten the struct field
Birthdate *goloquent.Date
ExtraInfo json.RawMessage
model // Embedded struct
Deleted goloquent.SoftDelete
}
The supported data type are :
- string
- int, int8, int16, int32 and int64 (signed integers)
- uint, uint8, uint16, uint32 and uint64
- bool
- float32 and float64
- []byte
- any type whose underlying type is one of the above predeclared types
- datastore.GeoPoint
- goloquent.Date
- goloquent.SoftDelete
- time.Time
- json.RawMessage
- structs whose fields are all valid value types
- pointers to any one of the above
- *datastore.Key
- slices of any of the above
Data Type | Mysql | Postgres | Default Value | CharSet |
---|---|---|---|---|
*datastore.Key | varchar(512) | varchar(512) | latin1 | |
datastore.GeoPoint | varchar(50) | varchar(50) | {Lat: 0, Lng: 0} | |
string | varchar(191) | varchar(191) | "" | utf8mb4 |
[]byte | mediumblob | bytea | ||
bool | boolean | bool | false | |
float32 | double | real | 0 | |
float64 | double | real | 0 | |
int | int | integer | 0 | |
int8 | tinyint | smallint | 0 | |
int16 | smallint | smallint | 0 | |
int32 | mediumint | integer | 0 | |
int64 | bigint | bigint | 0 | |
uint | int (unsigned) | integer (unsigned) | 0 | |
uint8 | smallint (unsigned) | smallint (unsigned) | 0 | |
uint16 | smallint (unsigned) | smallint (unsigned) | 0 | |
uint32 | smallint (unsigned) | integer (unsigned) | 0 | |
uint64 | bigint (unsigned) | bigint (unsigned) | 0 | |
slice or array | json | jsonb | ||
struct | json | jsonb | ||
json.RawMessage | json | jsonb | ||
Date | date | date | 0001-01-01 | |
time.Time | datetime | timestamp | 0001-01-01 00:00:00 | |
SoftDelete | datetime (nullable) | timestamp | NULL |
$Key, $Deleted are reserved words, please avoid to use these words as your column name
Documentation
¶
Index ¶
- Constants
- Variables
- func LoadStruct(src interface{}, data map[string]interface{}) error
- func ParseKey(str string) (*datastore.Key, error)
- func RegisterDialect(driver string, d Dialect)
- func SaveStruct(src interface{}) (map[string]Property, error)
- func SetPKSimple(flag bool)
- func StringKey(key *datastore.Key) string
- func StringifyKey(key *datastore.Key) string
- type CharSet
- type Client
- func (c Client) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (c Client) PrepareExec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (c Client) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (c Client) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- type Column
- type Config
- type Cursor
- type DB
- func (db *DB) Ancestor(ancestor *datastore.Key) *Query
- func (db *DB) AnyOfAncestor(ancestors ...*datastore.Key) *Query
- func (db *DB) Close() error
- func (db *DB) Create(ctx context.Context, model interface{}, parentKey ...*datastore.Key) error
- func (db *DB) Delete(ctx context.Context, model interface{}) error
- func (db *DB) Destroy(ctx context.Context, model interface{}) error
- func (db *DB) Exec(ctx context.Context, stmt string, args ...interface{}) (sql.Result, error)
- func (db *DB) Find(ctx context.Context, key *datastore.Key, model interface{}) error
- func (db *DB) First(ctx context.Context, model interface{}) error
- func (db *DB) Get(ctx context.Context, model interface{}) error
- func (db DB) ID() string
- func (db *DB) MatchAgainst(fields []string, value ...string) *Query
- func (db *DB) Migrate(ctx context.Context, model ...interface{}) error
- func (db DB) Name() string
- func (db *DB) NewQuery() *Query
- func (db *DB) Omit(fields ...string) Replacer
- func (db *DB) Paginate(ctx context.Context, p *Pagination, model interface{}) error
- func (db *DB) Query(ctx context.Context, stmt string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) RunInTransaction(cb TransactionHandler) error
- func (db *DB) Save(ctx context.Context, model interface{}) error
- func (db *DB) Select(fields ...string) *Query
- func (db *DB) Table(name string) *Table
- func (db *DB) Truncate(ctx context.Context, model ...interface{}) error
- func (db *DB) Upsert(ctx context.Context, model interface{}, parentKey ...*datastore.Key) error
- func (db *DB) Where(field string, operator string, value interface{}) *Query
- type Date
- type DefaultEncoder
- type DefaultStmtEncoder
- type Dialect
- type Filter
- type Iterator
- type JSON
- type Loader
- type LogHandler
- type NativeHandler
- type OmitDefault
- type Pagination
- type Property
- type Query
- func (q *Query) Ancestor(ancestor *datastore.Key) *Query
- func (q *Query) AnyOfAncestor(ancestors ...*datastore.Key) *Query
- func (q *Query) DistinctOn(fields ...string) *Query
- func (q *Query) Find(ctx context.Context, key *datastore.Key, model interface{}) error
- func (q *Query) First(ctx context.Context, model interface{}) error
- func (q *Query) Flush(ctx context.Context) error
- func (q *Query) Get(ctx context.Context, model interface{}) error
- func (q *Query) InsertInto(ctx context.Context, table string) error
- func (q *Query) Limit(limit int) *Query
- func (q *Query) Lock(mode locked) *Query
- func (q *Query) MatchAgainst(fields []string, values ...string) *Query
- func (q *Query) Offset(offset int) *Query
- func (q *Query) Omit(fields ...string) *Query
- func (q *Query) OrderBy(values ...interface{}) *Query
- func (q *Query) Paginate(ctx context.Context, p *Pagination, model interface{}) error
- func (q *Query) RLock() *Query
- func (q *Query) ReplaceInto(ctx context.Context, table string) error
- func (q *Query) Scan(ctx context.Context, dest ...interface{}) error
- func (q *Query) Select(fields ...string) *Query
- func (q *Query) Unscoped() *Query
- func (q *Query) Update(ctx context.Context, v interface{}) error
- func (q *Query) WLock() *Query
- func (q *Query) Where(field string, op string, value interface{}) *Query
- func (q *Query) WhereAnyLike(field string, v interface{}) *Query
- func (q *Query) WhereEqual(field string, v interface{}) *Query
- func (q *Query) WhereIn(field string, v interface{}) *Query
- func (q *Query) WhereJSON(field, op string, v interface{}) *Query
- func (q *Query) WhereJSONContainAny(field string, v interface{}) *Query
- func (q *Query) WhereJSONEqual(field string, v interface{}) *Query
- func (q *Query) WhereJSONIn(field string, v []interface{}) *Query
- func (q *Query) WhereJSONIsArray(field string) *Query
- func (q *Query) WhereJSONIsObject(field string) *Query
- func (q *Query) WhereJSONNotEqual(field string, v interface{}) *Query
- func (q *Query) WhereJSONNotIn(field string, v []interface{}) *Query
- func (q *Query) WhereJSONType(field, typ string) *Query
- func (q *Query) WhereLike(field, v string) *Query
- func (q *Query) WhereNotEqual(field string, v interface{}) *Query
- func (q *Query) WhereNotIn(field string, v interface{}) *Query
- func (q *Query) WhereNotLike(field, v string) *Query
- func (q *Query) WhereNotNull(field string) *Query
- func (q *Query) WhereNull(field string) *Query
- type QueryBuilder
- type Registry
- type Replacer
- type Saver
- type Schema
- type SoftDelete
- type Stmt
- type StmtRegistry
- type StructCodec
- type Table
- func (t *Table) AddIndex(ctx context.Context, fields ...string) error
- func (t *Table) AddUniqueIndex(ctx context.Context, fields ...string) error
- func (t *Table) Ancestor(ancestor *datastore.Key) *Query
- func (t *Table) AnyOfAncestor(ancestors ...*datastore.Key) *Query
- func (t *Table) Create(ctx context.Context, model interface{}, parentKey ...*datastore.Key) error
- func (t *Table) DistinctOn(fields ...string) *Query
- func (t *Table) DropIfExists(ctx context.Context) error
- func (t *Table) Exists(ctx context.Context) bool
- func (t *Table) Find(ctx context.Context, key *datastore.Key, model interface{}) error
- func (t *Table) First(ctx context.Context, model interface{}) error
- func (t *Table) Get(ctx context.Context, model interface{}) error
- func (t *Table) InsertInto(ctx context.Context, table string) error
- func (t *Table) Limit(limit int) *Query
- func (t *Table) Lock(mode locked) *Query
- func (t *Table) Migrate(ctx context.Context, model interface{}) error
- func (t *Table) Offset(offset int) *Query
- func (t *Table) Omit(fields ...string) *Query
- func (t *Table) OrderBy(fields ...interface{}) *Query
- func (t *Table) Paginate(ctx context.Context, p *Pagination, model interface{}) error
- func (t *Table) RLock() *Query
- func (t *Table) ReplaceInto(ctx context.Context, table string) error
- func (t *Table) Save(ctx context.Context, model interface{}) error
- func (t *Table) Scan(ctx context.Context, dest ...interface{}) error
- func (t *Table) Select(fields ...string) *Query
- func (t *Table) Truncate(ctx context.Context) error
- func (t *Table) Unscoped() *Query
- func (t *Table) Update(ctx context.Context, v interface{}) error
- func (t *Table) Upsert(ctx context.Context, model interface{}, parentKey ...*datastore.Key) error
- func (t *Table) WLock() *Query
- func (t *Table) Where(field, op string, value interface{}) *Query
- func (t *Table) WhereEqual(field string, v interface{}) *Query
- func (t *Table) WhereIn(field string, v []interface{}) *Query
- func (t *Table) WhereJSONEqual(field string, v interface{}) *Query
- func (t *Table) WhereLike(field, v string) *Query
- func (t *Table) WhereNotEqual(field string, v interface{}) *Query
- func (t *Table) WhereNotIn(field string, v []interface{}) *Query
- func (t *Table) WhereNotLike(field, v string) *Query
- func (t *Table) WhereNotNull(field string) *Query
- func (t *Table) WhereNull(field string) *Query
- type TransactionHandler
- type Writer
Constants ¶
const ( Equal operator = iota EqualTo NotEqual LessThan LessEqual GreaterThan GreaterEqual AnyLike Like NotLike ContainAny ContainAll In NotIn IsObject IsArray IsType MatchAgainst )
JSON :
const ( ReadLock locked = iota + 1 WriteLock )
lock mode
Variables ¶
var ( ErrNoSuchEntity = fmt.Errorf("goloquent: entity not found") ErrInvalidCursor = fmt.Errorf("goloquent: invalid cursor") )
CommonError :
Functions ¶
Types ¶
type Client ¶
type Client struct { CharSet // contains filtered or unexported fields }
Client :
func (Client) PrepareExec ¶
func (c Client) PrepareExec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PrepareExec :
type Config ¶
type Config struct { Username string Password string Host string Port string Database string UnixSocket string TLSConfig string CharSet *CharSet Logger LogHandler }
Config :
type Cursor ¶
type Cursor struct { Signature string `json:"signature"` Key *datastore.Key `json:"next"` // contains filtered or unexported fields }
Cursor :
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB :
func NewDB ¶
func NewDB(ctx context.Context, driver string, charset CharSet, conn sqlCommon, dialect Dialect, logHandler LogHandler) *DB
NewDB :
func (*DB) AnyOfAncestor ¶
AnyOfAncestor :
func (*DB) MatchAgainst ¶
Where :
func (*DB) Paginate ¶
func (db *DB) Paginate(ctx context.Context, p *Pagination, model interface{}) error
Paginate :
func (*DB) RunInTransaction ¶
func (db *DB) RunInTransaction(cb TransactionHandler) error
RunInTransaction :
type DefaultEncoder ¶
type DefaultEncoder struct {
// contains filtered or unexported fields
}
type DefaultStmtEncoder ¶
type DefaultStmtEncoder struct {
// contains filtered or unexported fields
}
type Dialect ¶
type Dialect interface { Open(c Config) (*sql.DB, error) SetDB(db Client) GetTable(ns string) string Version(ctx context.Context) (ver string) CurrentDB(ctx context.Context) (n string) Quote(n string) string Bind(i uint) string FilterJSON(f Filter) (s string, args []interface{}, err error) JSONMarshal(i interface{}) (b json.RawMessage) Value(v interface{}) string GetSchema(c Column) []Schema DataType(s Schema) string HasTable(ctx context.Context, tb string) bool HasIndex(ctx context.Context, tb, idx string) bool GetColumns(ctx context.Context, tb string) (cols []string) GetIndexes(ctx context.Context, tb string) (idxs []string) CreateTable(ctx context.Context, tb string, cols []Column) error AlterTable(ctx context.Context, tb string, cols []Column, unsafe bool) error OnConflictUpdate(tb string, cols []string) string UpdateWithLimit() bool ReplaceInto(ctx context.Context, src, dst string) error }
Dialect :
type Pagination ¶
Pagination :
func (*Pagination) Count ¶
func (p *Pagination) Count() uint
Count : record count in this pagination record set
func (*Pagination) NextCursor ¶
func (p *Pagination) NextCursor() string
NextCursor : next record set cursor
func (*Pagination) Reset ¶
func (p *Pagination) Reset()
Reset : reset all the value in pagination to default value
type Property ¶
type Property struct { Value interface{} // contains filtered or unexported fields }
Property :
type Query ¶
type Query struct {
// contains filtered or unexported fields
}
Query :
func (*Query) AnyOfAncestor ¶
AnyOfAncestor :
func (*Query) InsertInto ¶
InsertInto :
func (*Query) MatchAgainst ¶
MatchAgainst :
func (*Query) Paginate ¶
func (q *Query) Paginate(ctx context.Context, p *Pagination, model interface{}) error
Paginate :
func (*Query) ReplaceInto ¶
ReplaceInto :
func (*Query) WhereAnyLike ¶
WhereAnyLike :
func (*Query) WhereEqual ¶
WhereEqual :
func (*Query) WhereJSONContainAny ¶
WhereJSONContainAny :
func (*Query) WhereJSONEqual ¶
WhereJSONEqual :
func (*Query) WhereJSONIn ¶
WhereJSONIn :
func (*Query) WhereJSONIsArray ¶
WhereJSONIsArray :
func (*Query) WhereJSONIsObject ¶
WhereJSONIsObject :
func (*Query) WhereJSONNotEqual ¶
WhereJSONNotEqual :
func (*Query) WhereJSONNotIn ¶
WhereJSONNotIn :
func (*Query) WhereJSONType ¶
WhereJSONType :
func (*Query) WhereNotEqual ¶
WhereNotEqual :
func (*Query) WhereNotIn ¶
WhereNotIn :
type Registry ¶
func NewRegistry ¶
func NewRegistry() *Registry
func (*Registry) SetDefaultEncoders ¶
func (r *Registry) SetDefaultEncoders()
func (*Registry) SetKindEncoder ¶
func (*Registry) SetTypeEncoder ¶
type Replacer ¶
type Replacer interface { Upsert(ctx context.Context, model interface{}, k ...*datastore.Key) error Save(ctx context.Context, model interface{}) error }
Replacer :
type Schema ¶
type Schema struct { Name string DataType string DefaultValue interface{} IsUnsigned bool IsNullable bool IsIndexed bool CharSet }
Schema :
type StmtRegistry ¶
func NewStmtRegistry ¶
func NewStmtRegistry() *StmtRegistry
func (*StmtRegistry) BuildStatement ¶
func (r *StmtRegistry) BuildStatement(w Writer, v reflect.Value) ([]interface{}, error)
func (*StmtRegistry) SetDefaultEncoders ¶
func (r *StmtRegistry) SetDefaultEncoders()
func (*StmtRegistry) SetKindEncoder ¶
func (r *StmtRegistry) SetKindEncoder(k reflect.Kind, f writerFunc)
func (*StmtRegistry) SetTypeEncoder ¶
func (r *StmtRegistry) SetTypeEncoder(t reflect.Type, f writerFunc)
type StructCodec ¶
type StructCodec struct {
// contains filtered or unexported fields
}
StructCodec :
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table :
func (*Table) AddUniqueIndex ¶
AddUniqueIndex :
func (*Table) AnyOfAncestor ¶
AnyOfAncestor :
func (*Table) InsertInto ¶
InsertInto :
func (*Table) Paginate ¶
func (t *Table) Paginate(ctx context.Context, p *Pagination, model interface{}) error
Paginate :
func (*Table) ReplaceInto ¶
ReplaceInto :
func (*Table) WhereEqual ¶
WhereEqual :
func (*Table) WhereJSONEqual ¶
WhereJSONEqual :
func (*Table) WhereNotEqual ¶
WhereNotEqual :
func (*Table) WhereNotIn ¶
WhereNotIn :
type Writer ¶
type Writer interface { io.Writer io.StringWriter io.ByteWriter }