Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion src/SQLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ public List<T> Query<
var cmd = CreateCommand (query, args);
return cmd.ExecuteQuery<T> ();
}

/// <summary>
/// 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.
Expand Down Expand Up @@ -1460,6 +1460,49 @@ public IEnumerable<T> DeferredQuery<
return cmd.ExecuteDeferredQuery<T> ();
}


/// <summary>
/// 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
/// </summary>
/// <param name="query">
/// The fully escaped SQL.
/// </param>
/// <param name="args">
/// Arguments to substitute for the occurences of '?' in the query.
/// </param>
/// <returns>
/// An enumerable with one result for each row returned by the query as dictionary
/// </returns>
public List<Dictionary<string, object>> Query(string query, params object[] args)
{
var cmd = CreateCommand(query, args);
return cmd.ExecuteDeferredQuery().ToList();
}


/// <summary>
/// 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
/// </summary>
/// <param name="query">
/// The fully escaped SQL.
/// </param>
/// <param name="args">
/// Arguments to substitute for the occurences of '?' in the query.
/// </param>
/// <returns>
/// An enumerable with one result for each row returned by the query as dictionary
/// </returns>
public IEnumerable<Dictionary<string, object>> ExecuteDeferredQuery(string query, params object[] args)
{
var cmd = CreateCommand(query, args);
return cmd.ExecuteDeferredQuery();

}

/// <summary>
/// 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.
Expand Down Expand Up @@ -3609,6 +3652,61 @@ public IEnumerable<T> ExecuteDeferredQuery<T> (TableMapping map)
}
}

public IEnumerable<Dictionary<string, object>> 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<string, object>();
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<T> ()
{
if (_conn.Trace) {
Expand Down
34 changes: 26 additions & 8 deletions tests/SQLite.Tests/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}