db

package module
v0.0.0-...-297bc14 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2015 License: BSD-3-Clause Imports: 12 Imported by: 2

README

ORM library

Supported databases include MySQL, MariaDB, PostgreSQL, Sqlite3.

Build Status Coverage Status GoDoc License

Useage

Install

go get github.com/zhgo/db

Sample sql scripts

CREATE TABLE `table1` (
  `UserID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `CreationTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `BirthYear` year(4) NOT NULL,
  `Gender` enum('Secret','Male','Female') NOT NULL DEFAULT 'Secret',
  `Nickname` varchar(16) NOT NULL,
  PRIMARY KEY (`UserID`)
) ENGINE=InnoDB AUTO_INCREMENT=1000000 DEFAULT CHARSET=utf8;

The example use MySQL as default database instance.

Import

import (
    "github.com/zhgo/db"
)

Connect to database

s := db.Connect("mysql", "root:@tcp(127.0.0.1:3306)/zhgo?charset=utf8")

mysql is sql driver type, mysql, postgresql, sqlite. root:@tcp(127.0.0.1:3306)/zhgo?charset=utf8 is DSN.

Insert

// INSERT INTO table1(BirthYear, Gender, Nickname) VALUES(1980, 'Male', 'Bob')
r, err := s.InsertInto("table1").Fields("BirthYear", "Gender", "Nickname").Values(1980, "Male", "Bob").Exec()

Values() method can be called multiple times to insert multiple rows.

Or:

// INSERT INTO table1(BirthYear, Gender, Nickname) VALUES(1980, 'Male', 'Bob')
d = db.Item{"BirthYear": 1980, "Gender": "Male", "Nickname": "Bob"}
r, err := s.InsertInto("table1").Exec(d)

d is a map type.

Update

// UPDATE table1 SET BirthYear = 1982, Gender = 'Female', Nickname = 'Bob' WHERE UserID = 1000000
q := s.NewQuery()
q.Update("table1")
q.Set("BirthYear", 1982)
q.Set("Gender", "Female")
q.Set("Nickname", "Bob")
r, err := q.Where(q.Eq("UserID", 1000000)).Exec()

Set() method can be called multiple times to update multiple fields.

Or:

// UPDATE table1 SET BirthYear = 1988, Gender = 'Male', Nickname = 'C语言' WHERE UserID = 1000001
d = db.Item{"BirthYear": 1988, "Gender": "Male", "Nickname": "C语言"}
w = db.Where{"UserID": 1000001}
r, err := s.Update("table1").Exec(d, w)

Delete

// DELETE FROM table1 WHERE UserID = 1000000
q := s.NewQuery()
r, err := q.DeleteFrom("table1").Where(q.Eq("UserID", 1000000)).Exec()

Or:

// DELETE FROM table1 WHERE UserID = 1000001
w := db.Where{"UserID": 1000001}
r, err := s.DeleteFrom("table1").Exec(w)

Select

// SELECT * FROM table1 WHERE UserID = 1000001
d := []db.Item{}
q := s.NewQuery()
err := q.Select("*").From("table1").Where(q.Eq("UserID", 1000000)).Rows(&d)

Or:

// SELECT * FROM table1 WHERE UserID = 1000001
d = make(db.Item)
w = db.Where{"UserID": 1000001}
err := s.Select("*").From("table1").Row(&d, w)

Copyright 2015 The zhgo Authors. All rights reserved.

Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Documentation

Index

Constants

View Source
const (
	QueryInsert = iota
	QueryUpdate
	QueryDelete
	QuerySelect
)

Query type

Variables

View Source
var Env int8 = 0

Enviroment: 0, 1, 2, 3

View Source
var Servers = make(map[string]*Server)

Server list

Functions

This section is empty.

Types

type Condition

type Condition struct {
	Eq   map[string]string   `json:"eq"`
	Ge   map[string]string   `json:"ge"`
	Gt   map[string]string   `json:"gt"`
	Le   map[string]string   `json:"le"`
	Lt   map[string]string   `json:"lt"`
	Ne   map[string]string   `json:"ne"`
	Like map[string]string   `json:"like"`
	In   map[string][]string `json:"in"`
}

Condition struct

type Item

type Item map[string]interface{}

Alias of map[string]interface{}

type Items

type Items []map[string]interface{}

Alias of []map[string]interface{}

type Model

type Model struct {
	// Module name, as DB name.
	Module string

	// table instance
	Table *Table
}

Model struct

func NewModel

func NewModel(module string, table *Table) Model

New Model

func (*Model) Delete

func (m *Model) Delete() *Query

Delete

func (*Model) Insert

func (m *Model) Insert() *Query

Insert

func (*Model) Select

func (m *Model) Select(f ...string) *Query

Select

func (*Model) Update

func (m *Model) Update() *Query

Update

type Query

type Query struct {
	// Server
	Server *Server

	// Query type: Insert, Update, Delete, Select
	Type uint

	// Table primary field
	Primary string

	// Sql
	Sql map[string]string

	// Condition SQL
	SqlCond []string

	//args for Sql
	Args []interface{}

	//args index
	ArgIndex int

	// If the query object is created by Model, this is will be assigned. optional.
	Table *Table
	// contains filtered or unexported fields
}

Query struct

func NewQuery

func NewQuery(server *Server) *Query

New Query object

func (*Query) And

func (q *Query) And(qs ...string) string

And

func (*Query) AndEq

func (q *Query) AndEq(f string, v interface{}) string

And (Equal)

func (*Query) AndGe

func (q *Query) AndGe(f string, v interface{}) string

And (Greater than or equal)

func (*Query) AndGt

func (q *Query) AndGt(f string, v interface{}) string

And (Greater than)

func (*Query) AndIn

func (q *Query) AndIn(f string, v ...interface{}) string

And (In: Check whether a value is within a set of values)

func (*Query) AndLe

func (q *Query) AndLe(f string, v interface{}) string

And (Less than or equal)

func (*Query) AndLike

func (q *Query) AndLike(f string, v interface{}) string

And (Like: Simple pattern matching)

func (*Query) AndLt

func (q *Query) AndLt(f string, v interface{}) string

And (Less than)

func (*Query) AndNe

func (q *Query) AndNe(f string, v interface{}) string

And (Not equal)

func (*Query) DeleteFrom

func (q *Query) DeleteFrom(tb string) *Query

Delete

func (*Query) Eq

func (q *Query) Eq(f string, v interface{}) string

Equal

func (*Query) Exec

func (q *Query) Exec(d ...map[string]interface{}) (Result, error)

Exec

func (*Query) Fields

func (q *Query) Fields(f ...string) *Query

Fields(Insert)

func (*Query) From

func (q *Query) From(tb string) *Query

From

func (*Query) Ge

func (q *Query) Ge(f string, v interface{}) string

Greater than or equal

func (*Query) GroupBy

func (q *Query) GroupBy(f ...string) *Query

Group By

func (*Query) Gt

func (q *Query) Gt(f string, v interface{}) string

Greater than

func (*Query) Having

func (q *Query) Having(qs ...string) *Query

Having

func (*Query) In

func (q *Query) In(f string, v ...interface{}) string

In: Check whether a value is within a set of values

func (*Query) InnerJoin

func (q *Query) InnerJoin(tb string) *Query

Join Inner

func (*Query) InsertInto

func (q *Query) InsertInto(tb string) *Query

Insert

func (*Query) Join

func (q *Query) Join(tb string) *Query

Join

func (*Query) Le

func (q *Query) Le(f string, v interface{}) string

Less than or equal

func (*Query) LeftJoin

func (q *Query) LeftJoin(tb string) *Query

Join Left

func (*Query) Like

func (q *Query) Like(f string, v interface{}) string

Like: Simple pattern matching

func (*Query) Limit

func (q *Query) Limit(offset, rows int64) *Query

Limit

func (*Query) Lt

func (q *Query) Lt(f string, v interface{}) string

Less than

func (*Query) Ne

func (q *Query) Ne(f string, v interface{}) string

Not equal

func (*Query) On

func (q *Query) On(qs ...string) *Query

Join On

func (*Query) Or

func (q *Query) Or(qs ...string) string

Or

func (*Query) OrEq

func (q *Query) OrEq(f string, v interface{}) string

Or (Equal)

func (*Query) OrGe

func (q *Query) OrGe(f string, v interface{}) string

Or (Greater than or equal)

func (*Query) OrGt

func (q *Query) OrGt(f string, v interface{}) string

Or (Greater than)

func (*Query) OrIn

func (q *Query) OrIn(f string, v ...interface{}) string

Or (In: Check whether a value is within a set of values)

func (*Query) OrLe

func (q *Query) OrLe(f string, v interface{}) string

Or (Less than or equal)

func (*Query) OrLike

func (q *Query) OrLike(f string, v interface{}) string

Or (Like: Simple pattern matching)

func (*Query) OrLt

func (q *Query) OrLt(f string, v interface{}) string

Or (Less than)

func (*Query) OrNe

func (q *Query) OrNe(f string, v interface{}) string

Or (Not equal)

func (*Query) OrderAsc

func (q *Query) OrderAsc(f ...string) *Query

Order ASC

func (*Query) OrderDesc

func (q *Query) OrderDesc(f ...string) *Query

Order DESC

func (*Query) OuterJoin

func (q *Query) OuterJoin(tb string) *Query

Join Outer

func (*Query) Parse

func (q *Query) Parse(c Condition) *Query

Parse

func (*Query) RightJoin

func (q *Query) RightJoin(tb string) *Query

Join Right

func (*Query) Row

func (q *Query) Row(ptr interface{}, d ...Where) error

Row

func (*Query) Rows

func (q *Query) Rows(ptr interface{}, d ...Where) error

Rows

func (*Query) Select

func (q *Query) Select(f ...string) *Query

Select fields

func (*Query) Set

func (q *Query) Set(f string, v interface{}) *Query

Set(Update)

func (*Query) SetPrimary

func (q *Query) SetPrimary(p string)

Set primary field

func (*Query) ToString

func (q *Query) ToString() string

Connect all sql part to a corect sql string.

func (*Query) Update

func (q *Query) Update(tb string) *Query

Update

func (*Query) Values

func (q *Query) Values(vs ...interface{}) *Query

Values(Insert)

func (*Query) Where

func (q *Query) Where(qs ...string) *Query

Where

type Result

type Result struct {
	// LastInsertId returns the integer generated by the database
	// in response to a command. Typically this will be from an
	// "auto increment" column when inserting a new row. Not all
	// databases support this feature, and the syntax of such
	// statements varies.
	LastInsertId int64

	// RowsAffected returns the number of rows affected by an
	// update, insert, or delete. Not every database or database
	// driver may support this.
	RowsAffected int64
}

A Result summarizes an executed SQL command.

type Server

type Server struct {
	// Database type: mysql postgresql or sqlite3
	Type string `json:"type"`

	// Data Source Name
	DSN string `json:"dsn"`
}

Server struct

func Connect

func Connect(typ, dsn string) *Server

New Server, alias of NewServer.

func NewServer

func NewServer(typ, dsn string) *Server

New Server

func (*Server) Close

func (e *Server) Close()

func (*Server) DeleteFrom

func (e *Server) DeleteFrom(tb string) *Query

Delete from

func (*Server) Exec

func (e *Server) Exec(sql string, args ...interface{}) (sql.Result, error)

Execute query, only return sql.Result

func (*Server) InsertInto

func (e *Server) InsertInto(tb string) *Query

Insert into

func (*Server) NewQuery

func (e *Server) NewQuery() *Query

New query

func (*Server) Row

func (e *Server) Row(ptr interface{}, sql string, args ...interface{}) error

Get row.

func (*Server) Rows

func (e *Server) Rows(ptr interface{}, sql string, args ...interface{}) error

Get all rows

func (*Server) Select

func (e *Server) Select(f ...string) *Query

Select

func (*Server) Update

func (e *Server) Update(tb string) *Query

Update

type Table

type Table struct {
	// Table name
	Name string

	// Table primary
	Primary string

	// All fields, except primary
	Fields []string

	// Fields for select, include primary
	SelectFields []string

	// Fields for add
	AddFields []string

	// Fields for update
	UpdateFields []string

	// json and field property map
	FiledsMap map[string]string

	// Entity type
	EntityType reflect.Type
}

Table struct

func NewTable

func NewTable(tableName string, entity interface{}) *Table

New Table

type Where

type Where map[string]interface{}

Alias of map[string]interface{}

Jump to

Keyboard shortcuts

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