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
@# are not in use.
@goimports-reviser -company-prefixes github.com/neilotoole -set-alias \
-excludes *_windows.go \
-excludes '*_windows.go' \
-rm-unused -output write \
-project-name github.com/neilotoole/sq ./...

View File

@ -16,7 +16,7 @@ import (
"github.com/neilotoole/sq/cli"
"github.com/neilotoole/sq/cli/testrun"
"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/tablefq"
"github.com/neilotoole/sq/testh"
@ -86,7 +86,7 @@ func TestCreateTable_bytes(t *testing.T) {
th, src, _, _, _ := testh.NewWith(t, handle)
th.DiffDB(src)
tblDef := sqlmodel.NewTableDef(
tblDef := schema.NewTable(
stringz.UniqTableName("test_bytes"),
[]string{"col_name", "col_bytes"},
[]kind.Kind{kind.Text, kind.Bytes},
@ -119,7 +119,7 @@ func TestOutputRaw(t *testing.T) {
_, err := gif.Decode(bytes.NewReader(wantBytes))
require.NoError(t, err)
tblDef := sqlmodel.NewTableDef(
tblDef := schema.NewTable(
stringz.UniqTableName("test_bytes"),
[]string{"col_name", "col_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/lga"
"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"
)
@ -105,12 +105,12 @@ func mungeCSV2InsertRecord(ctx context.Context, mungers []kind.MungeFunc, csvRec
return a, nil
}
func createTblDef(tblName string, colNames []string, kinds []kind.Kind) *sqlmodel.TableDef {
tbl := &sqlmodel.TableDef{Name: tblName}
func createTblDef(tblName string, colNames []string, kinds []kind.Kind) *schema.Table {
tbl := &schema.Table{Name: tblName}
cols := make([]*sqlmodel.ColDef, len(colNames))
cols := make([]*schema.Column, len(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
@ -118,7 +118,7 @@ func createTblDef(tblName string, colNames []string, kinds []kind.Kind) *sqlmode
}
// 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)
if err != nil {
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/lgm"
"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/stringz"
"github.com/neilotoole/sq/libsq/driver"
@ -54,7 +54,7 @@ var (
)
// 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)
if err != nil {
return nil, err
@ -96,8 +96,8 @@ type processor struct {
// if flattened is true, the JSON object will be flattened into a single table.
flatten bool
root *entity
schema *importSchema
root *entity
importSchema *ingestSchema
colNamesOrdered []string
@ -111,7 +111,7 @@ type processor struct {
func newProcessor(flatten bool) *processor {
return &processor{
flatten: flatten,
schema: &importSchema{},
importSchema: &ingestSchema{},
root: &entity{name: source.MonotableName, detectors: map[string]*kind.Detector{}},
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.
func (p *processor) buildSchemaFlat() (*importSchema, error) {
tblDef := &sqlmodel.TableDef{
func (p *processor) buildSchemaFlat() (*ingestSchema, error) {
tblDef := &schema.Table{
Name: source.MonotableName,
}
var colDefs []*sqlmodel.ColDef
var colDefs []*schema.Column
schema := &importSchema{
colMungeFns: map[*sqlmodel.ColDef]kind.MungeFunc{},
entityTbls: map[*entity]*sqlmodel.TableDef{},
tblDefs: []*sqlmodel.TableDef{tblDef}, // Single table only because flat
schma := &ingestSchema{
colMungeFns: map[*schema.Column]kind.MungeFunc{},
entityTbls: map[*entity]*schema.Table{},
tblDefs: []*schema.Table{tblDef}, // Single table only because flat
}
visitFn := func(e *entity) error {
schema.entityTbls[e] = tblDef
schma.entityTbls[e] = tblDef
for _, field := range e.fieldNames {
if detector, ok := e.detectors[field]; ok {
@ -174,7 +174,7 @@ func (p *processor) buildSchemaFlat() (*importSchema, error) {
k = kind.Text
}
colDef := &sqlmodel.ColDef{
colDef := &schema.Column{
Name: p.calcColName(e, field),
Table: tblDef,
Kind: k,
@ -182,7 +182,7 @@ func (p *processor) buildSchemaFlat() (*importSchema, error) {
colDefs = append(colDefs, colDef)
if mungeFn != nil {
schema.colMungeFns[colDef] = mungeFn
schma.colMungeFns[colDef] = mungeFn
}
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
// 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) {
p.curObjVals = objectValueSet{}
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
// processor's unwrittenObjVals. After a non-error return, unwrittenObjVals
// is empty.
func (p *processor) buildInsertionsFlat(schema *importSchema) ([]*insertion, error) {
if len(schema.tblDefs) != 1 {
return nil, errz.Errorf("expected 1 table for flat JSON processing but got %d", len(schema.tblDefs))
func (p *processor) buildInsertionsFlat(schma *ingestSchema) ([]*insertion, error) {
if len(schma.tblDefs) != 1 {
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
// Each of unwrittenObjVals is effectively an INSERT row
@ -423,19 +423,19 @@ func walkEntity(ent *entity, visitFn func(*entity) error) error {
return nil
}
// importSchema encapsulates the table definitions that
// the JSON is imported to.
type importSchema struct {
tblDefs []*sqlmodel.TableDef
colMungeFns map[*sqlmodel.ColDef]kind.MungeFunc
// ingestSchema encapsulates the table definitions that
// the JSON is ingested to.
type ingestSchema struct {
tblDefs []*schema.Table
colMungeFns map[*schema.Column]kind.MungeFunc
// entityTbls is a mapping of entity to the table in which
// 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,
curSchema, newSchema *importSchema,
curSchema, newSchema *ingestSchema,
) error {
log := lg.FromContext(ctx)
var err error

View File

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

View File

@ -15,7 +15,7 @@ import (
"github.com/neilotoole/sq/libsq/core/lg/lga"
"github.com/neilotoole/sq/libsq/core/lg/lgm"
"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/driver"
"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
tblDef := sqlmodel.NewTableDef(source.MonotableName, colNames, colKinds)
tblDef := schema.NewTable(source.MonotableName, colNames, colKinds)
db, err := job.destGrip.DB(ctx)
if err != nil {
return err

View File

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

View File

@ -24,7 +24,7 @@ import (
"github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/record"
"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/stringz"
"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.
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)
_, 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)
for rows.Next() {
var schema string
if err = rows.Scan(&schema); err != nil {
var schma string
if err = rows.Scan(&schma); err != nil {
return nil, errz.Err(err)
}
schemas = append(schemas, schema)
schemas = append(schemas, schma)
}
if err = rows.Err(); err != nil {

View File

@ -5,7 +5,7 @@ import (
"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/tablefq"
"github.com/neilotoole/sq/testh"
@ -44,7 +44,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name())
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 {
colDef.NotNull = true
colDef.HasDefault = true

View File

@ -9,7 +9,7 @@ import (
"github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/core/errz"
"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"
)
@ -58,7 +58,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive
}
//nolint:funlen
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string {
func buildCreateTableStmt(tblDef *schema.Table) string {
buf := &bytes.Buffer{}
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/record"
"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/stringz"
"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.
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)
_, 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)
for rows.Next() {
var schema string
if err = rows.Scan(&schema); err != nil {
var schma string
if err = rows.Scan(&schma); err != nil {
return nil, errw(err)
}
schemas = append(schemas, schema)
schemas = append(schemas, schma)
}
if err = rows.Err(); err != nil {

View File

@ -15,7 +15,7 @@ import (
"github.com/neilotoole/sq/drivers/postgres"
"github.com/neilotoole/sq/libsq/core/errz"
"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/tablefq"
"github.com/neilotoole/sq/testh"
@ -163,7 +163,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name())
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 {
colDef.NotNull = true
colDef.HasDefault = true

View File

@ -7,7 +7,7 @@ import (
"github.com/neilotoole/sq/libsq/core/errz"
"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 {
@ -54,7 +54,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive
// buildCreateTableStmt builds a CREATE TABLE statement from tblDef.
// 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.WriteString(`CREATE TABLE "`)
sb.WriteString(tblDef.Name)

View File

@ -6,7 +6,7 @@ import (
"github.com/neilotoole/sq/libsq/core/errz"
"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
@ -24,7 +24,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive // ignore
kind.Unknown: `DEFAULT ''`,
}
func buildCreateTableStmt(tblDef *sqlmodel.TableDef) string {
func buildCreateTableStmt(tblDef *schema.Table) string {
var buf *bytes.Buffer
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/loz"
"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/stringz"
"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.
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)
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)
for rows.Next() {
var schema string
if err = rows.Scan(&schema); err != nil {
var schma string
if err = rows.Scan(&schma); err != nil {
return nil, errz.Err(err)
}
schemas = append(schemas, schema)
schemas = append(schemas, schma)
}
if err = rows.Err(); err != nil {

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require"
"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/stringz"
"github.com/neilotoole/sq/libsq/core/tablefq"
@ -172,7 +172,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name())
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 {
colDef.NotNull = true
colDef.HasDefault = true

View File

@ -9,7 +9,7 @@ import (
"github.com/neilotoole/sq/libsq/ast/render"
"github.com/neilotoole/sq/libsq/core/errz"
"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) {
@ -106,7 +106,7 @@ var createTblKindDefaults = map[kind.Kind]string{ //nolint:exhaustive
// buildCreateTableStmt builds a CREATE TABLE statement from tblDef.
// 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.WriteString(`CREATE TABLE "`)
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/loz"
"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/stringz"
"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)
for rows.Next() {
var schema string
if err = rows.Scan(&schema); err != nil {
var schma string
if err = rows.Scan(&schma); err != nil {
return nil, errz.Err(err)
}
schemas = append(schemas, schema)
schemas = append(schemas, schma)
}
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.
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)
_, 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.
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 {
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)
return errz.Wrapf(errw(err), "alter table: failed to rename table %q to %q", tbl, newName)
}
// AlterTableRenameColumn implements driver.SQLDriver.
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 {
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)
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.

View File

@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
"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/tablefq"
"github.com/neilotoole/sq/testh"
@ -81,7 +81,7 @@ func TestDriver_CreateTable_NotNullDefault(t *testing.T) {
tblName := stringz.UniqTableName(t.Name())
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 {
colDef.NotNull = true
colDef.HasDefault = true

View File

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

View File

@ -20,7 +20,7 @@ import (
"github.com/neilotoole/sq/libsq/core/kind"
"github.com/neilotoole/sq/libsq/core/lg"
"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/driver"
"github.com/neilotoole/sq/libsq/source"
@ -49,7 +49,7 @@ func Ingest(ctx context.Context, def *userdriver.DriverDef, data io.Reader, dest
def: def,
selStack: newSelStack(),
rowStack: newRowStack(),
tblDefs: map[string]*sqlmodel.TableDef{},
tblDefs: map[string]*schema.Table{},
tblSequence: map[string]int64{},
execInsertFns: map[string]func(ctx context.Context, insertVals []any) error{},
execUpdateFns: map[string]func(ctx context.Context, updateVals, whereArgs []any) error{},
@ -74,7 +74,7 @@ type ingester struct {
destDB sqlz.DB
selStack *selStack
rowStack *rowStack
tblDefs map[string]*sqlmodel.TableDef
tblDefs map[string]*schema.Table
// tblSequence is a map of table name to the last
// 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/options"
"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/driver"
"github.com/neilotoole/sq/libsq/source"
@ -30,7 +30,7 @@ const msgCloseRowIter = "Close Excel row iterator"
// sheetTable maps a sheet to a database table.
type sheetTable struct {
sheet *xSheet
def *sqlmodel.TableDef
def *schema.Table
colIngestMungeFns []kind.MungeFunc
hasHeaderRow bool
}
@ -400,10 +400,10 @@ func buildSheetTable(ctx context.Context, srcIngestHeader *bool, sheet *xSheet)
return nil, err
}
tblDef := &sqlmodel.TableDef{Name: sheet.name}
cols := make([]*sqlmodel.ColDef, len(colNames))
tblDef := &schema.Table{Name: sheet.name}
cols := make([]*schema.Column, len(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
lg.FromContext(ctx).Debug("Built table def",

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package sqlmodel_test
package sqlparser_test
import (
"strings"
@ -6,16 +6,16 @@ import (
"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"
)
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.
nTypes := func(n int, typ sqlmodel.StmtType) []sqlmodel.StmtType {
types := make([]sqlmodel.StmtType, n)
// convenience func to return a slice of n sqlparser.StmtType.
nTypes := func(n int, typ sqlparser.StmtType) []sqlparser.StmtType {
types := make([]sqlparser.StmtType, n)
for i := range types {
types[i] = typ
}
@ -30,7 +30,7 @@ func TestSplitInput(t *testing.T) {
input string
wantCount int
wantContains []string
wantTypes []sqlmodel.StmtType
wantTypes []sqlparser.StmtType
}{
"simple": {
{
@ -39,7 +39,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table; ",
wantCount: 1,
wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel},
wantTypes: []sqlparser.StmtType{sel},
},
{
name: "select_1_no_delim",
@ -47,7 +47,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table",
wantCount: 1,
wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel},
wantTypes: []sqlparser.StmtType{sel},
},
{
name: "select_1_many_delim",
@ -55,7 +55,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\n;;",
wantCount: 1,
wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel},
wantTypes: []sqlparser.StmtType{sel},
},
{
name: "select_2",
@ -63,7 +63,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ndrop table my_table;",
wantCount: 2,
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",
@ -71,28 +71,28 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ndrop table my_table",
wantCount: 2,
wantContains: []string{"select * from my_table", "drop table my_table"},
wantTypes: []sqlmodel.StmtType{sel, other},
wantTypes: []sqlparser.StmtType{sel, other},
},
},
"sqlite3": {
{
name: "sqlite3_type_test.sql",
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,
wantTypes: nTypes(4, other),
},
{
name: "sqlite3_address.sql",
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,
wantTypes: nTypes(4, other),
},
{
name: "sqlite3_person.sql",
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,
wantTypes: nTypes(10, other),
},
@ -101,21 +101,21 @@ func TestSplitInput(t *testing.T) {
{
name: "sqtype_public_type_test.sql",
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,
wantTypes: nTypes(5, other),
},
{
name: "sqtest_public_address.sql",
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,
wantTypes: nTypes(5, other),
},
{
name: "sqtest_public_person.sql",
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,
wantTypes: nTypes(11, other),
},
@ -128,7 +128,7 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ngo",
wantCount: 1,
wantContains: []string{"select * from my_table"},
wantTypes: []sqlmodel.StmtType{sel},
wantTypes: []sqlparser.StmtType{sel},
},
{
name: "select_2",
@ -137,13 +137,13 @@ func TestSplitInput(t *testing.T) {
input: "select * from my_table;\ndrop table my_table;",
wantCount: 2,
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",
delim: ";",
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,
wantTypes: nTypes(5, other),
},
@ -151,7 +151,7 @@ func TestSplitInput(t *testing.T) {
name: "sqtest_dbo_address.sql",
delim: ";",
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,
wantTypes: nTypes(5, other),
},
@ -159,7 +159,7 @@ func TestSplitInput(t *testing.T) {
name: "sqtest_dbo_person.sql",
delim: ";",
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,
wantTypes: nTypes(10, other),
},
@ -168,21 +168,21 @@ func TestSplitInput(t *testing.T) {
{
name: "mysql_type_test.sql",
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,
wantTypes: nTypes(5, other),
},
{
name: "mysql_address.sql",
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,
wantTypes: nTypes(5, other),
},
{
name: "mysql_person.sql",
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,
wantTypes: nTypes(9, other),
},
@ -195,7 +195,7 @@ func TestSplitInput(t *testing.T) {
for _, tc := range testGroup {
tc := tc
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.Equal(t, tc.wantCount, len(stmts))
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/lgm"
"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/driver"
"github.com/neilotoole/sq/libsq/source"
@ -63,7 +63,7 @@ func DBWriterCreateTableIfNotExistsHook(destTblName string) DBWriterPreWriteHook
destColNames := recMeta.Names()
destColKinds := recMeta.Kinds()
destTblDef := sqlmodel.NewTableDef(destTblName, destColNames, destColKinds)
destTblDef := schema.NewTable(destTblName, destColNames, destColKinds)
err = destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef)
if err != nil {

View File

@ -14,7 +14,7 @@ import (
"github.com/neilotoole/sq/libsq/core/options"
"github.com/neilotoole/sq/libsq/core/progress"
"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/tablefq"
"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
// build the foreign key constraints. At a minimum the implementation
// 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
// 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/kind"
"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/tablefq"
"github.com/neilotoole/sq/libsq/driver"
@ -124,7 +124,7 @@ func TestDriver_CreateTable_Minimal(t *testing.T) {
tblName := stringz.UniqTableName(t.Name())
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)
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/options"
"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/tablefq"
"github.com/neilotoole/sq/libsq/driver"
@ -446,7 +446,7 @@ func execCopyTable(ctx context.Context, fromDB driver.Grip, fromTbl tablefq.T,
) error {
destColNames := originRecMeta.Names()
destColKinds := originRecMeta.Kinds()
destTblDef := sqlmodel.NewTableDef(destTbl.Table, destColNames, destColKinds)
destTblDef := schema.NewTable(destTbl.Table, destColNames, destColKinds)
err := destGrip.SQLDriver().CreateTable(ctx, tx, destTblDef)
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/lgt"
"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/stringz"
"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
// the number of data rows inserted. If dropAfter is true, the created
// 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,
) (affected int64) {
grip := h.openNew(src)