File: mysql_statement.cpp

package info (click to toggle)
mysql-connector-c%2B%2B 1.1.12-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 4,904 kB
  • sloc: cpp: 44,895; ansic: 2,114; php: 528; sql: 403; xml: 109; sh: 33; makefile: 11
file content (598 lines) | stat: -rw-r--r-- 15,779 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2.0, as
 * published by the Free Software Foundation.
 *
 * This program is also distributed with certain software (including
 * but not limited to OpenSSL) that is licensed under separate terms,
 * as designated in a particular file or component or in included license
 * documentation.  The authors of MySQL hereby grant you an
 * additional permission to link the program and your derivative works
 * with the separately licensed software that they have included with
 * MySQL.
 *
 * Without limiting anything contained in the foregoing, this file,
 * which is part of MySQL Connector/C++, is also subject to the
 * Universal FOSS Exception, version 1.0, a copy of which can be found at
 * http://oss.oracle.com/licenses/universal-foss-exception.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License, version 2.0, for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */



#include <memory>
#include <algorithm>
#include <sstream>

/*
 * mysql_util.h includes private_iface, ie libmysql headers. and they must go
 * prior to any header including config.h to avoid annoying redefenition warnings
 */
#include "mysql_util.h"

#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/warning.h>
#include "mysql_connection.h"
#include "mysql_statement.h"
#include "mysql_resultset.h"
#include "mysql_warning.h"
#include "nativeapi/native_connection_wrapper.h"
#include "nativeapi/native_resultset_wrapper.h"

#include "mysql_debug.h"

