Interface D1Database

All Superinterfaces:
org.teavm.jso.JSObject

public interface D1Database extends org.teavm.jso.JSObject
A D1 database. Declared with d1(), named DB by default.

SQLite, so the SQL dialect is SQLite's. A statement is prepared and then bound, which is the only way to pass a value: string interpolation into SQL is an injection and D1 offers no escaping function to make it safe.

 D1Database db = env.d1();
 D1Result rows = db.prepare("select name from users where team = ?").bind("core").all();
 for (TSObject row : rows.rows()) {
 	System.out.println(row.get("name").asString());
 }
Since:
1.0.0
  • Method Details

    • prepare

      Prepares a statement.
      Parameters:
      sql - the SQL, with ? for each value
      Returns:
      the prepared statement
    • exec

      default D1Database.D1ExecResult exec(String sql)
      Runs one or more statements that take no values.

      Accepts several statements separated by semicolons, which is how a migration is applied. Values cannot be bound here; use prepare(String) for anything carrying input.

      Rows are not returned, whatever the statements were. D1 answers a count and a duration and nothing else, so a select run this way produces no results.

      Parameters:
      sql - the SQL
      Returns:
      how many statements ran, and how long they took
    • batch

      default List<D1Database.D1Result> batch(D1Database.D1Statement... statements)
      Runs several prepared statements in one round trip, inside an implicit transaction.

      D1 has no interactive transaction. This is the whole of it: either every statement applies or none does.

      Parameters:
      statements - the statements, each already bound
      Returns:
      one result per statement, in order
    • execute

      org.teavm.jso.core.JSPromise<D1Database.D1ExecResult> execute(String sql)
    • runBatch

      org.teavm.jso.core.JSPromise<org.teavm.jso.core.JSArrayReader<D1Database.D1Result>> runBatch(org.teavm.jso.core.JSArray<D1Database.D1Statement> statements)