1 #include "common.h"
2 
3 /* Test for {?=call store(?,123,'foo')} syntax and run */
4 
5 int
main(int argc,char * argv[])6 main(int argc, char *argv[])
7 {
8 	SQLINTEGER input, output;
9 	SQLINTEGER out1;
10 	SQLLEN ind, ind2, ind3;
11 	const char *sql;
12 
13 	odbc_connect();
14 
15 	if (odbc_command_with_result(odbc_stmt, "drop proc const_param") != SQL_SUCCESS)
16 		printf("Unable to execute statement\n");
17 
18 	odbc_command("create proc const_param @in1 int, @in2 int, @in3 datetime, @in4 varchar(10), @out int output as\n"
19 		"begin\n"
20 		" set nocount on\n"
21 		" select @out = 7654321\n"
22 		" if (@in1 <> @in2 and @in2 is not null) or @in3 <> convert(datetime, '2004-10-15 12:09:08') or @in4 <> 'foo'\n"
23 		"  select @out = 1234567\n"
24 		" return 24680\n"
25 		"end");
26 
27 	CHKBindParameter(1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &input, 0, &ind, "S");
28 	CHKBindParameter(2, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &out1, 0, &ind2, "S");
29 
30 	/* TODO use {ts ...} for date */
31 	CHKPrepare(T("{call const_param(?, 13579, '2004-10-15 12:09:08', 'foo', ?)}"), SQL_NTS, "S");
32 
33 	input = 13579;
34 	ind = sizeof(input);
35 	out1 = output = 0xdeadbeef;
36 	CHKExecute("S");
37 
38 	if (out1 != 7654321) {
39 		fprintf(stderr, "Invalid output %d (0x%x)\n", (int) out1, (int) out1);
40 		return 1;
41 	}
42 
43 	/* just to reset some possible buffers */
44 	odbc_command("DECLARE @i INT");
45 
46 	/* MS ODBC don't support empty parameters altough documented so avoid this test */
47 	if (odbc_driver_is_freetds()) {
48 
49 		CHKBindParameter(1, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &output, 0, &ind,  "S");
50 		CHKBindParameter(2, SQL_PARAM_INPUT,  SQL_C_SLONG, SQL_INTEGER, 0, 0, &input,  0, &ind2, "S");
51 		CHKBindParameter(3, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &out1,   0, &ind3, "S");
52 
53 		/* TODO use {ts ...} for date */
54 		CHKPrepare(T("{?=call const_param(?, , '2004-10-15 12:09:08', 'foo', ?)}"), SQL_NTS, "S");
55 
56 		input = 13579;
57 		ind2 = sizeof(input);
58 		out1 = output = 0xdeadbeef;
59 		CHKExecute("S");
60 
61 		if (out1 != 7654321) {
62 			fprintf(stderr, "Invalid output %d (0x%x)\n", (int) out1, (int) out1);
63 			return 1;
64 		}
65 
66 		if (output != 24680) {
67 			fprintf(stderr, "Invalid result %d (0x%x) expected 24680\n", (int) output, (int) output);
68 			return 1;
69 		}
70 	}
71 
72 	odbc_command("IF OBJECT_ID('const_param') IS NOT NULL DROP PROC const_param");
73 
74 	odbc_command("create proc const_param @in1 float, @in2 varbinary(100) as\n"
75 		"begin\n"
76 		" if @in1 <> 12.5 or @in2 <> 0x0102030405060708\n"
77 		"  return 12345\n"
78 		" return 54321\n"
79 		"end");
80 
81 	CHKBindParameter(1, SQL_PARAM_OUTPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &output, 0, &ind, "S");
82 
83 	CHKPrepare(T("{?=call const_param(12.5, 0x0102030405060708)}"), SQL_NTS, "S");
84 
85 	output = 0xdeadbeef;
86 	CHKExecute("S");
87 
88 	if (output != 54321) {
89 		fprintf(stderr, "Invalid result %d (0x%x) expected 54321\n", (int) output, (int) output);
90 		return 1;
91 	}
92 
93 	odbc_command("drop proc const_param");
94 
95 	odbc_command("create proc const_param @in varchar(20) as\n"
96 		"begin\n"
97 		" if @in = 'value' select 8421\n"
98 		" select 1248\n"
99 		"end");
100 
101 	/* catch problem reported by Peter Deacon */
102 	output = 0xdeadbeef;
103 	odbc_command("{CALL const_param('value')}");
104 	CHKBindCol(1, SQL_C_SLONG, &output, 0, &ind, "S");
105 	SQLFetch(odbc_stmt);
106 
107 	if (output != 8421) {
108 		fprintf(stderr, "Invalid result %d (0x%x)\n", (int) output, (int) output);
109 		return 1;
110 	}
111 
112 	odbc_reset_statement();
113 
114 	odbc_command("drop proc const_param");
115 
116 	sql = "create proc const_param @in1 bigint as\n"
117 	"begin\n"
118 	" if @in1 <> 1000000000000 select 0 else select 1\n"
119 	"end";
120 	if (odbc_command_with_result(odbc_stmt, sql) == SQL_SUCCESS) {
121 		output = 0xdeadbeef;
122 		odbc_command("{CALL const_param(1000000000000)}");
123 		CHKBindCol(1, SQL_C_SLONG, &output, 0, &ind, "S");
124 		SQLFetch(odbc_stmt);
125 
126 		if (output != 1) {
127 			fprintf(stderr, "Invalid result %d (0x%x)\n", (int) output, (int) output);
128 			return 1;
129 		}
130 
131 		odbc_reset_statement();
132 
133 		odbc_command("drop proc const_param");
134 	}
135 
136 	odbc_disconnect();
137 
138 	printf("Done.\n");
139 	return 0;
140 }
141