namespace sql
{
namespace mysql
{

/* {{{ MySQL_Statement::MySQL_Statement() -I- */
MySQL_Statement::MySQL_Statement(MySQL_Connection * conn, boost::shared_ptr< NativeAPI::NativeConnectionWrapper > & _proxy,
                  sql::ResultSet::enum_type rset_type, boost::shared_ptr< MySQL_DebugLogger > & l)
  : warnings(NULL), connection(conn), proxy(_proxy), isClosed(false), warningsHaveBeenLoaded(true),
  last_update_count(UL64(~0)), logger(l), resultset_type(rset_type), warningsCount(0)
{
  CPP_ENTER("MySQL_Statement::MySQL_Statement");
  CPP_INFO_FMT("this=%p", this);
}
/* }}} */


/* {{{ MySQL_Statement::~MySQL_Statement() -I- */
MySQL_Statement::~MySQL_Statement()
{
  CPP_ENTER("MySQL_Statement::~MySQL_Statement");
  CPP_INFO_FMT("this=%p", this);

  warnings.reset();
}
/* }}} */


/* {{{ MySQL_Statement::do_query() -I- */
/* All callers passed c_str from string, and we here created new string to pass to
   proxy->query. It didn't make much sense so changed interface here */
void
MySQL_Statement::do_query(const ::sql::SQLString &q)
{
  CPP_ENTER("MySQL_Statement::do_query");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();

  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy_p = proxy.lock();
  if (!proxy_p) {
    throw sql::InvalidInstanceException("Connection has been closed");
  }

  if (proxy_p->query(q) && proxy_p->errNo()) {
    CPP_ERR_FMT("Error during proxy->query : %d:(%s) %s", proxy_p->errNo(), proxy_p->sqlstate().c_str(), proxy_p->error().c_str());
    sql::mysql::util::throwSQLException(*proxy_p.get());
  }

  warningsCount= proxy_p->warning_count();

  warningsHaveBeenLoaded= false;
}
/* }}} */


/* {{{ MySQL_Statement::get_resultset() -I- */
boost::shared_ptr< NativeAPI::NativeResultsetWrapper >
MySQL_Statement::get_resultset()
{
  CPP_ENTER("MySQL_Statement::get_resultset");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();

  NativeAPI::NativeResultsetWrapper * result;

  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy_p = proxy.lock();
  if (!proxy_p) {
    throw sql::InvalidInstanceException("Connection has been closed");
  }

  //TODO: again - probably no need to catch-n-throw here. Or maybe no need to throw further
  try {
    result= (resultset_type == sql::ResultSet::TYPE_FORWARD_ONLY)
        ? proxy_p->use_result()
        : proxy_p->store_result();
    if (!result) {
      sql::mysql::util::throwSQLException(*proxy_p.get());
    }
  } catch (::sql::SQLException & e) {
    CPP_ERR_FMT("Error during %s_result : %d:(%s) %s", resultset_type == sql::ResultSet::TYPE_FORWARD_ONLY? "use":"store",
      proxy_p->errNo(), proxy_p->sqlstate().c_str(), proxy_p->error().c_str());
    throw e;
  }

  return boost::shared_ptr< NativeAPI::NativeResultsetWrapper >(result);
}
/* }}} */


/* {{{ MySQL_Statement::cancel() -U- */
void
MySQL_Statement::cancel()
{
  CPP_ENTER("MySQL_Statement::cancel");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::cancel");
}
/* }}} */


/* {{{ MySQL_Statement::execute() -I- */
bool
MySQL_Statement::execute(const sql::SQLString& sql)
{
  CPP_ENTER("MySQL_Statement::execute");
  CPP_INFO_FMT("this=%p", this);
  CPP_INFO_FMT("query=%s", sql.c_str());
  checkClosed();
  do_query(sql);
  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy_p = proxy.lock();
  if (!proxy_p) {
    throw sql::InvalidInstanceException("Connection has been closed");
  }
  bool ret = proxy_p->field_count() > 0;
  last_update_count = ret? UL64(~0):proxy_p->affected_rows();
  return ret;
}
/* }}} */


/* {{{ MySQL_Statement::executeQuery() -I- */
sql::ResultSet *
MySQL_Statement::executeQuery(const sql::SQLString& sql)
{
  CPP_ENTER("MySQL_Statement::executeQuery");
  CPP_INFO_FMT("this=%p", this);
  CPP_INFO_FMT("query=%s", sql.c_str());
  checkClosed();
  last_update_count = UL64(~0);
  do_query(sql);
  sql::ResultSet *tmp =
        new MySQL_ResultSet(
            get_resultset(),
            proxy,
            resultset_type==sql::ResultSet::TYPE_FORWARD_ONLY ? resultset_type : sql::ResultSet::TYPE_SCROLL_INSENSITIVE,
            this,
            logger
        );
  CPP_INFO_FMT("rset=%p", tmp);
  return tmp;
}
/* }}} */


/*{{{ sql::mysql::dirty_drop_resultset -I- */
void
dirty_drop_rs(boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy)
{
  boost::scoped_ptr<NativeAPI::NativeResultsetWrapper> result(proxy->store_result());
  // Destructor will do the job on result freeing
}

/* {{{ MySQL_Statement::executeUpdate() -I- */
int
MySQL_Statement::executeUpdate(const sql::SQLString& sql)
{
  CPP_ENTER("MySQL_Statement::executeUpdate");
  CPP_INFO_FMT("this=%p", this);
  CPP_INFO_FMT("query=%s", sql.c_str());
  checkClosed();

  do_query(sql);

  bool got_rs= false;

  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy_p = proxy.lock();
  if (!proxy_p) {
    throw sql::InvalidInstanceException("Connection has been closed");
  }

  do {
    if (proxy_p->field_count()) {
      /* We can't just throw - we need to walk through rest of results */
      got_rs= true;
      dirty_drop_rs(proxy_p);
    } else {
      /* We return update count for last query */
      last_update_count= proxy_p->affected_rows();
    }

    if (!proxy_p->more_results()) {
      if (got_rs){
        throw sql::InvalidArgumentException("Statement returning result set");
      } else {
        return static_cast<int>(last_update_count);
      }
    }

    switch (proxy_p->next_result()) {
    case 0:
      // There is next result and we go on next cycle iteration to process it
      break;
    case -1:
      throw sql::SQLException("Impossible! more_results() said true, next_result says no more results");
    default/* > 0 */:
      CPP_ERR_FMT("Error during executeUpdate : %d:(%s) %s", proxy_p->errNo(), proxy_p->sqlstate().c_str(), proxy_p->error().c_str());
      sql::mysql::util::throwSQLException(*proxy_p.get());
    }
  } while (1);

  /* Should not actually get here*/
  return 0;
}
/* }}} */


/* {{{ MySQL_Statement::getConnection() -I- */
sql::Connection *
MySQL_Statement::getConnection()
{
  CPP_ENTER("MySQL_Statement::getConnection");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  return connection;
}
/* }}} */


/* {{{ MySQL_Statement::getFetchSize() -U- */
size_t
MySQL_Statement::getFetchSize()
{
  CPP_ENTER("MySQL_Statement::getFetchSize");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::getFetchSize");
  return 0;
}
/* }}} */


/* {{{ MySQL_Statement::getResultSet() -I- */
sql::ResultSet *
MySQL_Statement::getResultSet()
{
  CPP_ENTER("MySQL_Statement::getResultSet");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();

  last_update_count = UL64(~0);

  boost::shared_ptr< NativeAPI::NativeResultsetWrapper > result;

  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy_p = proxy.lock();

  if (!proxy_p) {
    throw sql::InvalidInstanceException("Connection has been closed");
  }

  sql::ResultSet::enum_type tmp_type;

  try {
    NativeAPI::NativeResultsetWrapper * tmp_ptr;
    switch (resultset_type) {
      case sql::ResultSet::TYPE_FORWARD_ONLY:
        if (!(tmp_ptr = proxy_p->use_result())) {
          sql::mysql::util::throwSQLException(*proxy_p.get());
        }
        result.reset(tmp_ptr);
        tmp_type = sql::ResultSet::TYPE_FORWARD_ONLY;
        break;
      default:
        if (!(tmp_ptr = proxy_p->store_result())) {
          sql::mysql::util::throwSQLException(*proxy_p.get());
        }
        result.reset(tmp_ptr);
        tmp_type = sql::ResultSet::TYPE_SCROLL_INSENSITIVE;
    }
  } catch (::sql::SQLException & e ) {
    if (proxy_p->errNo() != 0)
    {
      CPP_ERR_FMT("Error during %s_result : %d:(%s) %s", resultset_type == sql::ResultSet::TYPE_FORWARD_ONLY? "use":"store",
        proxy_p->errNo(), proxy_p->sqlstate().c_str(), proxy_p->error().c_str());
      throw e;
    }
    else
    {
      return NULL;
    }
  }

  if (!result) {
    /* if there was an update then this method should return NULL and not throw */
    return NULL;
  }

  sql::ResultSet * ret = new MySQL_ResultSet(result, proxy, tmp_type, this, logger);

  CPP_INFO_FMT("res=%p", ret);
  return ret;
}
/* }}} */


/* {{{ MySQL_Statement::setFetchSize() -U- */
void
MySQL_Statement::setFetchSize(size_t /* fetch */)
{
  CPP_ENTER("MySQL_Statement::setFetchSize");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::setFetchSize");
}
/* }}} */


/* {{{ MySQL_Statement::setQueryTimeout() -U- */
void
MySQL_Statement::setQueryTimeout(unsigned int timeout)
{
  CPP_ENTER("MySQL_Statement::setQueryTimeout");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  connection->setSessionVariable("max_statement_time", timeout);
}
/* }}} */


/* {{{ MySQL_Statement::clearWarnings() -I- */
void
MySQL_Statement::clearWarnings()
{
  CPP_ENTER("MySQL_Statement::clearWarnings");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  warnings.reset();
}
/* }}} */


/* {{{ MySQL_Statement::close() -i- */
void
MySQL_Statement::close()
{
  CPP_ENTER("MySQL_Statement::close");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  clearWarnings();
  isClosed = true;
}
/* }}} */


/* {{{ MySQL_Statement::getMaxFieldSize() -U- */
unsigned int
MySQL_Statement::getMaxFieldSize()
{
  CPP_ENTER("MySQL_Statement::getMaxFieldSize");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::getMaxFieldSize");
  return 0;
}
/* }}} */


/* {{{ MySQL_Statement::getMaxRows() -U- */
uint64_t
MySQL_Statement::getMaxRows()
{
  CPP_ENTER("MySQL_Statement::getMaxRows");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::getMaxRows");
  return 0;
}
/* }}} */


/* {{{ MySQL_Statement::getMoreResults() -I- */
bool
MySQL_Statement::getMoreResults()
{
  CPP_ENTER("MySQL_Statement::getMoreResults");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();
  last_update_count = UL64(~0);
  boost::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy_p = proxy.lock();

  if (!proxy_p) {
    throw sql::InvalidInstanceException("Connection has been closed");
  }

  if (proxy_p->more_results()) {

    int next_result = proxy_p->next_result();

    if (next_result > 0) {
      CPP_ERR_FMT("Error during getMoreResults : %d:(%s) %s", proxy_p->errNo(), proxy_p->sqlstate().c_str(), proxy_p->error().c_str());
      sql::mysql::util::throwSQLException(*proxy_p.get());
    } else if (next_result == 0) {
      return proxy_p->field_count() != 0;
    } else if (next_result == -1) {
      throw sql::SQLException("Impossible! more_results() said true, next_result says no more results");
    }
  }
  return false;
}
/* }}} */


/* {{{ MySQL_Statement::getQueryTimeout() -U- */
unsigned int
MySQL_Statement::getQueryTimeout()
{
  checkClosed();
  sql::SQLString value= connection->getSessionVariable("max_statement_time");
  if (value.length() > 0) {
    unsigned int timeout;
    std::istringstream buffer(value);
    buffer >> timeout;
    if (buffer.rdstate() & std::istringstream::failbit) {
      return 0;
    } else {
      return timeout;
    }
  } else {
    return 0;
  }
}
/* }}} */


/* {{{ MySQL_Statement::getResultSetType() -I- */
sql::ResultSet::enum_type
MySQL_Statement::getResultSetType()
{
  CPP_ENTER("MySQL_Statement::getResultSetType");
  checkClosed();
  return resultset_type;
}
/* }}} */


/* {{{ MySQL_Statement::getUpdateCount() -I- */
uint64_t
MySQL_Statement::getUpdateCount()
{
  CPP_ENTER("MySQL_Statement::getUpdateCount");
  checkClosed();
  if (last_update_count == UL64(~0)) {
    return UL64(~0);
  }
  uint64_t ret = last_update_count;
  last_update_count = UL64(~0); /* the value will be returned once per result set */
  return ret;
}
/* }}} */


/* {{{ MySQL_Statement::getWarnings() -I- */
const SQLWarning *
MySQL_Statement::getWarnings()
{
  CPP_ENTER("MySQL_Statement::getWarnings");
  CPP_INFO_FMT("this=%p", this);
  checkClosed();

  if (!warningsHaveBeenLoaded)
  {
    warnings.reset(loadMysqlWarnings(connection, warningsCount));
    warningsHaveBeenLoaded= true;
  }

  return warnings.get();
}
/* }}} */


/* {{{ MySQL_Statement::setCursorName() -U- */
void
MySQL_Statement::setCursorName(const sql::SQLString &)
{
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::setCursorName");
}
/* }}} */


/* {{{ MySQL_Statement::setEscapeProcessing() -U- */
void
MySQL_Statement::setEscapeProcessing(bool)
{
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::setEscapeProcessing");
}
/* }}} */


/* {{{ MySQL_Statement::setMaxFieldSize() -U- */
void
MySQL_Statement::setMaxFieldSize(unsigned int)
{
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::setMaxFieldSize");
}
/* }}} */


/* {{{ MySQL_Statement::setMaxRows() -U- */
void
MySQL_Statement::setMaxRows(unsigned int)
{
  checkClosed();
  throw sql::MethodNotImplementedException("MySQL_Statement::setMaxRows");
}
/* }}} */


/* {{{ MySQL_Statement::setResultSetType() -I- */
sql::Statement *
MySQL_Statement::setResultSetType(sql::ResultSet::enum_type type)
{
  checkClosed();
  resultset_type = type;
  return this;
}
/* }}} */


/* {{{ MySQL_Statement::checkClosed() -I- */
void
MySQL_Statement::checkClosed()
{
  CPP_ENTER("MySQL_Statement::checkClosed");
  if (isClosed) {
    CPP_ERR("Statement has been closed");
    throw sql::InvalidInstanceException("Statement has been closed");
  }
}
/* }}} */

} /* namespace mysql */
} /* namespace sql */

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */