Java Code Examples for androidx.sqlite.db.SupportSQLiteDatabase#query()

The following examples show how to use androidx.sqlite.db.SupportSQLiteDatabase#query() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewInfo.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Reads the view information from the given database.
 *
 * @param database The database to read the information from.
 * @param viewName The view name.
 * @return A ViewInfo containing the schema information for the provided view name.
 */
@SuppressWarnings("SameParameterValue")
public static ViewInfo read(SupportSQLiteDatabase database, String viewName) {
    Cursor cursor = database.query("SELECT name, sql FROM sqlite_master "
            + "WHERE type = 'view' AND name = '" + viewName + "'");
    //noinspection TryFinallyCanBeTryWithResources
    try {
        if (cursor.moveToFirst()) {
            return new ViewInfo(cursor.getString(0), cursor.getString(1));
        } else {
            return new ViewInfo(viewName, null);
        }
    } finally {
        cursor.close();
    }
}
 
Example 2
Source File: DBUtil.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Drops all FTS content sync triggers created by Room.
 * <p>
 * FTS content sync triggers created by Room are those that are found in the sqlite_master table
 * who's names start with 'room_fts_content_sync_'.
 *
 * @param db The database.
 */
public static void dropFtsSyncTriggers(SupportSQLiteDatabase db) {
    List<String> existingTriggers = new ArrayList<>();
    Cursor cursor = db.query("SELECT name FROM sqlite_master WHERE type = 'trigger'");
    //noinspection TryFinallyCanBeTryWithResources
    try {
        while (cursor.moveToNext()) {
            existingTriggers.add(cursor.getString(0));
        }
    } finally {
        cursor.close();
    }

    for (String triggerName : existingTriggers) {
        if (triggerName.startsWith("room_fts_content_sync_")) {
            db.execSQL("DROP TRIGGER IF EXISTS " + triggerName);
        }
    }
}
 
Example 3
Source File: FtsTableInfo.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("TryFinallyCanBeTryWithResources")
private static Set<String> readColumns(SupportSQLiteDatabase database, String tableName) {
    Cursor cursor = database.query("PRAGMA table_info(`" + tableName + "`)");
    Set<String> columns = new HashSet<>();
    try {
        if (cursor.getColumnCount() > 0) {
            int nameIndex = cursor.getColumnIndex("name");
            while (cursor.moveToNext()) {
                columns.add(cursor.getString(nameIndex));
            }
        }
    } finally {
        cursor.close();
    }
    return columns;
}
 
Example 4
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static HashMap<UUID, ArrayList<Integer>> getKeyIndexes(@NonNull final SupportSQLiteDatabase database, final String tableName) {
    Cursor cursor = database.query("SELECT * FROM " + tableName);
    final HashMap<UUID, ArrayList<Integer>> netKeyIndexMap = new HashMap<>();
    if (cursor != null && cursor.moveToFirst()) {
        final UUID meshUuid = UUID.fromString(cursor.getString(cursor.getColumnIndex("mesh_uuid")).toUpperCase(Locale.US));
        do {
            final int index = cursor.getInt(cursor.getColumnIndex("index"));
            ArrayList<Integer> indexes = netKeyIndexMap.get(meshUuid);
            if (indexes != null) {
                indexes.add(index);
            } else {
                indexes = new ArrayList<>();
                indexes.add(index);
            }
            netKeyIndexMap.put(meshUuid, indexes);
        } while (cursor.moveToNext());
    }
    return netKeyIndexMap;
}
 
Example 5
Source File: TableInfo.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @return null if we cannot read the index due to older sqlite implementations.
 */
