database

package module
v0.0.0-...-daf71f5 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2019 License: MIT Imports: 4 Imported by: 0

README

Database

GoDoc Build Status Go Report Card codecov.io

Removes the need for building queries!

Usage

setup.sql
DROP DATABASE IF EXISTS test;

CREATE DATABASE test;
USE test;

CREATE TABLE people (
	name varchar(256) not null,
	age smallint not null,
	created_at TIMESTAMP not null,
	updated_at TIMESTAMP null,
	primary key (name)
);
main.go
package main

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/dtucker2/database"
	_ "github.com/go-sql-driver/mysql"
)

type Person struct {
	Name      string     `name:"name" key:"true"`
	Age       int        `name:"age"`
	CreatedAt *time.Time `name:"created_at" type:"created_at"`
	UpdatedAt *time.Time `name:"updated_at" type:"updated_at"`
}

func (person *Person) GetTableName() string {
	return "people"
}

func main() {

	sqlDB, _ := sql.Open("mysql", "user:password@/test?parseTime=true")
	db := database.NewDatabase(sqlDB)

	person := Person{Name: "Frank", Age: 32}

	db.Insert(&person)
	/* Result:
	+-------+-----+---------------------+------------+
	| name  | age | created_at          | updated_at |
	+-------+-----+---------------------+------------+
	| Frank |  32 | 2019-01-28 23:20:00 | NULL       |
	+-------+-----+---------------------+------------+
	*/

	person.Age = 33
	db.Update(&person)
	/* Result:
	+-------+-----+---------------------+---------------------+
	| name  | age | created_at          | updated_at          |
	+-------+-----+---------------------+---------------------+
	| Frank |  33 | 2019-01-28 23:20:00 | 2019-01-28 23:20:00 |
	+-------+-----+---------------------+---------------------+
	*/

	person = Person{Name: "Frank"} // Note the lack of any age.
	db.Select(&person)
	fmt.Printf("%+v\n", person)
	/* Output:
	{Name:Frank Age:33 CreatedAt:2019-01-28 23:20:00 +0000 UTC UpdatedAt:2019-01-28 23:20:01 +0000 UTC}
	*/

	db.Delete(&person)
	/* Result:
	Empty set (0.00 sec)
	*/

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	*sql.DB
	*query.QueryBuilder
}

Database wraps a sql.DB object and provides the ability to easily insert and update rows in a MySQL database using only a pointer to a struct.

func NewDatabase

func NewDatabase(db *sql.DB) *Database

NewDatabase returns a pointer to a new instance of the Database struct.

func (*Database) Delete

func (db *Database) Delete(object interface{}) error

Delete constructs and executes a delete query on the database using only the passed pointer to a struct. The structs primary key field must be populated as this populates the 'WHERE' clause of the query.

func (*Database) Insert

func (db *Database) Insert(object interface{}) error

Insert constructs and executes an insert query on the database using only the passed pointer to a struct.

func (*Database) Select

func (db *Database) Select(object interface{}) error

Select constructs and executes a select query on the database using only the passed pointer to a struct. The structs primary key field must be populated as this populates the 'WHERE' clause of the query. The resulting row will be returned by reference in the passed struct.

func (*Database) Update

func (db *Database) Update(object interface{}) error

Update constructs and executes an update query on the database using only the passed pointer to a struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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