Categorygithub.com/c9845/sqldb/v2
modulepackage
2.5.1
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.

Install:

Assuming you are using go modules the latest version can be installed with:

go get github.com/c9845/sqldb/v2

Then use in your code with:

import "github.com/c9845/sqldb/v2

And use via:

sqldb.New...()
sqldb.Connect()
//...

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

AddConnectionOption adds a key-value pair to the ConnectionOptions field for the package level config.
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 globally accessible package level config with some defaults set.
DefaultMapperFunc is the default MapperFunc set on configs.
DefaultMariaDBConfig initializes the globally accessible package level config with some defaults set.
DefaultMSSQLConfig initializes the globally accessible package level config with some defaults set.
DefaultMySQLConfig initializes the globally accessible package level config with some defaults set.
DefaultSQLiteConfig initializes the globally accessible package level config with some defaults set.
DeploySchema runs DeploySchemaWithOps with some defaults set for the default package level config.
DeploySchemaWithOps deploys the database for the default package level config.
GetDefaultConfig returns the package level saved config.
GetDefaultSQLitePragmas returns the default PRAGMAs this package defines for use with either SQLite library.
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.
IsMSSQL returns true if the database is a Microsoft SQL Server database.
IsMySQL returns true if the database is a MySQL database.
IsMySQLOrMariaDB returns if the database is a MySQL or MariaDB for the package level config.
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 to connect to and interact with a database.
NewMariaDBConfig returns a config for connecting to a MariaDB database.
NewMSSQLConfig returns a config for connecting to a Microsoft SQL Server 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.
TFMySQLToSQLiteBLOB translates TINYBLOB, MEDIUMBLOB, and LONGBLOB to BLOB.
TFMySQLToSQLiteReformatDatetime replaces DATETIME columns with TEXT columns.
TFMySQLToSQLiteReformatDefaultTimestamp handles converting UTC_TIMESTAMP values to CURRENT_TIMESTAMP values.
TFMySQLToSQLiteReformatID reformats the ID column from a MySQL format to a SQLite format.
TFMySQLToSQLiteRemovePrimaryKeyDefinition removes the primary key definition from a MySQL query for use in SQLite.
UFAddDuplicateColumn checks if an error was generated because a column already exists.
UFDropUnknownColumn checks if an error from was generated because a column does not exist.
UFIndexAlreadyExists handles errors when an index already exists.
UFModifySQLiteColumn checks if an error occured because you are trying to modify a column for a SQLite database.
UpdateSchema runs UpdateSchemaWithOps with some defaults set for the default package level config.
UpdateSchemaWithOps updates the database for the default package level config.
UseDefaultTranslateFuncs populates TranslateCreateTableFuncs and TranslateUpdateFuncs with the default translation funcs for the package level config.

# Constants

No description provided by the author
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.
primarily development related.
general errors, most typical use/.
some info on db connections, deployment, updates.
no logging.

# Variables

ErrConnected is returned when a trying to establish a connection to an alreadyconnected-to database.
ErrExtraCommaInColumnString is returned when building a column string for aquery but an extra comma exists which would cause the query to not run correctly.Extra commas are usually due to an empty column name being provided or a commabeing added to the column name by mistake.
ErrHostNotProvided is returned when user doesn't provide the host IP or FQDNof a MySQL or MariaDB server.
ErrInvalidLoggingLevel is returned when an invalid logging level is provided.
ErrInvalidPort is returned when user doesn't provide, or provided an invalidport, of a MySQL or MariaDB server.
ErrNameNotProvided is returned when user doesn't provide a name of a database.
ErrNoColumnsGiven is returned when user is trying to build a column list for aquery but no columns were provided.
ErrPasswordNotProvided is returned when user doesn't provide the password toconnect to the database with.
ErrSQLitePathNotProvided is returned when user doesn't provided a path to theSQLite database file, or the path provided is all whitespace.
ErrUserNotProvided is returned when user doesn't provide a user to connect tothe database server 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.
DeployFunc is the format of a function used to deploy the database schema.
UpdateFunc is the format of a function used to update the database schema.
UpdateIgnoreErrorFunc is function for handling errors returned when trying to update the schema of your database using UpdateSchema().
Where is the WHERE statement in a query.