File: my_use_result.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 (225 lines) | stat: -rw-r--r-- 6,233 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/***************************************************************************
                          my_use_result.c  -  description
                             -------------------
    begin                : Fri Sep 29 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"

SQLINTEGER my_max_rows = 10000;
clock_t t_start, t_end;
SQLDOUBLE my_time;

/**
  Clean data
*/
void t_clean_data()
{
  SQLHENV   henv;
  SQLHDBC   hdbc;
  SQLHSTMT  hstmt;
  SQLRETURN rc;
  
  myheader("t_clean_data");
    
    myconnect(&henv,&hdbc,&hstmt);

    rc = SQLExecDirect(hstmt,"drop database client_odbc_test1",SQL_NTS);
    mystmt(hstmt,rc);

    mydisconnect(&henv,&hdbc,&hstmt);
}
/**
  Initialize data
*/
void t_init_data()
{
  SQLHENV   henv;
  SQLHDBC   hdbc;
  SQLHSTMT  hstmt;
  SQLRETURN rc;
  SQLINTEGER i;
  SQLCHAR    ch[]="MySQL AB";
  
  myheader("t_init_data");

    
    myconnect(&henv,&hdbc,&hstmt);

    SQLExecDirect(hstmt,"drop database if exists client_odbc_test1",SQL_NTS);
    
    rc = SQLExecDirect(hstmt,"create database client_odbc_test1",SQL_NTS);
    mystmt(hstmt,rc);

    SQLExecDirect(hstmt,"drop table client_odbc_test1.t_use_result",SQL_NTS);

    rc = SQLExecDirect(hstmt,"create table client_odbc_test1.t_use_result(id int,name char(10))",SQL_NTS);
    mystmt(hstmt,rc);

    rc = SQLPrepare(hstmt,"insert into client_odbc_test1.t_use_result values(?,?)",SQL_NTS);
    mystmt(hstmt,rc);

    rc = SQLBindParameter(hstmt,1,SQL_PARAM_INPUT, SQL_C_ULONG,
                          SQL_INTEGER,0,0,&i,0,NULL);
    mystmt(hstmt,rc);

    rc = SQLBindParameter(hstmt,2,SQL_PARAM_INPUT, SQL_C_CHAR,
                          SQL_CHAR,0,0,&ch,10,NULL);
    mystmt(hstmt,rc);

    myprintf("\n inserting '%d' rows (it will take few minutes)\n",my_max_rows);
    t_start = clock();
    
    for( i = 1; i <= my_max_rows; i++ )
    {
      rc = SQLExecute(hstmt);
      mystmt(hstmt,rc);
      if (!(i % 1000)) myprintf(" \r %d",i);
    }
    t_end = clock();

    my_time = difftime(t_end,t_start)/CLOCKS_PER_SEC;   
    myprintf("\n total of '%d' rows inserted in %5.3f SECS",my_max_rows,my_time);

    SQLFreeStmt(hstmt,SQL_RESET_PARAMS);
    SQLFreeStmt(hstmt,SQL_CLOSE);

    rc = SQLEndTran(SQL_HANDLE_DBC,hdbc,SQL_COMMIT);
    mycon(hdbc,rc);
  
    mydisconnect(&henv,&hdbc,&hstmt);
}

/**
  Fetch and count the time
*/
SQLINTEGER t_fetch_data(SQLHDBC hdbc,SQLHSTMT hstmt)
{
  SQLRETURN   rc;
  SQLINTEGER  row_count=0;

    /* set row size as 1 */
    rc = SQLSetStmtAttr(hstmt,SQL_ATTR_ROW_ARRAY_SIZE,(SQLPOINTER)1,0);
    mystmt(hstmt,rc);
    
    SQLSetStmtAttr(hstmt, SQL_ATTR_CONCURRENCY, (SQLPOINTER) SQL_CONCUR_ROWVER, 0);
    SQLSetStmtAttr(hstmt, SQL_ATTR_CURSOR_TYPE, (SQLPOINTER) SQL_CURSOR_FORWARD_ONLY, 0);

    t_start = clock();
    rc = SQLExecDirect(hstmt,"select * from client_odbc_test1.t_use_result",SQL_NTS);
    mystmt(hstmt,rc);
    t_end = clock();

    my_time = difftime(t_end,t_start)/CLOCKS_PER_SEC;   
    myprintf("\n SELECT statement executed in %5.3f SECS",my_time);
  
    t_start = clock();
    rc = SQLFetch(hstmt);
    while(rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
    {
       row_count++;
       rc = SQLFetch(hstmt);
    }
    t_end = clock();  

    my_time = difftime(t_end,t_start)/CLOCKS_PER_SEC; 
    myprintf("\n total '%d' rows fetched in %5.3f SECS",row_count,my_time);

    SQLFreeStmt(hstmt,SQL_UNBIND);    
    SQLFreeStmt(hstmt,SQL_CLOSE);  
    return(row_count);
}

/**
  making use of mysql_use_result
*/
void t_use_result()
{
  SQLHENV   henv1;
  SQLHDBC   hdbc1;
  SQLHSTMT  hstmt1;
  SQLCHAR   conn[255];
  SQLINTEGER option = 131072L * 8;

  myheader("t_use_result");
    
    sprintf(conn,"DSN=%s;UID=%s;PWD=%s;OPTION=%d;",
        mydsn,myuid,mypwd,option);
    mydrvconnect(&henv1,&hdbc1,&hstmt1,conn);
    my_assert(t_fetch_data(hdbc1,hstmt1) == my_max_rows); 
    mydisconnect(&henv1,&hdbc1,&hstmt1);
}

/**
  making use of mysql_store_result
*/
void t_store_result()
{
  SQLHENV   henv1;
  SQLHDBC   hdbc1;
  SQLHSTMT  hstmt1;
  SQLCHAR   conn[255];
  SQLINTEGER option = 3;

  myheader("t_store_result");

    sprintf(conn,"DSN=%s;UID=%s;PWD=%s;OPTION=%d;",
        mydsn,myuid,mypwd,option);
    mydrvconnect(&henv1,&hdbc1,&hstmt1,conn);
    my_assert(t_fetch_data(hdbc1,hstmt1) == my_max_rows); 
    mydisconnect(&henv1,&hdbc1,&hstmt1);
}

/**
MAIN ROUTINE...
*/
int main(int argc, char *argv[])
{
  SQLINTEGER narg;      
  
    /*
     *  show the usage string when the user asks for this
    */    
      myprintf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
      myprintf("usage: my_use_result [max_rows][DSN] [UID] [PWD] \n\n");  
      myprintf("  max_rows    <-- maximum rows for test\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 )
        my_max_rows = atoi(argv[1]);    
      if ( narg == 2 )
        mydsn = argv[2];
      else if ( narg == 3 )
        myuid = argv[3];
      else if ( narg == 4 )
        mypwd = argv[4];                
    }
  
    t_init_data();
    t_use_result();
    t_store_result();
    t_clean_data();

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