simpledb

package module
v0.0.0-...-5e279cd Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2017 License: GPL-2.0 Imports: 2 Imported by: 0

README

simpledb

This library for simple use Databases (It works with drivers for database/sql).

LICENSE

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 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
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

How do I install it?

go get bitbucket.org/ignusius/simpledb

Note

MySQL               PostgreSQL            SQlite

WHERE col = ?       WHERE col = $1        WHERE col = $1 
VALUES(?, ?, ?)     VALUES($1, $2, $3)    WHERE col = ?
                                          VALUES($1, $2, $3) 
										  VALUES(?, ?, ?)

Example SQLite##

example.go

package main

import (
	"fmt"

	"bitbucket.org/ignusius/simpledb"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db := new(simpledb.DB)
	// or db:=simpledb.DB{}
	err := db.NewDatabase("sqlite3", "test.db")
	if err != nil {
		panic(err)
	}

	err = db.Exec("INSERT INTO data values ($1,$2,$3,$4,$5)", 1,"test","test",2,3)
	if err !=nil{
		panic(err)
	}

	arr, err := db.Query("SELECT * FROM data")
	if err != nil {
		panic(err)
	}
	fmt.Println(arr)
	fmt.Println(arr[0][2])

	db.Close()
}

Example PostgeSQL

example.go

package main

import (
	"fmt"

	"bitbucket.org/ignusius/simpledb"
	_ "github.com/lib/pq"
)

func main() {
	db := new(simpledb.DB)
	// or db:=simpledb.DB{}
	err := db.NewDatabase("postgres", `dbname=test user=test password=pass host=localhost port=5432  sslmode=disable`)
	if err != nil {
		panic(err)
	}

	err = db.Exec("INSERT INTO data values ($1,$2,$3,$4,$5)", 1,"test","test",2,3)
	if err !=nil{
		panic(err)
	}

	arr, err := db.Query("SELECT * FROM data")
	if err != nil {
		panic(err)
	}
	fmt.Println(arr)
	fmt.Println(arr[0][2])

	db.Close()
}

Example MySQL

example.go

package main

import (
	"fmt"

	"simpledb"

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

func main() {
	db := new(simpledb.DB)

	// or db:=simpledb.DB{}
	err := db.NewDatabase("mysql", "root:password@tcp(127.0.0.1:3306)/test")
	if err != nil {
		panic(err)
	}
	defer db.Close()

	err = db.Exec("INSERT INTO data values (?,?,?,?,?)", 1, "test", "test", 2, 3)
	if err != nil {
		panic(err)
	}

	arr, err := db.Query("SELECT * FROM data")
	if err != nil {
		panic(err)
	}

	fmt.Println(arr)
	fmt.Println(arr[0][2])

}

Output

[[1 test test 2 3]]
test

Transactions

package main

import (
    "fmt"

    "simpledb"
    _ "github.com/mattn/go-sqlite3"
)

func main() {

    db := new(simpledb.DB)
    // or db:=simpledb.DB{}
    err := db.NewDatabase("sqlite3", "test")
    if err != nil {
        panic(err)
    }
    db.Begin()
    

    db.TxPrepare("INSERT INTO data values ($1,$2,$3,$4,$5)")
    db.StmtExec(1,2,3,4,5)
    db.StmtExec(1,2,3,4,5)
    db.StmtExec(1,2,3,4,5)
    db.StmtExec(1,2,3,4,5)
    db.StmtExec(1,2,3,4,5)
	db.StmtClose()
    //db.Rollback()
 
    db.Commit()
   

    arr, err := db.Query("SELECT * FROM data")
    if err != nil {
        panic(err)
    }
    fmt.Println(arr)

    db.Close()
}

Output

[[1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5] [1 2 3 4 5]]

picture

Contacts

ignusius@gmail.com

Documentation

Overview

Package simpledb for simple use databases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	Db *sql.DB
	// contains filtered or unexported fields
}

DB is structure of database

func (*DB) Begin

func (d *DB) Begin() error

Begin starts a transaction. The default isolation level is dependent on the driver.

func (*DB) Close

func (d *DB) Close()

Close - close database

func (*DB) Commit

func (d *DB) Commit() error

Commit commits the transaction.

func (*DB) Exec

func (d *DB) Exec(exec string, args ...interface{}) error

Exec - for exec to database

func (*DB) NewDatabase

func (d *DB) NewDatabase(database, basename string) error

NewDatabase - create new database

func (*DB) Prepare

func (d *DB) Prepare(exec string) error

Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.

func (*DB) Query

func (d *DB) Query(query string, args ...interface{}) ([][]interface{}, error)

Query - for query from database

func (*DB) Rollback

func (d *DB) Rollback() error

Rollback aborts the transaction.

func (*DB) StmtClose

func (d *DB) StmtClose() error

StmtClose - close stmt

func (*DB) StmtExec

func (d *DB) StmtExec(args ...interface{}) error

StmtExec executes a query that doesn't return rows. StmtExec use in transaction.

func (*DB) TxPrepare

func (d *DB) TxPrepare(exec string) error

TxPrepare creates a prepared statement for use within a transaction.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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