Skip to content

Commit feb4d49

Browse files
committed
Rename to QueryScalars for #812
1 parent ef080b7 commit feb4d49

File tree

3 files changed

+56
-37
lines changed

3 files changed

+56
-37
lines changed

src/SQLite.cs

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,27 @@ public T ExecuteScalar<T> (string query, params object[] args)
970970
var cmd = CreateCommand (query, args);
971971
return cmd.ExecuteQuery<T> ();
972972
}
973-
public List<T> SingleQuery<T> (string query, params object[] args)
973+
974+
/// <summary>
975+
/// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?'
976+
/// in the command text for each of the arguments and then executes that command.
977+
/// It returns the first column of each row of the result.
978+
/// </summary>
979+
/// <param name="query">
980+
/// The fully escaped SQL.
981+
/// </param>
982+
/// <param name="args">
983+
/// Arguments to substitute for the occurences of '?' in the query.
984+
/// </param>
985+
/// <returns>
986+
/// An enumerable with one result for the first column of each row returned by the query.
987+
/// </returns>
988+
public List<T> QueryScalars<T> (string query, params object[] args)
974989
{
975990
var cmd = CreateCommand (query, args);
976-
return cmd.ExcuteSingleQuery<T> ().ToList ();
991+
return cmd.ExecuteQueryScalars<T> ().ToList ();
977992
}
993+
978994
/// <summary>
979995
/// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?'
980996
/// in the command text for each of the arguments and then executes that command.
@@ -2849,31 +2865,6 @@ protected virtual void OnInstanceCreated (object obj)
28492865
{
28502866
// Can be overridden.
28512867
}
2852-
public IEnumerable<T> ExcuteSingleQuery<T> ()
2853-
{
2854-
if (_conn.Trace) {
2855-
_conn.Tracer?.Invoke ("Executing Query: " + this);
2856-
}
2857-
var stmt = Prepare ();
2858-
try {
2859-
if (SQLite3.ColumnCount (stmt) != 1) {
2860-
throw new NotSupportedException ("Column count error");
2861-
}
2862-
while (SQLite3.Step (stmt) == SQLite3.Result.Row) {
2863-
var colType = SQLite3.ColumnType (stmt, 0);
2864-
var val = ReadCol (stmt, 0, colType, typeof (T));
2865-
if (val == null) {
2866-
yield return default (T);
2867-
}
2868-
else {
2869-
yield return (T)val;
2870-
}
2871-
}
2872-
}
2873-
finally {
2874-
Finalize (stmt);
2875-
}
2876-
}
28772868

28782869
public IEnumerable<T> ExecuteDeferredQuery<T> (TableMapping map)
28792870
{
@@ -2940,6 +2931,32 @@ public T ExecuteScalar<T> ()
29402931
return val;
29412932
}
29422933

2934+
public IEnumerable<T> ExecuteQueryScalars<T> ()
2935+
{
2936+
if (_conn.Trace) {
2937+
_conn.Tracer?.Invoke ("Executing Query: " + this);
2938+
}
2939+
var stmt = Prepare ();
2940+
try {
2941+
if (SQLite3.ColumnCount (stmt) < 1) {
2942+
throw new InvalidOperationException ("QueryScalars should return at least one column");
2943+
}
2944+
while (SQLite3.Step (stmt) == SQLite3.Result.Row) {
2945+
var colType = SQLite3.ColumnType (stmt, 0);
2946+
var val = ReadCol (stmt, 0, colType, typeof (T));
2947+
if (val == null) {
2948+
yield return default (T);
2949+
}
2950+
else {
2951+
yield return (T)val;
2952+
}
2953+
}
2954+
}
2955+
finally {
2956+
Finalize (stmt);
2957+
}
2958+
}
2959+
29432960
public void Bind (string name, object val)
29442961
{
29452962
_bindings.Add (new Binding {

src/SQLiteAsync.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,17 +1070,18 @@ public Task<T> ExecuteScalarAsync<T> (string query, params object[] args)
10701070
/// Arguments to substitute for the occurences of '?' in the query.
10711071
/// </param>
10721072
/// <returns>
1073-
/// An enumerable with one result for each row returned by the query.
1073+
/// A list with one result for each row returned by the query.
10741074
/// </returns>
10751075
public Task<List<T>> QueryAsync<T> (string query, params object[] args)
10761076
where T : new()
10771077
{
10781078
return ReadAsync (conn => conn.Query<T> (query, args));
10791079
}
1080+
10801081
/// <summary>
10811082
/// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?'
10821083
/// in the command text for each of the arguments and then executes that command.
1083-
/// It returns single row of the result
1084+
/// It returns the first column of each row of the result.
10841085
/// </summary>
10851086
/// <param name="query">
10861087
/// The fully escaped SQL.
@@ -1089,12 +1090,13 @@ public Task<List<T>> QueryAsync<T> (string query, params object[] args)
10891090
/// Arguments to substitute for the occurences of '?' in the query.
10901091
/// </param>
10911092
/// <returns>
1092-
/// An enumerable with one result for single row returned by the query.
1093+
/// A list with one result for the first column of each row returned by the query.
10931094
/// </returns>
1094-
public Task<List<T>> SingleQueryAsync<T> (string query, params object[] args)
1095+
public Task<List<T>> QueryScalarsAsync<T> (string query, params object[] args)
10951096
{
1096-
return ReadAsync (conn => conn.SingleQuery<T> (query, args));
1097+
return ReadAsync (conn => conn.QueryScalars<T> (query, args));
10971098
}
1099+
10981100
/// <summary>
10991101
/// Creates a SQLiteCommand given the command text (SQL) with arguments. Place a '?'
11001102
/// in the command text for each of the arguments and then executes that command.

tests/AsyncTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public void TestSingleQueryAsync ()
474474
}
475475

476476
// return the third one...
477-
var task = conn.SingleQueryAsync<string> ("select Email from customer where id=?", customers[2].Id);
477+
var task = conn.QueryScalarsAsync<string> ("select Email from customer where id=?", customers[2].Id);
478478
task.Wait ();
479479
var loaded = task.Result;
480480

@@ -483,7 +483,7 @@ public void TestSingleQueryAsync ()
483483
Assert.AreEqual (customers[2].Email, loaded[0]);
484484

485485
// return the third one...
486-
var inttest = conn.SingleQueryAsync<int> ("select Id from customer where id=?", customers[2].Id);
486+
var inttest = conn.QueryScalarsAsync<int> ("select Id from customer where id=?", customers[2].Id);
487487
task.Wait ();
488488
var intloaded = inttest.Result;
489489

@@ -492,7 +492,7 @@ public void TestSingleQueryAsync ()
492492
Assert.AreEqual (customers[2].Id, intloaded[0]);
493493

494494
// return string list
495-
var listtask = conn.SingleQueryAsync<string> ("select Email from customer order by Id");
495+
var listtask = conn.QueryScalarsAsync<string> ("select Email from customer order by Id");
496496
listtask.Wait ();
497497
var listloaded = listtask.Result;
498498

@@ -501,8 +501,8 @@ public void TestSingleQueryAsync ()
501501
Assert.AreEqual (customers[2].Email, listloaded[2]);
502502

503503
// select columns
504-
var columnstask= conn.SingleQueryAsync<string> ("select * from customer");
505-
ExceptionAssert.Throws<AggregateException> (() => columnstask.Wait ());
504+
var columnstask= conn.QueryScalarsAsync<string> ("select FirstName, LastName from customer");
505+
Assert.AreEqual (5, columnstask.Result.Count);
506506
}
507507
[Test]
508508
public void TestTableAsync ()

0 commit comments

Comments
 (0)