Allow overriding of default quoting behaviour

Open
#298 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
sqlite, swift
Domain
database

Research direction

The issue points to Expression(literal: ...) and the quoted identifiers generated when columns are fetched; start by tracing those paths in the Swift/SQLite.swift codebase. Decide whether case-by-case quoting can be overridden without breaking identifier lookup, or document the SQLite peculiarity if it cannot; done should be demonstrated by a focused example or documentation update.

Written by the indexing model from the issue text.

Description

enhancement

Yep. For now, everything's quoted where it can be and because of this, relying and detecting dots is problematic because you could, in theory, have a table name with a period in it.

(from #30)

A downside of using quoted identifiers everywhere is that db errors might get obscured, e.g. in this example from the documentation:

let count = try db.scalar(users.filter(name != nil).count)
// SELECT count(*) FROM "users" WHERE "name" IS NOT NULL

A missing name column will not raise an error. The reason is that identifiers can get reinterpreted as strings if the identifier is not found:

If a keyword in double quotes (ex: "key" or "glob") is used in a context where it cannot be resolved to an identifier but where a string literal is allowed, then the token is understood to be a string literal instead of an identifier.

(https://www.sqlite.org/lang_keywords.html)

Richard Hipp called this a "tragic design error":

The fact that SQLite will treat a double-quoted string as a string literal
rather than as a quoted identifier is a horrible mis-feature. It was added
10 years or so ago in an attempt to be more MySQL-compatible. I have come
to sorely regret that change. I'd love to get rid of this mis-feature, but
cannot do so now, since there are millions of applications in the wild that
use SQLite and some percentage of those (hopefully a very small percentage,
but still non-zero) will break if I remove the mis-feature.
The point is that allowing double-quoted strings is a tragic design error.

(http://sqlite.1065341.n5.nabble.com/quot-default-value-of-column-name-is-not-constant-quot-error-in-table-creation-when-using-double-quos-td66991.html)

It's not really SQLite.swift's fault, but it would be good if the quoting could be overridden on a case-by- case basis.
I tried Expression<T>(literal: ...) but that does not work since column names are also quoted when fetched from the database. Even if this is not addressed in SQLite.swift itself it might be helpful to mention this peculiarity somewhere in the documentation.

Dominant language
Swift
Stars
10.2k
Forks
1.6k
Avg merge
6d 2h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stephencelis/SQLite.swift

All issues in stephencelis/SQLite.swift

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.