internal DqliteTransaction(DqliteConnection connection, IsolationLevel isolationLevel) { if (isolationLevel == IsolationLevel.ReadUncommitted || isolationLevel == IsolationLevel.ReadCommitted || isolationLevel == IsolationLevel.RepeatableRead) { isolationLevel = IsolationLevel.Serializable; } _connection = connection; _isolationLevel = isolationLevel; if (isolationLevel == IsolationLevel.ReadUncommitted) { _connection.Connector.ExecuteNonQuery(_connection.CurrentDatabase, "PRAGMA read_uncommitted = 1;"); } else if (isolationLevel == IsolationLevel.Serializable) { _connection.Connector.ExecuteNonQuery(_connection.CurrentDatabase, "PRAGMA read_uncommitted = 0;"); } else if (isolationLevel != IsolationLevel.Unspecified) { throw new ArgumentException(Resources.InvalidIsolationLevel(isolationLevel)); } _connection.Connector.ExecuteNonQuery(_connection.CurrentDatabase, IsolationLevel == IsolationLevel.Serializable ? "BEGIN IMMEDIATE;" : "BEGIN;"); }
public async Task Test1Async(int rows) { var builder = new DqliteConnectionStringBuilder() { Nodes = new [] { "127.0.0.1:6543" }, DataSource = "main" }; using (var connection = new DqliteConnection(builder.ToString())) { await connection.OpenAsync(); using (var command = new DqliteCommand() { Connection = connection }) { command.CommandText = "CREATE TABLE IF NOT EXISTS Sample(n INT);"; await command.ExecuteNonQueryAsync(); command.CommandText = "DELETE FROM Sample;"; await command.ExecuteNonQueryAsync(); command.CommandText = "INSERT INTO Sample(n) VALUES(@value);"; await command.PrepareAsync(); for (int i = 0; i < rows; ++i) { command.Parameters.AddWithValue("@value", i); await command.ExecuteNonQueryAsync(); command.Parameters.Clear(); } command.CommandText = "SELECT count(0) FROM Sample;"; await using (var reader = await command.ExecuteReaderAsync()) { Assert.True(await reader.ReadAsync()); Assert.Equal(1, reader.FieldCount); Assert.Equal(rows, reader.GetInt64(0)); Assert.False(await reader.ReadAsync()); } command.CommandText = "SELECT n FROM Sample;"; await using (var reader = await command.ExecuteReaderAsync()) { for (int i = 0; i < rows; ++i) { Assert.True(await reader.ReadAsync()); Assert.Equal(i, reader.GetInt64(0)); } Assert.False(await reader.ReadAsync()); } } } }
public static T ExecuteScalar <T>( this DqliteConnection connection, string commandText, params DqliteParameter[] parameters) { using (var command = connection.CreateCommand()) { command.CommandText = commandText; command.Parameters.AddRange(parameters); return((T)command.ExecuteScalar()); } }
public static int ExecuteNonQuery( this DqliteConnection connection, string commandText, params DqliteParameter[] parameters) { using (var command = connection.CreateCommand()) { command.CommandText = commandText; command.Parameters.AddRange(parameters); return(command.ExecuteNonQuery()); } }
public static T ExecuteScalar <T>( this DqliteConnection connection, string commandText, params DqliteParameter[] parameters) where T : IConvertible { using (var command = connection.CreateCommand()) { command.CommandText = commandText; command.Parameters.AddRange(parameters); return((T)Convert.ChangeType(command.ExecuteScalar(), typeof(T))); } }
public static async Task <T> ExecuteScalarAsync <T>( this DqliteConnection connection, string commandText, DqliteParameter[] parameters, CancellationToken cancellationToken = default(CancellationToken)) { using (var command = connection.CreateCommand()) { command.CommandText = commandText; command.Parameters.AddRange(parameters); return((T)await command.ExecuteScalarAsync()); } }
public DqliteCommand(string commandText, DqliteConnection connection, DqliteTransaction transaction) : this(commandText, connection) { this.Transaction = transaction; }
public DqliteCommand(string commandText, DqliteConnection connection) : this(commandText) { this.Connection = connection; this.CommandTimeout = connection.DefaultTimeout; }
private void Complete() { _connection.Transaction = null; _connection = null; _completed = true; }
public static Task <int> ExecuteNonQueryAsync( this DqliteConnection connection, string commandText, params DqliteParameter[] parameters) => ExecuteNonQueryAsync(connection, commandText, parameters, CancellationToken.None);
public static Task <T> ExecuteScalarAsync <T>( this DqliteConnection connection, string commandText, params DqliteParameter[] parameters) => ExecuteScalarAsync <T>(connection, commandText, parameters, CancellationToken.None);