Ejemplo n.º 1
0
        /// <summary>
        /// Returns a DatabaseRoute object containing the path and connection string for
        /// a database based on the prefix of the provided key string, if it has one.
        /// If the key has a prefix, it is stripped before returning.
        /// </summary>
        /// <param name="key"></param>
        /// <returns>Returns a DatabaseRoute object.</returns>
        public DatabaseRoute Route(string key)
        {
            DatabaseRoute route = new DatabaseRoute();

            //If no prefix is given, the roaming user store is used as the default.
            //Otherwise, use the appropriate one.
            switch (key.Substring(0, 3))
            {
            case "@ru":
                route.ConnectionString = this.RoamingConnection;
                route.Path             = this.RoamingUserDbPath;
                break;

            case "@lu":
                route.ConnectionString = this.LocalConnection;
                route.Path             = this.LocalUserDbPath;
                break;

            case "@ap":
                route.ConnectionString = this.ApplicationConnection;
                route.Path             = this.ApplicationDbPath;
                break;

            default:
                route.ConnectionString = this.RoamingConnection;
                route.Path             = this.RoamingUserDbPath;
                break;
            }
            route.FileName = this.DatabaseName;
            return(route);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves an object to a settings store, determined by the key prefix.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SaveSetting <T>(string key, T value)
        {
            //Add the setting to the hashtable so subsequent reads don't read from the database.
            lock (SyncRoot)
            {
                if (!_settingsCache.ContainsKey(key))
                {
                    _settingsCache[key] = value;
                }
            }

            //Serialize the value object to a XML string.
            string xml = SerializeToString(value);
            //Determine which setting store to use based on key prefix.
            DatabaseRoute route = ConnectionStrings.Route(key);

            //I don't like this double-try setup, it feels kludgey. Must find better solution. #todo
            try //to insert the setting into the database.
            {
                InsertSetting(Strip(key), xml, route);
            }
            catch (SQLiteException e)
            {
                switch ((SQLiteErrorCode)e.ErrorCode)
                {
                case SQLiteErrorCode.CantOpen: //Database path doesn't exist.
                case SQLiteErrorCode.Error:    //Probably path exists but not database file or file is empty.
                    try                        //to create the database
                    {
                        CreateDatabase(route);
                    }
                    catch (Exception)     //Still getting errors, give up. 🤷‍♂️
                    {
                        throw;
                    }

                    try     // again to insert the setting into the database.
                    {
                        InsertSetting(Strip(key), xml, route);
                    }
                    catch (Exception)     //Still getting errors, give up. 🤦‍♂️
                    {
                        throw;
                    }

                    break;

                default:     //A SQLite error that we're not prepared for; just pass it along.
                    throw;
                }
            }
            catch (Exception)
            {
                throw; //A non-SQLite error; pass it along.
            }
        }