Added 'length' to area.
[UnsignedByte.git] / src / DAL / TableDef.cpp
blob8520e659c68870dde279cba1111d74be501faae0
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
21 #include <iostream>
22 #include <sstream>
24 #include "TableDef.h"
25 #include "FieldDef.h"
26 #include "KeyDef.h"
27 #include "Global.h"
28 #include "StringUtilities.h"
30 TableDef::TableDef(std::string name) :
31 m_name(name),
32 m_primarykeysize(0)
34 std::string foreignname;
35 foreignname.append("fk");
37 std::string tablename = name;
38 char first = tablename[0];
39 first = toupper(first);
41 tablename[0] = first;
42 foreignname.append(tablename);
44 m_foreignname = foreignname;
47 TableDef::~TableDef()
52 cstring TableDef::getName() const
54 return m_name;
57 void TableDef::addPK(const std::string& name)
59 FieldDefPtr field(new KeyDef(shared_from_this(), name, true));
60 m_deffields.push_back(field);
61 m_primarykeysize++;
64 void TableDef::addFPK(TableDefPtr table)
66 Assert(table);
67 addFPK(table, Global::Get()->EmptyString);
70 void TableDef::addFPK(TableDefPtr table, const std::string& suffix)
72 Assert(table);
74 std::string name;
75 name.append(table->tableForeignName());
76 name.append(suffix);
78 FieldDefPtr field(new KeyDef(table, name, true));
79 m_deffields.push_back(field);
80 m_primarykeysize++;
83 void TableDef::addValue(const std::string& name)
85 FieldDefPtr field(new FieldDef(name, false));
86 m_deffields.push_back(field);
89 void TableDef::addValue(const std::string& name, value_type defaultvalue)
91 FieldDefPtr field(new FieldDef(name, false, String::Get()->fromInt(defaultvalue)));
92 m_deffields.push_back(field);
95 void TableDef::addTextField(const std::string& name)
97 FieldDefPtr field(new FieldDef(name, true));
98 m_deffields.push_back(field);
101 void TableDef::addLookupTextField(const std::string& name)
103 FieldDefPtr field(new FieldDef(name, true, true));
104 m_deffields.push_back(field);
107 void TableDef::addTextField(const std::string& name, const std::string& defaulttext)
109 FieldDefPtr field(new FieldDef(name, true, defaulttext));
110 m_deffields.push_back(field);
113 void TableDef::addFK(TableDefPtr table)
115 Assert(table);
117 addFK(table, Global::Get()->EmptyString);
120 void TableDef::addFK(TableDefPtr table, const std::string& suffix)
122 Assert(table);
124 std::string name;
125 name.append(table->tableForeignName());
126 name.append(suffix);
128 FieldDefPtr field(new KeyDef(table, name, false));
129 m_deffields.push_back(field);
130 // TODO, add SQLite triggers
133 const std::string& TableDef::tableForeignName() const
135 return m_foreignname;