Exemplo n.º 1
0
// NewConnection creates a new connection, and sets it's `Dialect`
// appropriately based on the `ConnectionDetails` passed into it.
func NewConnection(deets *ConnectionDetails) (*Connection, error) {
	err := deets.Finalize()
	if err != nil {
		return nil, errors.Wrap(err, "could not create a new connection")
	}
	c := &Connection{
		ID: randx.String(30),
	}
	switch deets.Dialect {
	case "postgres":
		c.Dialect = newPostgreSQL(deets)
	case "mysql":
		c.Dialect = newMySQL(deets)
	case "sqlite3":
		c.Dialect = newSQLite(deets)
	}
	return c, nil
}
Exemplo n.º 2
0
func (c *Connection) NewTransaction() (*Connection, error) {
	var cn *Connection
	if c.TX == nil {
		tx, err := c.Store.Transaction()
		if err != nil {
			return cn, errors.Wrap(err, "couldn't start a new transaction")
		}
		cn = &Connection{
			ID:      randx.String(30),
			Store:   tx,
			Dialect: c.Dialect,
			TX:      tx,
		}
	} else {
		cn = c
	}
	return cn, nil
}
Exemplo n.º 3
0
// Rollback will open a new transaction and automatically rollback that transaction
// when the inner function returns, regardless. This can be useful for tests, etc...
func (c *Connection) Rollback(fn func(tx *Connection)) error {
	var cn *Connection
	if c.TX == nil {
		tx, err := c.Store.Transaction()
		if err != nil {
			return errors.Wrap(err, "couldn't start a new transaction")
		}
		cn = &Connection{
			ID:      randx.String(30),
			Store:   tx,
			Dialect: c.Dialect,
			TX:      tx,
		}
	} else {
		cn = c
	}
	fn(cn)
	return cn.TX.Rollback()
}
Exemplo n.º 4
0
func Test_String(t *testing.T) {
	r := require.New(t)
	r.Len(randx.String(5), 5)
	r.Len(randx.String(50), 50)
}