-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add embedder * Update coverage.svg and coverage.out * Update Embedder description * Move types over * Update coverage.svg and coverage.out * Add README --------- Co-authored-by: nullism <nullism@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
181 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package bqb | ||
|
||
// Dialect holds the Query dialect | ||
type Dialect string | ||
|
||
const ( | ||
// PGSQL postgres dialect | ||
PGSQL Dialect = "postgres" | ||
// MYSQL MySQL dialect | ||
MYSQL Dialect = "mysql" | ||
// RAW dialect uses no parameter conversion | ||
RAW Dialect = "raw" | ||
// SQL generic dialect | ||
SQL Dialect = "sql" | ||
|
||
paramPh = "{{xX_PARAM_Xx}}" | ||
) | ||
|
||
// Embedder embeds a value directly into a query string. | ||
// Note: Since this is embedded and not bound, | ||
// attention must be paid to sanitizing this input. | ||
type Embedder interface { | ||
RawValue() string | ||
} | ||
|
||
// JsonMap is a custom type which tells bqb to convert the parameter to | ||
// a JSON object without requiring reflection. | ||
type JsonMap map[string]interface{} | ||
|
||
// JsonList is a type that tells bqb to convert the parameter to a JSON | ||
// list without requiring reflection. | ||
type JsonList []interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package bqb | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
type embedder []string | ||
|
||
func (e embedder) RawValue() string { | ||
return strings.Join(e, ".") | ||
} | ||
|
||
type sortEmbedder string | ||
|
||
const ( | ||
down sortEmbedder = "down" | ||
up sortEmbedder = "up" | ||
) | ||
|
||
func (s sortEmbedder) RawValue() string { | ||
if s == down { | ||
return "DESC" | ||
} | ||
if s == up { | ||
return "ASC" | ||
} | ||
panic("invalid sort direction: " + s) | ||
} | ||
|
||
func TestEmbedder(t *testing.T) { | ||
emb := embedder{"one", "two", "three"} | ||
want := "one.two.three" | ||
|
||
if emb.RawValue() != want { | ||
t.Errorf("Embedder error: want=%v got=%v", want, emb.RawValue()) | ||
} | ||
|
||
q := New("SELECT ? FROM ? WHERE ?=?", embedder{"id"}, embedder{"schema", "table"}, embedder{"name"}, "bound") | ||
sql, args, err := q.ToSql() | ||
|
||
if err != nil { | ||
t.Errorf("got error: %v", err) | ||
} | ||
|
||
want = "SELECT id FROM schema.table WHERE name=?" | ||
if want != sql { | ||
t.Errorf("\n got:%v\nwant:%v", sql, want) | ||
} | ||
|
||
wantArgs := []any{"bound"} | ||
if !reflect.DeepEqual(args, wantArgs) { | ||
t.Errorf("\n got:%v\nwant:%v", args, wantArgs) | ||
} | ||
|
||
sortq := New("SELECT * FROM my_table ORDER BY name ?,?", down, up) | ||
want = "SELECT * FROM my_table ORDER BY name DESC,ASC" | ||
got, _, _ := sortq.ToSql() | ||
if got != want { | ||
t.Errorf("\n got:%v\nwant:%v", got, want) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters