Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ddlstreamer.cpp

Go to the documentation of this file.
00001 //============================================================================= 
00002 //
00003 // XDFLengine library
00004 //
00005 //-----------------------------------------------------------------------------
00006 //  DDLStreamer.CPP
00007 //-----------------------------------------------------------------------------
00013 //_____________________________________________________________________________
00014 //
00015 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00016 //
00017 //  This file is part of the XDFLengine project.
00018 //
00019 //  The XDFLengine is free software; you can redistribute it and/or modify
00020 //  it under the terms of the GNU General Public License as published by
00021 //  the Free Software Foundation; either version 2 of the License, or
00022 //  (at your option) any later version.
00023 //
00024 //  This program is distributed in the hope that it will be useful,
00025 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 //  GNU General Public License for more details.
00028 //
00029 //  You should have received a copy of the GNU General Public License
00030 //  along with this program; if not, write to the Free Software
00031 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00032 //  USA.
00033 //
00034 //  For more information, 
00035 //      contact  : guillaume@baurand.net 
00036 //      or visit : http://xdflengine.sourceforge.net
00037 //
00038 //=============================================================================
00039 
00040 #   include "ddlstreamer.hpp"
00041 #   include "processor/xmlflowcontext.hpp"
00042 #   include "streamers/db/dbobject/dbobjdefinition.hpp"
00043 
00044 BEGIN_XDFLENGINE_NS
00045 
00046 //=============================================================================
00047 //  CLASS DDLStreamer
00048 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00049 
00050 
00051     //_____________________________________________________________________
00052     //  SaxStreamer
00053     //---------------------------------------------------------------------
00054     DDLStreamer::DDLStreamer( const XMLStreamerFactory* p_pParent, StreamerParams* p_pParameters, XMLFlowContext* p_pContext, XMLStreamConsumer* p_pOutput, ContentHandler* p_pHandler, bool p_fCopyStreamToOutput)
00055     :SaxStreamer( p_pParent, p_pParameters, p_pContext, p_pOutput, p_pHandler, p_fCopyStreamToOutput)
00056     {
00057         DEBUG_CREATE(DDLStreamer)
00058     }
00059     
00060     //_____________________________________________________________________
00061     //  ~SaxStreamer
00062     //---------------------------------------------------------------------     
00063     DDLStreamer::~DDLStreamer()
00064     {
00065         DEBUG_DEL(DDLStreamer)
00066     }
00067 
00068     //_____________________________________________________________________
00069     //  COMMITSTREAM
00070     //---------------------------------------------------------------------
00071     bool DDLStreamer::commitStream(bool p_fFinal)
00072     {
00073         //---------------------------------------------------------------------
00074         bool                    l_fOK;
00075         DBOBJDefinitionNode*    l_pDefinitionRoot=0;
00076         //---------------------------------------------------------------------
00077 
00078         PREP_CATCH_XML_FLOW_ERROR;
00079 
00080         DEBUG_IN(DDLStreamer::writeData)
00081 
00082         l_fOK = SaxStreamer::commitStream( p_fFinal);
00083 
00084         if(l_fOK && p_fFinal)
00085         {   
00086 
00087             // get definition
00088             WATCH_XML_FLOW_ERROR
00089             {
00090                 // retrieve definition
00091                 l_pDefinitionRoot =  ((SAXDBOBJDefinitionBuilder*)m_pHandler)->getDefinition();     
00092                 // process definition completion
00093                 makeNodeDDL( l_pDefinitionRoot, m_pOutput); 
00094             } 
00095             CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN ;
00096         }
00097 
00098     RELEASE_AND_RETURN:
00099     
00100 
00101         // CACHE DE DEFINITIONS DESACTIVE !!!!
00102         delete l_pDefinitionRoot;
00103         
00104         ON_XML_FLOW_ERROR_DO l_fOK = false;
00105         ON_XML_FLOW_ERROR_THROW;    
00106 
00107         DEBUG_OUT(DDLStreamer::writeData)
00108 
00109         return l_fOK;
00110 
00111         
00112     }
00113 
00114 
00115 
00116    //_________________________________________________________________________
00117     //  MAKENODEDDL
00118     //-------------------------------------------------------------------------
00119     void DDLStreamer::makeNodeDDL( DBOBJDefinitionNode* p_pDefinitionNode, 
00120                                     XMLStreamConsumer* p_pOutput ) const
00121     {
00122         //_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00123         DBOBJDefinitionField*   l_pDefinitionField;
00124         DBOBJDefinitionNode*    l_pDefinitionChild;
00125 
00126         const char*             l_pszTableName = 0;
00127         const char*             l_pszFieldName = 0;
00128         char*                   l_pszFieldDBSpec = 0;
00129         char*                   l_pszFieldDDL = 0;
00130         const char*             l_pszSep = 0;
00131         const char*             l_pszOTLType = 0;
00132         const char*             l_pszDBType = 0;
00133         const char*             l_pszDBSize = 0;
00134         const char*             l_pszDBScale = 0;
00135         const char*             l_pszDBPrec = 0;
00136         const char*             l_pszDBNullOK = 0;
00137         char*                   l_pszTmp = 0;
00138         char*                   l_pszTmp2 = 0;
00139     
00140         unsigned int            l_uiFieldsCount;
00141         unsigned int            l_uiChildrenCount;
00142         unsigned int            i;
00143         //_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
00144 
00145         DEBUG_IN(DDLStreamer::makeNodeDDL)
00146 
00147         PREP_CATCH_XML_FLOW_ERROR;
00148 
00149         WATCH_XML_FLOW_ERROR {
00150 
00151             l_pszTableName=p_pDefinitionNode->getDBTable();
00152             if ( strlen( l_pszTableName) == 0) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_DDLHANDLER + ERRCODE_CAUSE_MISSINGREF , "Table name is missing in definition." , "", "DDLStreamer::makeNodeDDL", "", false);
00153 
00154             (*p_pOutput) << "<" << p_pDefinitionNode->getName() << ">";
00155             (*p_pOutput) << "\nCREATE TABLE " << l_pszTableName << " ( ";
00156             
00157             l_uiFieldsCount = p_pDefinitionNode->getFieldsCount();
00158             l_pszSep="";
00159             for(i=0 ; i<l_uiFieldsCount ; i++)
00160             {
00161                 // Get field props
00162                 l_pDefinitionField = p_pDefinitionNode->getField(i);
00163                 l_pszFieldName = l_pDefinitionField->getDBField();
00164                 if ( strlen( l_pszFieldName) == 0) THROW_XMLFLOW_EXCEPTION ( ERRCODE_LOC_DDLHANDLER + ERRCODE_CAUSE_MISSINGREF , "Field name is missing in definition of table " , l_pszTableName, "DDLStreamer::makeNodeDDL", "", false);
00165                             
00166                 l_pszFieldDBSpec = copyCharBuffer( l_pDefinitionField->getDBSpecProps());
00167                 l_pszDBNullOK="1";
00168 
00169                 // Generate field DDL
00170                 if( strlen( l_pszFieldDBSpec) == 0) l_pszFieldDDL=getColumnDDL_Simple( l_pDefinitionField->getMap());
00171                 else
00172                 {           
00173                     l_pszFieldDDL = OTL_getColumnDDL( l_pszFieldDBSpec, DBStreamerFactory::getSQLSyntax()); //l_pszOTLType, l_pszDBType, l_pszDBSize, l_pszDBScale, l_pszDBPrec, l_pszDBNullOK);
00174                     l_pszFieldDBSpec = releaseCharBuffer( l_pszFieldDBSpec);
00175                 }
00176 
00177                 (*p_pOutput) <<  l_pszSep << "\n\t" << l_pszFieldName << " " << l_pszFieldDDL;
00178                 if(  strcmp( l_pszDBNullOK, REQ_FALSE) == 0  ) (*p_pOutput) << " NOT NULL ";                    
00179                 l_pszSep=",";
00180                 l_pszFieldDDL = releaseCharBuffer( l_pszFieldDDL);
00181             }
00182 
00183             (*p_pOutput) << "\n)\n";
00184             (*p_pOutput) << "</" << p_pDefinitionNode->getName() << ">\n";
00185 
00186             l_uiChildrenCount = p_pDefinitionNode->getChildNodesCount();
00187             for( i=0 ; i<l_uiChildrenCount ; i++)
00188             {
00189                 l_pDefinitionChild = p_pDefinitionNode->getChildNode(i);
00190                 makeNodeDDL( l_pDefinitionChild, p_pOutput);
00191             }
00192 
00193         } CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN;
00194 
00195     RELEASE_AND_RETURN:
00196 
00197         // release 
00198         l_pszFieldDBSpec = releaseCharBuffer( l_pszFieldDBSpec);
00199         l_pszFieldDDL = releaseCharBuffer( l_pszFieldDDL);
00200 
00201         DEBUG_OUT(DDLStreamer::makeNodeDDL)
00202         ON_XML_FLOW_ERROR_THROW;
00203     }
00204 
00205 
00206     //_________________________________________________________________________
00207     //  GETCOLUMNDDL_SIMPLE
00208     //-------------------------------------------------------------------------
00209     char* DDLStreamer::getColumnDDL_Simple( const char* p_pszReducedType) const
00210     {
00211         char* l_pszDDL;
00212 
00213         DEBUG_FUNC(DDLStreamer::getColumnDDL_Simple)
00214         
00215         if( strcmp( p_pszReducedType, DBOBJDEFFIELD_MAP_NUM) == 0 )     l_pszDDL = copyCharBuffer( "DOUBLE");
00216         if( strcmp( p_pszReducedType, DBOBJDEFFIELD_MAP_STRING) == 0 )
00217         {
00218             l_pszDDL = copyCharBuffer( "VARCHAR(");
00219             l_pszDDL = concatCharBuffer( l_pszDDL, DDLSTREAMER_VARCHARSIZE);
00220             l_pszDDL = concatCharBuffer( l_pszDDL, ")");
00221         }
00222         if( strcmp( p_pszReducedType, DBOBJDEFFIELD_MAP_DATE) == 0 ) 
00223         {
00224             if (DBStreamerFactory::getSQLSyntax()==SQL_SYNTAX_ORA)
00225             {
00226                 l_pszDDL= copyCharBuffer( "DATE" );
00227             }
00228             else if (DBStreamerFactory::getSQLSyntax()==SQL_SYNTAX_MSSQL)
00229             {
00230                 l_pszDDL= copyCharBuffer( "TIMESTAMP" );
00231             }
00232             else if (DBStreamerFactory::getSQLSyntax()==SQL_SYNTAX_DB2)
00233             {
00234                 l_pszDDL= copyCharBuffer( "TIMESTAMP" );
00235             }
00236                         
00237         }
00238         return l_pszDDL;
00239     }
00240 
00241 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00242 //=============================================================================
00243 
00244 
00245 //=============================================================================
00246 //  CLASS DDLStreamerFactory
00247 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00248 
00249     XMLStreamer* DDLStreamerFactory::getStreamer(
00250         StreamerParams*         p_pParameters, 
00251         XMLFlowContext* p_pStreamContext, 
00252         XMLStreamConsumer*      p_pStreamConsumer) const
00253     {
00254         SAXDBOBJDefinitionBuilder*  l_pSAXDefinitionBuilder = 0;
00255 
00256         l_pSAXDefinitionBuilder = new SAXDBOBJDefinitionBuilder( p_pStreamContext);
00257         return (XMLStreamer*) new DDLStreamer( this, p_pParameters, p_pStreamContext, p_pStreamConsumer, l_pSAXDefinitionBuilder, false);
00258     }
00259 
00260 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00261 //=============================================================================
00262 
00263 
00264 END_XDFLENGINE_NS
00265 
00266 
00267 

Generated on Sat Oct 4 13:19:58 2003 for XDFLengine by doxygen1.3-rc2