-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package sqlite3 | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/ncruces/go-sqlite3/internal/util" | ||
) | ||
|
||
// Quote quotes a value such that it is safe to embed in SQL text. | ||
func Quote(value any) string { | ||
switch v := value.(type) { | ||
case nil: | ||
return "NULL" | ||
case bool: | ||
if v { | ||
return "TRUE" | ||
} else { | ||
return "FALSE" | ||
} | ||
|
||
case int: | ||
return strconv.Itoa(v) | ||
case int64: | ||
return strconv.FormatInt(v, 10) | ||
case float64: | ||
return strconv.FormatFloat(v, 'g', -1, 64) | ||
case ZeroBlob: | ||
return "zeroblob(" + strconv.FormatInt(int64(v), 10) + ")" | ||
case time.Time: | ||
return "'" + v.Format(time.RFC3339Nano) + "'" | ||
|
||
case string: | ||
if strings.IndexByte(v, 0) >= 0 { | ||
break | ||
} | ||
|
||
buf := make([]byte, 2+len(v)+strings.Count(v, "'")) | ||
|
||
i := 1 | ||
buf[0] = '\'' | ||
for _, b := range []byte(v) { | ||
if b == '\'' { | ||
buf[i] = b | ||
i += 1 | ||
} | ||
buf[i] = b | ||
i += 1 | ||
} | ||
buf[i] = '\'' | ||
return string(buf) | ||
|
||
case []byte: | ||
buf := make([]byte, 3+2*len(v)) | ||
|
||
i := 2 | ||
buf[0] = 'x' | ||
buf[1] = '\'' | ||
for _, b := range v { | ||
const hex = "0123456789ABCDEF" | ||
buf[i+0] = hex[b/16] | ||
buf[i+1] = hex[b%16] | ||
i += 2 | ||
} | ||
buf[i] = '\'' | ||
return string(buf) | ||
} | ||
|
||
panic(util.ValueErr) | ||
} |
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,49 @@ | ||
package tests | ||
|
||
import ( | ||
"math" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ncruces/go-sqlite3" | ||
) | ||
|
||
func TestQuote(t *testing.T) { | ||
tests := []struct { | ||
val any | ||
want string | ||
}{ | ||
{`abc`, "'abc'"}, | ||
{`a"bc`, "'a\"bc'"}, | ||
{`a'bc`, "'a''bc'"}, | ||
{"\x07bc", "'\abc'"}, | ||
{"\x1c\n", "'\x1c\n'"}, | ||
{[]byte("\xB0\x00\x0B"), "x'B0000B'"}, | ||
{"\xB0\x00\x0B", ""}, | ||
|
||
{0, "0"}, | ||
{nil, "NULL"}, | ||
{true, "TRUE"}, | ||
{false, "FALSE"}, | ||
{math.Pi, "3.141592653589793"}, | ||
{int64(math.MaxInt64), "9223372036854775807"}, | ||
{sqlite3.ZeroBlob(64), "zeroblob(64)"}, | ||
{time.Unix(0, 0).UTC(), "'1970-01-01T00:00:00Z'"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.want, func(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil && tt.want != "" { | ||
t.Errorf("Quote(%q) = %v", tt.val, r) | ||
} | ||
}() | ||
|
||
got := sqlite3.Quote(tt.val) | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Quote(%v) = %q, want %q", tt.val, got, tt.want) | ||
} | ||
}) | ||
} | ||
} |