@Nullable
private static Index readIndex(SupportSQLiteDatabase database, String name, boolean unique) {
    Cursor cursor = database.query("PRAGMA index_xinfo(`" + name + "`)");
    try {
        final int seqnoColumnIndex = cursor.getColumnIndex("seqno");
        final int cidColumnIndex = cursor.getColumnIndex("cid");
        final int nameColumnIndex = cursor.getColumnIndex("name");
        if (seqnoColumnIndex == -1 || cidColumnIndex == -1 || nameColumnIndex == -1) {
            // we cannot read them so better not validate any index.
            return null;
        }
        final TreeMap<Integer, String> results = new TreeMap<>();

        while (cursor.moveToNext()) {
            int cid = cursor.getInt(cidColumnIndex);
            if (cid < 0) {
                // Ignore SQLite row ID
                continue;
            }
            int seq = cursor.getInt(seqnoColumnIndex);
            String columnName = cursor.getString(nameColumnIndex);
            results.put(seq, columnName);
        }
        final List<String> columns = new ArrayList<>(results.size());
        columns.addAll(results.values());
        return new Index(name, unique, columns);
    } finally {
        cursor.close();
    }
}
 
Example 6
Source File: CompiledSelect1Impl.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
Cursor rawQuery(boolean inStream) {
  super.rawQuery(inStream);
  final SupportSQLiteDatabase db = dbConnection.getReadableDatabase();
  final long startNanos = nanoTime();
  final Cursor cursor = db.query(sql, args);
  if (SqliteMagic.LOGGING_ENABLED) {
    final long queryTimeInMillis = NANOSECONDS.toMillis(nanoTime() - startNanos);
    LogUtil.logQueryTime(queryTimeInMillis, observedTables, sql, args);
  }
  return FastCursor.tryCreate(cursor);
}
 
Example 7
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static List<NetworkKey> getNetKeys(@NonNull final SupportSQLiteDatabase database) {
    final List<NetworkKey> keys = new ArrayList<>();
    final Cursor cursor = database.query("SELECT * FROM network_key");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String meshUuid = cursor.getString(cursor.getColumnIndex("mesh_uuid")).toUpperCase(Locale.US);
            final int index = cursor.getInt(cursor.getColumnIndex("index"));
            final byte[] key = cursor.getBlob(cursor.getColumnIndex("key"));
            final NetworkKey networkKey = new NetworkKey(index, key);
            keys.add(networkKey);
        } while (cursor.moveToNext());
    }
    return keys;
}
 
Example 8
Source File: SqlLiteUtility.java    From dbsync with Apache License 2.0 5 votes vote down vote up
public static List<ValueMetadata> readTableMetadata(final SupportSQLiteDatabase db, final String tableName) {

        Cursor cursor = null;
        String columnName;
        String columnType;
        int notNull, pk, type;

        List<ValueMetadata> list = new ArrayList<>();

        cursor = db.query("PRAGMA table_info('" + tableName + "')", null);

        while (cursor.moveToNext()){

            columnName = getCursorString(cursor, "name");
            columnType = getCursorString(cursor, "type");
            notNull = getCursorInt(cursor, "notnull");
            pk = getCursorInt(cursor, "pk");

            switch (columnType) {
                case "INTEGER":
                    type = ValueMetadata.TYPE_LONG;
                    break;
                case "TEXT":
                    type = ValueMetadata.TYPE_STRING;
                    break;
                default:
                    throw new RuntimeException("Type " + columnType + " non supported !!!");
            }

           list.add(new ValueMetadata(columnName, type, notNull == 1, pk == 1));
        }

        cursor.close();

        return list;
    }
 
Example 9
Source File: PostHookSqlTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
private void assertOriginalContent(SupportSQLiteDatabase db) {
  Cursor c=db.query("SELECT _id, content FROM note;");

  assertNotNull(c);
  assertEquals(1, c.getCount());
  Assert.assertTrue(c.moveToFirst());
  assertEquals(1, c.getInt(0));
  assertEquals("this is a test", c.getString(1));
  c.close();
}
 
