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
|
/*=========================================================================
Program: Visualization Toolkit
Module: TestODBCDatabase.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.
----------------------------------------------------------------------------*/
// .SECTION Thanks
// Thanks to Andrew Wilson from Sandia National Laboratories for implementing
// this test.
#include "vtkODBCDatabase.h"
#include "vtkSQLQuery.h"
#include "vtkRowQueryToTable.h"
#include "vtkStdString.h"
#include "vtkTable.h"
#include "vtkVariant.h"
#include "vtkVariantArray.h"
#include "vtkToolkits.h"
#include <vtksys/ios/sstream>
#define LONGSTRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
int TestODBCDatabase( int, char ** const )
{
vtkODBCDatabase* db = vtkODBCDatabase::New();
db->SetDataSourceName( VTK_ODBC_TEST_DSN );
bool status = db->Open(NULL);
if ( ! status )
{
cerr << "Couldn't open database. Error message: "
<< db->GetLastErrorText() << "\n";
return 1;
}
vtkSQLQuery* query = db->GetQueryInstance();
vtkStdString createQuery( "CREATE TABLE people (name VARCHAR(1024), age INTEGER, weight FLOAT)" );
cout << createQuery << endl;
query->SetQuery( createQuery.c_str() );
if ( !query->Execute() )
{
cerr << "Create query failed. Error message: "
<< query->GetLastErrorText() << endl;
return 1;
}
int i;
for ( i = 0; i < 20; ++ i )
{
vtksys_ios::ostringstream queryBuf;
queryBuf << "INSERT INTO people VALUES('John Doe "
<< i
<< "', " << i << ", " << 10*i + 0.5 << ")";
cout << queryBuf.str() << endl;
query->SetQuery( queryBuf.str().c_str() );
if ( !query->Execute() )
{
cerr << "Insert query " << i << " failed. Error message: "
<< query->GetLastErrorText() << endl;
return 1;
}
}
const char *placeholders = "INSERT INTO people (name, age, weight) VALUES (?, ?, ?)";
query->SetQuery(placeholders);
for ( i = 21; i < 40; i++ )
{
char name[20];
sprintf(name, "John Doe %d", i);
bool bind1 = query->BindParameter(0, name);
bool bind2 = query->BindParameter(1, i);
bool bind3 = query->BindParameter(2, 10.1*i);
if (!(bind1 && bind2 && bind3))
{
cerr << "Parameter binding failed on query " << i
<< ": " << bind1 << " " << bind2 << " " << bind3 << endl;
return 1;
}
cout << query->GetQuery() << endl;
if (!query->Execute())
{
cerr << "Insert query " << i << " failed" << endl;
return 1;
}
}
const char* queryText = "SELECT name, age, weight FROM people WHERE age <= 30";
query->SetQuery( queryText );
cerr << endl << "Running query: " << query->GetQuery() << endl;
cerr << endl << "Using vtkSQLQuery directly to execute query:" << endl;
if ( !query->Execute() )
{
cerr << "Query failed with error message " << query->GetLastErrorText() << endl;
return 1;
}
cerr << "Fields returned by query: ";
for ( int col = 0; col < query->GetNumberOfFields(); ++ col )
{
if ( col > 0 )
{
cerr << ", ";
}
cerr << query->GetFieldName( col );
}
cerr << endl;
int thisRow = 0;
while ( query->NextRow() )
{
cerr << "Row " << thisRow << ": ";
++thisRow;
for ( int field = 0; field < query->GetNumberOfFields(); ++ field )
{
if ( field > 0 )
{
cerr << ", ";
}
cerr << query->DataValue( field ).ToString().c_str();
}
cerr << endl;
}
cerr << endl << "Using vtkSQLQuery to execute query and retrieve by row:" << endl;
if ( !query->Execute() )
{
cerr << "Query failed with error message " << query->GetLastErrorText() << endl;
return 1;
}
for ( int col = 0; col < query->GetNumberOfFields(); ++ col )
{
if ( col > 0 )
{
cerr << ", ";
}
cerr << query->GetFieldName( col );
}
cerr << endl;
vtkVariantArray* va = vtkVariantArray::New();
while ( query->NextRow( va ) )
{
for ( int field = 0; field < va->GetNumberOfValues(); ++ field )
{
if ( field > 0 )
{
cerr << ", ";
}
cerr << va->GetValue( field ).ToString().c_str();
}
cerr << endl;
}
va->Delete();
cerr << endl << "Using vtkRowQueryToTable to execute query:" << endl;
vtkRowQueryToTable* reader = vtkRowQueryToTable::New();
reader->SetQuery( query );
reader->Update();
vtkTable* table = reader->GetOutput();
for ( vtkIdType col = 0; col < table->GetNumberOfColumns(); ++ col )
{
table->GetColumn( col )->Print( cerr );
}
cerr << endl;
#if defined(PRINT_TABLE_CONTENTS)
for ( vtkIdType row = 0; row < table->GetNumberOfRows(); ++ row )
{
for ( vtkIdType col = 0; col < table->GetNumberOfColumns(); ++ col )
{
vtkVariant v = table->GetValue( row, col );
cerr << "row " << row << ", col " << col << " - "
<< v.ToString() << " ( " << vtkImageScalarTypeNameMacro( v.GetType()) << " )" << endl;
}
}
#endif
query->SetQuery( "DROP TABLE people" );
query->Execute();
reader->Delete();
query->Delete();
db->Delete();
return 0;
}
|