Commit 992aacda authored by Jason Moiron's avatar Jason Moiron

make all changes to documentation and code suggested by golint

parent b18fac1d
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package modl provides a non-declarative database modelling layer to ease
// the use of frequently repeated patterns in database-backed applications
// and centralize database use to ease profiling and reporting.
//
// It is a fork of the wonderful github.com/coopernurse/gorp package, but is
// rewritten to use github.com/jmoiron/sqlx as a base.
//
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
//
package modl
......@@ -35,7 +42,7 @@ type DbMap struct {
logPrefix string
}
// Return a new DbMap using the db connection and dialect
// NewDbMap returns a new DbMap using the db connection and dialect
func NewDbMap(db *sql.DB, dialect Dialect) *DbMap {
return &DbMap{Db: db, Dialect: dialect, Dbx: sqlx.NewDb(db, dialect.DriverName())}
}
......@@ -74,6 +81,7 @@ func (m *DbMap) AddTable(i interface{}, name ...string) *TableMap {
}
t := reflect.TypeOf(i)
// Use sqlx's NameMapper function if no name is supplied
if len(Name) == 0 {
Name = sqlx.NameMapper(t.Name())
}
......@@ -117,6 +125,7 @@ func (m *DbMap) AddTable(i interface{}, name ...string) *TableMap {
}
// AddTableWithName adds a new mapping of the interface to a table name.
func (m *DbMap) AddTableWithName(i interface{}, name string) *TableMap {
return m.AddTable(i, name)
}
......@@ -340,7 +349,11 @@ func (m *DbMap) Begin() (*Transaction, error) {
return &Transaction{m, tx}, nil
}
// Returns any matching tables for the interface i or nil if not found
// FIXME: This is a poor interface. Checking for nils is un-go-like, and this
// function should be TableFor(i interface{}) (*TableMap, error)
// FIXME: rewrite this in terms of sqlx's reflect helpers
// TableFor returns any matching tables for the interface i or nil if not found
// If i is a slice, then the table is given for the base slice type
func (m *DbMap) TableFor(i interface{}) *TableMap {
var t reflect.Type
......@@ -361,7 +374,9 @@ start:
return m.TableForType(t)
}
// Returns any matching tables for the type t or nil if not found
// FIXME: returning a nil pointer is not go-like; return (*TableMap, err) instead.
// TableForType returns any matching tables for the type t or nil if not found
func (m *DbMap) TableForType(t reflect.Type) *TableMap {
for _, table := range m.tables {
if table.gotype == t {
......@@ -371,12 +386,13 @@ func (m *DbMap) TableForType(t reflect.Type) *TableMap {
return nil
}
// Truncate all tables in the DbMap
// TruncateTables truncates all tables in the DbMap
func (m *DbMap) TruncateTables() error {
return m.truncateTables(false)
}
// Truncate all tables in the DbMap and reset identity counter
// TruncateTablesIdentityRestart truncates all tables in the DbMap and
// resets the identity counter.
func (m *DbMap) TruncateTablesIdentityRestart() error {
return m.truncateTables(true)
}
......@@ -390,8 +406,12 @@ func (m *DbMap) truncateTables(restartIdentity bool) error {
restartClause = m.Dialect.RestartIdentityClause(table.TableName)
}
// if the restart clause exists and starts with ';', then assume it's an
// additional query to run after we truncate. This is true with MySQL and
// SQLite, which do not have extra clauses for this during table truncation.
if len(restartClause) > 0 && restartClause[0] == ';' {
_, err = m.Exec(fmt.Sprintf("%s %s;", m.Dialect.TruncateClause(), m.Dialect.QuoteField(table.TableName)))
_, err = m.Exec(fmt.Sprintf("%s %s;", m.Dialect.TruncateClause(),
m.Dialect.QuoteField(table.TableName)))
if err != nil {
return err
}
......
This diff is collapsed.
......@@ -4,30 +4,44 @@ import (
"reflect"
)
// PreInserter is an interface used to determine if a table type implements
// a PreInsert hook
type PreInserter interface {
PreInsert(SqlExecutor) error
}
// PostInserter is an interface used to determine if a table type implements
// a PostInsert hook
type PostInserter interface {
PostInsert(SqlExecutor) error
}
// PostGetter is an interface used to determine if a table type implements
// a PostGet hook
type PostGetter interface {
PostGet(SqlExecutor) error
}
// PreUpdater is an interface used to determine if a table type implements
// a PreUpdate hook
type PreUpdater interface {
PreUpdate(SqlExecutor) error
}
// PostUpdater is an interface used to determine if a table type implements
// a PostUpdate hook
type PostUpdater interface {
PostUpdate(SqlExecutor) error
}
// PreDeleter is an interface used to determine if a table type implements
// a PreDelete hook
type PreDeleter interface {
PreDelete(SqlExecutor) error
}
// PostDeleter is an interface used to determine if a table type implements
// a PostDelete hook
type PostDeleter interface {
PostDelete(SqlExecutor) error
}
......
package modl
// Changes Copyright 2013 Jason Moiron. Original Gorp code
// Copyright 2012 James Cooper. All rights reserved.
//
......@@ -6,20 +8,22 @@
//
// Source code and project home:
// https://github.com/jmoiron/modl
//
package modl
import (
"database/sql"
"fmt"
"github.com/jmoiron/sqlx"
"reflect"
"github.com/jmoiron/sqlx"
)
// NoKeysErr is a special error type returned when modl's CRUD helpers are
// used on tables which have not been set up with a primary key.
type NoKeysErr struct {
Table *TableMap
}
// Error returns the string representation of a NoKeysError.
func (n NoKeysErr) Error() string {
return fmt.Sprintf("Could not find keys for table %v", n.Table)
}
......@@ -137,44 +141,44 @@ type Transaction struct {
tx *sqlx.Tx
}
// Same behavior as DbMap.Insert(), but runs in a transaction
// Insert has the same behavior as DbMap.Insert(), but runs in a transaction.
func (t *Transaction) Insert(list ...interface{}) error {
return insert(t.dbmap, t, list...)
}
// Same behavior as DbMap.Update(), but runs in a transaction
// Update has the same behavior as DbMap.Update(), but runs in a transaction.
func (t *Transaction) Update(list ...interface{}) (int64, error) {
return update(t.dbmap, t, list...)
}
// Same behavior as DbMap.Delete(), but runs in a transaction
// Delete has the same behavior as DbMap.Delete(), but runs in a transaction.
func (t *Transaction) Delete(list ...interface{}) (int64, error) {
return delete(t.dbmap, t, list...)
}
// Same behavior as DbMap.Get(), but runs in a transaction
// Get has the Same behavior as DbMap.Get(), but runs in a transaction.
func (t *Transaction) Get(dest interface{}, keys ...interface{}) error {
return get(t.dbmap, t, dest, keys...)
}
// Same behavior as DbMap.Select(), but runs in a transaction
// Select has the Same behavior as DbMap.Select(), but runs in a transaction.
func (t *Transaction) Select(dest interface{}, query string, args ...interface{}) error {
return hookedselect(t.dbmap, t, dest, query, args...)
}
// Same behavior as DbMap.Exec(), but runs in a transaction
// Exec has the same behavior as DbMap.Exec(), but runs in a transaction.
func (t *Transaction) Exec(query string, args ...interface{}) (sql.Result, error) {
t.dbmap.trace(query, args)
return t.tx.Exec(query, args...)
}
// Commits the underlying database transaction
// Commit commits the underlying database transaction.
func (t *Transaction) Commit() error {
t.dbmap.trace("commit;")
return t.tx.Commit()
}
// Rolls back the underlying database transaction
// Rollback rolls back the underlying database transaction.
func (t *Transaction) Rollback() error {
t.dbmap.trace("rollback;")
return t.tx.Rollback()
......@@ -246,7 +250,7 @@ func get(m *DbMap, exec SqlExecutor, dest interface{}, keys ...interface{}) erro
table := m.TableFor(dest)
if table == nil {
return fmt.Errorf("Could not find table for %v", dest)
return fmt.Errorf("could not find table for %v", dest)
}
if len(table.Keys) < 1 {
return &NoKeysErr{table}
......
This diff is collapsed.
......@@ -3,8 +3,9 @@ package modl
import (
"bytes"
"fmt"
"github.com/jmoiron/sqlx"
"reflect"
"github.com/jmoiron/sqlx"
)
// TableMap represents a mapping between a Go struct and a database table
......@@ -326,16 +327,17 @@ func (c *ColumnMap) SetTransient(b bool) *ColumnMap {
return c
}
// If true " unique" will be added to create table statements for this
// column
// SetUnique sets the unqiue clause for this column. If true, a unique clause
// will be added to create table statements for this column.
func (c *ColumnMap) SetUnique(b bool) *ColumnMap {
c.Unique = b
return c
}
// Set the column's sql type. This is a string, such as 'varchar(32)' or
// 'text', which will be used by CreateTable and nothing else. It is the
// caller's responsibility to ensure this will map cleanly to the struct
// SetSqlType sets an override for the column's sql type. This is a string,
// such as 'varchar(32)' or 'text', which will be used by CreateTable and
// nothing else. It is the caller's responsibility to ensure this will map
// cleanly to the underlying struct field via rows.Scan
func (c *ColumnMap) SetSqlType(t string) *ColumnMap {
c.sqltype = t
return c
......@@ -354,12 +356,12 @@ func (c *ColumnMap) SetMaxSize(size int) *ColumnMap {
func tableForPointer(m *DbMap, i interface{}, checkPk bool) (*TableMap, reflect.Value, error) {
v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
return nil, v, fmt.Errorf("Value %v not a pointer", v)
return nil, v, fmt.Errorf("value %v not a pointer", v)
}
v = v.Elem()
t := m.TableForType(v.Type())
if t == nil {
return nil, v, fmt.Errorf("Could not find table for %v", t)
return nil, v, fmt.Errorf("could not find table for %v", t)
}
if checkPk && len(t.Keys) < 1 {
return t, v, &NoKeysErr{t}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment