Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        public void ExecuteBindQuery(DatabaseDescriptor databaseDescriptor, EntityDescriptor entityDescriptor, String query, IEnumerator <Object> columnValues)
        {
            Log.Log.Debug(typeof(DatabaseImpl).FullName, "ExecuteBindQuery", "QUERY: " + query);

            ICollection <Object> values = new List <Object>();

            while (columnValues.MoveNext())
            {
                values.Add(columnValues.Current);
            }

            try
            {
                sqliteDatabase.Execute(query, values.ToArray());
            }
            catch (SQLiteException sqliteException)
            {
                if (sqliteDatabase.IsInTransaction)
                {
                    sqliteDatabase.Rollback();
                }

                Log.Log.Error(typeof(DatabaseImpl).FullName, "ExecuteBindQuery(" + query + ")", "SQLiteException caught while executing query, QUERY: " + query + ", " + sqliteException.Message);
                throw new DatabaseException(typeof(DatabaseImpl).FullName, "ExecuteBindQuery(" + query + ")", "SQLiteException caught while executing query, QUERY: " + query + ", " + sqliteException.Message);
            }
        }
Ejemplo n.º 3
0
        private DatabaseRegistry LoadConfig()
        {
            //var appEnv = CallContextServiceLocator.Locator.ServiceProvider.GetService(typeof(IApplicationEnvironment)) as IApplicationEnvironment;
            //var basePath = appEnv.ApplicationBasePath;

            var path = Setting.Instance.Get("DbSettingFilePath");

            var builder = new ConfigurationBuilder()
                          .AddJsonFile(path);

            var configuration = builder.Build();

            var databaseRegistry = new DatabaseRegistry();

            var descriptors = new List <DatabaseDescriptor>();

            foreach (var item in configuration.GetSection("databaseRegistry:databaseDescriptors").GetChildren())
            {
                var descriptor = new DatabaseDescriptor();
                descriptor.Name         = item.GetSection("databaseDescriptor").Value;
                descriptor.Address      = item.GetSection("address").Value;
                descriptor.DatabaseName = item.GetSection("databaseName").Value;
                descriptor.DatabaseType = EnumUtil.ParseEnum <DatabaseType>(item.GetSection("databaseType").Value);
                descriptor.UserName     = item.GetSection("userName").Value;
                descriptor.Password     = item.GetSection("password").Value;

                descriptors.Add(descriptor);
            }

            databaseRegistry.DatabaseDescriptors = descriptors;

            return(databaseRegistry);
        }
