1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
22 #include "ClassSourceGenerator.h"
28 #include "StringUtilities.h"
32 ClassSourceGenerator::ClassSourceGenerator(TableDefPtr table
, std::ofstream
* file
) :
39 m_name
= m_table
->getName();
42 ClassSourceGenerator::~ClassSourceGenerator()
47 void ClassSourceGenerator::GenerateClass()
49 Assert(m_table
->primarykeysize() > 0);
53 (*m_file
) << "void " << m_name
<< "::Initialize()" << endl
;
54 (*m_file
) << "{" << endl
;
55 for(FieldDefVector::const_iterator it
= m_table
->begin(); it
!= m_table
->end(); it
++)
57 FieldDefPtr field
= *it
;
59 (*m_file
) << m_tabs
<< String::Get()->toupper((*it
)->getName()) << " = ";
61 // C++ -does- have reflection, it does now at least.
64 KeyDef
* keydef
= (KeyDef
*)field
.get();
65 // KeyImpl(TableImplPtr <these ones>, TableImplPtr <these ones>, ...);
66 (*m_file
) << "KeyImplPtr(new KeyImpl";
67 (*m_file
) << "(TableImpls::Get()->" << String::Get()->toupper(m_table
->getName());
68 (*m_file
) << ", TableImpls::Get()->" << String::Get()->toupper(keydef
->getForeignTable()->getName());
72 // FieldImpl(TableImplPtr <this one>, ...);
73 (*m_file
) << "FieldImplPtr(new FieldImpl";
74 (*m_file
) << "(TableImpls::Get()->" << String::Get()->toupper(m_table
->getName());
77 // ...Impl(TableImplPtr table, ..., cstring <this one>, ...);
78 (*m_file
) << ", \"" << (*it
)->getName() << "\"";
82 if(field
->isPrimaryKey())
84 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool <primary>, ...);
85 (*m_file
) << ", true";
87 if(m_table
->primarykeysize() == 1)
89 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
90 (*m_file
) << ", true";
94 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
95 (*m_file
) << ", false";
100 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool <primary>, ...);
101 (*m_file
) << ", false";
103 if(field
->isLookup())
105 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
106 (*m_file
) << ", true";
110 // KeyImpl(TableImplPtr nativeTable, TableImplPtr foreignTable, cstring name, bool primary, bool <lookup>);
111 (*m_file
) << ", false";
119 // FieldImpl(TableImplPtr table, cstring name, bool <text>, ...);
120 (*m_file
) << ", true";
124 // FieldImpl(TableImplPtr table, cstring name, bool <text>, ...);
125 (*m_file
) << ", false";
128 std::string defaultvalue
= field
->getDefaultValue();
129 if(defaultvalue
.size() > 0)
131 // FieldImpl(TableImplPtr table, cstring name, bool text, const std::string& <defaultvalue>);
132 (*m_file
) << ", std::string(\"" << defaultvalue
<< "\")";
136 if(field
->isLookup())
138 // FieldImpl(TableImplPtr table, cstring name, bool text, bool <lookup>);
139 (*m_file
) << ", true";
143 // FieldImpl(TableImplPtr table, cstring name, bool text, bool <lookup>);
144 (*m_file
) << ", false";
154 for(FieldDefVector::const_iterator it
= m_table
->begin(); it
!= m_table
->end(); it
++)
156 FieldDefPtr field
= *it
;
159 (*m_file
) << m_tabs
<< "m_fields.push_back(" << String::Get()->toupper((*it
)->getName()) << ");" << endl
;
163 (*m_file
) << m_tabs
<< "updateFieldCount();" << endl
;
165 (*m_file
) << "}" << endl
;