Refactor sqlmodel pkg (#364)

* Refactor sqlmodel pkg
This commit is contained in:
Neil O'Toole 2024-01-25 00:42:51 -07:00 committed by GitHub
parent 26f0c9a381
commit 2898a92983
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
43 changed files with 157 additions and 157 deletions

View File

@ -30,7 +30,7 @@ fmt:
@# to mangle Go code that is guarded by build tags that @# to mangle Go code that is guarded by build tags that
@# are not in use. @# are not in use.
@goimports-reviser -company-prefixes github.com/neilotoole -set-alias \ @goimports-reviser -company-prefixes github.com/neilotoole -set-alias \
-excludes *_windows.go \ -excludes '*_windows.go' \
-rm-unused -output write \ -rm-unused -output write \
-project-name github.com/neilotoole/sq ./... -project-name github.com/neilotoole/sq ./...

View File

@ -16,7 +16,7 @@ import (
"github.com/neilotoole/sq/cli" "github.com/neilotoole/sq/cli"
"github.com/neilotoole/sq/cli/testrun" "github.com/neilotoole/sq/cli/testrun"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/testh" "github.com/neilotoole/sq/testh"
@ -86,7 +86,7 @@ func TestCreateTable_bytes(t *testing.T) {
th, src, _, _, _ := testh.NewWith(t, handle) th, src, _, _, _ := testh.NewWith(t, handle)
th.DiffDB(src) th.DiffDB(src)
tblDef := sqlmodel.NewTableDef( tblDef := schema.NewTable(
stringz.UniqTableName("test_bytes"), stringz.UniqTableName("test_bytes"),
[]string{"col_name", "col_bytes"}, []string{"col_name", "col_bytes"},
[]kind.Kind{kind.Text, kind.Bytes}, []kind.Kind{kind.Text, kind.Bytes},
@ -119,7 +119,7 @@ func TestOutputRaw(t *testing.T) {
_, err := gif.Decode(bytes.NewReader(wantBytes)) _, err := gif.Decode(bytes.NewReader(wantBytes))
require.NoError(t, err) require.NoError(t, err)
tblDef := sqlmodel.NewTableDef( tblDef := schema.NewTable(
stringz.UniqTableName("test_bytes"), stringz.UniqTableName("test_bytes"),
[]string{"col_name", "col_bytes"}, []string{"col_name", "col_bytes"},
[]kind.Kind{kind.Text, kind.Bytes}, []kind.Kind{kind.Text, kind.Bytes},

View File

@ -12,7 +12,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg" "github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
) )
@ -105,12 +105,12 @@ func mungeCSV2InsertRecord(ctx context.Context, mungers []kind.MungeFunc, csvRec
return a, nil return a, nil
} }
func createTblDef(tblName string, colNames []string, kinds []kind.Kind) *sqlmodel.TableDef { func createTblDef(tblName string, colNames []string, kinds []kind.Kind) *schema.Table {
tbl := &sqlmodel.TableDef{Name: tblName} tbl := &schema.Table{Name: tblName}
cols := make([]*sqlmodel.ColDef, len(colNames)) cols := make([]*schema.Column, len(colNames))
for i := range colNames { for i := range colNames {
cols[i] = &sqlmodel.ColDef{Table: tbl, Name: colNames[i], Kind: kinds[i]} cols[i] = &schema.Column{Table: tbl, Name: colNames[i], Kind: kinds[i]}
} }
tbl.Cols = cols tbl.Cols = cols
@ -118,7 +118,7 @@ func createTblDef(tblName string, colNames []string, kinds []kind.Kind) *sqlmode
} }
// getIngestRecMeta returns record.Meta to use with RecordWriter.Open. // getIngestRecMeta returns record.Meta to use with RecordWriter.Open.
func getIngestRecMeta(ctx context.Context, destGrip driver.Grip, tblDef *sqlmodel.TableDef) (record.Meta, error) { func getIngestRecMeta(ctx context.Context, destGrip driver.Grip, tblDef *schema.Table) (record.Meta, error) {
db, err := destGrip.DB(ctx) db, err := destGrip.DB(ctx)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -17,7 +17,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
@ -54,7 +54,7 @@ var (
) )
// getRecMeta returns record.Meta to use with RecordWriter.Open. // getRecMeta returns record.Meta to use with RecordWriter.Open.
func getRecMeta(ctx context.Context, grip driver.Grip, tblDef *sqlmodel.TableDef) (record.Meta, error) { func getRecMeta(ctx context.Context, grip driver.Grip, tblDef *schema.Table) (record.Meta, error) {
db, err := grip.DB(ctx) db, err := grip.DB(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -96,8 +96,8 @@ type processor struct {
// if flattened is true, the JSON object will be flattened into a single table. // if flattened is true, the JSON object will be flattened into a single table.
flatten bool flatten bool
root *entity root *entity
schema *importSchema importSchema *ingestSchema
colNamesOrdered []string colNamesOrdered []string
@ -111,7 +111,7 @@ type processor struct {
func newProcessor(flatten bool) *processor { func newProcessor(flatten bool) *processor {
return &processor{ return &processor{
flatten: flatten, flatten: flatten,
schema: &importSchema{}, importSchema: &ingestSchema{},
root: &entity{name: source.MonotableName, detectors: map[string]*kind.Detector{}}, root: &entity{name: source.MonotableName, detectors: map[string]*kind.Detector{}},
schemaDirtyEntities: map[*entity]struct{}{}, schemaDirtyEntities: map[*entity]struct{}{},
} }
@ -146,21 +146,21 @@ func (p *processor) calcColName(ent *entity, fieldName string) string {
} }
// buildSchemaFlat currently only builds a flat (single table) schema. // buildSchemaFlat currently only builds a flat (single table) schema.
func (p *processor) buildSchemaFlat() (*importSchema, error) { func (p *processor) buildSchemaFlat() (*ingestSchema, error) {
tblDef := &sqlmodel.TableDef{ tblDef := &schema.Table{
Name: source.MonotableName, Name: source.MonotableName,
} }
var colDefs []*sqlmodel.ColDef var colDefs []*schema.Column
schema := &importSchema{ schma := &ingestSchema{
colMungeFns: map[*sqlmodel.ColDef]kind.MungeFunc{}, colMungeFns: map[*schema.Column]kind.MungeFunc{},
entityTbls: map[*entity]*sqlmodel.TableDef{}, entityTbls: map[*entity]*schema.Table{},
tblDefs: []*sqlmodel.TableDef{tblDef}, // Single table only because flat tblDefs: []*schema.Table{tblDef}, // Single table only because flat
} }
visitFn := func(e *entity) error { visitFn := func(e *entity) error {
schema.entityTbls[e] = tblDef schma.entityTbls[e] = tblDef
for _, field := range e.fieldNames { for _, field := range e.fieldNames {
if detector, ok := e.detectors[field]; ok { if detector, ok := e.detectors[field]; ok {
@ -174,7 +174,7 @@ func (p *processor) buildSchemaFlat() (*importSchema, error) {
k = kind.Text k = kind.Text
} }
colDef := &sqlmodel.ColDef{ colDef := &schema.Column{
Name: p.calcColName(e, field), Name: p.calcColName(e, field),
Table: tblDef, Table: tblDef,
Kind: k, Kind: k,
@ -182,7 +182,7 @@ func (p *processor) buildSchemaFlat() (*importSchema, error) {
colDefs = append(colDefs, colDef) colDefs = append(colDefs, colDef)
if mungeFn != nil { if mungeFn != nil {
schema.colMungeFns[colDef] = mungeFn schma.colMungeFns[colDef] = mungeFn
} }
continue continue
} }
@ -205,11 +205,11 @@ func (p *processor) buildSchemaFlat() (*importSchema, error) {
} }
} }
return schema, nil return schma, nil
} }
// processObject processes the parsed JSON object m. If the structure // processObject processes the parsed JSON object m. If the structure
// of the importSchema changes due to this object, dirtySchema returns true. // of the ingestSchema changes due to this object, dirtySchema returns true.
func (p *processor) processObject(m map[string]any, chunk []byte) (dirtySchema bool, err error) { func (p *processor) processObject(m map[string]any, chunk []byte) (dirtySchema bool, err error) {
p.curObjVals = objectValueSet{} p.curObjVals = objectValueSet{}
err = p.doAddObject(p.root, m) err = p.doAddObject(p.root, m)
@ -317,12 +317,12 @@ func (p *processor) doAddObject(ent *entity, m map[string]any) error {
// buildInsertionsFlat builds a set of DB insertions from the // buildInsertionsFlat builds a set of DB insertions from the
// processor's unwrittenObjVals. After a non-error return, unwrittenObjVals // processor's unwrittenObjVals. After a non-error return, unwrittenObjVals
// is empty. // is empty.
func (p *processor) buildInsertionsFlat(schema *importSchema) ([]*insertion, error) { func (p *processor) buildInsertionsFlat(schma *ingestSchema) ([]*insertion, error) {
if len(schema.tblDefs) != 1 { if len(schma.tblDefs) != 1 {
return nil, errz.Errorf("expected 1 table for flat JSON processing but got %d", len(schema.tblDefs)) return nil, errz.Errorf("expected 1 table for flat JSON processing but got %d", len(schma.tblDefs))
} }
tblDef := schema.tblDefs[0] tblDef := schma.tblDefs[0]
var insertions []*insertion var insertions []*insertion
// Each of unwrittenObjVals is effectively an INSERT row // Each of unwrittenObjVals is effectively an INSERT row
@ -423,19 +423,19 @@ func walkEntity(ent *entity, visitFn func(*entity) error) error {
return nil return nil
} }
// importSchema encapsulates the table definitions that // ingestSchema encapsulates the table definitions that
// the JSON is imported to. // the JSON is ingested to.
type importSchema struct { type ingestSchema struct {
tblDefs []*sqlmodel.TableDef tblDefs []*schema.Table
colMungeFns map[*sqlmodel.ColDef]kind.MungeFunc colMungeFns map[*schema.Column]kind.MungeFunc
// entityTbls is a mapping of entity to the table in which // entityTbls is a mapping of entity to the table in which
// the entity's fields will be inserted. // the entity's fields will be inserted.
entityTbls map[*entity]*sqlmodel.TableDef entityTbls map[*entity]*schema.Table
} }
func execSchemaDelta(ctx context.Context, drvr driver.SQLDriver, db sqlz.DB, func execSchemaDelta(ctx context.Context, drvr driver.SQLDriver, db sqlz.DB,
curSchema, newSchema *importSchema, curSchema, newSchema *ingestSchema,
) error { ) error {
log := lg.FromContext(ctx) log := lg.FromContext(ctx)
var err error var err error

View File

@ -167,7 +167,7 @@ func ingestJSON(ctx context.Context, job ingestJob) error {
obj map[string]any obj map[string]any
chunk []byte chunk []byte
schemaModified bool schemaModified bool
curSchema *importSchema curSchema *ingestSchema
insertions []*insertion insertions []*insertion
hasMore bool hasMore bool
) )
@ -188,7 +188,7 @@ func ingestJSON(ctx context.Context, job ingestJob) error {
log.Debug("First time building the schema") log.Debug("First time building the schema")
} }
var newSchema *importSchema var newSchema *ingestSchema
newSchema, err = proc.buildSchemaFlat() newSchema, err = proc.buildSchemaFlat()
if err != nil { if err != nil {
return err return err

View File

@ -15,7 +15,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/files" "github.com/neilotoole/sq/libsq/files"
@ -127,7 +127,7 @@ func ingestJSONA(ctx context.Context, job ingestJob) error {
} }
// And now we need to create the dest table in destGrip // And now we need to create the dest table in destGrip
tblDef := sqlmodel.NewTableDef(source.MonotableName, colNames, colKinds) tblDef := schema.NewTable(source.MonotableName, colNames, colKinds)
db, err := job.destGrip.DB(ctx) db, err := job.destGrip.DB(ctx)
if err != nil { if err != nil {
return err return err

View File

@ -115,7 +115,7 @@ func ingestJSONL(ctx context.Context, job ingestJob) error { //nolint:gocognit
hasMore bool hasMore bool
schemaModified bool schemaModified bool
line []byte line []byte
curSchema *importSchema curSchema *ingestSchema
insertions []*insertion insertions []*insertion
) )
@ -132,7 +132,7 @@ func ingestJSONL(ctx context.Context, job ingestJob) error { //nolint:gocognit
log.Debug("First time building the schema") log.Debug("First time building the schema")
} }
var newSchema *importSchema var newSchema *ingestSchema
newSchema, err = proc.buildSchemaFlat() newSchema, err = proc.buildSchemaFlat()
if err != nil { if err != nil {
return err return err

View File

@ -24,7 +24,7 @@ import (
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/retry" "github.com/neilotoole/sq/libsq/core/retry"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
@ -165,7 +165,7 @@ func (d *driveri) DropSchema(ctx context.Context, db sqlz.DB, schemaName string)
} }
// CreateTable implements driver.SQLDriver. // CreateTable implements driver.SQLDriver.
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error { func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *schema.Table) error {
createStmt := buildCreateTableStmt(tblDef) createStmt := buildCreateTableStmt(tblDef)
_, err := db.ExecContext(ctx, createStmt) _, err := db.ExecContext(ctx, createStmt)
@ -208,11 +208,11 @@ func (d *driveri) ListSchemas(ctx context.Context, db sqlz.DB) ([]string, error)
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows) defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
for rows.Next() { for rows.Next() {
var schema string var schma string
if err = rows.Scan(&schema); err != nil { if err = rows.Scan(&schma); err != nil {
return nil, errz.Err(err) return nil, errz.Err(err)
} }
schemas = append(schemas, schema) schemas = append(schemas, schma)
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {

View File

@ -5,7 +5,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/testh" "github.com/neilotoole/sq/testh"
@ -44,7 +44,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name()) tblName := stringz.UniqTableName(t.Name())
colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false) colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false)
tblDef := sqlmodel.NewTableDef(tblName, colNames, colKinds) tblDef := schema.NewTable(tblName, colNames, colKinds)
for _, colDef := range tblDef.Cols { for _, colDef := range tblDef.Cols {
colDef.NotNull = true colDef.NotNull = true
colDef.HasDefault = true colDef.HasDefault = true

View File

@ -9,7 +9,7 @@ import (
"github.com/neilotoole/sq/libsq/ast/render" "github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
) )
@ -58,7 +58,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive
} }
//nolint:funlen //nolint:funlen
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string { func buildCreateTableStmt(tblDef *schema.Table) string {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
cols := make([]string, len(tblDef.Cols)) cols := make([]string, len(tblDef.Cols))

View File

@ -25,7 +25,7 @@ import (
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/retry" "github.com/neilotoole/sq/libsq/core/retry"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
@ -299,7 +299,7 @@ func (d *driveri) DropSchema(ctx context.Context, db sqlz.DB, schemaName string)
} }
// CreateTable implements driver.SQLDriver. // CreateTable implements driver.SQLDriver.
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error { func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *schema.Table) error {
stmt := buildCreateTableStmt(tblDef) stmt := buildCreateTableStmt(tblDef)
_, err := db.ExecContext(ctx, stmt) _, err := db.ExecContext(ctx, stmt)
@ -333,11 +333,11 @@ func (d *driveri) ListSchemas(ctx context.Context, db sqlz.DB) ([]string, error)
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows) defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
for rows.Next() { for rows.Next() {
var schema string var schma string
if err = rows.Scan(&schema); err != nil { if err = rows.Scan(&schma); err != nil {
return nil, errw(err) return nil, errw(err)
} }
schemas = append(schemas, schema) schemas = append(schemas, schma)
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {

View File

@ -15,7 +15,7 @@ import (
"github.com/neilotoole/sq/drivers/postgres" "github.com/neilotoole/sq/drivers/postgres"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/lg" "github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/testh" "github.com/neilotoole/sq/testh"
@ -163,7 +163,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name()) tblName := stringz.UniqTableName(t.Name())
colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false) colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false)
tblDef := sqlmodel.NewTableDef(tblName, colNames, colKinds) tblDef := schema.NewTable(tblName, colNames, colKinds)
for _, colDef := range tblDef.Cols { for _, colDef := range tblDef.Cols {
colDef.NotNull = true colDef.NotNull = true
colDef.HasDefault = true colDef.HasDefault = true

View File

@ -7,7 +7,7 @@ import (
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
) )
func dbTypeNameFromKind(knd kind.Kind) string { func dbTypeNameFromKind(knd kind.Kind) string {
@ -54,7 +54,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive
// buildCreateTableStmt builds a CREATE TABLE statement from tblDef. // buildCreateTableStmt builds a CREATE TABLE statement from tblDef.
// The implementation is minimal: it does not honor PK, FK, etc. // The implementation is minimal: it does not honor PK, FK, etc.
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string { func buildCreateTableStmt(tblDef *schema.Table) string {
sb := strings.Builder{} sb := strings.Builder{}
sb.WriteString(`CREATE TABLE "`) sb.WriteString(`CREATE TABLE "`)
sb.WriteString(tblDef.Name) sb.WriteString(tblDef.Name)

View File

@ -6,7 +6,7 @@ import (
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
) )
// createTblKindDefaults is a mapping of Kind to the value // createTblKindDefaults is a mapping of Kind to the value
@ -24,7 +24,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive // ignore
kind.Unknown: `DEFAULT ''`, kind.Unknown: `DEFAULT ''`,
} }
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string { func buildCreateTableStmt(tblDef *schema.Table) string {
var buf *bytes.Buffer var buf *bytes.Buffer
cols := make([]string, len(tblDef.Cols)) cols := make([]string, len(tblDef.Cols))

View File

@ -29,7 +29,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/loz" "github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
@ -654,7 +654,7 @@ func (d *driveri) DropSchema(ctx context.Context, db sqlz.DB, schemaName string)
} }
// CreateTable implements driver.SQLDriver. // CreateTable implements driver.SQLDriver.
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error { func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *schema.Table) error {
query := buildCreateTableStmt(tblDef) query := buildCreateTableStmt(tblDef)
stmt, err := db.PrepareContext(ctx, query) stmt, err := db.PrepareContext(ctx, query)
@ -695,11 +695,11 @@ func (d *driveri) ListSchemas(ctx context.Context, db sqlz.DB) ([]string, error)
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows) defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
for rows.Next() { for rows.Next() {
var schema string var schma string
if err = rows.Scan(&schema); err != nil { if err = rows.Scan(&schma); err != nil {
return nil, errz.Err(err) return nil, errz.Err(err)
} }
schemas = append(schemas, schema) schemas = append(schemas, schma)
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/neilotoole/sq/drivers/sqlite3" "github.com/neilotoole/sq/drivers/sqlite3"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
@ -172,7 +172,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name()) tblName := stringz.UniqTableName(t.Name())
colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false) colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false)
tblDef := sqlmodel.NewTableDef(tblName, colNames, colKinds) tblDef := schema.NewTable(tblName, colNames, colKinds)
for _, colDef := range tblDef.Cols { for _, colDef := range tblDef.Cols {
colDef.NotNull = true colDef.NotNull = true
colDef.HasDefault = true colDef.HasDefault = true

View File

@ -9,7 +9,7 @@ import (
"github.com/neilotoole/sq/libsq/ast/render" "github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
) )
func renderRange(_ *render.Context, rr *ast.RowRangeNode) (string, error) { func renderRange(_ *render.Context, rr *ast.RowRangeNode) (string, error) {
@ -106,7 +106,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive
// buildCreateTableStmt builds a CREATE TABLE statement from tblDef. // buildCreateTableStmt builds a CREATE TABLE statement from tblDef.
// The implementation is minimal: it does not honor PK, FK, etc. // The implementation is minimal: it does not honor PK, FK, etc.
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string { func buildCreateTableStmt(tblDef *schema.Table) string {
sb := strings.Builder{} sb := strings.Builder{}
sb.WriteString(`CREATE TABLE "`) sb.WriteString(`CREATE TABLE "`)
sb.WriteString(tblDef.Name) sb.WriteString(tblDef.Name)

View File

@ -21,7 +21,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/loz" "github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
@ -388,11 +388,11 @@ func (d *driveri) ListSchemas(ctx context.Context, db sqlz.DB) ([]string, error)
defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows) defer lg.WarnIfCloseError(log, lgm.CloseDBRows, rows)
for rows.Next() { for rows.Next() {
var schema string var schma string
if err = rows.Scan(&schema); err != nil { if err = rows.Scan(&schma); err != nil {
return nil, errz.Err(err) return nil, errz.Err(err)
} }
schemas = append(schemas, schema) schemas = append(schemas, schma)
} }
if err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
@ -512,7 +512,7 @@ func (d *driveri) DropSchema(ctx context.Context, db sqlz.DB, schemaName string)
} }
// CreateTable implements driver.SQLDriver. // CreateTable implements driver.SQLDriver.
func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error { func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *schema.Table) error {
stmt := buildCreateTableStmt(tblDef) stmt := buildCreateTableStmt(tblDef)
_, err := db.ExecContext(ctx, stmt) _, err := db.ExecContext(ctx, stmt)
@ -529,26 +529,26 @@ func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl, col
// AlterTableRename implements driver.SQLDriver. // AlterTableRename implements driver.SQLDriver.
func (d *driveri) AlterTableRename(ctx context.Context, db sqlz.DB, tbl, newName string) error { func (d *driveri) AlterTableRename(ctx context.Context, db sqlz.DB, tbl, newName string) error {
schema, err := d.CurrentSchema(ctx, db) schma, err := d.CurrentSchema(ctx, db)
if err != nil { if err != nil {
return err return err
} }
q := fmt.Sprintf(`exec sp_rename '[%s].[%s]', '%s'`, schema, tbl, newName) q := fmt.Sprintf(`exec sp_rename '[%s].[%s]', '%s'`, schma, tbl, newName)
_, err = db.ExecContext(ctx, q) _, err = db.ExecContext(ctx, q)
return errz.Wrapf(errw(err), "alter table: failed to rename table %q to %q", tbl, newName) return errz.Wrapf(errw(err), "alter table: failed to rename table %q to %q", tbl, newName)
} }
// AlterTableRenameColumn implements driver.SQLDriver. // AlterTableRenameColumn implements driver.SQLDriver.
func (d *driveri) AlterTableRenameColumn(ctx context.Context, db sqlz.DB, tbl, col, newName string) error { func (d *driveri) AlterTableRenameColumn(ctx context.Context, db sqlz.DB, tbl, col, newName string) error {
schema, err := d.CurrentSchema(ctx, db) schma, err := d.CurrentSchema(ctx, db)
if err != nil { if err != nil {
return err return err
} }
q := fmt.Sprintf(`exec sp_rename '[%s].[%s].[%s]', '%s'`, schema, tbl, col, newName) q := fmt.Sprintf(`exec sp_rename '[%s].[%s].[%s]', '%s'`, schma, tbl, col, newName)
_, err = db.ExecContext(ctx, q) _, err = db.ExecContext(ctx, q)
return errz.Wrapf(errw(err), "alter table: failed to rename column {%s.%s.%s} to {%s}", schema, tbl, col, newName) return errz.Wrapf(errw(err), "alter table: failed to rename column {%s.%s.%s} to {%s}", schma, tbl, col, newName)
} }
// CopyTable implements driver.SQLDriver. // CopyTable implements driver.SQLDriver.

View File

@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/testh" "github.com/neilotoole/sq/testh"
@ -81,7 +81,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name()) tblName := stringz.UniqTableName(t.Name())
colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false) colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false)
tblDef := sqlmodel.NewTableDef(tblName, colNames, colKinds) tblDef := schema.NewTable(tblName, colNames, colKinds)
for _, colDef := range tblDef.Cols { for _, colDef := range tblDef.Cols {
colDef.NotNull = true colDef.NotNull = true
colDef.HasDefault = true colDef.HasDefault = true

View File

@ -6,7 +6,7 @@ import (
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
) )
@ -299,9 +299,9 @@ func validateDefRoot(def *DriverDef) (drvrName string, errs []error) {
} }
// ToTableDef builds a TableDef from the TableMapping. // ToTableDef builds a TableDef from the TableMapping.
func ToTableDef(tblMapping *TableMapping) (*sqlmodel.TableDef, error) { func ToTableDef(tblMapping *TableMapping) (*schema.Table, error) {
tblDef := &sqlmodel.TableDef{Name: tblMapping.Name} tblDef := &schema.Table{Name: tblMapping.Name}
colDefs := make([]*sqlmodel.ColDef, len(tblMapping.Cols)) colDefs := make([]*schema.Column, len(tblMapping.Cols))
pkCols, err := tblMapping.PKCols() pkCols, err := tblMapping.PKCols()
if err != nil { if err != nil {
@ -311,7 +311,7 @@ func ToTableDef(tblMapping *TableMapping) (*sqlmodel.TableDef, error) {
tblDef.PKColName = pkCols[0].Name tblDef.PKColName = pkCols[0].Name
for i, colMapping := range tblMapping.Cols { for i, colMapping := range tblMapping.Cols {
colDef := &sqlmodel.ColDef{Table: tblDef, Name: colMapping.Name, Kind: colMapping.Kind} colDef := &schema.Column{Table: tblDef, Name: colMapping.Name, Kind: colMapping.Kind}
colDefs[i] = colDef colDefs[i] = colDef
} }

View File

@ -20,7 +20,7 @@ import (
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/lg" "github.com/neilotoole/sq/libsq/core/lg"
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source" "github.com/neilotoole/sq/libsq/source"
@ -49,7 +49,7 @@ func Ingest(ctx context.Context, def *userdriver.DriverDef, data io.Reader, dest
def: def, def: def,
selStack: newSelStack(), selStack: newSelStack(),
rowStack: newRowStack(), rowStack: newRowStack(),
tblDefs: map[string]*sqlmodel.TableDef{}, tblDefs: map[string]*schema.Table{},
tblSequence: map[string]int64{}, tblSequence: map[string]int64{},
execInsertFns: map[string]func(ctx context.Context, insertVals []any) error{}, execInsertFns: map[string]func(ctx context.Context, insertVals []any) error{},
execUpdateFns: map[string]func(ctx context.Context, updateVals, whereArgs []any) error{}, execUpdateFns: map[string]func(ctx context.Context, updateVals, whereArgs []any) error{},
@ -74,7 +74,7 @@ type ingester struct {
destDB sqlz.DB destDB sqlz.DB
selStack *selStack selStack *selStack
rowStack *rowStack rowStack *rowStack
tblDefs map[string]*sqlmodel.TableDef tblDefs map[string]*schema.Table
// tblSequence is a map of table name to the last // tblSequence is a map of table name to the last
// insert ID value for that table. See dbInsert for more. // insert ID value for that table. See dbInsert for more.

View File

@ -19,7 +19,7 @@ import (
"github.com/neilotoole/sq/libsq/core/loz" "github.com/neilotoole/sq/libsq/core/loz"
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/progress" "github.com/neilotoole/sq/libsq/core/progress"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source" "github.com/neilotoole/sq/libsq/source"
@ -30,7 +30,7 @@ const msgCloseRowIter = "Close Excel row iterator"
// sheetTable maps a sheet to a database table. // sheetTable maps a sheet to a database table.
type sheetTable struct { type sheetTable struct {
sheet *xSheet sheet *xSheet
def *sqlmodel.TableDef def *schema.Table
colIngestMungeFns []kind.MungeFunc colIngestMungeFns []kind.MungeFunc
hasHeaderRow bool hasHeaderRow bool
} }
@ -400,10 +400,10 @@ func buildSheetTable(ctx context.Context, srcIngestHeader *bool, sheet *xSheet)
return nil, err return nil, err
} }
tblDef := &sqlmodel.TableDef{Name: sheet.name} tblDef := &schema.Table{Name: sheet.name}
cols := make([]*sqlmodel.ColDef, len(colNames)) cols := make([]*schema.Column, len(colNames))
for i, colName := range colNames { for i, colName := range colNames {
cols[i] = &sqlmodel.ColDef{Table: tblDef, Name: colName, Kind: colKinds[i]} cols[i] = &schema.Column{Table: tblDef, Name: colName, Kind: colKinds[i]}
} }
tblDef.Cols = cols tblDef.Cols = cols
lg.FromContext(ctx).Debug("Built table def", lg.FromContext(ctx).Debug("Built table def",

View File

@ -1,5 +1,5 @@
// Package sqlmodel provides functionality for modeling SQL constructs. // Package schema provides functionality for modeling SQL constructs.
package sqlmodel package schema
import ( import (
"strings" "strings"
@ -8,8 +8,8 @@ import (
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
) )
// TableDef models a database table definition. // Table models a database table definition.
type TableDef struct { type Table struct {
// Name is the table name. // Name is the table name.
Name string `json:"name"` Name string `json:"name"`
@ -25,17 +25,17 @@ type TableDef struct {
AutoIncrement bool `json:"auto_increment"` AutoIncrement bool `json:"auto_increment"`
// Cols is the table's column definitions. // Cols is the table's column definitions.
Cols []*ColDef `json:"cols"` Cols []*Column `json:"cols"`
} }
// NewTableDef is a convenience constructor for creating // NewTable is a convenience constructor for creating
// a simple table definition. // a simple table definition.
func NewTableDef(tblName string, colNames []string, colKinds []kind.Kind) *TableDef { func NewTable(tblName string, colNames []string, colKinds []kind.Kind) *Table {
tblDef := &TableDef{Name: tblName} tblDef := &Table{Name: tblName}
cols := make([]*ColDef, len(colNames)) cols := make([]*Column, len(colNames))
for i := range colNames { for i := range colNames {
cols[i] = &ColDef{Table: tblDef, Name: colNames[i], Kind: colKinds[i]} cols[i] = &Column{Table: tblDef, Name: colNames[i], Kind: colKinds[i]}
} }
tblDef.Cols = cols tblDef.Cols = cols
@ -44,7 +44,7 @@ func NewTableDef(tblName string, colNames []string, colKinds []kind.Kind) *Table
// ColNames returns a new slice containing the names // ColNames returns a new slice containing the names
// of t's columns. // of t's columns.
func (t *TableDef) ColNames() []string { func (t *Table) ColNames() []string {
names := make([]string, len(t.Cols)) names := make([]string, len(t.Cols))
for i, col := range t.Cols { for i, col := range t.Cols {
names[i] = col.Name names[i] = col.Name
@ -54,7 +54,7 @@ func (t *TableDef) ColNames() []string {
// ColKinds returns a new slice containing the kinds // ColKinds returns a new slice containing the kinds
// of t's columns. // of t's columns.
func (t *TableDef) ColKinds() []kind.Kind { func (t *Table) ColKinds() []kind.Kind {
kinds := make([]kind.Kind, len(t.Cols)) kinds := make([]kind.Kind, len(t.Cols))
for i, col := range t.Cols { for i, col := range t.Cols {
kinds[i] = col.Kind kinds[i] = col.Kind
@ -62,14 +62,14 @@ func (t *TableDef) ColKinds() []kind.Kind {
return kinds return kinds
} }
func (t *TableDef) String() string { func (t *Table) String() string {
return t.Name + "(" + strings.Join(t.ColNames(), ",") + ")" return t.Name + "(" + strings.Join(t.ColNames(), ",") + ")"
} }
// ColsByName returns the ColDefs for each named column, or an error if any column // ColsByName returns the ColDefs for each named column, or an error if any column
// is not matched. // is not matched.
func (t *TableDef) ColsByName(cols []string) ([]*ColDef, error) { func (t *Table) ColsByName(cols []string) ([]*Column, error) {
defs := make([]*ColDef, len(cols)) defs := make([]*Column, len(cols))
for i, name := range cols { for i, name := range cols {
found := false found := false
@ -88,8 +88,8 @@ func (t *TableDef) ColsByName(cols []string) ([]*ColDef, error) {
return defs, nil return defs, nil
} }
// FindCol returns the named ColDef or nil if not found. // FindCol returns the named Column or nil if not found.
func (t *TableDef) FindCol(name string) (*ColDef, error) { func (t *Table) FindCol(name string) (*Column, error) {
for _, col := range t.Cols { for _, col := range t.Cols {
if col.Name == name { if col.Name == name {
return col, nil return col, nil
@ -98,10 +98,10 @@ func (t *TableDef) FindCol(name string) (*ColDef, error) {
return nil, errz.Errorf("could not find column definition {%s} in table {%s}", name, t.Name) return nil, errz.Errorf("could not find column definition {%s} in table {%s}", name, t.Name)
} }
// ColDef models a table column definition. // Column models a table column definition.
type ColDef struct { type Column struct {
Name string `json:"name"` Name string `json:"name"`
Table *TableDef `json:"-"` Table *Table `json:"-"`
Kind kind.Kind `json:"kind"` Kind kind.Kind `json:"kind"`
NotNull bool `json:"not_null"` NotNull bool `json:"not_null"`

View File

@ -1,4 +1,4 @@
package sqlmodel package sqlparser
import ( import (
"testing" "testing"

View File

@ -1,4 +1,4 @@
package sqlmodel package sqlparser
import ( import (
"bufio" "bufio"

View File

@ -1,4 +1,4 @@
package sqlmodel_test package sqlparser_test
import ( import (
"strings" "strings"
@ -6,16 +6,16 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/sqlparser"
"github.com/neilotoole/sq/testh/proj" "github.com/neilotoole/sq/testh/proj"
) )
func TestSplitInput(t *testing.T) { func TestSplitInput(t *testing.T) {
const sel, other = sqlmodel.StmtSelect, sqlmodel.StmtOther const sel, other = sqlparser.StmtSelect, sqlparser.StmtOther
// convenience func to return a slice of n core.sqlmodel.StmtType. // convenience func to return a slice of n sqlparser.StmtType.
nTypes := func(n int, typ sqlmodel.StmtType) []sqlmodel.StmtType { nTypes := func(n int, typ sqlparser.StmtType) []sqlparser.StmtType {
types := make([]sqlmodel.StmtType, n) types := make([]sqlparser.StmtType, n)
for i := range types { for i := range types {
types[i] = typ types[i] = typ
} }
@ -30,7 +30,7 @@ func TestSplitInput(t *testing.T) {
input string input string
wantCount int wantCount int
wantContains []string wantContains []string
wantTypes []sqlmodel.StmtType wantTypes []sqlparser.StmtType
}{ }{
"simple": { "simple": {
{ {
@ -39,7 +39,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table; ", input: "select * from my_table; ",
wantCount: 1, wantCount: 1,
wantContains: []string{"select * from my_table"}, wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel}, wantTypes: []sqlparser.StmtType{sel},
}, },
{ {
name: "select_1_no_delim", name: "select_1_no_delim",
@ -47,7 +47,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table", input: "select * from my_table",
wantCount: 1, wantCount: 1,
wantContains: []string{"select * from my_table"}, wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel}, wantTypes: []sqlparser.StmtType{sel},
}, },
{ {
name: "select_1_many_delim", name: "select_1_many_delim",
@ -55,7 +55,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\n;;", input: "select * from my_table;\n;;",
wantCount: 1, wantCount: 1,
wantContains: []string{"select * from my_table"}, wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel}, wantTypes: []sqlparser.StmtType{sel},
}, },
{ {
name: "select_2", name: "select_2",
@ -63,7 +63,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ndrop table my_table;", input: "select * from my_table;\ndrop table my_table;",
wantCount: 2, wantCount: 2,
wantContains: []string{"select * from my_table", "drop table my_table"}, wantContains: []string{"select * from my_table", "drop table my_table"},
wantTypes: []sqlmodel.StmtType{sel, other}, wantTypes: []sqlparser.StmtType{sel, other},
}, },
{ {
name: "select_2_no_trailing_delim", name: "select_2_no_trailing_delim",
@ -71,28 +71,28 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ndrop table my_table", input: "select * from my_table;\ndrop table my_table",
wantCount: 2, wantCount: 2,
wantContains: []string{"select * from my_table", "drop table my_table"}, wantContains: []string{"select * from my_table", "drop table my_table"},
wantTypes: []sqlmodel.StmtType{sel, other}, wantTypes: []sqlparser.StmtType{sel, other},
}, },
}, },
"sqlite3": { "sqlite3": {
{ {
name: "sqlite3_type_test.sql", name: "sqlite3_type_test.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/sqlite3_type_test.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/sqlite3_type_test.sql")),
wantCount: 4, wantCount: 4,
wantTypes: nTypes(4, other), wantTypes: nTypes(4, other),
}, },
{ {
name: "sqlite3_address.sql", name: "sqlite3_address.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/sqlite3_address.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/sqlite3_address.sql")),
wantCount: 4, wantCount: 4,
wantTypes: nTypes(4, other), wantTypes: nTypes(4, other),
}, },
{ {
name: "sqlite3_person.sql", name: "sqlite3_person.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/sqlite3_person.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/sqlite3_person.sql")),
wantCount: 10, wantCount: 10,
wantTypes: nTypes(10, other), wantTypes: nTypes(10, other),
}, },
@ -101,21 +101,21 @@ func TestSplitInput(t *testing.T) {
{ {
name: "sqtype_public_type_test.sql", name: "sqtype_public_type_test.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/postgres_public_type_test.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/postgres_public_type_test.sql")),
wantCount: 5, wantCount: 5,
wantTypes: nTypes(5, other), wantTypes: nTypes(5, other),
}, },
{ {
name: "sqtest_public_address.sql", name: "sqtest_public_address.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/postgres_public_address.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/postgres_public_address.sql")),
wantCount: 5, wantCount: 5,
wantTypes: nTypes(5, other), wantTypes: nTypes(5, other),
}, },
{ {
name: "sqtest_public_person.sql", name: "sqtest_public_person.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/postgres_public_person.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/postgres_public_person.sql")),
wantCount: 11, wantCount: 11,
wantTypes: nTypes(11, other), wantTypes: nTypes(11, other),
}, },
@ -128,7 +128,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ngo", input: "select * from my_table;\ngo",
wantCount: 1, wantCount: 1,
wantContains: []string{"select * from my_table"}, wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel}, wantTypes: []sqlparser.StmtType{sel},
}, },
{ {
name: "select_2", name: "select_2",
@ -137,13 +137,13 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ndrop table my_table;", input: "select * from my_table;\ndrop table my_table;",
wantCount: 2, wantCount: 2,
wantContains: []string{"select * from my_table", "drop table my_table"}, wantContains: []string{"select * from my_table", "drop table my_table"},
wantTypes: []sqlmodel.StmtType{sel, other}, wantTypes: []sqlparser.StmtType{sel, other},
}, },
{ {
name: "sqtype_dbo_type_test.sql", name: "sqtype_dbo_type_test.sql",
delim: ";", delim: ";",
moreDelims: []string{"go"}, moreDelims: []string{"go"},
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/sqlserver_dbo_type_test.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/sqlserver_dbo_type_test.sql")),
wantCount: 5, wantCount: 5,
wantTypes: nTypes(5, other), wantTypes: nTypes(5, other),
}, },
@ -151,7 +151,7 @@ func TestSplitInput(t *testing.T) {
name: "sqtest_dbo_address.sql", name: "sqtest_dbo_address.sql",
delim: ";", delim: ";",
moreDelims: []string{"go"}, moreDelims: []string{"go"},
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/sqlserver_dbo_address.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/sqlserver_dbo_address.sql")),
wantCount: 4, wantCount: 4,
wantTypes: nTypes(5, other), wantTypes: nTypes(5, other),
}, },
@ -159,7 +159,7 @@ func TestSplitInput(t *testing.T) {
name: "sqtest_dbo_person.sql", name: "sqtest_dbo_person.sql",
delim: ";", delim: ";",
moreDelims: []string{"go"}, moreDelims: []string{"go"},
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/sqlserver_dbo_person.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/sqlserver_dbo_person.sql")),
wantCount: 10, wantCount: 10,
wantTypes: nTypes(10, other), wantTypes: nTypes(10, other),
}, },
@ -168,21 +168,21 @@ func TestSplitInput(t *testing.T) {
{ {
name: "mysql_type_test.sql", name: "mysql_type_test.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/mysql_type_test.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/mysql_type_test.sql")),
wantCount: 5, wantCount: 5,
wantTypes: nTypes(5, other), wantTypes: nTypes(5, other),
}, },
{ {
name: "mysql_address.sql", name: "mysql_address.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/mysql_address.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/mysql_address.sql")),
wantCount: 4, wantCount: 4,
wantTypes: nTypes(5, other), wantTypes: nTypes(5, other),
}, },
{ {
name: "mysql_person.sql", name: "mysql_person.sql",
delim: ";", delim: ";",
input: string(proj.ReadFile("libsq/core/sqlmodel/testdata/mysql_person.sql")), input: string(proj.ReadFile("libsq/core/sqlparser/testdata/mysql_person.sql")),
wantCount: 9, wantCount: 9,
wantTypes: nTypes(9, other), wantTypes: nTypes(9, other),
}, },
@ -195,7 +195,7 @@ func TestSplitInput(t *testing.T) {
for _, tc := range testGroup { for _, tc := range testGroup {
tc := tc tc := tc
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
stmts, stmtTypes, err := sqlmodel.SplitSQL(strings.NewReader(tc.input), tc.delim, tc.moreDelims...) stmts, stmtTypes, err := sqlparser.SplitSQL(strings.NewReader(tc.input), tc.delim, tc.moreDelims...)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, tc.wantCount, len(stmts)) require.Equal(t, tc.wantCount, len(stmts))
require.Equal(t, len(stmts), len(stmtTypes)) require.Equal(t, len(stmts), len(stmtTypes))

View File

@ -10,7 +10,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lga" "github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
"github.com/neilotoole/sq/libsq/source" "github.com/neilotoole/sq/libsq/source"
@ -63,7 +63,7 @@ func DBWriterCreateTableIfNotExistsHook(destTblName string) DBWriterPreWriteHook
destColNames := recMeta.Names() destColNames := recMeta.Names()
destColKinds := recMeta.Kinds() destColKinds := recMeta.Kinds()
destTblDef := sqlmodel.NewTableDef(destTblName, destColNames, destColKinds) destTblDef := schema.NewTable(destTblName, destColNames, destColKinds)
err = destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef) err = destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef)
if err != nil { if err != nil {

View File

@ -14,7 +14,7 @@ import (
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/progress" "github.com/neilotoole/sq/libsq/core/progress"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/libsq/driver/dialect" "github.com/neilotoole/sq/libsq/driver/dialect"
@ -141,7 +141,7 @@ type SQLDriver interface {
// may not honor every field of tblDef, e.g. an impl might not // may not honor every field of tblDef, e.g. an impl might not
// build the foreign key constraints. At a minimum the implementation // build the foreign key constraints. At a minimum the implementation
// must honor the table name and column names and kinds from tblDef. // must honor the table name and column names and kinds from tblDef.
CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.TableDef) error CreateTable(ctx context.Context, db sqlz.DB, tblDef *schema.Table) error
// CreateSchema creates a new schema in db. Note that db's current // CreateSchema creates a new schema in db. Note that db's current
// connection schema is not changed. // connection schema is not changed.

View File

@ -13,7 +13,7 @@ import (
"github.com/neilotoole/sq/libsq/core/errz" "github.com/neilotoole/sq/libsq/core/errz"
"github.com/neilotoole/sq/libsq/core/kind" "github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
@ -124,7 +124,7 @@ func TestDriver_CreateTable_Minimal(t *testing.T) {
tblName := stringz.UniqTableName(t.Name()) tblName := stringz.UniqTableName(t.Name())
colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false) colNames, colKinds := fixt.ColNamePerKind(drvr.Dialect().IntBool, false, false)
tblDef := sqlmodel.NewTableDef(tblName, colNames, colKinds) tblDef := schema.NewTable(tblName, colNames, colKinds)
err := drvr.CreateTable(th.Context, db, tblDef) err := drvr.CreateTable(th.Context, db, tblDef)
require.NoError(t, err) require.NoError(t, err)

View File

@ -16,7 +16,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/record" "github.com/neilotoole/sq/libsq/core/record"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
"github.com/neilotoole/sq/libsq/driver" "github.com/neilotoole/sq/libsq/driver"
@ -446,7 +446,7 @@ func execCopyTable(ctx context.Context, fromDB driver.Grip, fromTbl tablefq.T,
) error { ) error {
destColNames := originRecMeta.Names() destColNames := originRecMeta.Names()
destColKinds := originRecMeta.Kinds() destColKinds := originRecMeta.Kinds()
destTblDef := sqlmodel.NewTableDef(destTbl.Table, destColNames, destColKinds) destTblDef := schema.NewTable(destTbl.Table, destColNames, destColKinds)
err := destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef) err := destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef)
if err != nil { if err != nil {

View File

@ -42,7 +42,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lgm" "github.com/neilotoole/sq/libsq/core/lg/lgm"
"github.com/neilotoole/sq/libsq/core/lg/lgt" "github.com/neilotoole/sq/libsq/core/lg/lgt"
"github.com/neilotoole/sq/libsq/core/options" "github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/sqlmodel" "github.com/neilotoole/sq/libsq/core/schema"
"github.com/neilotoole/sq/libsq/core/sqlz" "github.com/neilotoole/sq/libsq/core/sqlz"
"github.com/neilotoole/sq/libsq/core/stringz" "github.com/neilotoole/sq/libsq/core/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq" "github.com/neilotoole/sq/libsq/core/tablefq"
@ -452,7 +452,7 @@ func (h *Helper) RowCount(src *source.Source, tbl string) int64 {
// CreateTable creates a new table in src, and inserts data, returning // CreateTable creates a new table in src, and inserts data, returning
// the number of data rows inserted. If dropAfter is true, the created // the number of data rows inserted. If dropAfter is true, the created
// table is dropped when t.Cleanup is run. // table is dropped when t.Cleanup is run.
func (h *Helper) CreateTable(dropAfter bool, src *source.Source, tblDef *sqlmodel.TableDef, func (h *Helper) CreateTable(dropAfter bool, src *source.Source, tblDef *schema.Table,
data ...[]any, data ...[]any,
) (affected int64) { ) (affected int64) {
grip := h.openNew(src) grip := h.openNew(src)