Documentation ¶
Index ¶
- Constants
- func AllColumns(model any, exclude ...string) []string
- func CheckSQLError(err error, number int) bool
- func Columns(model any, exclude ...string) []string
- func ColumnsWithTypes(model any) ([]string, []string)
- func Exclude[T comparable](a []T, b []T) []T
- func GetColType(typ string, dialect string) string
- func GetColTypes(types []string, dialect string) []string
- func GetValue(model Model, column string) any
- func MapToSlice[T string | int | bool | int8 | int16 | int32 | int64](m map[T]any) ([]T, []any)
- func Scan(model Model, row *sql.Rows, exclude []string) error
- func SetValue(model Model, column string, value any)
- func TagValid(field reflect.StructField) bool
- func TagValues(field reflect.StructField) []string
- type Column
- type DBType
- type Database
- func (db *Database) Addr() string
- func (d *Database) AllModel(model Model, include []string) ModelSet
- func (db *Database) AllModels() []Model
- func (d *Database) AllQ(model Model, exclude []string) string
- func (db *Database) AlterDropOneToOne(from, to string) error
- func (db *Database) AlterOneToOne(from, to string) error
- func (db *Database) Begin() (*sql.Tx, error)
- func (db *Database) CheckError(err error, number int) bool
- func (db *Database) Close() error
- func (db *Database) Connect() error
- func (db *Database) Count(table_name string, filter ...Filter) (int, error)
- func (db *Database) CreateFKTable(from, to string) error
- func (d *Database) CreateTableQuery(table string, columns []string) string
- func (db *Database) DB_Columns(table_name string) ([]string, error)
- func (db *Database) DB_Columns_With_Type(table_name string) (map[string]string, error)
- func (d *Database) DB_Tables() ([]string, error)
- func (db *Database) DSN() string
- func (db *Database) DeleteFK(from, to Model) error
- func (d *Database) DeleteModel(model Model) error
- func (db *Database) DeleteOneToOne(from, to Model) error
- func (d *Database) DeleteQuery(table string, where string) string
- func (db *Database) DropFKTable(from, to string) error
- func (db *Database) DropTable(table_name string) error
- func (db *Database) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Database) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (d *Database) ExecCreateTable(table string, columns []string) error
- func (d *Database) ExecDelete(table string, where string) error
- func (d *Database) ExecInsert(table string, columns []string, values []interface{}) (int64, error)
- func (d *Database) ExecSelect(table string, where string) (*sql.Rows, error)
- func (d *Database) ExecUpdate(table string, columns []string, values []interface{}, where string, ...) (int64, error)
- func (d *Database) Filter(model Model, filter Filters, include []string) ModelSet
- func (d *Database) FilterWithLimit(model Model, filters Filters, limit int, include []string) ModelSet
- func (db *Database) GetColumnValue(model Model, col string, id any) interface{}
- func (db *Database) GetFromEnv() *Database
- func (db *Database) GetOneToOneReverse(from, to Model) (Model, error)
- func (db *Database) InsertFK(from, to Model) error
- func (d *Database) InsertModel(model Model) error
- func (db *Database) InsertOneToOne(from, to Model) error
- func (d *Database) InsertQuery(table string, columns []string) string
- func (db *Database) LoadCredentials() *Database
- func (db *Database) Migrate() error
- func (db *Database) NewQS(mdl Model) *QuerySet
- func (db *Database) Ping() error
- func (db *Database) Prepare(query string) (*sql.Stmt, error)
- func (db *Database) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (db *Database) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *Database) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (db *Database) QueryRow(query string, args ...interface{}) *sql.Row
- func (db *Database) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (d *Database) QuerySelect(table string, columns []string, where string) (*sql.Rows, error)
- func (d *Database) QuerySelectOne(table string, column string, where string) *sql.Row
- func (d *Database) QuerySelectRow(table string, columns []string, where string) *sql.Row
- func (db *Database) Register(model Model)
- func (db *Database) SelectFK(from, to Model) (ModelSet, error)
- func (db *Database) SelectFKReverse(from, to Model) (ModelSet, error)
- func (d *Database) SelectOneQuery(table string, column string, where string) string
- func (db *Database) SelectOneToOne(from, to Model) (Model, error)
- func (d *Database) SelectQuery(table string, columns []string, where string) string
- func (d *Database) SelectRowQuery(table string, columns []string, where string) string
- func (db *Database) String() string
- func (d *Database) UpdateModel(model Model) (Model, error)
- func (d *Database) UpdateQuery(table string, columns []string, where string) string
- type Filter
- type Filters
- type Migration
- type Model
- type ModelSet
- type ModelTags
- func (t ModelTags) Auto() bool
- func (t ModelTags) Default() string
- func (t ModelTags) Get(key string) string
- func (t ModelTags) Has(key string) bool
- func (t ModelTags) Index() bool
- func (t ModelTags) Length() int
- func (t ModelTags) Nullable() bool
- func (t ModelTags) Primary() bool
- func (t ModelTags) Raw() string
- func (t ModelTags) RelType() string
- func (t ModelTags) Set(key string, value string)
- func (t ModelTags) ToColumn(tname, name, typ string) Column
- func (t ModelTags) Type() string
- func (t ModelTags) Unique() bool
- type QuerySet
- func (q *QuerySet) Add(statement string) *QuerySet
- func (q *QuerySet) AddFiters(filters Filters) *QuerySet
- func (q *QuerySet) All() *QuerySet
- func (q *QuerySet) Clear() *QuerySet
- func (q *QuerySet) Count() *QuerySet
- func (q *QuerySet) Exec() (*sql.Rows, error)
- func (q *QuerySet) ExecOne() (*sql.Row, error)
- func (q *QuerySet) ExecRow() (*sql.Row, error)
- func (q *QuerySet) From(table ...string) *QuerySet
- func (q *QuerySet) Get(values ...int) *QuerySet
- func (q *QuerySet) GroupBy(columns ...string) *QuerySet
- func (q *QuerySet) Join(table string, column string, value any, op string) *QuerySet
- func (q *QuerySet) Limit(limit int) *QuerySet
- func (q *QuerySet) MultiModel(model ...Model) ModelSet
- func (q *QuerySet) Offset(offset int) *QuerySet
- func (q *QuerySet) OrderBy(column string, order string) *QuerySet
- func (q *QuerySet) Page(page int) ModelSet
- func (q *QuerySet) Query() (string, []interface{})
- func (q *QuerySet) Raw(sql string) (string, []any)
- func (q *QuerySet) Select(columns ...string) *QuerySet
- func (q *QuerySet) SingleModel(model ...Model) (Model, error)
- func (q *QuerySet) Where(column string, op string, value any) *QuerySet
- type Relation
- type Table
Constants ¶
const ( IN = "IN" EQ = "=" NE = "!=" GT = ">" GTE = ">=" LT = "<" LTE = "<=" LIKE = "LIKE" )
Equality operators for use in filters
const TAG = "simpledb"
Tag to get from model structs.
Variables ¶
This section is empty.
Functions ¶
func AllColumns ¶ added in v1.2.0
Get all of the model columns, including related fields. Optionally, you can specify a list of columns to exclude.
func CheckSQLError ¶ added in v1.0.8
Check for SQL errors
func Columns ¶
Get the model columns, excluding related fields. Optionally, you can specify a list of columns to exclude.
func ColumnsWithTypes ¶
Get the columns with golang types
func GetColType ¶
Get the column types for a specified golang type
func GetColTypes ¶
Get the column types for the model
func MapToSlice ¶
Convert a map to a slice of keys and a slice of values
func TagValid ¶
func TagValid(field reflect.StructField) bool
Verify if a field/tag is valid to be used.
func TagValues ¶
func TagValues(field reflect.StructField) []string
Split tags into lists of [Key:Value, Key:Value, ...] pairs.
Types ¶
type Column ¶
type Column struct { Table string Name string Default string Type DBType Raw string Length int Nullable bool Unique bool Primary bool Index bool Auto bool Tags ModelTags }
Representation of a column, used to create tables when migrating
func MigrationColumns ¶
Get the columns needed for a migration
type DBType ¶
type DBType string
const ( VARCHAR DBType = "VARCHAR" TEXT DBType = "TEXT" INT DBType = "INT" BOOLEAN DBType = "BOOLEAN" TINYINT DBType = "TINYINT" SMALLINT DBType = "SMALLINT" BIGINT DBType = "BIGINT" FLOAT DBType = "FLOAT" DOUBLE DBType = "DOUBLE" DATETIME DBType = "DATETIME" BLOB DBType = "BLOB" FOREIGN_KEY DBType = "FOREIGN KEY" )
Database types
type Database ¶
type Database struct { Host string Port any Username string Password string `json:"-"` Database string SSL_MODE string LIMIT int LatestMigration *Migration `json:"-"` Logger simplelog.Logger `json:"-"` // contains filtered or unexported fields }
Database is the main struct for the database
func (*Database) AlterDropOneToOne ¶
func (*Database) AlterOneToOne ¶
func (*Database) CheckError ¶ added in v1.0.8
Check for SQL errors
func (*Database) CreateFKTable ¶
func (*Database) CreateTableQuery ¶
Create a table from columns.
func (*Database) DB_Columns ¶
Get column information for a model.
func (*Database) DB_Columns_With_Type ¶
Get column information for a model. Also retrieves the column type.
func (*Database) DeleteModel ¶ added in v1.1.0
Delete a model from the database
func (*Database) DeleteOneToOne ¶ added in v1.1.9
func (*Database) DeleteQuery ¶
Delete a row from a table.
func (*Database) DropFKTable ¶
func (*Database) ExecContext ¶
func (db *Database) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec a query with context
func (*Database) ExecCreateTable ¶
Execute creating a table.
func (*Database) ExecDelete ¶
Execute deleting a row.
func (*Database) ExecInsert ¶
Execute inserting a row.
func (*Database) ExecSelect ¶
Execute selecting from a table.
func (*Database) ExecUpdate ¶
func (d *Database) ExecUpdate(table string, columns []string, values []interface{}, where string, conditional any) (int64, error)
Execute updating a row.
func (*Database) FilterWithLimit ¶
func (d *Database) FilterWithLimit(model Model, filters Filters, limit int, include []string) ModelSet
See Filter. Also allows you to specify a limit on the number of results returned.
func (*Database) GetColumnValue ¶
Get the column for a model.
func (*Database) GetFromEnv ¶
Get the credentials from the environment variables
func (*Database) GetOneToOneReverse ¶ added in v1.1.9
func (*Database) InsertModel ¶
Insert a model into the database. Takes a pointer to a model and a sql.Row and scans the ID of result into the model
func (*Database) InsertOneToOne ¶ added in v1.1.9
func (*Database) InsertQuery ¶
Insert a row into a table.
func (*Database) LoadCredentials ¶
Load the credentials from the .env file
func (*Database) PrepareContext ¶
Prepare a query with context
func (*Database) QueryContext ¶
func (db *Database) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query the database with context
func (*Database) QueryRowContext ¶
func (db *Database) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Query the database row with context
func (*Database) QuerySelect ¶
Execute selecting a row from a table.
func (*Database) QuerySelectOne ¶
Execute selecting one row from a table.
func (*Database) QuerySelectRow ¶
Execute selecting one row from a table.
func (*Database) Register ¶
Register a model with the database This will be used to create the table if it doesn't exist, and to create the migration if it doesn't exist
func (*Database) SelectFKReverse ¶
func (*Database) SelectOneQuery ¶
Select one row from a table.
func (*Database) SelectOneToOne ¶ added in v1.1.9
func (*Database) SelectQuery ¶
Select from a table.
func (*Database) SelectRowQuery ¶
Select a row from a table.
func (*Database) UpdateModel ¶ added in v1.0.5
Update a model in the database.
type Filters ¶
type Filters []*Filter
List of filters, handy for creating a query.
type Migration ¶
Migration represents a set of changes to the database
func (*Migration) CreateFromModels ¶
Create a migration from models.
func (Migration) GetLatestMigration ¶
Read the latest migration from the file system
type Model ¶
type Model interface {
TableName() string
}
Model interface
type ModelSet ¶
type ModelSet []Model
ModelSet is a client-side representation of a queryset. Allows limited filtering.
func (ModelSet) Filter ¶
Filter a modelset, returns a new modelset. Example:
qs.Filter("name=John,Paul") qs.Filter("age=18,19,20")
Better to use on smaller sets, less than 1000. Otherwise use the Database.Filter() method.
type ModelTags ¶
ModelTags is a map of tags for a model.
func TagMap ¶
func TagMap(field reflect.StructField) ModelTags
Generate a map from the list of [Key:Value, Key:Value, ...] pairs.
func (ModelTags) Default ¶
Default is a special tag that indicates that the column has a default value. This default value is however very limited, and can only be set in the model tags.
func (ModelTags) Raw ¶
Used for annotating your type tags with raw sql. Example:
type User struct { ID int `simpledb:"RAW: NOT NULL AUTO_INCREMENT PRIMARY"` }
type QuerySet ¶
type QuerySet struct { Statements []string Filters Filters Q string Model Model OFFSET int LIMIT int PAGESIZE int // contains filtered or unexported fields }
Queryset is a struct that handles generating SQL queries
func (*QuerySet) MultiModel ¶
Execute the query and return the results as a ModelSet
func (*QuerySet) Raw ¶
Enter raw SQL into the end of the query Returns the sql query, and the values to be passed to the database
func (*QuerySet) SingleModel ¶
Execute the query and return the results as a Model
type Relation ¶
Represents a relation between two tables.
func MigrationRelations ¶
Get the related fields for migrating a model.
type Table ¶
Table representation, used to create tables when migrating
func ModelToTable ¶
Convert a model to a table. Used for migrations