use internal send and recv functions

This commit is contained in:
Manuel Kniep
2015-08-12 11:34:40 +02:00
parent 6f0fd01c13
commit 7cae260dcb
7 changed files with 49 additions and 74 deletions

View File

@@ -12,14 +12,10 @@ AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION base36_recv(internal)
RETURNS base36
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS base36 LANGUAGE internal IMMUTABLE AS 'int4recv';
CREATE FUNCTION base36_send(base36)
RETURNS bytea
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS bytea LANGUAGE internal IMMUTABLE AS 'int4send';
CREATE TYPE base36 (
INPUT = base36_in,
@@ -164,14 +160,10 @@ AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION bigbase36_recv(internal)
RETURNS bigbase36
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS bigbase36 LANGUAGE internal IMMUTABLE AS 'int8recv';
CREATE FUNCTION bigbase36_send(bigbase36)
RETURNS bytea
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS bytea LANGUAGE internal IMMUTABLE AS 'int8send';
CREATE TYPE bigbase36 (
INPUT = bigbase36_in,

41
spec/binary_copy_spec.rb Normal file
View File

@@ -0,0 +1,41 @@
require 'spec_helper'
describe 'base36 binary' do
before do
install_extension
end
it "should copy data binary from country" do
query("CREATE TABLE before (a base36)")
query("INSERT INTO before VALUES ('aa'), ('-aa'), ('zik0zj'), ('-zik0zj'), ('0')")
query("CREATE TABLE after (a base36)")
query("COPY before TO '/tmp/tst' WITH (FORMAT binary)")
query("COPY after FROM '/tmp/tst' WITH (FORMAT binary)")
query("SELECT * FROM after").should match \
['aa'],
['-aa'],
['zik0zj'],
['-zik0zj'],
['0']
end
end
describe 'bigbase36 binary' do
before do
install_extension
end
it "should copy data binary from country" do
query("CREATE TABLE before (a bigbase36)")
query("INSERT INTO before VALUES ('aa'), ('-aa'), ('1y2p0ij32e8e7'), ('-1y2p0ij32e8e7'), ('0')")
query("CREATE TABLE after (a bigbase36)")
query("COPY before TO '/tmp/tst' WITH (FORMAT binary)")
query("COPY after FROM '/tmp/tst' WITH (FORMAT binary)")
query("SELECT * FROM after").should match \
['aa'],
['-aa'],
['1y2p0ij32e8e7'],
['-1y2p0ij32e8e7'],
['0']
end
end

View File

@@ -9,14 +9,10 @@ AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION base36_recv(internal)
RETURNS base36
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS base36 LANGUAGE internal IMMUTABLE AS 'int4recv';
CREATE FUNCTION base36_send(base36)
RETURNS bytea
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS bytea LANGUAGE internal IMMUTABLE AS 'int4send';
CREATE TYPE base36 (
INPUT = base36_in,

View File

@@ -9,14 +9,10 @@ AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION bigbase36_recv(internal)
RETURNS bigbase36
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS bigbase36 LANGUAGE internal IMMUTABLE AS 'int8recv';
CREATE FUNCTION bigbase36_send(bigbase36)
RETURNS bytea
AS '$libdir/base36'
LANGUAGE C IMMUTABLE STRICT;
RETURNS bytea LANGUAGE internal IMMUTABLE AS 'int8send';
CREATE TYPE bigbase36 (
INPUT = bigbase36_in,

View File

@@ -102,29 +102,6 @@ base36_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(base36_to_str(c));
}
PG_FUNCTION_INFO_V1(base36_recv);
Datum
base36_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
const char *str = pq_getmsgstring(buf);
pq_getmsgend(buf);
PG_RETURN_INT32(base36_from_str(str));
}
PG_FUNCTION_INFO_V1(base36_send);
Datum
base36_send(PG_FUNCTION_ARGS)
{
base36 c = PG_GETARG_INT32(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendstring(&buf, base36_to_str(c));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
PG_FUNCTION_INFO_V1(base36_cast_from_text);
Datum
base36_cast_from_text(PG_FUNCTION_ARGS)

View File

@@ -18,8 +18,6 @@ static int base36_digits[36] =
Datum base36_in(PG_FUNCTION_ARGS);
Datum base36_out(PG_FUNCTION_ARGS);
Datum base36_recv(PG_FUNCTION_ARGS);
Datum base36_send(PG_FUNCTION_ARGS);
Datum base36_cast_to_text(PG_FUNCTION_ARGS);
Datum base36_cast_from_text(PG_FUNCTION_ARGS);
Datum base36_cast_to_bigint(PG_FUNCTION_ARGS);
@@ -27,8 +25,6 @@ Datum base36_cast_from_bigint(PG_FUNCTION_ARGS);
Datum bigbase36_in(PG_FUNCTION_ARGS);
Datum bigbase36_out(PG_FUNCTION_ARGS);
Datum bigbase36_recv(PG_FUNCTION_ARGS);
Datum bigbase36_send(PG_FUNCTION_ARGS);
Datum bigbase36_cast_to_text(PG_FUNCTION_ARGS);
Datum bigbase36_cast_from_text(PG_FUNCTION_ARGS);
Datum bigbase36_cast_to_bigint(PG_FUNCTION_ARGS);

View File

@@ -109,29 +109,6 @@ bigbase36_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(bigbase36_to_str(c));
}
PG_FUNCTION_INFO_V1(bigbase36_recv);
Datum
bigbase36_recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
const char *str = pq_getmsgstring(buf);
pq_getmsgend(buf);
PG_RETURN_INT64(bigbase36_from_str(str));
}
PG_FUNCTION_INFO_V1(bigbase36_send);
Datum
bigbase36_send(PG_FUNCTION_ARGS)
{
bigbase36 c = PG_GETARG_INT64(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendstring(&buf, bigbase36_to_str(c));
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
PG_FUNCTION_INFO_V1(bigbase36_cast_from_text);
Datum
bigbase36_cast_from_text(PG_FUNCTION_ARGS)