propose method

  1. @protected
  2. @override
void propose()

Called by initialize; should contain the object initialization code.

The method is allowed to throw in the case of an error. The default Actor implementation will call dispose afterward to clean up even a partially initialized object; the thrown object is passed to dispose.

Implementation

@protected
@override
void propose() {
  super.propose();
  _database = filename != null
      ? sqlite3.open(filename!)
      : sqlite3.openInMemory();

  _beginTransaction = statement("BEGIN TRANSACTION");
  _commitTransaction = statement("COMMIT TRANSACTION");
  _rollbackTransaction = statement("ROLLBACK TRANSACTION");
  _savepoint = statement("SAVEPOINT sp");
  _rollbackToSavepoint = statement("ROLLBACK TO SAVEPOINT sp");

  beginTransaction();

  var ct = MessageFormat(
    "CREATE TABLE IF NOT EXISTS {0} " +
        "($columnKeyId NOT NULL UNIQUE, $columnValue NOT NULL)",
  );
  _database!.execute(ct.xFormat([tableMapping]));
  _database!.execute(ct.xFormat([tableScheme]));

  var stored = getStoredDefinition();
  var actual = scheme.toDefinition();
  if (!(const DeepCollectionEquality()).equals(stored, actual))
    migrateScheme(stored, actual);
  else
    scheme.initializeUpdatersLookup(this);

  _insert = statement(
    "INSERT OR IGNORE INTO $tableMapping ($columnKeyId, $columnValue) "
    "VALUES (?, jsonb(?)) RETURNING $columnKind, $columnRowId",
  );
  _updateByRowId = statement(
    "UPDATE OR IGNORE $tableMapping SET $columnValue = jsonb(?) "
    "WHERE $columnRowId == ? RETURNING $columnKind",
  );
  _updateByKeyId = statement(
    "UPDATE OR IGNORE $tableMapping SET $columnValue = jsonb(?) "
    "WHERE $columnKeyId == ? RETURNING $columnKind, $columnRowId",
  );
  _deleteByRowId = statement(
    "DELETE FROM $tableMapping "
    "WHERE $columnRowId == ? RETURNING $columnKind",
  );
  _deleteByKeyId = statement(
    "DELETE FROM $tableMapping "
    "WHERE $columnKeyId == ? RETURNING $columnKind, $columnRowId",
  );
  _existsByRowId = statement(
    "SELECT COUNT($columnRowId) "
    "FROM $tableMapping WHERE $columnRowId == ?",
  );
  _existsByKeyId = statement(
    "SELECT COUNT($columnKeyId) "
    "FROM $tableMapping WHERE $columnKeyId == ?",
  );
  _selectByRowId = statement(
    "SELECT $columnKeyId, json($columnValue) "
    "FROM $tableMapping WHERE $columnRowId == ?",
  );
  _selectByKeyId = statement(
    "SELECT $columnRowId, json($columnValue) "
    "FROM $tableMapping WHERE $columnKeyId == ?",
  );

  _domainQueryId = prepare(
    where: SqlI(columnKeyId).between(SqlP("@L"), SqlP("@U")),
    distinction: QueryDistinction.keyid,
  );

  commitTransaction();
}