~mysql/mysql-connector-cpp/trunk

« back to all changes in this revision

Viewing changes to examples/exceptions.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
35
35
#include <sstream>
36
36
#include <stdexcept>
37
37
 
 
38
#include <boost/scoped_ptr.hpp>
 
39
 
38
40
/*
39
41
  Public interface of the MySQL Connector/C++.
40
42
  You might not use it but directly include directly the different
65
67
        try {
66
68
                /* Using the Driver to create a connection */
67
69
                driver = sql::mysql::get_driver_instance();
68
 
                std::auto_ptr< sql::Connection > con(driver->connect(host, user, pass));
 
70
                boost::scoped_ptr< sql::Connection > con(driver->connect(host, user, pass));
69
71
 
70
72
                /* Run in autocommit mode */
71
73
                con->setAutoCommit(1);
72
 
                std::auto_ptr< sql::Savepoint > savepoint(NULL);
 
74
                boost::scoped_ptr< sql::Savepoint > savepoint(NULL);
73
75
                try {
74
76
                        // It makes no sense to set a savepoint in autocommit mode
75
77
                        savepoint.reset(con->setSavepoint(string("before_insert")));
82
84
 
83
85
                con->setSchema(database);
84
86
 
85
 
                std::auto_ptr< sql::Statement > stmt(con->createStatement());
 
87
                boost::scoped_ptr< sql::Statement > stmt(con->createStatement());
86
88
                stmt->execute("DROP TABLE IF EXISTS test");
87
89
                stmt->execute("CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, label CHAR(1))");
88
90
                cout << "#\t Test table created" << endl;
106
108
                savepoint.reset(con->setSavepoint(string("before_insert")));
107
109
 
108
110
                {
109
 
                        std::auto_ptr< sql::PreparedStatement > prep_stmt(con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)"));
 
111
                        boost::scoped_ptr< sql::PreparedStatement > prep_stmt(con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)"));
110
112
                        for (i = 0; i < EXAMPLE_NUM_TEST_ROWS; i++) {
111
113
                                prep_stmt->setInt(1, test_data[i].id);
112
114
                                prep_stmt->setString(2, test_data[i].label);
115
117
                }
116
118
 
117
119
                try {
118
 
                        std::auto_ptr< sql::PreparedStatement > prep_stmt(con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)"));
 
120
                        boost::scoped_ptr< sql::PreparedStatement > prep_stmt(con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)"));
119
121
                        prep_stmt->setInt(1, test_data[0].id);
120
122
                        /* This will cause a duplicate index error */
121
123
                        prep_stmt->executeUpdate();