simpledb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: GPL-2.0 Imports: 15 Imported by: 0

README

This is simpledb!

Installation

Easily install simpledb with the following command:

go get simpledb

Usage

Contributing

Documentation

Index

Constants

View Source
const (
	IN   = "IN"
	EQ   = "="
	NE   = "!="
	GT   = ">"
	GTE  = ">="
	LT   = "<"
	LTE  = "<="
	LIKE = "LIKE"
)

Equality operators for use in filters

View Source
const TAG = "simpledb"

Variables

This section is empty.

Functions

func Columns

func Columns(model any) []string

Get the model columns

func ColumnsWithTypes

func ColumnsWithTypes(model any) ([]string, []string)

Get the columns with golang types

func Exclude

func Exclude[T comparable](a []T, b []T) []T

Exclude an item from a slice.

func GetColType

func GetColType(typ string, dialect string) string

Get the column types for a specified golang type

func GetColTypes

func GetColTypes(types []string, dialect string) []string

Get the column types for the model

func GetValue

func GetValue(model Model, column string) any

Get a value from a model struct

func MapToSlice

func MapToSlice[T string | int | bool | int8 | int16 | int32 | int64](m map[T]any) ([]T, []any)

Convert a map to a slice of keys and a slice of values

func Scan

func Scan(model Model, row *sql.Rows) error

Scan a row into a model

func SetValue

func SetValue(model Model, column string, value any)

Set a value on a model struct

func TagValid

func TagValid(field reflect.StructField) bool

func TagValues

func TagValues(field reflect.StructField) []string

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

func MigrationColumns(model Model) []Column

Get the columns needed for a migration

func (Column) String

func (c Column) String() string

Generate a query for the column

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 NewDatabase

func NewDatabase(loglevel ...string) *Database

Connect to the database

func (*Database) Addr

func (db *Database) Addr() string

Get the address of the database

func (*Database) AllModel

func (d *Database) AllModel(model Model, exclude []string) ModelSet

func (*Database) AllQ

func (d *Database) AllQ(model Model, exclude []string) string

func (*Database) AlterDropOneToMany

func (db *Database) AlterDropOneToMany(from, to string) error

func (*Database) AlterDropOneToOne

func (db *Database) AlterDropOneToOne(from, to string) error

func (*Database) AlterOneToMany

func (db *Database) AlterOneToMany(from, to string) error

func (*Database) AlterOneToOne

func (db *Database) AlterOneToOne(from, to string) error

func (*Database) Begin

func (db *Database) Begin() (*sql.Tx, error)

Begin a transaction

func (*Database) Close

func (db *Database) Close() error

Close the database connection

func (*Database) Connect

func (db *Database) Connect() error

Connect to the database

func (*Database) Count

func (db *Database) Count(table_name string, filter ...Filter) (int, error)

func (*Database) CreateFKTable

func (db *Database) CreateFKTable(from, to string) error

func (*Database) CreateTableQuery

func (d *Database) CreateTableQuery(table string, columns []string) string

func (*Database) DB_ColumnType

func (db *Database) DB_ColumnType(table_name string, column_name string) ([]*sql.ColumnType, error)

func (*Database) DB_ColumnTypes

func (db *Database) DB_ColumnTypes(table_name string) ([]*sql.ColumnType, error)

func (*Database) DB_Columns

func (db *Database) DB_Columns(table_name string) ([]string, error)

func (*Database) DB_Columns_With_Type

func (db *Database) DB_Columns_With_Type(table_name string) (map[string]string, error)

func (*Database) DB_Tables

func (d *Database) DB_Tables() ([]string, error)

func (*Database) DSN

func (db *Database) DSN() string

Get the DSN for the database

func (*Database) DeleteFK

func (db *Database) DeleteFK(from, to Model) error

func (*Database) DeleteQuery

func (d *Database) DeleteQuery(table string, where string) string

func (*Database) DropFKTable

func (db *Database) DropFKTable(from, to string) error

func (*Database) DropTable

func (db *Database) DropTable(table_name string) error

func (*Database) Exec

func (db *Database) Exec(query string, args ...interface{}) (sql.Result, error)

Exec a query

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

func (d *Database) ExecCreateTable(table string, columns []string) error

func (*Database) ExecDelete

func (d *Database) ExecDelete(table string, where string) error

func (*Database) ExecInsert

func (d *Database) ExecInsert(table string, columns []string, values []interface{}) (int64, error)

func (*Database) ExecSelect

func (d *Database) ExecSelect(table string, where string) (*sql.Rows, error)

func (*Database) ExecUpdate

func (d *Database) ExecUpdate(table string, columns []string, values []interface{}, where string) error

func (*Database) Filter

func (d *Database) Filter(model Model, filter Filters) ModelSet

func (*Database) FilterWithLimit

