Пример #1
0
        public override IEnumerable <T> ExecuteQueryScalars <T>()
        {
            var stmt = Prepare();

            try
            {
                if (SQLite3.ColumnCount(stmt) < 1)
                {
                    throw new InvalidOperationException("QueryScalars should return at least one column");
                }
                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    var colType = SQLite3.ColumnType(stmt, 0);
                    var val     = ReadCol(stmt, 0, colType, typeof(T));
                    if (val == null)
                    {
                        yield return(default(T));
                    }
                    else
                    {
                        yield return((T)val);
                    }
                }
            }
            finally
            {
                Finalize(stmt);
            }
        }
Пример #2
0
        public override T ExecuteScalar <T>()
        {
            T val = default(T);

            var stmt = Prepare();

            try
            {
                var r = SQLite3.Step(stmt);
                if (r == SQLite3.Result.Row)
                {
                    var colType = SQLite3.ColumnType(stmt, 0);
                    var colval  = ReadCol(stmt, 0, colType, typeof(T));
                    if (colval != null)
                    {
                        val = (T)colval;
                    }
                }
                else if (r == SQLite3.Result.Done)
                {
                }
                else
                {
                    throw SQLiteException.New(r, SQLite3.GetErrmsg(_conn.Handle));
                }
            }
            finally
            {
                Finalize(stmt);
            }

            return(val);
        }
Пример #3
0
        /// <summary>
        /// This creates a strongly typed delegate that will permit fast setting of column values given a Sqlite3Statement and a column index.
        ///
        /// Note that this is identical to CreateTypedSetterDelegate(), but has an extra check to see if it should create a nullable version of the delegate.
        /// </summary>
        /// <typeparam name="ObjectType">The type of the object whose member column is being set</typeparam>
        /// <typeparam name="ColumnMemberType">The CLR type of the member in the object which corresponds to the given SQLite columnn</typeparam>
        /// <param name="column">The column mapping that identifies the target member of the destination object</param>
        /// <param name="getColumnValue">A lambda that can be used to retrieve the column value at query-time</param>
        /// <returns>A strongly-typed delegate</returns>
        private static Action <ObjectType, sqlite3_stmt, int> CreateNullableTypedSetterDelegate <ObjectType, ColumnMemberType>(Column column, Func <sqlite3_stmt, int, ColumnMemberType> getColumnValue) where ColumnMemberType : struct
        {
            var  clrTypeInfo = column.PropertyInfo.PropertyType.GetTypeInfo();
            bool isNullable  = false;

            if (clrTypeInfo.IsGenericType && clrTypeInfo.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                isNullable = true;
            }

            if (isNullable)
            {
                var setProperty = (Action <ObjectType, ColumnMemberType?>)Delegate.CreateDelegate(
                    typeof(Action <ObjectType, ColumnMemberType?>), null,
                    column.PropertyInfo.GetSetMethod());

                return((o, stmt, i) =>
                {
                    var colType = SQLite3.ColumnType(stmt, i);
                    if (colType != SQLite3.ColType.Null)
                    {
                        setProperty.Invoke(o, getColumnValue.Invoke(stmt, i));
                    }
                });
            }

            return(CreateTypedSetterDelegate <ObjectType, ColumnMemberType>(column, getColumnValue));
        }
Пример #4
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(Base.TableMapping map, DaemonCompiledSetter _compiledSetter) where T : class
        {
            DaemonCompiledSetter <T, sqlite3_stmt> compiledSetter = _compiledSetter as DaemonCompiledSetter <T, sqlite3_stmt>;
            List <T> result = new List <T>();

            if (_conn.IsClosed)
            {
                _conn.RenewConnection();
            }

            Log.Logger.Debug(this.CommandText);
            var stmt = Prepare();

            try
            {
                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    var obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < compiledSetter.Columns.Length; i++)
                    {
                        if (compiledSetter.Columns[i] == null)
                        {
                            continue;
                        }

                        if (compiledSetter.Setters[i] != null)
                        {
                            if (obj is not T)
                            {
                            }
                            compiledSetter.Setters[i].Invoke((T)obj, stmt, i);
                        }
                        else
                        {
                            var colType = SQLite3.ColumnType(stmt, i);
                            var val     = ReadCol(stmt, i, colType, compiledSetter.Columns[i].ColumnType);
                            compiledSetter.Columns[i].SetValue(obj, val);
                        }
                    }

                    OnInstanceCreated(obj);
                    result.Add((T)obj);
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex, "SqliteCommand.ExecuteDeferredQuery");
            }
            finally
            {
                SQLite3.Finalize(stmt);
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// This creates a strongly typed delegate that will permit fast setting of column values given a Sqlite3Statement and a column index.
        /// </summary>
        /// <typeparam name="ObjectType">The type of the object whose member column is being set</typeparam>
        /// <typeparam name="ColumnMemberType">The CLR type of the member in the object which corresponds to the given SQLite columnn</typeparam>
        /// <param name="column">The column mapping that identifies the target member of the destination object</param>
        /// <param name="getColumnValue">A lambda that can be used to retrieve the column value at query-time</param>
        /// <returns>A strongly-typed delegate</returns>
        private static Action <ObjectType, sqlite3_stmt, int> CreateTypedSetterDelegate <ObjectType, ColumnMemberType>(Column column, Func <sqlite3_stmt, int, ColumnMemberType> getColumnValue)
        {
            var setProperty = (Action <ObjectType, ColumnMemberType>)Delegate.CreateDelegate(
                typeof(Action <ObjectType, ColumnMemberType>), null,
                column.PropertyInfo.GetSetMethod());

            return((o, stmt, i) =>
            {
                var colType = SQLite3.ColumnType(stmt, i);
                if (colType != SQLite3.ColType.Null)
                {
                    setProperty.Invoke(o, getColumnValue.Invoke(stmt, i));
                }
            });
        }
Пример #6
0
        public override IEnumerable <T> ExecuteDeferredQuery <T>(Kit.Sql.Base.TableMapping map)
        {
            List <T> result = new List <T>();

            if (_conn.IsClosed)
            {
                _conn.RenewConnection();
            }

            Log.Logger.Debug(this.CommandText);
            var stmt = Prepare();

            try
            {
                var cols = new Column[SQLite3.ColumnCount(stmt)];
                var fastColumnSetters = new Action <T, sqlite3_stmt, int> [SQLite3.ColumnCount(stmt)];

                for (int i = 0; i < cols.Length; i++)
                {
                    var name = SQLite3.ColumnName16(stmt, i);
                    cols[i] = map.FindColumn(name);
                    if (cols[i] != null)
                    {
                        fastColumnSetters[i] = FastColumnSetter.GetFastSetter <T>(_conn, cols[i]);
                    }
                }

                while (SQLite3.Step(stmt) == SQLite3.Result.Row)
                {
                    var obj = Activator.CreateInstance(map.MappedType);
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }

                        if (fastColumnSetters[i] != null)
                        {
                            if (obj is not T)
                            {
                            }
                            fastColumnSetters[i].Invoke((T)obj, stmt, i);
                        }
                        else
                        {
                            var colType = SQLite3.ColumnType(stmt, i);
                            var val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                            cols[i].SetValue(obj, val);
                        }
                    }

                    OnInstanceCreated(obj);
                    result.Add((T)obj);
                }
            }
            catch (Exception ex)
            {
                Log.Logger.Error(ex, "SqliteCommand.ExecuteDeferredQuery");
            }
            finally
            {
                SQLite3.Finalize(stmt);
            }

            return(result);
        }