Beispiel #1
0
        private void Category_OnLoaded(object sender, RoutedEventArgs e)
        {
            var query = Db.Query("SELECT Name, FromFile FROM categories WHERE Path = @Path", new [] { new SQLiteParameter("Path", BookKeeper.GetRelativeBookFilePath(_bookFile)) });

            while (query.Read())
            {
                var category = new CategoryTag
                {
                    Name     = query["Name"].ToString(),
                    FromFile = SQLiteConvert.ToBoolean(query["FromFile"])
                };

                _categoryTagList.Add(category);
            }

            if (_categoryTagList.Count > 0)
            {
                CategoryTagsBorder.Visibility = Visibility.Visible;
            }

            Categories.ItemsSource = _categoryTagList;

            var defaultCategories = Properties.Settings.Default.DefaultCategories.Split(';');
            var hintList          = new List <string>(defaultCategories);

            hintList.AddRange(LibraryStructure.CategoryList());
            hintList.Sort();

            new Whisperer
            {
                TextBox  = (TextBox)sender,
                HintList = hintList
            };
        }
        public override bool TryGetValue(string keyword, out object value)
        {
            bool flag = base.TryGetValue(keyword, out value);

            if (!this._properties.ContainsKey(keyword))
            {
                return(flag);
            }
            PropertyDescriptor item = this._properties[keyword] as PropertyDescriptor;

            if (item == null)
            {
                return(flag);
            }
            if (!flag)
            {
                DefaultValueAttribute defaultValueAttribute = item.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
                if (defaultValueAttribute != null)
                {
                    value = defaultValueAttribute.Value;
                    flag  = true;
                }
            }
            else if (item.PropertyType == typeof(bool))
            {
                value = SQLiteConvert.ToBoolean(value);
            }
            else if (item.PropertyType != typeof(byte[]))
            {
                value = TypeDescriptor.GetConverter(item.PropertyType).ConvertFrom(value);
            }
            return(flag);
        }
Beispiel #3
0
        public static bool QueryExists(string sql, SQLiteParameter[] parameters = null)
        {
            bool exists;

            using (var command = new SQLiteCommand(sql, Connection))
            {
                if (parameters != null)
                {
                    command.Parameters.AddRange(parameters);
                }

                exists = SQLiteConvert.ToBoolean(command.ExecuteScalar());

                Connection.Close();
                Connection.Dispose();
            }

            return(exists);
        }
Beispiel #4
0
        /// <summary>
        /// Helper function for retrieving values from the connectionstring
        /// </summary>
        /// <param name="keyword">The keyword to retrieve settings for</param>
        /// <param name="value">The resulting parameter value</param>
        /// <returns>Returns true if the value was found and returned</returns>
        public override bool TryGetValue(string keyword, out object value)
        {
            bool b = base.TryGetValue(keyword, out value);

            if (!_properties.ContainsKey(keyword))
            {
                return(b);
            }

            PropertyDescriptor pd = _properties[keyword] as PropertyDescriptor;

            if (pd == null)
            {
                return(b);
            }

            // Attempt to coerce the value into something more solid
            if (b)
            {
                if (pd.PropertyType == typeof(Boolean))
                {
                    value = SQLiteConvert.ToBoolean(value);
                }
                else if (pd.PropertyType != typeof(byte[]))
                {
                    value = TypeDescriptor.GetConverter(pd.PropertyType).ConvertFrom(value);
                }
            }
            else
            {
                DefaultValueAttribute att = pd.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
                if (att != null)
                {
                    value = att.Value;
                    b     = true;
                }
            }
            return(b);
        }
Beispiel #5
0
        public override object ReadBoolean(DbDataReader reader, int index)
        {
            var value = reader.GetDecimal(index);

            return(SQLiteConvert.ToBoolean(value));
        }
        /// <summary>
        /// Perform the bind operation for an individual parameter
        /// </summary>
        /// <param name="index">The index of the parameter to bind</param>
        /// <param name="param">The parameter we're binding</param>
        private void BindParameter(int index, SQLiteParameter param)
        {
            if (param == null)
            {
                throw new SQLiteException("Insufficient parameters supplied to the command");
            }

            object obj     = param.Value;
            DbType objType = param.DbType;

            if ((obj != null) && (objType == DbType.Object))
            {
                objType = SQLiteConvert.TypeToDbType(obj.GetType());
            }

            if ((_flags & SQLiteConnectionFlags.LogPreBind) == SQLiteConnectionFlags.LogPreBind)
            {
                IntPtr handle = _sqlite_stmt;

                SQLiteLog.LogMessage(String.Format(
                                         "Binding statement {0} paramter #{1} with database type {2} and raw value {{{3}}}...",
                                         handle, index, objType, obj));
            }

            if ((obj == null) || Convert.IsDBNull(obj))
            {
                _sql.Bind_Null(this, _flags, index);
                return;
            }

            CultureInfo invariantCultureInfo = CultureInfo.InvariantCulture;
            bool        invariantText        = ((_flags & SQLiteConnectionFlags.BindInvariantText)
                                                == SQLiteConnectionFlags.BindInvariantText);

            if ((_flags & SQLiteConnectionFlags.BindAllAsText) == SQLiteConnectionFlags.BindAllAsText)
            {
                if (obj is DateTime)
                {
                    _sql.Bind_DateTime(this, _flags, index, (DateTime)obj);
                }
                else
                {
                    _sql.Bind_Text(this, _flags, index, invariantText ?
                                   SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                                   obj.ToString());
                }

                return;
            }

            CultureInfo cultureInfo = CultureInfo.CurrentCulture;

            if ((_flags & SQLiteConnectionFlags.ConvertInvariantText) == SQLiteConnectionFlags.ConvertInvariantText)
            {
                cultureInfo = invariantCultureInfo;
            }

            switch (objType)
            {
            case DbType.Date:
            case DbType.Time:
            case DbType.DateTime:
                //
                // NOTE: The old method (commented below) does not honor the selected date format
                //       for the connection.
                // _sql.Bind_DateTime(this, index, Convert.ToDateTime(obj, cultureInfo));
                _sql.Bind_DateTime(this, _flags, index, (obj is string) ?
                                   _sql.ToDateTime((string)obj) : Convert.ToDateTime(obj, cultureInfo));
                break;

            case DbType.Boolean:
                _sql.Bind_Int32(this, _flags, index, SQLiteConvert.ToBoolean(obj, cultureInfo, true) ? 1 : 0);
                break;

            case DbType.SByte:
                _sql.Bind_Int32(this, _flags, index, Convert.ToSByte(obj, cultureInfo));
                break;

            case DbType.Int16:
                _sql.Bind_Int32(this, _flags, index, Convert.ToInt16(obj, cultureInfo));
                break;

            case DbType.Int32:
                _sql.Bind_Int32(this, _flags, index, Convert.ToInt32(obj, cultureInfo));
                break;

            case DbType.Int64:
                _sql.Bind_Int64(this, _flags, index, Convert.ToInt64(obj, cultureInfo));
                break;

            case DbType.Byte:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToByte(obj, cultureInfo));
                break;

            case DbType.UInt16:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt16(obj, cultureInfo));
                break;

            case DbType.UInt32:
                _sql.Bind_UInt32(this, _flags, index, Convert.ToUInt32(obj, cultureInfo));
                break;

            case DbType.UInt64:
                _sql.Bind_UInt64(this, _flags, index, Convert.ToUInt64(obj, cultureInfo));
                break;

            case DbType.Single:
            case DbType.Double:
            case DbType.Currency:
                //case DbType.Decimal: // Dont store decimal as double ... loses precision
                _sql.Bind_Double(this, _flags, index, Convert.ToDouble(obj, cultureInfo));
                break;

            case DbType.Binary:
                _sql.Bind_Blob(this, _flags, index, (byte[])obj);
                break;

            case DbType.Guid:
                if (_command.Connection._binaryGuid == true)
                {
                    _sql.Bind_Blob(this, _flags, index, ((Guid)obj).ToByteArray());
                }
                else
                {
                    _sql.Bind_Text(this, _flags, index, invariantText ?
                                   SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                                   obj.ToString());
                }
                break;

            case DbType.Decimal: // Dont store decimal as double ... loses precision
                _sql.Bind_Text(this, _flags, index, Convert.ToDecimal(obj, cultureInfo).ToString(invariantCultureInfo));
                break;

            default:
                _sql.Bind_Text(this, _flags, index, invariantText ?
                               SQLiteConvert.ToStringWithProvider(obj, invariantCultureInfo) :
                               obj.ToString());
                break;
            }
        }
        /// <summary>
        /// Attempts to set the provider manifest options from the specified
        /// connection string parameters.  An exception may be thrown if one
        /// or more of the connection string parameter values do not conform
        /// to the expected type.
        /// </summary>
        /// <param name="opts">
        /// The dictionary containing the connection string parameters.
        /// </param>
        internal void SetFromOptions(
            SortedList <string, string> opts
            )
        {
            _dateTimeFormat       = SQLiteConnection.DefaultDateTimeFormat;
            _dateTimeKind         = SQLiteConnection.DefaultDateTimeKind;
            _dateTimeFormatString = SQLiteConnection.DefaultDateTimeFormatString;
            _binaryGuid           = /* SQLiteConnection.DefaultBinaryGUID; */ false; /* COMPAT: Legacy. */

            if (opts == null)
            {
                return;
            }

#if !PLATFORM_COMPACTFRAMEWORK
            string[] names = Enum.GetNames(typeof(SQLiteDateFormats));
#else
            string[] names =
            {
                SQLiteDateFormats.Ticks.ToString(),
                SQLiteDateFormats.ISO8601.ToString(),
                SQLiteDateFormats.JulianDay.ToString(),
                SQLiteDateFormats.UnixEpoch.ToString(),
                SQLiteDateFormats.InvariantCulture.ToString(),
                SQLiteDateFormats.CurrentCulture.ToString(),
                "Default"
            };
#endif

            foreach (string name in names)
            {
                if (String.IsNullOrEmpty(name))
                {
                    continue;
                }

                object value = SQLiteConnection.FindKey(opts, name, null);

                if (value == null)
                {
                    continue;
                }

                _dateTimeFormat = (SQLiteDateFormats)Enum.Parse(
                    typeof(SQLiteDateFormats), name, true);
            }

            object enumValue;

            enumValue = SQLiteConnection.TryParseEnum(
                typeof(SQLiteDateFormats), SQLiteConnection.FindKey(
                    opts, "DateTimeFormat", null), true);

            if (enumValue is SQLiteDateFormats)
            {
                _dateTimeFormat = (SQLiteDateFormats)enumValue;
            }

            enumValue = SQLiteConnection.TryParseEnum(
                typeof(DateTimeKind), SQLiteConnection.FindKey(
                    opts, "DateTimeKind", null), true);

            if (enumValue is DateTimeKind)
            {
                _dateTimeKind = (DateTimeKind)enumValue;
            }

            string stringValue = SQLiteConnection.FindKey(
                opts, "DateTimeFormatString", null);

            if (stringValue != null)
            {
                _dateTimeFormatString = stringValue;
            }

            stringValue = SQLiteConnection.FindKey(
                opts, "BinaryGUID", null);

            if (stringValue != null)
            {
                _binaryGuid = SQLiteConvert.ToBoolean(stringValue);
            }
        }
