Skip to content

Commit

Permalink
Merge branch 'master' of github.com:markbates/pop into pop
Browse files Browse the repository at this point in the history
# Conflicts:
#	executors.go
  • Loading branch information
u007 committed Oct 20, 2017
2 parents 0e667ff + ad183b2 commit 808ada3
Show file tree
Hide file tree
Showing 24 changed files with 130 additions and 47 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## [v3.39.2](https://github.com/markbates/pop/tree/v3.39.2) (2017-10-20)
[Full Changelog](https://github.com/markbates/pop/compare/v3.39.1...v3.39.2)

**Merged pull requests:**

- Handle remaining golint issues [\#144](https://github.com/markbates/pop/pull/144) ([stanislas-m](https://github.com/stanislas-m))
- \[MySQL\] Use SQL instead of mysql client for create and drop DB [\#142](https://github.com/markbates/pop/pull/142) ([stanislas-m](https://github.com/stanislas-m))
- Fix some golint issues [\#141](https://github.com/markbates/pop/pull/141) ([stanislas-m](https://github.com/stanislas-m))

## [v3.39.1](https://github.com/markbates/pop/tree/v3.39.1) (2017-10-17)
[Full Changelog](https://github.com/markbates/pop/compare/v3.39.0...v3.39.1)

Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func init() {
LoadConfigFile()
}

// LoadConfigFile loads a POP config file from the configured lookup paths
func LoadConfigFile() error {
path, err := findConfigPath()
if err != nil {
Expand All @@ -48,10 +49,12 @@ func LoadConfigFile() error {
return LoadFrom(f)
}

// LookupPaths returns the current configuration lookup paths
func LookupPaths() []string {
return lookupPaths
}

// AddLookupPaths add paths to the current lookup paths list
func AddLookupPaths(paths ...string) error {
lookupPaths = append(paths, lookupPaths...)
return LoadConfigFile()
Expand Down
6 changes: 6 additions & 0 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func (c *Connection) String() string {
return c.URL()
}

// URL returns the datasource connection string
func (c *Connection) URL() string {
return c.Dialect.URL()
}

// MigrationURL returns the datasource connection string used for running the migrations
func (c *Connection) MigrationURL() string {
return c.Dialect.MigrationURL()
}
Expand Down Expand Up @@ -80,6 +82,7 @@ func Connect(e string) (*Connection, error) {
return c, errors.Wrapf(err, "couldn't open connection for %s", e)
}

// Open creates a new datasource connection
func (c *Connection) Open() error {
if c.Store != nil {
return nil
Expand All @@ -92,6 +95,7 @@ func (c *Connection) Open() error {
return errors.Wrap(err, "coudn't connection to database")
}

// Close destroys an active datasource connection
func (c *Connection) Close() error {
return errors.Wrap(c.Store.Close(), "couldn't close connection")
}
Expand Down Expand Up @@ -120,6 +124,7 @@ func (c *Connection) Transaction(fn func(tx *Connection) error) error {

}

// NewTransaction starts a new transaction on the connection
func (c *Connection) NewTransaction() (*Connection, error) {
var cn *Connection
if c.TX == nil {
Expand Down Expand Up @@ -166,6 +171,7 @@ func (c *Connection) Q() *Query {
return Q(c)
}

// TruncateAll truncates all data from the datasource
func (c *Connection) TruncateAll() error {
return c.Dialect.TruncateAll(c)
}
Expand Down
3 changes: 3 additions & 0 deletions connection_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
)

// ConnectionDetails stores the data needed to connect to a datasource
type ConnectionDetails struct {
// Example: "postgres" or "sqlite3" or "mysql"
Dialect string
Expand Down Expand Up @@ -114,6 +115,7 @@ func (cd *ConnectionDetails) Parse(port string) error {
return cd.Finalize()
}

// RetrySleep returns the amount of time to wait between two connection retries
func (cd *ConnectionDetails) RetrySleep() time.Duration {
d, err := time.ParseDuration(defaults.String(cd.Options["retry_sleep"], "1ms"))
if err != nil {
Expand All @@ -122,6 +124,7 @@ func (cd *ConnectionDetails) RetrySleep() time.Duration {
return d
}

// RetryLimit returns the maximum number of accepted connection retries
func (cd *ConnectionDetails) RetryLimit() int {
i, err := strconv.Atoi(defaults.String(cd.Options["retry_limit"], "1000"))
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"io"

. "github.com/markbates/pop/columns"
"github.com/markbates/pop/columns"
"github.com/markbates/pop/fizz"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
Expand All @@ -20,8 +20,8 @@ type dialect interface {
MigrationURL() string
Details() *ConnectionDetails
TranslateSQL(string) string
Create(store, *Model, Columns) error
Update(store, *Model, Columns) error
Create(store, *Model, columns.Columns) error
Update(store, *Model, columns.Columns) error
Destroy(store, *Model) error
SelectOne(store, *Model, Query) error
SelectMany(store, *Model, Query) error
Expand All @@ -34,7 +34,7 @@ type dialect interface {
TruncateAll(*Connection) error
}

func genericCreate(s store, model *Model, cols Columns) error {
func genericCreate(s store, model *Model, cols columns.Columns) error {
keyType := model.PrimaryKeyType()
switch keyType {
case "int", "int64":
Expand Down Expand Up @@ -75,7 +75,7 @@ func genericCreate(s store, model *Model, cols Columns) error {
return errors.Errorf("can not use %s as a primary key type!", keyType)
}

func genericUpdate(s store, model *Model, cols Columns) error {
func genericUpdate(s store, model *Model, cols columns.Columns) error {
stmt := fmt.Sprintf("UPDATE %s SET %s where %s", model.TableName(), cols.Writeable().UpdateString(), model.whereID())
Log(stmt)
_, err := s.NamedExec(stmt, model.Value)
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
So what does Pop do exactly? Well, it wraps the absolutely amazing https://github.com/jmoiron/sqlx library. It cleans up some of the common patterns and workflows usually associated with dealing with databases in Go.
Package pop wraps the absolutely amazing https://github.com/jmoiron/sqlx library. It cleans up some of the common patterns and workflows usually associated with dealing with databases in Go.
Pop makes it easy to do CRUD operations, run migrations, and build/execute queries. Is Pop an ORM? I'll leave that up to you, the reader, to decide.
Expand Down
21 changes: 18 additions & 3 deletions executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package pop
import (
"fmt"

. "github.com/markbates/pop/columns"
"github.com/markbates/pop/columns"
"github.com/markbates/validate"
uuid "github.com/satori/go.uuid"
)

// Reload fetch fresh data for a given model, using its ID
func (c *Connection) Reload(model interface{}) error {
sm := Model{Value: model}
return c.Find(model, sm.ID())
}

// Exec runs the given query
func (q *Query) Exec() error {
return q.Connection.timeFunc("Exec", func() error {
sql, args := q.ToSQL(nil)
Expand All @@ -37,6 +39,8 @@ func (q *Query) ExecWithCount() (int, error) {
})
}

// ValidateAndSave applies validation rules on the given entry, then save it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndSave(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateSave(c)
Expand All @@ -51,6 +55,8 @@ func (c *Connection) ValidateAndSave(model interface{}, excludeColumns ...string

var emptyUUID = uuid.Nil.String()

// Save wraps the Create and Update methods. It executes a Create if no ID is provided with the entry;
// or issues an Update otherwise.
func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
sm := &Model{Value: model}
id := sm.ID()
Expand All @@ -61,6 +67,8 @@ func (c *Connection) Save(model interface{}, excludeColumns ...string) error {
return c.Update(model, excludeColumns...)
}

// ValidateAndCreate applies validation rules on the given entry, then creates it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateCreate(c)
Expand All @@ -73,6 +81,8 @@ func (c *Connection) ValidateAndCreate(model interface{}, excludeColumns ...stri
return verrs, c.Create(model, excludeColumns...)
}

// Create add a new given entry to the database, excluding the given columns.
// It updates `created_at` and `updated_at` columns automatically.
func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return c.timeFunc("Create", func() error {
var err error
Expand All @@ -86,7 +96,7 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
return err
}

cols := ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols := columns.ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols.Remove(excludeColumns...)

sm.touchCreatedAt()
Expand All @@ -104,6 +114,8 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
})
}

// ValidateAndUpdate applies validation rules on the given entry, then update it
// if the validation succeed, excluding the given columns.
func (c *Connection) ValidateAndUpdate(model interface{}, excludeColumns ...string) (*validate.Errors, error) {
sm := &Model{Value: model}
verrs, err := sm.validateUpdate(c)
Expand All @@ -116,6 +128,8 @@ func (c *Connection) ValidateAndUpdate(model interface{}, excludeColumns ...stri
return verrs, c.Update(model, excludeColumns...)
}

// Update writes changes from an entry to the database, excluding the given columns.
// It updates the `updated_at` column automatically.
func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return c.timeFunc("Update", func() error {
var err error
Expand All @@ -128,7 +142,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
return err
}

cols := ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols := columns.ColumnsForStructWithAlias(model, sm.TableName(), sm.As)
cols.Remove("id", "created_at")
cols.Remove(excludeColumns...)

Expand All @@ -145,6 +159,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
})
}

// Destroy deletes a given entry from the database
func (c *Connection) Destroy(model interface{}) error {
return c.timeFunc("Destroy", func() error {
var err error
Expand Down
3 changes: 3 additions & 0 deletions finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (q Query) Count(model interface{}) (int, error) {
return q.CountByField(model, "*")
}

// CountByField counts the number of records in the database, for a given field.
//
// q.Where("sex = ?", "f").Count(&User{}, "name")
func (q Query) CountByField(model interface{}, field string) (int, error) {
tmpQuery := Q(q.Connection)
q.Clone(tmpQuery) //avoid mendling with original query
Expand Down
1 change: 1 addition & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
)

// GroupClause holds the field to apply the GROUP clause on
type GroupClause struct {
Field string
}
Expand Down
1 change: 1 addition & 0 deletions having.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// HavingClause defines a condition and its arguments for a HAVING clause
type HavingClause struct {
Condition string
Arguments []interface{}
Expand Down
1 change: 1 addition & 0 deletions migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/pkg/errors"
)

// MigrationCreate writes contents for a given migration in normalized files
func MigrationCreate(path, name, ext string, up, down []byte) error {
n := time.Now().UTC()
s := n.Format("20060102150405")
Expand Down
2 changes: 2 additions & 0 deletions migration_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pop

import "github.com/pkg/errors"

// Migration handles the data for a given database migration
type Migration struct {
// Path to the migration (./migrations/123_create_widgets.up.sql)
Path string
Expand All @@ -26,6 +27,7 @@ func (mf Migration) Run(c *Connection) error {
return mf.Runner(mf, c)
}

// Migrations is a collection of Migration
type Migrations []Migration

func (mfs Migrations) Len() int {
Expand Down
2 changes: 2 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func MapTableName(name string, tableName string) {
tableMap[name] = tableName
}

// Value is the contents of a `Model`.
type Value interface{}

// Model is used throughout Pop to wrap the end user interface
Expand All @@ -54,6 +55,7 @@ func (m *Model) ID() interface{} {
return fbn.Interface()
}

// PrimaryKeyType gives the primary key type of the `Model`.
func (m *Model) PrimaryKeyType() string {
fbn, err := m.fieldByName("ID")
if err != nil {
Expand Down
Loading

0 comments on commit 808ada3

Please sign in to comment.