示例#1
0
        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);
        }
示例#2
0
 public static T query_scalar <T>(this sqlite3 db, string sql, params object[] a)
 {
     using (sqlite3_stmt stmt = db.prepare(sql, a))
     {
         stmt.step();
         return(stmt.column <T>(0));
     }
 }
示例#3
0
        public static IEnumerable <T> query_one_column <T> (this sqlite3 db, string sql, params object[] a)
        {
            using (sqlite3_stmt stmt = db.prepare(sql, a))
            {
                if (1 != stmt.column_count())
                {
                    throw new InvalidOperationException("the SELECT expression for query_one_column() must have exactly one column");
                }

                while (raw.SQLITE_ROW == stmt.step())
                {
                    yield return(stmt.column <T>(0));
                }
            }
        }
示例#4
0
 public static T column <T>(this sqlite3_stmt stmt, int index)
 {
     return((T)stmt.column(index, typeof(T)));
 }