tursodatabase / libsql

libSQL is a fork of SQLite that is both Open Source, and Open Contributions.
https://turso.tech/libsql
MIT License
11.27k stars 286 forks source link

Failing to parse double quote as strings in JS library. #1489

Open WoLfulus opened 5 months ago

WoLfulus commented 5 months ago

I'm using libsql package to run an introspection query, and it`s failing thinking "%AUTOINCREMENT%" is a column.

The SQL Standard defines "something" to be evaluated as strings, but SQLite is supposed to parse that a string since double-quoted strings are accepted. Ref: "8. Double-quoted String Literals Are Accepted" https://www.sqlite.org/quirks.html

Note that the same queries runs fine on Turso Dashboard's SQL runner.


const Database = require("libsql");

const syncUrl = "libsql://<something>.turso.io";
const authToken = "";

const queries = [
  // Original query
  'SELECT `name` from `sqlite_master` WHERE sql LIKE "%AUTOINCREMENT%"',

  // Test queries
  'SELECT `name` from `sqlite_master` WHERE sql LIKE "%AUTOINCREMENT%"', // Original, fails
  "SELECT `name` from `sqlite_master` WHERE sql LIKE '%AUTOINCREMENT%'", // Works
  'SELECT `name` from `sqlite_master` WHERE `sql` LIKE "%AUTOINCREMENT%"', // Fails
  "SELECT `name` from `sqlite_master` WHERE `sql` LIKE '%AUTOINCREMENT%'", // Works
  "SELECT name from sqlite_master WHERE sql = CONCAT('%AUTO', 'INCREMENT%')", // Works
  'SELECT name from sqlite_master WHERE sql = CONCAT("%AUTO", "INCREMENT%")', // Fails
];

const task = (async function run() {
  const db = new Database("./database.sqlite", {
    syncUrl,
    authToken,
  });

  db.sync();

  console.clear();
  for (const query of queries) {
    console.log("")
    try {
      console.log(`${query}\n`);
      const result = db.prepare(query).all();
      console.error(`  > OK`)
      //console.table(result);
    } catch (err) {
      console.error(`  > FAILED WITH: ${err.message}`)
    }
  }
})();

task.catch(console.error);

Error is thrown in databasePrepareSync call.

wldbest commented 3 months ago

This problem must be fixed to run with directus. https://github.com/linefusion/directus-libsql/tree/main?tab=readme-ov-file