Example 10
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void migrateMeshNetwork(final SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `mesh_network_temp` " +
            "(`mesh_uuid` TEXT NOT NULL, " +
            "`mesh_name` TEXT, " +
            "`timestamp` INTEGER NOT NULL, " +
            "`iv_index` INTEGER NOT NULL, " +
            "`iv_update_state` INTEGER NOT NULL, " +
            "`unicast_address` INTEGER NOT NULL DEFAULT 0x0001, " +
            "`last_selected` INTEGER NOT NULL, " +
            "PRIMARY KEY(`mesh_uuid`))");

    database.execSQL(
            "INSERT INTO mesh_network_temp (mesh_uuid, mesh_name, timestamp, iv_index, iv_update_state, last_selected) " +
                    "SELECT mesh_uuid, mesh_name, timestamp, iv_index, iv_update_state, last_selected FROM mesh_network");
    final Cursor cursor = database.query("SELECT * FROM mesh_network");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String uuid = cursor.getString(cursor.getColumnIndex("mesh_uuid"));
            final byte[] unicast = cursor.getBlob(cursor.getColumnIndex("unicast_address"));
            final int address = MeshAddress.addressBytesToInt(unicast);
            final ContentValues values = new ContentValues();
            values.put("unicast_address", address);
            database.update("mesh_network_temp", SQLiteDatabase.CONFLICT_REPLACE, values, "mesh_uuid = ?", new String[]{uuid});
        } while (cursor.moveToNext());
        cursor.close();
    }
    database.execSQL("DROP TABLE mesh_network");
    database.execSQL("ALTER TABLE mesh_network_temp RENAME TO mesh_network");
}
 
Example 11
Source File: RekeyTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
private void assertOriginalContent(SupportSQLiteDatabase db) {
  Cursor c=db.query("SELECT bar, goo FROM foo;");

  assertNotNull(c);
  assertEquals(1, c.getCount());
  assertTrue(c.moveToFirst());
  assertEquals(1, c.getInt(0));
  assertEquals("two", c.getString(1));
  c.close();
}
 
Example 12
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void migrateMeshNetwork7_8(@NonNull final SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `mesh_network_temp` " +
            "(`mesh_uuid` TEXT NOT NULL, " +
            "`mesh_name` TEXT, " +
            "`timestamp` INTEGER NOT NULL, " +
            "`iv_index` TEXT NOT NULL, " +
            "`sequence_numbers` TEXT NOT NULL, " +
            "`last_selected` INTEGER NOT NULL, " +
            "PRIMARY KEY(`mesh_uuid`))");

    final Cursor cursor = database.query("SELECT * FROM mesh_network");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String uuid = cursor.getString(cursor.getColumnIndex("mesh_uuid"));
            final String meshName = cursor.getString(cursor.getColumnIndex("mesh_name"));
            final long timestamp = cursor.getInt(cursor.getColumnIndex("timestamp"));
            final int ivIndex = cursor.getInt(cursor.getColumnIndex("iv_index"));
            final int ivUpdateState = cursor.getInt(cursor.getColumnIndex("iv_update_state"));
            final String sequenceNumbers = cursor.getString(cursor.getColumnIndex("sequence_numbers"));
            final int lastSelected = cursor.getInt(cursor.getColumnIndex("last_selected"));
            final ContentValues values = new ContentValues();
            values.put("mesh_uuid", uuid);
            values.put("mesh_name", meshName);
            values.put("timestamp", timestamp);
            values.put("iv_index", MeshTypeConverters.ivIndexToJson(new IvIndex(ivIndex, ivUpdateState == MeshNetwork.IV_UPDATE_ACTIVE, Calendar.getInstance())));
            values.put("sequence_numbers", sequenceNumbers);
            values.put("last_selected", lastSelected);
            database.insert("mesh_network_temp", SQLiteDatabase.CONFLICT_REPLACE, values);
        } while (cursor.moveToNext());
        cursor.close();
    }
    database.execSQL("DROP TABLE mesh_network");
    database.execSQL("ALTER TABLE mesh_network_temp RENAME TO mesh_network");
}
 
