sqlutil

package module
v0.0.0-...-2ef78b3 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2018 License: AGPL-3.0 Imports: 5 Imported by: 15

README

sqlutil

GitHub Slack License

Golang package that provides utils for working with dotsql structs and abstractions for working with postgres databases.

Copyright (C) 2017 Data Together
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3.0.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the LICENSE file for details.

Getting Involved

We would love involvement from more people! If you notice any errors or would like to submit changes, please see our Contributing Guidelines.

We use GitHub issues for tracking bugs and feature requests and Pull Requests (PRs) for submitting changes

Usage

Include in any Golang package with:

import "github.com/datatogether/sqlutil"

Technical documentation can be built with godoc . or, if your $GOPATH and repo structure is set up correctly, with something like godoc -http=:6060 & followed by browsing to http://localhost:6060/pkg/github.com/datatogether.

Development

Coming soon!

Documentation

Overview

utils for working with dotsql structs

abstractions for working with postgres databases

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectToDb

func ConnectToDb(driverName, url string, db *sql.DB) error

Uniform Database connector

func EnsureSeedData

func EnsureSeedData(db *sql.DB, schemaFilepath, dataFilepath string, tables ...string) (created []string, err error)

EnsureSeedData runs "EnsureTables", and then injects seed data for any newly-created tables

func EnsureTables

func EnsureTables(db *sql.DB, schemaFilepath string, tables ...string) ([]string, error)

EnsureTables checks for table existence, creating them from the schema file if not, returning a slice of table names that were created

func SetupConnection

func SetupConnection(driverName, connString string) (db *sql.DB, err error)

Sets up a connection with a given postgres db connection string

Types

type DataCommands

type DataCommands struct {
	// contains filtered or unexported fields
}

SchemaFile is an sql file that defines a database schema

func LoadDataCommands

func LoadDataCommands(sqlFilePath string) (*DataCommands, error)

LoadDataCommands takes a filepath to a sql file with create & drop table commands and returns a DataCommands

func LoadDataString

func LoadDataString(sql string) (*DataCommands, error)

func (*DataCommands) Commands

func (d *DataCommands) Commands() []string

func (*DataCommands) DeleteAll

func (d *DataCommands) DeleteAll(db Execable) error

DropAll executes the command named "drop-all" from the sql file this should be a command in the form: DROP TABLE IF EXISTS foo, bar, baz ...

func (*DataCommands) Reset

func (d *DataCommands) Reset(db Execable, tables ...string) error

type Execable

type Execable interface {
	Queryable
	Exec(query string, args ...interface{}) (sql.Result, error)
}

Execable ugrades a read-only interface to be able to execute sql DDL statements

type Queryable

type Queryable interface {
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

Querable unifies both *sql.DB & *sql.Tx for querying purposes

type Scannable

type Scannable interface {
	Scan(...interface{}) error
}

Scannable unifies both *sql.Row & *sql.Rows, functions can accept Scannable & work with both

type SchemaCommands

type SchemaCommands struct {
	// contains filtered or unexported fields
}

SchemaCommands is an sql file that defines a database schema

func LoadSchemaCommands

func LoadSchemaCommands(sqlFilePath string) (*SchemaCommands, error)

LoadSchemaCommands takes a filepath to a sql file with create & drop table commands and returns a SchemaCommands

func LoadSchemaString

func LoadSchemaString(sql string) (*SchemaCommands, error)

func (*SchemaCommands) Create

func (s *SchemaCommands) Create(db Execable, tables ...string) ([]string, error)

Create tables if they don't already exist

func (*SchemaCommands) DropAll

func (s *SchemaCommands) DropAll(db Execable) error

DropAll executes the command named "drop-all" from the sql file this should be a command in the form: DROP TABLE IF EXISTS foo, bar, baz ...

func (*SchemaCommands) DropAllCreate

func (s *SchemaCommands) DropAllCreate(db Execable, tables ...string) error

type TestSuite

type TestSuite struct {
	DB      *sql.DB
	Schema  *SchemaCommands
	Data    *DataCommands
	Cascade []string
}

func InitTestSuite

func InitTestSuite(o *TestSuiteOpts) (*TestSuite, error)

type TestSuiteOpts

type TestSuiteOpts struct {
	DriverName      string
	ConnString      string
	SchemaPath      string
	SchemaSqlString string
	DataPath        string
	DataSqlString   string
	Cascade         []string
}

type Transactable

type Transactable interface {
	Execable
	Begin() (*sql.Tx, error)
}

Jump to

Keyboard shortcuts

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