public static void bind(this sqlite3_stmt stmt, int ndx, string v) { stmt.bind_text(ndx, v); }
public static void bind(this sqlite3_stmt stmt, params object[] a) { if (a == null) { return; } int count = Math.Min(stmt.bind_parameter_count(), a.Length); // TODO instead of Math.Min(), consider comparing the two // counts and throwing if they're not equal. for (int i = 0; i < count; i++) { int ndx = i + 1; if (a[i] == null) { //Console.WriteLine("bind: {0} null", i); stmt.bind_null(ndx); } else { Type t = a[i].GetType(); //Console.WriteLine("bind: {0} {1} -- {2}", i, t, a[i]); if (typeof(String) == t) { stmt.bind_text(ndx, (string)a[i]); } else if ( (typeof(Int32) == t) || (typeof(Boolean) == t) || (typeof(Byte) == t) || (typeof(UInt16) == t) || (typeof(Int16) == t) || (typeof(sbyte) == t) || (typeof(Int64) == t) || (typeof(UInt32) == t) ) { stmt.bind_int64(ndx, (long)(Convert.ChangeType(a[i], typeof(long), null))); } else if ( (typeof(double) == t) || (typeof(float) == t) || (typeof(decimal) == t) ) { stmt.bind_double(ndx, (double)(Convert.ChangeType(a[i], typeof(double), null))); } else if (typeof(DateTime) == t) { DateTime d = (DateTime)a[i]; DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0); TimeSpan diff = d.ToUniversalTime() - origin; stmt.bind_int64(ndx, (long)diff.TotalSeconds); } else if (typeof(byte[]) == t) { stmt.bind_blob(ndx, (byte[])a[i]); } else { throw new NotSupportedException("Invalid type conversion" + t); } } } }