Line data Source code
1 : #include "common.h"
2 :
3 : /* Test for store procedure and params */
4 : /* Test from Tom Rogers */
5 :
6 : /* SP definition */
7 :
8 : static const char sp_define[] = "CREATE PROCEDURE spTestProc\n"
9 : " @InParam int,\n"
10 : " @OutParam int OUTPUT,\n"
11 : " @OutString varchar(20) OUTPUT\n"
12 : "AS\n"
13 : " SELECT @OutParam = @InParam\n"
14 : " SELECT @OutString = 'This is cool!'\n"
15 : " IF @InParam < 10\n" " RETURN (0)\n" " ELSE\n" " RETURN (1)\n";
16 :
17 : static char sp_text[] = "{?=call spTestProc(?,?,?)}";
18 :
19 : #define OUTSTRING_LEN 20
20 :
21 : static int
22 20 : Test(int bind_before)
23 : {
24 20 : SQLSMALLINT ReturnCode = 0;
25 20 : SQLSMALLINT InParam = 5;
26 20 : SQLSMALLINT OutParam = 1;
27 : char OutString[OUTSTRING_LEN];
28 20 : SQLLEN cbReturnCode = 0, cbInParam = 0, cbOutParam = 0;
29 20 : SQLLEN cbOutString = SQL_NTS;
30 :
31 20 : odbc_connect();
32 :
33 : /* drop proc */
34 20 : odbc_command("IF OBJECT_ID('spTestProc') IS NOT NULL DROP PROC spTestProc");
35 :
36 : /* create proc */
37 20 : odbc_command(sp_define);
38 :
39 20 : if (!bind_before)
40 10 : CHKPrepare(T(sp_text), sizeof(sp_text) - 1, "S");
41 :
42 20 : CHKBindParameter(1, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &ReturnCode, 0, &cbReturnCode, "S");
43 20 : CHKBindParameter(2, SQL_PARAM_INPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &InParam, 0, &cbInParam, "S");
44 20 : CHKBindParameter(3, SQL_PARAM_OUTPUT, SQL_C_SSHORT, SQL_INTEGER, 0, 0, &OutParam, 0, &cbOutParam, "S");
45 :
46 : OutString[0] = '\0';
47 20 : strcpy(OutString, "Test"); /* Comment this line and we get an error! Why? */
48 20 : CHKBindParameter(4, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, OUTSTRING_LEN, 0, OutString,
49 : OUTSTRING_LEN, &cbOutString, "S");
50 :
51 20 : if (bind_before)
52 10 : CHKPrepare(T(sp_text), sizeof(sp_text) - 1, "S");
53 :
54 20 : CHKExecute("S");
55 :
56 20 : odbc_command("DROP PROC spTestProc");
57 :
58 20 : printf("Output:\n");
59 20 : printf(" Return Code = %d\n", (int) ReturnCode);
60 20 : printf(" InParam = %d\n", (int) InParam);
61 20 : printf(" OutParam = %d\n", (int) OutParam);
62 20 : printf(" OutString = %s\n", OutString);
63 :
64 20 : if (InParam != OutParam) {
65 0 : fprintf(stderr, "InParam != OutParam\n");
66 0 : return 1;
67 : }
68 :
69 20 : if (strcmp(OutString, "This is cool!") != 0) {
70 0 : fprintf(stderr, "Bad string returned\n");
71 0 : return 1;
72 : }
73 :
74 20 : odbc_disconnect();
75 20 : ODBC_FREE();
76 20 : return 0;
77 : }
78 :
79 10 : TEST_MAIN()
80 : {
81 10 : odbc_use_version3 = false;
82 :
83 10 : if (Test(0))
84 : return 1;
85 10 : if (Test(1))
86 : return 1;
87 :
88 10 : printf("Done successfully!\n");
89 10 : return 0;
90 : }
|