Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr, docs, build scripts and ARCHER_* environment variables. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
commit
bc4a6b504f
99 files changed
+12752
No files matched your search
@@ -0,0 +1,71 @@
|
||||
// Package sqlutil holds small helpers for building SQL text safely.
|
||||
package sqlutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Ident quotes a MySQL identifier.
|
||||
func Ident(s string) string {
|
||||
return "`" + strings.ReplaceAll(s, "`", "``") + "`"
|
||||
}
|
||||
|
||||
// Qualified quotes a database-qualified table name.
|
||||
func Qualified(db, table string) string {
|
||||
if db == "" {
|
||||
return Ident(table)
|
||||
}
|
||||
return Ident(db) + "." + Ident(table)
|
||||
}
|
||||
|
||||
// Columns joins quoted column names with commas.
|
||||
func Columns(cols []string) string {
|
||||
parts := make([]string, len(cols))
|
||||
for i, c := range cols {
|
||||
parts[i] = Ident(c)
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
// Placeholders returns n comma separated question marks.
|
||||
func Placeholders(n int) string {
|
||||
if n <= 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.Repeat("?, ", n-1) + "?"
|
||||
}
|
||||
|
||||
// RowTuple renders a parenthesized, quoted column list.
|
||||
func RowTuple(cols []string) string {
|
||||
return "(" + Columns(cols) + ")"
|
||||
}
|
||||
|
||||
// ParamTuple renders a parenthesized list of n placeholders.
|
||||
func ParamTuple(n int) string {
|
||||
return "(" + Placeholders(n) + ")"
|
||||
}
|
||||
|
||||
// GreaterThan renders a keyset comparison (cols) > (?, ?, ...). For a single
|
||||
// column it emits the simpler col > ?.
|
||||
func GreaterThan(cols []string) string {
|
||||
if len(cols) == 1 {
|
||||
return Ident(cols[0]) + " > ?"
|
||||
}
|
||||
return RowTuple(cols) + " > " + ParamTuple(len(cols))
|
||||
}
|
||||
|
||||
// InList renders (cols) IN ((?,?),(?,?),...). count is the number of tuples.
|
||||
func InList(cols []string, count int) string {
|
||||
if count == 0 {
|
||||
return "1=0"
|
||||
}
|
||||
tuple := ParamTuple(len(cols))
|
||||
tuples := make([]string, count)
|
||||
for i := range tuples {
|
||||
tuples[i] = tuple
|
||||
}
|
||||
if len(cols) == 1 {
|
||||
return Ident(cols[0]) + " IN (" + Placeholders(count) + ")"
|
||||
}
|
||||
return RowTuple(cols) + " IN (" + strings.Join(tuples, ", ") + ")"
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package sqlutil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestIdentQuoting(t *testing.T) {
|
||||
if got := Ident("a`b"); got != "`a``b`" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
if got := Qualified("db", "t"); got != "`db`.`t`" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
if got := Qualified("", "t"); got != "`t`" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGreaterThan(t *testing.T) {
|
||||
if got := GreaterThan([]string{"id"}); got != "`id` > ?" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
if got := GreaterThan([]string{"a", "b"}); got != "(`a`, `b`) > (?, ?)" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInList(t *testing.T) {
|
||||
if got := InList([]string{"id"}, 3); got != "`id` IN (?, ?, ?)" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
if got := InList([]string{"a", "b"}, 2); got != "(`a`, `b`) IN ((?, ?), (?, ?))" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
if got := InList([]string{"id"}, 0); got != "1=0" {
|
||||
t.Fatalf("got %s", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user