mysql

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ColumnTypeSet  = "Set"
	ColumnTypeEnum = "Enum"
)

Variables

This section is empty.

Functions

func OverrideConfigSettings

func OverrideConfigSettings(config *mysql.Config, jsonContent map[string]interface{})

OverrideConfigSettings will use a map read in from a json file to modify the given config settings

Types

type DB

type DB struct {
	sql2.DbHelper
	// contains filtered or unexported fields
}

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 NewMysqlDB, but afterwards, work through the DB parent interface so that the underlying database can be swapped out later if needed.

Timezones Timezones are always tricky. 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.

The benefit of this is that you can move your database to a server in another timezone, and the times will automatically change to the correct timezone.

  • The mysql-go-driver has the ability to set a default timezone in the Loc configuration parameter

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.

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. If the database gets replicated around the world, you must explicitly set the timezone of each database master and slave to keep datetimes in sync. Also be aware that if you are using a scaling service that is global, it too may change the local timezone of the server, which may be different from the timezone of the database. Add to this the possibility that your users may be accessing the servers from different timezones than either the database or server, and you get quite a tangle.

Add to that the TIMESTAMP has a max year of 2038, so TIMESTAMP itself is going to have to change soon.

So, as a general rule, use DATETIME types to represent a date combined with a time, like an appointment in a calendar or a recurring event that happens is entered in the current timezone is and that is editable. If you change timezones, the time will change too. Use TIMESTAMP or DATETIME types to store data that records when an event happened in world time. Use separate DATE and TIME values to record a date and time that should always be thought of in the perspective of the viewer, and that if the viewer changes timezones, the time will not change. 9 am in one timezone is 9 am in the other(An alarm for example.)

Also, set the Loc configuration parameter to be the same as the server's timezone. By default, its UTC. That will make it so all dates and times are in the same timezone as those automatically generated by MYSQL. It is best to set this and your database to UTC, as this will make your database portable to other timezones.

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.

The driver will return times in the timezone of the mysql server. This will mean that you can save data in local time, but you will need to convert to local time in some situations. Be particularly careful of DATE and TIME types, since these have no timezone information, and will always be in server time; converting to local time may have unintended effects.

You need to be aware that when you view the data in the SQL, it will appear in whatever timezone the MYSQL server is set to.

func NewMysqlDB

func NewMysqlDB(dbKey string, params string, config *mysql.Config) *DB

NewMysqlDB returns a new DB database object that you can add to the datastore.

func (*DB) Associate

func (m *DB) Associate(ctx context.Context,
	table string,
	column string,
	pk interface{},
	_ string,
	relatedColumn string,
	relatedPks interface{})

Associate sets up the many-many association pointing from the given table and column to another table and column. table is the name of the association table. column is the name of the column in the association table that contains the pk for the record we are associating. pk is the value of the primary key. relatedTable is the table the association is pointing to. relatedColumn is the column in the association table that points to the relatedTable's pk. relatedPks are the new primary keys in the relatedTable we are associating.

func (*DB) Delete

func (m *DB) Delete(ctx context.Context, table string, pkName string, pkValue interface{})

Delete deletes the indicated record from the database.

func (*DB) Describe

func (m *DB) Describe() *db.Database

Describe returns the database description object

func (*DB) GenerateDeleteSql

func (m *DB) GenerateDeleteSql(qb QueryBuilderI) (sql string, args []interface{})

GenerateDeleteSql generates SQL for a DELETE clause. It returns the generated SQL plus the arguments for value substitutions.

func (*DB) GenerateSelectSql

func (m *DB) GenerateSelectSql(qb QueryBuilderI) (sql string, args []interface{})

GenerateSelectSql generates SQL for a SELECT clause. It returns the clause plus the arguments that substitute for values.

func (*DB) Insert

func (m *DB) Insert(ctx context.Context, table string, fields map[string]interface{}) string

Insert inserts the given data as a new record in the database. It returns the record id of the new record.

func (*DB) NewBuilder

func (m *DB) NewBuilder(ctx context.Context) QueryBuilderI

NewBuilder returns a new query builder to build a query that will be processed by the database.

func (*DB) Update

func (m *DB) Update(ctx context.Context, table string, fields map[string]interface{}, pkName string, pkValue interface{})

Update sets specific fields of a record that already exists in the database to the given data.

Jump to

Keyboard shortcuts

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