Skip to content

Commit b4fdef5

Browse files
committed
Implement parameter count
1 parent 7045a12 commit b4fdef5

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

Libsql.Client.Tests/StatementTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ public async Task Statement_CanBind_NNNIndex()
9292
Assert.Equal(expected, text.Value);
9393
}
9494

95+
[Theory]
96+
[InlineData("SELECT 1", 0)]
97+
[InlineData("SELECT ?", 1)]
98+
[InlineData("SELECT ?, ?", 2)]
99+
[InlineData("SELECT ?, ?, ?", 3)]
100+
[InlineData("SELECT ?10, ?11, ?33", 33)]
101+
[InlineData("SELECT ?10, ?33, ?11", 33)]
102+
[InlineData("SELECT ?, ?, ?2", 2)]
103+
[InlineData("SELECT ?, ?, ?2, ?, ?", 4)]
104+
public async Task Statement_CanGetParameterCount(string query, int expected)
105+
{
106+
using var statement = await _db.Prepare(query);
107+
108+
var parameterCount = statement.ParameterCount;
109+
110+
Assert.Equal(expected, parameterCount);
111+
}
112+
95113
[Fact]
96114
public async Task Statement_BoundValuesCount_IsCorrect()
97115
{

Libsql.Client/IStatement.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public interface IStatement : IDisposable
1313
/// </summary>
1414
int BoundValuesCount { get; }
1515

16+
/// <summary>
17+
/// Gets the number of parameters in the prepared statement.
18+
/// </summary>
19+
/// <remarks>
20+
/// If <c>?NNN</c> syntax is used, the Parameter count is the maximum value of
21+
/// <c>NNN</c> used plus any unannotated positional parameters to the right.
22+
/// </remarks>
23+
int ParameterCount { get; }
24+
1625
/// <summary>
1726
/// Binds a value to the prepared statement.
1827
/// </summary>

Libsql.Client/StatementWrapper.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ namespace Libsql.Client
88
internal class StatementWrapper : IStatement
99
{
1010
int IStatement.BoundValuesCount => _bindIndex - 1;
11+
12+
public int ParameterCount => GetParameterCount();
13+
1114
private int _bindIndex = 1;
1215
public readonly libsql_stmt_t Stmt;
1316
private readonly libsql_connection_t _connection;
@@ -120,6 +123,17 @@ private unsafe void Bind(int index)
120123
_bindIndex++;
121124
}
122125

126+
private unsafe int GetParameterCount()
127+
{
128+
var error = new Error();
129+
int count;
130+
var exitCode = Bindings.libsql_stmt_parameter_count(Stmt, &count,&error.Ptr);
131+
132+
error.ThrowIfNonZero(exitCode, $"Failed to get parameter count");
133+
134+
return count;
135+
}
136+
123137
public void Bind(Integer integer, int index)
124138
{
125139
BindInt(integer.Value, index);

0 commit comments

Comments
 (0)