示例#1
0
        public void CancelRawBinaryImport()
        {
            using (var conn = new NpgsqlConnection(ConnectionString))
            {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                var garbage = new byte[] { 1, 2, 3, 4 };
                using (var s = conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY"))
                {
                    s.Write(garbage, 0, garbage.Length);
                    s.Cancel();
                }

                Assert.That(ExecuteScalar("SELECT COUNT(*) FROM data", conn), Is.EqualTo(0));
            }
        }
示例#2
0
        public static Exception Copy(string sourceQuery, string destinationTableName, string sourceConnectionString, string destinationConnectionString, int timeoutMilliseconds = 100)
        {
            try
            {
                using (var sourceConnection = new NpgsqlConnection(sourceConnectionString))
                    using (var destinationConnection = new NpgsqlConnection(destinationConnectionString))
                    {
                        sourceConnection.Open();
                        destinationConnection.Open();

                        using (var inStream = sourceConnection.BeginRawBinaryCopy($"COPY ({sourceQuery}) TO STDOUT (FORMAT BINARY)"))
                            using (var outStream = destinationConnection.BeginRawBinaryCopy($"COPY {destinationTableName} FROM STDIN (FORMAT BINARY)"))
                            {
                                var buffer    = new byte[BUFFER_SIZE_BYTES];
                                var tryNumber = 0;

                                while (tryNumber <= MAX_READ_TRY_COUNT)
                                {
                                    tryNumber++;
                                    int readLength;
                                    while ((readLength = inStream.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        outStream.Write(buffer, 0, readLength);
                                        tryNumber = 0;
                                    }

                                    Thread.Sleep(timeoutMilliseconds);
                                }
                            }

                        sourceConnection.Close();
                        destinationConnection.Close();
                    }

                return(null);
            }
            catch (Exception exception)
            {
                QueryHandler.OnErrorExecute(exception);
                return(exception);
            }
        }
示例#3
0
        public static Exception ReadBinary(string query, string conn_string, out byte[] result, int timeoutMilliseconds = 100)
        {
            try
            {
                using (var sourceConnection = new NpgsqlConnection(conn_string))
                {
                    sourceConnection.Open();

                    using (var memoryStream = new MemoryStream())
                    {
                        using (var inStream = sourceConnection.BeginRawBinaryCopy($"COPY ({query}) TO STDOUT (FORMAT BINARY)"))
                        {
                            var buffer    = new byte[BUFFER_SIZE_BYTES];
                            var tryNumber = 0;

                            while (tryNumber <= MAX_READ_TRY_COUNT)
                            {
                                tryNumber++;
                                int readLength;
                                while ((readLength = inStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    memoryStream.Write(buffer, 0, readLength);
                                    tryNumber = 0;
                                }

                                Thread.Sleep(timeoutMilliseconds);
                            }
                        }

                        sourceConnection.Close();
                        result = memoryStream.ToArray();
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                result = null;
                QueryHandler.OnErrorExecute(ex);
                return(ex);
            }
        }
示例#4
0
        public void CloseDuringCopy()
        {
            // TODO: Check no broken connections were returned to the pool
            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginBinaryImport("COPY data (field_text, field_int4) FROM STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginBinaryExport("COPY data (field_text, field_int2) TO STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) TO STDIN BINARY");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
            }

            using (var conn = new NpgsqlConnection(ConnectionString)) {
                conn.Open();
                ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
                conn.BeginTextExport("COPY data (field_text, field_int4) TO STDIN");
            }
        }