db

package
v0.0.0-...-a39d757 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusMigrated    = -1
	StatusNotMigrated = 1
)

Migration statuses @see Filter.Status

Variables

This section is empty.

Functions

func FillRelation

func FillRelation(rValF reflect.Value, relName string)

func Scan

func Scan(rows *sql.Rows, col schema.Collection, query *schema.Query) error

Scan function takes result set from the database (rows) and maps all fields to the passed Model Collection

Types

type Adapter

type Adapter interface {

	// For default behaviour, see DefaultAdapter.Open
	Open(string) error

	// For default behaviour, see DefaultAdapter.Exec
	Exec(string, ...any) error

	// For default behaviour, see DefaultAdapter.Rows
	Rows(string, ...any) error

	// ScanRow takes the first arg as object
	// and tries to scan Row values from database source into it.
	ScanModel(schema.Model, *schema.Query) (mOut schema.Model, err error)

	// ScanRows takes the first arg as objects
	// and tries to scan Rows from database source into it
	// based on the Query we passed
	// E.g.
	// var menuCol MenuCollection
	// ScanRows(menuCol, NewQuery("menu", menuCol.BaseFIelds()))
	ScanCollection(schema.Collection, *schema.Query) error

	// ScanRelations looks for any given relations set in Query,
	// and queries the DB fro those and than populates into the Collection
	ScanRelations(*schema.Schema, schema.Collection, *schema.Query) error

	// Values TODO Generics (to replace "string")!
	Values(string, ...any) ([]string, error)

	// Type is one of DBAdapterMySql, DBAdapterSqlite, etc
	Type() string

	// InitMigrations creates migrations table in DB
	InitMigrations(table string) error
}

func AdapterFactory

func AdapterFactory(name string) (Adapter, error)

AdapterFactory creates new Adapter implementation based on adapter name (mysql, sqlite, etc)

type BaseAdapter

type BaseAdapter struct {
	Name   string
	DB     *sql.DB
	Logger log.Logger
}

BaseAdapter

func NewBaseAdapter

func NewBaseAdapter(parent string) *BaseAdapter

func (*BaseAdapter) Exec

func (o *BaseAdapter) Exec(q string, bind ...any) (err error)

Exec implements Adatpter interface. It only executes sql query

func (*BaseAdapter) GetMigrationSQL

func (o *BaseAdapter) GetMigrationSQL() string

Type returns the type name of the adapter: "mysql", "sqlite", etc

func (*BaseAdapter) InitMigrations

func (o *BaseAdapter) InitMigrations(table string) error

// InitMigrations creates the migrations table in the DB

func (*BaseAdapter) Open

func (o *BaseAdapter) Open(dsn string) error

Open implements Adatpter interface

func (*BaseAdapter) Rows

func (o *BaseAdapter) Rows(q string, bind ...any) (err error)

Rows implements Adatpter interface. It returns rows typical for SELECT statement

func (*BaseAdapter) ScanCollection

func (o *BaseAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)

ScanRows implements db.Adapter.ScanRows

func (*BaseAdapter) ScanModel

func (o *BaseAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)

ScanModel implements db.Adapter.ScanModel While ScanCollection scans into the passed collection, this fnction returns new model instance

func (*BaseAdapter) ScanRelations

func (o *BaseAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)

ScanRows implements db.Adapter.ScanRelations

func (*BaseAdapter) Type

func (o *BaseAdapter) Type() string

Type returns the type name of the adapter: "mysql", "sqlite", etc

func (*BaseAdapter) Values

func (o *BaseAdapter) Values(q string, bind ...any) (ss []string, err error)

Values implements Adatpter interface. It returns a slice of values defined in SQL. (SELECT name FROM users) will return slice of all user names TODO: Generics!

type DB

type DB struct {
	Config   *config.DBConfig
	Migrator Migrator
	Logger   log.Logger
	Adapter  Adapter
	Schema   *schema.Schema
	// Models is the holder for all models that we want to configue
	// along with default code models that are configured automativally
	Models  []schema.Model
	Seeders []DBSeeder
}

func DBWithConfig

func DBWithConfig(cfg *config.DBConfig) (*DB, error)

WithConfig returns new DB instance with specified db config

func NewDB

func NewDB() *DB

NewDB returns new DB instance

func (*DB) Init

func (o *DB) Init() error

Init checks needed data and tries to connect to database also, tries to migrate any migrations available

func (*DB) RegisterModels

func (o *DB) RegisterModels(models []schema.Model)

RegisterModels allows custom models registering that will be part of complete db Schema along with core models.

func (*DB) RegisterSeeder

func (o *DB) RegisterSeeder(f DBSeeder)

RegisterSeeder keeps track aff all handler functions that we need to call with DB object, so they can all seed thir parts to the DB

type DBSeeder

type DBSeeder func(*DB) error

type Filter

