sqlpw

package
Version: v0.0.0-...-a579f18 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2016 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Wrapper for an SQL database backend

This can be used to use an existing database for input output.

There are constructors for MySQL and PostgreSQL, as well as a generic constructor that allow you to specify your own queries.

See "mysql_test.go" and "postgres_test.go" for examples on how to create those.

Note that passwords are truncated at 64 runes (not bytes), so your schema/table column must support that.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Sql

type Sql struct {
	TxBulk bool // Do bulk inserts with a transaction.
	// contains filtered or unexported fields
}

Sql can be used for adding and checking passwords. Insert and Query are generated for MySQL, and should very likely be changed for other databases. See the "postgres_test" for an example.

func New

func New(db *sql.DB, query, insert string) *Sql

New returns a new database.

You must give an query, that returns the number of rows matching the parameter given.

You must give an insert statement that will insert the password given. It must be able to insert the same password multiple times without returning an error.

You should manually enable bulk transactions modifying the TxBulk variable on the returned object if your database/driver supports it.

func NewMysql

func NewMysql(db *sql.DB, schema, column string) *Sql

NewMysql returns a new database wrapper, set up for MySQL.

You must supply a schema (that should already exist), as well as the column the passwords should be inserted into.

Example

Example of using a MySQL database.

This assumes "test-table" has been created in "testdb". It could have been created like this:

	CREATE TABLE `testdb`.`test-table` (
   `Pass` VARCHAR(64) NOT NULL,
   PRIMARY KEY (`Pass`),
   UNIQUE INDEX `Pass_UNIQUE` (`Pass` ASC)
 ) DEFAULT CHARACTER SET = utf8;
db, err := sql.Open("mysql", "travis:@/testdb")
if err != nil {
	panic("MySQL connect error " + err.Error())
}

d := NewMysql(db, "test-table", "Pass")

// Test it
err = drivers.TestDriver(d)
if err != nil {
	panic(err)
}
Output:

func NewPostgresql

func NewPostgresql(db *sql.DB, table, column string) *Sql

NewPostgresql returns a new database wrapper, set up for PostgreSQL.

You must supply a "schema.table" (that should already exist), as well as the column the passwords should be inserted into.

Example

Example of using a Postgres database

Uses 'pwtesttable' in the 'testschema' schema, and reads/adds to the "pass" column.

Table can be created like this:

`CREATE TABLE ` + table + ` ("`+ column +`" VARCHAR(64) PRIMARY KEY);`

For Postgres to ignore duplicate inserts, you can use a rule like this:

`CREATE OR REPLACE RULE db_table_ignore_duplicate_inserts AS
    ON INSERT TO ` + table + `
    WHERE (EXISTS (
        SELECT 1
        FROM ` + table + `
        WHERE ` + table + `.` + column + ` = NEW.` + column + `
    )
) DO INSTEAD NOTHING;`
db, err := sql.Open("postgres", "user=postgres dbname=postgres sslmode=disable")
if err != nil {
	panic("Postgres connect error " + err.Error())
}
table := "testschema.pwtesttable"

d := NewPostgresql(db, table, "pass")

// Test it
err = drivers.TestDriver(d)
if err != nil {
	panic(err)
}
Output:

func (*Sql) Add

func (m *Sql) Add(s string) error

Add an entry to the password database

func (*Sql) AddMultiple

func (m *Sql) AddMultiple(s []string) error

Add multiple entries to the password database

func (*Sql) Has

func (m *Sql) Has(s string) (bool, error)

Has will return true if the database has the entry.

Source Files

Jump to

Keyboard shortcuts

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