Line data Source code
1 : /* Test we can insert long binary into database.
2 : */
3 : #include <config.h>
4 :
5 : #include <stdio.h>
6 :
7 : #if HAVE_STDLIB_H
8 : #include <stdlib.h>
9 : #endif /* HAVE_STDLIB_H */
10 :
11 : #if HAVE_STRING_H
12 : #include <string.h>
13 : #endif /* HAVE_STRING_H */
14 :
15 : #include <ctpublic.h>
16 : #include "common.h"
17 :
18 : static const CS_INT unused = CS_UNUSED, nullterm = CS_NULLTERM;
19 : static CS_INT result_len = -1;
20 :
21 : static CS_RETCODE
22 0 : csmsg_callback(CS_CONTEXT *ctx, CS_CLIENTMSG * emsgp)
23 : {
24 0 : printf("message from csmsg_callback(): %s\n", emsgp->msgstring);
25 0 : return CS_SUCCEED;
26 : }
27 :
28 : static void
29 148 : check_ret(const char *name, CS_RETCODE ret)
30 : {
31 148 : if (ret != CS_SUCCEED) {
32 0 : fprintf(stderr, "%s(): failed\n", name);
33 0 : exit(1);
34 : }
35 148 : }
36 :
37 : static int
38 40 : fetch_results(CS_COMMAND * command)
39 : {
40 : CS_INT result_type, int_result, copied, rows_read;
41 40 : CS_SMALLINT ind = 0;
42 : CS_DATAFMT datafmt;
43 40 : int result = 0;
44 :
45 40 : memset(&datafmt, 0, sizeof(datafmt));
46 40 : datafmt.datatype = CS_INT_TYPE;
47 40 : datafmt.count = 1;
48 :
49 40 : check_ret("ct_results", ct_results(command, &result_type));
50 : do {
51 108 : if (result_type == CS_ROW_RESULT) {
52 4 : check_ret("ct_bind", ct_bind(command, 1, &datafmt, &int_result, &copied, &ind));
53 4 : check_ret("ct_fetch", ct_fetch(command, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows_read));
54 4 : printf("received %d bytes\n", (int) int_result);
55 4 : result_len = int_result;
56 : }
57 108 : if (result_type == CS_CMD_FAIL) {
58 4 : result = 1;
59 : }
60 108 : } while (ct_results(command, &result_type) == CS_SUCCEED);
61 40 : return result;
62 : }
63 :
64 : static int
65 36 : execute_sql(CS_COMMAND * command, const char *sql)
66 : {
67 36 : printf("executing sql: %s\n", sql);
68 36 : check_ret("ct_command", ct_command(command, CS_LANG_CMD, sql, nullterm, unused));
69 36 : check_ret("ct_send", ct_send(command));
70 36 : return fetch_results(command);
71 : }
72 :
73 : int
74 8 : main(int argc, char **argv)
75 : {
76 8 : int verbose = 0;
77 : CS_COMMAND *command;
78 : CS_CONNECTION *connection;
79 : CS_CONTEXT *context;
80 : unsigned char buffer[65536];
81 8 : CS_INT buffer_len = 8192;
82 8 : CS_SMALLINT ind = 0;
83 : CS_DATAFMT datafmt;
84 : CS_INT ret;
85 : int i;
86 :
87 8 : printf("-- begin --\n");
88 :
89 8 : check_ret("try_ctlogin", try_ctlogin(&context, &connection, &command, verbose));
90 8 : check_ret("cs_config", cs_config(context, CS_SET, CS_MESSAGE_CB, (CS_VOID *) csmsg_callback, unused, 0));
91 :
92 8 : execute_sql(command, "if object_id('mps_table') is not null drop table mps_table");
93 8 : execute_sql(command, "if object_id('mps_rpc') is not null drop procedure mps_rpc");
94 : /* if this query fails probably we are using wrong database vendor or version */
95 8 : ret = execute_sql(command, "create procedure mps_rpc (@varbinary_param varbinary(max)) as "
96 : "insert mps_table values (@varbinary_param) " "select len(varbinary_data) from mps_table");
97 8 : if (ret != 0) {
98 4 : try_ctlogout(context, connection, command, verbose);
99 4 : return 0;
100 : }
101 4 : execute_sql(command, "create table mps_table (varbinary_data varbinary(max))");
102 :
103 4 : if (argc > 1)
104 0 : buffer_len = atoi(argv[1]);
105 4 : if (buffer_len < 0 || buffer_len > sizeof(buffer))
106 : return 1;
107 :
108 4 : printf("sending %d bytes\n", buffer_len);
109 :
110 32772 : for (i = 0; i < buffer_len; i++)
111 32768 : buffer[i] = (rand() % 16);
112 :
113 4 : memset(&datafmt, 0, sizeof(datafmt));
114 4 : strcpy(datafmt.name, "@varbinary_param");
115 4 : datafmt.namelen = nullterm;
116 4 : datafmt.datatype = CS_IMAGE_TYPE;
117 4 : datafmt.status = CS_INPUTVALUE;
118 :
119 4 : check_ret("ct_command", ct_command(command, CS_RPC_CMD, "mps_rpc", nullterm, unused));
120 4 : check_ret("ct_setparam", ct_setparam(command, &datafmt, buffer, &buffer_len, &ind));
121 4 : check_ret("ct_send", ct_send(command));
122 :
123 4 : fetch_results(command);
124 :
125 4 : execute_sql(command, "drop table mps_table");
126 4 : execute_sql(command, "drop procedure mps_rpc");
127 4 : try_ctlogout(context, connection, command, verbose);
128 :
129 4 : printf("-- end --\n");
130 4 : return (result_len == buffer_len) ? 0 : 1;
131 : }
|