Commit 8a3cb61e authored by Philippe Pebay's avatar Philippe Pebay
Browse files

ENH: now handling the case where the SQL backend does *not* support

     INDEX creation within a CREATE TABLE statement: in this case,
     such INDEX creations are handled separately after all table creations
     have been done.
STYLE: some re-formatting and indenting
parent 4724060c
......@@ -31,7 +31,7 @@
#define VTK_MYSQL_DEFAULT_PORT 3306
vtkCxxRevisionMacro(vtkMySQLDatabase, "1.13");
vtkCxxRevisionMacro(vtkMySQLDatabase, "1.14");
vtkStandardNewMacro(vtkMySQLDatabase);
// ----------------------------------------------------------------------
......@@ -412,8 +412,10 @@ vtkStdString vtkMySQLDatabase::GetColumnSpecification( vtkSQLDatabaseSchema* sch
// ----------------------------------------------------------------------
vtkStdString vtkMySQLDatabase::GetIndexSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int idxHandle )
int idxHandle,
bool& skipped )
{
skipped = false;
vtkStdString queryStr = ", ";
int idxType = schema->GetIndexTypeFromHandle( tblHandle, idxHandle );
......
......@@ -153,11 +153,14 @@ public:
// Description:
// Return the SQL string with the syntax to create an index inside a
// "CREATE TABLE" SQL statement.
// NB: this method implements the MySQL-specific syntax:
// NB1: this method implements the MySQL-specific syntax:
// <index type> [<index name>] (`<column name 1>`,... )
// NB2: since MySQL supports INDEX creation within a CREATE TABLE statement,
// skipped is always returned false.
virtual vtkStdString GetIndexSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int idxHandle );
int idxHandle,
bool& skipped );
protected:
vtkMySQLDatabase();
......
......@@ -31,7 +31,7 @@ PURPOSE. See the above copyright notice for more information.
#include <pqxx/pqxx>
vtkStandardNewMacro(vtkPostgreSQLDatabase);
vtkCxxRevisionMacro(vtkPostgreSQLDatabase, "1.13");
vtkCxxRevisionMacro(vtkPostgreSQLDatabase, "1.14");
// ----------------------------------------------------------------------
vtkPostgreSQLDatabase::vtkPostgreSQLDatabase()
......@@ -89,8 +89,8 @@ void vtkPostgreSQLDatabase::PrintSelf(ostream &os, vtkIndent indent)
// ----------------------------------------------------------------------
vtkStdString vtkPostgreSQLDatabase::GetColumnSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int colHandle )
int tblHandle,
int colHandle )
{
vtkStdString queryStr = schema->GetColumnNameFromHandle( tblHandle, colHandle );
......
......@@ -38,7 +38,7 @@ PURPOSE. See the above copyright notice for more information.
#include <vtksys/SystemTools.hxx>
vtkCxxRevisionMacro(vtkSQLDatabase, "1.25");
vtkCxxRevisionMacro(vtkSQLDatabase, "1.26");
// ----------------------------------------------------------------------
vtkSQLDatabase::vtkSQLDatabase()
......@@ -146,27 +146,41 @@ vtkStdString vtkSQLDatabase::GetColumnSpecification( vtkSQLDatabaseSchema* schem
// ----------------------------------------------------------------------
vtkStdString vtkSQLDatabase::GetIndexSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int idxHandle )
int idxHandle,
bool& skipped )
{
vtkStdString queryStr = ", ";
vtkStdString queryStr;
int idxType = schema->GetIndexTypeFromHandle( tblHandle, idxHandle );
switch ( idxType )
{
case vtkSQLDatabaseSchema::PRIMARY_KEY:
queryStr += "PRIMARY KEY ";
queryStr = ", PRIMARY KEY ";
skipped = false;
break;
case vtkSQLDatabaseSchema::UNIQUE:
queryStr += "UNIQUE ";
queryStr = ", UNIQUE ";
skipped = false;
break;
case vtkSQLDatabaseSchema::INDEX:
// Not supported within a CREATE TABLE statement by all SQL backends:
// must be created later with a CREATE INDEX statement
queryStr = "CREATE INDEX ";
skipped = true;
break;
case vtkSQLDatabaseSchema::INDEX: // Not supported by all SQL backends
return 0;
default:
return 0;
}
queryStr += schema->GetIndexNameFromHandle( tblHandle, idxHandle );
// CREATE INDEX <index name> ON <table name> syntax
if ( skipped )
{
queryStr += " ON ";
queryStr += schema->GetTableNameFromHandle( tblHandle );
}
queryStr += " (";
// Loop over all column names of the index
......@@ -286,6 +300,9 @@ bool vtkSQLDatabase::EffectSchema( vtkSQLDatabaseSchema* schema, bool dropIfExis
return false;
}
// In case INDEX indices are encountered in the schema
vtkstd::vector<vtkStdString> idxStatements;
// Loop over all tables of the schema and create them
int numTbl = schema->GetNumberOfTables();
for ( int tblHandle = 0; tblHandle < numTbl; ++ tblHandle )
......@@ -330,6 +347,7 @@ bool vtkSQLDatabase::EffectSchema( vtkSQLDatabaseSchema* schema, bool dropIfExis
}
// Loop over all indices of the current table
bool skipped = false;
int numIdx = schema->GetNumberOfIndicesInTable( tblHandle );
if ( numIdx < 0 )
{
......@@ -339,17 +357,25 @@ bool vtkSQLDatabase::EffectSchema( vtkSQLDatabaseSchema* schema, bool dropIfExis
for ( int idxHandle = 0; idxHandle < numIdx; ++ idxHandle )
{
// Get index creation syntax (backend-dependent)
vtkStdString idxStr = this->GetIndexSpecification( schema, tblHandle, idxHandle );
vtkStdString idxStr = this->GetIndexSpecification( schema, tblHandle, idxHandle, skipped );
if ( idxStr )
{
queryStr += idxStr;
if ( skipped )
{
// Must create this index later
idxStatements.push_back( idxStr );
continue;
}
else // if ( skipped )
{
queryStr += idxStr;
}
}
else // if ( idxStr )
{
query->RollbackTransaction();
return false;
}
}
queryStr += ")";
......@@ -363,7 +389,21 @@ bool vtkSQLDatabase::EffectSchema( vtkSQLDatabaseSchema* schema, bool dropIfExis
return false;
}
}
// Now, execute the CREATE INDEX statement -- if any
for ( vtkstd::vector<vtkStdString>::iterator it = idxStatements.begin();
it != idxStatements.end(); ++ it )
{
query->SetQuery( *it );
if ( ! query->Execute() )
{
vtkGenericWarningMacro( "Unable to effect the schema: unable to execute query.\nDetails: "
<< query->GetLastErrorText() );
query->RollbackTransaction();
return false;
}
}
// FIXME: eventually handle triggers
// Commit the transaction.
......
......@@ -153,13 +153,18 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject
// Description:
// Return the SQL string with the syntax to create an index inside a
// "CREATE TABLE" SQL statement.
// NB: this method implements the following minimally-portable syntax:
// NB1: this method implements the following minimally-portable syntax:
// <index type> [<index name>] (<column name 1>,... )
// It must be overwritten for those SQL backends which have a different
// syntax such as, e.g., MySQL.
// NB2: this method does not assume that INDEX creation is supported
// within a CREATE TABLE statement. Therefore, should such an INDEX arise
// in the schema, a CREATE INDEX statement is returned and skipped is
// set to true. Otherwise, skipped will always be returned false.
virtual vtkStdString GetIndexSpecification( vtkSQLDatabaseSchema* schema,
int tblHandle,
int idxHandle );
int idxHandle,
bool& skipped );
// Description:
// Create a the proper subclass given a URL.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment