gorose

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

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

Go to latest
Published: Apr 10, 2018 License: MIT Imports: 7 Imported by: 0

README

Gorose ORM

GoDoc Go Report Card Gitter gorose-orm

Official Website | (官方网站)

https://gohouse.github.io/gorose

Documentation
What is Gorose?

Gorose, a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developers and python or ruby developers.
Currently provides five major database drivers:

Quick Preview
// select * from users where id=1 limit 1
db.Table("users").Where("id",1).First()
// select id as uid,name,age from users where id=1 order by id desc limit 10
db.Table("users").Where("id",1).Fields("id as uid,name,age").Order("id desc").Limit(10).Get()

// query string
db.Query("select * from user limit 10")
db.Execute("update users set name='fizzday' where id=?", 1)
Features
  • Chain Operation
  • Connection Pool
Requirement
  • Golang 1.6+
  • Glide (optional, dependencies management for golang)
Installation
  • standard:
go get -u github.com/gohouse/gorose
glide get github.com/gohouse/gorose
Base Usage
package main

import (
	"github.com/gohouse/gorose"        //import Gorose
	_ "github.com/go-sql-driver/mysql" //import DB driver
	"fmt"
)

// DB Config.(Recommend to use configuration file to import)
var DbConfig = map[string]interface{}{
	// Default database configuration
	"Default": "mysql_dev",
	// (Connection pool) Max open connections, default value 0 means unlimit.
	"SetMaxOpenConns": 300,
	// (Connection pool) Max idle connections, default value is 1.
	"SetMaxIdleConns": 10,

	// Define the database configuration character "mysql_dev".
	"Connections":map[string]map[string]string{
		"mysql_dev": map[string]string{
                    "host":     "192.168.200.248",
                    "username": "gcore",
                    "password": "gcore",
                    "port":     "3306",
                    "database": "test",
                    "charset":  "utf8",
                    "protocol": "tcp",
                    "prefix":   "",      // Table prefix
                    "driver":   "mysql", // Database driver(mysql,sqlite,postgres,oracle,mssql)
                },
	},
}

func main() {
	connection, err := gorose.Open(DbConfig)
	if err != nil {
		fmt.Println(err)
		return
	}
	// close DB
	defer connection.Close()
	
	db := connection.GetInstance()

	res,err := db.Table("users").First()
	if err != nil {
    		fmt.Println(err)
    		return
    }
	fmt.Println(res)
}

For more usage, please read the Documentation.

License

MIT

Contribution
release notes

0.9.0

  • new seperate db instance

0.8.2

  • improve config format, new config format support file config like json/toml etc.

0.8.1

  • improve multi connection and nulti transation

0.8.0

  • add connection pool
  • adjust dir for open source standard
  • add glide version control
  • translate for english and chinese docment

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDB

func GetDB() *sql.DB

GetDB is get origin *sql.DB

Types

type Connection

type Connection struct {
	// all config sets
	DbConfig map[string]interface{}
	// default database
	Default string
	// current config on use
	CurrentConfig map[string]string
	//// all sql logs
	//SqlLog []string
	//// if in transaction, the code auto change
	//Trans bool
	// max open connections
	SetMaxOpenConns int
	// max freedom connections leave
	SetMaxIdleConns int
}

Connection is the database pre handle

var (
	// DB is origin DB
	DB *sql.DB
	// Tx is transaction DB
	//Tx *sql.Tx
	// Connect is the Connection Object
	Connect Connection
)

func Open

func Open(args ...interface{}) (Connection, error)

Open instance of sql.DB.Oper if args has 1 param , it will be derect connection or with default config set if args has 2 params , the second param will be the default dirver key

func (*Connection) Close

func (conn *Connection) Close() error

Close database

func (*Connection) Execute

func (conn *Connection) Execute(args ...interface{}) (int64, error)

Execute str

func (*Connection) GetInstance

func (conn *Connection) GetInstance() *Database

GetInstance , get the database object

func (*Connection) JsonEncode

func (conn *Connection) JsonEncode(arg interface{}) string

JsonEncode : parse json

func (*Connection) Ping

func (conn *Connection) Ping() error

Ping db

func (*Connection) Query

func (conn *Connection) Query(args ...interface{}) ([]map[string]interface{}, error)

Query str

func (*Connection) Table

func (conn *Connection) Table(table string) *Database

Table is set table from database

type Database

type Database struct {
	RowsAffected int // insert affected rows
	LastInsertId int // insert last insert id

	SqlLogs []string
	LastSql string
	// contains filtered or unexported fields
}

Database is data mapper struct

func (*Database) Avg

func (dba *Database) Avg(avg string) (interface{}, error)

Avg : select avg field

func (*Database) Begin

func (dba *Database) Begin()

func (*Database) Chunk

func (dba *Database) Chunk(limit int, callback func([]map[string]interface{}))

Chunk : select chunk more data to piceses block

func (*Database) Commit

func (dba *Database) Commit()

func (*Database) Count

func (dba *Database) Count(args ...interface{}) (int, error)

Count : select count rows

func (*Database) Data

func (dba *Database) Data(data interface{}) *Database

Data : insert or update data

func (*Database) Delete

func (dba *Database) Delete() (int, error)

Delete : delete data

func (*Database) Distinct

func (dba *Database) Distinct() *Database

Distinct : select distinct

func (*Database) Execute

func (dba *Database) Execute(args ...interface{}) (int64, error)

Execute : query instance of sql.DB.Execute

func (*Database) Fields

func (dba *Database) Fields(fields string) *Database

Fields : select fields

func (*Database) First

func (dba *Database) First(args ...interface{}) (map[string]interface{}, error)

First : select one row

func (*Database) Get

func (dba *Database) Get() ([]map[string]interface{}, error)

Get : select more rows , relation limit set

func (*Database) Group

func (dba *Database) Group(group string) *Database

Group : select group by

func (*Database) Having

func (dba *Database) Having(having string) *Database

Having : select having

func (*Database) Insert

func (dba *Database) Insert() (int, error)

Insert : insert data

func (*Database) Join

func (dba *Database) Join(args ...interface{}) *Database

Join : select join query

func (*Database) JsonEncode

func (dba *Database) JsonEncode(data interface{}) string

JsonEncode : parse json

func (*Database) LeftJoin

func (dba *Database) LeftJoin(args ...interface{}) *Database

LeftJoin : like join , the relation is left

func (*Database) Limit

func (dba *Database) Limit(limit int) *Database

Limit : select limit

func (*Database) Max

func (dba *Database) Max(max string) (interface{}, error)

Max : select max field

func (*Database) Min

func (dba *Database) Min(min string) (interface{}, error)

Min : select min field

func (*Database) Offset

func (dba *Database) Offset(offset int) *Database

Offset : select offset

func (*Database) OrWhere

func (dba *Database) OrWhere(args ...interface{}) *Database

OrWhere : like where , but the relation is or,

func (*Database) Order

func (dba *Database) Order(order string) *Database

Order : select order by

func (*Database) Page

func (dba *Database) Page(page int) *Database

Page : select page

func (*Database) Query

func (dba *Database) Query(args ...interface{}) ([]map[string]interface{}, error)

Query : query instance of sql.DB.Query

func (*Database) Reset

func (dba *Database) Reset()

Reset : reset union select

func (*Database) RightJoin

func (dba *Database) RightJoin(args ...interface{}) *Database

RightJoin : like join , the relation is right

func (*Database) Rollback

func (dba *Database) Rollback()

func (*Database) Sum

func (dba *Database) Sum(sum string) (interface{}, error)

Sum : select sum field

func (*Database) Table

func (dba *Database) Table(table string) *Database

Table : select table

func (*Database) Transaction

func (dba *Database) Transaction(closure func() error) bool
func (dba *Database) LastSql() string {
	if len(Connect.SqlLog) > 0 {
		return Connect.SqlLog[len(Connect.SqlLog)-1:][0]
	}
	return ""
}
func (dba *Database) SqlLogs() []string {
	return Connect.SqlLog
}

*

  • simple transaction

func (*Database) Update

func (dba *Database) Update() (int, error)

Update : update data

func (*Database) Value

func (dba *Database) Value(arg string) (interface{}, error)

Value : select one field's value

func (*Database) Where

func (dba *Database) Where(args ...interface{}) *Database

Where : query or execute where condition, the relation is and

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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