Skip to content

Commit

Permalink
Support table and column names with spaces. (#156)
Browse files Browse the repository at this point in the history
* sakila: initial test data

* sakila: more test data

* sakila: yet more test data setup

* whitespace cols: now working for sqlite

* grammar cleanup

* whitespace cols: now working inside count() func for sqlite

* whitespace cols: tests mostly passing; begining refactoring

* grammar: refactor handle

* grammar: more refactoring

* grammar: rename selElement to selector

* wip

* all tests passing

* all tests passing

* linting

* driver: implement CurrentSchema for all driver.SQLDriver impls

* driver: tests for AlterTableRename and AlterTableRenameColumn

* undo reformat of SQL

* undo reformat of SQL

* undo reformat of SQL

* undo reformat of SQL
  • Loading branch information
neilotoole authored Mar 22, 2023
1 parent 63127eb commit a1a89ee
Show file tree
Hide file tree
Showing 54 changed files with 2,123 additions and 1,445 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ linters-settings:
nestif:
# Minimal complexity of if statements to report.
# Default: 5
min-complexity: 15
min-complexity: 20

nolintlint:
# Exclude following linters from requiring an explanation.
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [v0.26.0] - 2023-03-22

### Added

- [#98]: Whitespace is now allowed in SLQ selector names. You can
do `@sakila | ."film actor" | ."actor id"`.


### Fixed

- [#155]: `sq inspect` now populates `schema` field in JSON for MySQL,
SQLite, and SQL Server (Postgres already worked).

## [v0.25.1] - 2023-03-19

### Fixed
Expand Down Expand Up @@ -175,6 +188,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#89]: Bug with SQL generated for joins.


[v0.26.0]: https://github.com/neilotoole/sq/compare/v0.25.1...v0.26.0
[v0.25.1]: https://github.com/neilotoole/sq/compare/v0.25.0...v0.25.1
[v0.25.0]: https://github.com/neilotoole/sq/compare/v0.24.4...v0.25.0
[v0.24.4]: https://github.com/neilotoole/sq/compare/v0.24.3...v0.24.4
Expand All @@ -196,11 +210,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[v0.15.3]: https://github.com/neilotoole/sq/compare/v0.15.2...v0.15.3
[v0.15.2]: https://github.com/neilotoole/sq/releases/tag/v0.15.2

[#155]: https://github.com/neilotoole/sq/issues/155
[#153]: https://github.com/neilotoole/sq/issues/153
[#151]: https://github.com/neilotoole/sq/issues/151
[#144]: https://github.com/neilotoole/sq/issues/144
[#142]: https://github.com/neilotoole/sq/issues/142
[#123]: https://github.com/neilotoole/sq/issues/123
[#98]: https://github.com/neilotoole/sq/issues/98
[#95]: https://github.com/neilotoole/sq/issues/93
[#91]: https://github.com/neilotoole/sq/pull/91
[#89]: https://github.com/neilotoole/sq/pull/89
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func execInspect(cmd *cobra.Command, args []string) error {
var table string
var err error

if len(args) == 0 { //nolint:nestif
if len(args) == 0 {
// No args supplied.

// There are two paths from here:
Expand Down
3 changes: 2 additions & 1 deletion drivers/mysql/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func setSourceSummaryMeta(ctx context.Context, db sqlz.DB, md *source.Metadata)
}

md.Name = schema
md.Schema = schema
md.FQName = schema
md.DBVersion = version
md.DBProduct = fmt.Sprintf("%s %s / %s (%s)", versionComment, version, versionOS, versionArch)
Expand Down Expand Up @@ -310,7 +311,7 @@ func getAllTblMetas(ctx context.Context, log lg.Log, db sqlz.DB) ([]*source.Tabl
const query = `SELECT t.TABLE_SCHEMA, t.TABLE_NAME, t.TABLE_TYPE, t.TABLE_COMMENT,
(DATA_LENGTH + INDEX_LENGTH) AS table_size,
c.COLUMN_NAME, c.ORDINAL_POSITION, c.COLUMN_KEY, c.DATA_TYPE, c.COLUMN_TYPE,
c.IS_NULLABLE, c.COLUMN_DEFAULT, c.COLUMN_COMMENT, c.EXTRA
c.IS_NULLABLE, c.COLßUMN_DEFAULT, c.COLUMN_COMMENT, c.EXTRA
FROM information_schema.TABLES t
LEFT JOIN information_schema.COLUMNS c
ON c.TABLE_CATALOG = t.TABLE_CATALOG
Expand Down
30 changes: 27 additions & 3 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p *Provider) DriverFor(typ source.Type) (driver.Driver, error) {
return &driveri{log: p.Log}, nil
}

var _ driver.Driver = (*driveri)(nil)
var _ driver.SQLDriver = (*driveri)(nil)

// driveri is the MySQL implementation of driver.Driver.
type driveri struct {
Expand Down Expand Up @@ -102,8 +102,8 @@ func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.
}

// AlterTableAddColumn implements driver.SQLDriver.
func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl, col string, knd kind.Kind) error {
q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + dbTypeNameFromKind(knd)
func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl, col string, knd kind.Kind) error {
q := fmt.Sprintf("ALTER TABLE `%s` ADD COLUMN `%s` ", tbl, col) + dbTypeNameFromKind(knd)

_, err := db.ExecContext(ctx, q)
if err != nil {
Expand All @@ -113,6 +113,30 @@ func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl, col
return nil
}

// CurrentSchema implements driver.SQLDriver.
func (d *driveri) CurrentSchema(ctx context.Context, db sqlz.DB) (string, error) {
var name string
if err := db.QueryRowContext(ctx, `SELECT DATABASE()`).Scan(&name); err != nil {
return "", errz.Err(err)
}

return name, nil
}

// AlterTableRename implements driver.SQLDriver.
func (d *driveri) AlterTableRename(ctx context.Context, db sqlz.DB, tbl, newName string) error {
q := fmt.Sprintf("RENAME TABLE `%s` TO `%s`", tbl, newName)
_, err := db.ExecContext(ctx, q)
return errz.Wrapf(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 {
q := fmt.Sprintf("ALTER TABLE `%s` RENAME COLUMN `%s` TO `%s`", tbl, col, newName)
_, err := db.ExecContext(ctx, q)
return errz.Wrapf(err, "alter table: failed to rename column {%s.%s} to {%s}", tbl, col, newName)
}

// PrepareInsertStmt implements driver.SQLDriver.
func (d *driveri) PrepareInsertStmt(ctx context.Context, db sqlz.DB, destTbl string, destColNames []string,
numRows int,
Expand Down
85 changes: 0 additions & 85 deletions drivers/mysql/sqlbuilder_test.go

This file was deleted.

28 changes: 26 additions & 2 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,32 @@ func (d *driveri) CreateTable(ctx context.Context, db sqlz.DB, tblDef *sqlmodel.
return errz.Err(err)
}

// CurrentSchema implements driver.SQLDriver.
func (d *driveri) CurrentSchema(ctx context.Context, db sqlz.DB) (string, error) {
var name string
if err := db.QueryRowContext(ctx, `SELECT CURRENT_SCHEMA()`).Scan(&name); err != nil {
return "", errz.Err(err)
}

return name, nil
}

// AlterTableRename implements driver.SQLDriver.
func (d *driveri) AlterTableRename(ctx context.Context, db sqlz.DB, tbl, newName string) error {
q := fmt.Sprintf(`ALTER TABLE %q RENAME TO %q`, tbl, newName)
_, err := db.ExecContext(ctx, q)
return errz.Wrapf(err, "alter table: failed to rename table {%s} to {%s}", tbl, newName)
}

// AlterTableRenameColumn implements driver.SQLDriver.
func (d *driveri) AlterTableRenameColumn(ctx context.Context, db sqlz.DB, tbl, col, newName string) error {
q := fmt.Sprintf("ALTER TABLE %q RENAME COLUMN %q TO %q", tbl, col, newName)
_, err := db.ExecContext(ctx, q)
return errz.Wrapf(err, "alter table: failed to rename column {%s.%s} to {%s}", tbl, col, newName)
}

// AlterTableAddColumn implements driver.SQLDriver.
func (d *driveri) AlterTableAddColumn(ctx context.Context, db *sql.DB, tbl, col string, knd kind.Kind) error {
func (d *driveri) AlterTableAddColumn(ctx context.Context, db sqlz.DB, tbl, col string, knd kind.Kind) error {
q := fmt.Sprintf("ALTER TABLE %q ADD COLUMN %q ", tbl, col) + dbTypeNameFromKind(knd)

_, err := db.ExecContext(ctx, q)
Expand Down Expand Up @@ -383,7 +407,7 @@ func (d *driveri) getTableRecordMeta(ctx context.Context, db sqlz.DB, tblName st
// returning the names of the table's columns in oridinal order.
func getTableColumnNames(ctx context.Context, log lg.Log, db sqlz.DB, tblName string) ([]string, error) {
const query = `SELECT column_name FROM information_schema.columns
WHERE table_schema = current_schema()
WHERE table_schema = CURRENT_SCHEMA()
AND table_name = $1
ORDER BY ordinal_position`

Expand Down
85 changes: 0 additions & 85 deletions drivers/postgres/sqlbuilder_test.go

This file was deleted.

Loading

0 comments on commit a1a89ee

Please sign in to comment.