diff --git a/src/SQLite.cs b/src/SQLite.cs index 72525c56..fc7cc845 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -1411,7 +1411,7 @@ public List Query< var cmd = CreateCommand (query, args); return cmd.ExecuteQuery (); } - + /// /// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?' /// in the command text for each of the arguments and then executes that command. @@ -1460,6 +1460,49 @@ public IEnumerable DeferredQuery< return cmd.ExecuteDeferredQuery (); } + + /// + /// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?' + /// in the command text for each of the arguments and then executes that command. + /// It returns each row of the result a dictionary + /// + /// + /// The fully escaped SQL. + /// + /// + /// Arguments to substitute for the occurences of '?' in the query. + /// + /// + /// An enumerable with one result for each row returned by the query as dictionary + /// + public List> Query(string query, params object[] args) + { + var cmd = CreateCommand(query, args); + return cmd.ExecuteDeferredQuery().ToList(); + } + + + /// + /// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?' + /// in the command text for each of the arguments and then executes that command. + /// It returns each row of the result a dictionary + /// + /// + /// The fully escaped SQL. + /// + /// + /// Arguments to substitute for the occurences of '?' in the query. + /// + /// + /// An enumerable with one result for each row returned by the query as dictionary + /// + public IEnumerable> ExecuteDeferredQuery(string query, params object[] args) + { + var cmd = CreateCommand(query, args); + return cmd.ExecuteDeferredQuery(); + + } + /// /// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?' /// in the command text for each of the arguments and then executes that command. @@ -3609,6 +3652,61 @@ public IEnumerable ExecuteDeferredQuery (TableMapping map) } } + public IEnumerable> ExecuteDeferredQuery() + { + if (_conn.Trace) + { + Debug.WriteLine("Executing Query: " + this); + } + + var stmt = Prepare(); + try + { + var cols = new string[SQLite3.ColumnCount(stmt)]; + + for (int i = 0; i < cols.Length; i++) + { + var name = SQLite3.ColumnName16(stmt, i); + cols[i] = name; + } + + while (SQLite3.Step(stmt) == SQLite3.Result.Row) + { + var obj = new Dictionary(); + for (int i = 0; i < cols.Length; i++) + { + var colType = SQLite3.ColumnType(stmt, i); + + Type targetType; + switch (colType) + { + case SQLite3.ColType.Text: + targetType = typeof(string); + break; + case SQLite3.ColType.Integer: + targetType = typeof(int); + break; + case SQLite3.ColType.Float: + targetType = typeof(double); + break; + default: + targetType = typeof(object); + break; + } + + var val = ReadCol(stmt, i, colType, targetType); + obj.Add(cols[i], val); + } + OnInstanceCreated(obj); + yield return obj; + } + } + finally + { + SQLite3.Finalize(stmt); + } + } + public T ExecuteScalar () { if (_conn.Trace) { diff --git a/tests/SQLite.Tests/QueryTest.cs b/tests/SQLite.Tests/QueryTest.cs index 9bd6c762..85dcfc16 100644 --- a/tests/SQLite.Tests/QueryTest.cs +++ b/tests/SQLite.Tests/QueryTest.cs @@ -24,14 +24,14 @@ private readonly (int Value, double Walue)[] _records = new[] (42, 0.5) }; - public QueryTest () + public QueryTest () { _db.Execute ("create table G(Value integer not null, Walue real not null)"); for (int i = 0; i < _records.Length; i++) { _db.Execute ("insert into G(Value, Walue) values (?, ?)", - _records[i].Value, _records[i].Walue); - } + _records[i].Value, _records[i].Walue); + } } class GenericObject @@ -48,20 +48,38 @@ public void QueryGenericObject () Assert.AreEqual (_records.Length, r.Count); Assert.AreEqual (_records[0].Value, r[0].Value); Assert.AreEqual (_records[0].Walue, r[0].Walue); - } - + } + + + [Test] + public void QueryGenericObjectAsDictionary () + { + var path = Path.GetTempFileName (); + var db = new SQLiteConnection (path, true); + + db.Execute ("create table G(Value integer not null)"); + db.Execute ("insert into G(Value) values (?)", 42); + db.Execute ("insert into G(Value) values (?)", 43); + db.Execute ("insert into G(Value) values (?)", 44); + var r = db.Query ("select * from G where value > ?", 42); + + Assert.AreEqual (2, r.Count); + Assert.AreEqual (43, r[0]["Value"]); + Assert.AreEqual (44, r[1]["Value"]); + } + #region Issue #1007 [Test] - public void QueryValueTuple() + public void QueryValueTuple() { var r = _db.Query<(int Value, double Walue)> ("select * from G"); Assert.AreEqual(_records.Length, r.Count); Assert.AreEqual(_records[0].Value, r[0].Value); - Assert.AreEqual(_records[0].Walue, r[0].Walue); + Assert.AreEqual(_records[0].Walue, r[0].Walue); } - + #endregion } }