Functions | |
int | sqlo_alloc_lob_desc (sqlo_db_handle_t dbh, sqlo_lob_desc_t *loblpp) |
Allocate a lob descriptor. | |
int | sqlo_free_lob_desc (sqlo_db_handle_t dbh, sqlo_lob_desc_t *loblpp) |
Free a lob descriptor. | |
int | sqlo_lob_write_buffer (sqlo_db_handle_t dbh, sqlo_lob_desc_t loblp, unsigned int loblen, void *bufp, unsigned int bufl, unsigned int piece) |
Write lob data from buffer into the lob column. | |
int | sqlo_lob_append_buffer (sqlo_db_handle_t dbh, sqlo_lob_desc_t loblp, unsigned int loblen, void *bufp, unsigned int bufl, unsigned int piece) |
Append lob data from buffer to the lob column. | |
int | sqlo_lob_write_stream (sqlo_db_handle_t dbh, sqlo_lob_desc_t loblp, unsigned int filelen, FILE *fp) |
Write lob data from a file into the lob column. | |
int | sqlo_lob_get_length (sqlo_db_handle_t dbh, sqlo_lob_desc_t loblp, unsigned int *loblenp) |
Get the length of a lob. | |
int | sqlo_lob_read_buffer (sqlo_db_handle_t dbh, sqlo_lob_desc_t loblp, unsigned int loblen, void *bufp, unsigned int bufl) |
Read lob data from lob column into a buffer. | |
int | sqlo_lob_read_stream (sqlo_db_handle_t dbh, sqlo_lob_desc_t loblp, unsigned int loblen, FILE *fp) |
Read lob data from lob column into a stream. |
int sqlo_alloc_lob_desc | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t * | loblpp | |||
) |
int sqlo_free_lob_desc | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t * | loblpp | |||
) |
int sqlo_lob_append_buffer | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t | loblp, | |||
unsigned int | loblen, | |||
void * | bufp, | |||
unsigned int | bufl, | |||
unsigned int | piece | |||
) |
Append lob data from buffer to the lob column.
dbh | I - A database handle. | |
loblen | I - The length of the lob. | |
loblp | I - A lob locator. | |
bufp | I - A buffer of data. | |
bufl | I - The length of the buffer in terms of bytes. | |
piece | I - The piece indicator
|
/* $Id: ex13.c 286 2004-06-15 10:15:52Z kpoitschke $ */ #include <stdio.h> #include <stdlib.h> #include "examples.h" int insert_into_blob_table(sqlo_db_handle_t dbh, int key ) { char * stmt = "INSERT INTO T_SQLORA_BLOB (KEY, CDATA) " "VALUES (:b1, EMPTY_CLOB()) RETURNING CDATA INTO :b2"; char data[MAX_BLOB_BUFFER_DATA]; /* a data buffer */ sqlo_lob_desc_t loblp; /* the lob locator */ sqlo_stmt_handle_t sth; int status; int k = key; printf("Insert CLOB\n"); /* create the test table */ create_blob_table(dbh); /* fill the data buffer with some characters */ fillbuf(data, MAX_BLOB_BUFFER_DATA); /* parse the statement */ if (0>(sth = sqlo_prepare(dbh, stmt))) error_exit(dbh, "sqlo_prepare"); /* alloate the lob descriptor */ if (0 > sqlo_alloc_lob_desc(dbh, &loblp)) error_exit(dbh, "sqlo_alloc_lob_desc"); /* bind input variables. Note: we bind the lob descriptor here */ if (SQLO_SUCCESS != (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(int), NULL, 0)) || (sqlo_bind_by_pos(sth, 2, SQLOT_CLOB, &loblp, 0, NULL, 0)) ) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_bind_by_pos"); } /* execute the statement */ status = sqlo_execute(sth, 1); if (SQLO_SUCCESS != status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_execute"); } /* write the lob */ status = sqlo_lob_write_buffer(dbh, loblp, MAX_BLOB_BUFFER_DATA, data, MAX_BLOB_BUFFER_DATA, SQLO_ONE_PIECE); if (status < 0) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_log_write_buffer"); } sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); return (1); } void create_blob_table(sqlo_db_handle_t dbh) { char * stmt= "CREATE TABLE T_SQLORA_BLOB (KEY INTEGER, CDATA CLOB, BDATA BLOB)"; if (SQLO_SUCCESS != sqlo_exec(dbh, stmt)) if (sqlo_geterrcode(dbh) != 955) /* table exists already */ error_exit(dbh, "sqlo_exec"); } void drop_blob_table(sqlo_db_handle_t dbh) { char * stmt = "DROP TABLE T_SQLORA_BLOB"; if (SQLO_SUCCESS != sqlo_exec(dbh, stmt)) error_exit(dbh, "sqlo_exec"); } void fillbuf(char * data, int len) { int i; for (i = 0; i < len; ++i) { data[i] = 'A' + i % 26; } } /* $Id: ex13.c 286 2004-06-15 10:15:52Z kpoitschke $ */
int sqlo_lob_get_length | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t | loblp, | |||
unsigned int * | loblenp | |||
) |
int sqlo_lob_read_buffer | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t | loblp, | |||
unsigned int | loblen, | |||
void * | bufp, | |||
unsigned int | bufl | |||
) |
Read lob data from lob column into a buffer.
Reads data from the lob and writes it into the supplied buffer.
Use sqlo_lob_get_length to get the loblen you have to use here.
dbh | I - A database handle. | |
loblp | I - A lob locator. | |
loblen | I - The length of the lob | |
bufp | O - The output data. | |
bufl | I - The capacity of the buffer in terms of bytes. |
/* $Id: ex15.c 286 2004-06-15 10:15:52Z kpoitschke $ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "examples.h" int select_from_blob_table(sqlo_db_handle_t dbh, int key ) { char * stmt = "SELECT KEY, CDATA FROM T_SQLORA_BLOB WHERE KEY = :1"; int status; sqlo_stmt_handle_t sth = SQLO_STH_INIT; int k = key; char * data; short ind; char cmp_data[MAX_BLOB_BUFFER_DATA]; /* our buffer to compare the result */ sqlo_lob_desc_t loblp; unsigned int loblen; printf("Query CLOB\n"); fillbuf2(cmp_data, MAX_BLOB_BUFFER_DATA); /* our reference data */ /* parse */ if (0 > (sth = sqlo_prepare(dbh, stmt))) error_exit(dbh, "sqlo_prepare"); /* bind input */ if (SQLO_SUCCESS != (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0))) { error_exit(dbh, "sqlo_bind_by_pos"); } /* allocate a lob descriptor */ if (0 > sqlo_alloc_lob_desc(dbh, &loblp)) error_exit(dbh, "sqlo_alloc_lob_desc"); /* define output */ if (SQLO_SUCCESS != (sqlo_define_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0, 0)) || (sqlo_define_by_pos(sth, 2, SQLOT_CLOB, &loblp, 0, &ind, 0, 0))) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_define_by_pos2"); } /* execute */ status = sqlo_execute(sth, 1); if (SQLO_SUCCESS != status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_execute"); } if (ind != SQLO_NULL_IND) { status = sqlo_lob_get_length(dbh, loblp, &loblen); if ( 0 > status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_free_lob_desc"); } if (loblen != MAX_BLOB_BUFFER_DATA) { printf("Invalid LOB size. Expected %d, got %d\n", MAX_BLOB_BUFFER_DATA, loblen); sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); return 0; } /* allocate the buffer for the data */ data = malloc(loblen * sizeof(char)); if (!data) { printf("FATAL: malloc error at %d\n", __LINE__); sqlo_free_lob_desc(dbh, &loblp); exit(EXIT_FAILURE); } /* read the data into the buffer */ status = sqlo_lob_read_buffer(dbh, loblp, loblen, data, loblen); if ( 0 > status) { printf("sqlo_lob_read_buffer failed: %s\n", sqlo_geterror(dbh) ); sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_lob_read_buffer"); } printf("Compare CLOB\n"); /* compare with our reference data */ if (memcmp(data, &cmp_data, MAX_BLOB_BUFFER_DATA)) { int i; printf("LOB read is different from LOB written\n"); for (i = 0; i <= MAX_BLOB_BUFFER_DATA; ++i) { if (data[i] != cmp_data[i]) printf("diff at pos %d\n", i); } } if (data) free(data); data = NULL; } else { printf("LOB is NULL\n"); return 0; } sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); return (1); } /* $Id: ex15.c 286 2004-06-15 10:15:52Z kpoitschke $ */
int sqlo_lob_read_stream | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t | loblp, | |||
unsigned int | loblen, | |||
FILE * | fp | |||
) |
Read lob data from lob column into a stream.
Use sqlo_lob_get_length to get the loblen you have to use here.
dbh | I - A database handle. | |
loblp | I - A lob locator. | |
loblen | I - The length of the lob | |
fp | I - A filepointer |
/* $Id: ex16.c 286 2004-06-15 10:15:52Z kpoitschke $ */ #include <stdio.h> #include <stdlib.h> #include "examples.h" int select_file_from_blob_table(sqlo_db_handle_t dbh, int key, const char *fname ) { char * stmt = "SELECT KEY, BDATA FROM T_SQLORA_BLOB WHERE KEY = :1"; int status; sqlo_stmt_handle_t sth = SQLO_STH_INIT; int k = key; FILE * fp; sqlo_lob_desc_t loblp; unsigned int loblen; short ind; if (!(fp = fopen(fname, "w"))) { printf("ERROR: Cannot open %s for write\n", fname); return 0; } /* parse */ if (0 > (sth = sqlo_prepare(dbh, stmt))) error_exit(dbh, "sqlo_prepare"); /* bind input */ if (SQLO_SUCCESS != (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0))) error_exit(dbh, "sqlo_bind_by_pos"); /* alloc lob desc */ if (SQLO_SUCCESS != sqlo_alloc_lob_desc(dbh, &loblp)) error_exit(dbh, "sqlo_alloc_lob_desc"); /* define output */ if (SQLO_SUCCESS != (sqlo_define_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0, 0)) || (sqlo_define_by_pos(sth, 2, SQLOT_BLOB, &loblp, 0, &ind, 0, 0))) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_define_by_pos2"); } /* execute */ status = sqlo_execute(sth, 1); if ( 0 > status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_execute"); } /* get the LOB length */ status = sqlo_lob_get_length(dbh, loblp, &loblen); if ( 0 > status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_log_get_length"); } /* write it to the file */ status = sqlo_lob_read_stream(dbh, loblp, loblen, fp); if ( 0 > status) { sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); error_exit(dbh, "sqlo_lob_read_stream"); } fclose(fp); sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); sqlo_rollback(dbh); drop_blob_table(dbh); return (1); } /* $Id: ex16.c 286 2004-06-15 10:15:52Z kpoitschke $ */
int sqlo_lob_write_buffer | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t | loblp, | |||
unsigned int | loblen, | |||
void * | bufp, | |||
unsigned int | bufl, | |||
unsigned int | piece | |||
) |
Write lob data from buffer into the lob column.
dbh | I - A database handle. | |
loblen | I - The length of the lob. | |
loblp | I - A lob locator. | |
bufp | I - A buffer of data. | |
bufl | I - The length of the buffer in terms of bytes. | |
piece | I - The piece indicator
|
/* $Id: ex13.c 286 2004-06-15 10:15:52Z kpoitschke $ */ #include <stdio.h> #include <stdlib.h> #include "examples.h" int insert_into_blob_table(sqlo_db_handle_t dbh, int key ) { char * stmt = "INSERT INTO T_SQLORA_BLOB (KEY, CDATA) " "VALUES (:b1, EMPTY_CLOB()) RETURNING CDATA INTO :b2"; char data[MAX_BLOB_BUFFER_DATA]; /* a data buffer */ sqlo_lob_desc_t loblp; /* the lob locator */ sqlo_stmt_handle_t sth; int status; int k = key; printf("Insert CLOB\n"); /* create the test table */ create_blob_table(dbh); /* fill the data buffer with some characters */ fillbuf(data, MAX_BLOB_BUFFER_DATA); /* parse the statement */ if (0>(sth = sqlo_prepare(dbh, stmt))) error_exit(dbh, "sqlo_prepare"); /* alloate the lob descriptor */ if (0 > sqlo_alloc_lob_desc(dbh, &loblp)) error_exit(dbh, "sqlo_alloc_lob_desc"); /* bind input variables. Note: we bind the lob descriptor here */ if (SQLO_SUCCESS != (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(int), NULL, 0)) || (sqlo_bind_by_pos(sth, 2, SQLOT_CLOB, &loblp, 0, NULL, 0)) ) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_bind_by_pos"); } /* execute the statement */ status = sqlo_execute(sth, 1); if (SQLO_SUCCESS != status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_execute"); } /* write the lob */ status = sqlo_lob_write_buffer(dbh, loblp, MAX_BLOB_BUFFER_DATA, data, MAX_BLOB_BUFFER_DATA, SQLO_ONE_PIECE); if (status < 0) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_log_write_buffer"); } sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); return (1); } void create_blob_table(sqlo_db_handle_t dbh) { char * stmt= "CREATE TABLE T_SQLORA_BLOB (KEY INTEGER, CDATA CLOB, BDATA BLOB)"; if (SQLO_SUCCESS != sqlo_exec(dbh, stmt)) if (sqlo_geterrcode(dbh) != 955) /* table exists already */ error_exit(dbh, "sqlo_exec"); } void drop_blob_table(sqlo_db_handle_t dbh) { char * stmt = "DROP TABLE T_SQLORA_BLOB"; if (SQLO_SUCCESS != sqlo_exec(dbh, stmt)) error_exit(dbh, "sqlo_exec"); } void fillbuf(char * data, int len) { int i; for (i = 0; i < len; ++i) { data[i] = 'A' + i % 26; } } /* $Id: ex13.c 286 2004-06-15 10:15:52Z kpoitschke $ */
/* $Id: ex13b.c 286 2004-06-15 10:15:52Z kpoitschke $ */ #include <stdio.h> #include <stdlib.h> #include "examples.h" int update_blob_table(sqlo_db_handle_t dbh, int key ) { char * stmt = "UPDATE T_SQLORA_BLOB SET CDATA = EMPTY_CLOB() WHERE KEY=:b1 " "RETURNING CDATA INTO :b2"; char data[MAX_BLOB_BUFFER_DATA]; /* a data buffer */ sqlo_lob_desc_t loblp; /* the lob locator */ sqlo_stmt_handle_t sth; int status; int k = key; printf("Update CLOB\n"); /* fill the data buffer with some characters */ fillbuf2(data, MAX_BLOB_BUFFER_DATA); /* parse the statement */ if (0>(sth = sqlo_prepare(dbh, stmt))) error_exit(dbh, "sqlo_prepare"); /* alloate the lob descriptor */ if (0 > sqlo_alloc_lob_desc(dbh, &loblp)) error_exit(dbh, "sqlo_alloc_lob_desc"); /* bind input variables. Note: we bind the lob descriptor here */ if (SQLO_SUCCESS != (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(int), NULL, 0)) || (sqlo_bind_by_pos(sth, 2, SQLOT_CLOB, &loblp, 0, NULL, 0)) ) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_bind_by_pos"); } /* execute the statement */ status = sqlo_execute(sth, 1); if (SQLO_SUCCESS != status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_execute"); } /* write the lob */ status = sqlo_lob_write_buffer(dbh, loblp, MAX_BLOB_BUFFER_DATA, data, MAX_BLOB_BUFFER_DATA, SQLO_ONE_PIECE); if (status < 0) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_log_write_buffer"); } sqlo_free_lob_desc(dbh, &loblp); sqlo_close(sth); return (1); } void fillbuf2(char * data, int len) { int i; for (i = 0; i < len; ++i) { data[i] = 'a' + i % 26; } } /* $Id: ex13b.c 286 2004-06-15 10:15:52Z kpoitschke $ */
int sqlo_lob_write_stream | ( | sqlo_db_handle_t | dbh, | |
sqlo_lob_desc_t | loblp, | |||
unsigned int | filelen, | |||
FILE * | fp | |||
) |
Write lob data from a file into the lob column.
This function reads the data from the stream and writes it into the lob column via sqlo_lob_write_buffer.
dbh | I - A database handle. | |
loblp | I - A lob locator. | |
filelen | I - The size of the file (total lob length) | |
fp | I - The filepointer |
/* $Id: ex14.c 286 2004-06-15 10:15:52Z kpoitschke $ */ #include <stdio.h> #include <stdlib.h> #include "examples.h" int insert_file_into_blob_table(sqlo_db_handle_t dbh, int key, const char * fname ) { char * stmt = "INSERT INTO T_SQLORA_BLOB (KEY, BDATA) " "VALUES (:b1, EMPTY_BLOB()) RETURNING BDATA INTO :b2"; sqlo_lob_desc_t loblp; /* the lob locator */ sqlo_stmt_handle_t sth; int status; FILE * fp; unsigned filelen; short ind; int k = key; create_blob_table(dbh); if (! (fp = fopen(fname, "r"))) { printf("ERROR: cannot open file %s\n", fname); return 0; } /* determine file len */ fseek(fp, 0, SEEK_END); filelen = ftell(fp); fseek(fp, 0, 0); /* parse */ if (0>(sth = sqlo_prepare(dbh, stmt))) error_exit(dbh, "sqlo_prepare"); /* allocate the lob locator */ if (0 > sqlo_alloc_lob_desc(dbh, &loblp)) error_exit(dbh, "sqlo_alloc_lob_desc"); /* bind input variables */ if (SQLO_SUCCESS != (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(int), NULL, 0)) || (sqlo_bind_by_pos(sth, 2, SQLOT_BLOB, &loblp, 0, &ind, 0)) ) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_bind_by_pos"); } /* execute */ status = sqlo_execute(sth, 1); if (SQLO_SUCCESS != status) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_execute"); } /* write the lob */ status = sqlo_lob_write_stream(dbh, loblp, filelen, fp); if (status < 0) { sqlo_free_lob_desc(dbh, &loblp); error_exit(dbh, "sqlo_lob_write_stream"); } sqlo_close(sth); sqlo_free_lob_desc(dbh, &loblp); fclose(fp); return (1); } /* $Id: ex14.c 286 2004-06-15 10:15:52Z kpoitschke $ */