mathaou / termdbms

A TUI for viewing and editing database files.
MIT License
1.7k stars 36 forks source link

Missing quoting when tables have "special" names #9

Closed kilroy42 closed 3 years ago

kilroy42 commented 3 years ago

I have a database with a table called transaction. Opening it view the viewer:

panic: SQL logic error: near "transaction": syntax error (1)

goroutine 1 [running]:
sqlite3-viewer/viewer.(*TuiModel).SetModel(0x987d60, 0x0, 0xc0000142c6)
        termdbms/viewer/events.go:171 +0x765
main.main()
        termdbms/main.go:113 +0x4e5

Probably only a missing quoting somewhere...

Schema is:

CREATE TABLE `transaction` (
        id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        sheetId INTEGER NOT NULL,
        transactionId INTEGER NOT NULL,
        lineno INTEGER NOT NULL,
        lineId INTEGER NOT NULL REFERENCES line(id)
);
mathaou commented 3 years ago

This is a limitation of the SQL library I'm using, but I might be able to fix it with a simple find and replace. Will investigate and get back to you.

mathaou commented 3 years ago

@kilroy42 no amount of find+replace can fix that I'm afraid. This might be dependent on the database I'm using (MySql support or Postgres might not care), but "transaction" is a reserved keyword, so quoted, unquoted, or backticks around it aren't the issue. Table names can't be reserved keywords like "transaction". Haven't tested it with fields, but I would just advise to use names that have some kind of prefix or suffix if you really want that reserved keyword nomenclature.

kilroy42 commented 3 years ago

Just checked your code. You really should use prepared statements, that is one of the reasons it doesn't work. And will avoid a whole lot of other errors, too!

I fixed it in viewer/viewer.go line 278:

        var statement strings.Builder
        statement.WriteString("select * from `")
        statement.WriteString(schemaName)
        statement.WriteString("`")

(Sadly I don't know go, so excuse my code style)