GithubHelp home page GithubHelp logo

Comments (1)

ForbesLindesay avatar ForbesLindesay commented on May 20, 2024

I think this is specific to SQLite, because we use Database.all to run the underlying query and that API only supports passing a single query.

Overall this is pretty inconsistent between databases though. I'm starting to think the best option would be to have an API in @databases/sql that you can call to split multiple statements into an Array (i.e. SQLQuery[]). We could then make a breaking change to @databases/pg and possibly @databases/mysql so that the interface becomes overloaded like:

interface Queryable {
  /**
   * Accepts an SQLQuery with a single statement, and errors if there are multiple statements in one query
   */
  query(q: SQLQuery): Promise<any[]>

  /**
   * Accepts an array of SQLQueries with one statement each, and returns an array where each
   * element represents the results of one statement.
   *
   * If you pass it an empty array it returns an empty array without any call to the database.
   * If you pass it an array with only one statement, it returns an array of length 1 containing
   * an array of the results to the statement.
   */
  query(q: SQLQuery[]): Promise<any[][]>
}

We would then extend SQLQuery with:

interface SQLQuery {
  // Split this query into an array where each item in the array is one statement.
  // If this query is an empty string, or only contains comments, a string of length 0 is returned.
  split(): SQLQuery[]
  // ...existing methods & properties...
}

The breaking change here is that currently we allow you to pass multiple statements in one go to query in Postgres. If you do that, we run all statements, and return the results of the last statement.

Decisions / Questions

  1. Is it a good idea to require the caller of the API to be explicit about when a query has multiple statements? The problem with supporting this implicitly, is that it can be easy to have code that breaks when there is only one query or where there are 0 queries. We could keep the postgres behaviour for query (i.e. if there are multiple queries, just return the value from the last query).
  2. What happens when a query fails? If this API is used outside of a transaction, we could implicitly create a transaction for just these queries, this might be the safest thing to do. SQLite's .exec function just runs them one at a time and stops at the first error, but this feels pretty dangerous.

from atdatabases.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.