Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ It is designed to satisfy a CRUD-centered record management system,
filling the following contract:

```go
type Recorder interface {
Bind(string, Record) Recorder // link struct to table
Interface() interface{} // Get the struct that has been linked
Insert() error // INSERT just one record
Update() error // UPDATE just one record
Delete() error // DELETE just one record
Exists() (bool, error) // Check for just one record
ExistsWhere(cond interface{}, args ...interface{}) (bool, error)
Load() error // SELECT just one record
LoadWhere(cond interface{}, args ...interface{}) error // Alternate Load()
}
type Recorder interface {
Bind(string, Record) Recorder // link struct to table
Interface() interface{} // Get the struct that has been linked
Insert() error // INSERT just one record
Update() error // UPDATE just one record
Delete() error // DELETE just one record
Exists() (bool, error) // Check for just one record
ExistsWhere(cond interface{}, args ...interface{}) (bool, error)
Load() error // SELECT just one record
LoadWhere(cond interface{}, args ...interface{}) error // Alternate Load()
}
```

Squirrel already provides the ability to perform more complicated
Expand Down Expand Up @@ -60,27 +60,27 @@ Structable works by mapping a struct to columns in a database.
To annotate a struct, you do something like this:

```go
type Stool struct {
Id int `stbl:"id, PRIMARY_KEY, AUTO_INCREMENT"`
Legs int `stbl:"number_of_legs"`
Material string `stbl:"material"`
Ignored string // will not be stored. No tag.
}
type Stool struct {
Id int `stbl:"id, PRIMARY_KEY, AUTO_INCREMENT"`
Legs int `stbl:"number_of_legs"`
Material string `stbl:"material"`
Ignored string // will not be stored. No tag.
}
```

To manage instances of this struct, you do something like this:

```go
stool := new(Stool)
stool.Material = "Wood"
db := getDb() // Get a sql.Db. You're on the hook to do this part.
stool := new(Stool)
stool.Material = "Wood"
db := getDb() // Get a sql.Db. You're on the hook to do this part.

// Create a new structable.Recorder and tell it to
// bind the given struct as a row in the given table.
r := structable.New(db, "mysql").Bind("test_table", stool)
// Create a new structable.Recorder and tell it to
// bind the given struct as a row in the given table.
r := structable.New(db, "mysql").Bind("test_table", stool)

// This will insert the stool into the test_table.
err := r.Insert()
// This will insert the stool into the test_table.
err := r.Insert()
```

And of course you have `Load()`, `Update()`, `Delete()` and so on.
Expand Down Expand Up @@ -110,24 +110,24 @@ of definitions from a table described in a struct named `Table`:
```go
func (s *SchemaInfo) Tables() ([]*Table, error) {

// Bind a new recorder. We use an empty object just to get the field
// data for that struct.
// Bind a new recorder. We use an empty object just to get the field
// data for that struct.
t := &Table{}
st := structable.New(s.Queryer, s.Driver).Bind(t.TableName(), t)

// We want to return no more than 10 of these.
// We want to return no more than 10 of these.
fn := func(d structable.Describer, q squirrel.SelectBuilder) (squirrel.SelectBuilder, error) {
return q.Limit(10), nil
}

// Fetch a list of Table structs.
// Fetch a list of Table structs.
items, err := structable.ListWhere(st, fn)
if err != nil {
return []*Table{}, err
}

// Because we get back a []Recorder, we need to get the original data
// back out. We have to manually convert it back to its real type.
// Because we get back a []Recorder, we need to get the original data
// back out. We have to manually convert it back to its real type.
tables := make([]*Table, len(items))
for i, item := range items {
tables[i] = item.Interface().(*Table)
Expand Down