/// <summary> /// It process all DatabaseDescriptor.xml and initialize Database and stores in Resource Manager. /// </summary> protected static void ProcessDatabase() { ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor(); IEnumerator<DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; String databasePath = new DatabaseUtils().GetDatabasePath(databaseDescriptor); DatabaseBundle databaseBundle = null; try { databaseBundle = DatabaseHelper.CreateDatabase(databaseDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while getting database instance from database factory, DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } IDatabaseImpl database = databaseBundle.GetDatabase(); IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder(); /* * If Database exists then open and return. * If Database does not exists create the database. */ String databaseName = databaseDescriptor.GetDatabaseName(); if (!databaseName.EndsWith(".db")) { databaseName = databaseName + ".db"; } bool fileExists = FileUtils.DoesFileExists(databasePath, databaseName, FileUtils.LOCAL_FOLDER); if (fileExists) { /* * Open Database */ try { database.OpenOrCreate(databaseDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while opening database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Enable Foreign Key Constraints */ try { database.ExecuteQuery(databaseDescriptor, null, Constants.SQLITE_DATABASE_QUERY_TO_ENABLE_FOREIGN_KEYS_MAPPING); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while executing query to enable foreign keys, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Safe MultiThread Transaction */ /*try { database.ExecuteMethod(Constants.SQLITE_DATABASE_ENABLE_LOCKING, databaseDescriptor.IsTransactionSafe()); } catch (DatabaseException databaseException) { FileUtils.DoesFileExists(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while enabling locking on database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); }*/ /* * Upgrade Database */ try { DatabaseHelper.UpgradeDatabase(databaseDescriptor); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while upgrading database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } } else { /* * Create Database Directory */ try { FileUtils.GetFolder(databasePath, FileUtils.LOCAL_FOLDER); } catch (System.Exception exception) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "Exception caught while creating database directories, DATABASE-PATH: " + databasePath + ", DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + exception.Message); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", exception.Message); } /* * Create Database File. */ try { database.OpenOrCreate(databaseDescriptor); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while creating database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Set Database Version */ IDictionary<String, Object> parameters = new Dictionary<String, Object>(); parameters.Add(IQueryBuilder.FORM_UPDATE_DATABASE_VERSION_QUERY_DATABASE_VERSION_PARAMETER, databaseDescriptor.GetVersion()); try { String updateDatabaseVersionQuery = queryBuilder.FormUpdateDatabaseVersionQuery(parameters); database.ExecuteQuery(databaseDescriptor, null, updateDatabaseVersionQuery); } catch (DatabaseException databaseException) { Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "Database Exception caught while updating database version, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } IDatabaseEvents databaseEventHandler = coreResourceManager.GetDatabaseEventHandler(); if (databaseEventHandler != null) { databaseEventHandler.OnDatabaseCreated(databaseDescriptor); } /* * Enable Foreign Key Constraints */ try { database.ExecuteQuery(databaseDescriptor, null, Constants.SQLITE_DATABASE_QUERY_TO_ENABLE_FOREIGN_KEYS_MAPPING); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while executing query to enable foreign keys, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } /* * Safe MultiThread Transaction */ /*try { database.ExecuteMethod(Constants.SQLITE_DATABASE_ENABLE_LOCKING, databaseDescriptor.IsTransactionSafe()); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while enabling locking on database, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); }*/ /* * Create Tables */ try { DatabaseHelper.CreateTables(databaseDescriptor.OrderedEntityDescriptors()); } catch (DatabaseException databaseException) { FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER); Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while creating tables, " + databaseException.GetMessage()); throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage()); } } } }
/// <summary> /// It drop's the whole database based on database-descriptor /// <para> /// Drop the Book table /// <code> /// DatabaseDescriptor databaseDescriptor = new Book().GetDatabaseDescriptor(); /// /// try { /// Database.DropDatabase(databaseDescriptor); /// } catch(DatabaseException databaseException) { /// //Log It. /// } /// </code> /// </para> /// </summary> /// <param name="databaseDescriptor">Database Descriptor object which defines the structure of table</param> /// <exception cref="Siminov.Core.Exception.DatabaseException">If not able to drop database</exception> public static void DropDatabase(DatabaseDescriptor databaseDescriptor) { Siminov.IsActive(); DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseImpl database = databaseBundle.GetDatabase(); if (database == null) { Log.Log.Error(typeof(DatabaseHelper).FullName, "DropDatabase", "No Database Instance Found For DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName()); throw new DeploymentException(typeof(DatabaseHelper).FullName, "DropDatabase", "No Database Instance Found For DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName()); } String databasePath = new DatabaseUtils().GetDatabasePath(databaseDescriptor); database.Close(databaseDescriptor); String databaseName = databaseDescriptor.GetDatabaseName(); if (!databaseName.EndsWith(".db")) { databaseName = databaseName + ".db"; } FileUtils.DeleteFile(databasePath, databaseName, FileUtils.LOCAL_FOLDER); resourceManager.RemoveDatabaseBundle(databaseDescriptor.GetDatabaseName()); IDatabaseEvents databaseEventHandler = resourceManager.GetDatabaseEventHandler(); if (databaseEventHandler != null) { databaseEventHandler.OnDatabaseDropped(databaseDescriptor); } }
/// <summary> /// It is used to check whether database exists or not. /// </summary> protected static void DoesDatabaseExists() { ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor(); IEnumerator<DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors(); bool databaseExists = true; while (databaseDescriptors.MoveNext()) { DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current; String databasePath = new DatabaseUtils().GetDatabasePath(databaseDescriptor); String databaseName = databaseDescriptor.GetDatabaseName(); if (!databaseName.EndsWith(".db")) { databaseName = databaseName + ".db"; } bool fileExists = FileUtils.DoesFileExists(databasePath, databaseName, FileUtils.LOCAL_FOLDER); if (!fileExists) { databaseExists = false; } } if (!databaseExists) { firstTimeProcessed = true; } else { firstTimeProcessed = false; } }
/// <summary> /// Get database path based on which storage needed /// </summary> /// <param name="databaseDescriptor">Database Descriptor Object</param> /// <returns></returns> public String GetDatabasePath(DatabaseDescriptor databaseDescriptor) { DatabaseUtils databaseUtils = new DatabaseUtils(); String databasePath = null; bool isExternalStorageEnable = databaseDescriptor.IsExternalStorageEnable(); if (isExternalStorageEnable) { if (databaseUtils.IsSDCardPresent()) { databasePath = databaseUtils.SdcardMemoryDatabasePath(databaseDescriptor); } else { databasePath = databaseUtils.InternalMemoryDatabasePath(databaseDescriptor); } } else { databasePath = databaseUtils.InternalMemoryDatabasePath(databaseDescriptor); } return databasePath; }