From 947963709e23726c0680473054fe4d34b682e039 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Sat, 10 Jul 2010 12:00:30 -0700 Subject: [PATCH] Support table options for CREATE TABLE. This allows one to create MySQL tables with the ENGINE=InnoDB option so that transactions will be honored. The default MyISAM storage engine does not honor transactions. --- IO/vtkSQLDatabase.cxx | 19 +++ IO/vtkSQLDatabase.h | 46 +++---- IO/vtkSQLDatabaseSchema.cxx | 246 ++++++++++++++++++++++++------------ IO/vtkSQLDatabaseSchema.h | 240 +++++++++++++++++++---------------- 4 files changed, 332 insertions(+), 219 deletions(-) diff --git a/IO/vtkSQLDatabase.cxx b/IO/vtkSQLDatabase.cxx index b98318598b6..1185e107699 100644 --- a/IO/vtkSQLDatabase.cxx +++ b/IO/vtkSQLDatabase.cxx @@ -587,6 +587,25 @@ bool vtkSQLDatabase::EffectSchema( vtkSQLDatabaseSchema* schema, bool dropIfExis } queryStr += ")"; + // Add options to the end of the CREATE TABLE statement + int numOpt = schema->GetNumberOfOptionsInTable( tblHandle ); + if ( numOpt < 0 ) + { + query->RollbackTransaction(); + query->Delete(); + return false; + } + for ( int optHandle = 0; optHandle < numOpt; ++ optHandle ) + { + vtkStdString optBackend = schema->GetOptionBackendFromHandle( tblHandle, optHandle ); + if ( strcmp( optBackend, VTK_SQL_ALLBACKENDS ) && strcmp( optBackend, this->GetClassName() ) ) + { + continue; + } + queryStr += " "; + queryStr += schema->GetOptionTextFromHandle( tblHandle, optHandle ); + } + // Execute the CREATE TABLE query query->SetQuery( queryStr ); if ( ! query->Execute() ) diff --git a/IO/vtkSQLDatabase.h b/IO/vtkSQLDatabase.h index 9ab83056c69..d1b3b4f4810 100644 --- a/IO/vtkSQLDatabase.h +++ b/IO/vtkSQLDatabase.h @@ -34,12 +34,12 @@ PURPOSE. See the above copyright notice for more information. // // The subclass should also provide API to set connection parameters. // -// This class also provides the function EffectSchema to transform a +// This class also provides the function EffectSchema to transform a // database schema into a SQL database. // // .SECTION Thanks // Thanks to Andrew Wilson from Sandia National Laboratories for his work -// on the database classes and for the SQLite example. Thanks to David Thompson +// on the database classes and for the SQLite example. Thanks to David Thompson // and Philippe Pebay from Sandia National Laboratories for implementing // this class. // @@ -61,7 +61,7 @@ class vtkStringArray; // This is a list of features that each database may or may not // support. As yet (April 2008) we don't provide access to most of -// them. +// them. #define VTK_SQL_FEATURE_TRANSACTIONS 1000 #define VTK_SQL_FEATURE_QUERY_SIZE 1001 #define VTK_SQL_FEATURE_BLOB 1002 @@ -73,7 +73,7 @@ class vtkStringArray; #define VTK_SQL_FEATURE_BATCH_OPERATIONS 1008 #define VTK_SQL_FEATURE_TRIGGERS 1009 // supported -// Default size for columns types which require a size to be specified +// Default size for columns types which require a size to be specified // (i.e., VARCHAR), when no size has been specified #define VTK_SQL_DEFAULT_COLUMN_SIZE 32 @@ -93,7 +93,7 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // Description: // Close the connection to the database. virtual void Close() = 0; - + // Description: // Return whether the database has an open connection. virtual bool IsOpen() = 0; @@ -101,11 +101,11 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // Description: // Return an empty query on this database. virtual vtkSQLQuery* GetQueryInstance() = 0; - + // Description: // Did the last operation generate an error virtual bool HasError() = 0; - + // Description: // Get the last error text from the database // I'm using const so that people do NOT @@ -113,15 +113,15 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // implementation, because 99% of the time that // will not be the correct thing to do... virtual const char* GetLastErrorText() = 0; - + // Description: // Get the type of the database (e.g. mysql, psql,..). virtual char* GetDatabaseType() = 0; - + // Description: // Get the list of tables from the database. virtual vtkStringArray* GetTables() = 0; - + // Description: // Get the list of fields for a particular table. virtual vtkStringArray* GetRecord(const char *table) = 0; @@ -129,7 +129,7 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // Description: // Return whether a feature is supported by the database. virtual bool IsSupported(int vtkNotUsed(feature)) { return false; } - + // Description: // Get the URL of the database. virtual vtkStdString GetURL() = 0; @@ -141,34 +141,34 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // It must be overwritten for those SQL backends which allow such // preambles such as, e.g., MySQL. virtual vtkStdString GetTablePreamble( bool ) { return vtkStdString(); } - + // Description: // Return the SQL string with the syntax to create a column inside a // "CREATE TABLE" SQL statement. // NB: this method implements the following minimally-portable syntax: // - // It must be overwritten for those SQL backends which have a different + // It must be overwritten for those SQL backends which have a different // syntax such as, e.g., MySQL. virtual vtkStdString GetColumnSpecification( vtkSQLDatabaseSchema* schema, int tblHandle, int colHandle ); - + // Description: // Return the SQL string with the syntax to create an index inside a // "CREATE TABLE" SQL statement. // NB1: this method implements the following minimally-portable syntax: // [] (,... ) - // It must be overwritten for those SQL backends which have a different + // 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 + // 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, bool& skipped ); - + // Description: // Return the SQL string with the syntax to create a trigger using a // "CREATE TRIGGER" SQL statement. @@ -176,12 +176,12 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // a supported feature. Not all backends (e.g., SQLite) support it. // NB2: this method implements the following minimally-portable syntax: // {BEFORE | AFTER} ON FOR EACH ROW - // It must be overwritten for those SQL backends which have a different + // It must be overwritten for those SQL backends which have a different // syntax such as, e.g., PostgreSQL. virtual vtkStdString GetTriggerSpecification( vtkSQLDatabaseSchema* schema, int tblHandle, int trgHandle ); - + // Description: // Create a the proper subclass given a URL. // The URL format for SQL databases is a true URL of the form: @@ -209,9 +209,9 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject // Description: // Stores the database class pointer as an information key. This is currently // used to store database pointers as part of 'data on demand' data objects. - // For example: The application may have a table/tree/whatever of documents, - // the data structure is storing the meta-data but not the full text. Further - // down the pipeline algorithms or views may want to retrieve additional + // For example: The application may have a table/tree/whatever of documents, + // the data structure is storing the meta-data but not the full text. Further + // down the pipeline algorithms or views may want to retrieve additional // information (full text)for specific documents. static vtkInformationObjectBaseKey* DATABASE(); @@ -219,7 +219,7 @@ class VTK_IO_EXPORT vtkSQLDatabase : public vtkObject protected: vtkSQLDatabase(); ~vtkSQLDatabase(); - + // Description: // Subclasses should override this method to determine connection paramters // given the URL. This is called by CreateFromURL() to initialize the instance. diff --git a/IO/vtkSQLDatabaseSchema.cxx b/IO/vtkSQLDatabaseSchema.cxx index 93822f75e31..372811f6352 100644 --- a/IO/vtkSQLDatabaseSchema.cxx +++ b/IO/vtkSQLDatabaseSchema.cxx @@ -36,34 +36,40 @@ class vtkSQLDatabaseSchemaInternals { public: // NB: use of string instead of char* here to avoid leaks on destruction. struct Statement - { + { vtkStdString Name; vtkStdString Action; // may have backend-specific stuff vtkStdString Backend; // only active for this backend, if != "" - }; + }; struct Column - { + { vtkSQLDatabaseSchema::DatabaseColumnType Type; int Size; // used when required, ignored otherwise (e.g. varchar) vtkStdString Name; vtkStdString Attributes; // may have backend-specific stuff - }; + }; struct Index - { + { vtkSQLDatabaseSchema::DatabaseIndexType Type; vtkStdString Name; vtkstd::vector ColumnNames; - }; + }; struct Trigger - { + { vtkSQLDatabaseSchema::DatabaseTriggerType Type; vtkStdString Name; vtkStdString Action; // may have backend-specific stuff vtkStdString Backend; // only active for this backend, if != "" - }; + }; + + struct Option + { + vtkStdString Text; + vtkStdString Backend; + }; struct Table { @@ -71,6 +77,7 @@ class vtkSQLDatabaseSchemaInternals vtkstd::vector Columns; vtkstd::vector Indices; vtkstd::vector Triggers; + vtkstd::vector