feat: Add delete, drop, insert, truncate, update
This commit is contained in:
@@ -5,13 +5,18 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.secnex.io/secnex/pgson/schema"
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
func Create(s *schema.Table) (string, error) {
|
||||
func CreateSQL(s *schema.Table) (string, error) {
|
||||
schemaParts := make([]string, len(s.Schema))
|
||||
for i, field := range s.Schema {
|
||||
schemaParts[i] = field.SQL()
|
||||
}
|
||||
query := fmt.Sprintf("CREATE TABLE %s (%s)", s.Name, strings.Join(schemaParts, ",\n"))
|
||||
|
||||
schemaParts = append(schemaParts, "\"created_at\" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP")
|
||||
schemaParts = append(schemaParts, "\"updated_at\" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP")
|
||||
schemaParts = append(schemaParts, "\"deleted_at\" TIMESTAMP NULL")
|
||||
query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s)", utils.SQLQuoteIdent(s.Name), strings.Join(schemaParts, ", "))
|
||||
return query, nil
|
||||
}
|
||||
|
||||
16
build/delete.go
Normal file
16
build/delete.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.secnex.io/secnex/pgson/schema"
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
func DeleteSQL(s *schema.Table, where string) (string, error) {
|
||||
return fmt.Sprintf("UPDATE %s SET deleted_at = CURRENT_TIMESTAMP WHERE %s = %s", utils.SQLQuoteIdent(s.Name), utils.SQLQuoteIdent(s.PrimaryKey), utils.SQLQuoteValue(where)), nil
|
||||
}
|
||||
|
||||
func HardDeleteSQL(s *schema.Table, where string) (string, error) {
|
||||
return fmt.Sprintf("DELETE FROM %s WHERE %s = %s", utils.SQLQuoteIdent(s.Name), utils.SQLQuoteIdent(s.PrimaryKey), utils.SQLQuoteValue(where)), nil
|
||||
}
|
||||
12
build/drop.go
Normal file
12
build/drop.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.secnex.io/secnex/pgson/schema"
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
func DropSQL(s *schema.Table) (string, error) {
|
||||
return fmt.Sprintf("DROP TABLE IF EXISTS %s", utils.SQLQuoteIdent(s.Name)), nil
|
||||
}
|
||||
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})
|
||||
}
|
||||
19
build/truncate.go
Normal file
19
build/truncate.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.secnex.io/secnex/pgson/schema"
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
func TruncateSQL(s *schema.Table, cascade bool, restartIdentity bool) (string, error) {
|
||||
query := fmt.Sprintf("TRUNCATE TABLE %s", utils.SQLQuoteIdent(s.Name))
|
||||
if cascade {
|
||||
query += " CASCADE"
|
||||
}
|
||||
if restartIdentity {
|
||||
query += " RESTART IDENTITY"
|
||||
}
|
||||
return query, nil
|
||||
}
|
||||
33
build/update.go
Normal file
33
build/update.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.secnex.io/secnex/pgson/schema"
|
||||
"git.secnex.io/secnex/pgson/utils"
|
||||
)
|
||||
|
||||
func UpdateSQL(s *schema.Table, data map[string]any, where string) (string, error) {
|
||||
// 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]
|
||||
}
|
||||
|
||||
setClause := make([]string, 0, len(data))
|
||||
for field, value := range data {
|
||||
// Check if field is of type "hash" and needs hashing
|
||||
if schemaField, exists := fieldMap[field]; exists && schemaField.Type == "hash" && schemaField.Algorithm != nil {
|
||||
valueStr := fmt.Sprintf("%v", value)
|
||||
hashed, err := utils.Hash(valueStr, *schemaField.Algorithm)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
value = hashed
|
||||
}
|
||||
setClause = append(setClause, fmt.Sprintf("%s = %s", utils.SQLQuoteIdent(field), utils.SQLQuoteValue(value)))
|
||||
}
|
||||
setClause = append(setClause, "updated_at = CURRENT_TIMESTAMP")
|
||||
return fmt.Sprintf("UPDATE %s SET %s WHERE %s = %s", utils.SQLQuoteIdent(s.Name), strings.Join(setClause, ", "), utils.SQLQuoteIdent(s.PrimaryKey), utils.SQLQuoteValue(where)), nil
|
||||
}
|
||||
Reference in New Issue
Block a user