void BindAll(IntPtr stmt)
        {
            int nextIdx = 1;

            foreach (var b in _bindings)
            {
                if ((b.Name != null) && (CommandText.IndexOf(b.Name) != -1))
                {
                    b.Index = SQLite3.BindParameterIndex(stmt, b.Name);
                }
                else
                {
                    b.Index = nextIdx++;
                }
            }
            for (int c = 0; c < _bindings.Count; c++)
            {
                var b = _bindings[c];
                if (b.Value == null)
                {
                    int n = SQLite3.BindNull(stmt, b.Index);
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
                if (b.Value is Byte || b.Value is UInt16 || b.Value is SByte || b.Value is Int16 || b.Value is Int32 || b.Value is Boolean)
                {
                    int n = SQLite3.BindInt(stmt, b.Index, Convert.ToInt32(b.Value, CultureInfo.InvariantCulture));
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
                if (b.Value is UInt32 || b.Value is Int64)
                {
                    int n = SQLite3.BindInt64(stmt, b.Index, Convert.ToInt64(b.Value, CultureInfo.InvariantCulture));
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
                if (b.Value is Single || b.Value is Double || b.Value is Decimal)
                {
                    int n = SQLite3.BindDouble(stmt, b.Index, Convert.ToDouble(b.Value, CultureInfo.InvariantCulture));
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
                if (b.Value is String)
                {
                    int n = SQLite3.BindText(stmt, b.Index, b.Value.ToString(), -1, IntPtr.Zero);
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
                if (b.Value is byte[])
                {
                    int n = SQLite3.BindBlob(stmt, b.Index, (byte[])b.Value, ((byte[])b.Value).Length, IntPtr.Zero);
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
                if (b.Value is DateTime)
                {
                    int n = SQLite3.BindText(stmt, b.Index, DateToString((DateTime)b.Value), -1, IntPtr.Zero);
                    if (n > 0)
                    {
                        throw new SQLiteException((SQLite3.Result)n, SQLiteLastError());
                    }
                    continue;
                }
            }
        }
 internal static void BindParameter(SQLitePCL.sqlite3_stmt stmt, int index, object value, bool storeDateTimeAsTicks)
 {
     if (value == null)
     {
         SQLite3.BindNull(stmt, index);
     }
     else
     {
         if (value is Int32)
         {
             SQLite3.BindInt(stmt, index, (int)value);
         }
         else if (value is String)
         {
             SQLite3.BindText(stmt, index, (string)value, -1, NegativePointer);
         }
         else if (value is Byte || value is UInt16 || value is SByte || value is Int16)
         {
             SQLite3.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is Boolean)
         {
             SQLite3.BindInt(stmt, index, (bool)value ? 1 : 0);
         }
         else if (value is UInt32 || value is Int64)
         {
             SQLite3.BindInt64(stmt, index, Convert.ToInt64(value));
         }
         else if (value is Single || value is Double || value is Decimal)
         {
             SQLite3.BindDouble(stmt, index, Convert.ToDouble(value));
         }
         else if (value is TimeSpan)
         {
             SQLite3.BindInt64(stmt, index, ((TimeSpan)value).Ticks);
         }
         else if (value is DateTime)
         {
             if (storeDateTimeAsTicks)
             {
                 SQLite3.BindInt64(stmt, index, ((DateTime)value).Ticks);
             }
             else
             {
                 SQLite3.BindText(stmt, index, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"), -1, NegativePointer);
             }
         }
         else if (value is DateTimeOffset)
         {
             SQLite3.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks);
         }
         else if (value.GetType().GetTypeInfo().IsEnum)
         {
             SQLite3.BindInt(stmt, index, Convert.ToInt32(value));
         }
         else if (value is byte[])
         {
             SQLite3.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer);
         }
         else if (value is Guid)
         {
             SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, NegativePointer);
         }
         else
         {
             throw new NotSupportedException("Cannot store type: " + value.GetType());
         }
     }
 }
Example #3
0
        internal static int BindParameter(Sqlite3Statement stmt, int index, object value, bool storeDateTimeAsTicks)
        {
            if (value == null)
            {
                return(SQLite3.BindNull(stmt, index));
            }
            if (value is int)
            {
                return(SQLite3.BindInt(stmt, index, (int)value));
            }
            if (value is string)
            {
                return(SQLite3.BindText(stmt, index, (string)value, -1, NegativePointer));
            }
            if (value is byte || value is ushort || value is sbyte || value is short)
            {
                return(SQLite3.BindInt(stmt, index, Convert.ToInt32(value)));
            }
            if (value is bool)
            {
                return(SQLite3.BindInt(stmt, index, (bool)value ? 1 : 0));
            }
            if (value is uint || value is long)
            {
                return(SQLite3.BindInt64(stmt, index, Convert.ToInt64(value)));
            }
            if (value is float || value is double || value is decimal)
            {
                return(SQLite3.BindDouble(stmt, index, Convert.ToDouble(value)));
            }
            if (value is TimeSpan)
            {
                return(SQLite3.BindInt64(stmt, index, ((TimeSpan)value).Ticks));
            }
            if (value is DateTime)
            {
                if (storeDateTimeAsTicks)
                {
                    return(SQLite3.BindInt64(stmt, index, ((DateTime)value).Ticks));
                }
                return(SQLite3.BindText(stmt, index, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"), -1,
                                        NegativePointer));
            }

            if (value is DateTimeOffset)
            {
                return(SQLite3.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks));
            }

            {
                if (value.GetType().IsEnum)
                {
                    return(SQLite3.BindInt(stmt, index, Convert.ToInt32(value)));
                }
            }

            if (value is byte[])
            {
                return(SQLite3.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer));
            }
            if (value is Guid)
            {
                return(SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, NegativePointer));
            }

            throw new NotSupportedException("Cannot store type: " + value.GetType());
        }
        /// <summary>
        /// bind a value to the statment with his index</summary>
        /// <returns>
        /// returnif the operation is a success </returns>
        /// <param name="value"> the object to bind</param>
        /// <param name="index"> the index of parameter</param>
        private bool bindValue(object value, int index)
        {
            if (value == null)
            {
                SQLite3.BindNull(stmt, index);
            }
            else
            {
                if (value is Int32)
                {
                    SQLite3.BindInt(stmt, index, (int)value);
                }
                else if (value is String)
                {
                    SQLite3.BindText(stmt, index, (string)value, -1, new IntPtr(-1));
                }
                else if (value is Byte || value is UInt16 || value is SByte || value is Int16)
                {
                    SQLite3.BindInt(stmt, index, Convert.ToInt32(value));
                }
                else if (value is Boolean)
                {
                    SQLite3.BindInt(stmt, index, (bool)value ? 1 : 0);
                }
                else if (value is UInt32 || value is Int64)
                {
                    SQLite3.BindInt64(stmt, index, Convert.ToInt64(value));
                }
                else if (value is Single || value is Double || value is Decimal)
                {
                    SQLite3.BindDouble(stmt, index, Convert.ToDouble(value));
                }
                else if (value is DateTime)
                {
                    SQLite3.BindText(stmt, index, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"), -1, new IntPtr(-1));
#if !NETFX_CORE
                }
                else if (value.GetType().IsEnum)
                {
#else
                }
                else if (value.GetType().GetTypeInfo().IsEnum)
                {
#endif
                    SQLite3.BindInt(stmt, index, Convert.ToInt32(value));
                }
                else if (value is byte[])
                {
                    SQLite3.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, new IntPtr(-1));
                }
                else if (value is Guid)
                {
                    SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, new IntPtr(-1));
                }
                else
                {
                    throw new NotSupportedException("Cannot store type: " + value.GetType());
                }
            }
            return(true);
        }