Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static T ExecuteScalar<T>(
this SqliteConnection connection,
string commandText,
params SqliteParameter[] parameters)
=> (T)connection.ExecuteScalar(commandText, parameters);
=> (T)connection.ExecuteScalar(commandText, parameters)!;
Comment thread
smitpatel marked this conversation as resolved.

private static object ExecuteScalar(
private static object? ExecuteScalar(
this SqliteConnection connection,
string commandText,
params SqliteParameter[] parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ Microsoft.Data.Sqlite.SqliteException
Microsoft.Data.Sqlite.SqliteFactory
Microsoft.Data.Sqlite.SqliteParameter
Microsoft.Data.Sqlite.SqliteTransaction</Description>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
<TargetFramework>net5.0</TargetFramework>
Comment thread
bricelam marked this conversation as resolved.
<MinClientVersion>3.6</MinClientVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>Microsoft.Data.Sqlite.Core.ruleset</CodeAnalysisRuleSet>
<PackageTags>SQLite;Data;ADO.NET</PackageTags>
<PackageProjectUrl>https://docs.microsoft.com/dotnet/standard/data/sqlite/</PackageProjectUrl>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
18 changes: 3 additions & 15 deletions src/Microsoft.Data.Sqlite.Core/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions src/Microsoft.Data.Sqlite.Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@
<data name="CallRequiresOpenConnection" xml:space="preserve">
<value>{methodName} can only be called when the connection is open.</value>
</data>
<data name="CallRequiresSetCommandText" xml:space="preserve">
<value>CommandText must be set before {methodName} can be called.</value>
</data>
<data name="ConnectionStringRequiresClosedConnection" xml:space="preserve">
<value>ConnectionString cannot be set when the connection is open.</value>
</data>
Expand All @@ -147,9 +144,6 @@
<data name="NoData" xml:space="preserve">
<value>No data exists for the row/column.</value>
</data>
<data name="OpenRequiresSetConnectionString" xml:space="preserve">
<value>ConnectionString must be set before Open can be called.</value>
</data>
<data name="ParallelTransactionsNotSupported" xml:space="preserve">
<value>SqliteConnection does not support nested transactions.</value>
</data>
Expand Down
8 changes: 4 additions & 4 deletions src/Microsoft.Data.Sqlite.Core/SqliteBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Data.Sqlite
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/blob-io">BLOB I/O</seealso>
public class SqliteBlob : Stream
{
private sqlite3_blob _blob;
private sqlite3_blob? _blob;
private readonly sqlite3 _db;
private long _position;

Expand Down Expand Up @@ -62,17 +62,17 @@ public SqliteBlob(
throw new InvalidOperationException(Resources.SqlBlobRequiresOpenConnection);
}

if (string.IsNullOrEmpty(tableName))
if (tableName is null)
Comment thread
smitpatel marked this conversation as resolved.
{
throw new ArgumentNullException(nameof(tableName));
}

if (string.IsNullOrEmpty(columnName))
if (columnName is null)
{
throw new ArgumentNullException(nameof(columnName));
}

_db = connection.Handle;
_db = connection.Handle!;
CanWrite = !readOnly;
var rc = sqlite3_blob_open(
_db,
Expand Down
62 changes: 24 additions & 38 deletions src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -23,12 +24,13 @@ namespace Microsoft.Data.Sqlite
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/async">Async Limitations</seealso>
public class SqliteCommand : DbCommand
{
private SqliteParameterCollection _parameters;
private SqliteParameterCollection? _parameters;

private readonly List<sqlite3_stmt> _preparedStatements = new List<sqlite3_stmt>();
private SqliteConnection _connection;
private SqliteConnection? _connection;
private string _commandText = string.Empty;
private bool _prepared;
private int? _commandTimeout;

/// <summary>
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
Expand All @@ -41,19 +43,18 @@ public SqliteCommand()
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
/// </summary>
/// <param name="commandText">The SQL to execute against the database.</param>
public SqliteCommand(string commandText)
public SqliteCommand(string? commandText)
=> CommandText = commandText;

/// <summary>
/// Initializes a new instance of the <see cref="SqliteCommand" /> class.
/// </summary>
/// <param name="commandText">The SQL to execute against the database.</param>
/// <param name="connection">The connection used by the command.</param>
public SqliteCommand(string commandText, SqliteConnection connection)
public SqliteCommand(string? commandText, SqliteConnection? connection)
: this(commandText)
{
Connection = connection;
CommandTimeout = connection.DefaultTimeout;
}

/// <summary>
Expand All @@ -62,7 +63,7 @@ public SqliteCommand(string commandText, SqliteConnection connection)
/// <param name="commandText">The SQL to execute against the database.</param>
/// <param name="connection">The connection used by the command.</param>
/// <param name="transaction">The transaction within which the command executes.</param>
public SqliteCommand(string commandText, SqliteConnection connection, SqliteTransaction transaction)
public SqliteCommand(string? commandText, SqliteConnection? connection, SqliteTransaction? transaction)
: this(commandText, connection)
=> Transaction = transaction;

Expand All @@ -88,6 +89,7 @@ public override CommandType CommandType
/// </summary>
/// <value>The SQL to execute against the database.</value>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/batching">Batching</seealso>
[AllowNull]
public override string CommandText
{
get => _commandText;
Expand All @@ -110,7 +112,7 @@ public override string CommandText
/// Gets or sets the connection used by the command.
/// </summary>
/// <value>The connection used by the command.</value>
public new virtual SqliteConnection Connection
public new virtual SqliteConnection? Connection
{
get => _connection;
set
Expand All @@ -135,26 +137,26 @@ public override string CommandText
/// Gets or sets the connection used by the command. Must be a <see cref="SqliteConnection" />.
/// </summary>
/// <value>The connection used by the command.</value>
protected override DbConnection DbConnection
protected override DbConnection? DbConnection
{
get => Connection;
set => Connection = (SqliteConnection)value;
set => Connection = (SqliteConnection?)value;
}

/// <summary>
/// Gets or sets the transaction within which the command executes.
/// </summary>
/// <value>The transaction within which the command executes.</value>
public new virtual SqliteTransaction Transaction { get; set; }
public new virtual SqliteTransaction? Transaction { get; set; }

/// <summary>
/// Gets or sets the transaction within which the command executes. Must be a <see cref="SqliteTransaction" />.
/// </summary>
/// <value>The transaction within which the command executes.</value>
protected override DbTransaction DbTransaction
protected override DbTransaction? DbTransaction
{
get => Transaction;
set => Transaction = (SqliteTransaction)value;
set => Transaction = (SqliteTransaction?)value;
}

/// <summary>
Expand All @@ -181,7 +183,11 @@ protected override DbParameterCollection DbParameterCollection
/// The timeout is used when the command is waiting to obtain a lock on the table.
/// </remarks>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public override int CommandTimeout { get; set; } = 30;
public override int CommandTimeout
{
get => _commandTimeout ?? _connection?.DefaultTimeout ?? 30;
set => _commandTimeout = value;
}

/// <summary>
/// Gets or sets a value indicating whether the command should be visible in an interface control.
Expand All @@ -199,7 +205,7 @@ protected override DbParameterCollection DbParameterCollection
/// Gets or sets the data reader currently being used by the command, or null if none.
/// </summary>
/// <value>The data reader currently being used by the command.</value>
protected internal virtual SqliteDataReader DataReader { get; set; }
protected internal virtual SqliteDataReader? DataReader { get; set; }

/// <summary>
/// Releases any resources used by the connection and closes it.
Expand Down Expand Up @@ -244,11 +250,6 @@ public override void Prepare()
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(Prepare)));
}

if (string.IsNullOrEmpty(_commandText))
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(Prepare)));
}

if (_prepared)
{
return;
Expand Down Expand Up @@ -292,11 +293,6 @@ public override void Prepare()
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(ExecuteReader)));
}

if (string.IsNullOrEmpty(_commandText))
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(ExecuteReader)));
}

if (Transaction != _connection.Transaction)
{
throw new InvalidOperationException(
Expand Down Expand Up @@ -435,11 +431,6 @@ public override int ExecuteNonQuery()
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(ExecuteNonQuery)));
}

if (string.IsNullOrEmpty(_commandText))
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(ExecuteNonQuery)));
}

var reader = ExecuteReader();
reader.Dispose();

Expand All @@ -452,18 +443,13 @@ public override int ExecuteNonQuery()
/// <returns>The first column of the first row of the results, or null if no results.</returns>
/// <exception cref="SqliteException">A SQLite error occurs during execution.</exception>
/// <seealso href="https://docs.microsoft.com/dotnet/standard/data/sqlite/database-errors">Database Errors</seealso>
public override object ExecuteScalar()
public override object? ExecuteScalar()
{
if (_connection?.State != ConnectionState.Open)
{
throw new InvalidOperationException(Resources.CallRequiresOpenConnection(nameof(ExecuteScalar)));
}

if (string.IsNullOrEmpty(_commandText))
{
throw new InvalidOperationException(Resources.CallRequiresSetCommandText(nameof(ExecuteScalar)));
}

using var reader = ExecuteReader();
return reader.Read()
? reader.GetValue(0)
Expand All @@ -489,7 +475,7 @@ private IEnumerable<sqlite3_stmt> PrepareAndEnumerateStatements(Stopwatch timer)
timer.Start();

string nextTail;
while (IsBusy(rc = sqlite3_prepare_v2(_connection.Handle, tail, out stmt, out nextTail)))
while (IsBusy(rc = sqlite3_prepare_v2(_connection!.Handle, tail, out stmt, out nextTail)))
{
if (CommandTimeout != 0
&& timer.ElapsedMilliseconds >= CommandTimeout * 1000L)
Expand All @@ -508,7 +494,7 @@ private IEnumerable<sqlite3_stmt> PrepareAndEnumerateStatements(Stopwatch timer)
// Statement was empty, white space, or a comment
if (stmt.IsInvalid)
{
if (!string.IsNullOrEmpty(tail))
if (tail.Length != 0)
{
continue;
}
Expand All @@ -520,7 +506,7 @@ private IEnumerable<sqlite3_stmt> PrepareAndEnumerateStatements(Stopwatch timer)

yield return stmt;
}
while (!string.IsNullOrEmpty(tail));
while (tail.Length != 0);

_prepared = true;
}
Expand Down
Loading