initializeDB method
initializeDB is performed automatically in this version. You no longer need to call this method
Implementation
Future<bool> initializeDB() async {
databaseTables = databaseTables ?? [];
sequences = sequences ?? [];
final dbSequences = sequences!.where((i) => !i.initialized).toList();
if (dbSequences.isNotEmpty) {
final tableSquence = await SqfEntityProvider(this)
.execDataTable('PRAGMA table_info(sqfentitysequences)');
if (tableSquence.isEmpty) {
await SqfEntityProvider(this).execSQL(
'Create table sqfentitysequences (id text UNIQUE, value integer)');
}
for (SqfEntitySequenceBase sequence in dbSequences) {
final sqRow = await SqfEntityProvider(this).execDataTable(
'SELECT * FROM sqfentitysequences WHERE id=?',
[sequence.sequenceName]);
if (sqRow.isEmpty) {
await SqfEntityProvider(this).execSQL(
'INSERT INTO sqfentitysequences (id, value) VALUES (?,?)',
[sequence.sequenceName, sequence.startWith]);
}
sequence.initialized = true;
print(
'SQFENTITIY: Sequence [${sequence.sequenceName}] initialized successfully');
}
}
final dbTables = databaseTables!.where((i) => !i.initialized).toList();
if (dbTables.isNotEmpty) {
//List<String> updateQueryList = <String>[];
for (SqfEntityTableBase table in dbTables) {
switch (table.objectType) {
case ObjectType.view:
await SqfEntityProvider(this).execSQLList([
'DROP VIEW IF EXISTS ${table.tableName};',
'''CREATE VIEW ${table.tableName}
AS
${table.sqlStatement}'''
]);
break;
case ObjectType.table:
// check existing table fields in the database
final tableFields = await SqfEntityProvider(this)
.execDataTable('PRAGMA table_info(`${table.tableName}`)');
final List<TableField> existingDBfields = <TableField>[];
if (tableFields.isNotEmpty) {
String? primaryKeyName;
for (final row in tableFields) {
if (row['pk'].toString() == '1') {
primaryKeyName = row['name'].toString();
break;
}
}
primaryKeyName =
primaryKeyName ?? tableFields[0]['name'].toString();
if (!table.primaryKeyNames.contains(primaryKeyName)) {
throw Exception(
'SQFENTITIY: DATABASE INITIALIZE ERROR The primary key name \'$primaryKeyName\' for table named [${table.tableName}] must be in [${table.primaryKeyNames.join(',')}]');
}
final startIndex = table.primaryKeyName != null &&
table.primaryKeyName!.isNotEmpty
? 1
: 0;
for (int i = startIndex; i < tableFields.length; i++) {
existingDBfields.add(TableField(
tableFields[i]['name'].toString(),
parseDbType(tableFields[i]['type'].toString())));
}
// create SQL Command for new columns
final List<String> alterTableColsQuery =
checkTableColumns(table, existingDBfields);
if (alterTableColsQuery.isNotEmpty) {
print('SQFENTITIY: alterTableQuery => $alterTableColsQuery');
final result = await SqfEntityProvider(this)
.execSQLList(alterTableColsQuery);
if (result.success) {
table.initialized = true;
print(
'SQFENTITIY: Table named [${table.tableName}] was initialized successfully (Added new columns)');
if (checkForIsReadyDatabase(dbTables)) {
return true;
}
}
} else {
table.initialized = true;
print(
'SQFENTITIY: Table named [${table.tableName}] was initialized successfully (No added new columns)');
if (checkForIsReadyDatabase(dbTables)) {
return true;
}
}
} else // The table if not exist
{
final createTable =
await SqfEntityProvider(this).execSQL(table.createTableSQL);
if (createTable.success) {
final List<String> alterTableIndexesQuery =
checkTableIndexes(table);
table.initialized = true;
print(
'SQFENTITIY: Table named [${table.tableName}] has initialized successfully (created table)');
if (alterTableIndexesQuery.isNotEmpty) {
await SqfEntityProvider(this)
.execSQLList(alterTableIndexesQuery);
print(
'SQFENTITIY: alterTableIndexesQuery => $alterTableIndexesQuery');
}
if (checkForIsReadyDatabase(dbTables)) {
return true;
}
} else // table can not created
{
print(
'SQFENTITIY ERROR: Table named [${table.tableName}] could not create. Message: ${createTable.toString()}');
return false;
}
}
break;
default:
}
}
}
return true;
}