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); } }
private object ReadCol(IntPtr stmt, int index, SQLite3.ColType type, Type clrType) { if (type == SQLite3.ColType.Null) { return(null); } if (clrType == typeof(string)) { return(SQLite3.ColumnString(stmt, index)); } if (clrType == typeof(int)) { return(SQLite3.ColumnInt(stmt, index)); } if (clrType == typeof(bool)) { return(SQLite3.ColumnInt(stmt, index) == 1); } if (clrType == typeof(double)) { return(SQLite3.ColumnDouble(stmt, index)); } if (clrType == typeof(float)) { return((float)SQLite3.ColumnDouble(stmt, index)); } if (clrType == typeof(TimeSpan)) { return(new TimeSpan(SQLite3.ColumnInt64(stmt, index))); } if (clrType == typeof(DateTime)) { if (_conn.StoreDateTimeAsTicks) { return(new DateTime(SQLite3.ColumnInt64(stmt, index))); } string s = SQLite3.ColumnString(stmt, index); return(DateTime.Parse(s)); } if (clrType == typeof(DateTimeOffset)) { return(new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero)); } if (clrType.IsEnum) { return(SQLite3.ColumnInt(stmt, index)); } if (clrType == typeof(long)) { return(SQLite3.ColumnInt64(stmt, index)); } if (clrType == typeof(uint)) { return((uint)SQLite3.ColumnInt64(stmt, index)); } if (clrType == typeof(decimal)) { return((decimal)SQLite3.ColumnDouble(stmt, index)); } if (clrType == typeof(byte)) { return((byte)SQLite3.ColumnInt(stmt, index)); } if (clrType == typeof(ushort)) { return((ushort)SQLite3.ColumnInt(stmt, index)); } if (clrType == typeof(short)) { return((short)SQLite3.ColumnInt(stmt, index)); } if (clrType == typeof(sbyte)) { return((sbyte)SQLite3.ColumnInt(stmt, index)); } if (clrType == typeof(byte[])) { return(SQLite3.ColumnByteArray(stmt, index)); } if (clrType == typeof(Guid)) { string g = SQLite3.ColumnString(stmt, index); return(new Guid(g)); } throw new NotSupportedException("Don't know how to read " + clrType); }
public static string ColumnString(IntPtr stmt, int index) { return(Marshal.PtrToStringUni(SQLite3.ColumnText16(stmt, index))); }
private void Finalize(IntPtr stmt) { SQLite3.Finalize(stmt); }
private static void RegisterDefaultTypes() { #if UNITY_STANDALONE Register( typeof(Vector2), (stmt, index) => { var bytes = SQLite3.ColumnByteArray(stmt, index); var x = BitConverter.ToSingle(bytes, 0); var y = BitConverter.ToSingle(bytes, sizeof(float)); return(new Vector2(x, y)); }, (c) => { return("blob"); }, (stmt, index, value) => { var vec = (Vector2)value; var bytes = new byte[sizeof(float) * 2]; Buffer.BlockCopy(BitConverter.GetBytes(vec.x), 0, bytes, 0, sizeof(float)); Buffer.BlockCopy(BitConverter.GetBytes(vec.y), 0, bytes, sizeof(float), sizeof(float)); SQLite3.BindBlob(stmt, index, bytes, bytes.Length, new System.IntPtr(-1)); }); Register( typeof(Vector3), (stmt, index) => { var bytes = SQLite3.ColumnByteArray(stmt, index); var x = BitConverter.ToSingle(bytes, 0); var y = BitConverter.ToSingle(bytes, sizeof(float)); var z = BitConverter.ToSingle(bytes, sizeof(float) * 2); return(new Vector3(x, y, z)); }, (c) => { return("blob"); }, (stmt, index, value) => { var vec = (Vector3)value; var bytes = new byte[sizeof(float) * 3]; Buffer.BlockCopy(BitConverter.GetBytes(vec.x), 0, bytes, 0, sizeof(float)); Buffer.BlockCopy(BitConverter.GetBytes(vec.y), 0, bytes, sizeof(float), sizeof(float)); Buffer.BlockCopy(BitConverter.GetBytes(vec.z), 0, bytes, sizeof(float) * 2, sizeof(float)); SQLite3.BindBlob(stmt, index, bytes, bytes.Length, new System.IntPtr(-1)); }); Register( typeof(Color), (stmt, index) => { var bytes = SQLite3.ColumnByteArray(stmt, index); var r = BitConverter.ToSingle(bytes, 0); var g = BitConverter.ToSingle(bytes, sizeof(float)); var b = BitConverter.ToSingle(bytes, sizeof(float) * 2); var a = BitConverter.ToSingle(bytes, sizeof(float) * 3); return(new Color(r, g, b, a)); }, (c) => { return("blob"); }, (stmt, index, value) => { var vec = (Color)value; var bytes = new byte[sizeof(float) * 4]; Buffer.BlockCopy(BitConverter.GetBytes(vec.r), 0, bytes, 0, sizeof(float)); Buffer.BlockCopy(BitConverter.GetBytes(vec.g), 0, bytes, sizeof(float), sizeof(float)); Buffer.BlockCopy(BitConverter.GetBytes(vec.b), 0, bytes, sizeof(float) * 2, sizeof(float)); Buffer.BlockCopy(BitConverter.GetBytes(vec.a), 0, bytes, sizeof(float) * 3, sizeof(float)); SQLite3.BindBlob(stmt, index, bytes, bytes.Length, new System.IntPtr(-1)); }); #endif }
/// <summary> /// Gets the last row identifier. /// </summary> /// <returns> /// The <see cref="long"/>. /// </returns> public long GetLastRowId() => SQLite3.LastInsertRowid(this.Handle);
protected virtual IntPtr Prepare() { return(SQLite3.Prepare2(Connection.Handle, CommandText)); }
protected virtual Sqlite3Statement Prepare() { var stmt = SQLite3.Prepare2(Connection.Handle, CommandText); return(stmt); }
object ReadCol(Sqlite3Statement stmt, int index, SQLite3.ColType type, Type clrType) { if (type == SQLite3.ColType.Null) { return(null); } else { if (clrType == typeof(String)) { return(SQLite3.ColumnString(stmt, index)); } else if (clrType == typeof(Int32)) { return((int)SQLite3.ColumnInt(stmt, index)); } else if (clrType == typeof(Boolean)) { return(SQLite3.ColumnInt(stmt, index) == 1); } else if (clrType == typeof(double)) { return(SQLite3.ColumnDouble(stmt, index)); } else if (clrType == typeof(float)) { return((float)SQLite3.ColumnDouble(stmt, index)); } else if (clrType == typeof(TimeSpan)) { return(new TimeSpan(SQLite3.ColumnInt64(stmt, index))); } else if (clrType == typeof(DateTime)) { if (_conn.StoreDateTimeAsTicks) { return(new DateTime(SQLite3.ColumnInt64(stmt, index))); } else { var text = SQLite3.ColumnString(stmt, index); DateTime resultDate; if (!DateTime.TryParseExact(text, DateTimeExactStoreFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out resultDate)) { resultDate = DateTime.Parse(text); } return(resultDate); } } else if (clrType == typeof(DateTimeOffset)) { return(new DateTimeOffset(SQLite3.ColumnInt64(stmt, index), TimeSpan.Zero)); #if !USE_NEW_REFLECTION_API } else if (clrType.IsEnum) { #else } else if (clrType.GetTypeInfo().IsEnum) { #endif if (type == SQLite3.ColType.Text) { var value = SQLite3.ColumnString(stmt, index); return(Enum.Parse(clrType, value.ToString(), true)); } else { return(SQLite3.ColumnInt(stmt, index)); } } else if (clrType == typeof(Int64)) { return(SQLite3.ColumnInt64(stmt, index)); } else if (clrType == typeof(UInt32)) { return((uint)SQLite3.ColumnInt64(stmt, index)); } else if (clrType == typeof(decimal)) { return((decimal)SQLite3.ColumnDouble(stmt, index)); } else if (clrType == typeof(Byte)) { return((byte)SQLite3.ColumnInt(stmt, index)); } else if (clrType == typeof(UInt16)) { return((ushort)SQLite3.ColumnInt(stmt, index)); } else if (clrType == typeof(Int16)) { return((short)SQLite3.ColumnInt(stmt, index)); } else if (clrType == typeof(sbyte)) { return((sbyte)SQLite3.ColumnInt(stmt, index)); } else if (clrType == typeof(byte[])) { return(SQLite3.ColumnByteArray(stmt, index)); } else if (clrType == typeof(Guid)) { var text = SQLite3.ColumnString(stmt, index); return(new Guid(text)); } else { throw new NotSupportedException("Don't know how to read " + clrType); } } }
internal static void BindParameter(Sqlite3Statement 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(DateTimeExactStoreFormat, System.Globalization.CultureInfo.InvariantCulture), -1, NegativePointer); } } else if (value is DateTimeOffset) { SQLite3.BindInt64(stmt, index, ((DateTimeOffset)value).UtcTicks); } 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.BindText(stmt, index, enumInfo.EnumValues[enumIntValue], -1, NegativePointer); } else { SQLite3.BindInt(stmt, index, enumIntValue); } } 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()); } } } }
void Finalize(Sqlite3Statement stmt) { SQLite3.Finalize(stmt); }