Exemple #1
0
        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);
        }
 public static bool ToBoolean(object source)
 {
     if (source is bool)
     {
         return((bool)source);
     }
     return(SQLiteConvert.ToBoolean(SQLiteConvert.ToStringWithProvider(source, CultureInfo.InvariantCulture)));
 }
        internal static bool ToBoolean(object obj, IFormatProvider provider, bool viaFramework)
        {
            object[]    objArray;
            CultureInfo currentCulture;

            if (obj == null)
            {
                return(false);
            }
            TypeCode typeCode = Type.GetTypeCode(obj.GetType());

            switch (typeCode)
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:
            {
                return(false);
            }

            case TypeCode.Object:
            case TypeCode.DateTime:
            case TypeCode.Object | TypeCode.DateTime:
            {
                currentCulture = CultureInfo.CurrentCulture;
                objArray       = new object[] { typeCode };
                throw new SQLiteException(HelperMethods.StringFormat(currentCulture, "Cannot convert type {0} to boolean", objArray));
            }

            case TypeCode.Boolean:
            {
                return((bool)obj);
            }

            case TypeCode.Char:
            {
                if ((char)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.SByte:
            {
                if ((sbyte)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Byte:
            {
                if ((byte)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Int16:
            {
                if ((short)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.UInt16:
            {
                if ((ushort)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Int32:
            {
                if ((int)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.UInt32:
            {
                if ((uint)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Int64:
            {
                if ((long)obj == (long)0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.UInt64:
            {
                if ((ulong)obj == (long)0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Single:
            {
                if ((float)obj == 0f)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Double:
            {
                if ((double)obj == 0)
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.Decimal:
            {
                if ((decimal)obj == new decimal(0))
                {
                    return(false);
                }
                return(true);
            }

            case TypeCode.String:
            {
                if (viaFramework)
                {
                    return(Convert.ToBoolean(obj, provider));
                }
                return(SQLiteConvert.ToBoolean(SQLiteConvert.ToStringWithProvider(obj, provider)));
            }

            default:
            {
                currentCulture = CultureInfo.CurrentCulture;
                objArray       = new object[] { typeCode };
                throw new SQLiteException(HelperMethods.StringFormat(currentCulture, "Cannot convert type {0} to boolean", objArray));
            }
            }
        }
Exemple #4
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;
            }
            }
        }