Beispiel #8
0
        private void BindParameter(int index, SQLiteParameter param)
        {
            bool                  flag;
            SQLiteBase            sQLiteBase;
            SQLiteConnectionFlags sQLiteConnectionFlag;
            int    num;
            string str;

            if (param == null)
            {
                throw new SQLiteException("Insufficient parameters supplied to the command");
            }
            if ((this._flags & SQLiteConnectionFlags.UseConnectionBindValueCallbacks) == SQLiteConnectionFlags.UseConnectionBindValueCallbacks)
            {
                this.InvokeBindValueCallback(index, param, out flag);
                if (flag)
                {
                    return;
                }
            }
            object value  = param.Value;
            DbType dbType = param.DbType;

            if (value != null && dbType == DbType.Object)
            {
                dbType = SQLiteConvert.TypeToDbType(value.GetType());
            }
            if (HelperMethods.LogPreBind(this._flags))
            {
                IntPtr      _sqliteStmt    = this._sqlite_stmt;
                CultureInfo currentCulture = CultureInfo.CurrentCulture;
                object[]    objArray       = new object[] { _sqliteStmt, index, dbType, value };
                SQLiteLog.LogMessage(HelperMethods.StringFormat(currentCulture, "Binding statement {0} paramter #{1} with database type {2} and raw value {{{3}}}...", objArray));
            }
            if (value == null || Convert.IsDBNull(value))
            {
                this._sql.Bind_Null(this, this._flags, index);
                return;
            }
            CultureInfo invariantCulture = CultureInfo.InvariantCulture;
            bool        flag1            = (this._flags & SQLiteConnectionFlags.BindInvariantText) == SQLiteConnectionFlags.BindInvariantText;
            CultureInfo cultureInfo      = CultureInfo.CurrentCulture;

            if ((this._flags & SQLiteConnectionFlags.ConvertInvariantText) == SQLiteConnectionFlags.ConvertInvariantText)
            {
                cultureInfo = invariantCulture;
            }
            if ((this._flags & SQLiteConnectionFlags.BindAllAsText) == SQLiteConnectionFlags.BindAllAsText)
            {
                if (value is DateTime)
                {
                    this._sql.Bind_DateTime(this, this._flags, index, (DateTime)value);
                    return;
                }
                this._sql.Bind_Text(this, this._flags, index, (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo)));
                return;
            }
            bool flag2 = (this._flags & SQLiteConnectionFlags.BindInvariantDecimal) == SQLiteConnectionFlags.BindInvariantDecimal;

            if ((this._flags & SQLiteConnectionFlags.BindDecimalAsText) == SQLiteConnectionFlags.BindDecimalAsText && value is decimal)
            {
                this._sql.Bind_Text(this, this._flags, index, (flag1 || flag2 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo)));
                return;
            }
            switch (dbType)
            {
            case DbType.Binary:
            {
                this._sql.Bind_Blob(this, this._flags, index, (byte[])value);
                return;
            }

            case DbType.Byte:
            {
                this._sql.Bind_UInt32(this, this._flags, index, Convert.ToByte(value, cultureInfo));
                return;
            }

            case DbType.Boolean:
            {
                this._sql.Bind_Boolean(this, this._flags, index, SQLiteConvert.ToBoolean(value, cultureInfo, true));
                return;
            }

            case DbType.Currency:
            case DbType.Double:
            case DbType.Single:
            {
                this._sql.Bind_Double(this, this._flags, index, Convert.ToDouble(value, cultureInfo));
                return;
            }

            case DbType.Date:
            case DbType.DateTime:
            case DbType.Time:
            {
                this._sql.Bind_DateTime(this, this._flags, index, (value is string?this._sql.ToDateTime((string)value) : Convert.ToDateTime(value, cultureInfo)));
                return;
            }

            case DbType.Decimal:
            {
                this._sql.Bind_Text(this, this._flags, index, (flag1 || flag2 ? SQLiteConvert.ToStringWithProvider(Convert.ToDecimal(value, cultureInfo), invariantCulture) : SQLiteConvert.ToStringWithProvider(Convert.ToDecimal(value, cultureInfo), cultureInfo)));
                return;
            }

            case DbType.Guid:
            {
                if (this._command.Connection._binaryGuid)
                {
                    SQLiteBase            sQLiteBase1           = this._sql;
                    SQLiteConnectionFlags sQLiteConnectionFlag1 = this._flags;
                    Guid guid = (Guid)value;
                    sQLiteBase1.Bind_Blob(this, sQLiteConnectionFlag1, index, guid.ToByteArray());
                    return;
                }
                this._sql.Bind_Text(this, this._flags, index, (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo)));
                return;
            }

            case DbType.Int16:
            {
                this._sql.Bind_Int32(this, this._flags, index, Convert.ToInt16(value, cultureInfo));
                return;
            }

            case DbType.Int32:
            {
                this._sql.Bind_Int32(this, this._flags, index, Convert.ToInt32(value, cultureInfo));
                return;
            }

            case DbType.Int64:
            {
                this._sql.Bind_Int64(this, this._flags, index, Convert.ToInt64(value, cultureInfo));
                return;
            }

            case DbType.Object:
            case DbType.String:
            {
                sQLiteBase           = this._sql;
                sQLiteConnectionFlag = this._flags;
                num = index;
                str = (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo));
                sQLiteBase.Bind_Text(this, sQLiteConnectionFlag, num, str);
                return;
            }

            case DbType.SByte:
            {
                this._sql.Bind_Int32(this, this._flags, index, Convert.ToSByte(value, cultureInfo));
                return;
            }

            case DbType.UInt16:
            {
                this._sql.Bind_UInt32(this, this._flags, index, Convert.ToUInt16(value, cultureInfo));
                return;
            }

            case DbType.UInt32:
            {
                this._sql.Bind_UInt32(this, this._flags, index, Convert.ToUInt32(value, cultureInfo));
                return;
            }

            case DbType.UInt64:
            {
                this._sql.Bind_UInt64(this, this._flags, index, Convert.ToUInt64(value, cultureInfo));
                return;
            }

            default:
            {
                sQLiteBase           = this._sql;
                sQLiteConnectionFlag = this._flags;
                num = index;
                str = (flag1 ? SQLiteConvert.ToStringWithProvider(value, invariantCulture) : SQLiteConvert.ToStringWithProvider(value, cultureInfo));
                sQLiteBase.Bind_Text(this, sQLiteConnectionFlag, num, str);
                return;
            }
            }
        }