Delete unused code
This commit is contained in:
parent
6b966954ee
commit
9f52cf6ec9
2 changed files with 2 additions and 96 deletions
|
@ -479,48 +479,10 @@ func convertQuestionMarks(clause string, startAt int) (string, int) {
|
|||
return paramBuf.String(), total
|
||||
}
|
||||
|
||||
// identifierMapping creates a map of all identifiers to potential model names
|
||||
func identifierMapping(q *Query) map[string]string {
|
||||
var ids map[string]string
|
||||
setID := func(alias, name string) {
|
||||
if ids == nil {
|
||||
ids = make(map[string]string)
|
||||
}
|
||||
ids[alias] = name
|
||||
}
|
||||
|
||||
for _, from := range q.from {
|
||||
tokens := strings.Split(from, " ")
|
||||
parseIdentifierClause(tokens, setID)
|
||||
}
|
||||
|
||||
for _, join := range q.joins {
|
||||
tokens := strings.Split(join.clause, " ")
|
||||
parseIdentifierClause(tokens, setID)
|
||||
}
|
||||
|
||||
return ids
|
||||
}
|
||||
|
||||
// parseIdentifierClause takes a set of tokens and looks for something of the form:
|
||||
// parseFromClause will parse something that looks like
|
||||
// a
|
||||
// a b
|
||||
// a as b
|
||||
// where 'a' and 'b' are valid SQL identifiers
|
||||
// It only evaluates the first 3 tokens (anything past that is superfluous)
|
||||
// It stops parsing when it finds "on" or an invalid identifier
|
||||
func parseIdentifierClause(tokens []string, setID func(string, string)) {
|
||||
alias, name, ok := parseFromClause(tokens)
|
||||
if !ok {
|
||||
panic("could not parse from statement")
|
||||
}
|
||||
|
||||
if len(alias) > 0 {
|
||||
setID(alias, name)
|
||||
} else {
|
||||
setID(name, name)
|
||||
}
|
||||
}
|
||||
|
||||
func parseFromClause(toks []string) (alias, name string, ok bool) {
|
||||
if len(toks) > 3 {
|
||||
toks = toks[:3]
|
||||
|
|
|
@ -123,62 +123,6 @@ func TestBuildQuery(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIdentifierMapping(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
In Query
|
||||
Out map[string]string
|
||||
}{
|
||||
{
|
||||
In: Query{from: []string{`a`}},
|
||||
Out: map[string]string{"a": "a"},
|
||||
},
|
||||
{
|
||||
In: Query{from: []string{`"a"`, `b`}},
|
||||
Out: map[string]string{"a": "a", "b": "b"},
|
||||
},
|
||||
{
|
||||
In: Query{from: []string{`a as b`}},
|
||||
Out: map[string]string{"b": "a"},
|
||||
},
|
||||
{
|
||||
In: Query{from: []string{`a AS "b"`, `"c" as d`}},
|
||||
Out: map[string]string{"b": "a", "d": "c"},
|
||||
},
|
||||
{
|
||||
In: Query{joins: []join{{kind: JoinInner, clause: `a on stuff = there`}}},
|
||||
Out: map[string]string{"a": "a"},
|
||||
},
|
||||
{
|
||||
In: Query{joins: []join{{kind: JoinNatural, clause: `"a" on stuff = there`}}},
|
||||
Out: map[string]string{"a": "a"},
|
||||
},
|
||||
{
|
||||
In: Query{joins: []join{{kind: JoinNatural, clause: `a as b on stuff = there`}}},
|
||||
Out: map[string]string{"b": "a"},
|
||||
},
|
||||
{
|
||||
In: Query{joins: []join{{kind: JoinOuterRight, clause: `"a" as "b" on stuff = there`}}},
|
||||
Out: map[string]string{"b": "a"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
m := identifierMapping(&test.In)
|
||||
|
||||
for k, v := range test.Out {
|
||||
val, ok := m[k]
|
||||
if !ok {
|
||||
t.Errorf("%d) want: %s = %s, but was missing", i, k, v)
|
||||
}
|
||||
if val != v {
|
||||
t.Errorf("%d) want: %s = %s, got: %s", i, k, v, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStars(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue