execute method

Future execute(
  1. String sql, {
  2. List<String>? tables,
  3. List<Object?> params = const [],
  4. String? dbName,
})

Executes an SQL statement.

Returns different results based on the operation:

  • INSERT: the last inserted row id
  • UPDATE: the number of affected rows
  • DELETE: the number of affected rows
  • other: the result of the statement

tables is used by the reactive stream system to know which watched queries to refresh after this operation.

Implementation

Future<dynamic> execute(String sql,
    {List<String>? tables,
    List<Object?> params = const [],
    String? dbName}) async {
  if (debugMode) {
    // ignore: avoid_print
    print("execute: $sql - params: $params - tables: $tables");
  }
  final String sqlCommand = sql.contains(" ")
      ? sql.substring(0, sql.indexOf(" ")).toUpperCase()
      : sql;
  final db = _getDB(dbName);
  final resolvedParams = fixBoolParams(params);
  try {
    switch (sqlCommand) {
      case "INSERT":
        await db.execute(sql, resolvedParams);
        return await query("SELECT last_insert_rowid()",
            dbName: dbName, singleResult: true);
      case "UPDATE":
        await db.execute(sql, resolvedParams);
        return await query("SELECT changes()",
            dbName: dbName, singleResult: true);
      case "DELETE":
        await db.execute(sql, resolvedParams);
        return await query("SELECT changes()",
            dbName: dbName, singleResult: true);
      default:
        return await db.execute(sql, resolvedParams);
    }
  } finally {
    await updateStreams(tables);
  }
}