public void test_column_origin() { using (sqlite3 db = ugly.open(":memory:")) { db.exec("CREATE TABLE foo (x int, v int, t text, d real, b blob, q blob);"); byte[] blob = db.query_scalar <byte[]>("SELECT randomblob(5);"); db.exec("INSERT INTO foo (x,v,t,d,b,q) VALUES (?,?,?,?,?,?)", 32, 44, "hello", 3.14, blob, null); #if not // maybe we should just let this fail so we can // see the differences between running against the built-in // sqlite vs a recent version? if (1 == raw.sqlite3_compileoption_used("ENABLE_COLUMN_METADATA")) #endif { using (sqlite3_stmt stmt = db.prepare("SELECT x AS mario FROM foo;")) { stmt.step(); Assert.IsTrue(stmt.stmt_readonly() != 0); Assert.AreEqual(stmt.column_database_name(0), "main"); Assert.AreEqual(stmt.column_table_name(0), "foo"); Assert.AreEqual(stmt.column_origin_name(0), "x"); Assert.AreEqual(stmt.column_name(0), "mario"); Assert.AreEqual(stmt.column_decltype(0), "int"); } } } }
public static T row <T>(this sqlite3_stmt stmt) where T : new() { Type typ = typeof(T); var obj = new T(); for (int i = 0; i < stmt.column_count(); i++) { string colname = stmt.column_name(i); #if OLD_REFLECTION var prop = typ.GetProperty(colname); #else var prop = typ.GetTypeInfo().GetDeclaredProperty(colname); #endif if ( (null != prop) && prop.CanWrite ) { prop.SetValue(obj, stmt.column(i, prop.PropertyType), null); } else { throw new NotSupportedException("property not found"); } } return(obj); }
public void test_row() { using (sqlite3 db = ugly.open(":memory:")) { db.exec("CREATE TABLE foo (x int, v int, t text, d real, b blob, q blob);"); byte[] blob = db.query_scalar <byte[]>("SELECT randomblob(5);"); db.exec("INSERT INTO foo (x,v,t,d,b,q) VALUES (?,?,?,?,?,?)", 32, 44, "hello", 3.14, blob, null); foreach (row r in db.query <row>("SELECT x,v,t,d,b,q FROM foo;")) { Assert.AreEqual(r.x, 32); Assert.AreEqual(r.v, 44); Assert.AreEqual(r.t, "hello"); Assert.AreEqual(r.d, 3.14); Assert.AreEqual(r.b.Length, blob.Length); for (int i = 0; i < blob.Length; i++) { Assert.AreEqual(r.b[i], blob[i]); } Assert.AreEqual(r.q, null); } using (sqlite3_stmt stmt = db.prepare("SELECT x,v,t,d,b,q FROM foo;")) { stmt.step(); Assert.AreEqual(stmt.db_handle(), db); Assert.AreEqual(stmt.column_int(0), 32); Assert.AreEqual(stmt.column_int64(1), 44); Assert.AreEqual(stmt.column_text(2), "hello"); Assert.AreEqual(stmt.column_double(3), 3.14); Assert.AreEqual(stmt.column_bytes(4), blob.Length); byte[] b2 = stmt.column_blob(4); Assert.AreEqual(b2.Length, blob.Length); for (int i = 0; i < blob.Length; i++) { Assert.AreEqual(b2[i], blob[i]); } Assert.AreEqual(stmt.column_type(5), raw.SQLITE_NULL); Assert.AreEqual(stmt.column_name(0), "x"); Assert.AreEqual(stmt.column_name(1), "v"); Assert.AreEqual(stmt.column_name(2), "t"); Assert.AreEqual(stmt.column_name(3), "d"); Assert.AreEqual(stmt.column_name(4), "b"); Assert.AreEqual(stmt.column_name(5), "q"); } } }