Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jmoiron/modl
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
  • Loading branch information
jmoiron committed Aug 23, 2014
2 parents 80e598e + c952508 commit 729532c
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 164 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Modl: Go database model & mapping

[![Build Status](https://drone.io/github.com/jmoiron/modl/status.png)](https://drone.io/github.com/jmoiron/modl/latest)
[![Godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/jmoiron/modl)

Modl is a library which provides database modelling and mapping. It is heavily based
on the work in James Cooper's wonderful [gorp](http://github.com/coopernurse/gorp).
Modl is a library which provides database modelling and mapping. It is a fork of
James Cooper's wonderful [gorp](http://github.com/coopernurse/gorp).

**Note**. Modl's public facing interface is considered unfinished and open to
change. The current API will not be broken lightly, but additions are likely.
Expand Down Expand Up @@ -70,6 +71,11 @@ you want to run the benchmarks, as described below.

The original README.md follows:

## API Stability

The API of Modl has been quite stable since its conception. Changes to the API
are avoided as much as possible but there is currently no promise of forward or
backward compatibility.

## Supported Databases

Expand All @@ -87,8 +93,7 @@ The test suite is continuously run against all of these databases.

## Documentation

TODO. API Documentation will go onto [godoc](http://godoc.org) when the API
has stabilized.
API Documentation is available on [godoc](http://godoc.org).

## Performance ##

Expand Down
27 changes: 11 additions & 16 deletions dbmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (m *DbMap) Get(dest interface{}, keys ...interface{}) error {
}

// Select runs an arbitrary SQL query, binding the columns in the result
// to fields on the struct specified by i. args represent the bind
// to fields on the struct specified by dest. args represent the bind
// parameters for the SQL statement.
//
// Column names on the SELECT statement should be aliased to the field names
Expand All @@ -328,8 +328,14 @@ func (m *DbMap) Get(dest interface{}, keys ...interface{}) error {
// and nil returned.
//
// i does NOT need to be registered with AddTable()
func (m *DbMap) Select(i interface{}, query string, args ...interface{}) error {
return hookedselect(m, m, i, query, args...)
func (m *DbMap) Select(dest interface{}, query string, args ...interface{}) error {
return hookedselect(m, m, dest, query, args...)
}

// SelectOne runs an arbitrary SQL Query, binding the columns in the result to
// fields on the struct specified by dest.
func (m *DbMap) SelectOne(dest interface{}, query string, args ...interface{}) error {
return hookedget(m, m, dest, query, args...)
}

// Exec runs an arbitrary SQL statement. args represent the bind parameters.
Expand Down Expand Up @@ -437,19 +443,8 @@ func (m *DbMap) truncateTables(restartIdentity bool) error {
return nil
}

func (m *DbMap) queryRow(query string, args ...interface{}) *sql.Row {
m.trace(query, args)
return m.Db.QueryRow(query, args...)
}

func (m *DbMap) queryRowx(query string, args ...interface{}) *sqlx.Row {
m.trace(query, args)
return m.Dbx.QueryRowx(query, args...)
}

func (m *DbMap) query(query string, args ...interface{}) (*sql.Rows, error) {
m.trace(query, args)
return m.Db.Query(query, args...)
func (m *DbMap) handle() handle {
return &tracingHandle{h: m.Dbx, d: m}
}

func (m *DbMap) trace(query string, args ...interface{}) {
Expand Down
18 changes: 9 additions & 9 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Dialect interface {
CreateTableSuffix() string

// InsertAutoIncr
InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error)
InsertAutoIncr(e SqlExecutor, insertSql string, params ...interface{}) (int64, error)

// BindVar returns the variable string to use when forming SQL statements
// in many dbs it is "?", but Postgres requires '$#'
Expand All @@ -57,8 +57,8 @@ type Dialect interface {
DriverName() string
}

func standardInsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
res, err := exec.Exec(insertSql, params...)
func standardInsertAutoIncr(e SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
res, err := e.handle().Exec(insertSql, params...)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -135,8 +135,8 @@ func (d SqliteDialect) BindVar(i int) string {
}

// InsertAutoIncr runs the standard
func (d SqliteDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(exec, insertSql, params...)
func (d SqliteDialect) InsertAutoIncr(e SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(e, insertSql, params...)
}

// QuoteField quotes f with "" for sqlite
Expand Down Expand Up @@ -241,8 +241,8 @@ func (d PostgresDialect) BindVar(i int) string {

// InsertAutoIncr inserts via a query and reads the resultant rows for the new
// auto increment ID, as it's not returned with the result in PostgreSQL.
func (d PostgresDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
rows, err := exec.query(insertSql, params...)
func (d PostgresDialect) InsertAutoIncr(e SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
rows, err := e.handle().Queryx(insertSql, params...)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -355,8 +355,8 @@ func (d MySQLDialect) BindVar(i int) string {

// InsertAutoIncr runs the standard Insert Exec, which uses LastInsertId to get
// the value of the auto increment column.
func (d MySQLDialect) InsertAutoIncr(exec SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(exec, insertSql, params...)
func (d MySQLDialect) InsertAutoIncr(e SqlExecutor, insertSql string, params ...interface{}) (int64, error) {
return standardInsertAutoIncr(e, insertSql, params...)
}

// QuoteField quotes f using ``.
Expand Down
51 changes: 51 additions & 0 deletions handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package modl

import (
"database/sql"

"github.com/jmoiron/sqlx"
)

// a cursor is either an sqlx.Db or an sqlx.Tx
type handle interface {
Select(dest interface{}, query string, args ...interface{}) error
Get(dest interface{}, query string, args ...interface{}) error
Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
QueryRowx(query string, args ...interface{}) *sqlx.Row
Exec(query string, args ...interface{}) (sql.Result, error)
/*
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
*/
}

// an implmentation of handle which traces using dbmap
type tracingHandle struct {
d *DbMap
h handle
}

func (t *tracingHandle) Select(dest interface{}, query string, args ...interface{}) error {
t.d.trace(query, args...)
return t.h.Select(dest, query, args...)
}

func (t *tracingHandle) Get(dest interface{}, query string, args ...interface{}) error {
t.d.trace(query, args...)
return t.h.Get(dest, query, args...)
}

func (t *tracingHandle) Queryx(query string, args ...interface{}) (*sqlx.Rows, error) {
t.d.trace(query, args...)
return t.h.Queryx(query, args...)
}

func (t *tracingHandle) QueryRowx(query string, args ...interface{}) *sqlx.Row {
t.d.trace(query, args...)
return t.h.QueryRowx(query, args...)
}

func (t *tracingHandle) Exec(query string, args ...interface{}) (sql.Result, error) {
t.d.trace(query, args...)
return t.h.Exec(query, args...)
}
Loading

0 comments on commit 729532c

Please sign in to comment.