-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_builder.go
More file actions
55 lines (49 loc) · 1.77 KB
/
query_builder.go
File metadata and controls
55 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"regexp"
"strings"
)
// extractTableName extracts the table name from a query of the form:
// SELECT * FROM "tablename" or SELECT * FROM "table""name" (with escaped quotes)
// Returns the unescaped table name.
func extractTableName(query string) string {
// Match SELECT * FROM "tablename" pattern
// The table name may contain escaped double quotes (doubled)
re := regexp.MustCompile(`(?i)SELECT\s+\*\s+FROM\s+"([^"]*(?:""[^"]*)*)"`)
matches := re.FindStringSubmatch(query)
if len(matches) >= 2 {
// Unescape doubled quotes
return strings.ReplaceAll(matches[1], `""`, `"`)
}
// Fallback: try without quotes (shouldn't happen from replacement scan)
re = regexp.MustCompile(`(?i)SELECT\s+\*\s+FROM\s+(\S+)`)
matches = re.FindStringSubmatch(query)
if len(matches) >= 2 {
return matches[1]
}
return ""
}
// buildProjectedQuery constructs a SQL query with specific columns.
// If columns is empty, uses SELECT *.
// tableName should be unescaped; this function handles escaping.
func buildProjectedQuery(tableName string, columns []string) string {
escapedTable := strings.ReplaceAll(tableName, `"`, `""`)
var columnList string
if len(columns) == 0 {
columnList = "*"
} else {
escapedCols := make([]string, len(columns))
for i, col := range columns {
escapedCols[i] = fmt.Sprintf(`"%s"`, strings.ReplaceAll(col, `"`, `""`))
}
columnList = strings.Join(escapedCols, ", ")
}
return fmt.Sprintf(`SELECT %s FROM "%s"`, columnList, escapedTable)
}
// buildSchemaQuery constructs a query that returns only the schema (no rows).
// Uses WHERE 1=0 to avoid fetching any data.
func buildSchemaQuery(tableName string) string {
escapedTable := strings.ReplaceAll(tableName, `"`, `""`)
return fmt.Sprintf(`SELECT * FROM "%s" WHERE 1=0`, escapedTable)
}