00001
00002
00003
00004
00005
00006
00007
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
00048
00049
00050
00051
00052
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
00062
00063 DDLStreamer::~DDLStreamer()
00064 {
00065 DEBUG_DEL(DDLStreamer)
00066 }
00067
00068
00069
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
00088 WATCH_XML_FLOW_ERROR
00089 {
00090
00091 l_pDefinitionRoot = ((SAXDBOBJDefinitionBuilder*)m_pHandler)->getDefinition();
00092
00093 makeNodeDDL( l_pDefinitionRoot, m_pOutput);
00094 }
00095 CATCH_XML_FLOW_ERROR_RELEASE_AND_RETURN ;
00096 }
00097
00098 RELEASE_AND_RETURN:
00099
00100
00101
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
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
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
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());
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
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
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
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