Subject | Converting BLOB to long VARCHAR |
---|---|
Author | Josef Kokeš |
Post date | 2011-12-28T10:21:34Z |
Hi!
I am trying to convert a BLOB (unlimited size, theoretically) to a long
VARCHAR (say, VARCHAR(8192)). This should be easy enough to do:
DECLARE VARIABLE b BLOB; /* input */
DECLARE VARIABLE v VARCHAR(8192); /* output */
...
v = SUBSTRING(b, 1, 8192);
Except that as soon as I convert between BLOB and VARCHAR, I lose all
high-ASCII characters (they get replaced by a dot). E.g., to expand the
example above:
DECLARE VARIABLE b BLOB; /* input */
DECLARE VARIABLE b2 BLOB; /* output */
DECLARE VARIABLE v VARCHAR(8192); /* output */
...
v = SUBSTRING(b, 1, 8192);
b2 = SUBSTRING(b, 1, 8192);
When this code is run, both b2 and v contain the first 8192 characters
from the input - but in v's case, the high-ascii characters get
converted to a dot.
Supposedly this should be done using a UDF, but I just can't figure out
how to write one (in Delphi):
function BLOBSTR(Value: PBlob; Size: Longint; Dest: PAnsiChar):
PAnsiChar; cdecl;
begin
Result := Dest;
FillChar(Result^, Size, 'a');
end;
(I decided to remove the BLOB-reading code to make sure I have no errors
in my parameter-passing code.)
But with a declaration:
DECLARE EXTERNAL FUNCTION BLOBSTR
BLOB, INTEGER, CSTRING(8192)
RETURNS PARAMETER 3
ENTRY_POINT 'BLOBSTR' MODULE_NAME 'pwIBUDF';
and a select:
SELECT BLOBSTR(blobfield, 10) FROM sometable WHERE id=1
I get an empty (zero-byte, NOT NULL) result. I would expect to get
either a 10-byte result (if the buffer for parameter 3 gets filled with
zeroes before the UDF is called) or something between 10 and 8192 byte
result (if the buffer does not get initialized), but zero has me stumped.
I would appreciate any pointers.
Thanks,
Pepak
I am trying to convert a BLOB (unlimited size, theoretically) to a long
VARCHAR (say, VARCHAR(8192)). This should be easy enough to do:
DECLARE VARIABLE b BLOB; /* input */
DECLARE VARIABLE v VARCHAR(8192); /* output */
...
v = SUBSTRING(b, 1, 8192);
Except that as soon as I convert between BLOB and VARCHAR, I lose all
high-ASCII characters (they get replaced by a dot). E.g., to expand the
example above:
DECLARE VARIABLE b BLOB; /* input */
DECLARE VARIABLE b2 BLOB; /* output */
DECLARE VARIABLE v VARCHAR(8192); /* output */
...
v = SUBSTRING(b, 1, 8192);
b2 = SUBSTRING(b, 1, 8192);
When this code is run, both b2 and v contain the first 8192 characters
from the input - but in v's case, the high-ascii characters get
converted to a dot.
Supposedly this should be done using a UDF, but I just can't figure out
how to write one (in Delphi):
function BLOBSTR(Value: PBlob; Size: Longint; Dest: PAnsiChar):
PAnsiChar; cdecl;
begin
Result := Dest;
FillChar(Result^, Size, 'a');
end;
(I decided to remove the BLOB-reading code to make sure I have no errors
in my parameter-passing code.)
But with a declaration:
DECLARE EXTERNAL FUNCTION BLOBSTR
BLOB, INTEGER, CSTRING(8192)
RETURNS PARAMETER 3
ENTRY_POINT 'BLOBSTR' MODULE_NAME 'pwIBUDF';
and a select:
SELECT BLOBSTR(blobfield, 10) FROM sometable WHERE id=1
I get an empty (zero-byte, NOT NULL) result. I would expect to get
either a 10-byte result (if the buffer for parameter 3 gets filled with
zeroes before the UDF is called) or something between 10 and 8192 byte
result (if the buffer does not get initialized), but zero has me stumped.
I would appreciate any pointers.
Thanks,
Pepak