Initial
This commit is contained in:
425
ClassGen/Main.cpp
Normal file
425
ClassGen/Main.cpp
Normal file
@@ -0,0 +1,425 @@
|
||||
#include <common/file.hpp>
|
||||
#include <common/profile.hpp>
|
||||
#include <common/string.hpp>
|
||||
#include <common/block.hpp>
|
||||
#include <ClassGen/NameValuePair.hpp>
|
||||
|
||||
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<NameValuePair> &variables);
|
||||
bool createCSharpDataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block<NameValuePair> &variables,Block<NameValuePair> &originals);
|
||||
bool createJAVAClass(const String &packageName,const String &className,Block<NameValuePair> &variables);
|
||||
bool createJAVADataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block<NameValuePair> &variables,Block<NameValuePair> &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<NameValuePair> 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<NameValuePair> variables;
|
||||
Block<NameValuePair> 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<NameValuePair> &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.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=variables[index];
|
||||
outFile.writeLine(String(" private ")+nameValuePair.getValue()+String(" ")+nameValuePair.getName()+String(";"));
|
||||
}
|
||||
for(index=0;index<variables.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=variables[index];
|
||||
outFile.writeLine(String(" public ")+nameValuePair.getValue()+String(" ")+makeFirstUpper(nameValuePair.getName()));
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(String(" get{return ")+nameValuePair.getName()+String(";}"));
|
||||
outFile.writeLine(String(" set{")+nameValuePair.getName()+String("=value;}"));
|
||||
outFile.writeLine(" }");
|
||||
}
|
||||
outFile.writeLine(" }");
|
||||
if(closeNameSpace)outFile.writeLine("}");
|
||||
outFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ClassMapper::createCSharpDataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block<NameValuePair> &variables,Block<NameValuePair> &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<originals.size();index++)
|
||||
{
|
||||
select+=originals[index].getName();
|
||||
if(index<originals.size()-1)select+=String(", ");
|
||||
}
|
||||
select+=String(" from ")+tableName+String("\";");
|
||||
outFile.writeLine(select);
|
||||
outFile.writeLine(String(" sqlConnection=createSqlConnection(")+databaseName+String(",")+tableName+String(",\"sa\",\"\");"));
|
||||
outFile.writeLine(" sqlCommand=new SqlCommand(strQuery,sqlConnection);");
|
||||
outFile.writeLine(" sqlDataReader=sqlCommand.ExecuteReader();");
|
||||
outFile.writeLine(" while(sqlDataReader.Read())");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(String(" ")+className+String(" ")+instanceName+String("=new ")+className+String("();"));
|
||||
for(index=0;index<variables.size();index++)
|
||||
{
|
||||
NameValuePair &original=originals[index];
|
||||
NameValuePair &variable=variables[index];
|
||||
outFile.writeLine(String(" ")+instanceName+String(".")+makeFirstUpper(variable.getName())+String("=sqlDataReader.get")+makeFirstUpper(variable.getValue())+String("(")+String().fromInt(index)+String(");"));
|
||||
}
|
||||
outFile.writeLine(String(" arrayList.add(")+instanceName+String(");"));
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" return arrayList;");
|
||||
outFile.writeLine(" }");
|
||||
|
||||
outFile.writeLine(" finally");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" if(null!=sqlDataReader)sqlDataReader.Close();");
|
||||
outFile.writeLine(" if(null!=sqlConnection)sqlConnection.Close();");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" }");
|
||||
|
||||
String connectionString=" string connectionString=\"Data Source="+databaseName+";User ID="+"sa;password=\";";
|
||||
outFile.writeLine(" private static SqlConnection createSqlConnection(string datssource,string database,string username,string password)");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" try");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(connectionString);
|
||||
|
||||
outFile.writeLine(" SqlConnection connection=new SqlConnection(connectionString);");
|
||||
outFile.writeLine(" connection.Open();");
|
||||
outFile.writeLine(" return connection;");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" catch(SqlException exception)");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" Console.WriteLine(exception.ToString());");
|
||||
outFile.writeLine(" return null;");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" }");
|
||||
if(closeNameSpace)outFile.writeLine("}");
|
||||
outFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ClassMapper::createJAVAClass(const String &packageName,const String &className,Block<NameValuePair> &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.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=variables[index];
|
||||
outFile.writeLine(String(" private ")+nameValuePair.getValue()+String(" ")+nameValuePair.getName()+String(";"));
|
||||
}
|
||||
for(index=0;index<variables.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=variables[index];
|
||||
outFile.writeLine(String(" public ")+nameValuePair.getValue()+String(" ")+makeAccessor(nameValuePair.getName())+String("()"));
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(String(" return ")+nameValuePair.getName()+String(";"));
|
||||
outFile.writeLine(" }");
|
||||
|
||||
outFile.writeLine(String(" public void ")+makeMutator(nameValuePair.getName())+String("(")+nameValuePair.getValue()+String(" ")+nameValuePair.getName()+String(")"));
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(String(" this.")+nameValuePair.getName()+String("=")+nameValuePair.getName()+String(";"));
|
||||
outFile.writeLine(" }");
|
||||
}
|
||||
outFile.writeLine("};");
|
||||
outFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ClassMapper::createJAVADataAccess(const String &packageName,const String &className,const String &instanceName,const String &databaseName,const String &tableName,Block<NameValuePair> &variables,Block<NameValuePair> &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<originals.size();index++)
|
||||
{
|
||||
select+=originals[index].getName();
|
||||
if(index<originals.size()-1)select+=String(", ");
|
||||
}
|
||||
select+=String(" from ")+tableName+String("\";");
|
||||
outFile.writeLine(select);
|
||||
outFile.writeLine(" rs=statement.executeQuery(strQuery);");
|
||||
outFile.writeLine(" while(rs.next())");
|
||||
outFile.writeLine(" {");
|
||||
for(index=0;index<variables.size();index++)
|
||||
{
|
||||
NameValuePair &original=originals[index];
|
||||
NameValuePair &variable=variables[index];
|
||||
outFile.writeLine(String(" ")+instanceName+String(".")+makeMutator(variable.getName())+String("(rs.get")+makeFirstUpper(variable.getValue())+String("(\"")+original.getName()+String("\"));"));
|
||||
}
|
||||
outFile.writeLine(String(" list.add(")+instanceName+String(");"));
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" return list;");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" finally");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" if(null!=rs)rs.close();");
|
||||
outFile.writeLine(" if(null!=statement)statement.close();");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" }");
|
||||
|
||||
|
||||
outFile.writeLine(" private Connection getConnection()throws SQLException");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" try");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" InitialContext jndiCntx=new InitialContext();");
|
||||
outFile.writeLine(String(" DataSource ds=(DataSource)jndiCntx.lookup(\"java:") +databaseName+String("\")"));
|
||||
outFile.writeLine(" jndiCntx.close();");
|
||||
outFile.writeLine(" return ds.getConnection();");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" catch(NamingException exception)");
|
||||
outFile.writeLine(" {");
|
||||
outFile.writeLine(" message(\"[getConnection] Object not found\");");
|
||||
outFile.writeLine(" throw new EJBException(exception);");
|
||||
outFile.writeLine(" }");
|
||||
outFile.writeLine(" }");
|
||||
|
||||
outFile.writeLine("};");
|
||||
outFile.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ClassMapper::mapType(const String &type,String &mapped)
|
||||
{
|
||||
for(int index=0;index<mNameValuePairs.size();index++)
|
||||
{
|
||||
NameValuePair &nameValuePair=mNameValuePairs[index];
|
||||
if(nameValuePair.getName()==type)return mapped=nameValuePair.getValue(),true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ClassMapper::loadMapping(const String &strPathMapFile)
|
||||
{
|
||||
File inFile;
|
||||
String strLine;
|
||||
String key;
|
||||
String value;
|
||||
|
||||
mNameValuePairs.remove();
|
||||
if(!inFile.open(strPathMapFile,"rb"))return false;
|
||||
while(true)
|
||||
{
|
||||
inFile.readLine(strLine);
|
||||
if(strLine.isNull())break;
|
||||
key=strLine.betweenString(0,'=');
|
||||
value=strLine.betweenString('=',0);
|
||||
mNameValuePairs.insert(&NameValuePair(key,value));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
String ClassMapper::makeHungarian(const String &name)
|
||||
{
|
||||
String hungarianName;
|
||||
|
||||
int length=name.length();
|
||||
for(int index=0;index<length;index++)
|
||||
{
|
||||
char ch=name.charAt(index);
|
||||
if(ch=='_')
|
||||
{
|
||||
if(index+1>=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 <java{c#}><packagename><classname><databasename><tablename><pathtomappingtable><pathtotable>\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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user