-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,81 @@ | ||
package dbx | ||
|
||
import "database/sql" | ||
import "errors" | ||
|
||
type TableModel interface { | ||
TableName() string | ||
} | ||
|
||
type ModelQuery struct { | ||
builder Builder | ||
model *structValue | ||
builder Builder | ||
model *structValue | ||
exclude []string | ||
lastError error | ||
} | ||
|
||
func newModelQuery(model interface{}, fieldMapFunc FieldMapFunc, builder Builder) *ModelQuery { | ||
sv := newStructValue(model, fieldMapFunc) | ||
if sv == nil { | ||
// todo: log error | ||
} | ||
return &ModelQuery{ | ||
q := &ModelQuery{ | ||
builder: builder, | ||
model: sv, | ||
} | ||
if sv == nil { | ||
q.lastError = errors.New("The model must be specified as a pointer to the model struct.") | ||
} | ||
return q | ||
} | ||
|
||
func (q *ModelQuery) Insert(attrs ...string) (sql.Result, error) { | ||
func (q *ModelQuery) Exclude(attrs ...string) *ModelQuery { | ||
q.exclude = attrs | ||
return q | ||
} | ||
|
||
func (q *ModelQuery) Insert(attrs ...string) error { | ||
if q.lastError != nil { | ||
return q.lastError | ||
} | ||
tableName := q.model.tableName | ||
cols := q.model.fields(attrs...) | ||
// todo: fill in PK, remove nil PK | ||
return q.builder.Insert(tableName, Params(cols)).Execute() | ||
cols := q.model.columns(attrs, q.exclude) | ||
pk := q.model.pk() | ||
ai := "" // auto-incremental column | ||
for pkc := range pk { | ||
if col, ok := cols[pkc]; ok { | ||
if col == nil || col == 0 { | ||
delete(cols, pkc) | ||
ai = pkc | ||
} | ||
} | ||
} | ||
|
||
result, err := q.builder.Insert(tableName, Params(cols)).Execute() | ||
if err == nil && ai != "" { | ||
pkValue, err := result.LastInsertId() | ||
if err != nil { | ||
return err | ||
} | ||
q.model.dbNameMap[ai].getField(q.model.value).SetInt(pkValue) | ||
} | ||
return err | ||
} | ||
|
||
func (q *ModelQuery) Update(attrs ...string) (sql.Result, error) { | ||
cols := q.model.fields(attrs...) | ||
func (q *ModelQuery) Update(attrs ...string) error { | ||
if q.lastError != nil { | ||
return q.lastError | ||
} | ||
cols := q.model.columns(attrs, q.exclude) | ||
pk := q.model.pk() | ||
for name := range pk { | ||
delete(cols, name) | ||
} | ||
return q.builder.Update(q.model.tableName, Params(cols), HashExp(pk)).Execute() | ||
_, err := q.builder.Update(q.model.tableName, Params(cols), HashExp(pk)).Execute() | ||
return err | ||
} | ||
|
||
func (q *ModelQuery) Delete() (sql.Result, error) { | ||
pk := HashExp(q.model.pk()) | ||
return q.builder.Delete(q.model.tableName, pk).Execute() | ||
func (q *ModelQuery) Delete() error { | ||
if q.lastError != nil { | ||
return q.lastError | ||
} | ||
pk := q.model.pk() | ||
_, err := q.builder.Delete(q.model.tableName, HashExp(pk)).Execute() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters