package models
import "fmt"
import "time"
import "database/sql"
//go:generate marlowc -input author.go
// Example file for testing.
const (
// AuthorImported indicates the author was imported from an external source.
AuthorImported = 1 << iota
// AuthorHasMultipleTitles is a convenience value indicating the author has multiple titles.
AuthorHasMultipleTitles
)
// Author represents an author of a book.
type Author struct {
table bool `marlow:"tableName=authors"`
ID int `marlow:"column=system_id&autoIncrement=true"`
Name string `marlow:"column=name"`
UniversityID sql.NullInt64 `marlow:"column=university_id"`
ReaderRating float64 `marlow:"column=rating"`
AuthorFlags uint8 `marlow:"column=flags&bitmask"`
Birthday time.Time `marlow:"column=birthday"`
}
func (a *Author) String() string {
return fmt.Sprintf("%v (born %v)", a.Name, a.Birthday.Format(time.RFC1123))
}
// Code auto-generated by marlow
package models
import "fmt"
import "bytes"
import "strings"
import "database/sql"
import "time"
import "io"
import "os"
// [marlow] createable
func (a *authorStore) CreateAuthors(_records ...Author) (int64, error) {
if len(_records) == 0 {
return 0, nil
}
_placeholderList := make([]string, 0, len(_records))
_valueList := make([]interface{}, 0, len(_records))
for _, _record := range _records {
_placeholders := []string{"?", "?", "?", "?", "?"}
_valueList = append(_valueList, _record.AuthorFlags, _record.Birthday, _record.Name, _record.ReaderRating, _record.UniversityID)
_placeholderList = append(_placeholderList, fmt.Sprintf("(%s)", strings.Join(_placeholders, ",")))
}
_query := new(bytes.Buffer)
fmt.Fprintf(_query, "INSERT INTO authors (flags,birthday,name,rating,university_id) VALUES %s;", strings.Join(_placeholderList, ", "))
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _query, _valueList)
_statement, _e := a.Prepare(_query.String())
if _e != nil {
return -1, _e
}
defer _statement.Close()
_result, _execError := _statement.Exec(_valueList...)
if _execError != nil {
return -1, _execError
}
_affectedResult, _affectedError := _result.LastInsertId()
return _affectedResult, _affectedError
}
// [marlow] updater method for flags
func (a *authorStore) AddAuthorAuthorFlags(_updates uint8, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET flags = flags | %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for flags
func (a *authorStore) DropAuthorAuthorFlags(_updates uint8, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET flags = flags & ~%s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for flags
func (a *authorStore) UpdateAuthorAuthorFlags(_updates uint8, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET flags = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for birthday
func (a *authorStore) UpdateAuthorBirthday(_updates time.Time, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET birthday = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for system_id
func (a *authorStore) UpdateAuthorID(_updates int, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET system_id = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for name
func (a *authorStore) UpdateAuthorName(_updates string, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET name = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for university_id
func (a *authorStore) UpdateAuthorUniversityID(_updates *sql.NullInt64, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET university_id = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for rating
func (a *authorStore) UpdateAuthorReaderRating(_updates float64, _blueprint *AuthorBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE authors SET rating = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := a.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] deleteable
func (a *authorStore) DeleteAuthors(_blueprint *AuthorBlueprint) (int64, error) {
if _blueprint == nil || _blueprint.String() == "" {
return -1, fmt.Errorf("deletion blueprints must generate limiting clauses")
}
_query := fmt.Sprintf("DELETE FROM authors %s", _blueprint)
_statement, _e := a.Prepare(_query + ";")
if _e != nil {
return -1, _e
}
defer _statement.Close()
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _query, _blueprint.Values())
_execResult, _e := _statement.Exec(_blueprint.Values()...)
if _e != nil {
return -1, _e
}
_count, _e := _execResult.RowsAffected()
if _e != nil {
return -1, _e
}
return _count, nil
}
// [marlow feature]: finder on table[authors]
func (a *authorStore) FindAuthors(_blueprint *AuthorBlueprint) ([]*Author, error) {
_results := make([]*Author, 0)
_queryString := bytes.NewBufferString("SELECT authors.flags,authors.birthday,authors.system_id,authors.name,authors.rating,authors.university_id FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit := 100
if _blueprint != nil && _blueprint.Limit >= 1 {
_limit = _blueprint.Limit
}
_offset := 0
if _blueprint != nil && _blueprint.Offset >= 1 {
_offset = _blueprint.Offset
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
_statement, _se := a.Prepare(_queryString.String())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
for _queryResult.Next() {
var _row Author
if e := _queryResult.Scan(&_row.AuthorFlags, &_row.Birthday, &_row.ID, &_row.Name, &_row.ReaderRating, &_row.UniversityID); e != nil {
return nil, e
}
_results = append(_results, &_row)
}
return _results, nil
}
// [marlow feature]: counter on table[authors]
func (a *authorStore) CountAuthors(_blueprint *AuthorBlueprint) (int, error) {
if _blueprint == nil {
_blueprint = &AuthorBlueprint{}
}
_raw := fmt.Sprintf("SELECT COUNT(*) FROM authors %s;", _blueprint)
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _raw, _blueprint.Values())
_statement, _statementError := a.Prepare(_raw)
if _statementError != nil {
return -1, _statementError
}
defer _statement.Close()
_queryResult, _queryError := _statement.Query(_blueprint.Values()...)
if _queryError != nil {
return -1, _queryError
}
defer _queryResult.Close()
if _queryResult.Next() != true {
return -1, fmt.Errorf("invalid-scan")
}
var _scanResult int
_scanError := _queryResult.Scan(&_scanResult)
if _scanError != nil {
return -1, _scanError
}
return _scanResult, nil
}
// [marlow] field selector for ID (SelectAuthorIDs) [print: AuthorBlueprint]
func (a *authorStore) SelectAuthorIDs(_blueprint *AuthorBlueprint) ([]int, error) {
_results := make([]int, 0)
_queryString := bytes.NewBufferString("SELECT authors.system_id FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := a.Prepare(_queryString.String())
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row int
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for Name (SelectAuthorNames) [print: AuthorBlueprint]
func (a *authorStore) SelectAuthorNames(_blueprint *AuthorBlueprint) ([]string, error) {
_results := make([]string, 0)
_queryString := bytes.NewBufferString("SELECT authors.name FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := a.Prepare(_queryString.String())
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row string
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for UniversityID (SelectAuthorUniversityIDs) [print: AuthorBlueprint]
func (a *authorStore) SelectAuthorUniversityIDs(_blueprint *AuthorBlueprint) ([]sql.NullInt64, error) {
_results := make([]sql.NullInt64, 0)
_queryString := bytes.NewBufferString("SELECT authors.university_id FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := a.Prepare(_queryString.String())
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row sql.NullInt64
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for ReaderRating (SelectAuthorReaderRatings) [print: AuthorBlueprint]
func (a *authorStore) SelectAuthorReaderRatings(_blueprint *AuthorBlueprint) ([]float64, error) {
_results := make([]float64, 0)
_queryString := bytes.NewBufferString("SELECT authors.rating FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := a.Prepare(_queryString.String())
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row float64
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for AuthorFlags (SelectAuthorAuthorFlags) [print: AuthorBlueprint]
func (a *authorStore) SelectAuthorAuthorFlags(_blueprint *AuthorBlueprint) ([]uint8, error) {
_results := make([]uint8, 0)
_queryString := bytes.NewBufferString("SELECT authors.flags FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := a.Prepare(_queryString.String())
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row uint8
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for Birthday (SelectAuthorBirthdays) [print: AuthorBlueprint]
func (a *authorStore) SelectAuthorBirthdays(_blueprint *AuthorBlueprint) ([]time.Time, error) {
_results := make([]time.Time, 0)
_queryString := bytes.NewBufferString("SELECT authors.birthday FROM authors")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := a.Prepare(_queryString.String())
fmt.Fprintf(a.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row time.Time
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
type AuthorBlueprint struct {
UniversityID []sql.NullInt64
ReaderRatingRange []float64
ReaderRating []float64
AuthorFlagsRange []uint8
AuthorFlags []uint8
BirthdayRange []time.Time
Birthday []time.Time
IDRange []int
ID []int
NameLike []string
Name []string
Inclusive bool
Limit int
Offset int
OrderBy string
OrderDirection string
}
// [marlow] type IN clause for "authors.flags"
func (a *AuthorBlueprint) flagsInString(_count int) (string, []interface{}) {
if len(a.AuthorFlags) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(a.AuthorFlags))
_values := make([]interface{}, 0, len(a.AuthorFlags))
for _, _v := range a.AuthorFlags {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("authors.flags IN (%s)", _joined), _values
}
// [marlow] range clause methods for authors.flags
func (a *AuthorBlueprint) flagsRangeString(_count int) (string, []interface{}) {
if len(a.AuthorFlagsRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = a.AuthorFlagsRange[0]
_values[1] = a.AuthorFlagsRange[1]
return "(authors.flags > ? AND authors.flags < ?)", _values
}
// [marlow] type IN clause for "authors.birthday"
func (a *AuthorBlueprint) birthdayInString(_count int) (string, []interface{}) {
if len(a.Birthday) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(a.Birthday))
_values := make([]interface{}, 0, len(a.Birthday))
for _, _v := range a.Birthday {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("authors.birthday IN (%s)", _joined), _values
}
// [marlow] range clause methods for authors.birthday
func (a *AuthorBlueprint) birthdayRangeString(_count int) (string, []interface{}) {
if len(a.BirthdayRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = a.BirthdayRange[0]
_values[1] = a.BirthdayRange[1]
return "(authors.birthday > ? AND authors.birthday < ?)", _values
}
// [marlow] type IN clause for "authors.system_id"
func (a *AuthorBlueprint) system_idInString(_count int) (string, []interface{}) {
if len(a.ID) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(a.ID))
_values := make([]interface{}, 0, len(a.ID))
for _, _v := range a.ID {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("authors.system_id IN (%s)", _joined), _values
}
// [marlow] range clause methods for authors.system_id
func (a *AuthorBlueprint) system_idRangeString(_count int) (string, []interface{}) {
if len(a.IDRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = a.IDRange[0]
_values[1] = a.IDRange[1]
return "(authors.system_id > ? AND authors.system_id < ?)", _values
}
// [marlow] type IN clause for "authors.name"
func (a *AuthorBlueprint) nameInString(_count int) (string, []interface{}) {
if len(a.Name) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(a.Name))
_values := make([]interface{}, 0, len(a.Name))
for _, _v := range a.Name {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("authors.name IN (%s)", _joined), _values
}
// [marlow] string LIKE clause for "authors.name"
func (a *AuthorBlueprint) nameLikeString(_count int) (string, []interface{}) {
if a == nil || a.NameLike == nil || len(a.NameLike) == 0 {
return "", nil
}
_placeholders := make([]string, 0, len(a.NameLike))
_values := make([]interface{}, 0, len(a.NameLike))
for _, _value := range a.NameLike {
_like := "authors.name LIKE ?"
_placeholders = append(_placeholders, _like)
_values = append(_values, _value)
}
_conjunc := " AND "
if a.Inclusive == true {
_conjunc = " OR "
}
return strings.Join(_placeholders, _conjunc), _values
}
// [marlow] type IN clause for "authors.rating"
func (a *AuthorBlueprint) ratingInString(_count int) (string, []interface{}) {
if len(a.ReaderRating) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(a.ReaderRating))
_values := make([]interface{}, 0, len(a.ReaderRating))
for _, _v := range a.ReaderRating {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("authors.rating IN (%s)", _joined), _values
}
// [marlow] range clause methods for authors.rating
func (a *AuthorBlueprint) ratingRangeString(_count int) (string, []interface{}) {
if len(a.ReaderRatingRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = a.ReaderRatingRange[0]
_values[1] = a.ReaderRatingRange[1]
return "(authors.rating > ? AND authors.rating < ?)", _values
}
// [marlow] nullable clause gen for "authors.university_id"
func (a *AuthorBlueprint) university_idInString(_count int) (string, []interface{}) {
if a.UniversityID == nil {
return "", nil
}
if len(a.UniversityID) == 0 {
return "authors.university_id NOT NULL", nil
}
_placeholders := make([]string, 0, len(a.UniversityID))
_values := make([]interface{}, 0, len(a.UniversityID))
for _, _v := range a.UniversityID {
if _v.Valid == false {
return "authors.university_id IS NULL", nil
}
_placeholders = append(_placeholders, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholders, ",")
return fmt.Sprintf("authors.university_id IN (%s)", _joined), _values
}
func (a *AuthorBlueprint) String() string {
_clauses := make([]string, 0, 11)
_count := 1
if _item, _values := a.flagsInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.flagsRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.birthdayInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.birthdayRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.system_idInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.system_idRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.nameInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.nameLikeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.ratingInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.ratingRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := a.university_idInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if len(_clauses) == 0 {
return ""
}
_map := " AND "
if a.Inclusive == true {
_map = " OR "
}
return "WHERE " + strings.Join(_clauses, _map)
}
func (a *AuthorBlueprint) Values() []interface{} {
_clauses := make([]interface{}, 0, 11)
if a == nil {
return nil
}
if _, _item := a.flagsInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.flagsRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.birthdayInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.birthdayRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.system_idInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.system_idRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.nameInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.nameLikeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.ratingInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.ratingRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := a.university_idInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
return _clauses
}
type authorStore struct {
*sql.DB
logger io.Writer
}
func NewAuthorStore(_db *sql.DB, _logger io.Writer) AuthorStore {
if _logger == nil {
_logger, _ = os.Open(os.DevNull)
}
return &authorStore{DB: _db, logger: _logger}
}
type AuthorStore interface {
AddAuthorAuthorFlags(uint8, *AuthorBlueprint) (int64, error)
UpdateAuthorAuthorFlags(uint8, *AuthorBlueprint) (int64, error)
UpdateAuthorName(string, *AuthorBlueprint) (int64, error)
UpdateAuthorUniversityID(*sql.NullInt64, *AuthorBlueprint) (int64, error)
FindAuthors(*AuthorBlueprint) ([]*Author, error)
SelectAuthorUniversityIDs(*AuthorBlueprint) ([]sql.NullInt64, error)
SelectAuthorAuthorFlags(*AuthorBlueprint) ([]uint8, error)
CreateAuthors(...Author) (int64, error)
UpdateAuthorID(int, *AuthorBlueprint) (int64, error)
UpdateAuthorReaderRating(float64, *AuthorBlueprint) (int64, error)
DeleteAuthors(*AuthorBlueprint) (int64, error)
SelectAuthorIDs(*AuthorBlueprint) ([]int, error)
SelectAuthorNames(*AuthorBlueprint) ([]string, error)
SelectAuthorBirthdays(*AuthorBlueprint) ([]time.Time, error)
DropAuthorAuthorFlags(uint8, *AuthorBlueprint) (int64, error)
UpdateAuthorBirthday(time.Time, *AuthorBlueprint) (int64, error)
CountAuthors(*AuthorBlueprint) (int, error)
SelectAuthorReaderRatings(*AuthorBlueprint) ([]float64, error)
}
package models
import "fmt"
import "database/sql"
//go:generate marlowc -input book.go
// Example file for testing.
// Book represents a book in the example application
type Book struct {
table string `marlow:"defaultLimit=10"`
ID int `marlow:"column=system_id&autoIncrement=true"`
Title string `marlow:"column=title"`
AuthorID int `marlow:"column=author"`
SeriesID sql.NullInt64 `marlow:"column=series"`
YearPublished int `marlow:"column=year_published" json:"year_published"`
}
// String returns the book with good info.
func (b *Book) String() string {
return fmt.Sprintf("%s (published in %d)", b.Title, b.YearPublished)
}
// Code auto-generated by marlow
package models
import "fmt"
import "bytes"
import "strings"
import "database/sql"
import "io"
import "os"
// [marlow] createable
func (b *bookStore) CreateBooks(_records ...Book) (int64, error) {
if len(_records) == 0 {
return 0, nil
}
_placeholderList := make([]string, 0, len(_records))
_valueList := make([]interface{}, 0, len(_records))
for _, _record := range _records {
_placeholders := []string{"?", "?", "?", "?"}
_valueList = append(_valueList, _record.AuthorID, _record.SeriesID, _record.Title, _record.YearPublished)
_placeholderList = append(_placeholderList, fmt.Sprintf("(%s)", strings.Join(_placeholders, ",")))
}
_query := new(bytes.Buffer)
fmt.Fprintf(_query, "INSERT INTO books (author,series,title,year_published) VALUES %s;", strings.Join(_placeholderList, ", "))
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _query, _valueList)
_statement, _e := b.Prepare(_query.String())
if _e != nil {
return -1, _e
}
defer _statement.Close()
_result, _execError := _statement.Exec(_valueList...)
if _execError != nil {
return -1, _execError
}
_affectedResult, _affectedError := _result.LastInsertId()
return _affectedResult, _affectedError
}
// [marlow] updater method for system_id
func (b *bookStore) UpdateBookID(_updates int, _blueprint *BookBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE books SET system_id = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := b.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for title
func (b *bookStore) UpdateBookTitle(_updates string, _blueprint *BookBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE books SET title = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := b.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for author
func (b *bookStore) UpdateBookAuthorID(_updates int, _blueprint *BookBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE books SET author = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := b.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for series
func (b *bookStore) UpdateBookSeriesID(_updates *sql.NullInt64, _blueprint *BookBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE books SET series = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := b.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for year_published
func (b *bookStore) UpdateBookYearPublished(_updates int, _blueprint *BookBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := "?"
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE books SET year_published = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := b.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
_values = append(_values, _updates)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] deleteable
func (b *bookStore) DeleteBooks(_blueprint *BookBlueprint) (int64, error) {
if _blueprint == nil || _blueprint.String() == "" {
return -1, fmt.Errorf("deletion blueprints must generate limiting clauses")
}
_query := fmt.Sprintf("DELETE FROM books %s", _blueprint)
_statement, _e := b.Prepare(_query + ";")
if _e != nil {
return -1, _e
}
defer _statement.Close()
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _query, _blueprint.Values())
_execResult, _e := _statement.Exec(_blueprint.Values()...)
if _e != nil {
return -1, _e
}
_count, _e := _execResult.RowsAffected()
if _e != nil {
return -1, _e
}
return _count, nil
}
// [marlow feature]: finder on table[books]
func (b *bookStore) FindBooks(_blueprint *BookBlueprint) ([]*Book, error) {
_results := make([]*Book, 0)
_queryString := bytes.NewBufferString("SELECT books.author,books.system_id,books.series,books.title,books.year_published FROM books")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit := 10
if _blueprint != nil && _blueprint.Limit >= 1 {
_limit = _blueprint.Limit
}
_offset := 0
if _blueprint != nil && _blueprint.Offset >= 1 {
_offset = _blueprint.Offset
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
_statement, _se := b.Prepare(_queryString.String())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
for _queryResult.Next() {
var _row Book
if e := _queryResult.Scan(&_row.AuthorID, &_row.ID, &_row.SeriesID, &_row.Title, &_row.YearPublished); e != nil {
return nil, e
}
_results = append(_results, &_row)
}
return _results, nil
}
// [marlow feature]: counter on table[books]
func (b *bookStore) CountBooks(_blueprint *BookBlueprint) (int, error) {
if _blueprint == nil {
_blueprint = &BookBlueprint{}
}
_raw := fmt.Sprintf("SELECT COUNT(*) FROM books %s;", _blueprint)
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _raw, _blueprint.Values())
_statement, _statementError := b.Prepare(_raw)
if _statementError != nil {
return -1, _statementError
}
defer _statement.Close()
_queryResult, _queryError := _statement.Query(_blueprint.Values()...)
if _queryError != nil {
return -1, _queryError
}
defer _queryResult.Close()
if _queryResult.Next() != true {
return -1, fmt.Errorf("invalid-scan")
}
var _scanResult int
_scanError := _queryResult.Scan(&_scanResult)
if _scanError != nil {
return -1, _scanError
}
return _scanResult, nil
}
// [marlow] field selector for SeriesID (SelectBookSeriesIDs) [print: BookBlueprint]
func (b *bookStore) SelectBookSeriesIDs(_blueprint *BookBlueprint) ([]sql.NullInt64, error) {
_results := make([]sql.NullInt64, 0)
_queryString := bytes.NewBufferString("SELECT books.series FROM books")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 10, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := b.Prepare(_queryString.String())
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row sql.NullInt64
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for YearPublished (SelectBookYearPublisheds) [print: BookBlueprint]
func (b *bookStore) SelectBookYearPublisheds(_blueprint *BookBlueprint) ([]int, error) {
_results := make([]int, 0)
_queryString := bytes.NewBufferString("SELECT books.year_published FROM books")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 10, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := b.Prepare(_queryString.String())
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row int
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for ID (SelectBookIDs) [print: BookBlueprint]
func (b *bookStore) SelectBookIDs(_blueprint *BookBlueprint) ([]int, error) {
_results := make([]int, 0)
_queryString := bytes.NewBufferString("SELECT books.system_id FROM books")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 10, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := b.Prepare(_queryString.String())
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row int
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for Title (SelectBookTitles) [print: BookBlueprint]
func (b *bookStore) SelectBookTitles(_blueprint *BookBlueprint) ([]string, error) {
_results := make([]string, 0)
_queryString := bytes.NewBufferString("SELECT books.title FROM books")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 10, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := b.Prepare(_queryString.String())
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row string
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for AuthorID (SelectBookAuthorIDs) [print: BookBlueprint]
func (b *bookStore) SelectBookAuthorIDs(_blueprint *BookBlueprint) ([]int, error) {
_results := make([]int, 0)
_queryString := bytes.NewBufferString("SELECT books.author FROM books")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 10, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := b.Prepare(_queryString.String())
fmt.Fprintf(b.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row int
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
type BookBlueprint struct {
YearPublishedRange []int
YearPublished []int
IDRange []int
ID []int
TitleLike []string
Title []string
AuthorIDRange []int
AuthorID []int
SeriesID []sql.NullInt64
Inclusive bool
Limit int
Offset int
OrderBy string
OrderDirection string
}
// [marlow] type IN clause for "books.author"
func (b *BookBlueprint) authorInString(_count int) (string, []interface{}) {
if len(b.AuthorID) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(b.AuthorID))
_values := make([]interface{}, 0, len(b.AuthorID))
for _, _v := range b.AuthorID {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("books.author IN (%s)", _joined), _values
}
// [marlow] range clause methods for books.author
func (b *BookBlueprint) authorRangeString(_count int) (string, []interface{}) {
if len(b.AuthorIDRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = b.AuthorIDRange[0]
_values[1] = b.AuthorIDRange[1]
return "(books.author > ? AND books.author < ?)", _values
}
// [marlow] type IN clause for "books.system_id"
func (b *BookBlueprint) system_idInString(_count int) (string, []interface{}) {
if len(b.ID) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(b.ID))
_values := make([]interface{}, 0, len(b.ID))
for _, _v := range b.ID {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("books.system_id IN (%s)", _joined), _values
}
// [marlow] range clause methods for books.system_id
func (b *BookBlueprint) system_idRangeString(_count int) (string, []interface{}) {
if len(b.IDRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = b.IDRange[0]
_values[1] = b.IDRange[1]
return "(books.system_id > ? AND books.system_id < ?)", _values
}
// [marlow] nullable clause gen for "books.series"
func (b *BookBlueprint) seriesInString(_count int) (string, []interface{}) {
if b.SeriesID == nil {
return "", nil
}
if len(b.SeriesID) == 0 {
return "books.series NOT NULL", nil
}
_placeholders := make([]string, 0, len(b.SeriesID))
_values := make([]interface{}, 0, len(b.SeriesID))
for _, _v := range b.SeriesID {
if _v.Valid == false {
return "books.series IS NULL", nil
}
_placeholders = append(_placeholders, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholders, ",")
return fmt.Sprintf("books.series IN (%s)", _joined), _values
}
// [marlow] type IN clause for "books.title"
func (b *BookBlueprint) titleInString(_count int) (string, []interface{}) {
if len(b.Title) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(b.Title))
_values := make([]interface{}, 0, len(b.Title))
for _, _v := range b.Title {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("books.title IN (%s)", _joined), _values
}
// [marlow] string LIKE clause for "books.title"
func (b *BookBlueprint) titleLikeString(_count int) (string, []interface{}) {
if b == nil || b.TitleLike == nil || len(b.TitleLike) == 0 {
return "", nil
}
_placeholders := make([]string, 0, len(b.TitleLike))
_values := make([]interface{}, 0, len(b.TitleLike))
for _, _value := range b.TitleLike {
_like := "books.title LIKE ?"
_placeholders = append(_placeholders, _like)
_values = append(_values, _value)
}
_conjunc := " AND "
if b.Inclusive == true {
_conjunc = " OR "
}
return strings.Join(_placeholders, _conjunc), _values
}
// [marlow] type IN clause for "books.year_published"
func (b *BookBlueprint) year_publishedInString(_count int) (string, []interface{}) {
if len(b.YearPublished) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(b.YearPublished))
_values := make([]interface{}, 0, len(b.YearPublished))
for _, _v := range b.YearPublished {
_placeholder = append(_placeholder, "?")
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("books.year_published IN (%s)", _joined), _values
}
// [marlow] range clause methods for books.year_published
func (b *BookBlueprint) year_publishedRangeString(_count int) (string, []interface{}) {
if len(b.YearPublishedRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = b.YearPublishedRange[0]
_values[1] = b.YearPublishedRange[1]
return "(books.year_published > ? AND books.year_published < ?)", _values
}
func (b *BookBlueprint) String() string {
_clauses := make([]string, 0, 9)
_count := 1
if _item, _values := b.authorInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.authorRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.system_idInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.system_idRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.seriesInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.titleInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.titleLikeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.year_publishedInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := b.year_publishedRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if len(_clauses) == 0 {
return ""
}
_map := " AND "
if b.Inclusive == true {
_map = " OR "
}
return "WHERE " + strings.Join(_clauses, _map)
}
func (b *BookBlueprint) Values() []interface{} {
_clauses := make([]interface{}, 0, 9)
if b == nil {
return nil
}
if _, _item := b.authorInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.authorRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.system_idInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.system_idRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.seriesInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.titleInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.titleLikeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.year_publishedInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := b.year_publishedRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
return _clauses
}
type bookStore struct {
*sql.DB
logger io.Writer
}
func NewBookStore(_db *sql.DB, _logger io.Writer) BookStore {
if _logger == nil {
_logger, _ = os.Open(os.DevNull)
}
return &bookStore{DB: _db, logger: _logger}
}
type BookStore interface {
UpdateBookTitle(string, *BookBlueprint) (int64, error)
FindBooks(*BookBlueprint) ([]*Book, error)
SelectBookSeriesIDs(*BookBlueprint) ([]sql.NullInt64, error)
SelectBookIDs(*BookBlueprint) ([]int, error)
SelectBookAuthorIDs(*BookBlueprint) ([]int, error)
UpdateBookID(int, *BookBlueprint) (int64, error)
UpdateBookAuthorID(int, *BookBlueprint) (int64, error)
UpdateBookYearPublished(int, *BookBlueprint) (int64, error)
DeleteBooks(*BookBlueprint) (int64, error)
SelectBookYearPublisheds(*BookBlueprint) ([]int, error)
SelectBookTitles(*BookBlueprint) ([]string, error)
CreateBooks(...Book) (int64, error)
UpdateBookSeriesID(*sql.NullInt64, *BookBlueprint) (int64, error)
CountBooks(*BookBlueprint) (int, error)
}
package models
import "io"
import "fmt"
import "database/sql"
import "github.com/dadleyy/marlow/examples/library/data"
// marlow:ignore
const (
psqlConnectionString = "user=%s dbname=%s port=%s sslmode=disable password=%s"
)
// DatabaseConnections represent the different connection types used by the example app.
type DatabaseConnections struct {
Config *DatabaseConfig
postgres *sql.DB
sqlite *sql.DB
}
// Initialize opens the various database connection types and seeds their database tables.
func (db *DatabaseConnections) Initialize() error {
sqlite, e := sql.Open("sqlite3", db.Config.SQLite.Filename)
if e != nil {
return e
}
schema, e := data.Asset("data/sqlite.sql")
if e != nil {
return e
}
r, e := sqlite.Exec(string(schema))
if r == nil || e != nil {
return fmt.Errorf("unable to load sqlite schema (e %v)", e)
}
db.sqlite = sqlite
constr := fmt.Sprintf(
psqlConnectionString,
db.Config.Postgres.Username,
db.Config.Postgres.Database,
db.Config.Postgres.Port,
db.Config.Postgres.Password,
)
postgres, e := sql.Open("postgres", constr)
if e != nil {
return fmt.Errorf("postgres connection error (e %s)", e.Error())
}
schema, e = data.Asset("data/postgres.sql")
if e != nil {
return fmt.Errorf("unable to load postgres schema (e %s)", e.Error())
}
if _, e := postgres.Exec(string(schema)); e != nil {
return fmt.Errorf("unable to seed postgres db (e %s)", e.Error())
}
db.postgres = postgres
return nil
}
// Stores builds the various model stores generated by marlow.
func (db *DatabaseConnections) Stores(logger io.Writer) *Stores {
return &Stores{
Books: NewBookStore(db.sqlite, logger),
Authors: NewAuthorStore(db.sqlite, logger),
Genres: NewGenreStore(db.postgres, logger),
}
}
// Close will attempt to close the open database connections.
func (db *DatabaseConnections) Close() error {
dbs := []*sql.DB{db.sqlite, db.postgres}
var e error
for _, d := range dbs {
if d == nil {
continue
}
e = d.Close()
}
return e
}
package models
import "fmt"
import "database/sql"
// Genre records are used to group and describe a types of books.
type Genre struct {
table bool `marlow:"tableName=genres&dialect=postgres&primaryKey=id"`
ID uint `marlow:"column=id&autoIncrement=true"`
Name string `marlow:"column=name"`
ParentID sql.NullInt64 `marlow:"column=parent_id"`
}
func (g *Genre) String() string {
return fmt.Sprintf("%s", g.Name)
}
// Code auto-generated by marlow
package models
import "fmt"
import "bytes"
import "strings"
import "database/sql"
import "io"
import "os"
// [marlow feature]: finder on table[genres]
func (g *genreStore) FindGenres(_blueprint *GenreBlueprint) ([]*Genre, error) {
_results := make([]*Genre, 0)
_queryString := bytes.NewBufferString("SELECT genres.id,genres.name,genres.parent_id FROM genres")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit := 100
if _blueprint != nil && _blueprint.Limit >= 1 {
_limit = _blueprint.Limit
}
_offset := 0
if _blueprint != nil && _blueprint.Offset >= 1 {
_offset = _blueprint.Offset
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
_statement, _se := g.Prepare(_queryString.String())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
for _queryResult.Next() {
var _row Genre
if e := _queryResult.Scan(&_row.ID, &_row.Name, &_row.ParentID); e != nil {
return nil, e
}
_results = append(_results, &_row)
}
return _results, nil
}
// [marlow feature]: counter on table[genres]
func (g *genreStore) CountGenres(_blueprint *GenreBlueprint) (int, error) {
if _blueprint == nil {
_blueprint = &GenreBlueprint{}
}
_raw := fmt.Sprintf("SELECT COUNT(*) FROM genres %s;", _blueprint)
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _raw, _blueprint.Values())
_statement, _statementError := g.Prepare(_raw)
if _statementError != nil {
return -1, _statementError
}
defer _statement.Close()
_queryResult, _queryError := _statement.Query(_blueprint.Values()...)
if _queryError != nil {
return -1, _queryError
}
defer _queryResult.Close()
if _queryResult.Next() != true {
return -1, fmt.Errorf("invalid-scan")
}
var _scanResult int
_scanError := _queryResult.Scan(&_scanResult)
if _scanError != nil {
return -1, _scanError
}
return _scanResult, nil
}
// [marlow] field selector for Name (SelectGenreNames) [print: GenreBlueprint]
func (g *genreStore) SelectGenreNames(_blueprint *GenreBlueprint) ([]string, error) {
_results := make([]string, 0)
_queryString := bytes.NewBufferString("SELECT genres.name FROM genres")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := g.Prepare(_queryString.String())
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row string
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for ParentID (SelectGenreParentIDs) [print: GenreBlueprint]
func (g *genreStore) SelectGenreParentIDs(_blueprint *GenreBlueprint) ([]sql.NullInt64, error) {
_results := make([]sql.NullInt64, 0)
_queryString := bytes.NewBufferString("SELECT genres.parent_id FROM genres")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := g.Prepare(_queryString.String())
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row sql.NullInt64
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for ID (SelectGenreIDs) [print: GenreBlueprint]
func (g *genreStore) SelectGenreIDs(_blueprint *GenreBlueprint) ([]uint, error) {
_results := make([]uint, 0)
_queryString := bytes.NewBufferString("SELECT genres.id FROM genres")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := g.Prepare(_queryString.String())
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row uint
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] createable
func (g *genreStore) CreateGenres(_records ...Genre) (int64, error) {
if len(_records) == 0 {
return 0, nil
}
_placeholderList := make([]string, 0, len(_records))
_valueList := make([]interface{}, 0, len(_records))
for _recordIndex, _record := range _records {
_placeholders := []string{fmt.Sprintf("$%d", (_recordIndex*2)+1), fmt.Sprintf("$%d", (_recordIndex*2)+2)}
_valueList = append(_valueList, _record.Name, _record.ParentID)
_placeholderList = append(_placeholderList, fmt.Sprintf("(%s)", strings.Join(_placeholders, ",")))
}
_query := new(bytes.Buffer)
fmt.Fprintf(_query, "INSERT INTO genres (name,parent_id) VALUES %s RETURNING id;", strings.Join(_placeholderList, ", "))
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _query, _valueList)
_statement, _e := g.Prepare(_query.String())
if _e != nil {
return -1, _e
}
defer _statement.Close()
_result, _execError := _statement.Query(_valueList...)
if _execError != nil {
return -1, _execError
}
var _affectedResult int64
defer _result.Close()
for _result.Next() {
if _affectedError := _result.Scan(&_affectedResult); _affectedError != nil {
return -1, _affectedError
}
}
return _affectedResult, nil
}
// [marlow] updater method for id
func (g *genreStore) UpdateGenreID(_updates uint, _blueprint *GenreBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := fmt.Sprintf("$%d", _valueCount)
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE genres SET id = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := g.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
_values = append(_values, _updates)
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for name
func (g *genreStore) UpdateGenreName(_updates string, _blueprint *GenreBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := fmt.Sprintf("$%d", _valueCount)
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE genres SET name = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := g.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
_values = append(_values, _updates)
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for parent_id
func (g *genreStore) UpdateGenreParentID(_updates *sql.NullInt64, _blueprint *GenreBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := fmt.Sprintf("$%d", _valueCount)
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE genres SET parent_id = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := g.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
_values = append(_values, _updates)
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] deleteable
func (g *genreStore) DeleteGenres(_blueprint *GenreBlueprint) (int64, error) {
if _blueprint == nil || _blueprint.String() == "" {
return -1, fmt.Errorf("deletion blueprints must generate limiting clauses")
}
_query := fmt.Sprintf("DELETE FROM genres %s", _blueprint)
_statement, _e := g.Prepare(_query + ";")
if _e != nil {
return -1, _e
}
defer _statement.Close()
fmt.Fprintf(g.logger, "[marlow] %v | %v\n", _query, _blueprint.Values())
_execResult, _e := _statement.Exec(_blueprint.Values()...)
if _e != nil {
return -1, _e
}
_count, _e := _execResult.RowsAffected()
if _e != nil {
return -1, _e
}
return _count, nil
}
type GenreBlueprint struct {
IDRange []uint
ID []uint
NameLike []string
Name []string
ParentID []sql.NullInt64
Inclusive bool
Limit int
Offset int
OrderBy string
OrderDirection string
}
// [marlow] type IN clause for "genres.id"
func (g *GenreBlueprint) idInString(_count int) (string, []interface{}) {
if len(g.ID) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(g.ID))
_values := make([]interface{}, 0, len(g.ID))
for _i, _v := range g.ID {
_p := fmt.Sprintf("$%d", _i+_count)
_placeholder = append(_placeholder, _p)
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("genres.id IN (%s)", _joined), _values
}
// [marlow] range clause methods for genres.id
func (g *GenreBlueprint) idRangeString(_count int) (string, []interface{}) {
if len(g.IDRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = g.IDRange[0]
_values[1] = g.IDRange[1]
return fmt.Sprintf("(genres.id > $%d AND genres.id < $%d)", _count, _count+1), _values
}
// [marlow] type IN clause for "genres.name"
func (g *GenreBlueprint) nameInString(_count int) (string, []interface{}) {
if len(g.Name) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(g.Name))
_values := make([]interface{}, 0, len(g.Name))
for _i, _v := range g.Name {
_p := fmt.Sprintf("$%d", _i+_count)
_placeholder = append(_placeholder, _p)
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("genres.name IN (%s)", _joined), _values
}
// [marlow] string LIKE clause for "genres.name"
func (g *GenreBlueprint) nameLikeString(_count int) (string, []interface{}) {
if g == nil || g.NameLike == nil || len(g.NameLike) == 0 {
return "", nil
}
_placeholders := make([]string, 0, len(g.NameLike))
_values := make([]interface{}, 0, len(g.NameLike))
for _i, _value := range g.NameLike {
_like := fmt.Sprintf("genres.name LIKE $%d", _count+_i)
_placeholders = append(_placeholders, _like)
_values = append(_values, _value)
}
_conjunc := " AND "
if g.Inclusive == true {
_conjunc = " OR "
}
return strings.Join(_placeholders, _conjunc), _values
}
// [marlow] nullable clause gen for "genres.parent_id"
func (g *GenreBlueprint) parent_idInString(_count int) (string, []interface{}) {
if g.ParentID == nil {
return "", nil
}
if len(g.ParentID) == 0 {
return "genres.parent_id IS NOT NULL", nil
}
_placeholders := make([]string, 0, len(g.ParentID))
_values := make([]interface{}, 0, len(g.ParentID))
for _i, _v := range g.ParentID {
if _v.Valid == false {
return "genres.parent_id IS NULL", nil
}
_placeholders = append(_placeholders, fmt.Sprintf("$%d", _i+_count))
_values = append(_values, _v)
}
_joined := strings.Join(_placeholders, ",")
return fmt.Sprintf("genres.parent_id IN (%s)", _joined), _values
}
func (g *GenreBlueprint) String() string {
_clauses := make([]string, 0, 5)
_count := 1
if _item, _values := g.idInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := g.idRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := g.nameInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := g.nameLikeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := g.parent_idInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if len(_clauses) == 0 {
return ""
}
_map := " AND "
if g.Inclusive == true {
_map = " OR "
}
return "WHERE " + strings.Join(_clauses, _map)
}
func (g *GenreBlueprint) Values() []interface{} {
_clauses := make([]interface{}, 0, 5)
if g == nil {
return nil
}
if _, _item := g.idInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := g.idRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := g.nameInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := g.nameLikeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := g.parent_idInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
return _clauses
}
type genreStore struct {
*sql.DB
logger io.Writer
}
func NewGenreStore(_db *sql.DB, _logger io.Writer) GenreStore {
if _logger == nil {
_logger, _ = os.Open(os.DevNull)
}
return &genreStore{DB: _db, logger: _logger}
}
type GenreStore interface {
UpdateGenreID(uint, *GenreBlueprint) (int64, error)
UpdateGenreName(string, *GenreBlueprint) (int64, error)
FindGenres(*GenreBlueprint) ([]*Genre, error)
CountGenres(*GenreBlueprint) (int, error)
SelectGenreIDs(*GenreBlueprint) ([]uint, error)
CreateGenres(...Genre) (int64, error)
UpdateGenreParentID(*sql.NullInt64, *GenreBlueprint) (int64, error)
DeleteGenres(*GenreBlueprint) (int64, error)
SelectGenreNames(*GenreBlueprint) ([]string, error)
SelectGenreParentIDs(*GenreBlueprint) ([]sql.NullInt64, error)
}
// Code auto-generated by marlow
package models
import "fmt"
import "bytes"
import "strings"
import "database/sql"
import "io"
import "os"
// [marlow] updater method for id
func (m *multiAutoStore) UpdateMultiAutoID(_updates uint, _blueprint *MultiAutoBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := fmt.Sprintf("$%d", _valueCount)
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE multi_auto SET id = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := m.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
_values = append(_values, _updates)
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for status
func (m *multiAutoStore) UpdateMultiAutoStatus(_updates string, _blueprint *MultiAutoBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := fmt.Sprintf("$%d", _valueCount)
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE multi_auto SET status = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := m.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
_values = append(_values, _updates)
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] updater method for name
func (m *multiAutoStore) UpdateMultiAutoName(_updates string, _blueprint *MultiAutoBlueprint) (int64, error) {
_valueCount := 1
if _blueprint != nil && len(_blueprint.Values()) > 0 {
_valueCount = len(_blueprint.Values()) + 1
}
_target := fmt.Sprintf("$%d", _valueCount)
_queryString := bytes.NewBufferString(fmt.Sprintf("UPDATE multi_auto SET name = %s", _target))
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_statement, _se := m.Prepare(_queryString.String() + ";")
if _se != nil {
return -1, _se
}
defer _statement.Close()
_values := make([]interface{}, 0, _valueCount)
if _blueprint != nil {
_values = append(_values, _blueprint.Values()...)
}
_values = append(_values, _updates)
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _values)
_queryResult, _queryError := _statement.Exec(_values...)
if _queryError != nil {
return -1, _queryError
}
_rowCount, _re := _queryResult.RowsAffected()
if _re != nil {
return -1, _re
}
return _rowCount, nil
}
// [marlow] deleteable
func (m *multiAutoStore) DeleteMultiAutos(_blueprint *MultiAutoBlueprint) (int64, error) {
if _blueprint == nil || _blueprint.String() == "" {
return -1, fmt.Errorf("deletion blueprints must generate limiting clauses")
}
_query := fmt.Sprintf("DELETE FROM multi_auto %s", _blueprint)
_statement, _e := m.Prepare(_query + ";")
if _e != nil {
return -1, _e
}
defer _statement.Close()
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _query, _blueprint.Values())
_execResult, _e := _statement.Exec(_blueprint.Values()...)
if _e != nil {
return -1, _e
}
_count, _e := _execResult.RowsAffected()
if _e != nil {
return -1, _e
}
return _count, nil
}
// [marlow feature]: finder on table[multi_auto]
func (m *multiAutoStore) FindMultiAutos(_blueprint *MultiAutoBlueprint) ([]*MultiAuto, error) {
_results := make([]*MultiAuto, 0)
_queryString := bytes.NewBufferString("SELECT multi_auto.id,multi_auto.name,multi_auto.status FROM multi_auto")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit := 100
if _blueprint != nil && _blueprint.Limit >= 1 {
_limit = _blueprint.Limit
}
_offset := 0
if _blueprint != nil && _blueprint.Offset >= 1 {
_offset = _blueprint.Offset
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
_statement, _se := m.Prepare(_queryString.String())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
for _queryResult.Next() {
var _row MultiAuto
if e := _queryResult.Scan(&_row.ID, &_row.Name, &_row.Status); e != nil {
return nil, e
}
_results = append(_results, &_row)
}
return _results, nil
}
// [marlow feature]: counter on table[multi_auto]
func (m *multiAutoStore) CountMultiAutos(_blueprint *MultiAutoBlueprint) (int, error) {
if _blueprint == nil {
_blueprint = &MultiAutoBlueprint{}
}
_raw := fmt.Sprintf("SELECT COUNT(*) FROM multi_auto %s;", _blueprint)
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _raw, _blueprint.Values())
_statement, _statementError := m.Prepare(_raw)
if _statementError != nil {
return -1, _statementError
}
defer _statement.Close()
_queryResult, _queryError := _statement.Query(_blueprint.Values()...)
if _queryError != nil {
return -1, _queryError
}
defer _queryResult.Close()
if _queryResult.Next() != true {
return -1, fmt.Errorf("invalid-scan")
}
var _scanResult int
_scanError := _queryResult.Scan(&_scanResult)
if _scanError != nil {
return -1, _scanError
}
return _scanResult, nil
}
// [marlow] field selector for Name (SelectMultiAutoNames) [print: MultiAutoBlueprint]
func (m *multiAutoStore) SelectMultiAutoNames(_blueprint *MultiAutoBlueprint) ([]string, error) {
_results := make([]string, 0)
_queryString := bytes.NewBufferString("SELECT multi_auto.name FROM multi_auto")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := m.Prepare(_queryString.String())
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row string
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for ID (SelectMultiAutoIDs) [print: MultiAutoBlueprint]
func (m *multiAutoStore) SelectMultiAutoIDs(_blueprint *MultiAutoBlueprint) ([]uint, error) {
_results := make([]uint, 0)
_queryString := bytes.NewBufferString("SELECT multi_auto.id FROM multi_auto")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := m.Prepare(_queryString.String())
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row uint
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] field selector for Status (SelectMultiAutoStatuses) [print: MultiAutoBlueprint]
func (m *multiAutoStore) SelectMultiAutoStatuses(_blueprint *MultiAutoBlueprint) ([]string, error) {
_results := make([]string, 0)
_queryString := bytes.NewBufferString("SELECT multi_auto.status FROM multi_auto")
if _blueprint != nil {
fmt.Fprintf(_queryString, " %s", _blueprint)
}
_limit, _offset := 100, 0
if _blueprint != nil && _blueprint.Offset > 0 {
_offset = _blueprint.Offset
}
if _blueprint != nil && _blueprint.Limit > 0 {
_limit = _blueprint.Limit
}
fmt.Fprintf(_queryString, " LIMIT %d OFFSET %d", _limit, _offset)
_statement, _se := m.Prepare(_queryString.String())
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _queryString, _blueprint.Values())
if _se != nil {
return nil, _se
}
defer _statement.Close()
_queryResult, _qe := _statement.Query(_blueprint.Values()...)
if _qe != nil {
return nil, _qe
}
defer _queryResult.Close()
for _queryResult.Next() {
var _row string
if _re := _queryResult.Scan(&_row); _re != nil {
return nil, _re
}
_results = append(_results, _row)
}
return _results, nil
}
// [marlow] createable
func (m *multiAutoStore) CreateMultiAutos(_records ...MultiAuto) (int64, error) {
if len(_records) == 0 {
return 0, nil
}
_placeholderList := make([]string, 0, len(_records))
_valueList := make([]interface{}, 0, len(_records))
for _recordIndex, _record := range _records {
_placeholders := []string{fmt.Sprintf("$%d", (_recordIndex*1)+1)}
_valueList = append(_valueList, _record.Name)
_placeholderList = append(_placeholderList, fmt.Sprintf("(%s)", strings.Join(_placeholders, ",")))
}
_query := new(bytes.Buffer)
fmt.Fprintf(_query, "INSERT INTO multi_auto (name) VALUES %s RETURNING id;", strings.Join(_placeholderList, ", "))
fmt.Fprintf(m.logger, "[marlow] %v | %v\n", _query, _valueList)
_statement, _e := m.Prepare(_query.String())
if _e != nil {
return -1, _e
}
defer _statement.Close()
_result, _execError := _statement.Query(_valueList...)
if _execError != nil {
return -1, _execError
}
var _affectedResult int64
defer _result.Close()
for _result.Next() {
if _affectedError := _result.Scan(&_affectedResult); _affectedError != nil {
return -1, _affectedError
}
}
return _affectedResult, nil
}
type MultiAutoBlueprint struct {
NameLike []string
Name []string
IDRange []uint
ID []uint
StatusLike []string
Status []string
Inclusive bool
Limit int
Offset int
OrderBy string
OrderDirection string
}
// [marlow] type IN clause for "multi_auto.id"
func (m *MultiAutoBlueprint) idInString(_count int) (string, []interface{}) {
if len(m.ID) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(m.ID))
_values := make([]interface{}, 0, len(m.ID))
for _i, _v := range m.ID {
_p := fmt.Sprintf("$%d", _i+_count)
_placeholder = append(_placeholder, _p)
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("multi_auto.id IN (%s)", _joined), _values
}
// [marlow] range clause methods for multi_auto.id
func (m *MultiAutoBlueprint) idRangeString(_count int) (string, []interface{}) {
if len(m.IDRange) != 2 {
return "", nil
}
_values := make([]interface{}, 2)
_values[0] = m.IDRange[0]
_values[1] = m.IDRange[1]
return fmt.Sprintf("(multi_auto.id > $%d AND multi_auto.id < $%d)", _count, _count+1), _values
}
// [marlow] type IN clause for "multi_auto.name"
func (m *MultiAutoBlueprint) nameInString(_count int) (string, []interface{}) {
if len(m.Name) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(m.Name))
_values := make([]interface{}, 0, len(m.Name))
for _i, _v := range m.Name {
_p := fmt.Sprintf("$%d", _i+_count)
_placeholder = append(_placeholder, _p)
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("multi_auto.name IN (%s)", _joined), _values
}
// [marlow] string LIKE clause for "multi_auto.name"
func (m *MultiAutoBlueprint) nameLikeString(_count int) (string, []interface{}) {
if m == nil || m.NameLike == nil || len(m.NameLike) == 0 {
return "", nil
}
_placeholders := make([]string, 0, len(m.NameLike))
_values := make([]interface{}, 0, len(m.NameLike))
for _i, _value := range m.NameLike {
_like := fmt.Sprintf("multi_auto.name LIKE $%d", _count+_i)
_placeholders = append(_placeholders, _like)
_values = append(_values, _value)
}
_conjunc := " AND "
if m.Inclusive == true {
_conjunc = " OR "
}
return strings.Join(_placeholders, _conjunc), _values
}
// [marlow] type IN clause for "multi_auto.status"
func (m *MultiAutoBlueprint) statusInString(_count int) (string, []interface{}) {
if len(m.Status) == 0 {
return "", nil
}
_placeholder := make([]string, 0, len(m.Status))
_values := make([]interface{}, 0, len(m.Status))
for _i, _v := range m.Status {
_p := fmt.Sprintf("$%d", _i+_count)
_placeholder = append(_placeholder, _p)
_values = append(_values, _v)
}
_joined := strings.Join(_placeholder, ",")
return fmt.Sprintf("multi_auto.status IN (%s)", _joined), _values
}
// [marlow] string LIKE clause for "multi_auto.status"
func (m *MultiAutoBlueprint) statusLikeString(_count int) (string, []interface{}) {
if m == nil || m.StatusLike == nil || len(m.StatusLike) == 0 {
return "", nil
}
_placeholders := make([]string, 0, len(m.StatusLike))
_values := make([]interface{}, 0, len(m.StatusLike))
for _i, _value := range m.StatusLike {
_like := fmt.Sprintf("multi_auto.status LIKE $%d", _count+_i)
_placeholders = append(_placeholders, _like)
_values = append(_values, _value)
}
_conjunc := " AND "
if m.Inclusive == true {
_conjunc = " OR "
}
return strings.Join(_placeholders, _conjunc), _values
}
func (m *MultiAutoBlueprint) String() string {
_clauses := make([]string, 0, 6)
_count := 1
if _item, _values := m.idInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := m.idRangeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := m.nameInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := m.nameLikeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := m.statusInString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if _item, _values := m.statusLikeString(_count); _item != "" {
_clauses = append(_clauses, _item)
_count += len(_values)
}
if len(_clauses) == 0 {
return ""
}
_map := " AND "
if m.Inclusive == true {
_map = " OR "
}
return "WHERE " + strings.Join(_clauses, _map)
}
func (m *MultiAutoBlueprint) Values() []interface{} {
_clauses := make([]interface{}, 0, 6)
if m == nil {
return nil
}
if _, _item := m.idInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := m.idRangeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := m.nameInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := m.nameLikeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := m.statusInString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
if _, _item := m.statusLikeString(0); _item != nil && len(_item) > 0 {
_clauses = append(_clauses, _item...)
}
return _clauses
}
type multiAutoStore struct {
*sql.DB
logger io.Writer
}
func NewMultiAutoStore(_db *sql.DB, _logger io.Writer) MultiAutoStore {
if _logger == nil {
_logger, _ = os.Open(os.DevNull)
}
return &multiAutoStore{DB: _db, logger: _logger}
}
type MultiAutoStore interface {
UpdateMultiAutoID(uint, *MultiAutoBlueprint) (int64, error)
CountMultiAutos(*MultiAutoBlueprint) (int, error)
SelectMultiAutoNames(*MultiAutoBlueprint) ([]string, error)
SelectMultiAutoStatuses(*MultiAutoBlueprint) ([]string, error)
CreateMultiAutos(...MultiAuto) (int64, error)
UpdateMultiAutoStatus(string, *MultiAutoBlueprint) (int64, error)
UpdateMultiAutoName(string, *MultiAutoBlueprint) (int64, error)
DeleteMultiAutos(*MultiAutoBlueprint) (int64, error)
FindMultiAutos(*MultiAutoBlueprint) ([]*MultiAuto, error)
SelectMultiAutoIDs(*MultiAutoBlueprint) ([]uint, error)
}