Example 13
Source File: TableInfo.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private static Map<String, Column> readColumns(SupportSQLiteDatabase database,
        String tableName) {
    Cursor cursor = database
            .query("PRAGMA table_info(`" + tableName + "`)");
    //noinspection TryFinallyCanBeTryWithResources
    Map<String, Column> columns = new HashMap<>();
    try {
        if (cursor.getColumnCount() > 0) {
            int nameIndex = cursor.getColumnIndex("name");
            int typeIndex = cursor.getColumnIndex("type");
            int notNullIndex = cursor.getColumnIndex("notnull");
            int pkIndex = cursor.getColumnIndex("pk");
            int defaultValueIndex = cursor.getColumnIndex("dflt_value");

            while (cursor.moveToNext()) {
                final String name = cursor.getString(nameIndex);
                final String type = cursor.getString(typeIndex);
                final boolean notNull = 0 != cursor.getInt(notNullIndex);
                final int primaryKeyPosition = cursor.getInt(pkIndex);
                final String defaultValue = cursor.getString(defaultValueIndex);
                columns.put(name,
                        new Column(name, type, notNull, primaryKeyPosition, defaultValue,
                                CREATED_FROM_DATABASE));
            }
        }
    } finally {
        cursor.close();
    }
    return columns;
}
 
Example 14
Source File: DecryptTest.java    From cwac-saferoom with Apache License 2.0 5 votes vote down vote up
private void assertOriginalContent(SupportSQLiteDatabase db) {
  Cursor c=db.query("SELECT bar, goo FROM foo;");

  assertNotNull(c);
  assertEquals(1, c.getCount());
  assertTrue(c.moveToFirst());
  assertEquals(1, c.getInt(0));
  assertEquals("two", c.getString(1));
  c.close();
}
 
Example 15
Source File: FtsTableInfo.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("TryFinallyCanBeTryWithResources")
private static Set<String> readOptions(SupportSQLiteDatabase database, String tableName) {
    String sql = "";
    Cursor cursor = database.query(
            "SELECT * FROM sqlite_master WHERE `name` = '" + tableName + "'");
    try {
        if (cursor.moveToFirst()) {
            sql = cursor.getString(cursor.getColumnIndexOrThrow("sql"));
        }
    } finally {
        cursor.close();
    }
    return parseOptions(sql);
}
 
Example 16
Source File: RoomOpenHelper.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private static boolean hasEmptySchema(SupportSQLiteDatabase db) {
    Cursor cursor = db.query(
            "SELECT count(*) FROM sqlite_master WHERE name != 'android_metadata'");
    //noinspection TryFinallyCanBeTryWithResources
    try {
        return cursor.moveToFirst() && cursor.getInt(0) == 0;
    } finally {
        cursor.close();
    }
}
 
Example 17
Source File: CompiledSelectImpl.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
Cursor rawQuery(boolean inStream) {
  super.rawQuery(inStream);
  final SupportSQLiteDatabase db = dbConnection.getReadableDatabase();
  final long startNanos = nanoTime();
  final Cursor cursor = db.query(sql, args);
  if (SqliteMagic.LOGGING_ENABLED) {
    final long queryTimeInMillis = NANOSECONDS.toMillis(nanoTime() - startNanos);
    LogUtil.logQueryTime(queryTimeInMillis, observedTables, sql, args);
  }
  return FastCursor.tryCreate(cursor);
}
 
