Documentation
¶
Index ¶
- func OverrideConfigSettings(config *mysql.Config, jsonContent map[string]interface{})
- type DB
- func (m *DB) DeleteUsesAlias() bool
- func (m *DB) ExtractSchema(options map[string]any) schema2.Database
- func (m *DB) FormatArgument(_ int) string
- func (m *DB) Insert(ctx context.Context, table string, fields map[string]any, autoPkKey string) error
- func (m *DB) OperationSql(op Operator, operands []Node, operandStrings []string) (sql string)
- func (m *DB) QuoteIdentifier(v string) string
- func (m *DB) SupportsForUpdate() bool
- func (m *DB) TableDefinitionSql(d *schema2.Database, table *schema2.Table) (tableSql string, extraClauses []string)
- func (m *DB) Update(ctx context.Context, table string, primaryKey map[string]any, ...) (err error)
- func (m *DB) WithConstraintsOff(ctx context.Context, f func(ctx context.Context) error) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OverrideConfigSettings ¶
OverrideConfigSettings will use a map read in from a json file to modify the given config settings
Types ¶
type DB ¶
DB is the goradd driver for mysql databases. It works through the excellent go-sql-driver driver, to supply functionality above go's built in driver. To use it, call NewDB, but afterward, work through the DB parent interface so that the underlying database can be swapped out later if needed.
Primary Keys ¶
Historically, Mysql uses auto generated integers as primary keys. Newer versions of Mysql are able to generate UUIDs, but the implementation is v1 only, and is not the most secure, nor the best for balancing storage needs. To use UUIDs, instead override the getXXXinsertFields function and manually set your own UUID.
Timezones ¶
Mysql has some interesting quirks:
- Datetime types are internally stored in the timezone of the server, and then returned based on the timezone of the client.
- Timestamp types are internally stored in UTC and returned in the timezone of the client.
This means that when querying based on DateTime fields, you need to be aware that the timezone will have shifted to the timezone of the server. Also, when transferring or syncing between databases in different timezones, you will need to account for the time differences.
The easiest way to handle this is to set the timezone of the server to UTC, and make sure all time values are stored in UTC.
When displaying times to users, change the timezone to that of the user, which may not be the Mysql server's timezone, and may not be the timezone of the computer running the go-sql-driver (which from MySQL's perspective is the client).
The mysql-go-driver has the ability to set a default timezone in the Loc configuration parameter. It defaults to UTC. It appears to convert all times to this timezone before sending them to the database, and then when receiving times, it will set this as the timezone of the date. It is best to set this and your database to UTC, as this will make your database portable to other timezones.
These issues are further compounded by the fact that MYSQL can initialize date and time values to what it believes is the current date and time in its server's timezone, but will not save the timezone itself. Because of that, you should initialize values in the application, rather than using MySQL's ability to set a default value.
Set the ParseTime configuration parameter to TRUE so that the driver will parse the times into the correct timezone, navigating the GO server and database server timezones. Otherwise, we can only assume that the database is in UTC time, since we will not get any timezone info from the server.
Be aware that when you view the data in SQL, it will appear in whatever timezone the MYSQL server is set to.
func NewDB ¶
NewDB returns a new DB database object that you can add to the datastore. If connectionString is set, it will be used to create the configuration. Otherwise, use a config setting.
func (*DB) DeleteUsesAlias ¶
DeleteUsesAlias indicates the database requires the alias of a table after a delete clause when using aliases in the delete.
func (*DB) FormatArgument ¶
FormatArgument formats the given argument number for embedding in a SQL statement. Mysql just uses a question mark as a placeholder.
func (*DB) Insert ¶
func (m *DB) Insert(ctx context.Context, table string, fields map[string]any, autoPkKey string) error
Insert inserts the given data as a new record in the database.
func (*DB) OperationSql ¶
OperationSql provides Mysql specific SQL for certain operators.
func (*DB) QuoteIdentifier ¶
QuoteIdentifier surrounds the given identifier with quote characters appropriate for mysql
func (*DB) SupportsForUpdate ¶
func (*DB) TableDefinitionSql ¶
func (m *DB) TableDefinitionSql(d *schema2.Database, table *schema2.Table) (tableSql string, extraClauses []string)
TableDefinitionSql will return the sql needed to create the table. This can include clauses separated by semicolons. All the returned tableSql items will be executed for all tables before extraSql will be executed. This allows extraSql to refer to other tables in the schema that might not have been created yet, and is particularly useful for handling cyclic foreign key references.
func (*DB) Update ¶
func (m *DB) Update(ctx context.Context, table string, primaryKey map[string]any, changes map[string]any, optLockFieldName string, optLockFieldValue int64, ) (err error)
Update sets specific fields of a single record that exists in the database. optLockFieldName is the name of a version field that will implement an optimistic locking check while doing the update. If optLockFieldName is provided:
- That field will be used to limit the update,
- That field will be updated with a new version and returned in changes.
- If the record was previously deleted or updated, an OptimisticLockError will be returned. You will need to query further to determine if the record still exists.
Otherwise, if optLockFieldName is blank, and the record we are attempting to change does not exist, the database will not be altered, and no error will be returned.
func (*DB) WithConstraintsOff ¶
WithConstraintsOff makes sure operations in f occur with foreign key constraints turned off. As a byproduct of this, the operations will happen on the same pinned connection, meaning the operations should not be long-running so that the connection pool will not run dry. Nested calls will continue to operate with checks off, and the outermost call will turn them on.