#include #include #include #include #include class ClassMapper { public: ClassMapper(); virtual ~ClassMapper(); bool createClass(const String &language,const String &packageName,String className,const String &databaseName,const String &tableName,const String &strPathMapFile,const String &strPathTable); private: bool loadMapping(const String &strPathMapFile); bool mapType(const String &type,String &mapped); bool createCSharpClass(const String &packageName,const String &className,Block &variables); bool createCSharpDataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block &variables,Block &originals); bool createJAVAClass(const String &packageName,const String &className,Block &variables); bool createJAVADataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block &variables,Block &originals); String makeHungarian(const String &name); String makeAccessor(const String &name); String makeMutator(const String &name); String makeFirstUpper(const String &name); String makeFirstLower(const String &name); Block mNameValuePairs; }; ClassMapper::ClassMapper() { } ClassMapper::~ClassMapper() { } bool ClassMapper::createClass(const String &language,const String &packageName,String className,const String &databaseName,const String &tableName,const String &strPathMapFile,const String &strPathTable) { File inFile; String strLine; String name; String value; String instanceName; Block variables; Block originals; int errors=0; if(!loadMapping(strPathMapFile))return false; if(!inFile.open(strPathTable,"rb"))return false; while(true) { inFile.readLine(strLine); if(strLine.isNull())break; name=strLine.betweenString(0,' '); value=strLine.betweenString(' ',' '); if(!mapType(value,value)) { printf("Don't know how to map %s\n",value.str()); errors++; continue; } variables.insert(&NameValuePair(makeFirstLower(makeHungarian(name)),value)); originals.insert(&NameValuePair(name,value)); } inFile.close(); if(errors)return false; if(!variables.size())return false; instanceName=makeFirstLower(className); className=makeFirstUpper(className); if(language=="JAVA") { createJAVAClass(packageName,className,variables); createJAVADataAccess(packageName,className,instanceName,databaseName,tableName,variables,originals);\ } else if(language=="C#") { createCSharpClass(packageName,className,variables); createCSharpDataAccess(packageName,className,instanceName,databaseName,tableName,variables,originals);\ } printf("generation completed.\n"); return true; } bool ClassMapper::createCSharpClass(const String &packageName,const String &className,Block &variables) { File outFile; bool closeNameSpace=false; if(!outFile.open(className+String(".cs"),"wb"))return false; outFile.writeLine("using System;"); outFile.writeLine("\t"); outFile.writeLine("\t"); if(!packageName.isNull()) { outFile.writeLine(String("namespace ")+packageName); outFile.writeLine("{"); closeNameSpace=true; } outFile.writeLine(" [Serializable]"); outFile.writeLine(String(" public class ")+className); outFile.writeLine(" {"); for(int index=0;index &variables,Block &originals) { File outFile; String select; bool closeNameSpace=false; if(!outFile.open(className+String("DA")+String(".cs"),"wb"))return false; outFile.writeLine("using System;"); outFile.writeLine("using System.Collections;"); outFile.writeLine("using System.Data.SqlClient;"); outFile.writeLine("\t"); if(!packageName.isNull()) { outFile.writeLine(String("namespace ")+packageName); outFile.writeLine("{"); closeNameSpace=true; } outFile.writeLine(String(" public class ")+className+String("DA")); outFile.writeLine(" {"); outFile.writeLine(" public ArrayList readAll()"); outFile.writeLine(" {"); outFile.writeLine(" SqlConnection sqlConnection=null;"); outFile.writeLine(" SqlDataReader sqlDataReader=null;"); outFile.writeLine(" SqlCommand sqlCommand=null;"); outFile.writeLine(" try"); outFile.writeLine(" {"); outFile.writeLine(" ArrayList arrayList=new ArrayList();"); select=" string strQuery=\"select "; for(int index=0;index &variables) { File outFile; if(!outFile.open(className+String(".java"),"wb"))return false; if(!packageName.isNull())outFile.writeLine(String("package ")+packageName); outFile.writeLine("\t"); outFile.writeLine("\t"); outFile.writeLine(String("public class ")+className); outFile.writeLine("{"); for(int index=0;index &variables,Block &originals) { File outFile; String select; if(!outFile.open(className+String("DA")+String(".java"),"wb"))return false; if(!packageName.isNull())outFile.writeLine(String("package ")+packageName); outFile.writeLine("import java.util.*;"); outFile.writeLine("import javax.naming.*;"); outFile.writeLine("import java.sql.*;"); outFile.writeLine("import javax.sql.*;"); outFile.writeLine("import javax.ejb.*"); outFile.writeLine("\t"); outFile.writeLine("\t"); outFile.writeLine(String("public class ")+className+String("DA")); outFile.writeLine("{"); outFile.writeLine(" public List readAll()throws SQLException"); outFile.writeLine(" {"); outFile.writeLine(" Statement statement=null;"); outFile.writeLine(" ResultSet rs=null;"); outFile.writeLine(" Connection connection=null;"); outFile.writeLine(" String strQuery=null;"); outFile.writeLine(" List list=null;"); outFile.writeLine("\t"); outFile.writeLine(" try"); outFile.writeLine(" {"); outFile.writeLine(String(" ")+className+String(" ")+instanceName+String(" = new ")+className+String("();")); outFile.writeLine(" connection=getConnection();"); outFile.writeLine(" statement=connection.createStatement();"); outFile.writeLine(" list=new ArrayList();"); select=" strQuery=\"select "; for(int index=0;index=length)break; index++; ch=toupper(name.charAt(index)); hungarianName+=ch; } else hungarianName+=ch; } return hungarianName; } String ClassMapper::makeAccessor(const String &name) { String str; str+="get"; str+=toupper(name.charAt(0)); str+=name.substr(1); return str; } String ClassMapper::makeMutator(const String &name) { String str; str+="set"; str+=toupper(name.charAt(0)); str+=name.substr(1); return str; } String ClassMapper::makeFirstUpper(const String &name) { String str; str+=toupper(name.charAt(0)); str+=name.substr(1); return str; } String ClassMapper::makeFirstLower(const String &name) { String str; str+=tolower(name.charAt(0)); str+=name.substr(1); return str; } int main(int argc,char **argv) { if(8!=argc) { printf("USAGE: mapclass \n"); printf("(ie) mapclass c# zbi.risk.server.vhi.mapped Historic DRMS dt_main_positions c:\\work\\classgen\\mapping.txt c:\\work\\classgen\\table.txt\n"); printf("where 'table.txt' contains lines of pairs...\n"); printf("trade_date smalldatetime 4 0\n\n"); printf("and 'mapping.txt' contains lines of pairs...\n"); printf("nvarchar=string\n"); printf("decimal=decimal\n"); return 0; } ClassMapper classMapper; if(!classMapper.createClass(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6],argv[7]))return 1; return 0; }