Example 18
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void migrateNodes(final SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `nodes_temp` " +
            "(`timestamp` INTEGER NOT NULL, " +
            "`mAddedNetworkKeys` TEXT, " +
            "`name` TEXT, `ttl` INTEGER, " +
            "`blacklisted` INTEGER NOT NULL, " +
            "`secureNetworkBeacon` INTEGER, " +
            "`mesh_uuid` TEXT, `uuid` TEXT NOT NULL, " +
            "`security` INTEGER NOT NULL, " +
            "`unicast_address` INTEGER NOT NULL DEFAULT 1, " +
            "`configured` INTEGER NOT NULL, " +
            "`device_key` BLOB, " +
            "`seq_number` INTEGER NOT NULL, " +
            "`cid` INTEGER, " +
            "`pid` INTEGER, " +
            "`vid` INTEGER, " +
            "`crpl` INTEGER, " +
            "`mElements` TEXT, " +
            "`mAddedApplicationKeys` TEXT, " +
            "`networkTransmitCount` INTEGER, " +
            "`networkIntervalSteps` INTEGER, " +
            "`relayTransmitCount` INTEGER, " +
            "`relayIntervalSteps` INTEGER, " +
            "`friend` INTEGER, " +
            "`lowPower` INTEGER, " +
            "`proxy` INTEGER, " +
            "`relay` INTEGER, " +
            "PRIMARY KEY(`uuid`), " +
            "FOREIGN KEY(`mesh_uuid`) REFERENCES `mesh_network`(`mesh_uuid`) ON UPDATE CASCADE ON DELETE CASCADE )");

    database.execSQL(
            "INSERT INTO nodes_temp (timestamp, mAddedNetworkKeys, name, blacklisted, secureNetworkBeacon, mesh_uuid, " +
                    "security, configured, device_key, seq_number, cid, pid, vid, crpl, mElements, " +
                    "mAddedApplicationKeys, networkTransmitCount, networkIntervalSteps, relayTransmitCount, relayIntervalSteps, " +
                    "friend, lowPower, proxy, relay, uuid, mesh_uuid) " +
                    "SELECT timestamp, mAddedNetworkKeys, name, blacklisted, secureNetworkBeacon, mesh_uuid, " +
                    "security, configured, device_key, seq_number, cid, pid, vid, crpl, mElements, " +
                    "mAddedApplicationKeys, networkTransmitCount, networkIntervalSteps, relayTransmitCount, relayIntervalSteps," +
                    "friend, lowPower, proxy, relay, uuid, mesh_uuid FROM nodes");

    final Cursor cursor = database.query("SELECT * FROM nodes");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String uuid = cursor.getString(cursor.getColumnIndex("uuid"));
            final byte[] unicast = cursor.getBlob(cursor.getColumnIndex("unicast_address"));
            final int address = MeshAddress.addressBytesToInt(unicast);
            final ContentValues values = new ContentValues();
            values.put("unicast_address", address);
            database.update("nodes_temp", SQLiteDatabase.CONFLICT_REPLACE, values, "uuid = ?", new String[]{uuid});
        } while (cursor.moveToNext());
        cursor.close();
    }
    database.execSQL("DROP TABLE nodes");
    database.execSQL("ALTER TABLE nodes_temp RENAME TO nodes");
    database.execSQL("CREATE INDEX index_nodes_mesh_uuid ON `nodes` (mesh_uuid)");
}
 
