~mysql/mysql-connector-cpp/trunk

« back to all changes in this revision

Viewing changes to examples/prepared_statement.cpp

  • Committer: Hemant Dangi
  • Date: 2014-12-17 16:28:39 UTC
  • Revision ID: hemant.dangi@oracle.com-20141217162839-sdbpj3fcwn5gxjxh
Bug #75251: Add C++11 support and replace (deprecated in C++11) auto_ptr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
 
2
Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
3
3
 
4
4
The MySQL Connector/C++ is licensed under the terms of the GPLv2
5
5
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
63
63
#include <sstream>
64
64
#include <stdexcept>
65
65
 
 
66
#include <boost/scoped_ptr.hpp>
 
67
 
66
68
/*
67
69
  Public interface of the MySQL Connector/C++.
68
70
  You might not use it but directly include directly the different
73
75
/* Connection parameter and sample data */
74
76
#include "examples.h"
75
77
 
76
 
bool prepare_execute(std::auto_ptr< sql::Connection > & con, const char *sql);
77
 
sql::Statement* emulate_prepare_execute(std::auto_ptr< sql::Connection > & con, const char *sql);
 
78
bool prepare_execute(boost::scoped_ptr< sql::Connection > & con, const char *sql);
 
79
sql::Statement* emulate_prepare_execute(boost::scoped_ptr< sql::Connection > & con, const char *sql);
78
80
 
79
81
using namespace std;
80
82
 
100
102
        try {
101
103
                /* Using the Driver to create a connection */
102
104
                driver = sql::mysql::get_driver_instance();
103
 
                std::auto_ptr< sql::Connection > con(driver->connect(url, user, pass));
 
105
                boost::scoped_ptr< sql::Connection > con(driver->connect(url, user, pass));
104
106
 
105
107
                /* The usage of USE is not supported by the prepared statement protocol */
106
 
                std::auto_ptr< sql::Statement > stmt(con->createStatement());
 
108
                boost::scoped_ptr< sql::Statement > stmt(con->createStatement());
107
109
                stmt->execute("USE " + database);
108
110
 
109
111
                /*
124
126
                the example program will continue to do it to demonstrate the (ab)use of
125
127
                prepared statements (and to prove that you really can do more than SELECT with PS).
126
128
                */
127
 
                std::auto_ptr< sql::PreparedStatement > prep_stmt(con->prepareStatement("DROP TABLE IF EXISTS test"));
 
129
                boost::scoped_ptr< sql::PreparedStatement > prep_stmt(con->prepareStatement("DROP TABLE IF EXISTS test"));
128
130
                prep_stmt->execute();
129
131
 
130
132
                prepare_execute(con, "CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, label CHAR(1))");
153
155
                cout << "#\t Test table populated" << endl;
154
156
 
155
157
                /* We will reuse the SELECT a bit later... */
156
 
                std::auto_ptr< sql::PreparedStatement > prep_select(con->prepareStatement("SELECT id, label FROM test ORDER BY id ASC"));
 
158
                boost::scoped_ptr< sql::PreparedStatement > prep_select(con->prepareStatement("SELECT id, label FROM test ORDER BY id ASC"));
157
159
                cout << "#\t Running 'SELECT id, label FROM test ORDER BY id ASC'" << endl;
158
 
                std::auto_ptr< sql::ResultSet > res(prep_select->executeQuery());
 
160
                boost::scoped_ptr< sql::ResultSet > res(prep_select->executeQuery());
159
161
                row = 0;
160
162
                while (res->next()) {
161
163
                        cout << "#\t\t Row " << row << " - id = " << res->getInt("id");
278
280
}
279
281
 
280
282
 
281
 
bool prepare_execute(std::auto_ptr< sql::Connection > & con, const char *sql)
 
283
bool prepare_execute(boost::scoped_ptr< sql::Connection > & con, const char *sql)
282
284
{
283
285
        sql::PreparedStatement * prep_stmt;
284
286
 
290
292
}
291
293
 
292
294
 
293
 
sql::Statement* emulate_prepare_execute(std::auto_ptr< sql::Connection > & con, const char *sql)
 
295
sql::Statement* emulate_prepare_execute(boost::scoped_ptr< sql::Connection > & con, const char *sql)
294
296
{
295
297
        sql::PreparedStatement *prep_stmt;
296
298
        sql::Statement *stmt = NULL;