void BindAll(IntPtr stmt) { int nextIdx = 1; foreach (var b in _bindings) { if (b.Name != null) { b.Index = SQLite3.BindParameterIndex(stmt, b.Name); } else { b.Index = nextIdx++; } } foreach (var b in _bindings) { if (b.Value == null) { SQLite3.BindNull(stmt, b.Index); } else { var bty = b.Value.GetType(); if (b.Value is Byte || b.Value is UInt16 || b.Value is SByte || b.Value is Int16 || b.Value is Int32) { SQLite3.BindInt(stmt, b.Index, Convert.ToInt32(b.Value)); } else if (b.Value is Boolean) { SQLite3.BindInt(stmt, b.Index, Convert.ToBoolean(b.Value) ? 1 : 0); } else if (b.Value is UInt32 || b.Value is Int64) { SQLite3.BindInt64(stmt, b.Index, Convert.ToInt64(b.Value)); } else if (b.Value is Single || b.Value is Double || b.Value is Decimal) { SQLite3.BindDouble(stmt, b.Index, Convert.ToDouble(b.Value)); } else if (b.Value is String) { SQLite3.BindText(stmt, b.Index, b.Value.ToString(), -1, new IntPtr(-1)); } else if (b.Value is DateTime) { SQLite3.BindText(stmt, b.Index, ((DateTime)b.Value).ToString("yyyy-MM-dd HH:mm:ss"), -1, new IntPtr(-1)); } else if (bty.IsEnum) { SQLite3.BindInt(stmt, b.Index, Convert.ToInt32(b.Value)); } } } }
private static void BindParameter(Sqlite3Statement stmt, int index, object value, bool storeDateTimeAsTicks) { if (value == null) { SQLite3.BindNull(stmt, index); } else { if (value is Int32) { SQLite3.BindInt32(stmt, index, (int)value); } else if (value is String) { SQLite3.BindStringUTF8(stmt, index, (string)value, -1, NegativePointer); } else if (value is Byte || value is UInt16 || value is SByte || value is Int16) { SQLite3.BindInt32(stmt, index, Convert.ToInt32(value)); } else if (value is Boolean) { SQLite3.BindInt32(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.BindStringUTF8(stmt, index, ((DateTime)value).ToString(DateTimeExactStoreFormat, System.Globalization.CultureInfo.InvariantCulture), -1, NegativePointer); } } else if (value is DateTimeOffset) { SQLite3.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks); } else if (value is byte[]) { SQLite3.BindBlob(stmt, index, (byte[])value, ((byte[])value).Length, NegativePointer); } else if (value is Guid) { SQLite3.BindStringUTF8(stmt, index, ((Guid)value).ToString(), 72, NegativePointer); } else if (value is Uri) { SQLite3.BindStringUTF8(stmt, index, ((Uri)value).ToString(), -1, NegativePointer); } else if (value is StringBuilder) { SQLite3.BindStringUTF8(stmt, index, ((StringBuilder)value).ToString(), -1, NegativePointer); } else if (value is UriBuilder) { SQLite3.BindStringUTF8(stmt, index, ((UriBuilder)value).ToString(), -1, NegativePointer); } else { // Now we could possibly get an enum, retrieve cached info var valueType = value.GetType(); var enumInfo = EnumCache.GetInfo(valueType); if (enumInfo.IsEnum) { var enumIntValue = Convert.ToInt32(value); if (enumInfo.StoreAsText) { SQLite3.BindStringUTF8(stmt, index, enumInfo.GetEnumFromInt32Value(enumIntValue).ToString(), -1, NegativePointer); } else { SQLite3.BindInt32(stmt, index, enumIntValue); } } else { var type = value.GetType(); throw new NotSupportedException("Cannot store type: " + type); } } } }
internal static void BindParameter(IntPtr stmt, int index, object value, bool storeDateTimeAsTicks) { if (value == null) { SQLite3.BindNull(stmt, index); } else if (value is int) { 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 ushort || value is sbyte || value is short) { SQLite3.BindInt(stmt, index, Convert.ToInt32(value)); } else if (value is bool) { SQLite3.BindInt(stmt, index, ((bool)value) ? 1 : 0); } else if (value is uint || value is long) { SQLite3.BindInt64(stmt, index, Convert.ToInt64(value)); } else if (value is float || 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().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)) { throw new NotSupportedException("Cannot store type: " + value.GetType()); } SQLite3.BindText(stmt, index, ((Guid)value).ToString(), 72, NegativePointer); } }