private static T ExecuteCommand <T>(
            SpannerConnection connection, Func <DbCommand, T> execute, string sql, bool useTransaction,
            object[] parameters)
        {
            if (connection.State != ConnectionState.Closed)
            {
                connection.Close();
            }
            connection.Open();
            try
            {
                using (var transaction = useTransaction ? connection.BeginTransaction() : null)
                {
                    T result;
                    using (var command = CreateCommand(connection, sql, parameters))
                    {
                        command.Transaction = transaction;
                        result = execute(command);
                    }
                    transaction?.Commit();

                    return(result);
                }
            }
            finally
            {
                if (connection.State == ConnectionState.Closed &&
                    connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }
                //To debug the parse error, set SkipToLine= this value.
                Trace.WriteLine($"Error encountered at statement {s_gStatements}");
            }
        }
Beispiel #2
0
        public void OpenWithNoDatabase_InvalidCredentials()
        {
            var builder = new SpannerConnectionStringBuilder
            {
                DataSource     = "projects/project_id/instances/instance_id",
                CredentialFile = "this_will_not_exist.json"
            };

            using (var connection = new SpannerConnection(builder))
            {
                Assert.Throws <FileNotFoundException>(() => connection.Open());
            }
        }
    public void TestCreateBackup()
    {
        string connectionString = $"Data Source=projects/{_spannerFixture.ProjectId}/instances/{_spannerFixture.InstanceId}/databases/{_spannerFixture.BackupDatabaseId}";

        using var connection = new SpannerConnection(connectionString);
        connection.Open();
        var versionTime = (DateTime)connection.CreateSelectCommand("SELECT CURRENT_TIMESTAMP").ExecuteScalar();

        CreateBackupSample createBackupSample = new CreateBackupSample();
        // Backup already exists since it was created in the test setup so it should throw an exception.
        var exception = Assert.Throws <RpcException>(()
                                                     => createBackupSample.CreateBackup(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.BackupDatabaseId, _spannerFixture.BackupId, versionTime));

        Assert.Equal(StatusCode.AlreadyExists, exception.StatusCode);
    }
 public override void OpenConnection() => _connection.Open();