public static int sqlite3_bind_parameter_index(Sqlite3StmtHandle pStmt, string zName) { var ptr = MarshalEx.StringToHGlobalUTF8(zName); try { return sqlite3_bind_parameter_index(pStmt, ptr); } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } }
public static int sqlite3_bind_text(Sqlite3StmtHandle pStmt, int i, string data, IntPtr xDel) { int nLen; var zData = MarshalEx.StringToHGlobalUTF8(data, out nLen); try { return Sqlite3.bind_text(pStmt, i, zData, nLen, xDel); } finally { if (zData != IntPtr.Zero) { Marshal.FreeHGlobal(zData); } } }
public static byte[] sqlite3_column_blob(Sqlite3StmtHandle pStmt, int iCol) { var ptr = sqlite3_column_blob_raw(pStmt, iCol); if (ptr == IntPtr.Zero) { return null; } var bytes = sqlite3_column_bytes(pStmt, iCol); var result = new byte[bytes]; Marshal.Copy(ptr, result, 0, bytes); return result; }
public static string sqlite3_bind_parameter_name(Sqlite3StmtHandle stmt, int i) => MarshalEx.PtrToStringUTF8(sqlite3_bind_parameter_name_raw(stmt, i));
private static extern int sqlite3_bind_blob(Sqlite3StmtHandle pStmt, int i, byte[] zData, int nData, IntPtr xDel);
public static extern int sqlite3_stmt_readonly(Sqlite3StmtHandle pStmt);
public static extern int sqlite3_bind_null(Sqlite3StmtHandle pStmt, int i);
private static extern IntPtr sqlite3_column_text_raw(Sqlite3StmtHandle stmt, int iCol);
public static int sqlite3_prepare_v2(Sqlite3Handle db, string zSql, out Sqlite3StmtHandle ppStmt, out string pzTail) { int nByte; var zSqlPtr = MarshalEx.StringToHGlobalUTF8(zSql, out nByte); try { // TODO: Something fancy with indexes? IntPtr pzTailPtr; var rc = sqlite3_prepare_v2(db, zSqlPtr, nByte, out ppStmt, out pzTailPtr); pzTail = MarshalEx.PtrToStringUTF8(pzTailPtr); return rc; } finally { Marshal.FreeHGlobal(zSqlPtr); } }
private static extern int sqlite3_bind_double(Sqlite3StmtHandle pStmt, int i, double rValue);
public int bind_double(Sqlite3StmtHandle pStmt, int i, double rValue) => sqlite3_bind_double(pStmt, i, rValue);
public int stmt_readonly(Sqlite3StmtHandle pStmt) => sqlite3_stmt_readonly(pStmt);
public int bind_blob(Sqlite3StmtHandle pStmt, int i, byte[] zData, int nData, IntPtr xDel) => sqlite3_bind_blob(pStmt, i, zData, nData, xDel);
private static extern int sqlite3_stmt_readonly(Sqlite3StmtHandle pStmt);
public int step(Sqlite3StmtHandle stmt) => sqlite3_step(stmt);
private static extern int sqlite3_step(Sqlite3StmtHandle stmt);
public static extern int sqlite3_column_count(Sqlite3StmtHandle stmt);
private static extern int sqlite3_bind_int64(Sqlite3StmtHandle pStmt, int i, long iValue);
public static extern double sqlite3_column_double(Sqlite3StmtHandle stmt, int iCol);
public int bind_int64(Sqlite3StmtHandle pStmt, int i, long iValue) => sqlite3_bind_int64(pStmt, i, iValue);
public static extern int sqlite3_column_type(Sqlite3StmtHandle stmt, int iCol);
private static extern int sqlite3_bind_null(Sqlite3StmtHandle pStmt, int i);
public static extern int sqlite3_step(Sqlite3StmtHandle stmt);
public int bind_null(Sqlite3StmtHandle pStmt, int i) => sqlite3_bind_null(pStmt, i);
public static extern int sqlite3_bind_double(Sqlite3StmtHandle pStmt, int i, double rValue);
private static extern int sqlite3_bind_parameter_count(Sqlite3StmtHandle stmt);
private static extern int sqlite3_bind_parameter_index(Sqlite3StmtHandle pStmt, IntPtr zName);
public int bind_parameter_count(Sqlite3StmtHandle stmt) => sqlite3_bind_parameter_count(stmt);
public int reset(Sqlite3StmtHandle stmt) => sqlite3_reset(stmt);
private static extern int sqlite3_bind_text(Sqlite3StmtHandle pStmt, int i, IntPtr zData, int n, IntPtr xDel);
public int bind_parameter_index(Sqlite3StmtHandle pStmt, IntPtr zName) => sqlite3_bind_parameter_index(pStmt, zName);
private static extern int sqlite3_column_bytes(Sqlite3StmtHandle pStmt, int iCol);
private static extern IntPtr sqlite3_bind_parameter_name(Sqlite3StmtHandle stmt, int i);
public static extern IntPtr sqlite3_column_decltype_raw(Sqlite3StmtHandle stmt, int iCol);
public IntPtr bind_parameter_name(Sqlite3StmtHandle stmt, int i) => sqlite3_bind_parameter_name(stmt, i);
public static extern long sqlite3_column_int64(Sqlite3StmtHandle stmt, int iCol);
public static string sqlite3_column_text(Sqlite3StmtHandle stmt, int iCol) => MarshalEx.PtrToStringUTF8(sqlite3_column_text_raw(stmt, iCol));
public int bind_text(Sqlite3StmtHandle pStmt, int i, IntPtr zData, int n, IntPtr xDel) => sqlite3_bind_text(pStmt, i, zData, n, xDel);
private static extern int sqlite3_prepare_v2(Sqlite3Handle db, IntPtr zSql, int nByte, out Sqlite3StmtHandle ppStmt, out IntPtr pzTail);
public static extern int sqlite3_reset(Sqlite3StmtHandle stmt);
//TODO move to a utility class public static int sqlite3_step_blocking(Sqlite3Handle db, Sqlite3StmtHandle stmt, int milliseconds) { var timer = new Stopwatch(); int rc; timer.Start(); while (SQLITE_LOCKED == (rc = sqlite3_step(stmt))) { if (timer.ElapsedMilliseconds >= milliseconds) { return rc; } sqlite3_reset(stmt); } return rc; }
public static extern int sqlite3_bind_blob(Sqlite3StmtHandle pStmt, int i, byte[] zData, int nData, IntPtr xDel);
public static extern int sqlite3_bind_int64(Sqlite3StmtHandle pStmt, int i, long iValue);
public static extern int sqlite3_bind_parameter_count(Sqlite3StmtHandle stmt);
internal int Bind(Sqlite3StmtHandle stmt) { var bound = 0; foreach (var parameter in _parameters) { if (parameter.Bind(stmt)) { bound++; } } return bound; }
private static extern IntPtr sqlite3_bind_parameter_name_raw(Sqlite3StmtHandle stmt, int i);
private static void BindBlob(Sqlite3StmtHandle stmt, int index, byte[] value) => NativeMethods.sqlite3_bind_blob(stmt, index, value, value.Length, Constants.SQLITE_TRANSIENT);
public static string sqlite3_column_name(Sqlite3StmtHandle stmt, int iCol) => MarshalEx.PtrToStringUTF8(sqlite3_column_name_raw(stmt, iCol));
private static extern int sqlite3_reset(Sqlite3StmtHandle stmt);
internal bool Bind(Sqlite3StmtHandle stmt) { if (_parameterName.Length == 0) { throw new InvalidOperationException(Strings.FormatRequiresSet("ParameterName")); } var index = NativeMethods.sqlite3_bind_parameter_index(stmt, _parameterName); if (index == 0) { return false; } if (_value == null) { throw new InvalidOperationException(Strings.FormatRequiresSet("Value")); } if (!_bindActionValid) { var type = Value.GetType().UnwrapNullableType().UnwrapEnumType(); if (type == typeof(bool)) { var value = (bool)_value ? 1L : 0; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(byte)) { var value = (long)(byte)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(byte[])) { var value = (byte[])_value; _bindAction = (s, i) => BindBlob(s, i, value); } else if (type == typeof(char)) { var value = (long)(char)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(DateTime)) { var value = ((DateTime)_value).ToString(@"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFF"); _bindAction = (s, i) => BindText(s, i, value); } else if (type == typeof(DateTimeOffset)) { var value = ((DateTimeOffset)_value).ToString(@"yyyy\-MM\-dd HH\:mm\:ss.FFFFFFFzzz"); _bindAction = (s, i) => BindText(s, i, value); } else if (type == typeof(DBNull)) { _bindAction = (s, i) => NativeMethods.sqlite3_bind_null(s, i); } else if (type == typeof(decimal)) { var value = ((decimal)_value).ToString(CultureInfo.InvariantCulture); _bindAction = (s, i) => BindText(s, i, value); } else if (type == typeof(double)) { var value = (double)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_double(s, i, value); } else if (type == typeof(float)) { var value = (double)(float)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_double(s, i, value); } else if (type == typeof(Guid)) { var value = ((Guid)_value).ToByteArray(); _bindAction = (s, i) => BindBlob(s, i, value); } else if (type == typeof(int)) { var value = (long)(int)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(long)) { var value = (long)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(sbyte)) { var value = (long)(sbyte)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(short)) { var value = (long)(short)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(string)) { var value = (string)_value; _bindAction = (s, i) => BindText(s, i, value); } else if (type == typeof(TimeSpan)) { var value = ((TimeSpan)_value).ToString("c"); _bindAction = (s, i) => BindText(s, i, value); } else if (type == typeof(uint)) { var value = (long)(uint)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(ulong)) { var value = (long)(ulong)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else if (type == typeof(ushort)) { var value = (long)(ushort)_value; _bindAction = (s, i) => NativeMethods.sqlite3_bind_int64(s, i, value); } else { throw new InvalidOperationException(Strings.FormatUnknownDataType(type)); } _bindActionValid = true; } _bindAction(stmt, index); return true; }
private static void BindText(Sqlite3StmtHandle stmt, int index, string value) => NativeMethods.sqlite3_bind_text(stmt, index, value, Constants.SQLITE_TRANSIENT);
public int prepare_v2(Sqlite3Handle db, IntPtr zSql, int nByte, out Sqlite3StmtHandle ppStmt, out IntPtr pzTail) => sqlite3_prepare_v2(db, zSql, nByte, out ppStmt, out pzTail);