예제 #1
0
        public IEnumerable <object> ExecuteQuery(TableMapping map)
        {
            if (_conn.Trace)
            {
                Console.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

            var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

            for (int i = 0; i < cols.Length; i++)
            {
                var name = Marshal.PtrToStringUni(SQLite3.ColumnName16(stmt, i));
                cols[i] = map.FindColumn(name);
            }

            while (SQLite3.Step(stmt) == SQLite3.Result.Row)
            {
                var obj = Activator.CreateInstance(map.MappedType);
                map.SetConnection(obj, _conn);
                for (int i = 0; i < cols.Length; i++)
                {
                    if (cols[i] == null)
                    {
                        continue;
                    }
                    var val = ReadCol(stmt, i, cols[i].ColumnType);
                    cols[i].SetValue(obj, val);
                }
                yield return(obj);
            }

            SQLite3.Finalize(stmt);
        }
예제 #2
0
 public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
 {
     if (_conn.Trace)
     {
         _conn.InvokeTrace("Executing Query: " + this);
     }
     lock (_conn.SyncObject)
     {
         IntPtr stmt = Prepare();
         try
         {
             TableMapping.Column[] cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];
             for (int j = 0; j < cols.Length; j++)
             {
                 string name = SQLite3.ColumnName16(stmt, j);
                 cols[j] = map.FindColumn(name);
             }
             while (SQLite3.Step(stmt) == SQLite3.Result.Row)
             {
                 object obj = Activator.CreateInstance(map.MappedType);
                 for (int i = 0; i < cols.Length; i++)
                 {
                     if (cols[i] != null)
                     {
                         SQLite3.ColType colType = SQLite3.ColumnType(stmt, i);
                         object          val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                         cols[i].SetValue(obj, val);
                     }
                 }
                 OnInstanceCreated(obj);
                 yield return((T)obj);
             }
         }
         finally
         {
             ((_003CExecuteDeferredQuery_003Ec__Iterator3 <T>) /*Error near IL_0234: stateMachine*/)._003C_003E__Finally0();
         }
     }
 }
예제 #3
0
        public IEnumerable <T> ExecuteDeferredQuery <T>(TableMapping map)
        {
            if (_conn.Trace)
            {
                Debug.WriteLine("Executing Query: " + this);
            }

            var stmt = Prepare();

            try {
                var cols = new TableMapping.Column[SQLite3.ColumnCount(stmt)];

                for (int i = 0; i < cols.Length; i++)
                {
                    var name = SQLite3.ColumnName16(stmt, i);
                    cols[i] = map.FindColumn(name);
                }

                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;
                        }
                        var colType = SQLite3.ColumnType(stmt, i);
                        var val     = ReadCol(stmt, i, colType, cols[i].ColumnType);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }
            } finally {
                SQLite3.Finalize(stmt);
            }
        }
예제 #4
0
        public IEnumerable <T> ExecuteQuery <T>(TableMapping map, object[] source)
        {
            CheckDisposed();
            Log(nameof(ExecuteQuery), source);
            OnExecutionStarted();

            var sw = Stopwatch.StartNew();

            try
            {
                var r = Result.OK;

                if (!Prepared)
                {
                    Statement = Prepare();
                    Prepared  = true;
                }

                //bind the values.
                if (source != null)
                {
                    for (int i = 0; i < source.Length; i++)
                    {
                        BindParameter(Statement, i + 1, source[i], Connection.StoreDateTimeAsTicks);
                    }
                }

                var cols = new TableColumn[SQLite3.ColumnCount(Statement)];

                for (int i = 0; i < cols.Length; i++)
                {
                    var name = SQLite3.ColumnNameUTF8(Statement, i);
                    cols[i] = map.FindColumn(name);
                }

                while (SQLite3.Step(Statement) == Result.Row)
                {
                    var obj = map.CreateInstance();
                    for (int i = 0; i < cols.Length; i++)
                    {
                        if (cols[i] == null)
                        {
                            continue;
                        }
                        var colType = SQLite3.ColumnType(Statement, i);
                        var val     = ReadCol(Statement, i, colType, cols[i].ColumnType, cols[i].PropertyDefaultValue);
                        cols[i].SetValue(obj, val);
                    }
                    OnInstanceCreated(obj);
                    yield return((T)obj);
                }

                if (r == Result.Done || r == Result.OK)
                {
                }
                else
                {
                    var msg = SQLite3.GetErrorMessageUTF8(Connection.Handle);
                    var ex  = new SQLiteException(r, msg, sql: CommandText);
                    ex.PopulateColumnFromTableMapping(map);

                    throw ex;
                }
            }
            finally
            {
                if (Statement != null)
                {
                    SQLite3.Reset(Statement);
                }

                sw.Stop();
                Log(sw);
                OnExecutionEnded();
            }
        }