00001 /* $Id: ex15.c 286 2004-06-15 10:15:52Z kpoitschke $ */ 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <string.h> 00005 #include "examples.h" 00006 00007 int select_from_blob_table(sqlo_db_handle_t dbh, int key ) 00008 { 00009 char * stmt = 00010 "SELECT KEY, CDATA FROM T_SQLORA_BLOB WHERE KEY = :1"; 00011 int status; 00012 sqlo_stmt_handle_t sth = SQLO_STH_INIT; 00013 int k = key; 00014 char * data; 00015 short ind; 00016 char cmp_data[MAX_BLOB_BUFFER_DATA]; /* our buffer to compare the result */ 00017 sqlo_lob_desc_t loblp; 00018 unsigned int loblen; 00019 00020 printf("Query CLOB\n"); 00021 00022 fillbuf2(cmp_data, MAX_BLOB_BUFFER_DATA); /* our reference data */ 00023 00024 /* parse */ 00025 if (0 > (sth = sqlo_prepare(dbh, stmt))) 00026 error_exit(dbh, "sqlo_prepare"); 00027 00028 /* bind input */ 00029 if (SQLO_SUCCESS != 00030 (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0))) { 00031 error_exit(dbh, "sqlo_bind_by_pos"); 00032 } 00033 00034 /* allocate a lob descriptor */ 00035 if (0 > sqlo_alloc_lob_desc(dbh, &loblp)) 00036 error_exit(dbh, "sqlo_alloc_lob_desc"); 00037 00038 /* define output */ 00039 if (SQLO_SUCCESS != 00040 (sqlo_define_by_pos(sth, 1, SQLOT_INT, &k, sizeof(k), 0, 0, 0)) || 00041 (sqlo_define_by_pos(sth, 2, SQLOT_CLOB, &loblp, 0, &ind, 0, 0))) { 00042 00043 sqlo_free_lob_desc(dbh, &loblp); 00044 error_exit(dbh, "sqlo_define_by_pos2"); 00045 } 00046 00047 /* execute */ 00048 status = sqlo_execute(sth, 1); 00049 00050 if (SQLO_SUCCESS != status) { 00051 sqlo_free_lob_desc(dbh, &loblp); 00052 error_exit(dbh, "sqlo_execute"); 00053 } 00054 00055 if (ind != SQLO_NULL_IND) 00056 { 00057 status = sqlo_lob_get_length(dbh, loblp, &loblen); 00058 00059 if ( 0 > status) { 00060 sqlo_free_lob_desc(dbh, &loblp); 00061 error_exit(dbh, "sqlo_free_lob_desc"); 00062 } 00063 00064 if (loblen != MAX_BLOB_BUFFER_DATA) { 00065 printf("Invalid LOB size. Expected %d, got %d\n", MAX_BLOB_BUFFER_DATA, 00066 loblen); 00067 sqlo_free_lob_desc(dbh, &loblp); 00068 sqlo_close(sth); 00069 return 0; 00070 } 00071 00072 /* allocate the buffer for the data */ 00073 data = malloc(loblen * sizeof(char)); 00074 if (!data) { 00075 printf("FATAL: malloc error at %d\n", __LINE__); 00076 sqlo_free_lob_desc(dbh, &loblp); 00077 exit(EXIT_FAILURE); 00078 } 00079 00080 /* read the data into the buffer */ 00081 status = sqlo_lob_read_buffer(dbh, loblp, loblen, data, loblen); 00082 00083 if ( 0 > status) { 00084 printf("sqlo_lob_read_buffer failed: %s\n", sqlo_geterror(dbh) ); 00085 sqlo_free_lob_desc(dbh, &loblp); 00086 error_exit(dbh, "sqlo_lob_read_buffer"); 00087 } 00088 00089 printf("Compare CLOB\n"); 00090 /* compare with our reference data */ 00091 if (memcmp(data, &cmp_data, MAX_BLOB_BUFFER_DATA)) { 00092 int i; 00093 printf("LOB read is different from LOB written\n"); 00094 for (i = 0; i <= MAX_BLOB_BUFFER_DATA; ++i) { 00095 if (data[i] != cmp_data[i]) 00096 printf("diff at pos %d\n", i); 00097 } 00098 } 00099 00100 if (data) 00101 free(data); 00102 data = NULL; 00103 } else { 00104 printf("LOB is NULL\n"); 00105 return 0; 00106 } 00107 00108 sqlo_free_lob_desc(dbh, &loblp); 00109 sqlo_close(sth); 00110 00111 return (1); 00112 00113 } 00114 00115 /* $Id: ex15.c 286 2004-06-15 10:15:52Z kpoitschke $ */