feat: Add delete, drop, insert, truncate, update
This commit is contained in:
56
build/insert.go
Normal file
56
build/insert.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.secnex.io/secnex/pgson/schema"
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
func InsertManySQL(s *schema.Table, data []map[string]any) (string, error) {
|
||||
// Keep unquoted column names for data access
|
||||
columnNames := make([]string, 0, len(data[0]))
|
||||
for column := range data[0] {
|
||||
columnNames = append(columnNames, column)
|
||||
}
|
||||
|
||||
// Create quoted column names for SQL
|
||||
columns := make([]string, len(columnNames))
|
||||
for i, col := range columnNames {
|
||||
columns[i] = utils.SQLQuoteIdent(col)
|
||||
}
|
||||
|
||||
// Create a map for quick field lookup
|
||||
fieldMap := make(map[string]*schema.Field)
|
||||
for i := range s.Schema {
|
||||
fieldMap[s.Schema[i].Name] = &s.Schema[i]
|
||||
}
|
||||
|
||||
values := make([]string, len(data))
|
||||
for i, row := range data {
|
||||
rowValues := make([]string, len(columnNames))
|
||||
for j, colName := range columnNames {
|
||||
value := row[colName]
|
||||
|
||||
if field, exists := fieldMap[colName]; exists && field.Type == "hash" && field.Algorithm != nil {
|
||||
valueStr := fmt.Sprintf("%v", value)
|
||||
hashed, err := utils.Hash(valueStr, *field.Algorithm)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
value = hashed
|
||||
}
|
||||
|
||||
rowValues[j] = utils.SQLQuoteValue(value)
|
||||
}
|
||||
values[i] = fmt.Sprintf("(%s)", strings.Join(rowValues, ", "))
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", utils.SQLQuoteIdent(s.Name), strings.Join(columns, ", "), strings.Join(values, ", "))
|
||||
return query, nil
|
||||
}
|
||||
|
||||
func InsertSQL(s *schema.Table, data map[string]any) (string, error) {
|
||||
return InsertManySQL(s, []map[string]any{data})
|
||||
}
|
||||
Reference in New Issue
Block a user