func (d *Database) FilterWithLimit(model Model, filters Filters, limit int) ModelSet

func (*Database) GetColumnValue

func (db *Database) GetColumnValue(model Model, col string, id any) interface{}

func (*Database) GetFromEnv

func (db *Database) GetFromEnv() *Database

Get the credentials from the environment variables

func (*Database) GetOneToMany

func (db *Database) GetOneToMany(from, to Model) (ModelSet, error)

func (*Database) GetOneToOne

func (db *Database) GetOneToOne(from, to Model) (ModelSet, error)

func (*Database) InsertFK

func (db *Database) InsertFK(from, to Model) error

func (*Database) InsertModel

func (d *Database) InsertModel(model Model) error

Takes a pointer to a model and a sql.Row and scans the ID of result into the model

func (*Database) InsertQuery

func (d *Database) InsertQuery(table string, columns []string) string

func (*Database) LoadCredentials

func (db *Database) LoadCredentials() *Database

Load the credentials from the .env file

func (*Database) Migrate

func (db *Database) Migrate() error

Migrate the database to the latest version

func (*Database) NewQS

func (db *Database) NewQS(mdl Model) *QuerySet

Shorthand for a queryset

func (*Database) Ping

func (db *Database) Ping() error

Ping the database

func (*Database) Prepare

func (db *Database) Prepare(query string) (*sql.Stmt, error)

Prepare a query

func (*Database) PrepareContext

func (db *Database) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

Prepare a query with context

func (*Database) Query

func (db *Database) Query(query string, args ...interface{}) (*sql.Rows, error)

Query the database

func (*Database) QueryContext

func (db *Database) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query the database with context

func (*Database) QueryRow

func (db *Database) QueryRow(query string, args ...interface{}) *sql.Row

Query a database row

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

func (d *Database) QuerySelect(table string, columns []string, where string) (*sql.Rows, error)

func (*Database) QuerySelectOne

func (d *Database) QuerySelectOne(table string, column string, where string) *sql.Row

func (*Database) QuerySelectRow

func (d *Database) QuerySelectRow(table string, columns []string, where string) *sql.Row

func (*Database) Register

func (db *Database) Register(model Model)

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

func (db *Database) SelectFK(from, to Model) (ModelSet, error)

func (*Database) SelectFKReverse

func (db *Database) SelectFKReverse(from, to Model) (ModelSet, error)

func (*Database) SelectOneQuery

func (d *Database) SelectOneQuery(table string, column string, where string) string

func (*Database) SelectQuery

func (d *Database) SelectQuery(table string, columns []string, where string) string

func (*Database) SelectRowQuery

func (d *Database) SelectRowQuery(table string, columns []string, where string) string

func (*Database) String

func (db *Database) String() string

Database string method

func (*Database) UpdateQuery

func (d *Database) UpdateQuery(table string, columns []string, where string) string

type Filter

type Filter struct {
	Column   string
	Value    any
	Operator string
}

Generic filter for a queryset.

type Filters

type Filters []*Filter

List of filters, handy for creating a query.

func (Filters) Add

func (f Filters) Add(column string, op string, value any) Filters

Add a filter to the list of filters.

func (Filters) Get

func (f Filters) Get(column string) any

Get a filter from the list of filters.

func (Filters) Has

func (f Filters) Has(column string) bool

Check if a filter exists in the list of filters.

func (Filters) Len

func (f Filters) Len() int

func (Filters) Query

func (f Filters) Query(and bool) (string, []interface{})

Returns a string with the query and a list of values to be used in the query.

func (Filters) Remove

func (f Filters) Remove(column string) Filters

Remove a filter from the list of filters.

type Migration

type Migration struct {
	Database  *Database
	Tables    []Table
	Models    []Model
	Directory string
}

Migration represents a set of changes to the database

func NewMigration

func NewMigration(db *Database) *Migration

Initialize a new migration

func (*Migration) CreateFromModels

func (m *Migration) CreateFromModels(models []Model)

Create a migration from models.

func (Migration) GetLatestMigration

func (m Migration) GetLatestMigration() (Migration, error)

Read the latest migration from the file system

func (Migration) Run

func (m Migration) Run() error

Execute a migration

func (Migration) Validate

func (m Migration) Validate(other Migration) ([]Table, []Column, []Relation, []Column, []Table, []Column, []Relation)

Validate a migration This is used to make sure all fields are the same, if not, we can ALTER the fields on the database side.

func (Migration) Write

func (m Migration) Write() error

Write a migration to the file system

type Model

type Model interface {
	TableName() string
}

Model interface

func NewModel

func NewModel(model Model) Model

Get a new model struct from an existing model struct

func ScanRow

func ScanRow(row *sql.Row, model Model) (Model, error)

type ModelSet