Example 19
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void migrateNodes3_4(final SupportSQLiteDatabase database) {
    database.execSQL("CREATE TABLE `nodes_temp` " +
            "(`timestamp` INTEGER NOT NULL, " +
            "`name` TEXT, `ttl` INTEGER, " +
            "`blacklisted` INTEGER NOT NULL, " +
            "`secureNetworkBeacon` INTEGER, " +
            "`mesh_uuid` TEXT, `uuid` TEXT NOT NULL, " +
            "`security` INTEGER NOT NULL, " +
            "`unicast_address` INTEGER NOT NULL DEFAULT 1, " +
            "`configured` INTEGER NOT NULL, " +
            "`device_key` BLOB, " +
            "`seq_number` INTEGER NOT NULL, " +
            "`cid` INTEGER, " +
            "`pid` INTEGER, " +
            "`vid` INTEGER, " +
            "`crpl` INTEGER, " +
            "`mElements` TEXT, " +
            "`netKeys` TEXT, " +
            "`appKeys` TEXT, " +
            "`networkTransmitCount` INTEGER, " +
            "`networkIntervalSteps` INTEGER, " +
            "`relayTransmitCount` INTEGER, " +
            "`relayIntervalSteps` INTEGER, " +
            "`friend` INTEGER, " +
            "`lowPower` INTEGER, " +
            "`proxy` INTEGER, " +
            "`relay` INTEGER, " +
            "PRIMARY KEY(`uuid`), " +
            "FOREIGN KEY(`mesh_uuid`) REFERENCES `mesh_network`(`mesh_uuid`) ON UPDATE CASCADE ON DELETE CASCADE )");

    database.execSQL(
            "INSERT INTO nodes_temp (timestamp, name, blacklisted, secureNetworkBeacon, mesh_uuid, " +
                    "security, unicast_address, configured, device_key, seq_number, cid, pid, vid, crpl, mElements, " +
                    "networkTransmitCount, networkIntervalSteps, relayTransmitCount, relayIntervalSteps, " +
                    "friend, lowPower, proxy, relay, uuid, mesh_uuid) " +
                    "SELECT timestamp, name, blacklisted, secureNetworkBeacon, mesh_uuid, " +
                    "security, unicast_address, configured, device_key, seq_number, cid, pid, vid, crpl, mElements, " +
                    "networkTransmitCount, networkIntervalSteps, relayTransmitCount, relayIntervalSteps," +
                    "friend, lowPower, proxy, relay, uuid, mesh_uuid FROM nodes");

    final Cursor cursor = database.query("SELECT * FROM nodes");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final ContentValues values = new ContentValues();
            final String uuid = cursor.getString(cursor.getColumnIndex("uuid"));

            final String netKeysJson = cursor.getString(cursor.getColumnIndex("mAddedNetworkKeys"));
            final List<NetworkKey> netKeys = MeshTypeConverters.fromJsonToAddedNetKeys(netKeysJson);
            final List<Integer> keyIndexes = new ArrayList<>();
            for (NetworkKey networkKey : netKeys) {
                if (networkKey != null) {
                    keyIndexes.add(networkKey.getKeyIndex());
                }
            }
            values.put("netKeys", MeshTypeConverters.integerToJson(keyIndexes));

            keyIndexes.clear();
            final String appKeysJson = cursor.getString(cursor.getColumnIndex("mAddedApplicationKeys"));
            final Map<Integer, ApplicationKey> appKeyMap = MeshTypeConverters.fromJsonToAddedAppKeys(appKeysJson);
            for (Map.Entry<Integer, ApplicationKey> applicationKeyEntry : appKeyMap.entrySet()) {
                final ApplicationKey key = applicationKeyEntry.getValue();
                if (key != null) {
                    keyIndexes.add(key.getKeyIndex());
                }
            }
            values.put("appKeys", MeshTypeConverters.integerToJson(keyIndexes));
            database.update("nodes_temp", SQLiteDatabase.CONFLICT_REPLACE, values, "uuid = ?", new String[]{uuid});
        } while (cursor.moveToNext());
        cursor.close();
    }
    database.execSQL("DROP TABLE nodes");
    database.execSQL("ALTER TABLE nodes_temp RENAME TO nodes");
    database.execSQL("CREATE INDEX index_nodes_mesh_uuid ON `nodes` (mesh_uuid)");
}
 
