コード例 #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 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));
                }
            }
        }