File: TestSQLDatabaseSchema.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (294 lines) | stat: -rw-r--r-- 9,126 bytes parent folder | download | duplicates (3)
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
/*=========================================================================

Program:   Visualization Toolkit
Module:    TestSQLDatabaseSchema.cxx

Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/
// .SECTION Thanks
// Thanks to Philippe Pebay and David Thompson from Sandia National Laboratories
// for implementing this test.

#include "vtkSQLDatabaseSchema.h"
#include "DatabaseSchemaWith2Tables.h"
#include "vtkStdString.h"

#include <set>

int TestSQLDatabaseSchema( int /*argc*/, char* /*argv*/[] )
{
  bool status = true;

  // 1. Create the schema
  DatabaseSchemaWith2Tables schema;

  // 2. Check the schema

  // Define the correct (reference) columns and types
  std::set<vtkStdString> preNames;
  preNames.insert( vtkStdString( "dropplpgsql" ) );
  preNames.insert( vtkStdString( "loadplpgsql" ) );
  preNames.insert( vtkStdString( "createsomefunction" ) );
  std::multiset<vtkStdString> preBackends;
  preBackends.insert( vtkStdString( VTK_SQL_POSTGRESQL ) );
  preBackends.insert( vtkStdString( VTK_SQL_POSTGRESQL ) );
  preBackends.insert( vtkStdString( VTK_SQL_POSTGRESQL ) );

  // Loop over all preambles
  int numPre = schema->GetNumberOfPreambles();
  if ( numPre != 3 )
  {
    cerr << "Read " << numPre << " != 3 preamble in test schema.\n";
    status = false;
  }

  for ( int preHandle = 0; preHandle < numPre; ++ preHandle )
  {
    vtkStdString preName = schema->GetPreambleNameFromHandle( preHandle );
    cerr << "Preamble name: "
         << preName
         << "\n";

    std::set<vtkStdString>::iterator sit = preNames.find( preName );
    if ( sit != preNames.end() )
    {
      preNames.erase ( sit );
    }
    else
    {
      cerr << "Could not retrieve preamble name " << preName  << " from test schema.\n";
      status = false;
    }

    vtkStdString preBackend = schema->GetPreambleBackendFromHandle( preHandle );
    cerr << "Preamble backend: "
         << preBackend
         << "\n";

    std::multiset<vtkStdString>::iterator mit = preBackends.find( preBackend );
    if ( mit != preBackends.end() )
    {
      preBackends.erase ( mit );
    }
    else
    {
      cerr << "Could not retrieve preamble backend " << preBackend  << " from test schema.\n";
      status = false;
    }
  }

  // Define the correct (reference) columns and types
  std::set<vtkStdString> colNames;
  colNames.insert( vtkStdString( "somenmbr" ) );
  colNames.insert( vtkStdString( "somename" ) );
  colNames.insert( vtkStdString( "tablekey" ) );
  std::set<int> colTypes;
  colTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::BIGINT ) );
  colTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::SERIAL ) );
  colTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::VARCHAR ) );

  // Loop over all columns of the first table
  int tblHandle = 0;
  int numCol = schema->GetNumberOfColumnsInTable( tblHandle );
  if ( numCol != 3 )
  {
    cerr << "Read " << numCol << " != 3 columns in test schema.\n";
    status = false;
  }

  for ( int colHandle = 0; colHandle < numCol; ++ colHandle )
  {
    vtkStdString colName = schema->GetColumnNameFromHandle( tblHandle, colHandle );
    cerr << "Column name: "
         << colName
         << "\n";

    std::set<vtkStdString>::iterator sit = colNames.find( colName );
    if ( sit != colNames.end() )
    {
      colNames.erase ( sit );
    }
    else
    {
      cerr << "Could not retrieve column name " << colName  << " from test schema.\n";
      status = false;
    }

    int colType = schema->GetColumnTypeFromHandle( tblHandle, colHandle );
    cerr << "Column type: "
         << colType
         << "\n";

    std::set<int>::iterator iit = colTypes.find( colType );
    if ( iit != colTypes.end() )
    {
      colTypes.erase ( iit );
    }
    else
    {
      cerr << "Could not retrieve column type " << colType  << " from test schema.\n";
      status = false;
    }
  }

  // Define the correct (reference) indices and types
  std::set<vtkStdString> idxNames;
  idxNames.insert( vtkStdString ( "bigkey" ) );
  idxNames.insert( vtkStdString ( "reverselookup" ) );
  std::set<int> idxTypes;
  idxTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::PRIMARY_KEY ) );
  idxTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::UNIQUE ) );

  // Loop over all indices of the previously created table
  int numIdx = schema->GetNumberOfIndicesInTable( tblHandle );
  if ( numIdx != 2 )
  {
    cerr << "Read " << numIdx << " != 2 indices in test schema.\n";
    status = false;
  }

  for ( int idxHandle = 0; idxHandle < numIdx; ++ idxHandle )
  {
    vtkStdString idxName = schema->GetIndexNameFromHandle( tblHandle, idxHandle );
    cerr << "Index name: "
         << idxName
         << "\n";

    std::set<vtkStdString>::iterator sit = idxNames.find( idxName );
    if ( sit != idxNames.end() )
    {
      idxNames.erase ( sit );
    }
    else
    {
      cerr << "Could not retrieve index name " << idxName  << " from test schema.\n";
      status = false;
    }

    int idxType = schema->GetIndexTypeFromHandle( tblHandle, idxHandle );
    cerr << "Index type: "
         << idxType
         << "\n";

    std::set<int>::iterator iit = idxTypes.find( idxType );
    if ( iit != idxTypes.end() )
    {
      idxTypes.erase ( iit );
    }
    else
    {
      cerr << "Could not retrieve index type " << idxType  << " from test schema.\n";
      status = false;
    }
  }

  // Define the correct (reference) triggers and types
  std::multiset<vtkStdString> trgNames;
  trgNames.insert( vtkStdString ( "inserttrigger" ) );
  trgNames.insert( vtkStdString ( "inserttrigger" ) );
  trgNames.insert( vtkStdString ( "inserttrigger" ) );

  std::multiset<int> trgTypes;
  trgTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::AFTER_INSERT ) );
  trgTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::AFTER_INSERT ) );
  trgTypes.insert( static_cast<int>( vtkSQLDatabaseSchema::AFTER_INSERT ) );

  std::multiset<vtkStdString> trgActions;
  trgActions.insert( vtkStdString( "DO NOTHING" ) );
  trgActions.insert( vtkStdString( "FOR EACH ROW INSERT INTO btable SET somevalue = NEW.somenmbr" ) );
  trgActions.insert( vtkStdString( "FOR EACH ROW EXECUTE PROCEDURE somefunction ()" ) );

  std::multiset<vtkStdString> trgBackends;
  trgBackends.insert( vtkStdString( VTK_SQL_MYSQL ) );
  trgBackends.insert( vtkStdString( VTK_SQL_SQLITE ) );
  trgBackends.insert( vtkStdString( VTK_SQL_POSTGRESQL ) );

  // Loop over all triggers of the previously created table
  int numTrg = schema->GetNumberOfTriggersInTable( tblHandle );
  if ( numTrg != 3 )
  {
    cerr << "Read " << numTrg << " != 3 triggers in test schema.\n";
    status = false;
  }

  for ( int trgHandle = 0; trgHandle < numTrg; ++ trgHandle )
  {
    vtkStdString trgName = schema->GetTriggerNameFromHandle( tblHandle, trgHandle );
    cerr << "Trigger name: "
         << trgName
         << "\n";

    std::multiset<vtkStdString>::iterator sit = trgNames.find( trgName );
    if ( sit != trgNames.end() )
    {
      trgNames.erase ( sit );
    }
    else
    {
      cerr << "Could not retrieve trigger name " << trgName  << " from test schema.\n";
      status = false;
    }

    int trgType = schema->GetTriggerTypeFromHandle( tblHandle, trgHandle );
    cerr << "Trigger type: "
         << trgType
         << "\n";

    std::multiset<int>::iterator iit = trgTypes.find( trgType );
    if ( iit != trgTypes.end() )
    {
      trgTypes.erase ( iit );
    }
    else
    {
      cerr << "Could not retrieve trigger type " << trgType  << " from test schema.\n";
      status = false;
    }

    vtkStdString trgAction = schema->GetTriggerActionFromHandle( tblHandle, trgHandle );
    cerr << "Trigger action: "
         << trgAction
         << "\n";

    sit = trgActions.find( trgAction );
    if ( sit != trgActions.end() )
    {
      trgActions.erase ( sit );
    }
    else
    {
      cerr << "Could not retrieve trigger action " << trgAction  << " from test schema.\n";
      status = false;
    }

    vtkStdString trgBackend = schema->GetTriggerBackendFromHandle( tblHandle, trgHandle );
    cerr << "Trigger backend: "
         << trgBackend
         << "\n";

    sit = trgBackends.find( trgBackend );
    if ( sit != trgBackends.end() )
    {
      trgBackends.erase ( sit );
    }
    else
    {
      cerr << "Could not retrieve trigger backend " << trgBackend  << " from test schema.\n";
      status = false;
    }
  }

  return status ? 0 : 1;
}