dbIO

package module
v1.3.6 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: GPL-3.0 Imports: 15 Imported by: 46

README

Build Status GoDoc

dbIO is a lightweight, straightforward MySQL interface written in Go.

These scripts are still under developement, but are available for use.

Copyright 2018 by Shawn Rupp

  1. Dependencies
  2. Usage
  3. Uploading
  4. Extracting

Dependencies:

Prompter

dbIO used Prompter to query the user's MySQL password.

go get github.com/Songmu/prompter  
Golang Mysql driver

Required for using go's sql package with MySQL.

go get github.com/go-sql-driver/mysql  
iotools

dbIO uses iotools to read in database schema template file.

go get github.com/icwells/go-tools/iotools  

Installation

go get github.com/icwells/dbIO  

Usage

dbIO stores relevant connection in a DBIO struct which is returned by the Connect function. Below are some examples of usage. See the GoDoc page for a comprehensive list.

Connect and the DBIO struct
dbIO.Connect(host, database, user, password string) *DBIO  

Attempts to connect to given sql database with given user name. If the password is left blank, it will prompt for a password from the user before storing the start time (for recording program run time).

Returns a DBIO instance containing:

DB        *sql.DB  
Host	  string
Database  string  
User      string  
Password  string  
Starttime time.Time  
Columns   map[string]string  
Creating/Replacing Databases

CreateDatabase can be used to initializes a database with a given name (although NewTables must be called to initialize the tables within the databse).
Similarly, ReplaceDatabase will drop an existing database (if it exists) and re-initialize it (for testing).

dbio.CreateDatabase(host, database, user string)  
dbio.ReplaceDatabase(host, database, user, password string)  

Additionally, the Ping function can be used to test credentials:

dbio.Ping(host, database, user, password string)  

It will return true if a connection was successfully established, or false if it was not.

Uploading to a database
DBIO.NewTables(infile string)

This command will read a text file of tables, columns, and types and initialize new tables if they do not already exist.

The input file should contain a series of valid sql CREATE TABLE commands, with each command seperated by a single space and ending with a semicolon (;). E.G.:

''' CREATE TABLE IF NOT EXISTS Accounts ( account_id INT PRIMARY KEY, Account TEXT, submitter_name TEXT; INDEX (account_id) );

CREATE TABLE IF NOT EXISTS Update_time ( update_number INT PRIMARY KEY AUTO_INCREMENT, Time TEXT ); '''

DBIO.ReadColumns(infile string)

Reads in tables and columns from input file (see above) and stores in DBIO.Columns. The column types and any additional column descriptors will be stored for creating tables.

DBIO.GetTableColumns()

Retrieves names tables and their columns from an existing database and stores in Columns map.

Formatting data for upload
dbIO.FormatMap(data map[string][]string) (string, int)  
dbIO.FormatSlice(data [][]string) (string, int)  

These functions will format a map or slice of string slices into a comma/parentheses seperated string for upload to a database:

