Categorygithub.com/c9845/sqldb
modulepackage
1.4.2
Repository: https://github.com/c9845/sqldb.git
Documentation: pkg.go.dev

# README

Introduction:

Package sqldb provides tooling for establishing and managing a database connection, deploying and updating schemas, and running queries. This wraps the sql package to reduce some boilerplate while providing tools for schema and query management.

Details:

  • Supports MySQL, MariaDB, and SQLite (with and without CGO). Additional database support should be relatively easy to add.
  • Define a database connection configuration and manage the connection pool for running queries.
  • Deploy and update database schema.
  • Works either by storing the connection details within the package in a global manner or you can store the details separately elsewhere in your app. Storing the details separately allows for multiple different databases to be connected to at the same time.

Getting Started:

You first need to define a configuration using New...Config or Default...Config based on if you want to store the configuration yourself or store it within this package. You can also define the configuration yourself using sqldb.Config{}.

Once you have a configuration defined, you can connect to your database using Connect() which will validate your configuration and then try connecting. If you want to deploy or update your database schema, call DeploySchema() or UpdateSchema() before connecting.

Now with an established connection, run queries using your config in a manner such as myconfig.Connection().Exec().

Deploying and Updating Schema:

This works by providing a list of queries to run in your config. When the appropriate function is called, the queries will be run in order.

Queries used to deploy a database can optionally be translated from one database format to another (i.e.: MySQL to SQLite). This is useful since different database types structure their CREATE TABLE queries slightly differently but it would be a lot of extra work to maintain a separate set of queries for each database type. This works by doing a string replacement, and some more modifications, to queries so that you can write the queries in one database's format and still deploy to multiple database types.

Queries used to update the database are checked for safely ignorable errors. This is useful for instances where you rerun the UpdateSchema function (think, on each app start to ensure the schema is up to date) and want to ignore errors such as when a column already exists or was already removed.

SQLite Libraries:

You can use github.com/mattn/go-sqlite3 or modernc.org/sqlite. The mattn library requires CGO which makes cross compiling a bit more difficult, however, it does use the SQLite C source code. The modernc library is a C to Go translation of the SQLite source, which while it doesn't required CGO, it isn't the original SQLite source code.

Simply put: if you are worried about cross compiling, using modernc is the way to go.

Use either library by providing the associated build tag (mattn is the default if no tag is provided):

go build -tags mattn ...
go build -tags modernc ...
go run -tags mattn ...
go run -tags modernc ...

# Functions

Close closes the connection using the default package level config.
Connect handles the connection to the database using the default package level config.
Connected returns if the config represents an established connection to the database.
Connection returns the database connection for the package level config.
DBType returns a dbType.
DefaultConfig initializes the package level config.
DefaultMapperFunc is the default MapperFunc set on configs.
DefaultMariaDBConfig initializes the package level config with some defaults set.
DefaultMySQLConfig initializes the package level config with some defaults set.
DefaultSQLiteConfig initializes the package level config with some defaults set.
DeploySchema runs DeploySchemaWithOps with some defaults set for the default packagelevel config.
DeploySchemaWithOps deploys the database for the default package level config.
GetDefaultConfig returns the package level saved config.
GetSQLiteBusyTimeout returns the SQLite busy timeout used for the connected db.
GetSQLiteJournalMode returns the SQLite journalling mode used for the connected db.
GetSQLiteLibrary returns the sqlite library that was used to build the binary.
GetSQLiteVersion returns the version of SQLite that is embedded into the app.
IsMariaDB returns true if the database is a MariaDb database.
IsMySQL returns true if the database is a MySQL database.
IsSQLite returns true if the database is a SQLite database.
MapperFunc sets the mapper func for the package level config.
NewConfig returns a base configuration that will need to be modified for use with a db.Typically you would use New...Config() instead.
NewMariaDBConfig returns a config for connecting to a MySQL database.
NewMySQLConfig returns a config for connecting to a MySQL database.
NewSQLiteConfig returns a config for connecting to a SQLite database.
Save saves a configuration to the package level config.
SetDeployFuncs sets the list of funcs to deploy the database schema for the packagelevel config.
SetDeployQueries sets the list of queries to deploy the database schema for the packagelevel config.
SetUpdateIgnoreErrorFuncs sets the list of funcs to handle update schema errors for thepackage level config.
SetUpdateQueries sets the list of funcs to update the database schema for the package levelconfig.
TFMySQLToSQLiteReformatDatetime replaces DATETIME columns with TEXT columns.
TFMySQLToSQLiteReformatDefaultTimestamp handles converting UTC_TIMESTAMP values toCURRENT_TIMESTAMP values.
TFMySQLToSQLiteReformatID reformats the ID column to a SQLite format.
TFMySQLToSQLiteRemovePrimaryKeyDefinition removes the primary key definitionfor a SQLite query since SQLite doesn't use this.
TranslateCreateTableFuncs sets the translation funcs for creating a table for the packagelevel config.
UFAddDuplicateColumn checks if an error was generated because a column already exists.This typically happens because you are rerunning UpdateSchema() and the column hasalready been added.
UFAlreadyExists handles errors when an index already exists.
UFDropUnknownColumn checks if an error from was generated because a column does not exist.This typically happens because you are rerunning UpdateSchema() and the column hasalready been dropped.
UFModifySQLiteColumn checks if an error occured because you are trying to modify acolumn for a SQLite database.
UpdateSchema runs UpdateSchemaWithOps with some defaults set for the default packagelevel config.
UpdateSchemaWithOps updates the database for the default package level config.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
InMemoryFilePathRaceSafe is the "path" to provide for the SQLite file when youwant to use an in-memory database between multiple "Connect" calls.
InMemoryFilePathRacy is the "path" to provide for the SQLite file when you wantto use an in-memory database instead of a filesystem file database.

# Variables

ErrConnected is returned when a database connection is already established and a user istrying to connect or trying to modify a config that is already in use.
ErrDoubleCommaInColumnString is returned when building a column string for a query buta double comma exists which would cause the query to not run correctly.
ErrHostNotProvided is returned when user doesn't provide the host (IP or FQDN) where a MySQLor MariaDB server is running.
ErrInvalidPort is returned when user doesn't provide, or provided an invalid port, for wherethe MySQL or MariaDB server is running.
ErrNameNotProvided is returned when user doesn't provide a name for a database.
ErrNoColumnsGiven is returned when user is trying to build a column list for a query butnot columns were provided.
ErrPasswordNotProvided is returned when user doesn't provide the password to connect to thedatabase with.
ErrSQLitePathNotProvided is returned when user doesn't provided a path to the SQLite databasefile, or the path provided is all whitespace.
ErrUserNotProvided is returned when user doesn't provide a user to connect to the databaseserver with.

# Structs

Config is the details used for establishing and using a database connection.
DeploySchemaOptions provides options when deploying a schema.
UpdateSchemaOptions provides options when updating a schema.

# Type aliases

Bindvars holds the parameters you want to use in a query.
Columns is used to hold columns for a query.
TranslateFunc is function used to translate a query from one database formatto another.
UpdateIgnoreErrorFunc is function for handling errors returned when trying to updatethe schema of your database using UpdateSchema().
Where is the WHERE statement in a query.