From 8f9173c1223f7986b8fe0838a177bf0e2bcbae94 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Wed, 13 Mar 2024 14:28:03 +0100 Subject: [PATCH] Cleanups and tests --- builq.go | 22 ---------------------- example_test.go | 11 +++++++++++ write.go | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/builq.go b/builq.go index 601c67a..a375f41 100644 --- a/builq.go +++ b/builq.go @@ -2,7 +2,6 @@ package builq import ( "errors" - "reflect" "strings" ) @@ -134,21 +133,6 @@ func (b *Builder) build() (_ string, _ []any) { return q, resArgs } -func (b *Builder) asSlice(v any) []any { - value := reflect.ValueOf(v) - - if value.Kind() != reflect.Slice { - b.setErr(errNonSliceArgument) - return nil - } - - res := make([]any, value.Len()) - for i := 0; i < value.Len(); i++ { - res[i] = value.Index(i).Interface() - } - return res -} - var ( // errTooFewArguments passed to [Builder.Addf] method. errTooFewArguments = errors.New("too few arguments") @@ -171,9 +155,3 @@ var ( // errNonNumericArg expected number for %d but got something else. errNonNumericArg = errors.New("expected numeric argument") ) - -func (b *Builder) setErr(err error) { - if b.err == nil { - b.err = err - } -} diff --git a/example_test.go b/example_test.go index 89bf9d9..9c9c644 100644 --- a/example_test.go +++ b/example_test.go @@ -41,10 +41,14 @@ func ExampleNew() { panic(err) } + debug := q.DebugBuild() + fmt.Println("query:") fmt.Println(query) fmt.Println("args:") fmt.Println(args) + fmt.Println("debug:") + fmt.Println(debug) // Output: // @@ -55,6 +59,10 @@ func ExampleNew() { // // args: // [42 root] + // debug: + // SELECT foo, bar FROM 'users' + // WHERE active IS TRUE + // AND user_id = 42 OR user = 'root' } func ExampleBuilder() { @@ -113,12 +121,14 @@ func ExampleBuilder_DebugBuild() { cols := builq.Columns{"foo", "bar"} ts := time.Date(2009, time.November, 10, 12, 13, 15, 16, time.UTC) + d := 4 * time.Second var sb builq.Builder sb.Addf("SELECT %s FROM table", cols) sb.Addf("WHERE id = %$", 123) sb.Addf("OR id = %$ + %d", "42", 69.069) sb.Addf("XOR created_at = %$", ts) + sb.Addf("MORE offset = %$", d) sb.Addf("MAYBE IN arr = %$", []int{1, 2, 3}) fmt.Printf("debug:\n%v", sb.DebugBuild()) @@ -129,6 +139,7 @@ func ExampleBuilder_DebugBuild() { // WHERE id = 123 // OR id = '42' + 69.069 // XOR created_at = '2009-11-10 12:13:15:999999' + // MORE offset = '4s' // MAYBE IN arr = '[1 2 3]' } diff --git a/write.go b/write.go index ba99dbe..da09e4f 100644 --- a/write.go +++ b/write.go @@ -2,6 +2,7 @@ package builq import ( "fmt" + "reflect" "strconv" "strings" "time" @@ -155,6 +156,27 @@ func (b *Builder) writeDebug(sb *strings.Builder, arg any) { } } +func (b *Builder) asSlice(v any) []any { + value := reflect.ValueOf(v) + + if value.Kind() != reflect.Slice { + b.setErr(errNonSliceArgument) + return nil + } + + res := make([]any, value.Len()) + for i := 0; i < value.Len(); i++ { + res[i] = value.Index(i).Interface() + } + return res +} + +func (b *Builder) setErr(err error) { + if b.err == nil { + b.err = err + } +} + func (b *Builder) assertNumber(v any) { switch v.(type) { case int, int8, int16, int32, int64,