golangdb

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

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

Go to latest
Published: Jan 29, 2015 License: MIT Imports: 10 Imported by: 0

README

Golang DB

This package is a simple abstraction I use to allow my projects to connect to various databases through a single interface.

Version 0.3

MySQL
import (
    "log"

    "github.com/adampresley/golangdb"
)

connection := DatabaseConnection{
    Engine:   golangdb.MYSQL,
    Address:  "localhost",
    Port:     3306,
    Database: "test",
    UserName: "user",
    Password: "password",
}

if err := connection.Connect("main"); err != nil {
    log.Fatalf("Error connecting to database: %s", err.Error())
}

// Database handle now lives in golangdb.Db["main"]
MSSQL
import (
    "log"

    "github.com/adampresley/golangdb"
)

connection := DatabaseConnection{
    Engine:   golangdb.MSSQL,
    Address:  "localhost",
    Port:     1433,
    Database: "test",
    UserName: "user",
    Password: "password",
}

if err := connection.Connect("main"); err != nil {
    log.Fatalf("Error connecting to database: %s", err.Error())
}

// Database handle now lives in golangdb.Db["main"]
SQLite
import (
    "log"

    "github.com/adampresley/golangdb"
)

connection := DatabaseConnection{
    Engine:   golangdb.SQLITE,
    Database: "./test.db",
}

if err := connection.Connect("main"); err != nil {
    log.Fatalf("Error connecting to database: %s", err.Error())
}

// Database handle now lives in golangdb.Db["main"]
TestDB
import (
    "log"

    "github.com/adampresley/golangdb"
)

connection := DatabaseConnection{
    Engine: golangdb.TESTDB,
}

if err := connection.Connect("test"); err != nil {
    log.Fatalf("Error connecting to database: %s", err.Error())
}

// Database handle now lives in golangdb.Db["test"]

History

  • 2014-09-29
    • Supports multiple connections by name
    • Added support for go-testdb
  • 2014-09-17
    • Initial release

License

The MIT License (MIT)

Copyright (c) 2014 Adam Presley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Structures representing a database connection and methods for configuring and connecting to a MySQL database.

Index

Constants

This section is empty.

Variables

View Source
var Db = make(map[string]*sql.DB)

Global database connection handle used throughout your application

Functions

func ConnectMSSQL

func ConnectMSSQL(connectionInfo *DatabaseConnection) (*sql.DB, error)

This method establishes a connection to a MSSQL database server. The connection details are stored and sent via a DatabaseConnection structure.

func ConnectMySQL

func ConnectMySQL(connectionInfo *DatabaseConnection) (*sql.DB, error)

This method establishes a connection to a MySQL database server. The connection details are stored and sent via a DatabaseConnection structure. Please note that this method respects the environment variable max_connections, and will default to "151" if it is not present.

func ConnectSQLite

func ConnectSQLite(connectionInfo *DatabaseConnection) (*sql.DB, error)

func ConnectTestDB

func ConnectTestDB(connectionInfo *DatabaseConnection) (*sql.DB, error)

This method establishes a fake database connection. The connection details are stored and sent via a DatabaseConnection structure.

func DropSQLite

func DropSQLite(connectionInfo *DatabaseConnection)

Types

type DatabaseConnection

type DatabaseConnection struct {
	Engine   DatabaseEngine
	Address  string
	Port     int
	Database string
	UserName string
	Password string
}

DatabaseConnection contains data necessary to establish a connection to a database server. Most fields are useful for standard SQL servers. The DatabaseFile field is used by SQLite.

func (*DatabaseConnection) Connect

func (this *DatabaseConnection) Connect(connectionName string) error

Connect to a database. This method will use the specified engine to determine how to connect.

func (*DatabaseConnection) ToString

func (this *DatabaseConnection) ToString() string

This method converts a DatabaseConnection structure into a string appropriate for connecting to a database server.

type DatabaseEngine

type DatabaseEngine int

Defines a type of database engine

const (
	NONE DatabaseEngine = iota
	SQLITE
	MYSQL
	MSSQL
	TESTDB
)

Constants used to specify a type of database connection

func GetDatabaseEngineFromName

func GetDatabaseEngineFromName(name string) DatabaseEngine

func (*DatabaseEngine) ToString

func (this *DatabaseEngine) ToString() string

Jump to

Keyboard shortcuts

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