File: my_blob.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 (229 lines) | stat: -rw-r--r-- 7,675 bytes parent folder | download
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
226
227
228
229
/***************************************************************************
                          my_blob.c  -  description
                             -------------------
    begin                : Tue Oct 16 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"

/**
BLOB SPEED TEST
*/
void t_blob(SQLHDBC hdbc, SQLHSTMT hstmt, SQLUINTEGER blob_size)
{
  SQLRETURN rc;  
  SQLUINTEGER cbValue;
  SQLUINTEGER j= 0;
  SQLINTEGER l;
  SQLCHAR* blobbuf;
  SQLUINTEGER blobbuf_size = 1024 * 1 * 6L;
  SQLUINTEGER blob_read;
  SQLPOINTER token;
  clock_t start, finish;
  double duration;

  myheader("t_blob");
                
        rc = SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, 0L);
        mycon(hdbc,rc);

        rc = SQLExecDirect(hstmt,
                "DROP TABLE TBLOB",
                SQL_NTS);
        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);

        rc = SQLExecDirect(hstmt,
                "CREATE TABLE TBLOB (I INTEGER NOT NULL PRIMARY KEY, B LONGBLOB)",
                SQL_NTS);
        mystmt(hstmt,rc);
        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);
        mycon(hdbc,rc);

        cbValue = 0;
        rc = SQLPrepare(hstmt, "INSERT INTO TBLOB VALUES (1, ?)", SQL_NTS);
        mystmt(hstmt,rc);
        rc = SQLBindParameter(hstmt,SQL_PARAM_INPUT, 1, SQL_C_BINARY, SQL_LONGVARBINARY, 
                              blob_size, 0, NULL, 0, &cbValue);
        mystmt(hstmt,rc);
        cbValue = SQL_DATA_AT_EXEC;        
        blobbuf = (SQLCHAR *)malloc(blobbuf_size);
        memset(blobbuf, 'A', blobbuf_size);

		    start = clock();
        
		    rc = SQLExecute(hstmt);
        mystmt_err(hstmt,rc == SQL_NEED_DATA, rc);
        rc = SQLParamData(hstmt, &token);
        if (rc == SQL_NEED_DATA) {
            for (j = 0; j < blob_size; ) {
                SDWORD s;

                s = (SDWORD)blobbuf_size;
                if (s + j > blob_size) {
                    s -= (s + j) - blob_size;
                    myassert(s + j == blob_size);
                }
                rc = SQLPutData(hstmt, blobbuf, s);
                mystmt(hstmt,rc);
                j += (SQLUINTEGER)s;
            }
              rc = SQLParamData(hstmt, &token);
            mystmt(hstmt,rc);
        } else {
            my_error();
        }
        finish = clock();

        duration = (finish-start)/CLOCKS_PER_SEC;
        myprintf("\n j: %d", j);
        myassert(j == blob_size);
        myprintf("Wrote %ld bytes in %3.3lf seconds (%lg bytes/s)\n",
            j, duration, duration == 0.0 ? 9.99e99 : j / duration);

        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);
        mycon(hdbc,rc);

        rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
        mystmt(hstmt,rc);


        memset(blobbuf, ~0, 100);
        rc = SQLPrepare(hstmt, "SELECT I, B FROM TBLOB WHERE I = 1", SQL_NTS);
        mystmt(hstmt,rc);

		    start = clock();
        
        rc = SQLExecute(hstmt);
        mystmt(hstmt,rc);
        rc = SQLFetch(hstmt);
        mystmt(hstmt,rc);
        rc = SQLGetData(hstmt, 1, SQL_C_LONG, &l, 0L, &cbValue);
        mystmt(hstmt,rc);

        blob_read = 0L;
        do {
            rc = SQLGetData(hstmt, 2, SQL_C_BINARY, blobbuf, blobbuf_size, &cbValue);
            myassert(cbValue > 0);
            blob_read += (cbValue < blobbuf_size ? cbValue : blobbuf_size);
        } while (rc == SQL_SUCCESS_WITH_INFO);
        myassert(rc == SQL_SUCCESS);
        myassert(blob_read == blob_size);
        finish = clock();
        duration = (finish-start)/CLOCKS_PER_SEC;
        myprintf("Read  %ld bytes in %3.3lf seconds (%lg bytes/s)\n",
            blob_read, duration, duration == 0.0 ? 9.99e99 : blob_read / duration);

        rc = SQLFreeStmt(hstmt, SQL_CLOSE);
        mystmt(hstmt,rc);
        free(blobbuf);    
}
void t_1piecewrite2(HDBC hdbc, HSTMT hstmt)
{
  SQLRETURN rc;
  SQLINTEGER cbValue,cbValue2,l;
  SQLCHAR* blobbuf;
  size_t i;

  myheader("t_1piecewrite2");

        rc = SQLExecDirect(hstmt,
                "DROP TABLE TBLOB",
                SQL_NTS);
        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);

        rc = SQLExecDirect(hstmt,
                "CREATE TABLE TBLOB (I INTEGER NOT NULL PRIMARY KEY, B LONG VARCHAR NOT NULL)",
                SQL_NTS);
        mystmt(hstmt,rc);
        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);
        mycon(hdbc,rc);
        
        cbValue = 3510L;

        blobbuf = (SQLCHAR *)malloc((size_t)cbValue + 1);
        for (i = 0; i < (size_t)cbValue; i++) {
            blobbuf[i] = (char)((i % ('z' - 'a' + 1)) + 'a');
        }
        blobbuf[i] = '\0';
        l = 1;
        rc = SQLBindParameter(hstmt,SQL_PARAM_INPUT,1, SQL_C_LONG, SQL_INTEGER, 0, 0, &l,0, NULL);
        mystmt(hstmt,rc);
        rc = SQLBindParameter(hstmt,SQL_PARAM_INPUT, 2, SQL_C_CHAR, SQL_LONGVARCHAR, 0, 0, blobbuf,cbValue, NULL);
        mystmt(hstmt,rc);
        rc = SQLExecDirect(hstmt, "INSERT INTO TBLOB VALUES (1,?)", SQL_NTS);
        mystmt(hstmt,rc);
        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);
        mycon(hdbc,rc);
        memset(blobbuf, 1, (size_t)cbValue);
        rc = SQLFreeStmt(hstmt, SQL_RESET_PARAMS);
        mystmt(hstmt,rc);
        rc = SQLExecDirect(hstmt, "SELECT B FROM TBLOB WHERE I = 1", SQL_NTS);
        mystmt(hstmt,rc);
        rc = SQLFetch(hstmt);
        mystmt(hstmt,rc);
        rc = SQLGetData(hstmt, 1, SQL_C_BINARY, blobbuf, cbValue, &cbValue2);
        mystmt(hstmt,rc);
        myassert(cbValue2 == cbValue);
        for (i = 0; i < (size_t)cbValue; i++) {
            myassert(blobbuf[i] == (char)((i % ('z' - 'a' + 1)) + 'a'));
        }
        rc = SQLFreeStmt(hstmt, SQL_CLOSE);
        mystmt(hstmt,rc);
        rc = SQLTransact(NULL, hdbc, SQL_COMMIT);
        mycon(hdbc,rc);
        free(blobbuf);
}
/**
MAIN ROUTINE...
*/
int main(int argc, char *argv[])
{
  SQLHENV    henv;
  SQLHDBC    hdbc;
  SQLHSTMT   hstmt;
  SQLINTEGER narg;
  SQLUINTEGER blob_size = 1 * 1024L * 5L;  
  
    /*
     *  show the usage string when the user asks for this
    */    
      myprintf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
      myprintf("usage: my_blob [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];      
          
    }    

    myconnect(&henv,&hdbc,&hstmt);
    t_blob(hdbc,hstmt,blob_size);
    t_1piecewrite2(hdbc,hstmt);
    mydisconnect(&henv,&hdbc,&hstmt);

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