Example 20
Source File: MeshNetworkDb.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void migrateProvisioner4_5(final SupportSQLiteDatabase database) {
    final List<AllocatedUnicastRange> unicastRange = new ArrayList<>();
    final List<AllocatedGroupRange> groupRange = new ArrayList<>();
    final List<AllocatedSceneRange> sceneRange = new ArrayList<>();
    unicastRange.add(new AllocatedUnicastRange(0x0001, 0x199A));
    groupRange.add(new AllocatedGroupRange(0xC000, 0xCC9A));
    sceneRange.add(new AllocatedSceneRange(0x0001, 0x3333));

    database.execSQL("CREATE TABLE `provisioner_temp` " +
            "(`mesh_uuid` TEXT NOT NULL, " +
            "`provisioner_uuid` TEXT NOT NULL, " +
            "`name` TEXT, " +
            "`allocated_unicast_ranges` TEXT NOT NULL, " +
            "`allocated_group_ranges` TEXT NOT NULL, " +
            "`allocated_scene_ranges` TEXT NOT NULL, " +
            "`sequence_number` INTEGER NOT NULL, " +
            "`provisioner_address` INTEGER," +
            "`global_ttl` INTEGER NOT NULL, " +
            "`last_selected` INTEGER NOT NULL, PRIMARY KEY(`provisioner_uuid`), " +
            "FOREIGN KEY(`mesh_uuid`) REFERENCES `mesh_network`(`mesh_uuid`) ON UPDATE CASCADE ON DELETE CASCADE )");

    database.execSQL(
            "INSERT INTO provisioner_temp (mesh_uuid, provisioner_uuid, name, " +
                    "allocated_unicast_ranges, allocated_group_ranges, allocated_scene_ranges, " +
                    "sequence_number, global_ttl, last_selected) " +
                    "SELECT mesh_uuid, provisioner_uuid, name, " +
                    "allocatedUnicastRanges, allocatedGroupRanges, allocatedSceneRanges," +
                    "sequence_number, global_ttl, last_selected FROM provisioner");

    final List<Provisioner> provisioners = new ArrayList<>();
    Cursor cursor = database.query("SELECT * FROM provisioner");
    if (cursor != null && cursor.moveToFirst()) {
        do {
            final String meshUuid = cursor.getString(cursor.getColumnIndex("mesh_uuid"));
            final String uuid = cursor.getString(cursor.getColumnIndex("provisioner_uuid"));
            final String name = cursor.getString(cursor.getColumnIndex("name"));
            final String unicastRanges = cursor.getString(cursor.getColumnIndex("allocatedUnicastRanges"));
            final String groupRanges = cursor.getString(cursor.getColumnIndex("allocatedGroupRanges"));
            final String sceneRanges = cursor.getString(cursor.getColumnIndex("allocatedSceneRanges"));
            final int sequenceNumber = cursor.getInt(cursor.getColumnIndex("sequence_number"));
            final int globalTtl = cursor.getInt(cursor.getColumnIndex("global_ttl"));
            final boolean lastSelected = cursor.getInt(cursor.getColumnIndex("last_selected")) == 1;
            final int unicast = cursor.getInt(cursor.getColumnIndex("provisioner_address"));
            final ContentValues values = new ContentValues();
            values.put("mesh_uuid", meshUuid);
            values.put("provisioner_uuid", uuid);
            values.put("name", name);
            values.put("sequence_number", sequenceNumber);
            values.put("global_ttl", globalTtl);
            values.put("last_selected", lastSelected);
            if (unicast == 0) {
                final Integer t = null;
                values.put("provisioner_address", t);
            } else {
                values.put("provisioner_address", unicast);
            }
            values.put("allocated_unicast_ranges", unicastRanges.equalsIgnoreCase("null") ?
                    MeshTypeConverters.allocatedUnicastRangeToJson(unicastRange) : unicastRanges);
            values.put("allocated_group_ranges", groupRanges.equalsIgnoreCase("null") ?
                    MeshTypeConverters.allocatedGroupRangeToJson(groupRange) : groupRanges);
            values.put("allocated_scene_ranges", sceneRanges.equalsIgnoreCase("null") ?
                    MeshTypeConverters.allocatedSceneRangeToJson(sceneRange) : sceneRanges);
            database.update("provisioner_temp", SQLiteDatabase.CONFLICT_REPLACE, values, "provisioner_uuid = ?", new String[]{uuid});
            final Provisioner provisioner = new Provisioner(uuid,
                    unicastRanges.equalsIgnoreCase("null") ? unicastRange : MeshTypeConverters.fromJsonToAllocatedUnicastRanges(unicastRanges),
                    groupRanges.equalsIgnoreCase("null") ? groupRange : MeshTypeConverters.fromJsonToAllocatedGroupRanges(groupRanges),
                    sceneRanges.equalsIgnoreCase("null") ? sceneRange : MeshTypeConverters.fromJsonToAllocatedSceneRanges(sceneRanges),
                    meshUuid);
            provisioner.setProvisionerName(name);
            provisioner.setProvisionerAddress(unicast);
            provisioner.setLastSelected(lastSelected);
            provisioner.setGlobalTtl(globalTtl);
            provisioners.add(provisioner);
        } while (cursor.moveToNext());
        cursor.close();
    }

    database.execSQL("DROP TABLE provisioner");
    database.execSQL("ALTER TABLE provisioner_temp RENAME TO provisioner");
    database.execSQL("CREATE INDEX index_provisioner_mesh_uuid ON `provisioner` (mesh_uuid)");
    addProvisionerNodes(database, provisioners);
}