dbi

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: GPL-3.0 Imports: 8 Imported by: 0

README

dbi

Go Reference Go Report Card

Package dbi implements a database/sql wrapper.

This is an EXPERIMENTAL package used for experimenting.

Installation

The recommended way to install dbi

go get github.com/stianwa/dbi

Examples


package main
 
import (
       "github.com/stianwa/dbi"
     _ "github.com/lib/pq"
       "fmt"
)

type SomeData struct {
       Name string `dbi:"name"`
       Age  int    `dbi:"age"`
}

func main() {
       db := &dbi.Config{Name: "db",
                         User: "dbuser}

       if err := db.Open(); err != nil {
          fmt.Printf("%v\n", err)
          return
       }

       var rows []*SomeData
       if err := db.Unmarshal(&rows, "select from somedata where age = ?", 21); err != nil {
          fmt.Printf("%v\n", err)
          return
       }

       for _, row := range rows {
          fmt.Printf("name: %s age: %d\n", row.Name, row.Age)
       }
}

State

The dbi module is experimental and under development. Do not use for production.

License

GPLv3, see LICENSE.md

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Database name
	Name string `yaml:"name"     json:"name"`
	// Database user name
	User string `yaml:"user"     json:"user"`
	// Database password can be one of:
	// 1) password text
	// 2) pointer to a file containing the password:       file:/path/to/secret.pw
	// 3) an environment variable containing the password: env:PASSWORD_ENV
	Password string `yaml:"password" json:"password"`
	// SSLMode: disable,allow,prefer,require,verify-ca,verify-full. Default: disable
	SSLMode string `yaml:"sslmode"  json:"sslmode"`
	// Driver, only postgresql (the default) is tested.
	Driver string `yaml:"driver"   json:"driver"`
	// Hostname can be a hostname or a path to a socket. Defaults to /var/run/postgresql
	Host string `yaml:"host"     json:"host"`
	// Maximum open connections
	MaxOpenConns int `yaml:"maxOpenConns"`
	// Maximum idle connections
	MaxIdleConns int `yaml:"maxIdleConns"`
	// contains filtered or unexported fields
}

Config holds the connection string parameters and handler

func (*Config) Close

func (c *Config) Close() error

Close the handler. Return error upon errors.

func (*Config) MultiQuery

func (c *Config) MultiQuery(sql []string, args ...[]interface{}) ([][]map[string]interface{}, error)

MultiQuery takes a SQL list and data list (paired), and returns set of rows with string map to interfaces values.

func (*Config) Open

func (c *Config) Open() error

Open initialize a configuration. An error is returned if the initializing failed.

func (*Config) Ping

func (c *Config) Ping() error

Ping the database. Return error upon errors.

func (*Config) PrepareTest

func (c *Config) PrepareTest(sql string) error

PrepareTest test a sql, and returns an error if it fails.

func (*Config) Query

func (c *Config) Query(sql string, args ...interface{}) ([]map[string]interface{}, error)

Query takes an SQL and data, and returns rows with string map to interfaces values.

func (*Config) QueryInterface

func (c *Config) QueryInterface(sql string, args ...interface{}) ([]map[string]interface{}, error)

QueryInterface takes an SQL and data, and returns rows with string map to interfaces values.

func (*Config) QueryString

func (c *Config) QueryString(sql string, args ...interface{}) ([]map[string]string, error)

QueryString takes an SQL and data, and returns rows with string map to stringified values.

func (*Config) Transaction

func (c *Config) Transaction(sqls []string, args ...[][]interface{}) error

Transaction takes a SQL list and an array list of values for transactions. of rows with string map to interfaces values.

func (*Config) Unmarshal

func (c *Config) Unmarshal(v interface{}, SQL string, args ...interface{}) error

Unmarshal rows into a slice with pointers to a struct. The mapping between row columns and struct fields are done with field tags named dbi. When getting dates from the database, the pq modules returnes a date format looking very much like a timestamp including time and time zone. To get rid of this, the unmarshaler can strip away the time part if the field type is a string and the option date is specified after the column name. Example. `dbi:"date,date"`

func (*Config) UnmarshalReadOnly added in v0.1.4

func (c *Config) UnmarshalReadOnly(v interface{}, SQL string, args ...interface{}) error

UnmarshalReadOnly rows into a slice with pointers to a struct. The mapping between row columns and struct fields are done with field tags named dbi. When getting dates from the database, the pq modules returnes a date format looking very much like a timestamp including time and time zone. To get rid of this, the unmarshaler can strip away the time part if the field type is a string and the option date is specified after the column name. Example. `dbi:"date,date"`

func (*Config) UnmarshalWithOptions added in v0.1.4

func (c *Config) UnmarshalWithOptions(txOpts *sql.TxOptions, v interface{}, SQL string, args ...interface{}) error

UnmarshalWithOptions rows into a slice with pointers to a struct. The mapping between row columns and struct fields are done with field tags named dbi. When getting dates from the database, the pq modules returnes a date format looking very much like a timestamp including time and time zone. To get rid of this, the unmarshaler can strip away the time part if the field type is a string and the option date is specified after the column name. Example. `dbi:"date,date"`

func (*Config) Upsert

func (c *Config) Upsert(sql string, array [][]interface{}) error

Upsert takes a SQL and an array list of values for upserts. of rows with string map to interfaces values.

Jump to

Keyboard shortcuts

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