Ejemplo n.º 4
0
        /// <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;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// It process all DatabaseDescriptor.xml files defined by Application and stores in Resource Manager.
        /// </summary>
        protected static void ProcessDatabaseDescriptors()
        {
            Core.Siminov.ProcessDatabaseDescriptors();

            DatabaseDescriptorReader databaseDescriptorReader = new DatabaseDescriptorReader(Constants.DATABASE_DESSCRIPTOR_PATH);
            DatabaseDescriptor       databaseDescriptor       = databaseDescriptorReader.GetDatabaseDescriptor();

            Connect.Model.ApplicationDescriptor applicationDescriptor = connectResourceManager.GetApplicationDescriptor();
            applicationDescriptor.AddDatabaseDescriptor(Constants.DATABASE_DESSCRIPTOR_PATH, databaseDescriptor);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Parse the entity descriptor descriptor defined
        /// </summary>
        /// <exception cref="Siminov.Core.Exception.SiminovException">Any exception during parsing the descriptor file</exception>
        public void Process()
        {
            ApplicationDescriptor applicationDescriptor = resourceManager.GetApplicationDescriptor();

            if (applicationDescriptor == null)
            {
                Log.Log.Error(typeof(QuickEntityDescriptorReader).FullName, "Process", "Invalid Application Descriptor Found");
                throw new DeploymentException(typeof(QuickEntityDescriptorReader).FullName, "Process", "Invalid Application Descriptor Found.");
            }

            if (!applicationDescriptor.IsDatabaseNeeded())
            {
                doesMatch = false;
                return;
            }


            IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors();

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor   databaseDescriptor = databaseDescriptors.Current;
                IEnumerator <String> entityDescriptors  = databaseDescriptor.GetEntityDescriptorPaths();

                while (entityDescriptors.MoveNext())
                {
                    String entityDescriptorPath = entityDescriptors.Current;

                    Stream entityDescriptorStream = null;

                    try
                    {
                        ParseMessage(entityDescriptorStream);
                    }
                    catch (SiminovException exception)
                    {
                        Log.Log.Error(typeof(QuickEntityDescriptorReader).FullName, "Process", "Exception caught while parsing ENTITY-DESCRIPTOR: " + entityDescriptorPath + ", " + exception.GetMessage());
                        throw new SiminovException(typeof(QuickEntityDescriptorReader).FullName, "Process", "Exception caught while parsing ENTITY-DESCRIPTOR: " + entityDescriptorPath + ", " + exception.GetMessage());
                    }

                    if (doesMatch)
                    {
                        EntityDescriptorReader entityDescriptorParser = new EntityDescriptorReader(entityDescriptorPath);

                        this.entityDescriptor = entityDescriptorParser.GetEntityDescriptor();
                        databaseDescriptor.AddEntityDescriptor(entityDescriptorPath, entityDescriptor);

                        return;
                    }
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Get sdcard database path
 /// </summary>
 /// <param name="databaseDescriptor">Database Descriptor Object</param>
 /// <returns></returns>
 public String SdcardMemoryDatabasePath(DatabaseDescriptor databaseDescriptor)
 {
     /*File externalStorage = Environment.GetExternalStorageDirectory();
      * String externalStoragePath = externalStorage.GetAbsolutePath();
      *
      * ResourceManager resourceManager = ResourceManager.GetInstance();
      * ApplicationDescriptor applicationDescriptor = resourceManager.GetApplicationDescriptor();
      *
      * String databaseDirName = databaseDescriptor.GetDatabaseName();
      *
      * String databaseDirPath = externalStoragePath + File.separator + applicationDescriptor.getName() + File.separator + Constants.DATABASE_PATH_DATABASE + File.separator + databaseDirName + File.separator;
      * return databaseDirPath;*/
     return("");
 }
Ejemplo n.º 8
0
        public override void StartElement(XmlReader reader, IDictionary <String, String> attributes)
        {
            String localName = reader.Name;

            tempValue = new StringBuilder();

            if (localName.Equals(Constants.DATABASE_DESCRIPTOR, StringComparison.OrdinalIgnoreCase))
            {
                databaseDescriptor = new DatabaseDescriptor();
            }
            else if (localName.Equals(Constants.DATABASE_DESCRIPTOR_PROPERTY, StringComparison.OrdinalIgnoreCase))
            {
                InitializeProperty(attributes);
            }
        }
Ejemplo n.º 9
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
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Get all entity descriptors
        /// </summary>
        /// <returns>Entity Descriptors</returns>
        public IEnumerator <EntityDescriptor> GetEntityDescriptors()
        {
            ICollection <EntityDescriptor>   entityDescriptors   = new LinkedList <EntityDescriptor>();
            IEnumerator <DatabaseDescriptor> databaseDescriptors = this.applicationDescriptor.GetDatabaseDescriptors();

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current;

                IEnumerator <EntityDescriptor> descriptors = databaseDescriptor.GetEntityDescriptors();
                while (descriptors.MoveNext())
                {
                    EntityDescriptor entityDescriptor = descriptors.Current;
                    entityDescriptors.Add(entityDescriptor);
                }
            }

            return(entityDescriptors.GetEnumerator());
        }
Ejemplo n.º 13
0
        public void ExecuteQuery(DatabaseDescriptor databaseDescriptor, EntityDescriptor entityDescriptor, String query)
        {
            Log.Log.Debug(typeof(DatabaseImpl).FullName, "ExecuteQuery(" + query + ")", "QUERY: " + query);

            try
            {
                sqliteDatabase.Execute(query);
            }
            catch (SQLiteException sqliteException)
            {
                if (sqliteDatabase.IsInTransaction)
                {
                    sqliteDatabase.Rollback();
                }

                Log.Log.Error(typeof(DatabaseImpl).FullName, "ExecuteQuery(" + query + ")", "SQLiteException caught while executing query, QUERY: " + query + ", " + sqliteException.Message);
                throw new DatabaseException(typeof(DatabaseImpl).FullName, "ExecuteQuery(" + query + ")", "SQLiteException caught while executing query, QUERY: " + query + ", " + sqliteException.Message);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// It process all DatabaseDescriptor.xml files defined by Application and stores in Resource Manager.
        /// </summary>
        protected static void ProcessDatabaseDescriptors()
        {
            IEnumerator <String> databaseDescriptorPaths = coreResourceManager.GetApplicationDescriptor().GetDatabaseDescriptorPaths();

            while (databaseDescriptorPaths.MoveNext())
            {
                String databaseDescriptorPath = databaseDescriptorPaths.Current;

                DatabaseDescriptorReader databaseDescriptorParser = new DatabaseDescriptorReader(databaseDescriptorPath);

                DatabaseDescriptor databaseDescriptor = databaseDescriptorParser.GetDatabaseDescriptor();
                if (databaseDescriptor == null)
                {
                    Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabaseDescriptors", "Invalid Database Descriptor Path Found, DATABASE-DESCRIPTOR: " + databaseDescriptorPath);
                    throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabaseDescriptors", "Invalid Database Descriptor Path Found, DATABASE-DESCRIPTOR: " + databaseDescriptorPath);
                }

                coreResourceManager.GetApplicationDescriptor().AddDatabaseDescriptor(databaseDescriptorPath, databaseDescriptor);
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// It process all LibraryDescriptor.xml files defined by application, and stores in Resource Manager.
        /// </summary>
        protected static void ProcessLibraries()
        {
            ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor();
            IEnumerator <String>  libraries             = applicationDescriptor.GetLibraryDescriptorPaths();

            while (libraries.MoveNext())
            {
                String library = libraries.Current;

                /*
                 * Parse LibraryDescriptor.
                 */
                LibraryDescriptorReader libraryDescriptorReader = new LibraryDescriptorReader(library);
                LibraryDescriptor       libraryDescriptor       = libraryDescriptorReader.GetLibraryDescriptor();


                /*
                 * Map Entity Descriptors
                 */
                IEnumerator <String> entityDescriptors = libraryDescriptor.GetEntityDescriptorPaths();
                while (entityDescriptors.MoveNext())
                {
                    String libraryEntityDescriptorPath = entityDescriptors.Current;

                    String databaseDescriptorName = libraryEntityDescriptorPath.Substring(0, libraryEntityDescriptorPath.IndexOf(Constants.LIBRARY_DESCRIPTOR_ENTITY_DESCRIPTOR_SEPRATOR));
                    String entityDescriptor       = libraryEntityDescriptorPath.Substring(libraryEntityDescriptorPath.IndexOf(Constants.LIBRARY_DESCRIPTOR_ENTITY_DESCRIPTOR_SEPRATOR) + 1, ((libraryEntityDescriptorPath.Length - libraryEntityDescriptorPath.IndexOf(Constants.LIBRARY_DESCRIPTOR_ENTITY_DESCRIPTOR_SEPRATOR)) - 1));


                    IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors();
                    while (databaseDescriptors.MoveNext())
                    {
                        DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current;
                        if (databaseDescriptor.GetDatabaseName().Equals(databaseDescriptorName, StringComparison.OrdinalIgnoreCase))
                        {
                            databaseDescriptor.AddEntityDescriptorPath(library.Replace(".", "/") + FileUtils.Separator + entityDescriptor);
                        }
                    }
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// It process all EntityDescriptor.xml file defined in Application, and stores in Resource Manager.
        /// </summary>
        protected static void ProcessEntityDescriptors()
        {
            DoesDatabaseExists();

            ApplicationDescriptor applicationDescriptor = coreResourceManager.GetApplicationDescriptor();

            IEnumerator <DatabaseDescriptor> databaseDescriptors = applicationDescriptor.GetDatabaseDescriptors();

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor   databaseDescriptor    = databaseDescriptors.Current;
                IEnumerator <String> entityDescriptorPaths = databaseDescriptor.GetEntityDescriptorPaths();

                while (entityDescriptorPaths.MoveNext())
                {
                    String entityDescriptorPath = entityDescriptorPaths.Current;
                    EntityDescriptorReader entityDescriptorParser = new EntityDescriptorReader(entityDescriptorPath);

                    databaseDescriptor.AddEntityDescriptor(entityDescriptorPath, entityDescriptorParser.GetEntityDescriptor());
                }
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Get Entity Descriptor based on table name provided.
        /// </summary>
        /// <param name="tableName">Name of table</param>
        /// <returns>Entity Descriptor object in respect to table name.</returns>
        public EntityDescriptor GetEntityDescriptorBasedOnTableName(String tableName)
        {
            if (this.applicationDescriptor == null)
            {
                throw new DeploymentException(typeof(ResourceManager).FullName, "GetEntityDescriptorBasedOnTableName", "Siminov Not Active, INVALID APPLICATION-DESCRIPTOR FOUND");
            }

            IEnumerator <DatabaseDescriptor> databaseDescriptors = this.applicationDescriptor.GetDatabaseDescriptors();

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor databaseDescriptor             = databaseDescriptors.Current;
                bool containsEntityDescriptorInDatabaseDescriptor = databaseDescriptor.ContainsEntityDescriptorBasedOnTableName(tableName);

                if (containsEntityDescriptorInDatabaseDescriptor)
                {
                    return(databaseDescriptor.GetEntityDescriptorBasedOnTableName(tableName));
                }
            }

            return(null);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// It is used to stop all service started by SIMINOV.
        /// When application shutdown they should call this. It do following services:
        ///
        /// <list type="bullet">
        /// <item>
        ///     <description>Close all database's opened by SIMINOV.</description>
        /// </item>
        /// <item>
        ///     <description>Deallocate all resources held by SIMINOV.</description>
        /// </item>
        /// </list>
        /// </summary>
        public static void Shutdown()
        {
            IsActive();

            IEnumerator <DatabaseDescriptor> databaseDescriptors = coreResourceManager.GetDatabaseDescriptors();

            bool failed = false;

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current;
                DatabaseBundle     databaseBundle     = coreResourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName());
                IDatabaseImpl      database           = databaseBundle.GetDatabase();

                try
                {
                    database.Close(databaseDescriptor);
                }
                catch (DatabaseException databaseException)
                {
                    failed = true;

                    Log.Log.Error(typeof(Siminov).FullName, "Shutdown", "DatabaseException caught while closing database, " + databaseException.GetMessage());
                    continue;
                }
            }

            if (failed)
            {
                throw new SiminovCriticalException(typeof(Siminov).FullName, "Shutdown", "DatabaseException caught while closing database.");
            }

            ISiminovEvents coreEventHandler = coreResourceManager.GetSiminovEventHandler();

            if (coreResourceManager.GetSiminovEventHandler() != null)
            {
                coreEventHandler.OnSiminovStopped();
            }
        }
Ejemplo n.º 19
0
 /// <summary>
 /// Marks the current transaction as successful
 /// Finally it will End a transaction.
 /// <para>
 /// Example: Make Book Object
 /// <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>
 /// <exception cref="Siminov.Core.Exception.DatabaseException">If not able to commit the transaction</exception>
 public static void CommitTransaction(DatabaseDescriptor databaseDescriptor)
 {
     DatabaseHelper.CommitTransaction(databaseDescriptor);
 }
Ejemplo n.º 20
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)
 {
     DatabaseHelper.EndTransaction(databaseDescriptor);
 }
Ejemplo n.º 21
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());
 }
Ejemplo n.º 22
0
 public void Close(DatabaseDescriptor databaseDescriptor)
 {
     sqliteDatabase.Close();
 }
 public LocalizationsLoader(DatabaseDescriptor database)
 {
     Database = database;
 }
Ejemplo n.º 24
0
        public IEnumerator <IDictionary <String, Object> > ExecuteSelectQuery(DatabaseDescriptor databaseDescriptor, EntityDescriptor entityDescriptor, String query)
        {
            #if XAMARIN
            SQLitePCL.sqlite3_stmt statement = SQLite3.Prepare2(sqliteDatabase.Handle, query);
            #elif WINDOWS
            List <SQLiteQueryRow> statement = null;
            #endif

            try
            {
                #if XAMARIN
                statement = SQLite3.Prepare2(sqliteDatabase.Handle, query);
                #elif WINDOWS
                statement = sqliteDatabase.Query2(query);
                #endif
            }
            catch (System.Exception exception)
            {
                if (sqliteDatabase.IsInTransaction)
                {
                    sqliteDatabase.Rollback();
                }

                Log.Log.Error(typeof(Database).FullName, "ExecuteSelectQuery", "Exception caught while executing the select query, QUERY: " + query);
                throw new DatabaseException(typeof(Database).FullName, "ExecuteSelectQuery", exception.Message);
            }

            List <Dictionary <String, Object> > tuples = new List <Dictionary <String, Object> >();

            #if XAMARIN
            SQLite3.Result result;
            while ((result = SQLite3.Step(statement)) == SQLite3.Result.Row)
            {
                IDictionary <String, Object> tuple = new Dictionary <String, Object>();

                String stmtResult = result.ToString();

                //string[] names = SQLite3.ColType.GetNames ();
                //string[] values = SQLite3.ColType.GetValues ();
                int columnsCount = SQLite3.ColumnCount(statement);
                for (int i = 0; i < columnsCount; i++)
                {
                    String          columnName  = SQLite3.ColumnName(statement, i);
                    SQLite3.ColType columnType  = SQLite3.ColumnType(statement, i);
                    Object          columnValue = SQLite3.ColumnText(statement, i);

                    bool isString = false;
                    bool isLong   = false;
                    bool isFloat  = false;
                    bool isBlob   = false;

                    if (columnType == SQLite3.ColType.Text)
                    {
                        isString = true;
                    }
                    else if (columnType == SQLite3.ColType.Integer)
                    {
                        isLong = true;
                    }
                    else if (columnType == SQLite3.ColType.Float)
                    {
                        isFloat = true;
                    }
                    else if (columnType == SQLite3.ColType.Blob)
                    {
                        isBlob = true;
                    }
                    else if (columnType == SQLite3.ColType.Null)
                    {
                    }


                    if (isString)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_PRIMITIVE_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? true : false);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? Boolean.TrueString : Boolean.FalseString);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isLong)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isFloat)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }

                        tuple.Add(columnName, columnValue);
                    }
                    else if (isBlob)
                    {
                        tuple.Add(columnName, columnValue);
                    }
                }

                tuples.Add((Dictionary <String, Object>)tuple);
            }

            SQLite3.Finalize(statement);

            return(tuples.GetEnumerator());
            #elif WINDOWS
            foreach (SQLiteQueryRow cursor in statement)
            {
                IDictionary <String, Object> tuple = new Dictionary <String, Object>();

                List <SQLiteQueryColumn> columns = cursor.column;
                if (columns == null || columns.Count <= 0)
                {
                    continue;
                }

                foreach (SQLiteQueryColumn column in columns)
                {
                    String columnName  = column.Key;
                    Object columnValue = column.Value;


                    bool isString = false;
                    bool isLong   = false;
                    bool isFloat  = false;
                    bool isBlob   = false;

                    if (columnValue != null)
                    {
                        if (columnValue.GetType().FullName.Equals(typeof(String).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isString = true;
                        }
                        else if (columnValue.GetType().FullName.Equals(typeof(long).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isLong = true;
                        }
                        else if (columnValue.GetType().FullName.Equals(typeof(float).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isFloat = true;
                        }
                        else if (columnValue.GetType().FullName.Equals(typeof(byte).FullName, StringComparison.OrdinalIgnoreCase))
                        {
                            isBlob = true;
                        }
                    }
                    else
                    {
                        isString = true;
                    }


                    if (isString)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_PRIMITIVE_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? true : false);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.CSHARP_BOOLEAN_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue.Equals(Boolean.TrueString.ToString()) ? Boolean.TrueString : Boolean.FalseString);
                                }
                                else if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_STRING_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, (String)columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isLong)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }
                    }
                    else if (isFloat)
                    {
                        if (entityDescriptor != null)
                        {
                            EntityDescriptor.Attribute attribute = entityDescriptor.GetAttributeBasedOnColumnName(columnName);

                            if (attribute != null)
                            {
                                if (attribute.GetType().Equals(IDataTypeHandler.JAVASCRIPT_NUMBER_DATA_TYPE, StringComparison.OrdinalIgnoreCase))
                                {
                                    tuple.Add(columnName, columnValue);
                                }
                            }
                            else
                            {
                                tuple.Add(columnName, columnValue);
                            }
                        }
                        else
                        {
                            tuple.Add(columnName, columnValue);
                        }

                        tuple.Add(columnName, columnValue);
                    }
                    else if (isBlob)
                    {
                        tuple.Add(columnName, columnValue);
                    }
                }

                tuples.Add((Dictionary <String, Object>)tuple);
            }


            return(tuples.GetEnumerator());
            #endif
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Begins a transaction in EXCLUSIVE mode.
 /// <para>
 ///   Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back.
 ///   The changes will be rolled back if any transaction is ended without being marked as clean(by calling commitTransaction). Otherwise they will be committed.
 /// <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>
 /// <exception cref="Siminov.Core.ExceptionDatabaseException">If beginTransaction does not starts</exception>
 public static void BeginTransaction(DatabaseDescriptor databaseDescriptor)
 {
     DatabaseHelper.BeginTransaction(databaseDescriptor);
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Get IDatabase object based on Database Descriptor name.
        /// </summary>
        /// <param name="databaseName">Name of Database Descriptor</param>
        /// <returns>IDatabase object</returns>
        public DatabaseBundle GetDatabaseBundle(String databaseName)
        {
            DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptorBasedOnName(databaseName);

            return(this.databaseFactory.GetDatabaseBundle(databaseDescriptor));
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Remove IDatabase object from Resources based on Database Descriptor name.
        /// </summary>
        /// <param name="databaseDescriptorName">Database Descriptor name</param>
        public void RemoveDatabaseBundle(String databaseDescriptorName)
        {
            DatabaseDescriptor databaseDescriptor = GetDatabaseDescriptorBasedOnName(databaseDescriptorName);

            this.databaseFactory.RemoveDatabaseBundle(databaseDescriptor);
        }
Ejemplo n.º 28
0
        /// <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());
                    }
                }
            }
        }