Skip to content

Commit

Permalink
Rename IndexPlaceholders -> UseIndexPlaceholders
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondl committed Nov 12, 2017
1 parent ba06e88 commit a280795
Show file tree
Hide file tree
Showing 23 changed files with 64 additions and 98 deletions.
2 changes: 1 addition & 1 deletion boilingcore/boilingcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (s *State) initDriver(driverName string) error {

s.Dialect.LQ = s.Driver.LeftQuote()
s.Dialect.RQ = s.Driver.RightQuote()
s.Dialect.IndexPlaceholders = s.Driver.IndexPlaceholders()
s.Dialect.UseIndexPlaceholders = s.Driver.UseIndexPlaceholders()
s.Dialect.UseTopClause = s.Driver.UseTopClause()

return nil
Expand Down
34 changes: 0 additions & 34 deletions boilingcore/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,4 @@ type Config struct {
StructTagCasing string

Imports importers.Collection

Postgres PostgresConfig
MySQL MySQLConfig
MSSQL MSSQLConfig
}

// PostgresConfig configures a postgres database
type PostgresConfig struct {
User string
Pass string
Host string
Port int
DBName string
SSLMode string
}

// MySQLConfig configures a mysql database
type MySQLConfig struct {
User string
Pass string
Host string
Port int
DBName string
SSLMode string
}

// MSSQLConfig configures a mysql database
type MSSQLConfig struct {
User string
Pass string
Host string
Port int
DBName string
SSLMode string
}
4 changes: 2 additions & 2 deletions drivers/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (m testMockDriver) LeftQuote() byte {
return '"'
}

// IndexPlaceholders returns true to indicate fake support of indexed placeholders
func (m testMockDriver) IndexPlaceholders() bool {
// UseIndexPlaceholders returns true to indicate fake support of indexed placeholders
func (m testMockDriver) UseIndexPlaceholders() bool {
return false
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/mocks/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m *MockDriver) LeftQuote() byte {
return '"'
}

// IndexPlaceholders returns true to indicate fake support of indexed placeholders
func (m *MockDriver) IndexPlaceholders() bool {
// UseIndexPlaceholders returns true to indicate fake support of indexed placeholders
func (m *MockDriver) UseIndexPlaceholders() bool {
return false
}
2 changes: 1 addition & 1 deletion queries/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Dialect struct {
RQ byte
// Bool flag indicating whether indexed
// placeholders ($1) are used, or ? placeholders.
IndexPlaceholders bool
UseIndexPlaceholders bool
// Bool flag indicating whether "TOP" or "LIMIT" clause
// must be used for rows limitation
UseTopClause bool
Expand Down
28 changes: 14 additions & 14 deletions queries/query_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
args = append(args, j.args...)
}
var resp string
if q.dialect.IndexPlaceholders {
if q.dialect.UseIndexPlaceholders {
resp, _ = convertQuestionMarks(joinBuf.String(), argsLen+1)
} else {
resp = joinBuf.String()
Expand Down Expand Up @@ -166,7 +166,7 @@ func buildUpdateQuery(q *Query) (*bytes.Buffer, []interface{}) {

setSlice := make([]string, len(cols))
for index, col := range cols {
setSlice[index] = fmt.Sprintf("%s = %s", col, strmangle.Placeholders(q.dialect.IndexPlaceholders, 1, index+1, 1))
setSlice[index] = fmt.Sprintf("%s = %s", col, strmangle.Placeholders(q.dialect.UseIndexPlaceholders, 1, index+1, 1))
}
fmt.Fprintf(buf, " SET %s", strings.Join(setSlice, ", "))

Expand Down Expand Up @@ -207,7 +207,7 @@ func BuildUpsertQueryMySQL(dia Dialect, tableName string, update, whitelist []st
"INSERT IGNORE INTO %s (%s) VALUES (%s)",
tableName,
columns,
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
strmangle.Placeholders(dia.UseIndexPlaceholders, len(whitelist), 1, 1),
)
return buf.String()
}
Expand All @@ -217,7 +217,7 @@ func BuildUpsertQueryMySQL(dia Dialect, tableName string, update, whitelist []st
"INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE ",
tableName,
columns,
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1),
strmangle.Placeholders(dia.UseIndexPlaceholders, len(whitelist), 1, 1),
)

for i, v := range update {
Expand Down Expand Up @@ -247,7 +247,7 @@ func BuildUpsertQueryPostgres(dia Dialect, tableName string, updateOnConflict bo
if len(whitelist) != 0 {
columns = fmt.Sprintf("(%s) VALUES (%s)",
strings.Join(whitelist, ", "),
strmangle.Placeholders(dia.IndexPlaceholders, len(whitelist), 1, 1))
strmangle.Placeholders(dia.UseIndexPlaceholders, len(whitelist), 1, 1))
}

fmt.Fprintf(
Expand Down Expand Up @@ -294,7 +294,7 @@ func BuildUpsertQueryMSSQL(dia Dialect, tableName string, primary, update, inser

fmt.Fprintf(buf, "MERGE INTO %s as [t]\n", tableName)
fmt.Fprintf(buf, "USING (SELECT %s) as [s] ([%s])\n",
strmangle.Placeholders(dia.IndexPlaceholders, len(primary), startIndex, 1),
strmangle.Placeholders(dia.UseIndexPlaceholders, len(primary), startIndex, 1),
strings.Join(primary, string(dia.RQ)+","+string(dia.LQ)))
fmt.Fprint(buf, "ON (")
for i, v := range primary {
Expand All @@ -315,7 +315,7 @@ func BuildUpsertQueryMSSQL(dia Dialect, tableName string, primary, update, inser
fmt.Fprint(buf, "WHEN NOT MATCHED THEN ")
fmt.Fprintf(buf, "INSERT (%s) VALUES (%s)",
strings.Join(insert, ", "),
strmangle.Placeholders(dia.IndexPlaceholders, len(insert), startIndex, 1))
strmangle.Placeholders(dia.UseIndexPlaceholders, len(insert), startIndex, 1))

if len(output) > 0 {
fmt.Fprintf(buf, "\nOUTPUT INSERTED.[%s];", strings.Join(output, "],INSERTED.["))
Expand Down Expand Up @@ -343,7 +343,7 @@ func writeModifiers(q *Query, buf *bytes.Buffer, args *[]interface{}) {
*args = append(*args, j.args...)
}
var resp string
if q.dialect.IndexPlaceholders {
if q.dialect.UseIndexPlaceholders {
resp, _ = convertQuestionMarks(havingBuf.String(), argsLen+1)
} else {
resp = havingBuf.String()
Expand Down Expand Up @@ -471,7 +471,7 @@ func whereClause(q *Query, startAt int) (string, []interface{}) {
}

var resp string
if q.dialect.IndexPlaceholders {
if q.dialect.UseIndexPlaceholders {
resp, _ = convertQuestionMarks(buf.String(), startAt)
} else {
resp = buf.String()
Expand Down Expand Up @@ -515,7 +515,7 @@ func inClause(q *Query, startAt int) (string, []interface{}) {
// column name side, however if this case is being hit then the regexp
// probably needs adjustment, or the user is passing in invalid clauses.
if matches == nil {
clause, count := convertInQuestionMarks(q.dialect.IndexPlaceholders, in.clause, startAt, 1, ln)
clause, count := convertInQuestionMarks(q.dialect.UseIndexPlaceholders, in.clause, startAt, 1, ln)
buf.WriteString(clause)
startAt = startAt + count
} else {
Expand All @@ -530,7 +530,7 @@ func inClause(q *Query, startAt int) (string, []interface{}) {

var leftClause string
var leftCount int
if q.dialect.IndexPlaceholders {
if q.dialect.UseIndexPlaceholders {
leftClause, leftCount = convertQuestionMarks(strings.Join(cols, ","), startAt)
} else {
// Count the number of cols that are question marks, so we know
Expand All @@ -542,7 +542,7 @@ func inClause(q *Query, startAt int) (string, []interface{}) {
}
leftClause = strings.Join(cols, ",")
}
rightClause, rightCount := convertInQuestionMarks(q.dialect.IndexPlaceholders, rightSide, startAt+leftCount, groupAt, ln-leftCount)
rightClause, rightCount := convertInQuestionMarks(q.dialect.UseIndexPlaceholders, rightSide, startAt+leftCount, groupAt, ln-leftCount)
buf.WriteString(leftClause)
buf.WriteString(" IN ")
buf.WriteString(rightClause)
Expand All @@ -560,7 +560,7 @@ func inClause(q *Query, startAt int) (string, []interface{}) {
// It uses groupAt to determine how many placeholders should be in each group,
// for example, groupAt 2 would result in: (($1,$2),($3,$4))
// and groupAt 1 would result in ($1,$2,$3,$4)
func convertInQuestionMarks(indexPlaceholders bool, clause string, startAt, groupAt, total int) (string, int) {
func convertInQuestionMarks(UseIndexPlaceholders bool, clause string, startAt, groupAt, total int) (string, int) {
if startAt == 0 || len(clause) == 0 {
panic("Not a valid start number.")
}
Expand All @@ -582,7 +582,7 @@ func convertInQuestionMarks(indexPlaceholders bool, clause string, startAt, grou

paramBuf.WriteString(clause[:foundAt])
paramBuf.WriteByte('(')
paramBuf.WriteString(strmangle.Placeholders(indexPlaceholders, total, startAt, groupAt))
paramBuf.WriteString(strmangle.Placeholders(UseIndexPlaceholders, total, startAt, groupAt))
paramBuf.WriteByte(')')
paramBuf.WriteString(clause[foundAt+1:])

Expand Down
10 changes: 5 additions & 5 deletions queries/query_builders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestBuildQuery(t *testing.T) {

for i, test := range tests {
filename := filepath.Join("_fixtures", fmt.Sprintf("%02d.sql", i))
test.q.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
test.q.dialect = &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true}
out, args := buildQuery(test.q)

if *writeGoldenFiles {
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestWriteStars(t *testing.T) {
}

for i, test := range tests {
test.In.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
test.In.dialect = &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true}
selects := writeStars(&test.In)
if !reflect.DeepEqual(selects, test.Out) {
t.Errorf("writeStar test fail %d\nwant: %v\ngot: %v", i, test.Out, selects)
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestWhereClause(t *testing.T) {
}

for i, test := range tests {
test.q.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
test.q.dialect = &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true}
result, _ := whereClause(&test.q, 1)
if result != test.expect {
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, result)
Expand Down Expand Up @@ -410,7 +410,7 @@ func TestInClause(t *testing.T) {
}

for i, test := range tests {
test.q.dialect = &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true}
test.q.dialect = &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true}
result, args := inClause(&test.q, 1)
if result != test.expect {
t.Errorf("%d) Mismatch between expect and result:\n%s\n%s\n", i, test.expect, result)
Expand Down Expand Up @@ -524,7 +524,7 @@ func TestWriteAsStatements(t *testing.T) {
`a.clown.run`,
`COUNT(a)`,
},
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
}

expect := []string{
Expand Down
12 changes: 6 additions & 6 deletions queries/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestBindStruct(t *testing.T) {

query := &Query{
from: []string{"fun"},
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
}

db, mock, err := sqlmock.New()
Expand Down Expand Up @@ -83,7 +83,7 @@ func TestBindSlice(t *testing.T) {

query := &Query{
from: []string{"fun"},
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
}

db, mock, err := sqlmock.New()
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestBindPtrSlice(t *testing.T) {

query := &Query{
from: []string{"fun"},
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
}

db, mock, err := sqlmock.New()
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestBindSingular(t *testing.T) {

query := &Query{
from: []string{"fun"},
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
}

db, mock, err := sqlmock.New()
Expand Down Expand Up @@ -488,7 +488,7 @@ func TestBind_InnerJoin(t *testing.T) {
query := &Query{
from: []string{"fun"},
joins: []join{{kind: JoinInner, clause: "happy as h on fun.id = h.fun_id"}},
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
}

db, mock, err := sqlmock.New()
Expand Down Expand Up @@ -542,7 +542,7 @@ func TestBind_InnerJoinSelect(t *testing.T) {
}{}

query := &Query{
dialect: &Dialect{LQ: '"', RQ: '"', IndexPlaceholders: true},
dialect: &Dialect{LQ: '"', RQ: '"', UseIndexPlaceholders: true},
selectCols: []string{"fun.id", "h.id"},
from: []string{"fun"},
joins: []join{{kind: JoinInner, clause: "happy as h on fun.happy_id = h.id"}},
Expand Down
6 changes: 3 additions & 3 deletions strmangle/strmangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,8 @@ func PrefixStringSlice(str string, strs []string) []string {
// Placeholders generates the SQL statement placeholders for in queries.
// For example, ($1,$2,$3),($4,$5,$6) etc.
// It will start counting placeholders at "start".
// If indexPlaceholders is false, it will convert to ? instead of $1 etc.
func Placeholders(indexPlaceholders bool, count int, start int, group int) string {
// If useIndexPlaceholders is false, it will convert to ? instead of $1 etc.
func Placeholders(useIndexPlaceholders bool, count int, start int, group int) string {
buf := GetBuffer()
defer PutBuffer(buf)

Expand All @@ -465,7 +465,7 @@ func Placeholders(indexPlaceholders bool, count int, start int, group int) strin
buf.WriteByte(',')
}
}
if indexPlaceholders {
if useIndexPlaceholders {
buf.WriteString(fmt.Sprintf("$%d", start+i))
} else {
buf.WriteByte('?')
Expand Down
2 changes: 1 addition & 1 deletion templates/07_relationship_to_one_eager.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula

query := fmt.Sprintf(
"select * from {{.ForeignTable | $dot.SchemaTable}} where {{.ForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
strmangle.Placeholders(dialect.UseIndexPlaceholders, count, 1, 1),
)

if boil.DebugMode {
Expand Down
2 changes: 1 addition & 1 deletion templates/08_relationship_one_to_one_eager.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula

query := fmt.Sprintf(
"select * from {{.ForeignTable | $dot.SchemaTable}} where {{.ForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
strmangle.Placeholders(dialect.UseIndexPlaceholders, count, 1, 1),
)

if boil.DebugMode {
Expand Down
4 changes: 2 additions & 2 deletions templates/09_relationship_to_many_eager.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func ({{$varNameSingular}}L) Load{{$txt.Function.Name}}(e boil.Executor, singula
{{- $schemaJoinTable := .JoinTable | $dot.SchemaTable -}}
query := fmt.Sprintf(
"select {{id 0 | $dot.Quotes}}.*, {{id 1 | $dot.Quotes}}.{{.JoinLocalColumn | $dot.Quotes}} from {{$schemaForeignTable}} as {{id 0 | $dot.Quotes}} inner join {{$schemaJoinTable}} as {{id 1 | $dot.Quotes}} on {{id 0 | $dot.Quotes}}.{{.ForeignColumn | $dot.Quotes}} = {{id 1 | $dot.Quotes}}.{{.JoinForeignColumn | $dot.Quotes}} where {{id 1 | $dot.Quotes}}.{{.JoinLocalColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
strmangle.Placeholders(dialect.UseIndexPlaceholders, count, 1, 1),
)
{{else -}}
query := fmt.Sprintf(
"select * from {{$schemaForeignTable}} where {{.ForeignColumn | $dot.Quotes}} in (%s)",
strmangle.Placeholders(dialect.IndexPlaceholders, count, 1, 1),
strmangle.Placeholders(dialect.UseIndexPlaceholders, count, 1, 1),
)
{{end -}}

Expand Down
4 changes: 2 additions & 2 deletions templates/10_relationship_to_one_setops.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo

updateQuery := fmt.Sprintf(
"UPDATE {{$schemaTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.Column}}"{{"}"}}),
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}2{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns),
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.Column}}"{{"}"}}),
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.UseIndexPlaceholders}}2{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns),
)
values := []interface{}{related.{{$txt.ForeignTable.ColumnNameGo}}, o.{{$dot.Table.PKey.Columns | stringMap $dot.StringFuncs.titleCase | join ", o."}}{{"}"}}

Expand Down
4 changes: 2 additions & 2 deletions templates/11_relationship_one_to_one_setops.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo
} else {
updateQuery := fmt.Sprintf(
"UPDATE {{$foreignSchemaTable}} SET %s WHERE %s",
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.ForeignColumn}}"{{"}"}}),
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}2{{else}}0{{end}}, {{$foreignVarNameSingular}}PrimaryKeyColumns),
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.ForeignColumn}}"{{"}"}}),
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.UseIndexPlaceholders}}2{{else}}0{{end}}, {{$foreignVarNameSingular}}PrimaryKeyColumns),
)
values := []interface{}{o.{{$txt.LocalTable.ColumnNameGo}}, related.{{$foreignPKeyCols | stringMap $dot.StringFuncs.titleCase | join ", related."}}{{"}"}}

Expand Down
Loading

0 comments on commit a280795

Please sign in to comment.