File: vtkSQLDatabaseTableSource.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 (284 lines) | stat: -rw-r--r-- 7,708 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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkSQLDatabaseTableSource.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 (c) Sandia Corporation
 See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/

#include "vtkSQLDatabaseTableSource.h"

#include "vtkDataSetAttributes.h"
#include "vtkEventForwarderCommand.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkRowQueryToTable.h"
#include "vtkSQLDatabase.h"
#include "vtkSQLQuery.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"

//---------------------------------------------------------------------------
class vtkSQLDatabaseTableSource::implementation
{
public:
  implementation() :
    Database(0),
    Query(0),
    Table(0)
  {
  }

  ~implementation()
  {
    if(this->Table)
      this->Table->Delete();

    if(this->Query)
      this->Query->Delete();

    if(this->Database)
      this->Database->Delete();
  }

  vtkStdString URL;
  vtkStdString Password;
  vtkStdString QueryString;

  vtkSQLDatabase* Database;
  vtkSQLQuery* Query;
  vtkRowQueryToTable* Table;
};

vtkStandardNewMacro(vtkSQLDatabaseTableSource);

//---------------------------------------------------------------------------
vtkSQLDatabaseTableSource::vtkSQLDatabaseTableSource() :
  Implementation(new implementation())
{
  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
  this->PedigreeIdArrayName = 0;
  this->SetPedigreeIdArrayName("id");
  this->GeneratePedigreeIds = true;

  // Set up eventforwarder
  this->EventForwarder = vtkEventForwarderCommand::New();
  this->EventForwarder->SetTarget(this);
}

//---------------------------------------------------------------------------
vtkSQLDatabaseTableSource::~vtkSQLDatabaseTableSource()
{
  delete this->Implementation;
  this->SetPedigreeIdArrayName(0);
  this->EventForwarder->Delete();
}

//---------------------------------------------------------------------------
void vtkSQLDatabaseTableSource::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "URL: " << this->Implementation->URL << endl;
  os << indent << "Query: " << this->Implementation->QueryString << endl;
  os << indent << "GeneratePedigreeIds: " << this->GeneratePedigreeIds << endl;
  os << indent << "PedigreeIdArrayName: " << this->PedigreeIdArrayName << endl;
}

vtkStdString vtkSQLDatabaseTableSource::GetURL()
{
  return this->Implementation->URL;
}

void vtkSQLDatabaseTableSource::SetURL(const vtkStdString& url)
{
  if(url == this->Implementation->URL)
    return;

  if(this->Implementation->Query)
  {
    this->Implementation->Query->Delete();
    this->Implementation->Query = 0;
  }

  if(this->Implementation->Database)
  {
    this->Implementation->Database->Delete();
    this->Implementation->Database = 0;
  }

  this->Implementation->URL = url;

  this->Modified();
}

void vtkSQLDatabaseTableSource::SetPassword(const vtkStdString& password)
{
  if(password == this->Implementation->Password)
    return;

  if(this->Implementation->Query)
  {
    this->Implementation->Query->Delete();
    this->Implementation->Query = 0;
  }

  if(this->Implementation->Database)
  {
    this->Implementation->Database->Delete();
    this->Implementation->Database = 0;
  }

  this->Implementation->Password = password;

  this->Modified();
}

vtkStdString vtkSQLDatabaseTableSource::GetQuery()
{
  return this->Implementation->QueryString;
}

void vtkSQLDatabaseTableSource::SetQuery(const vtkStdString& query)
{
  if(query == this->Implementation->QueryString)
    return;

  this->Implementation->QueryString = query;
  this->Modified();
}

//---------------------------------------------------------------------------
int vtkSQLDatabaseTableSource::RequestData(
  vtkInformation*,
  vtkInformationVector**,
  vtkInformationVector* outputVector)
{
  if(this->Implementation->URL.empty())
    return 1;

  if(this->Implementation->QueryString.empty())
    return 1;

  if(!this->PedigreeIdArrayName)
  {
    vtkErrorMacro(<< "You must specify a pedigree id array name.");
    return 0;
  }

  if(!this->Implementation->Database)
  {
    this->Implementation->Database = vtkSQLDatabase::CreateFromURL(this->Implementation->URL);
    if(!this->Implementation->Database)
    {
      vtkErrorMacro(<< "Error creating database using URL: " << this->Implementation->URL.c_str());
      return 0;
    }

    if(!this->Implementation->Database->Open(this->Implementation->Password))
    {
      this->Implementation->Database->Delete();
      this->Implementation->Database = 0;

      vtkErrorMacro(<< "Error opening database: " << this->Implementation->URL.c_str());
      return 0;
    }
  }

  if(!this->Implementation->Query)
  {
    this->Implementation->Query = this->Implementation->Database->GetQueryInstance();
    if(!this->Implementation->Query)
    {
      vtkErrorMacro(<< "Internal error creating query instance.");
      return 0;
    }
  }

  // Set Progress Text
  this->SetProgressText("DatabaseTableSource");

  // I have a database: 5% progress
  this->UpdateProgress(.05);

  this->Implementation->Query->SetQuery(this->Implementation->QueryString.c_str());
  if(!this->Implementation->Query->Execute())
  {
    vtkErrorMacro(<< "Error executing query: " << this->Implementation->QueryString.c_str());
    return 0;
  }

  // Executed query: 33% progress
  this->UpdateProgress(.33);

  // Set Progress Text
  this->SetProgressText("DatabaseTableSource: RowQueryToTable");

  if(!this->Implementation->Table)
  {
    this->Implementation->Table = vtkRowQueryToTable::New();

    // Now forward progress events from the graph layout
    this->Implementation->Table->AddObserver(vtkCommand::ProgressEvent,
                                 this->EventForwarder);
  }
  this->Implementation->Table->SetQuery(this->Implementation->Query);
  this->Implementation->Table->Update();

  // Created Table: 66% progress
  this->SetProgressText("DatabaseTableSource");
  this->UpdateProgress(.66);

  vtkTable* const output = vtkTable::SafeDownCast(
    outputVector->GetInformationObject(0)->Get(vtkDataObject::DATA_OBJECT()));

  output->ShallowCopy(this->Implementation->Table->GetOutput());

  if (this->GeneratePedigreeIds)
  {
    vtkSmartPointer<vtkIdTypeArray> pedigreeIds =
      vtkSmartPointer<vtkIdTypeArray>::New();
    vtkIdType numRows = output->GetNumberOfRows();
    pedigreeIds->SetNumberOfTuples(numRows);
    pedigreeIds->SetName(this->PedigreeIdArrayName);
    for (vtkIdType i = 0; i < numRows; ++i)
    {
      pedigreeIds->InsertValue(i, i);
    }
    output->GetRowData()->SetPedigreeIds(pedigreeIds);
  }
  else
  {
    vtkAbstractArray* arr =
      output->GetColumnByName(this->PedigreeIdArrayName);
    if (arr)
    {
      output->GetRowData()->SetPedigreeIds(arr);
    }
    else
    {
      vtkErrorMacro(<< "Could find pedigree id array: "
        << this->PedigreeIdArrayName);
      return 0;
    }
  }

  // Done: 100% progress
  this->UpdateProgress(1);

  return 1;
}