type ModelSet []Model

func ScanRows

func ScanRows(rows *sql.Rows, model Model) ModelSet

func (ModelSet) Filter

func (ms ModelSet) Filter(args []string) ModelSet

Better to use on smaller sets, less than 1000. Otherwise use the Database.Filter() method.

func (ModelSet) Len

func (ms ModelSet) Len() int

func (ModelSet) String

func (ms ModelSet) String() string

func (ModelSet) Values

func (ms ModelSet) Values(exclude ...string) []map[string]any

type ModelTags

type ModelTags map[string]string

func TagMap

func TagMap(field reflect.StructField) ModelTags

func (ModelTags) Auto

func (t ModelTags) Auto() bool

func (ModelTags) Default

func (t ModelTags) Default() string

func (ModelTags) Get

func (t ModelTags) Get(key string) string

func (ModelTags) Has

func (t ModelTags) Has(key string) bool

func (ModelTags) Index

func (t ModelTags) Index() bool

func (ModelTags) Length

func (t ModelTags) Length() int

func (ModelTags) Nullable

func (t ModelTags) Nullable() bool

func (ModelTags) Primary

func (t ModelTags) Primary() bool

func (ModelTags) Raw

func (t ModelTags) Raw() string

func (ModelTags) RelType

func (t ModelTags) RelType() string

func (ModelTags) Set

func (t ModelTags) Set(key string, value string)

func (ModelTags) ToColumn

func (t ModelTags) ToColumn(tname, name, typ string) Column

func (ModelTags) Type

func (t ModelTags) Type() string

func (ModelTags) Unique

func (t ModelTags) Unique() bool

type QuerySet

type QuerySet struct {
	Statements []string
	Filters    Filters
	Q          string

	Model    Model
	OFFSET   int
	LIMIT    int
	PAGESIZE int
	// contains filtered or unexported fields
}

func NewQuerySet

func NewQuerySet(db *Database, model ...Model) *QuerySet

func (*QuerySet) Add

func (q *QuerySet) Add(statement string) *QuerySet

func (*QuerySet) AddFiters

func (q *QuerySet) AddFiters(filters Filters) *QuerySet

func (*QuerySet) All

func (q *QuerySet) All() *QuerySet

func (*QuerySet) Clear

func (q *QuerySet) Clear() *QuerySet

func (*QuerySet) Count

func (q *QuerySet) Count() *QuerySet

func (*QuerySet) Exec

func (q *QuerySet) Exec() (*sql.Rows, error)

func (*QuerySet) ExecOne

func (q *QuerySet) ExecOne() (*sql.Row, error)

func (*QuerySet) ExecRow

func (q *QuerySet) ExecRow() (*sql.Row, error)

func (*QuerySet) From

func (q *QuerySet) From(table ...string) *QuerySet

func (*QuerySet) Get

func (q *QuerySet) Get(values ...int) *QuerySet

Get a single model from the database

func (*QuerySet) GetFrom

func (q *QuerySet) GetFrom(from, where, op string, value any) *QuerySet

func (*QuerySet) GroupBy

func (q *QuerySet) GroupBy(columns ...string) *QuerySet

func (*QuerySet) Join

func (q *QuerySet) Join(table string, column string, value any, op string) *QuerySet

func (*QuerySet) Limit

func (q *QuerySet) Limit(limit int) *QuerySet

func (*QuerySet) MultiModel

func (q *QuerySet) MultiModel(model ...Model) ModelSet

func (*QuerySet) Offset

func (q *QuerySet) Offset(offset int) *QuerySet

func (*QuerySet) OrderBy

func (q *QuerySet) OrderBy(column string, order string) *QuerySet

func (*QuerySet) Page

func (q *QuerySet) Page(page int) ModelSet

func (*QuerySet) Query

func (q *QuerySet) Query() (string, []interface{})

func (*QuerySet) Raw

func (q *QuerySet) Raw(sql string) (string, []any)

func (*QuerySet) Select

func (q *QuerySet) Select(columns ...string) *QuerySet

func (*QuerySet) SingleModel

func (q *QuerySet) SingleModel(model ...Model) (Model, error)

func (*QuerySet) Where

func (q *QuerySet) Where(column string, op string, value any) *QuerySet

type Relation

type Relation struct {
	From string
	To   string
	Type DBType
}

Represents a relation between two tables.

func MigrationRelations

func MigrationRelations(model Model) []Relation

Get the related fields for migrating a model.

type Table

type Table struct {
	Name      string
	Columns   []Column
	Relations []Relation
}

Table representation, used to create tables when migrating

func ModelToTable

func ModelToTable(model Model) Table

Convert a model to a table. Used for migrations

func (Table) String

func (t Table) String() string

Generate a query for the table

Jump to

Keyboard shortcuts

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