1 /* Copyright (C) 2014 InfiniDB, Inc.
2    Copyright (C) 2016 MariaDB Corporation
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License
6    as published by the Free Software Foundation; version 2 of
7    the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17    MA 02110-1301, USA. */
18 
19 /***********************************************************************
20  *   $Id: ddlpackageprocessor.h 9627 2013-06-18 13:59:21Z rdempsey $
21  *
22  *
23  ***********************************************************************/
24 /** @file */
25 
26 #ifndef DDLPACKAGEPROCESSOR_H
27 #define DDLPACKAGEPROCESSOR_H
28 #include <unistd.h>
29 #include <string>
30 #include <stdexcept>
31 #include <sstream>
32 #include <iostream>
33 #include <stdint.h>
34 
35 #include <boost/any.hpp>
36 #include <boost/tuple/tuple.hpp>
37 
38 #include "calpontsystemcatalog.h"
39 #include "objectidmanager.h"
40 #include "sessionmanager.h"
41 #include "brmtypes.h"
42 #include "ddlpkg.h"
43 #include "messageobj.h"
44 #include "we_type.h"
45 #include "we_define.h"
46 #include "writeengine.h"
47 #include "columnresult.h"
48 #include "../../writeengine/client/we_clients.h"
49 #include "liboamcpp.h"
50 
51 #if defined(_MSC_VER) && defined(DDLPKGPROC_DLLEXPORT)
52 #define EXPORT __declspec(dllexport)
53 #else
54 #define EXPORT
55 #endif
56 
57 //#define IDB_DDL_DEBUG
58 namespace ddlpackageprocessor
59 {
60 
61 #define SUMMARY_INFO( message ) \
62 	if ( isDebug(SUMMARY) ) \
63     { \
64 	  std::cerr << message << std::endl; \
65     }
66 
67 #define DETAIL_INFO( message ) \
68 	if ( isDebug(DETAIL) ) \
69 	{ \
70 	   std::cerr << message << std::endl; \
71 	}
72 
73 #define VERBOSE_INFO( message ) \
74     if ( isDebug(VERBOSE) ) \
75     { \
76         std::cerr << message << std::endl; \
77     }
78 
79 /** @brief base class that defines the common interface and
80  * implementation of a DDLPacakageProcessor
81  */
82 class DDLPackageProcessor
83 {
84 
85 public:
86 
87     /** @brief Result code
88      */
89     enum ResultCode { NO_ERROR, CREATE_ERROR,  ALTER_ERROR, DROP_ERROR, TRUNC_ERROR,
90                       TOKENIZATION_ERROR, NOT_ACCEPTING_PACKAGES, PK_NOTNULL_ERROR, WARNING, USER_ERROR, NETWORK_ERROR, PARTITION_WARNING,
91                       WARN_NO_PARTITION, DROP_TABLE_NOT_IN_CATALOG_ERROR
92                     };
93 
94     enum DebugLevel                            /** @brief Debug level type enumeration */
95     {
96         NONE                    = 0,           /** @brief No debug info */
97         SUMMARY                 = 1,           /** @brief Summary level debug info */
98         DETAIL                  = 2,           /** @brief A little detail debug info */
99         VERBOSE                 = 3,           /** @brief Detailed debug info */
100     };
101 
102     enum LogFileType { DROPTABLE_LOG, DROPPART_LOG, TRUNCATE_LOG};
103     typedef std::vector<execplan::CalpontSystemCatalog::OID> OidList;
104     typedef std::set<BRM::LogicalPartition> PartitionNums;
105     struct LogInfo
106     {
107         LogFileType fileType;
108         OidList oids;
109         PartitionNums partitionNums;
110     };
111     typedef std::map<execplan::CalpontSystemCatalog::OID, LogInfo> TableLogInfo;
112 
113     /** @brief the result of dml operations
114      */
115     struct DDLResult
116     {
117         /** @brief the result code
118          */
119         ResultCode result;
120         /** @brief the error message if result != NO_ERROR
121          */
122         logging::Message message;
123     };
124 
125     /** @brief a structure to hold ddlcolumn attributes
126      */
127     struct DDLColumn
128     {
129         execplan::CalpontSystemCatalog::OID oid;
130         execplan::CalpontSystemCatalog::ColType colType;
131         execplan:: CalpontSystemCatalog::TableColName tableColName;
132     };
133 
134     /** @brief a list of DDLColumns
135      */
136     typedef std::vector<DDLColumn> ColumnList;
137 
138     /** @brief a strcuture to hold index object
139      */
140     struct IndexOID
141     {
142         int listOID;
143         int treeOID;
144     };
145 
146     /** @brief a vector of index object ids
147      */
148     typedef std::vector<IndexOID> IndexOIDList;
149 
150     /** @brief a structure to hold a dictionary's
151       *  object ids
152       */
153     struct DictOID
154     {
155         int dictOID;
156         int listOID;
157         int treeOID;
158         int colWidth;
159         int compressionType;
160     };
161     /** @brief a structure to hold a date
162       */
163 
164     struct Date
165     {
166         unsigned spare  : 6;
167         unsigned day    : 6;
168         unsigned month  : 4;
169         unsigned year   : 16;
170         // NULL column value = 0xFFFFFFFE
DateDate171         EXPORT Date( )
172         {
173             year = 0xFFFF;
174             month = 0xF;
175             day = 0x3F;
176             spare = 0x3E;
177         }
178     };
179     /*
180         struct Date
181         {
182         int year   : 16;
183         int month  : 4;
184         int day    : 6;
185         int spare  : 6;
186             Date( )   { year = 0; month = 0; day = 0; spare = 0;}
187         }; */
188     /** @brief a structure to hold a datetime
189       */
190     struct dateTime
191     {
192         unsigned msecond : 20;
193         unsigned second  : 6;
194         unsigned minute  : 6;
195         unsigned hour    : 6;
196         unsigned day     : 6;
197         unsigned month   : 4;
198         unsigned year    : 16;
199         // NULL column value = 0xFFFFFFFFFFFFFFFE
dateTimedateTime200         EXPORT dateTime( )
201         {
202             year = 0xFFFF;
203             month = 0xF;
204             day = 0x3F;
205             hour = 0x3F;
206             minute = 0x3F;
207             second = 0x3F;
208             msecond = 0xFFFFE;
209         }
210     };
211     /*
212         struct dateTime
213         {
214         int year    : 16;
215         int month   : 4;
216         int day     : 6;
217         int hour    : 6;
218         int minute  : 6;
219         int second  : 6;
220         int msecond : 20;
221             dateTime( )   { year = 0; month = 0; day = 0; hour = 0; minute = 0; second = 0; msecond = 0; }
222         }
223         ; */
224     /** @brief a vector of dictionary object ids
225       */
226     typedef std::vector<DictOID> DictionaryOIDList;
227 
228     /** the type of a list of ColumnResult as returned from getSysData
229       */
230     typedef std::vector <execplan::ColumnResult*> NJLSysDataVector;
231     struct NJLSysDataList
232     {
233         NJLSysDataVector sysDataVec;
NJLSysDataListNJLSysDataList234         EXPORT NJLSysDataList() {};
235         EXPORT ~NJLSysDataList();
beginNJLSysDataList236         NJLSysDataVector::const_iterator begin()
237         {
238             return sysDataVec.begin();
239         }
endNJLSysDataList240         NJLSysDataVector::const_iterator end()
241         {
242             return sysDataVec.end();
243         }
push_backNJLSysDataList244         void push_back(execplan::ColumnResult* cr)
245         {
246             sysDataVec.push_back(cr);
247         }
sizeNJLSysDataList248         unsigned int size()
249         {
250             return static_cast<unsigned int>(sysDataVec.size());
251         }
findColumnNJLSysDataList252         int findColumn(const execplan::CalpontSystemCatalog::OID& columnOID)
253         {
254             for (uint32_t i = 0; i < sysDataVec.size(); i++)
255             {
256                 if (sysDataVec[i]->ColumnOID() == columnOID)
257                 {
258                     return i;
259                 }
260             }
261 
262             return -1;
263         }
264     };
265 
266 
267     /** @brief constructor
268       */
DDLPackageProcessor(BRM::DBRM * aDbrm)269     DDLPackageProcessor(BRM::DBRM* aDbrm) : fStartingColOID(0), fDDLLoggingId(23), fDebugLevel( NONE )
270     {
271         fWEClient = new WriteEngine::WEClients(WriteEngine::WEClients::DDLPROC);
272         fPMCount = fWEClient->getPmCount();
273         fDbrm = aDbrm;
274         //std::cout << "in DDLPackageProcessor constructor " << this << std::endl;
275     }
276 
277     /** @brief destructor
278       */
279     EXPORT virtual ~DDLPackageProcessor();
280 
281 
282     /** @brief Is it required to debug
283       */
isDebug(const DebugLevel level)284     bool isDebug( const DebugLevel level ) const
285     {
286         return level <= fDebugLevel;
287     }
288 
289     /** @brief Get debug level
290       */
getDebugLevel()291     DebugLevel getDebugLevel() const
292     {
293         return fDebugLevel;
294     }
295 
296     /** @brief Set debug level
297       */
setDebugLevel(const DebugLevel level)298     void  setDebugLevel( const DebugLevel level )
299     {
300         fDebugLevel = level;
301     }
302 
303     /** @brief Get index oid that was allocated during index creation
304       */
getIndexOID()305     IndexOID getIndexOID() const
306     {
307         return fIdxOID;
308     }
309 
310     /** @brief Get starting column oid that was allocated during table
311       * creation.
312       */
getStartingColumnOID()313     int getStartingColumnOID() const
314     {
315         return fStartingColOID;
316     }
317 
318     /** @brief access and mutator of fPKName */
PKName()319     const std::string PKName() const
320     {
321         return fPKName;
322     }
PKName(const std::string PKName)323     void PKName (const std::string PKName)
324     {
325         fPKName = PKName;
326     }
327     /**  @brief Flush primproc cache for associated lbids.
328     *
329     *  @param oidList the list of OIDs for
330     *  which the lbids for those files will be removed
331     */
332     EXPORT void flushPrimprocCache( std::vector<execplan::CalpontSystemCatalog::OID>& oidList );
333 
334     /**  @brief remove the physical files
335     *
336     *  @param txnID the transaction id
337     *  @param oidList the list of OIDs for
338     *  which the files should be removed
339     */
340     EXPORT void removeFiles(const uint64_t uniqueId, std::vector<execplan::CalpontSystemCatalog::OID>& oidList);
341 
342     EXPORT void createFiles(execplan::CalpontSystemCatalog::TableName aTableName, const int useDBRoot, const uint64_t uniqueId, const uint32_t numOids);
343 
344     /**  @brief remove the physical files for the specified partition
345      *
346      *  @param oidList the list of OIDs for
347      *  which the files should be removed
348      *  @param partition number
349      */
350     EXPORT void removePartitionFiles(std::vector<execplan::CalpontSystemCatalog::OID>& oidList,
351                                      const PartitionNums& partitions,
352                                      uint64_t uniqueId);
353 
354     /**  @brief remove the extents from extent map
355      *
356      *  @param txnID the transaction id
357      *  @param result the result of the operation
358      *  @param oidList the list of OIDs for
359      *  which the extents should be removed
360      */
361     EXPORT void removeExtents(std::vector<execplan::CalpontSystemCatalog::OID>& oidList);
362 
363 
364     /**  @brief create and open log file to log a table information
365     *
366     *  @param tableOid the oid of the table
367     *  @param tableName the shcema, table name
368     */
369     EXPORT void createWriteDropLogFile(execplan::CalpontSystemCatalog::OID tableOid,
370                                        uint64_t uniqueId, std::vector<execplan::CalpontSystemCatalog::OID>& oidList);
371 
372     /**  @brief create and open log file to log a table partition information
373      *
374      *  @param tableOid the oid of the table
375      *  @param tableName the shcema, table name
376      *  @param partition the partition number to be dropped
377      */
378     EXPORT void createWritePartitionLogFile(execplan::CalpontSystemCatalog::OID tableOid,
379                                             const PartitionNums& partitionNums,
380                                             std::vector<execplan::CalpontSystemCatalog::OID>& oidList,
381                                             uint64_t uniqueId);
382 
383     // EXPORT void createOpenTruncateTableLogFile(execplan::CalpontSystemCatalog::OID tableOid, execplan::CalpontSystemCatalog::TableName tableName);
384 
385     /**  @brief create and open log file to log a truncae table information
386      *
387      *  @param tableOid the oid of the table
388      *  @param tableName the shcema, table name
389      */
390     EXPORT void createWriteTruncateTableLogFile(execplan::CalpontSystemCatalog::OID tableOid, uint64_t uniqueId, std::vector<execplan::CalpontSystemCatalog::OID>& oidList);
391 
392 
393     /**  @brief delete log file
394      *
395      */
396     EXPORT void deleteLogFile(LogFileType fileType, execplan::CalpontSystemCatalog::OID tableOid, uint64_t uniqueId);
397 
398     /**  @brief fetch log file infomation
399      *
400      */
401     EXPORT void fetchLogFile(TableLogInfo& tableLogInfos, uint64_t uniqueId);
402 
403     BRM::TxnID fTxnid;
404 
405 protected:
406     /** @brief get a list of DDLColumns for the given schema.table
407      *
408      * @param schema the schema the table belongs to
409      * @param table the table name
410      * @param colList will contain the list of columns on return
411      */
412     EXPORT void getColumnsForTable( uint32_t sessionID, std::string schema, std::string table,
413                                     ColumnList& colList );
414 
415     /** @brief convert parsed ddl data type to a system catalog data type
416      *
417      * @param dateType the parsed ddl data type
418      */
419     execplan::CalpontSystemCatalog::ColDataType convertDataType(int dataType);
420 
421     /** @brief get the null representation for the given column type
422      *
423      * @param colType the column type
424      */
425     boost::any getNullValueForType(const execplan::CalpontSystemCatalog::ColType& colType);
426 
427     /** @brief return a tokenized value for the supplied data value
428       *
429       * @param result the result of the operation
430       * @param colType the column type
431       * @param data the value to tokenize
432       */
433     boost::any tokenizeData(execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
434                             const execplan::CalpontSystemCatalog::ColType& colType,
435                             const boost::any& data );
436 
437     /** @brief does the supplied constraint type require an index
438       *
439       * @param type the constraint type
440       */
441     bool isIndexConstraint(ddlpackage::DDL_CONSTRAINTS type);
442 
443     /** @brief get the char code for the constraint type
444       *
445       * @param type the constraint type
446       */
447     char getConstraintCode(ddlpackage::DDL_CONSTRAINTS type);
448 
449     /** @brief get the column refrences for the given table constraint
450       *
451       * @param tableConstraint the table constraint
452       * @param columns on return will contain the list of columns
453       */
454     void getColumnReferences(ddlpackage::TableConstraintDef& tableConstraint,
455                              ddlpackage::ColumnNameList& columns);
456 
457     /** @brief build a table constraint name
458      *
459      * @param schema the schema the table belongs to
460      * @param table the name of the table
461      * @param constraintNumber the constraint number
462      * @param type the constraint type
463      */
464     std::string buildTableConstraintName(int oid,
465                                          ddlpackage::DDL_CONSTRAINTS type);
466 
467     /** @brief build a column constraint name
468       *
469       * @param schema the schema the table belongs to
470       * @param table the name of the table
471       * @param column the column name
472       * @param type the constraint type
473       */
474     std::string buildColumnConstraintName(const std::string& schema,
475                                           const std::string& table,
476                                           const std::string& column,
477                                           ddlpackage::DDL_CONSTRAINTS type);
478 
479 
480     /** @brief write the tables meta data to the SYSTABLE table
481      *
482      * @param txnID the transaction id
483      * @param result the result of the operation
484      * @param tableDef the table definition
485      */
486     //void writeSysTableMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
487     //                           ddlpackage::TableDef& tableDef, uint32_t tableWithAutoi=0);
488 
489     /** @brief write the table columns meta data to the SYSCOLUMN table
490      *
491      * @param txnID the transaction id
492      * @param result the result of the operation
493      * @param tableDefCols the table columns definition
494      * @param qualifiedName the name of catalog, schema, object names
495      */
496     //void writeSysColumnMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
497     //                            ddlpackage::ColumnDefList& tableDefCols,
498     //                            ddlpackage::QualifiedName& qualifiedName, int colpos, bool alterFlag=false );
499 
500     /** @brief write the table constraint meta data to the SYSCONSTRAINT table
501      *
502      * @param txnID the transaction id
503      * @param result the result of the operation
504      * @param constraintList the table constrain list
505      * @param qualifiedName the name of catalog, schema, object names
506      */
507 //   void writeTableSysConstraintMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
508 //                                       ddlpackage::TableConstraintDefList& constraintList, ddlpackage::QualifiedName&
509 //                                        qualifiedName, bool alterFlag=false );
510     /** @brief write the table constraint meta data to the SYSCONSTRAINTCOL table
511       *
512       * @param txnID the transaction id
513       * @param result the result of the operation
514       * @param constraintList the table constrain list
515       * @param qualifiedName the name of catalog, schema, object names
516       */
517 //   void writeTableSysConstraintColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
518 //                                           const DDLResult& result, ddlpackage::TableConstraintDefList& constraintList,
519 //                                           ddlpackage::QualifiedName& qualifiedName, bool alterFlag=false );
520 
521     /** @brief write the column constraint meta data to the SYSCONTRAINT table
522       *
523       * @param txnID the transaction id
524       * @param result the result of the operation
525       * @param constraintList the table constrain list
526       * @param qualifiedName the name of catalog, schema, object names
527       */
528 //    void writeColumnSysConstraintMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
529 //                                          ddlpackage::ColumnDefList& tableDefCols,
530 //                                          ddlpackage::QualifiedName& qualifiedName );
531 
532     /** @brief write the column constraint meta data to the SYSCONTRAINTCOL table
533       *
534       * @param txnID the transaction id
535       * @param result the result of the operation
536       * @param tableDef the table definition
537       */
538 //    void writeColumnSysConstraintColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
539 //            const DDLResult& result, ddlpackage::ColumnDefList& tableDefCols,
540 //           ddlpackage::QualifiedName& qualifiedName);
541 
542 
543     /** @brief write the index meta data to the SYSINDEX table
544      *
545      * @param txnID the transaction id
546      * @param result the result of the operation
547      * @param tableDef the table definiton
548      * @param consDef the table constraint
549      * @param indexName name of the index
550      */
551     // void writeSysIndexMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
552     //                            ddlpackage::QualifiedName& qualifiedName,
553     //                            ddlpackage::DDL_CONSTRAINTS type,
554     //                            std::string& indexName, bool multicol, bool alterFlag=false);
555 
556     /** @brief write the index meta data to the SYSINDEXCOL table
557      *
558      * @param txnID the transaction id
559      * @param result the result of the operation
560      * @param tableDef the table definiton
561      * @param constraintCols the list of columns in this index
562      * @param indexName name of the index
563      */
564     // void writeSysIndexColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
565 //ddlpackage::QualifiedName& qualifiedName,
566     //                                ddlpackage::ColumnNameList& constraintCols,
567     //                               std::string& indexName, bool alterFlag=false);
568 
569     /** @brief remove all indexes for the supplied table from the SYSINDEX table
570       *
571       * @param txnID the transaction id
572       * @param result the result of the operation
573       * @param tableName the qualified name of the table
574       */
575     //void removeSysIndexMetaDataForTable(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
576     //                                   DDLResult& result, ddlpackage::QualifiedName& tableName);
577 
578     /** @brief remove all index columns for the supplied table from the SYSINDEXCOL table
579       *
580       * @param txnID the transaction id
581       * @param result the result of the operation
582       * @param tableName the qualified name of the table
583       */
584     // void removeSysIndexColMetaDataForTable(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
585     //                                        DDLResult& result, ddlpackage::QualifiedName& tableName);
586 
587     /** @brief remove an index from the SYSINDEX table
588       *
589       * @param txnID the transaction id
590       * @param result the result of the operation
591       * @param indexName the qualified name of the index
592       */
593     // void removeSysIndexMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
594     //                            ddlpackage::QualifiedName& indexName);
595 
596     /** @brief remove index columns from the SYSINDEXCOL table
597       *
598       * @param txnID the transaction id
599       * @param result the result of the operation
600       * @param indexName the qualified name of the index
601       */
602     // void removeSysIndexColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
603     //                                ddlpackage::QualifiedName& indexName);
604 
605     /** @brief remove the table meta data from the SYSTABLE table
606       *
607       * @param txnID the transaction id
608       * @param result the result of the operation
609       * @param tableName the qualified name of the table to remove
610       */
611     // void removeSysTableMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
612     //                             ddlpackage::QualifiedName& tableName);
613 
614     /** @brief remove the column meta data from the SYSCOLUMN table
615       *
616       * @param txnID the transaction id
617       * @param result the result of the operation
618       * @param tableName the qualified name of the table whose columns
619       * are to be removed
620       */
621     // void removeSysColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
622     //                           ddlpackage::QualifiedName& tableName);
623 
624     /** @brief remove the column meta data from the SYSCOLUMN table
625       *
626       * @param txnID the transaction id
627       * @param result the result of the operation
628       * @param columnInfo the qualified name of the column
629       * to be removed
630       */
631     // void removeColSysColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
632     //                           ddlpackage::QualifiedName& columnInfo);
633 
634     /**  @brief remove the constraint meta data from the SYSCONSTRAINT table
635       *
636       * @param txnID the transaction id
637       * @param result the result of the operation
638       * @param tableName the qualified name of the table whose constraints
639       * are to be removed
640       */
641 //   void removeSysContraintMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
642     //                                  ddlpackage::QualifiedName& tableName);
643 
644     /**  @brief remove the constraint meta data from the SYSCONSTRAINT table
645       *
646       * @param txnID the transaction id
647       * @param result the result of the operation
648       * @param indexName the index name to be removed
649       */
650 //   void removeSysIndexMetaDataForIndex(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
651 //       DDLResult& result,
652 //       execplan::CalpontSystemCatalog::IndexNameList& indexNameList);
653 
654     /**  @brief remove the column constraint meta data from the SYSCONSTRAINTCOL table
655       *
656       * @param txnID the transaction id
657       * @param result the result of the operation
658       * @param tableName the qualified name of the table whose column constraints
659       * are to be removed
660       */
661     //  void removeSysConstraintColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
662     //                                     ddlpackage::QualifiedName& tableName);
663     /**  @brief remove the column constraint meta data from the SYSCONSTRAINT table
664     *
665     * @param txnID the transaction id
666     * @param result the result of the operation
667     * @param constrintNames the names of the constraints
668     * are to be removed
669     */
670 #if 0
671     void removeSysContraintMetaDataForConstraint(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID,
672             DDLResult& result,
673             execplan::CalpontSystemCatalog::IndexNameList& constrintNames);
674     /**  @brief remove the column constraint meta data from the SYSCONSTRAINTCOL table
675       *
676       * @param txnID the transaction id
677       * @param result the result of the operation
678       * @param columnInfo the qualified name of the column whose constraints
679       * are to be removed
680       */
681     void removeColSysConstraintColMetaData(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
682                                            ddlpackage::QualifiedName& columnInfo);
683 
684     /** @brief create the physical dictionary files
685      *
686      * @param txnID the transaction id
687      * @param result the result of the operation
688      */
689     void createDictionaryFiles(execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result, const int useDBRoot);
690 
691     /** @brief create the physical column files
692      *
693      * @param txnID the transaction id
694      * @param result the result of the operation
695      * @param tableDefCols the table column definition list
696      */
697     void createColumnFiles(execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
698                            ddlpackage::ColumnDefList& tableDefCols, const int useDBRoot, const uint32_t partitionNum = 0);
699 
700     /**  @brief update the SYSCOLUMN table
701      *
702      *  @param txnID the transaction id
703      *  @param result the result of the operation
704      *  @param ridList the list of OIDs for
705      *  which the column file should be removed
706      */
707     void updateSyscolumns( execplan::CalpontSystemCatalog::SCN txnID,
708                            DDLResult& result, WriteEngine::RIDList& ridList,
709                            WriteEngine::ColValueList& colValuesList,
710                            WriteEngine::ColValueList& colOldValuesList);
711 
712     /** @brief remove the physical index files
713      *
714      * @param txnID the transaction id
715      * @param result the result of the operation
716      * @param idxOIDList the list of OIDs for
717      * which the index files should be removed
718      */
719     void removeIndexFiles(execplan::CalpontSystemCatalog::SCN txnID, DDLResult& result,
720                           execplan::CalpontSystemCatalog::IndexOIDList& idxOIDList);
721 #endif
722 
723     /** @brief return the OIDs used by the database objects
724      *
725      * @param ridList the list of column OIDs to return
726      * @param dictOIDList the list of dictionary OIDs to return
727      */
728     void returnOIDs(execplan::CalpontSystemCatalog::RIDList& ridList,
729                     execplan::CalpontSystemCatalog::DictOIDList& dictOIDList);
730 
731     /**
732       * @brief convert a columns data, represnted as a string, to it's native
733       * data type
734       *
735       * @param type the columns database type
736       * @param data the columns string representation of it's data
737       */
738     //boost::any convertColumnData( execplan::CalpontSystemCatalog::ColType colType,
739     //                             const std::string& data );
740 
741     /**
742       * @brief Find the given column in the given system catalog table and
743       * return it as a DDLColumn object
744       *
745       * @param systableName the table to find the column in
746       * @param colName the name of the column to find in the table
747       * @param sysCol on success the returned sysCol object
748       */
749     void findColumnData(uint32_t sessionID, execplan::CalpontSystemCatalog::TableName& systableName,
750                         const std::string& colName, DDLColumn& sysCol );
751 
752     /** @brief remove the supplied row from the supplied system catalog table
753       *
754       * @param result the result of the operation
755       * @param sysCatalogTableName the qualified name of the system catalog table
756       * @param rid the id of the row to remove
757       */
758     void removeRowFromSysCatalog(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
759                                  ddlpackage::QualifiedName& sysCatalogTableName, WriteEngine::RID& rid);
760 
761     /** @brief validate reference constraint for altering existing table
762       *
763       * @param sessionID session ID
764       * @param result the result of the operation
765       * @param tcn the column which has the foreign key constraint
766       * @param refIndexName the index name of the referenced primary key constraint
767       * @return true if violation
768       */
769     bool referenceConstraintViolation(uint32_t sessionID,
770                                       DDLResult& result,
771                                       execplan::CalpontSystemCatalog::TableColName tcn,
772                                       execplan::CalpontSystemCatalog::IndexName refIndexName);
773 
774     /** @brief validate PK constraint (not null part) for altering existing table
775       *
776       * @param sessionID session ID
777       * @param result the result of the operation
778       * @param qualifiedName schema.table name
779       * @param constraintCols the columns associated with the primary key
780       * @return true if violation
781       */
782     bool PKConstraintViolation(uint32_t sessionID,
783                                DDLResult& result,
784                                ddlpackage::QualifiedName& qualifiedName,
785                                ddlpackage::ColumnNameList& constraintCols);
786     /** @brief validate check constraint for altering existing table
787       *
788       * @param sessionID session ID
789       * @param result the result of the operation
790       * @param qualifiedName schema.table name
791       * @param checkConstraint the constraint text string
792       * @return true if violation
793       */
794     bool checkConstraintViolation(uint32_t sessionID,
795                                   DDLResult& result,
796                                   ddlpackage::QualifiedName& qualifiedName,
797                                   std::string& checkConstraint);
798 
799 
800     /** @brief remove the supplied rows from the supplied system catalog table
801       *
802       * @param result the result of the operation
803       * @param sysCatalogTableName the qualified name of the system catalog table
804       * @param colRidList the list of row ids to remove
805       */
806     void removeRowsFromSysCatalog(uint32_t sessionID, execplan::CalpontSystemCatalog::SCN txnID, const DDLResult& result,
807                                   ddlpackage::QualifiedName& sysCatalogTableName,
808                                   execplan::CalpontSystemCatalog::RIDList& colRidList);
809 
810 
811     WriteEngine::WriteEngineWrapper fWriteEngine;
812 
813     BRM::DBRM* fDbrm;
814 
815     execplan::SessionManager fSessionManager;
816     uint32_t fPMCount;
817     WriteEngine::WEClients* fWEClient;
818 
819 
820     DictionaryOIDList fDictionaryOIDList;
821 
822     // store oids used during table and index creation
823     // for external reference
824     int fTableOID;
825     int fStartingColOID;
826     int fColumnNum;
827     IndexOID fIdxOID;
828     std::ofstream	       fDDLLogFile;
829     std::string            fDDLLogFileName;
830 
831     std::string fPKName;    // primary key name supplied by Oracle. Oracle will issue
832     // two separate DDL statements for a create table with primary
833     // key DDL, with the 1st one being create index and the 2nd
834     // one being create table. This PK name will be stored here
835     // when creatindexprocessor gets the create index statement. This
836     // is to make sure Calpont use the same system primary key name as Oracle
837     unsigned const fDDLLoggingId;
838 
839     //std::ofstream	       fDDLLogFile;
840     //std::string            fDDLLogFileName;
841 
842     /** @brief convert absolute rid to relative rid in a segement file
843      *
844      * @param rid  in:the absolute rid  out:  relative rid in a segement file
845      * @param dbRoot,partition, segment   the extent information obtained from rid
846      * @param filesPerColumnPartition,extentRows, extentsPerSegmentFile   the extent map parameters
847      * @param startDBRoot the dbroot this table starts
848      * @param dbrootCnt the number of dbroot in db
849      */
850     void convertRidToColumn(uint64_t& rid, unsigned& dbRoot, unsigned& partition,
851                             unsigned& segment, unsigned filesPerColumnPartition,
852                             unsigned  extentsPerSegmentFile, unsigned extentRows,
853                             unsigned startDBRoot, unsigned dbrootCnt);
854 
855     int rollBackTransaction(uint64_t uniqueId, BRM::TxnID txnID, uint32_t sessionID);
856     int commitTransaction(uint64_t uniqueId, BRM::TxnID txnID);
857 
858     // MCOL-66 The DBRM can't handle concurrent DDL
859     static boost::mutex dbrmMutex;
860 private:
861     /** @brief clean beginning and ending glitches and spaces from string
862        *
863        * @param s string to be cleaned
864        */
865     void cleanString(std::string& s);
866     //std::string            fDDLLogFileName;
867     DebugLevel fDebugLevel; // internal use debug level
868 
869 
870 
871 };
872 /** @brief helper template function to do safe from string to type conversions
873   *
874   */
875 template <class T>
from_string(T & t,const std::string & s,std::ios_base & (* f)(std::ios_base &))876 bool from_string(T& t,
877                  const std::string& s,
878                  std::ios_base & (*f)(std::ios_base&))
879 {
880     std::istringstream iss(s);
881     return !(iss >> f >> t).fail();
882 }
883 
884 }
885 
886 #undef EXPORT
887 
888 #endif //DDLPACKAGEPROCESSOR_H
889 // vim:ts=4 sw=4:
890 
891