[][]string{{"5, "Apple"}, {"3", "Orange"},}  

becomes

"('5','Apple'),('3','Orange')"  

They both return a string of the data and an integer of the number rows that were formatted. Both are stand-alone functions and do not use a DBIO struct.
The input data should contian the same number of columns as the table is to be uploaded to. (Map keys are not included in the upload.)

DBIO.UpdateDB(table, values string, l int) int

UpdateDB uploads a formatted string of data (see previous functions) to a given table. It will print the number of rows uploaded (given with l).
It returns an integer (rather than a boolean) so multiple results can be tallied if needed.

Extracting from a database
DBIO.GetRows(table, column, key, target string) [][]string

Returns rows of target columns with key in column. Use "*" for target to select entire row or a comma seperated string of column names for multiple columns.

DBIOEvaluateRows(table, column, op, key, target string) [][]string

Returns rows of target column(s) same as GetRows, except it compares key to the column value using the given operator (>=/=/...; ie. column >= 7).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatMap

func FormatMap(data map[string][]string) (string, int)

FormatMap converts a map of string slices to a string formatted with parentheses, commas, and appostrophe's where needed. Returns the number of rows formatted.

func FormatSlice

func FormatSlice(data [][]string) (string, int)

FormatSlice converts a two-dimensional string slice to a string formatted with parentheses, commas, and appostrophe's where needed. Returns the number of rows formatted.

func Ping added in v1.2.1

func Ping(host, database, user, password string) bool

Ping returns true if the given credentials are valid, and discards the connection.

Types

type DBIO

type DBIO struct {
	// DB is the database connection. Any SQL query can be run directly using DB.
	DB *sql.DB
	// Host is the host IP.
	Host string
	// Database stores the name of the database.
	Database string
	// User is the MySQL user name used in this session.
	User string
	// Password stores the user's password. It is not secure, so be careful how you use it.
	Password string
	// Starttime is the time point after the password is given.
	Starttime time.Time
	// Columns stores a map with a comma-seperated string of column name for each table.
	Columns map[string]string
	// contains filtered or unexported fields
}

DBIO is the central struct containing all releveant connection information.

func Connect

func Connect(host, database, user, password string) (*DBIO, error)

Connect attempts to connect to the MySQL database located at host/database using the given user name and password.

func CreateDatabase

func CreateDatabase(host, database, user string) *DBIO

CreateDatabase connects to MySQL and creates a new database.

func NewDBIO added in v1.2.1

func NewDBIO(host, database, user, password string) *DBIO

NewDBIO returns an initialized struct. If host is left blank, it will default to localHost.

func ReplaceDatabase

func ReplaceDatabase(host, database, user, password string) *DBIO

ReplaceDatabase deletes the given database and creates a new, empty, one (for testing).

func (*DBIO) BackupDB added in v1.3.1

func (d *DBIO) BackupDB(outdir string)

BackupDB calls mysldump to back up database to local machine

func (*DBIO) ColumnContains added in v1.2.5

func (d *DBIO) ColumnContains(table, column, value, target string) [][]string

ColumnContains returns a 2D string slice from table if value is in column.

func (*DBIO) Count

func (d *DBIO) Count(table, column, target, op, key string, distinct bool) int

Count returns count of entries from target column(s) in table where key relates to column via op (>=/=/...; ie. column >= 7). Returns total if distinct is false; returns number of unique entries if distinct is true. Give operator, key, and target as emtpy strings to count without evaluating.

func (*DBIO) CountRows

func (d *DBIO) CountRows(table string) int

CountRows returns the number of rows from the given table.

func (*DBIO) DeleteRow

func (d *DBIO) DeleteRow(table, column, value string)

DeleteRow deletes a single row from the database where the value in the given column equals value.

func (*DBIO) DeleteRows added in v1.2.1

func (d *DBIO) DeleteRows(table, column string, values []string)

DeleteRows deletes rows from the database if the value in the given column is contained in the values slice.

func (*DBIO) EvaluateRows

func (d *DBIO) EvaluateRows(table, column, op, key, target string) [][]string

EvaluateRows returns rows of columns where key relates to target via op (>=/=/...) (i.e. column <= key).

func (*DBIO) Execute added in v1.2.1

func (d *DBIO) Execute(cmd string) [][]string

Execute submits the given command as a MySQL query.

func (*DBIO) GetColumnInt

func (d *DBIO) GetColumnInt(table, column string) []int

GetColumnInt returns a slice of all entries in column of integers.

func (*DBIO) GetColumnText

func (d *DBIO) GetColumnText(table, column string) []string

GetColumnText returns a slice of all entries in column of text.

func (*DBIO) GetColumns

func (d *DBIO) GetColumns(table string, columns []string) [][]string

GetColumns returns a slice of slices of all entries in given columns.

func (*DBIO) GetMax

func (d *DBIO) GetMax(table, column string) int

GetMax returns the highest number from the given column.

func (*DBIO) GetNumOccurances

func (d *DBIO) GetNumOccurances(table, column string) map[string]int

GetNumOccurances returns a map with the number of unique entries in column.

func (*DBIO) GetRows

func (d *DBIO) GetRows(table, column, key, target string) [][]string

GetRows returns rows of target columns with key in column.

func (*DBIO) GetRowsMin

func (d *DBIO) GetRowsMin(table, column, target string, min int) [][]string

GetRowsMin returns all rows of target columns where column >= key.

func (*DBIO) GetTable

func (d *DBIO) GetTable(table string) [][]string

GetTable returns all contents of the given table.

func (*DBIO) GetTableColumns

func (d *DBIO) GetTableColumns()

GetTableColumns extracts table and column names from the database and stores them in the Columns map.

func (*DBIO) GetTableMap

func (d *DBIO) GetTableMap(table string) map[string][]string

GetTableMap returns the given table as a map with the first column as the key.

func (*DBIO) GetUpdateTimes added in v1.2.2

func (d *DBIO) GetUpdateTimes() map[string]time.Time

GetUpdateTimes returns a map the last update date and time for each table.

func (*DBIO) Insert added in v1.2.4

func (d *DBIO) Insert(table, command string) error

Insert executes the given INSERT command

func (*DBIO) LastUpdate added in v1.2.2

func (d *DBIO) LastUpdate() time.Time

LastUpdate returns the time of the most recent update.

func (*DBIO) NewTables

func (d *DBIO) NewTables(infile string)

NewTables executes new table commands from infile. See README for infile formatting.

func (*DBIO) OptimizeTables added in v1.3.3

func (d *DBIO) OptimizeTables()

OptimizeTables calls optimize on all tables in the database.

func (*DBIO) ReadColumns

func (d *DBIO) ReadColumns(infile string) []string

ReadColumns builds a map of column statements with types from infile. See README for infile formatting.

func (*DBIO) TruncateTable

func (d *DBIO) TruncateTable(table string)

TruncateTable clears all content from the given table.

func (*DBIO) UpdateColumns added in v1.2.1

func (d *DBIO) UpdateColumns(table, idcol string, values map[string]map[string]string) bool

UpdateColumns updates columns (specified as outer map key) in table where column == inner map key with map values. Returns true if successful.

func (*DBIO) UpdateDB

func (d *DBIO) UpdateDB(table, values string, l int) int

UpdateDB adds new rows to table. Values must be formatted using FormatMap or FormatSlice.

func (*DBIO) UpdateRow

func (d *DBIO) UpdateRow(table, target, value, column, op, key string) bool

UpdateRow updates a single column in the given table and returns true if successful.

func (*DBIO) UploadSlice added in v1.2.6

func (d *DBIO) UploadSlice(table string, values [][]string) error

UploadSlice formats two-dimensional string slice for upload to database and splits uploads into chunks if it exceeds SQL size limit.

Jump to

Keyboard shortcuts

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