Documentation ¶
Index ¶
- func Reset()
- func Shutdown()
- type Connection
- type Database
- func (db *Database) Connection() Connection
- func (db *Database) ConnectionString() string
- func (db *Database) CreateDatabase() *Result
- func (db *Database) GenerateInsert(obj interface{}) *Query
- func (db *Database) Insert(obj interface{}) *Result
- func (db *Database) InsertAll(objs interface{}) ([]*Result, error)
- func (db *Database) Ping() error
- func (db *Database) Query(sql string, args ...interface{}) *Query
- func (db *Database) Schema(name string) *Schema
- func (db *Database) Stats() sql.DBStats
- type Field
- type LastInsertId
- type Logger
- type PrimaryKey
- type Query
- func (q *Query) Exec() *Result
- func (q *Query) ExecAll(dest interface{}) *Result
- func (q *Query) ExecAllIn(dest interface{}) *Result
- func (q *Query) ExecNonQuery() *Result
- func (q *Query) ExecSingle(dest interface{}) *Result
- func (q *Query) Repeat(times int, pSelectorFn SelectParamsFn) *Repeat
- func (q *Query) String() string
- type Repeat
- type Result
- type RowsAffected
- type SSLMode
- type Schema
- type SelectParamsFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Connection ¶
type Connection struct { Database string User string Password string Host string Port string AppName string Timeout int SSLMode SSLMode SSLCert string SSLKey string SSLRootCert string }
Connection holds all database connection configuration.
func NewConnection ¶
func NewConnection(databaseURL string, appName string) Connection
NewConnection creates and returns the Connection object to the postgres server
func (Connection) NewDatabase ¶
func (conn Connection) NewDatabase() *Database
NewDatabase creates a new Database object
func (*Connection) String ¶
func (conn *Connection) String() string
String method builds a DSN(Data Source Name) connection string based on the given database connection settings and returns it.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database contains all required database attributes
func (*Database) Connection ¶
func (db *Database) Connection() Connection
Connection returns the connection information for a database
func (*Database) ConnectionString ¶
ConnectionString returns the DSN(Data Source Name) connection string for the current DB connection.
func (*Database) CreateDatabase ¶
CreateDatabase creates a default database Good for use during testing and local dev
func (*Database) GenerateInsert ¶
GenerateInsert generates an insert query for the given object
func (*Database) InsertAll ¶
InsertAll inserts a slice of objects concurrently. objs must be a slice with items in it. the Result slice will be in the same order as objs so a simple loop will set all the primary keys if needed:
for i, r := range results { objs[i].Id = r.LastInsertId.ID }
func (*Database) Query ¶
Query creates a base new query object that can be used for all database operations
type Field ¶
Field is a struct field that represents a single entity of an object. To set a field as primary add `db_pk:true` to tag.
type LastInsertId ¶
type LastInsertId struct { ID PrimaryKey Err error }
LastInsertId is the last inserted ID from a script
type Logger ¶
type Logger interface { Info(args ...interface{}) Infof(format string, args ...interface{}) Debug(args ...interface{}) Debugf(format string, args ...interface{}) }
Logger is the required interface for the papergres logger
var Log Logger
Log is the way to log the scripting activity happening from the library to the database
type Query ¶
type Query struct { SQL string Database *Database Args []interface{} // contains filtered or unexported fields }
Query holds the SQL to execute and the connection string
func (*Query) Exec ¶
Exec runs a sql command given a connection and expects LastInsertId or RowsAffected to be returned by the script. Use this for INSERTs
func (*Query) ExecAll ¶
ExecAll gets many rows and populates the given slice dest should be a pointer to a slice
func (*Query) ExecAllIn ¶ added in v1.1.0
ExecAllIn works with IN queries. It will take a slice of values and attach the slice to the query as a list of values. One key difference with bindvar used for IN query is a `?` (question mark) the query then has to be rebinded to change default bindvar to target bindvar like `$1` (dollar sign followed by a number) for postgres etc.
func (*Query) ExecNonQuery ¶
ExecNonQuery runs the SQL and doesn't look for any results
func (*Query) ExecSingle ¶
ExecSingle fetches a single row from the database and puts it into dest. If more than 1 row is returned it takes the first one. Expects at least 1 row or it will return an error.
func (*Query) Repeat ¶
func (q *Query) Repeat(times int, pSelectorFn SelectParamsFn) *Repeat
Repeat will execute a query N times. The param selector function will pass in the current iteration and expect back the destination obj and args for that index. Make sure to use pointers to ensure the sql results fill your structs. Use this when you want to run the same query for many different parameters, like getting data for child entities for a collection of parents. This function executes the iterations concurrently so each loop should not rely on state from a previous loops execution. The function should be extremely fast and efficient with DB resources. Returned error will contain all errors that occurred in any iterations.
Example usage:
params := func(i int) (dest interface{}, args []interface{}) { p := &parents[i] // get parent at i to derive parameters args := MakeArgs(p.Id, true, "current") // create arg list, variadic return &p.Child, args // &p.Child will be filled with returned data } // len(parents) is parent slice and determines how many times to execute query results, err := db.Query(sql).Repeat(len(parents), params).Exec()
type Repeat ¶
type Repeat struct { Query *Query ParamsFn SelectParamsFn N int }
Repeat holds the iteration count and params function for executing a query N times.
type Result ¶
type Result struct { LastInsertId LastInsertId RowsAffected RowsAffected RowsReturned int ExecutionTime time.Duration Err error }
Result holds the results of an executed query
func Exec ¶
func Exec(sql string, conn Connection, args ...interface{}) *Result
Exec executes an ad-hoc query against a connection. This is only recommended for use if you have a weird case where you need to modify the connection string just for this query, like when creating a new database. Otherwise just new New() and save the DAtabase instance.
func ExecNonQuery ¶
func ExecNonQuery(sql string, conn Connection, args ...interface{}) *Result
ExecNonQuery executes an ad-hoc query against a connection. This is only recommended for use if you have a weird case where you need to modify the connection string just for this query, like when creating a new database. Otherwise just new New() and save the Database instance.
type RowsAffected ¶
RowsAffected is the returned rows affected from a script
type SSLMode ¶
type SSLMode string
SSLMode defines all possible SSL options
const ( // SSLDisable - No SSL SSLDisable SSLMode = "disable" // SSLRequire - Always SSL, no verification SSLRequire SSLMode = "require" // SSLVerifyCA - Always SSL, verifies that certificate was signed by trusted CA SSLVerifyCA SSLMode = "verify-ca" // SSLVerifyFull - Always SSL, verifies that certificate was signed by trusted CA // and server host name matches the one in the certificate SSLVerifyFull SSLMode = "verify-full" )
type Schema ¶
Schema holds the schema to query along with the Database
func (*Schema) GenerateInsert ¶
GenerateInsert generates the insert query for the given object
func (*Schema) GenerateInsertWithPK ¶ added in v1.1.0
GenerateInsertWithPK generates the insert query for the given object in which PrimaryKey value is also supposed to be populated during insert.
func (*Schema) Insert ¶
Insert inserts the passed in object in DB.
NOTE: It does not account for client side generated value for a primary key and expects that the logic for populating value of primary key should reside in the database as sequence.
DO NOT use Insert() if you wish to populate a client side generated value in primary key, use InsertWithPK() instead.
func (*Schema) InsertAll ¶
InsertAll inserts a slice of objects concurrently. objs must be a slice with items in it. the Result slice will be in the same order as objs so a simple loop will set all the primary keys if needed:
for i, r := range results { objs[i].Id = r.LastInsertId.ID }
func (*Schema) InsertWithPK ¶ added in v1.1.0
InsertWithPK performs inserts on objects and persists the Primary key value to DB as well. It will fail to insert duplicate values to DB.
NOTE: Proceed with caution! Only use this method you wish to persist a client side generated value in Primary key and don't rely on database sequenece to autogenerate PrimaryKey values.
type SelectParamsFn ¶
type SelectParamsFn func(i int) (dest interface{}, args []interface{})
SelectParamsFn is a function that takes in the iteration and returns the destination and args for a SQL execution.