File: my_unixodbc.c

package info (click to toggle)
myodbc 3.51.09-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,400 kB
  • ctags: 1,596
  • sloc: ansic: 29,806; sh: 10,765; cpp: 627; makefile: 610
file content (189 lines) | stat: -rw-r--r-- 5,529 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
                          my_unixodbc.c  -  description
                             -------------------
    begin                : Wed Aug 8 2001
    copyright            : (C) MySQL AB 1997-2001
    author               : venu ( venu@mysql.com )
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "mytest3.h"

void t_odbc3_envattr()
{
  SQLRETURN rc; 
  SQLHENV henv;
  SQLHDBC hdbc;
  SQLPOINTER ov_version;

  myheader("t_odbc3_envattr");

    rc = SQLAllocEnv(&henv);
    myenv(henv,rc);

    rc = SQLAllocConnect(henv,&hdbc);
    myenv(henv,rc);

    rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
    myenv_err(henv,rc == SQL_ERROR,rc);

    rc = SQLGetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,&ov_version,0,0);
    myenv(henv,rc);
    myprintf("\ndefault odbc version:%d",ov_version);
    my_assert(ov_version == (SQLPOINTER)SQL_OV_ODBC2);

    rc = SQLFreeConnect(hdbc);
    mycon(hdbc,rc);

    rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
    myenv(henv,rc);

    rc = SQLGetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,&ov_version,0,0);
    myenv(henv,rc);
    myprintf("\nnew odbc version:%d",ov_version);
    my_assert(ov_version == (SQLPOINTER)SQL_OV_ODBC3);
    
    rc = SQLFreeEnv(henv);
    myenv(henv,rc);
}
void t_odbc3_handle()
{
  SQLRETURN rc; 
  SQLHENV henv;
  SQLHDBC hdbc;
  SQLHSTMT hstmt;
  SQLPOINTER ov_version;

  myheader("t_odbc3_handle");

    rc = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
    myenv(henv,rc);

#if 0
    rc = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
    myenv_err(henv,rc == SQL_ERROR,rc);

    rc = SQLGetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,&ov_version,0,0);
    myenv(henv,rc);
    myprintf("\ndefault odbc version:%d",ov_version);
    my_assert(ov_version == (SQLPOINTER)SQL_OV_ODBC2);
#endif

    rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
    myenv(henv,rc);

    rc = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
    myenv(henv,rc);

    rc = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,0);
    myenv_err(henv,rc == SQL_ERROR,rc);

    rc = SQLGetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,&ov_version,0,0);
    myenv(henv,rc);
    myprintf("\ndefault odbc version:%d",ov_version);
    my_assert(ov_version == (SQLPOINTER)SQL_OV_ODBC3);

    rc = SQLConnect(hdbc, mydsn, SQL_NTS, myuid, SQL_NTS,  mypwd, SQL_NTS);
    mycon(hdbc,rc);

    rc = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt);
    mycon(hdbc, rc);

    rc = SQLDisconnect(hdbc);
    mycon(hdbc, rc);

    rc = SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
    mycon(hdbc,rc);

    rc = SQLFreeHandle(SQL_HANDLE_ENV,henv);
    myenv(henv,rc);
}
void t_driver_connect()
{
  SQLRETURN rc; 
  SQLHENV henv;
  SQLHDBC hdbc;
  SQLCHAR conn_in[255], conn_out[255];

  myheader("t_driver_connect");

    rc = SQLAllocEnv(&henv);
    myenv(henv,rc);

    rc = SQLAllocConnect(henv,&hdbc);
    myenv(henv,rc);

    sprintf(conn_in,"DRIVER={MySQL ODBC 3.51 Driver};USER=%s;PASSWORD=%s;DATABASE=%s;SERVER=%s;OPTION=3;SOCKET=/tmp/mysql.sock;STMT=use mysql;",
            myuid, mypwd, mydb, myserver);

    rc = SQLDriverConnect(hdbc, (SQLHWND)0, (SQLCHAR *)conn_in, sizeof(conn_in),
                          (SQLCHAR *)conn_out, sizeof(conn_out), 0, 
                          SQL_DRIVER_NOPROMPT);

    if (rc == SQL_SUCCESS)
    {
      mycon(hdbc,rc);
      fprintf(stdout, "output string: `%s`", conn_out); 

      rc = SQLDisconnect(hdbc);
      mycon(hdbc,rc);
    }

    rc = SQLFreeConnect(hdbc);
    mycon(hdbc,rc);
    
    rc = SQLFreeEnv(henv);
    myenv(henv,rc);
}
/*---------------------------------------------------------------------
@ type       : MyODBC3 test main function
@ name       : main
@ purpose    : to control all tests
@ limitation : none
*---------------------------------------------------------------------*/
int main(int argc, char *argv[])
{  
  SQLINTEGER narg;      
  
    /*
     *  show the usage string when the user asks for this
    */    
      myprintf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
      myprintf("usage: mytest [DSN] [UID] [PWD] \n\n");  
      myprintf("       DSN    <-- data source name\n");
      myprintf("       UID    <-- user name\n");
      myprintf("       PWD    <-- password\n");
      myprintf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");     

    /*
     * if connection string supplied through arguments, overrite
     * the default one..
    */
    for(narg = 1; narg < argc; narg++)
    {     
      if ( narg == 1 )
        mydsn = argv[1];
      else if ( narg == 2 )
        myuid = argv[2];
      else if ( narg == 3 )
        mypwd = argv[3];      
          
    }   

    t_odbc3_envattr();
    t_odbc3_handle();
    t_driver_connect();

  myprintf("\n\n-- test-end --\n");
  return(0);
}