type Filter struct {
	//          current
	//     -1  <=  |  => 1
	//  M1  |  M2  |  M3  |  M4  |  M5  |  M6  |
	// -1 migrated (left) , 1 unmigrated ( right )
	Status uint8
}

Filter helps to filter all migration files available

type Migrate

type Migrate struct {
	Config *config.MigrateConfig
	DB     *DB
	// contains filtered or unexported fields
}

func NewMigrate

func NewMigrate(db *DB) *Migrate

NewMigrate craetes new instance of the Migrate (Manager) MIgrator also requeres MigrateConfig, to create with config see NewMigrateWithConfig

func NewMigrateWithConfig

func NewMigrateWithConfig(db *DB, cfg *config.MigrateConfig) (m *Migrate)

NewMigrateWithConfig creates new instance of Migrate, and immediately sets the proper config of MigrateConfig

func (*Migrate) AbsDir

func (o *Migrate) AbsDir() (string, error)

AbsDir returns absolute directory to the migrations

func (*Migrate) Apply

func (o *Migrate) Apply(m *Migration) error

func (*Migrate) CreateMigration

func (o *Migrate) CreateMigration(name string) (m *Migration, err error)

CreateMigration accepts name like "create_users_table" or "Create Users Table" and creates migration file

func (*Migrate) EnsureTable

func (o *Migrate) EnsureTable() error

EnsureTable ensures that the migrations table in the DB exists

func (*Migrate) Files

func (o *Migrate) Files() ([]string, error)

Files returns all migration files available, no matter if they are already executed or not

func (*Migrate) FilteredFiles

func (o *Migrate) FilteredFiles(filter Filter) ([]string, error)

Returns slice of files filtered by status, etc.

func (*Migrate) Init

func (o *Migrate) Init() (err error)

Init prepares migrations table, etc.

func (*Migrate) Migrate

func (o *Migrate) Migrate() (err error)

Migrate executes all migrations that are not already executed in up direction

func (*Migrate) Migrated

func (o *Migrate) Migrated() (ss []string, err error)

Migrated returns all migrated migrations (string names) from the database

func (*Migrate) MigrationExists

func (o *Migrate) MigrationExists(name string) (bool, error)

MigrationExists checks if the migration file exists

in the migrations directory

func (*Migrate) Path

func (o *Migrate) Path(p string) (string, error)

Path will convert relative path to absolute path

func (*Migrate) RecordMigrated

func (o *Migrate) RecordMigrated(name string, batch uint32) error

Migrated returns all migrated migrations (string names) from the database

func (*Migrate) SetConfig

func (o *Migrate) SetConfig(cfg *config.MigrateConfig)

In some cases, we want to update the migrator config, so this func provides a way to do it

type Migration

type Migration struct {

	// Pointer to the Migrate (Manager)
	Migrate *Migrate

	// Id mirror in database
	Id schema.ObjectId

	// Name is migration file name (no dir) and also database name
	Name string

	// While Name can be changed with adding timestamp and extension,
	// OrigName is the name we used initially to create the migration
	OrigName string
}

Migration is mirror to single migration file

func FromName

func FromName(mm *Migrate, name string) *Migration

FromName retruns new Migration instance based on the (file) name. This is reversing of the situation when we want to get file name based on some string like "Create Users Table". Here we want to get a Migration from e.g. "200000000000_create_users_table.sql"

func NewMigration

func NewMigration(mm *Migrate, name string) *Migration

NewMigration creates new migration struct without ceateing any file in the system. It takes migration manager (mm) as the argument, so it has access to the config. To create migration use: Migrate.CreateMigration

func (*Migration) Create

func (m *Migration) Create() (err error)

CreateAt creates (writes) migration file at specific directory

func (*Migration) Exists

func (o *Migration) Exists() (bool, error)

Exists returns true if the migration file exists

func (*Migration) ExtractTableName

func (m *Migration) ExtractTableName(alt string) (t string)

ExtractTableName tries to extract table name from the migration name. e.g. '20220712121033_create_users_table.sql' will extract 'users'

func (*Migration) FullPath

func (o *Migration) FullPath() (string, error)

FullPath returns the absolute path of the migration file

func (*Migration) Read

func (o *Migration) Read() (c string, err error)

Read gets the contents from our migration file

func (*Migration) Remove

func (o *Migration) Remove() (err error)

Remove deletes the migration file from thy system

func (*Migration) Write

func (o *Migration) Write(sql string) (err error)

Write sets the content of our new migration file

type Migrator

type Migrator interface {
	CreateMigration(string) (*Migration, error)
	MigrationExists(string) (bool, error)
	Init() error
	EnsureTable() error
	SetConfig(*config.MigrateConfig)
}

type MySqlAdapter

type MySqlAdapter struct {
	Name   string
	Base   *BaseAdapter
	DB     *sql.DB
	Logger log.Logger
}

MySqlAdapter

func NewMySqlAdapter

func NewMySqlAdapter() (a *MySqlAdapter)

NewMySqlAdapter

func (*MySqlAdapter) Exec

func (o *MySqlAdapter) Exec(q string, bind ...any) (err error)

Exec implements Adatpter interface. It only executes sql query

func (*MySqlAdapter) GetMigrationSQL

func (o *MySqlAdapter) GetMigrationSQL() string

Type returns the type name of the adapter: "mysql", "sqlite", etc

func (*MySqlAdapter) InitMigrations

func (o *MySqlAdapter) InitMigrations(table string) error

// InitMigrations creates the migrations table in the DB

func (*MySqlAdapter) Open

func (o *MySqlAdapter) Open(dsn string) error

Open implements Adatpter interface

func (*MySqlAdapter) Rows

func (o *MySqlAdapter) Rows(q string, bind ...any) (err error)

Rows implements Adatpter interface. It returns rows typical for SELECT statement

func (*MySqlAdapter) ScanCollection

func (o *MySqlAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)

ScanRows implements db.Adapter.ScanRows

func (*MySqlAdapter) ScanModel

func (o *MySqlAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)

ScanModel implements db.Adapter.ScanModel While ScanCollection scans into the passed collection, this fnction returns new model instance

func (*MySqlAdapter) ScanRelations

func (o *MySqlAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)

ScanRows implements db.Adapter.ScanRelations

func (*MySqlAdapter) Type

func (o *MySqlAdapter) Type() string

Type returns the type name of the adapter: "mysql", "sqlite", etc

func (*MySqlAdapter) Values

func (o *MySqlAdapter) Values(q string, bind ...any) (ss []string, err error)

Values implements Adatpter interface. It returns a slice of values defined in SQL. (SELECT name FROM users) will return slice of all user names TODO: Generics!

type Session

type Session struct {
	DB *DB
	// contains filtered or unexported fields
}

func NewEntitySession

func NewEntitySession(db *DB, model schema.Model) *Session

func NewSession

func NewSession(db *DB, model schema.Model) *Session

func (*Session) All

func (s *Session) All() (schema.Collection, error)

All returns all models from the database

func (*Session) First

func (s *Session) First() (schema.Model, error)

All returns all models from the database

func (*Session) Get

func (s *Session) Get() (schema.Collection, error)

All returns all models from the database

func (*Session) Save

func (s *Session) Save() error

Save inserts or updates model item into the database

func (*Session) Where

func (s *Session) Where(key string, val string) *Session

Where returns back the same session object and assigns query with passed where filter

type SqliteAdapter

type SqliteAdapter struct {
	Name   string
	Base   *BaseAdapter
	DB     *sql.DB
	Logger log.Logger
}

SqliteAdapter

func NewSqliteAdapter

func NewSqliteAdapter() (a *SqliteAdapter)

NewSqliteAdapter

func (*SqliteAdapter) Exec

func (o *SqliteAdapter) Exec(q string, bind ...any) (err error)

Exec implements Adatpter interface. It only executes sql query

func (*SqliteAdapter) GetMigrationSQL

func (o *SqliteAdapter) GetMigrationSQL() string

Type returns the type name of the adapter: "mysql", "sqlite", etc

func (*SqliteAdapter) InitMigrations

func (o *SqliteAdapter) InitMigrations(table string) error

// InitMigrations creates the migrations table in the DB

func (*SqliteAdapter) Open

func (o *SqliteAdapter) Open(dsn string) error

Open implements Adatpter interface

func (*SqliteAdapter) Rows

func (o *SqliteAdapter) Rows(q string, bind ...any) (err error)

Rows implements Adatpter interface. It returns rows typical for SELECT statement

func (*SqliteAdapter) ScanCollection

func (o *SqliteAdapter) ScanCollection(col schema.Collection, query *schema.Query) (err error)

ScanRows implements db.Adapter.ScanRows

func (*SqliteAdapter) ScanModel

func (o *SqliteAdapter) ScanModel(m schema.Model, query *schema.Query) (mOut schema.Model, err error)

ScanModel implements db.Adapter.ScanModel While ScanCollection scans into the passed collection, this fnction returns new model instance

func (*SqliteAdapter) ScanRelations

func (o *SqliteAdapter) ScanRelations(sch *schema.Schema, col schema.Collection, query *schema.Query) (err error)

ScanRows implements db.Adapter.ScanRelations

func (*SqliteAdapter) Type

func (o *SqliteAdapter) Type() string

Type returns the type name of the adapter: "mysql", "sqlite", etc

func (*SqliteAdapter) Values

func (o *SqliteAdapter) Values(q string, bind ...any) (ss []string, err error)

Values implements Adatpter interface. It returns a slice of values defined in SQL. (SELECT name FROM users) will return slice of all user names TODO: Generics!

Jump to

Keyboard shortcuts

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