Switch composite types for field types
This commit is contained in:
parent
c6922d220a
commit
d99293dba1
109
db.go
109
db.go
@ -3,6 +3,7 @@ package peachy
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -32,28 +33,42 @@ func (db *DB) Close() {
|
|||||||
db.conn.Close()
|
db.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
var fieldTypeMatcher = match.MustCompile("(10:field type%s)")
|
var tableNameMatcher = match.MustCompile("(%s%s)")
|
||||||
|
|
||||||
func parseFieldTypeName(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, error) {
|
func parseTableName(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, error) {
|
||||||
rawName := args[0].Text()
|
rawName := args[0].Text()
|
||||||
|
classOrName := args[1].Text()
|
||||||
|
|
||||||
|
var class, name string
|
||||||
|
var result *string
|
||||||
|
switch classOrName {
|
||||||
|
case "class":
|
||||||
|
result = &class
|
||||||
|
case "name":
|
||||||
|
result = &name
|
||||||
|
default:
|
||||||
|
return sqlite.Value{}, errors.New(`parse_table_name: 2nd arg must be "class" or "name"`)
|
||||||
|
}
|
||||||
|
|
||||||
sexp, err := csexp.ParseString(rawName)
|
sexp, err := csexp.ParseString(rawName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sqlite.Value{}, nil
|
return sqlite.Value{}, nil
|
||||||
}
|
}
|
||||||
var name string
|
err = tableNameMatcher.Match(sexp, &class, &name)
|
||||||
err = fieldTypeMatcher.Match(sexp, &name)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Printf("parseTableName: unexpected sexp structure for %s", rawName)
|
||||||
return sqlite.Value{}, nil
|
return sqlite.Value{}, nil
|
||||||
}
|
}
|
||||||
return sqlite.TextValue(name), nil
|
|
||||||
|
return sqlite.TextValue(*result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupConn(conn *sqlite.Conn) error {
|
func setupConn(conn *sqlite.Conn) error {
|
||||||
return conn.CreateFunction("parse_field_type_name", &sqlite.FunctionImpl{
|
return conn.CreateFunction("parse_table_name", &sqlite.FunctionImpl{
|
||||||
NArgs: 1,
|
NArgs: 2,
|
||||||
Deterministic: true,
|
Deterministic: true,
|
||||||
AllowIndirect: true,
|
AllowIndirect: true,
|
||||||
Scalar: parseFieldTypeName,
|
Scalar: parseTableName,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,41 +140,83 @@ func quoteName(name string) string {
|
|||||||
return `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
|
return `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const addFieldTypeQueryFmt = `CREATE TABLE %s (
|
type CompositeKind int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Record CompositeKind = iota
|
||||||
|
Variant
|
||||||
|
)
|
||||||
|
|
||||||
|
type CompositeType struct {
|
||||||
|
Name string
|
||||||
|
Kind CompositeKind
|
||||||
|
}
|
||||||
|
|
||||||
|
const addConcreteCompositeTypeQuery = `CREATE TABLE %s (
|
||||||
id INTEGER PRIMARY KEY
|
id INTEGER PRIMARY KEY
|
||||||
);`
|
);`
|
||||||
|
|
||||||
type FieldType struct {
|
const addAbstractCompositeTypeQuery = `CREATE TABLE %s (
|
||||||
Name string
|
id INTEGER PRIMARY KEY,
|
||||||
|
%s_value_id INTEGER NOT NULL REFERENCES %s(id)
|
||||||
|
);`
|
||||||
|
|
||||||
|
func (db *DB) AddCompositeType(name string, kind CompositeKind) error {
|
||||||
|
var kindStr string
|
||||||
|
switch kind {
|
||||||
|
case Record:
|
||||||
|
kindStr = "record"
|
||||||
|
case Variant:
|
||||||
|
kindStr = "variant"
|
||||||
|
default:
|
||||||
|
return errors.New("invalid kind")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) AddFieldType(name string) error {
|
abstractTableName := quoteName(csexp.List{
|
||||||
tableName := csexp.List{csexp.Atom("field type"), csexp.Atom(name)}.String()
|
csexp.Atom("composite-value"),
|
||||||
quotedName := quoteName(tableName)
|
csexp.Atom(name),
|
||||||
query := fmt.Sprintf(addFieldTypeQueryFmt, quotedName)
|
}.String())
|
||||||
err := sqlitex.Execute(db.conn, query, nil)
|
concreteTableName := quoteName(csexp.List{
|
||||||
|
csexp.Atom(kindStr + "-value"),
|
||||||
|
csexp.Atom(name),
|
||||||
|
}.String())
|
||||||
|
|
||||||
|
err := sqlitex.Execute(db.conn, fmt.Sprintf(addConcreteCompositeTypeQuery, concreteTableName), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("AddFieldType: %w", err)
|
return fmt.Errorf("AddCompositeType: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = sqlitex.Execute(db.conn, fmt.Sprintf(addAbstractCompositeTypeQuery, abstractTableName, kindStr, concreteTableName), nil)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("AddCompositeType: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const getFieldTypesQuery = `SELECT parse_field_type_name(name)
|
const getCompositeTypesQuery = `SELECT
|
||||||
|
parse_table_name(name, 'name'),
|
||||||
|
parse_table_name(name, 'class')
|
||||||
FROM sqlite_schema
|
FROM sqlite_schema
|
||||||
WHERE parse_field_type_name(name) IS NOT NULL
|
WHERE parse_table_name(name, 'class') IN ('record-value', 'variant-value')
|
||||||
ORDER BY parse_field_type_name(name) ASC`
|
ORDER BY parse_table_name(name, 'name') ASC;`
|
||||||
|
|
||||||
func (db *DB) GetFieldTypes() ([]FieldType, error) {
|
func (db *DB) GetCompositeTypes() ([]CompositeType, error) {
|
||||||
var results []FieldType
|
var results []CompositeType
|
||||||
err := sqlitex.Execute(db.conn, getFieldTypesQuery, &sqlitex.ExecOptions{
|
err := sqlitex.Execute(db.conn, getCompositeTypesQuery, &sqlitex.ExecOptions{
|
||||||
ResultFunc: func(stmt *sqlite.Stmt) error {
|
ResultFunc: func(stmt *sqlite.Stmt) error {
|
||||||
name := stmt.ColumnText(0)
|
result := CompositeType{Name: stmt.ColumnText(0)}
|
||||||
results = append(results, FieldType{Name: name})
|
if stmt.ColumnText(1) == "record-value" {
|
||||||
|
result.Kind = Record
|
||||||
|
} else {
|
||||||
|
result.Kind = Variant
|
||||||
|
}
|
||||||
|
results = append(results, result)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("GetFieldTypes: %w", err)
|
return nil, fmt.Errorf("GetCompositeTypes: %w", err)
|
||||||
}
|
}
|
||||||
return results, nil
|
return results, nil
|
||||||
}
|
}
|
||||||
|
22
db_test.go
22
db_test.go
@ -6,7 +6,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.codemonkeysoftware.net/b/peachy-go"
|
"git.codemonkeysoftware.net/b/peachy-go"
|
||||||
"github.com/shoenig/test"
|
|
||||||
"github.com/shoenig/test/must"
|
"github.com/shoenig/test/must"
|
||||||
"zombiezen.com/go/sqlite"
|
"zombiezen.com/go/sqlite"
|
||||||
)
|
)
|
||||||
@ -69,23 +68,22 @@ func TestOpen(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFieldTypes(t *testing.T) {
|
func TestCompositeTypes(t *testing.T) {
|
||||||
db, err := peachy.Create(":memory:")
|
db, err := peachy.Create(":memory:")
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
names := []string{"alpha", `"bravo`, " charl!e"}
|
addedTypes := []peachy.CompositeType{
|
||||||
for _, name := range names {
|
{Name: "alpha", Kind: peachy.Record},
|
||||||
err := db.AddFieldType(name)
|
{Name: `"bravo`, Kind: peachy.Variant},
|
||||||
|
{Name: " charl!e"},
|
||||||
|
}
|
||||||
|
for _, ty := range addedTypes {
|
||||||
|
err := db.AddCompositeType(ty.Name, ty.Kind)
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldTypes, err := db.GetFieldTypes()
|
gotTypes, err := db.GetCompositeTypes()
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
for _, name := range names {
|
must.SliceContainsAllOp(t, addedTypes, gotTypes)
|
||||||
test.SliceContainsFunc(t, fieldTypes, name,
|
|
||||||
func(ft peachy.FieldType, s string) bool {
|
|
||||||
return ft.Name == s
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -105,8 +105,10 @@ list := (list <element-type> <parent-object-type> <field-name>)
|
|||||||
<record|variant>-value-id references (<record|variant>-value <composite-type-name>)
|
<record|variant>-value-id references (<record|variant>-value <composite-type-name>)
|
||||||
(record-value <composite-type-name>)
|
(record-value <composite-type-name>)
|
||||||
id integer primary key
|
id integer primary key
|
||||||
(<composite-type-name> <field-name>) <db-type> ...
|
(<composite-type-name> <field-name>) integer references (composite-value <composite-type-name>) ...
|
||||||
|
(primitive <primitive-type-name> <field-name>) <primitive-type-impl-name> ...
|
||||||
(variant-value <composite-type-name>)
|
(variant-value <composite-type-name>)
|
||||||
id integer primary key
|
id integer primary key
|
||||||
(<composite-type-name> <field-name>) <db-type> ...
|
(<composite-type-name> <field-name>) integer references (composite-value <composite-type-name>) ...
|
||||||
|
(primitive <primitive-type-name> <field-name>) <primitive-type-impl-name> ...
|
||||||
```
|
```
|
2
go.mod
2
go.mod
@ -27,3 +27,5 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
replace git.codemonkeysoftware.net/b/gigaparsec v0.0.0-20240917174243-4fa3c2a46611 => ../gigaparsec
|
replace git.codemonkeysoftware.net/b/gigaparsec v0.0.0-20240917174243-4fa3c2a46611 => ../gigaparsec
|
||||||
|
|
||||||
|
replace github.com/shoenig/test v1.11.0 => ../shoenig-test
|
||||||
|
Loading…
Reference in New Issue
Block a user