Exemplo n.º 1
0
        /// <summary>
        /// Get IDatabase Instance
        /// </summary>
        /// <param name="databaseDescriptor">DatabaseDescriptor Object</param>
        /// <returns>IDatabase Object</returns>
        /// <exception cref="Siminov.Core.Exception.DatabaseException">If not able to create IDatabase instance</exception>
        public DatabaseBundle GetDatabaseBundle(DatabaseDescriptor databaseDescriptor)
        {
            if (databaseBundles.ContainsKey(databaseDescriptor.GetDatabaseName()))
            {
                return databaseBundles[databaseDescriptor.GetDatabaseName()];
            }

            String type = databaseDescriptor.GetType();
            String packageName = DATABASE_PACKAGE_NAME + "." + type;

            DatabaseBundle databaseBundle = GetDatabaseBundle(packageName);
            databaseBundles.Add(databaseDescriptor.GetDatabaseName(), databaseBundle);

            return databaseBundle;
        }
Exemplo n.º 2
0
        public void OpenOrCreate(DatabaseDescriptor databaseDescriptor)
        {

            String databasePath = new DatabaseUtils().GetDatabasePath(databaseDescriptor);

            if (!FileUtils.DoesFolderExists(databasePath, FileUtils.LOCAL_FOLDER))
            {
                try
                {
                    FileUtils.CreateFolder(databasePath, FileUtils.LOCAL_FOLDER);
                }
                catch (System.Exception exception)
                {
                    Log.Log.Error(typeof(DatabaseImpl).FullName, "OpenOrCreate", "Exception caught while creating database directories, DATABASE-PATH: " + databasePath + ", DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + exception.Message);
                    throw new DeploymentException(typeof(DatabaseImpl).FullName, "OpenOrCreate", "Exception caught while creating database directories, DATABASE-PATH: " + databasePath + ", DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + exception.Message);
                }
            }

            String databaseName = databaseDescriptor.GetDatabaseName();
            if (databaseName == null || databaseName.Length <= 0)
            {
                Log.Log.Error(typeof(DatabaseImpl).FullName, "OpenOrCreate", "DATABASE-NAME IS MANDATORY FIELD - DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
                throw new DatabaseException(typeof(DatabaseImpl).FullName, "OpenOrCreate", "DATABASE-NAME IS MANDATORY FIELD - DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
            }

            if (!databaseName.EndsWith(".db"))
            {
                databaseName = databaseName + ".db";
            }

            try
            {

                #if XAMARIN
                    String databaseURI = Path.Combine (databasePath, databaseName);
                    sqliteDatabase = new SQLiteConnection(databaseURI);
                #elif WINDOWS
                    sqliteDatabase = new SQLiteConnection(databasePath + databaseName);
                #endif
            }
            catch (SQLiteException sqliteException)
            {
                Log.Log.Error(typeof(DatabaseImpl).FullName, "OpenOrCreate", "SQLiteException caught while opening database, " + sqliteException.Message);
                throw new DatabaseException(typeof(DatabaseImpl).FullName, "OpenOrCreate", "SQLiteException caught while opening database, " + sqliteException.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get internal memory database path
        /// </summary>
        /// <param name="databaseDescriptor">Database Descriptor Object</param>
        /// <returns></returns>
        public String InternalMemoryDatabasePath(DatabaseDescriptor databaseDescriptor)
        {

            #if XAMARIN
            
                var rootFolder = FileSystem.Current.LocalStorage;
			    return rootFolder.Path;

            #elif WINDOWS

                ResourceManager resourceManager = ResourceManager.GetInstance();
                ApplicationDescriptor applicationDescriptor = resourceManager.GetApplicationDescriptor();

                String databaseDirName = databaseDescriptor.GetDatabaseName();

                String databaseDirPath = applicationDescriptor.GetName() + FileUtils.Separator + Constants.DATABASE_PATH_DATABASE + FileUtils.Separator + databaseDirName + FileUtils.Separator;
                return databaseDirPath;
            
            #endif

        }
Exemplo n.º 4
0
 /// <summary>
 /// Remove DatabaseDescriptor object from Resources.
 /// </summary>
 /// <param name="databaseDescriptor">DatabaseDescriptor object which needs to be removed</param>
 public void RemoveDatabaseDescriptor(DatabaseDescriptor databaseDescriptor)
 {
     RemoveDatabaseDescriptorBasedOnName(databaseDescriptor.GetDatabaseName());
 }
Exemplo n.º 5
0
 /// <summary>
 /// Add Database Descriptor object in respect to database descriptor path.
 /// </summary>
 /// <param name="databaseDescriptorPath">Database Descriptor Path</param>
 /// <param name="databaseDescriptor">Database Descriptor Object</param>
 public void AddDatabaseDescriptor(String databaseDescriptorPath, DatabaseDescriptor databaseDescriptor)
 {
     this.databaseDescriptorsBasedOnPath.Add(databaseDescriptorPath, databaseDescriptor);
     this.databaseDescriptorsBasedOnName.Add(databaseDescriptor.GetDatabaseName(), databaseDescriptor);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Add Database Descriptor object in respect to database descriptor path.
 /// </summary>
 /// <param name="databaseDescriptorPath">Database Descriptor Path</param>
 /// <param name="databaseDescriptor">Database Descriptor Object</param>
 public void AddDatabaseDescriptor(String databaseDescriptorPath, DatabaseDescriptor databaseDescriptor)
 {
     this.databaseDescriptorsBasedOnPath.Add(databaseDescriptorPath, databaseDescriptor);
     this.databaseDescriptorsBasedOnName.Add(databaseDescriptor.GetDatabaseName(), databaseDescriptor);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Remove DatabaseDescriptor object from Resources.
 /// </summary>
 /// <param name="databaseDescriptor">DatabaseDescriptor object which needs to be removed</param>
 public void RemoveDatabaseDescriptor(DatabaseDescriptor databaseDescriptor)
 {
     RemoveDatabaseDescriptorBasedOnName(databaseDescriptor.GetDatabaseName());
 }
Exemplo n.º 8
0
        /// <summary>
        /// Marks the current transaction as successful
        /// <para>
        /// Finally it will End a transaction
        /// Example: Make Book Object
        /// <code>
        ///        Book cBook = new Book();
        ///        cBook.SetTitle(Book.BOOK_TYPE_C);
        ///        cBook.SetDescription("c_description");
        ///        cBook.SetHistory("c_author");
        ///        cBook.SetLink("c_link");
        ///        
        ///        DatabaseDescriptor databaseDescriptor = cBook.GetDatabaseDescriptor();
        /// 
        ///        try {
        ///            Database.BeginTransaction(databaseDescriptor);
        ///
        ///            cBook.Save();
        /// 
        ///            Database.CommitTransaction(databaseDescriptor);
        ///        } catch(DatabaseException de) {
        ///            //Log it.
        ///        } finally {
        ///            Database.EndTransaction(databaseDescriptor);
        ///        }
        /// </code>
        /// </para>
        /// </summary>
        /// <param name="databaseDescriptor">Database Descriptor Object</param>
        /// <exception cref="Siminiov.Core.Exception.DatabaseException">If not able to commit the transaction</exception>
        public static void CommitTransaction(DatabaseDescriptor databaseDescriptor)
        {
            Siminov.IsActive();

            if(databaseDescriptor.IsTransactionSafe())
            {
                transactionSafe.Release();
            }


            /*
             * 1. Get entity descriptor object for mapped invoked class object.
             */
            DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName());
            IDatabaseImpl database = databaseBundle.GetDatabase();

            if (database == null)
            {
                Log.Log.Error(typeof(DatabaseHelper).FullName, "CommitTransaction", "No Database Instance Found For DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
                throw new DeploymentException(typeof(DatabaseHelper).FullName, "CommitTransaction", "No Database Instance Found For DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
            }

            database.ExecuteMethod(Constants.SQLITE_DATABASE_COMMIT_TRANSACTION, null);
        }
Exemplo n.º 9
0
        /// <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);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Upgrade Existing Database
        /// </summary>
        /// <param name="databaseDescriptor">Database Descriptor Object</param>
        /// <exception cref="Siminov.Core.Exception.DatabaseException">throws If any exception thrown</exception>
        public static void UpgradeDatabase(DatabaseDescriptor databaseDescriptor)
        {

            DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName());

            IDatabaseImpl database = databaseBundle.GetDatabase();
            IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder();

            if (database == null)
            {
                Log.Log.Error(typeof(DatabaseHelper).FullName, "UpgradeDatabase", "No Database Instance Found For, DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
                throw new DatabaseException(typeof(DatabaseHelper).FullName, "UpgradeDatabase", "No Database Instance Found For, DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
            }


            /*
             * Fetch Database Version
             */
            String fetchDatabaseVersionQuery = queryBuilder.FormFetchDatabaseVersionQuery(null);
            Log.Log.Debug(typeof(DatabaseHelper).FullName, "UpgradeDatabase", "Fetch Database Version Query: " + fetchDatabaseVersionQuery);


            double currentDatabaseVersion = 0;
            IEnumerator<IDictionary<String, Object>> datas = database.ExecuteSelectQuery(databaseDescriptor, null, fetchDatabaseVersionQuery);

            while (datas.MoveNext())
            {
                IDictionary<String, Object> data = datas.Current;
                ICollection<Object> parse = data.Values;

                IEnumerator<Object> values = parse.GetEnumerator();
                while (values.MoveNext())
                {

                    Object value = values.Current;
                    if (value.GetType().FullName.Equals(typeof(String).FullName, StringComparison.OrdinalIgnoreCase))
                    {
                        currentDatabaseVersion = Double.Parse((String)value);
                    }
                    else if (value.GetType().FullName.Equals(typeof(Double).FullName, StringComparison.OrdinalIgnoreCase))
                    {
                        currentDatabaseVersion = (double)value;
                    }
                }
            }


            if (currentDatabaseVersion == databaseDescriptor.GetVersion())
            {
                return;
            }

            ICollection<EntityDescriptor> allEntityDescriptor = new List<EntityDescriptor>();
            IEnumerator<EntityDescriptor> allEntityDescriptorIterator = resourceManager.GetEntityDescriptors();
            while (allEntityDescriptorIterator.MoveNext())
            {
                allEntityDescriptor.Add(allEntityDescriptorIterator.Current);
            }



            ICollection<String> tableNames = new List<String>();

            IEnumerator<EntityDescriptor> entityDescriptors = databaseDescriptor.GetEntityDescriptors();

            /*
             * Get Table Names
             */
            String fetchTableNamesQuery = queryBuilder.FormTableNames(null);
            Log.Log.Debug(typeof(DatabaseHelper).FullName, "UpgradeDatabase", "Fetch Table Names, " + fetchTableNamesQuery);

            datas = database.ExecuteSelectQuery(databaseDescriptor, null, fetchTableNamesQuery);
            while (datas.MoveNext())
            {
                IDictionary<String, Object> data = datas.Current;
                IEnumerator<String> keys = data.Keys.GetEnumerator();

                while (keys.MoveNext())
                {
                    String key = keys.Current;

                    if (key.Equals(Constants.FORM_TABLE_NAMES_NAME, StringComparison.OrdinalIgnoreCase))
                    {
                        tableNames.Add((String)data[key]);
                    }
                }
            }


            /*
             * Create Or Upgrade Table
             */
            while (entityDescriptors.MoveNext())
            {
                EntityDescriptor entityDescriptor = entityDescriptors.Current;

                bool contain = false;
                foreach (String tableName in tableNames)
                {

                    if (tableName.Equals(entityDescriptor.GetTableName(), StringComparison.OrdinalIgnoreCase))
                    {
                        contain = true;
                        break;
                    }
                }

                if (contain)
                {
                    DatabaseHelper.UpgradeTable(entityDescriptor);
                }
                else
                {
                    DatabaseHelper.CreateTable(entityDescriptor);
                }
            }


            /*
             * Drop Table
             */
            foreach (String tableName in tableNames)
            {
                if (tableName.Equals(Constants.ANDROID_METADATA_TABLE_NAME, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }


                bool contain = false;

                foreach (EntityDescriptor entityDescriptor in allEntityDescriptor)
                {

                    if (tableName.Equals(entityDescriptor.GetTableName(), StringComparison.OrdinalIgnoreCase))
                    {
                        contain = true;
                        break;
                    }
                }

                if (!contain)
                {

                    IDictionary<String, Object> childParameters = new Dictionary<String, Object>();
                    childParameters.Add(IQueryBuilder.FORM_DROP_TABLE_QUERY_TABLE_NAME_PARAMETER, tableName);

                    database.ExecuteQuery(databaseDescriptor, null, queryBuilder.FormDropTableQuery(childParameters));
                }
            }




            /*
             * Update Database Version
             */
            IDictionary<String, Object> parameters = new Dictionary<String, Object>();
            parameters.Add(IQueryBuilder.FORM_UPDATE_DATABASE_VERSION_QUERY_DATABASE_VERSION_PARAMETER, databaseDescriptor.GetVersion());

            String updateDatabaseVersionQuery = queryBuilder.FormUpdateDatabaseVersionQuery(parameters);
            Log.Log.Debug(typeof(DatabaseHelper).FullName, "UpgradeDatabase", "Update Database Version Query: " + updateDatabaseVersionQuery);

            database.ExecuteQuery(databaseDescriptor, null, updateDatabaseVersionQuery);
        }
Exemplo n.º 11
0
        /// <summary>
        /// End the current transaction
        /// <para>
        /// Example:
        /// <code>
        ///        Book cBook = new Book();
        ///        cBook.SetTitle(Book.BOOK_TYPE_C);
        ///        cBook.SetDescription("c_description");
        ///        cBook.SetAuthor("c_author");
        ///        cBook.SetLink("c_link");
        ///        
        ///        DatabaseDescriptor databaseDescriptor = cBook.GetDatabaseDescriptor();
        /// 
        ///        try {
        ///            Database.BeginTransaction(databaseDescriptor);
        ///
        ///            cBook.Save();
        /// 
        ///            Database.CommitTransaction(databaseDescriptor);
        ///        } catch(DatabaseException de) {
        ///            //Log it.
        ///        } finally {
        ///            Database.EndTransaction(databaseDescriptor);
        ///        }
        /// </code>
        /// </para>
        /// </summary>
        /// <param name="databaseDescriptor">Database Descriptor Object</param>
        public static void EndTransaction(DatabaseDescriptor databaseDescriptor)
        {
            Siminov.IsActive();

            /*
             * 1. Get entity descriptor object for mapped invoked class object.
             */
            DatabaseBundle databaseBundle = resourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName());
            IDatabaseImpl database = databaseBundle.GetDatabase();

            if (database == null)
            {
                Log.Log.Error(typeof(DatabaseHelper).FullName, "CommitTransaction", "No Database Instance Found For DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
                throw new DeploymentException(typeof(DatabaseHelper).FullName, "CommitTransaction", "No Database Instance Found For DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName());
            }

            try
            {
                database.ExecuteMethod(Constants.SQLITE_DATABASE_END_TRANSACTION, null);
            }
            catch (DatabaseException databaseException)
            {
                Log.Log.Error(typeof(DatabaseHelper).FullName, "CommitTransaction", "DatabaseException caught while executing end transaction method, " + databaseException.GetMessage());
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Removes database bundle instance
 /// </summary>
 /// <param name="databaseDescriptor">Database descriptor instance object</param>
 public void RemoveDatabaseBundle(DatabaseDescriptor databaseDescriptor)
 {
     this.databaseBundles.Remove(databaseDescriptor.GetDatabaseName());
 }