~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to library/dbc/unit-tests/dbc_general_test.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU General Public License as
 
6
 * published by the Free Software Foundation; version 2 of the
 
7
 * License.
 
8
 * 
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
12
 * GNU General Public License for more details.
 
13
 * 
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#include "tut_stdafx.h"
 
21
 
 
22
#include "connection_helpers.h"
 
23
 
 
24
#define VERBOSE_TESTING 0
 
25
 
 
26
using namespace std;
 
27
 
 
28
BEGIN_TEST_DATA_CLASS(module_dbc_general_test)
 
29
public:
 
30
  GRT grt;
 
31
END_TEST_DATA_CLASS
 
32
 
 
33
TEST_MODULE(module_dbc_general_test, "DBC: general tests");
 
34
 
 
35
TEST_FUNCTION(1)
 
36
{
 
37
  // load structs
 
38
  grt.scan_metaclasses_in("../../res/grt/");
 
39
  grt.end_loading_metaclasses();
 
40
 
 
41
  ensure_equals("load structs", grt.get_metaclasses().size(), INT_METACLASS_COUNT);
 
42
}
 
43
 
 
44
TEST_FUNCTION(2)
 
45
{
 
46
  db_mgmt_ConnectionRef connectionProperties(&grt);
 
47
 
 
48
  setup_env(&grt, connectionProperties);
 
49
 
 
50
  sql::DriverManager *dm= sql::DriverManager::getDriverManager();
 
51
  sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
52
  sql::Connection* connection= wrapper.get();
 
53
  sql::DatabaseMetaData *meta(connection->getMetaData());
 
54
  std::auto_ptr<sql::ResultSet> rset(meta->getSchemata());
 
55
 
 
56
  while (rset->next())
 
57
  {
 
58
#if VERBOSE_TESTING
 
59
    std::cout << rset->getString("schema_name") << std::endl;
 
60
    std::cout << "  Schema Objects:" << std::endl;
 
61
 
 
62
#endif
 
63
    
 
64
    std::auto_ptr<sql::ResultSet> rset2(meta->getSchemaObjects("", rset->getString("database")));
 
65
    while (rset2->next())
 
66
    {
 
67
#if VERBOSE_TESTING
 
68
      std::cout << rset2->getString("object_type") << ": " << rset2->getString("name") << "," << 
 
69
        rset2->getString("ddl") << std::endl;
 
70
#endif
 
71
    }
 
72
  }
 
73
}
 
74
 
 
75
TEST_FUNCTION(3)
 
76
{
 
77
  db_mgmt_ConnectionRef connectionProperties(&grt);
 
78
 
 
79
  setup_env(&grt, connectionProperties);
 
80
 
 
81
  sql::DriverManager *dm= sql::DriverManager::getDriverManager();
 
82
  sql::ConnectionWrapper wrapper= dm->getConnection(connectionProperties);
 
83
  sql::Connection* connection= wrapper.get();
 
84
 
 
85
  std::auto_ptr<sql::Statement> stmt(connection->createStatement());
 
86
 
 
87
  stmt->execute("DROP TABLE IF EXISTS test.product");
 
88
  stmt->execute("CREATE TABLE test.product(idproduct INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(80))");
 
89
 
 
90
  connection->setAutoCommit(0);
 
91
 
 
92
#if VERBOSE_TESTING
 
93
  std::cout << "Insert Data." << std:endl;
 
94
#endif
 
95
 
 
96
  std::auto_ptr<sql::PreparedStatement> prepStmt(connection->prepareStatement("INSERT INTO test.product(idproduct, name)  VALUES(?, ?)"));
 
97
  prepStmt->setInt(1, 1);
 
98
  prepStmt->setString(2, "Harry Potter");
 
99
  prepStmt->executeUpdate();
 
100
 
 
101
#if VERBOSE_TESTING
 
102
  std::cout << "Display Data." << std::endl;
 
103
#endif
 
104
 
 
105
  std::auto_ptr<sql::ResultSet> rset1(stmt->executeQuery("SELECT * FROM test.product"));
 
106
 
 
107
  int i = 0;
 
108
  while (rset1->next())
 
109
  {
 
110
#if VERBOSE_TESTING
 
111
    std::cout << rset1->getString(2) << ", " << rset1->getString("name") << std::endl;
 
112
#endif
 
113
    i++;
 
114
  }
 
115
#if VERBOSE_TESTING
 
116
  printf("%d row(s)", i);
 
117
 
 
118
  printf("Rollback");
 
119
#endif
 
120
 
 
121
  connection->rollback();
 
122
 
 
123
#if VERBOSE_TESTING
 
124
  printf("Display Data Again.\n");
 
125
#endif
 
126
  std::auto_ptr<sql::ResultSet> rset2(stmt->executeQuery("SELECT * FROM test.product"));
 
127
 
 
128
  i = 0;
 
129
  while (rset2->next())
 
130
  {
 
131
#if VERBOSE_TESTING
 
132
    std::cout << rset2->getString(2) << ", " << rset2->getString("name") << std::endl;
 
133
#endif
 
134
    i++;
 
135
  }
 
136
#if VERBOSE_TESTING
 
137
  std::cout << i << " row(s)" << std::endl;
 
138
#endif
 
139
}
 
140
 
 
141
END_TESTS