File: TestSQLDatabaseSchema.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (257 lines) | stat: -rw-r--r-- 7,951 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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
// .SECTION Thanks
// Thanks to Philippe Pebay and David Thompson from Sandia National Laboratories
// for implementing this test.

#include "DatabaseSchemaWith2Tables.h"
#include "vtkSQLDatabaseSchema.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<std::string> preNames;
  preNames.insert(std::string("dropplpgsql"));
  preNames.insert(std::string("loadplpgsql"));
  preNames.insert(std::string("createsomefunction"));
  std::multiset<std::string> preBackends;
  preBackends.insert(std::string(VTK_SQL_POSTGRESQL));
  preBackends.insert(std::string(VTK_SQL_POSTGRESQL));
  preBackends.insert(std::string(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)
  {
    std::string preName = schema->GetPreambleNameFromHandle(preHandle);
    cerr << "Preamble name: " << preName << "\n";

    std::set<std::string>::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;
    }

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

    std::multiset<std::string>::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<std::string> colNames;
  colNames.insert(std::string("somenmbr"));
  colNames.insert(std::string("somename"));
  colNames.insert(std::string("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)
  {
    std::string colName = schema->GetColumnNameFromHandle(tblHandle, colHandle);
    cerr << "Column name: " << colName << "\n";

    std::set<std::string>::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<std::string> idxNames;
  idxNames.insert(std::string("bigkey"));
  idxNames.insert(std::string("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)
  {
    std::string idxName = schema->GetIndexNameFromHandle(tblHandle, idxHandle);
    cerr << "Index name: " << idxName << "\n";

    std::set<std::string>::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<std::string> trgNames;
  trgNames.insert(std::string("inserttrigger"));
  trgNames.insert(std::string("inserttrigger"));
  trgNames.insert(std::string("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<std::string> trgActions;
  trgActions.insert(std::string("DO NOTHING"));
  trgActions.insert(std::string("FOR EACH ROW INSERT INTO btable SET somevalue = NEW.somenmbr"));
  trgActions.insert(std::string("FOR EACH ROW EXECUTE PROCEDURE somefunction ()"));

  std::multiset<std::string> trgBackends;
  trgBackends.insert(std::string(VTK_SQL_MYSQL));
  trgBackends.insert(std::string(VTK_SQL_SQLITE));
  trgBackends.insert(std::string(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)
  {
    std::string trgName = schema->GetTriggerNameFromHandle(tblHandle, trgHandle);
    cerr << "Trigger name: " << trgName << "\n";

    std::multiset<std::string>::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;
    }

    std::string 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;
    }

    std::string 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;
}