Skip to content

Simplify driver API #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Simplify driver API #5

wants to merge 5 commits into from

Conversation

rkistner
Copy link
Contributor

@rkistner rkistner commented Aug 29, 2024

The new PreparedStatement interface is listed below. See discussion in halvardssm/stdext#6 for some background.

This avoids the need for separate bind/step/reset calls. Instead, statements now just take a single call to either return all results, or an iterator of result chunks.

Additionally, this splits out query methods into separate calls for object rows vs array rows.

This simplifies the driver API enough that it can be used directly by client code, versus the previous iteration that felt like you absolutely need a wrapping API.

This loses some flexibility:

  1. You now have to specify query parameters each time you run the query, you cannot just bind once and run multiple times.
  2. You cannot combine array parameters with named parameters in a single query anymore.

Those were not likely to be real use cases anyway, and the underlying drivers typically did not support those properly either.

This now also moves some options from the prepare call to the query call, so you can use the same prepared statement with different options.

TODO:

  • Proper error on concurrent querying on the same prepared statement.
  • Implement streaming iterators via worker_threads.
  • Add tests for streaming iterators.
export interface SqliteDriverStatement {
  /**
   * Run a query, and return results as an array of row objects.
   *
   * If the query does not return results, an empty array is returned.
   */
  all(
    parameters?: SqliteParameterBinding,
    options?: QueryOptions
  ): Promise<SqliteObjectRow[]>;

  /**
   * Run a query, and return results as an array of row arrays.
   *
   * If the query does not return results, an empty array is returned.
   */
  allArray(
    parameters?: SqliteParameterBinding,
    options?: QueryOptions
  ): Promise<SqliteArrayRow[]>;

  /**
   * Run a query, and return as an iterator of array of row object chunks.
   *
   * It is an error to call any other query methods on the same statement
   * before the iterator has returned.
   */
  stream(
    parameters?: SqliteParameterBinding,
    options?: StreamQueryOptions
  ): AsyncIterableIterator<SqliteObjectRow[]>;

  /**
   * Run a query, and return as an iterator of array of row array chunks.
   *
   * It is an error to call any other query methods on the same statement
   * before the iterator has returned.
   */
  streamArray(
    parameters?: SqliteParameterBinding,
    options?: StreamQueryOptions
  ): AsyncIterableIterator<SqliteArrayRow[]>;

  /**
   * Run a query, and return the number of changed rows, and last insert id.
   */
  run(
    parameters?: SqliteParameterBinding,
    options?: QueryOptions
  ): Promise<SqliteChanges>;

  /**
   * Get the column names of the data returned by the query.
   */
  getColumns(): Promise<string[]>;

  finalize(): void;
  [Symbol.dispose](): void;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant