openOrCreate method

dynamic openOrCreate({
  1. Function? dbCreateFunction,
})

Implementation

openOrCreate({Function? dbCreateFunction}) {
  _sqliteDb.open(populateFunction: dbCreateFunction);

  // 1196444487 (the 32-bit integer value of 0x47504B47 or GPKG in ASCII) for GPKG 1.2 and
  // greater
  // 1196437808 (the 32-bit integer value of 0x47503130 or GP10 in ASCII) for GPKG 1.0 or
  // 1.1
  var res = _sqliteDb.select("PRAGMA application_id");
  int appId = res.first.get('application_id');
  if (0x47503130 == appId) {
    _gpkgVersion = "1.0/1.1";
  } else if (0x47504B47 == appId) {
    _gpkgVersion = "1.2";
  }

  _isGpgkInitialized = _gpkgVersion != null;

  createFunctions();

  if (doRtreeTestCheck) {
    try {
      String checkTable = "rtree_test_check";
      String checkRtree = "CREATE VIRTUAL TABLE " +
          checkTable +
          " USING rtree(id, minx, maxx, miny, maxy)";
      _sqliteDb.execute(checkRtree);
      String drop = "DROP TABLE " + checkTable;
      _sqliteDb.execute(drop);
      _supportsRtree = true;
    } catch (e) {
      _supportsRtree = false;
    }
  }

  if (!_isGpgkInitialized) {
    _sqliteDb.transaction((_db) {
      _db.execute(GPKG_SPATIAL_REF_SYS);
      _db.execute(GPKG_GEOMETRY_COLUMNS);
      _db.execute(GPKG_CONTENTS);
      _db.execute(GPKG_TILE_MATRIX_SET);
      _db.execute(GPKG_TILE_MATRIX);
      _db.execute(GPKG_DATA_COLUMNS);
      _db.execute(GPKG_METADATA);
      _db.execute(GPKG_METADATA_REFERENCE);
      _db.execute(GPKG_DATA_COLUMN_CONSTRAINTS);
      _db.execute(GPKG_EXTENSIONS);
    });

    // var lines = sqlString.split("\n");
    // lines.removeWhere((line) => line.trim().startsWith("--"));
    // sqlString = lines.join(" ");
    // var split = sqlString.trim().split(";");
    // for (int i = 0; i < split.length; i++) {
    //   var sql = split[i].trim();
    //   if (sql.length > 0 && !sql.startsWith("--")) {
    //     print(sql);
    //     _sqliteDb.execute(sql);
    //   }
    // }

    addDefaultSpatialReferences();

    _sqliteDb.execute("PRAGMA application_id = 0x47503130;");
    _gpkgVersion = "1.0/1.1";
  }
}