Documentation ¶
Overview ¶
Package minidb is a minimalist database. It stores items in tables, where each item has a fixed number of fields. The package has two APIs. The direct API is centered around MDB structures that represent database connections. Functions of MDB call directly the underlying database layer. The second API is slower and may be used for cases when commands and results have to be serialized. It uses Command structures that are created by functions like OpenCommand, AddTableCommand, etc. These are passed to Exec() which returns a Result structure that is populated with result values.
Index ¶
- Constants
- func CloseAllDBs()
- func CreateDirIfNotExist(dir string) error
- func Fail(msg string, args ...interface{}) error
- func GenerateExternalSalt(params *Params) []byte
- func GenerateKey(password string, salt []byte, params *Params) *saltedKey
- func GetUserTypeString(field FieldType) string
- func ParseTime(s string) (time.Time, error)
- func QuerySortToStr(s QuerySort) string
- type Command
- func AddTableCommand(db CommandDB, table string, fields []Field) *Command
- func BackupCommand(db CommandDB, destination string) *Command
- func BeginCommand(db CommandDB) *Command
- func CloseCommand(db CommandDB) *Command
- func CommitCommand(db CommandDB, tx TxID) *Command
- func CountCommand(db CommandDB, table string) *Command
- func DeleteBlobCommand(db CommandDB, tx TxID, key int64) *Command
- func DeleteDateCommand(db CommandDB, tx TxID, key int64) *Command
- func DeleteIntCommand(db CommandDB, tx TxID, key int64) *Command
- func DeleteStrCommand(db CommandDB, tx TxID, key int64) *Command
- func FieldExistsCommand(db CommandDB, table string, field string) *Command
- func FieldIsEmptyCommand(db CommandDB, item Item, field string) *Command
- func FieldIsNullCommand(db CommandDB, item Item, field string) *Command
- func FindCommand(db CommandDB, query *Query, limit int64) *Command
- func GetBlobCommand(db CommandDB, key int64) *Command
- func GetCommand(db CommandDB, table string, item Item, field string) *Command
- func GetDateCommand(db CommandDB, key int64) *Command
- func GetFieldsCommand(db CommandDB, table string) *Command
- func GetIntCommand(db CommandDB, key int64) *Command
- func GetStrCommand(db CommandDB, key int64) *Command
- func GetTablesCommand(db CommandDB) *Command
- func HasBlobCommand(db CommandDB, key int64) *Command
- func HasDateCommand(db CommandDB, key int64) *Command
- func HasIntCommand(db CommandDB, key int64) *Command
- func HasStrCommand(db CommandDB, key int64) *Command
- func IndexCommand(db CommandDB, tx TxID, table string, field string) *Command
- func IsEmptyListFieldCommand(db CommandDB, table string, item Item, field string) *Command
- func IsListFieldCommand(db CommandDB, table string, field string) *Command
- func ItemExistsCommand(db CommandDB, table string, item Item) *Command
- func ListBlobCommand(db CommandDB) *Command
- func ListDateCommand(db CommandDB) *Command
- func ListIntCommand(db CommandDB, tx TxID) *Command
- func ListItemsCommand(db CommandDB, table string, limit int64) *Command
- func ListStrCommand(db CommandDB) *Command
- func MustGetFieldTypeCommand(db CommandDB, table string, field string) *Command
- func NewItemCommand(db CommandDB, tx TxID, table string) *Command
- func OpenCommand(driver string, file string) *Command
- func ParseFieldValuesCommand(db CommandDB, table string, field string, data []string) *Command
- func RemoveItemCommand(db CommandDB, tx TxID, table string, item Item) *Command
- func RollbackCommand(db CommandDB, tx TxID) *Command
- func SetBlobCommand(db CommandDB, tx TxID, key int64, value []byte) *Command
- func SetCommand(db CommandDB, tx TxID, table string, item Item, field string, data []Value) *Command
- func SetDateCommand(db CommandDB, tx TxID, key int64, value time.Time) *Command
- func SetDateStrCommand(db CommandDB, tx TxID, key int64, value string) *Command
- func SetIntCommand(db CommandDB, tx TxID, key int64, value int64) *Command
- func SetStrCommand(db CommandDB, tx TxID, key int64, value string) *Command
- func TableExistsCommand(db CommandDB, table string) *Command
- func ToSqlCommand(db CommandDB, table string, query *Query, limit int64) *Command
- type CommandDB
- type CommandID
- type ErrCode
- type Field
- type FieldType
- type Item
- type MDB
- func (db *MDB) AddTable(table string, fields []Field) error
- func (db *MDB) Backup(destination string) error
- func (db *MDB) Base() *sql.DB
- func (db *MDB) Begin() (*Tx, error)
- func (db *MDB) Close() error
- func (db *MDB) Count(table string) (int64, error)
- func (db *MDB) FieldExists(table string, field string) bool
- func (db *MDB) FieldIsEmpty(table string, item Item, field string) bool
- func (db *MDB) FieldIsNull(table string, item Item, field string) bool
- func (db *MDB) Find(query *Query, limit int64) ([]Item, error)
- func (db *MDB) Get(table string, item Item, field string) ([]Value, error)
- func (db *MDB) GetBlob(key int64) []byte
- func (db *MDB) GetDate(key int64) time.Time
- func (db *MDB) GetDateStr(key int64) string
- func (db *MDB) GetFields(table string) ([]Field, error)
- func (db *MDB) GetInt(key int64) int64
- func (db *MDB) GetStr(key int64) string
- func (db *MDB) GetTables() []string
- func (db *MDB) HasBlob(key int64) bool
- func (db *MDB) HasDate(key int64) bool
- func (db *MDB) HasInt(key int64) bool
- func (db *MDB) HasStr(key int64) bool
- func (db *MDB) IsEmptyListField(table string, item Item, field string) bool
- func (db *MDB) IsListField(table string, field string) bool
- func (db *MDB) ItemExists(table string, item Item) bool
- func (db *MDB) ListBlob() []int64
- func (db *MDB) ListDate() []int64
- func (db *MDB) ListInt() []int64
- func (db *MDB) ListItems(table string, limit int64) ([]Item, error)
- func (db *MDB) ListStr() []int64
- func (db *MDB) MustGetFieldType(table string, field string) FieldType
- func (db *MDB) NewItem(table string) (Item, error)
- func (db *MDB) ParseFieldValues(table string, field string, data []string) ([]Value, error)
- func (db *MDB) TableExists(table string) bool
- func (db *MDB) ToSql(table string, inquery *Query, limit int64) (string, error)
- func (db *MDB) UseItem(table string, id uint64) (Item, error)
- type MultiDB
- func (m *MultiDB) ArchiveUser(user *User, archivedir string) (ErrCode, error)
- func (m *MultiDB) Authenticate(username string, key *saltedKey) (*User, ErrCode, error)
- func (m *MultiDB) BaseDir() string
- func (m *MultiDB) Begin() (*Tx, error)
- func (m *MultiDB) Close() (ErrCode, error)
- func (m *MultiDB) Delete() (ErrCode, error)
- func (m *MultiDB) DeleteUser(user *User) (ErrCode, error)
- func (m *MultiDB) DeleteUserContent(user *User) (ErrCode, error)
- func (m *MultiDB) ExistingEmail(email string) bool
- func (m *MultiDB) ExistingUser(username string) bool
- func (m *MultiDB) ExternalSalt(username string) ([]byte, ErrCode, error)
- func (m *MultiDB) NewUser(username, email string, key *saltedKey) (*User, ErrCode, error)
- func (m *MultiDB) UserDB(user *User) (*MDB, ErrCode, error)
- func (m *MultiDB) UserDir(user *User) string
- func (m *MultiDB) UserEmail(user *User) (string, ErrCode, error)
- type Params
- type Query
- type QuerySort
- type Result
- type Tx
- func (tx *Tx) Commit() error
- func (tx *Tx) DeleteBlob(key int64)
- func (tx *Tx) DeleteDate(key int64)
- func (tx *Tx) DeleteInt(key int64)
- func (tx *Tx) DeleteStr(key int64)
- func (tx *Tx) Index(table, field string) error
- func (tx *Tx) RemoveItem(table string, item Item) error
- func (tx *Tx) Rollback() error
- func (tx *Tx) Set(table string, item Item, field string, data []Value) error
- func (tx *Tx) SetBlob(key int64, value []byte)
- func (tx *Tx) SetDate(key int64, value time.Time)
- func (tx *Tx) SetDateStr(key int64, value string)
- func (tx *Tx) SetInt(key int64, value int64)
- func (tx *Tx) SetStr(key int64, value string)
- type TxID
- type User
- type Value
Constants ¶
const ( NoErr int64 = iota + 1 ErrCannotOpen ErrUnknownDB ErrUnknownCommand ErrAddTableFailed ErrClosingDB ErrCountFailed ErrFindFailed ErrGetFailed ErrGetTablesFailed ErrListItemsFailed ErrNewItemFailed ErrParseFieldValuesFailed ErrSetFailed ErrToSQLFailed ErrFieldExistsFailed ErrGetFieldsFailed ErrInvalidDate ErrBackupFailed ErrRemoveItemFailed ErrIndexFailed ErrUnknownTx ErrBeginFailed ErrCommitFailed ErrRollbackFailed )
Numeric error codes returned by Exec() in a Result structure's Int field.
Variables ¶
This section is empty.
Functions ¶
func CloseAllDBs ¶
func CloseAllDBs()
CloseAllDBs closes all open DB connections and cleans up resources.
func CreateDirIfNotExist ¶ added in v0.0.8
CreateDirIfNotExists creates a directory including all subdirectories needed, or returns an error.
func GenerateExternalSalt ¶ added in v0.0.8
GenerateExternalSalt returns some new external salt of the length specified in params. This salt should be passed to NewUser and can be used for passphrase hashing prior to calling NewUser. It is stored in the user database and can be retrieved as ExternalSalt.
func GenerateKey ¶ added in v0.0.8
GenerateKey takes a password and some salt, and generates a salted key of length 64 bytes. Use the ExternalSalt as salt and the original, unaltered password. The function use Blake2b-512 for key derivation.
func GetUserTypeString ¶
GetUserTypeString returns a user-readable string for the type of a field.
func ParseTime ¶ added in v0.0.3
ParseTime parses a time string in RFC3339 format and returns the time or an error if the format is wrong.
func QuerySortToStr ¶ added in v0.0.4
QuerySortToStr convert the sort of a query to a string. This is merely used for debugging and testing.
Types ¶
type Command ¶
type Command struct { ID CommandID `json:"id"` DB CommandDB `json:"dbid"` Tx TxID `json:"txid"` StrArgs []string `json:"strings"` ItemArg Item `json:"item"` FieldArgs []Field `json:"fields"` ValueArgs []Value `json:"values"` QueryArg Query `json:"query"` IntArg int64 `json:"int"` IntArg2 int64 `json:"int2"` }
Command structures contains all information needed to execute an arbitrary command. Use Exec() to execute a command and get the result. Every function <Name> in minidb has a corresponding function <Name>Command that returns the corresponding command. Consult the API for <Name> for help, as the input parameters are exactly the same, except that the first two arguments are a database path of type CommandDB and often also a transaction id of type TxID. You should never use Command structures directly, but use the provided wrapper functions for strong typing. Result structures have the HasError field set to true if an error has occurred. Commands and results can be serialized to json.
func AddTableCommand ¶
AddTableCommand returns a pointer to a command structure for mdb.AddTable().
func BackupCommand ¶ added in v0.0.8
BackupCommand returns a pointer to a command structure for tx.Backup().
func BeginCommand ¶ added in v0.0.8
BeginCommand returns a pointer to a command structure for mdb.Begin().
func CloseCommand ¶
CloseCommand returns a pointer to a command structure for mdb.Close().
func CommitCommand ¶ added in v0.0.8
CommitCommand returns a pointer to a command structure for tx.Commit().
func CountCommand ¶
CountCommand returns a pointer to a command structure for tx.Count()
func DeleteBlobCommand ¶ added in v0.0.3
DeleteBlobCommand returns a pointer to a command structure for tx.DeleteBlob().
func DeleteDateCommand ¶ added in v0.0.3
DeleteDateCommand returns a pointer to a command structure for tx.DeleteDate().
func DeleteIntCommand ¶ added in v0.0.3
DeleteIntCommand returns a pointer to a command structure for tx.DeleteInt().
func DeleteStrCommand ¶ added in v0.0.3
DeleteStrCommand returns a pointer to a command structure for tx.DeleteStr().
func FieldExistsCommand ¶
FieldExistsCommand returns a pointer to a command structure for tx.FieldExists().
func FieldIsEmptyCommand ¶ added in v0.0.5
FieldIsEmptyCommand returns a pointer to a command structure for tx.FieldIsEmpty().
func FieldIsNullCommand ¶
FieldIsNullCommand returns a pointer to a command structure for tx.FieldIsNull().
func FindCommand ¶
FindCommand returns a pointer to a command structure for tx.Find().
func GetBlobCommand ¶ added in v0.0.3
GetBlobCommand returns a pointer to a command structure for tx.GetBlob().
func GetCommand ¶
GetCommand returns a pointer to a command structure for tx.Get().
func GetDateCommand ¶ added in v0.0.3
GetDateCommand returns a pointer to a command structure for tx.GetDate().
func GetFieldsCommand ¶
GetFieldsCommand returns a pointer to a command structure for tx.GetFields().
func GetIntCommand ¶ added in v0.0.3
GetIntCommand returns a pointer to a command structure for tx.GetInt().
func GetStrCommand ¶ added in v0.0.3
GetStrCommand returns a pointer to a command structure for tx.GetStr().
func GetTablesCommand ¶
GetTablesCommand returns a pointer to a command structure for tx.GetTables().
func HasBlobCommand ¶ added in v0.0.3
HasBlobCommand returns a pointer to a command structure for tx.HasBlob().
func HasDateCommand ¶ added in v0.0.3
HasDateCommand returns a pointer to a command structure for tx.HasDate().
func HasIntCommand ¶ added in v0.0.3
HasIntCommand returns a pointer to a command structure for tx.HasInt().
func HasStrCommand ¶ added in v0.0.3
HasStrCommand returns a pointer to a command structure for tx.HasStr().
func IndexCommand ¶ added in v0.0.8
IndexCommand returns a pointer to a command structure for tx.Index().
func IsEmptyListFieldCommand ¶
IsEmptyListFieldCommand returns a pointer to a command structure for tx.IsEmptyListField().
func IsListFieldCommand ¶
IsListFieldCommand returns a pointer to a command structure for tx.IsListField().
func ItemExistsCommand ¶
ItemExistsCommand returns a pointer to a command structure for tx.ItemExists().
func ListBlobCommand ¶ added in v0.0.3
ListBlobCommand returns a pointer to a command structure for tx.ListBlob().
func ListDateCommand ¶ added in v0.0.3
ListDateCommand returns a pointer to a command structure for tx.ListDate().
func ListIntCommand ¶ added in v0.0.3
ListIntCommand returns a pointer to a command structure for tx.ListInt().
func ListItemsCommand ¶
ListItemsCommand returns a pointer to a command structure for tx.ListItems().
func ListStrCommand ¶ added in v0.0.3
ListStrCommand returns a pointer to a command structure for tx.ListStr().
func MustGetFieldTypeCommand ¶
MustGetFieldTypeCommand returns a pointer to a command structure for tx.MustGetFieldType().
func NewItemCommand ¶
NewItemCommand returns a pointer to a command structure for tx.NewItem().
func OpenCommand ¶
OpenCommand returns a pointer to a command structure for mdb.Open().
func ParseFieldValuesCommand ¶
ParseFieldValuesCommand returns a pointer to a command structure for tx.ParseFieldValues().
func RemoveItemCommand ¶ added in v0.0.8
RemoveItemCommand returns a pointer to a command structure for tx.RemoveItem().
func RollbackCommand ¶ added in v0.0.8
RollbackCommand returns a pointer to a command structure for tx.Rollback().
func SetBlobCommand ¶ added in v0.0.3
SetBlobCommand returns a pointer to a command structure for tx.SetBlob().
func SetCommand ¶
func SetCommand(db CommandDB, tx TxID, table string, item Item, field string, data []Value) *Command
SetCommand returns a pointer to a command structure for tx.Set().
func SetDateCommand ¶ added in v0.0.3
SetDateCommand returns a pointer to a command structure for tx.SetDate().
func SetDateStrCommand ¶ added in v0.0.3
SetDateStrCommand returns a pointer to a command structure for tx.SetDateStr().
func SetIntCommand ¶ added in v0.0.3
SetIntCommand returns a pointer to a command structure for tx.SetInt().
func SetStrCommand ¶ added in v0.0.3
SetStrCommand returns a pointer to a command structure for tx.SetStr().
func TableExistsCommand ¶
TableExistsCommand returns a pointer to a command structure for tx.TableExists().
type CommandID ¶
type CommandID int
CommandID represents the type of a command in the Exec() function.
const ( // CmdOpen is the type of an Open command struct. CmdOpen CommandID = iota + 1 // CmdBegin opens a transaction CmdBegin // CmdRollback rolls back a transaction CmdRollback // CmdCommit commits a transaction CmdCommit // CmdAddTable is the type of an AddTable command struct. CmdAddTable // CmdClose is the type of a Close command struct. CmdClose // CmdCount is the type of a Count command struct. CmdCount // CmdFind is the type of a Find command struct. CmdFind // CmdGet is the type of a Get command struct. CmdGet // CmdGetTables is the type of a GetTables command struct. CmdGetTables // CmdIsListField is the type of an IsListField command struct. CmdIsListField // CmdItemExists is the type of an ItemExists command struct. CmdItemExists // CmdListItems is the type of a ListItems command struct. CmdListItems // CmdNewItem is the type of a NewItem command struct. CmdNewItem // CmdParseFieldValues is the type of a ParseFieldValues command struct. CmdParseFieldValues // CmdSet is the type of a Set command struct. CmdSet // CmdTableExists is the type of a TableExists command struct. CmdTableExists // CmdToSQL is the type of a ToSQL command struct. CmdToSQL // CmdFieldIsNull is the type of a FieldIsNull command struct. CmdFieldIsNull // CmdFieldExists is the type of a FieldExists command struct. CmdFieldExists // CmdGetFields is the type of a GetFields command struct. CmdGetFields // CmdIsEmptyListField is the type of an IsEmptyListField command struct. CmdIsEmptyListField // CmdMustGetFieldType is the type of a MustGetFieldType command struct. CmdMustGetFieldType // CmdGetInt is the type of a GetInt command struct. CmdGetInt // CmdGetStr is the type of a GetStr command struct. CmdGetStr // CmdGetBlob is the type of a GetBlob command struct. CmdGetBlob // CmdGetDate is the type of a GetDate command struct. CmdGetDate // CmdSetInt is the type of a SetInt command struct. CmdSetInt // CmdSetStr is the type of a SetStr commmand struct. CmdSetStr // CmdSetBlob is the type of a SetBlob commmand struct. CmdSetBlob // CmdSetDate is the type of a SetDate command struct. CmdSetDate // CmdDeleteInt is the type of a DeleteInt command struct. CmdDeleteInt // CmdDeleteStr is the type of a DeleteStr command struct. CmdDeleteStr // CmdDeleteBlob is the type of a DeleteBlob command struct. CmdDeleteBlob // CmdDeleteDate is the type of a DeleteDate command struct. CmdDeleteDate // CmdHadInt is the type of a HasInt command struct. CmdHasInt // CmdHasStr is the type of a HasStr command struct. CmdHasStr // CmdHasBlob is the type of a HasBlob command struct. CmdHasBlob // CmdHasDate is the type of a HasDate command struct. CmdHasDate // CmdListInt is the type of a ListInt commmand struct. CmdListInt // CmdListStr is the type of a ListStr command struct. CmdListStr // CmdListBlob is the type of a ListBlob command struct. CmdListBlob // CmdListDate is the type of a ListDate command struct. CmdListDate // CmdSetDateStr is the type of a SetDateStr command struct. CmdSetDateStr // CmdFieldIsEmpty is the type of a FieldIsEmpty commmand struct. CmdFieldIsEmpty // CmdBackup is the type of a Backup commmand struct. CmdBackup // CmdRemoveItem is the type of a RemoveItem commmand struct. CmdRemoveItem // CmdIndex is the type of an Index command struct. CmdIndex )
The actual Exec() CommandID values. Names mirror the respective functions.
type ErrCode ¶ added in v0.0.8
type ErrCode int
ErrCode types represent errors instead of error structures.
const ( ErrAuthenticationFailed ErrCode = iota + 1 // User authentication has failed (wrong password). OK // No error has occured. ErrUsernameInUse // The user name is already being used. ErrEmailInUse // The email is already being used. ErrCryptoRandFailure // The random number generator has failed. ErrInvalidParams // One or more parameters were invalid. ErrUnknownUser // The user is not known. ErrNotEnoughSalt // Insufficiently long salt has been supplied. ErrInvalidUser // The user name or email is invalid. ErrDBClosed // The internal housekeeping DB is locked, corrupted, or closed. ErrDBFail // A database operation has failed. ErrFileSystem // A directory or file could not be created. ErrNoHome // The user's DB home directory does not exist. ErrCloseFailed // Could not close the user database. ErrOpenFailed // Could not open the user database. ErrPackFail // Compressing user data failed. ErrInvalidKey // A given salted key is invalid (either nil, or other problems). ErrTransactionFail // Could not perform op because of a failed transaction. )
Error codes returned by the functions.
type Field ¶
Field represents a database field.
func ParseFieldDesc ¶
ParseFieldDesc parses the given string slice into a []Field slice based on the format "type name", or returns an error. This can be used for command line parsing.
type FieldType ¶
type FieldType int
FieldType is a field in the database, which might be a list type or a base type.
const ( // DBError represents an error in a field definition. DBError FieldType = iota + 1 // DBInt is the type of an int64 field. DBInt // DBString is the type of a string field. DBString // DBBlob is the type of a []byte field. DBBlob // DBIntList is the type of a list of int64 field. DBIntList // DBStringList is the type of a list of strings field. DBStringList // DBBlobList is the type of a list of []byte field, i.e., corresponding to [][]byte. DBBlobList // DBDate is the type of an RFC 3339 date field. DBDate // DBDateList is the type of a list of RFC 3339 dates field. DBDateList )
func ToBaseType ¶
ToBaseType converts a list type into the list's base type. A non-list type remains unchanged.
type MDB ¶
type MDB struct {
// contains filtered or unexported fields
}
MDB is the main database object.
func (*MDB) AddTable ¶
AddTable is used to create a new table. Table and field names are validated. They need to be alphanumeric sequences plus underscore "_" as the only allowed special character. None of the names may start with an underscore.
func (*MDB) Backup ¶ added in v0.0.8
Backup a database and return the original opened, not the backed up database. The new MDB pointer needs to be used after this method unless an error has occurred since the old one might have become invalid. This function may result in a corrupt copy if the database is open by another process, so you need to make sure that it isn't.
func (*MDB) Base ¶ added in v0.0.8
Base returns the base sqlx.DB that minidb uses for its underlying storage.
func (*MDB) Close ¶
Close closes the database, making sure that all remaining transactions are finished.
func (*MDB) FieldExists ¶
FieldExists returns true if the table has the field, false otherwise.
func (*MDB) FieldIsEmpty ¶ added in v0.0.4
FieldIsEmpty returns true if the field is null or empty, false otherwise.
func (*MDB) FieldIsNull ¶
FieldIsNull returns true if the field is null for the item in the table, false otherwise.
func (*MDB) Find ¶
Find items matching the query, return error if the query is ill-formed and the items otherwise.
func (*MDB) GetBlob ¶ added in v0.0.3
GetBlob returns the blob value for a key, nil if key doesn't exist.
func (*MDB) GetDate ¶ added in v0.0.3
GetDate returns the time.Time value of a, January 1, year 1, 00:00:00.000000000 UTC if the key doesn't exist.
func (*MDB) GetDateStr ¶ added in v0.0.3
GetDateStr returns a date in RFC3339 string form, the empty string if the key doesn't exist.
func (*MDB) GetInt ¶ added in v0.0.3
GetInt returns the int64 value for a key, 0 if key doesn't exist.
func (*MDB) GetStr ¶ added in v0.0.3
GetStr returns the string value for a key, "" if key doesn't exist.
func (*MDB) HasBlob ¶ added in v0.0.3
HasBlob returns true if a byte array value is stored for the key, false otherwise.
func (*MDB) HasDate ¶ added in v0.0.3
HasDate returns true if a time.Time value is stored for the key, false otherwise.
func (*MDB) HasInt ¶ added in v0.0.3
HasInt returns true if an int value is stored for the key, false otherwise.
func (*MDB) HasStr ¶ added in v0.0.3
HasStr returns true of a string value is stored for the key, false otherwise.
func (*MDB) IsEmptyListField ¶
IsEmptyListField returns true if the field is a list field and has no element matching item. If the item does not exist, the function returns true as well.
func (*MDB) IsListField ¶
IsListField is true if the field in the table is a list, false otherwise. List fields are internally stored as special tables.
func (*MDB) ItemExists ¶
ItemExists returns true if the item exists in the table, false otherwise.
func (*MDB) MustGetFieldType ¶
MustGetFieldType returns the type of the field. This method panics if the table or field don't exist.
func (*MDB) ParseFieldValues ¶
ParseFieldValues parses potential value(s) for a field from strings, returns an error if their type is incompatible with the field type, the table or field don't exist, or if the input is empty. Data for a Blob field must be Base64 encoded, data for an Integer field must be a valid digit sequence for a 64 bit integer in base 10 format.
func (*MDB) TableExists ¶
TableExists returns true if the table exists, false otherwise.
func (*MDB) ToSql ¶
ToSql returns the sql query for the table, taking into account list fields, or returns an error if the query structure is ill-formed.
type MultiDB ¶ added in v0.0.8
type MultiDB struct {
// contains filtered or unexported fields
}
MultiDB contains all information needed for housekeeping multiple DBs, except for the parameters and context-specific information like passwords.
func NewMultiDB ¶ added in v0.0.8
NewMultiDB returns a new multi user database.
func (*MultiDB) ArchiveUser ¶ added in v0.0.8
ArchiveUser stores the user data in a packed zip file but does not close or remove the user. This can be used for backups or for archiving.
func (*MultiDB) Authenticate ¶ added in v0.0.8
Authenticate a user by given name and salted password. Returns the user and OK if successful, otherwise nil, a numeric error code and the error. Notice that the external salt is not passed to this function. Instead, the password string should have been prepared (securely hashed, whitened, etc.) before calling this function on the basis of the user's ExternalSalt.
func (*MultiDB) BaseDir ¶ added in v0.0.8
BaseDir returns the base directory of the multiuser database. This directory contains databases for all users.
func (*MultiDB) Close ¶ added in v0.0.8
Close the MultiDB, closing the internal housekeeping and all open user databases.
func (*MultiDB) Delete ¶ added in v0.0.8
Delete deletes the whole multiuser db including all housekeeping information and directories. This action cannot be undone.
func (*MultiDB) DeleteUser ¶ added in v0.0.8
DeleteUser deletes a user and all associated user content from a multiuser database.
func (*MultiDB) DeleteUserContent ¶ added in v0.0.8
DeleteUserContent deletes a user's content in the multiuser database, i.e., all the user data. This action cannot be undone.
func (*MultiDB) ExistingEmail ¶ added in v0.0.8
ExistingEmail returns true if a user with this email address exists, false otherwise.
func (*MultiDB) ExistingUser ¶ added in v0.0.8
ExistingUser returns true if a user with the given user name exists, false otherwise.
func (*MultiDB) ExternalSalt ¶ added in v0.0.8
ExternalSalt is the salt associated with a user. It is stored in the database and may be used for hashing the password prior to authentication. The external salt is not used for internal key derivation.
func (*MultiDB) NewUser ¶ added in v0.0.8
NewUser creates a new user with given username, email, and password. Based on a strong salt that is only used internally and the Argon2 algorithm with the given parameters an internal key is created and stored in an internal database. The user and OK are returned unless an error has occurred. The integer returned is a numeric error code to make it easier to distinguish certain cases: EmailInUse - the email has already been registered, UsernameInUse - a user with the same user name has already been registered. Both emails and usernames must be unique and cannot be registered twice.
type Params ¶ added in v0.0.8
type Params struct { Argon2Memory uint32 Argon2Iterations uint32 Argon2Parallelism uint8 KeyLength uint32 InternalSaltLength uint32 ExternalSaltLength uint32 }
Params contain all the parameters that are used by a multiuser database.
func DefaultParams ¶ added in v0.0.8
func DefaultParams() *Params
DefaultParams returns parameters with reasonable default values that are safe to use. Be aware that default parameters may change from release to release to reflect updates and changes in security requirements.
type Query ¶
type Query struct { Sort QuerySort `json:"sort"` Children []Query `json:"children"` Data string `json:"data"` }
Query represents a simple or complex database query.
func FailedQuery ¶
FailedQuery returns a failed Query pointer with the given message as explanation why it failed.
func ParseQuery ¶
ParseQuery parses a string representing the part after the table name into a Query structure. Implements the classic Shunting Yard algorithm.
type QuerySort ¶
type QuerySort int
QuerySort is the type of a query. Some of these sorts are only used internally.
const ( // ParseError indicates that query parsing was unsuccessful. ParseError QuerySort = iota + 1 // TableString is the type of a string for a table, e.g. "Person". TableString // QueryString is the type of a string for a query, i.e., what's right of "=". QueryString // LogicalAnd is the type of "and". LogicalAnd // LogicalOr is the type of "or". LogicalOr // LogicalNot is the type of "not". LogicalNot // FieldString is the type of a string for a field, e.g. "name". FieldString // SearchClause is the type of a main search clause (outermost level of a query). SearchClause // NoTerm is the type of "no" in a list-field query like "Person no name=John". NoTerm // EveryTerm is the type of "every" in a list-field query like "Person every name=%s%". EveryTerm // LeftParen is the type of "(" and variants, used internally. LeftParen // RightParen is the type of ")" and variants, used internally. RightParen // InfixOP is the type of "=". InfixOP )
The sorts of query entries.
type Result ¶
type Result struct { Str string `json:"str"` Strings []string `json:"strings"` Int int64 `json:"int64"` Bool bool `json:"bool"` Items []Item `json:"items"` Values []Value `json:"values"` Fields []Field `json:"fields"` Bytes []byte `json:"binary"` Ints []int64 `json:"ints"` HasError bool `json:"iserror"` }
Result is a structure representing the result of a command execution via Exec(). If an error has occurred, then HasError is true and the Int and S fields contain the numeric error code and the error message string. Otherwise the respective fields are filled in, as corresponding to the return value(s) of the respective function call.
type Tx ¶ added in v0.0.8
type Tx struct {
// contains filtered or unexported fields
}
Tx represents a transaction similar to sql.Tx.
func (*Tx) DeleteBlob ¶ added in v0.0.8
DeleteBlob deletes the key and string for given key. It has no effect if the key-value pair doesn't exist.
func (*Tx) DeleteDate ¶ added in v0.0.8
DeleteDate deletes the key and date value for given key. It has no effect if the key-value pair doesn't exist.
func (*Tx) DeleteInt ¶ added in v0.0.8
DeleteInt deletes the key and int value for given key. It has no effect if the key-value pair doesn't exist.
func (*Tx) DeleteStr ¶ added in v0.0.8
DeleteStr deletes the key and string value for given key. It has no effect if the key-value pair doesn't exist.
func (*Tx) Index ¶ added in v0.0.8
Index creates an index for field in table unless the index exists already. An index increases the search speed of certain string queries on the field, such as "Person name=joh%".
func (*Tx) RemoveItem ¶ added in v0.0.8
RemoveItem remove an item from the table.
func (*Tx) Set ¶ added in v0.0.8
Set the given values in the item in table and given field. An error is returned if the field types don't match the data.
func (*Tx) SetDateStr ¶ added in v0.0.8
SetDateStr stores a datetime in RFC3339 format by key. The correctness of the string is not validated. Use this function in combination with GetDateStr to prevent unnecessary conversions.
type User ¶ added in v0.0.8
type User struct {
// contains filtered or unexported fields
}
User represents a user.
type Value ¶
Value holds the values that can be put into the database or retrieved from it.
func NewBytes ¶
NewBytes creates a value that holds a []byte slice. This is similar to String() but notice that strings and byte slices are handled differently in the database. For example, byte slices may contain NULL characters and may be converted to and from Base64.
func NewDateStr ¶ added in v0.0.3
NewDateStr creates a value that holds a datetime given by a RFC3339 representation. The correctness of the date string is not validated, so use this function with care.
func (*Value) Bytes ¶
Bytes returns the value as a bytes slice. It automatically converts int64 and string. An int64 is written in Little Endian format.