Skip to content

Commit

Permalink
Unexported unneeded fields and functions
Browse files Browse the repository at this point in the history
Some fields and functions were related to implementation details, made
them private so that they can be changed in the future without breaking
public API.
  • Loading branch information
bokwoon95 committed Sep 18, 2020
1 parent f20dee3 commit c80c0d5
Show file tree
Hide file tree
Showing 56 changed files with 413 additions and 382 deletions.
6 changes: 3 additions & 3 deletions mysql/base_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
Linterpolate LogFlag = 1 << iota
Lstats
Lresults
Lparse
// Lparse
Lverbose = Lstats | Lresults
)

Expand Down Expand Up @@ -230,7 +230,7 @@ func (q BaseQuery) DeleteFrom(tables ...BaseTable) DeleteQuery {
// Union transforms the BaseQuery into a VariadicQuery.
func (q BaseQuery) Union(queries ...Query) VariadicQuery {
return VariadicQuery{
TopLevel: true,
topLevel: true,
Operator: QueryUnion,
Queries: queries,
DB: q.DB,
Expand All @@ -242,7 +242,7 @@ func (q BaseQuery) Union(queries ...Query) VariadicQuery {
// UnionAll transforms the BaseQuery into a VariadicQuery.
func (q BaseQuery) UnionAll(queries ...Query) VariadicQuery {
return VariadicQuery{
TopLevel: true,
topLevel: true,
Operator: QueryUnionAll,
Queries: queries,
DB: q.DB,
Expand Down
2 changes: 1 addition & 1 deletion mysql/boolean_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (f BooleanField) String() string {
buf := &strings.Builder{}
var args []interface{}
f.AppendSQLExclude(buf, &args, nil)
return QuestionInterpolate(buf.String(), args...)
return questionInterpolate(buf.String(), args...)
}

// GetAlias returns the alias of the BooleanField.
Expand Down
14 changes: 7 additions & 7 deletions mysql/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func (f PredicateCases) AppendSQLExclude(buf *strings.Builder, args *[]interface
buf.WriteString("CASE")
for _, Case := range f.Cases {
buf.WriteString(" WHEN ")
AppendSQLValue(buf, args, excludedTableQualifiers, Case.Condition)
appendSQLValue(buf, args, excludedTableQualifiers, Case.Condition)
buf.WriteString(" THEN ")
AppendSQLValue(buf, args, excludedTableQualifiers, Case.Result)
appendSQLValue(buf, args, excludedTableQualifiers, Case.Result)
}
if f.Fallback != nil {
buf.WriteString(" ELSE ")
AppendSQLValue(buf, args, excludedTableQualifiers, f.Fallback)
appendSQLValue(buf, args, excludedTableQualifiers, f.Fallback)
}
buf.WriteString(" END")
}
Expand Down Expand Up @@ -94,16 +94,16 @@ type SimpleCases struct {
// It propagates the excludedTableQualifiers down to its child elements.
func (f SimpleCases) AppendSQLExclude(buf *strings.Builder, args *[]interface{}, excludedTableQualifiers []string) {
buf.WriteString("CASE ")
AppendSQLValue(buf, args, excludedTableQualifiers, f.Expression)
appendSQLValue(buf, args, excludedTableQualifiers, f.Expression)
for _, Case := range f.Cases {
buf.WriteString(" WHEN ")
AppendSQLValue(buf, args, excludedTableQualifiers, Case.Value)
appendSQLValue(buf, args, excludedTableQualifiers, Case.Value)
buf.WriteString(" THEN ")
AppendSQLValue(buf, args, excludedTableQualifiers, Case.Result)
appendSQLValue(buf, args, excludedTableQualifiers, Case.Result)
}
if f.Fallback != nil {
buf.WriteString(" ELSE ")
AppendSQLValue(buf, args, excludedTableQualifiers, f.Fallback)
appendSQLValue(buf, args, excludedTableQualifiers, f.Fallback)
}
buf.WriteString(" END")
}
Expand Down
2 changes: 1 addition & 1 deletion mysql/cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func AppendCTEs(buf *strings.Builder, args *[]interface{}, CTEs []CTE, fromTable
case nil:
buf.WriteString("NULL")
case VariadicQuery:
q.TopLevel = true
q.topLevel = true
q.NestThis().AppendSQL(buf, args)
default:
q.NestThis().AppendSQL(buf, args)
Expand Down
6 changes: 3 additions & 3 deletions mysql/cte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func TestCTEs_AppendSQL(t *testing.T) {
q1 := Select(u.USER_ID, u.EMAIL).From(u).Where(u.USER_ID.EqInt(1))
q2 := Select(u.USER_ID, u.EMAIL).From(u).Where(u.USER_ID.EqInt(2))
q3 := Select(u.USER_ID, u.EMAIL).From(u).Where(u.USER_ID.EqInt(3))
q := Union(q1, q2, q3).CTE("cte")
q := UnionAll(q1, q2, q3).CTE("cte")
tt.q = Select(q["user_id"], q["email"]).From(q)
tt.wantQuery = "WITH cte AS" +
" (SELECT u.user_id, u.email FROM devlab.users AS u WHERE u.user_id = ?" +
" UNION" +
" UNION ALL" +
" SELECT u.user_id, u.email FROM devlab.users AS u WHERE u.user_id = ?" +
" UNION" +
" UNION ALL" +
" SELECT u.user_id, u.email FROM devlab.users AS u WHERE u.user_id = ?)" +
" SELECT cte.user_id, cte.email FROM cte"
tt.wantArgs = []interface{}{1, 2, 3}
Expand Down
6 changes: 3 additions & 3 deletions mysql/custom_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sq
import "strings"

// CustomField is a Field that can render itself in an arbitrary way by calling
// ExpandValues on its Format and Values.
// expandValues on its Format and Values.
type CustomField struct {
Alias string
Format string
Expand All @@ -14,7 +14,7 @@ type CustomField struct {
// AppendSQLExclude marshals the CustomField into a buffer and an args slice.
// It propagates the excludedTableQualifiers down to its child elements.
func (f CustomField) AppendSQLExclude(buf *strings.Builder, args *[]interface{}, excludedTableQualifiers []string) {
ExpandValues(buf, args, excludedTableQualifiers, f.Format, f.Values)
expandValues(buf, args, excludedTableQualifiers, f.Format, f.Values)
if f.IsDesc != nil {
if *f.IsDesc {
buf.WriteString(" DESC")
Expand Down Expand Up @@ -139,7 +139,7 @@ func (f CustomField) String() string {
buf := &strings.Builder{}
var args []interface{}
f.AppendSQLExclude(buf, &args, nil)
return QuestionInterpolate(buf.String(), args...)
return questionInterpolate(buf.String(), args...)
}

// GetAlias returns the alias of the CustomField.
Expand Down
32 changes: 16 additions & 16 deletions mysql/delete_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// DeleteQuery represents a DELETE query.
type DeleteQuery struct {
Nested bool
nested bool
Alias string
// WITH
CTEs []CTE
Expand All @@ -33,12 +33,12 @@ type DeleteQuery struct {
// Logging
Log Logger
LogFlag LogFlag
LogSkip int
logSkip int
}

// ToSQL marshals the DeleteQuery into a query string and args slice.
func (q DeleteQuery) ToSQL() (string, []interface{}) {
q.LogSkip += 1
q.logSkip += 1
buf := &strings.Builder{}
var args []interface{}
q.AppendSQL(buf, &args)
Expand All @@ -48,7 +48,7 @@ func (q DeleteQuery) ToSQL() (string, []interface{}) {
// AppendSQL marshals the DeleteQuery into a buffer and args slice.
func (q DeleteQuery) AppendSQL(buf *strings.Builder, args *[]interface{}) {
// WITH
if !q.Nested {
if !q.nested {
AppendCTEs(buf, args, q.CTEs, nil, q.JoinTables)
}
// DELETE FROM
Expand Down Expand Up @@ -97,7 +97,7 @@ func (q DeleteQuery) AppendSQL(buf *strings.Builder, args *[]interface{}) {
// WHERE
if len(q.WherePredicate.Predicates) > 0 {
buf.WriteString(" WHERE ")
q.WherePredicate.Toplevel = true
q.WherePredicate.toplevel = true
q.WherePredicate.AppendSQLExclude(buf, args, nil)
}
// ORDER BY
Expand All @@ -113,40 +113,40 @@ func (q DeleteQuery) AppendSQL(buf *strings.Builder, args *[]interface{}) {
}
*args = append(*args, *q.LimitValue)
}
if !q.Nested {
if !q.nested {
if q.Log != nil {
query := buf.String()
var logOutput string
switch {
case Lstats&q.LogFlag != 0:
logOutput = "\n----[ Executing query ]----\n" + query + " " + fmt.Sprint(*args) +
"\n----[ with bind values ]----\n" + QuestionInterpolate(query, *args...)
"\n----[ with bind values ]----\n" + questionInterpolate(query, *args...)
case Linterpolate&q.LogFlag != 0:
logOutput = "Executing query: " + QuestionInterpolate(query, *args...)
logOutput = "Executing query: " + questionInterpolate(query, *args...)
default:
logOutput = "Executing query: " + query + " " + fmt.Sprint(*args)
}
switch q.Log.(type) {
case *log.Logger:
q.Log.Output(q.LogSkip+2, logOutput)
_ = q.Log.Output(q.logSkip+2, logOutput)
default:
q.Log.Output(q.LogSkip+1, logOutput)
_ = q.Log.Output(q.logSkip+1, logOutput)
}
}
}
}

// NestThis indicates to the DeleteQuery that it is nested.
func (q DeleteQuery) NestThis() Query {
q.Nested = true
q.nested = true
return q
}

// DeleteFrom creates a new DeleteQuery.
func DeleteFrom(tables ...BaseTable) DeleteQuery {
return DeleteQuery{
FromTables: tables,
Alias: RandomString(8),
Alias: randomString(8),
}
}

Expand Down Expand Up @@ -254,7 +254,7 @@ func (q DeleteQuery) Limit(limit int) DeleteQuery {
// Exec will execute the DeleteQuery with the given DB. It will only compute
// the rowsAffected if the ErowsAffected Execflag is passed to it.
func (q DeleteQuery) Exec(db DB, flag ExecFlag) (rowsAffected int64, err error) {
q.LogSkip += 1
q.logSkip += 1
return q.ExecContext(nil, db, flag)
}

Expand Down Expand Up @@ -284,16 +284,16 @@ func (q DeleteQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (row
if logBuf.Len() > 0 {
switch q.Log.(type) {
case *log.Logger:
q.Log.Output(q.LogSkip+2, logBuf.String())
_ = q.Log.Output(q.logSkip+2, logBuf.String())
default:
q.Log.Output(q.LogSkip+1, logBuf.String())
_ = q.Log.Output(q.logSkip+1, logBuf.String())
}
}
}()
var res sql.Result
tmpbuf := &strings.Builder{}
var tmpargs []interface{}
q.LogSkip += 1
q.logSkip += 1
q.AppendSQL(tmpbuf, &tmpargs)
if ctx == nil {
res, err = db.Exec(tmpbuf.String(), tmpargs...)
Expand Down
2 changes: 1 addition & 1 deletion mysql/delete_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestDeleteQuery_Exec(t *testing.T) {

// SQL syntax error
// use a tempDB so we don't foul up the current db transaction with the error
tempDB, err := sql.Open("txdb", RandomString(8))
tempDB, err := sql.Open("txdb", randomString(8))
is.NoErr(err)
_, err = WithLog(customLogger, Linterpolate).
WithDB(tempDB).
Expand Down
4 changes: 2 additions & 2 deletions mysql/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type FieldAssignment struct {
// AppendSQLExclude marshals the FieldAssignment into a buffer and an args
// slice. It propagates the excludedTableQualifiers down to its child elements.
func (set FieldAssignment) AppendSQLExclude(buf *strings.Builder, args *[]interface{}, excludedTableQualifiers []string) {
AppendSQLValue(buf, args, excludedTableQualifiers, set.Field)
appendSQLValue(buf, args, excludedTableQualifiers, set.Field)
buf.WriteString(" = ")
AppendSQLValue(buf, args, excludedTableQualifiers, set.Value)
appendSQLValue(buf, args, excludedTableQualifiers, set.Value)
}

// AssertAssignment implements the Assignment interface.
Expand Down
32 changes: 16 additions & 16 deletions mysql/insert_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// InsertQuery represents an INSERT query.
type InsertQuery struct {
Nested bool
nested bool
Alias string
// INSERT INTO
Ignore bool
Expand All @@ -30,12 +30,12 @@ type InsertQuery struct {
// Logging
Log Logger
LogFlag LogFlag
LogSkip int
logSkip int
}

// ToSQL marshals the InsertQuery into a query string and args slice.
func (q InsertQuery) ToSQL() (string, []interface{}) {
q.LogSkip += 1
q.logSkip += 1
buf := &strings.Builder{}
var args []interface{}
q.AppendSQL(buf, &args)
Expand Down Expand Up @@ -75,32 +75,32 @@ func (q InsertQuery) AppendSQL(buf *strings.Builder, args *[]interface{}) {
q.RowValues.AppendSQL(buf, args)
case q.SelectQuery != nil:
buf.WriteString(" ")
q.SelectQuery.Nested = true
q.SelectQuery.nested = true
q.SelectQuery.AppendSQL(buf, args)
}
// ON DUPLICATE KEY UPDATE
if len(q.Resolution) > 0 {
buf.WriteString(" ON DUPLICATE KEY UPDATE ")
q.Resolution.AppendSQLExclude(buf, args, excludedTableQualifiers)
}
if !q.Nested {
if !q.nested {
if q.Log != nil {
query := buf.String()
var logOutput string
switch {
case Lstats&q.LogFlag != 0:
logOutput = "\n----[ Executing query ]----\n" + query + " " + fmt.Sprint(*args) +
"\n----[ with bind values ]----\n" + QuestionInterpolate(query, *args...)
"\n----[ with bind values ]----\n" + questionInterpolate(query, *args...)
case Linterpolate&q.LogFlag != 0:
logOutput = "Executing query: " + QuestionInterpolate(query, *args...)
logOutput = "Executing query: " + questionInterpolate(query, *args...)
default:
logOutput = "Executing query: " + query + " " + fmt.Sprint(*args)
}
switch q.Log.(type) {
case *log.Logger:
q.Log.Output(q.LogSkip+2, logOutput)
_ = q.Log.Output(q.logSkip+2, logOutput)
default:
q.Log.Output(q.LogSkip+1, logOutput)
_ = q.Log.Output(q.logSkip+1, logOutput)
}
}
}
Expand All @@ -110,7 +110,7 @@ func (q InsertQuery) AppendSQL(buf *strings.Builder, args *[]interface{}) {
func InsertInto(table BaseTable) InsertQuery {
return InsertQuery{
IntoTable: table,
Alias: RandomString(8),
Alias: randomString(8),
}
}

Expand All @@ -119,7 +119,7 @@ func InsertIgnoreInto(table BaseTable) InsertQuery {
return InsertQuery{
Ignore: true,
IntoTable: table,
Alias: RandomString(8),
Alias: randomString(8),
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ func Values(field Field) CustomField {
// compute both, bitwise or the flags together i.e.
// ElastInsertID|ErowsAffected.
func (q InsertQuery) Exec(db DB, flag ExecFlag) (lastInsertID, rowsAffected int64, err error) {
q.LogSkip += 1
q.logSkip += 1
return q.ExecContext(nil, db, flag)
}

Expand Down Expand Up @@ -223,16 +223,16 @@ func (q InsertQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (las
if logBuf.Len() > 0 {
switch q.Log.(type) {
case *log.Logger:
q.Log.Output(q.LogSkip+2, logBuf.String())
_ = q.Log.Output(q.logSkip+2, logBuf.String())
default:
q.Log.Output(q.LogSkip+1, logBuf.String())
_ = q.Log.Output(q.logSkip+1, logBuf.String())
}
}
}()
var res sql.Result
tmpbuf := &strings.Builder{}
var tmpargs []interface{}
q.LogSkip += 1
q.logSkip += 1
q.AppendSQL(tmpbuf, &tmpargs)
if ctx == nil {
res, err = db.Exec(tmpbuf.String(), tmpargs...)
Expand All @@ -259,6 +259,6 @@ func (q InsertQuery) ExecContext(ctx context.Context, db DB, flag ExecFlag) (las

// NestThis indicates to the InsertQuery that it is nested.
func (q InsertQuery) NestThis() Query {
q.Nested = true
q.nested = true
return q
}
Loading

0 comments on commit c80c0d5

Please sign in to comment.