Added 'length' to area.
[UnsignedByte.git] / src / DAL / Keys.cpp
blob16536f9786eff72ce17c89b91809f0b6a85ccf50
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 "Keys.h"
22 #include "KeyValue.h"
23 #include "KeyImpl.h"
24 #include "TableImpl.h"
25 #include "FieldImpl.h"
26 #include "Parse.h"
27 #include "StringUtilities.h"
29 Keys::Keys(TableImplPtr table) :
30 m_table(table)
32 Assert(table);
35 Keys::Keys(TableImplPtr table, cstring initstring) :
36 m_table(table)
38 Assert(table);
39 Parse p(initstring);
41 try
43 for(FieldImplVector::const_iterator it = m_table->begin(); it != m_table->end(); it++)
45 FieldImplPtr field = *it;
46 Assert(field);
48 if(!field->isPrimaryKey())
49 continue;
51 KeyImplPtr keyimpl = boost::static_pointer_cast<KeyImpl>(field);
52 Assert(keyimpl);
54 std::string keystring = p.getword();
55 int value = atoi(keystring.c_str());
56 KeyValuePtr key(new KeyValue(keyimpl, value));
58 addKey(key);
61 catch(std::exception& e)
63 throw std::invalid_argument("Keys::Keys(), could not parse initstring.");
67 Keys::~Keys()
72 KeyValuePtr Keys::getKey(KeyImplPtr key) const
74 Assert(key);
75 KeyImplMap::const_iterator it = m_keys.find(key.get());
76 Assert(it != m_keys.end());
78 return it->second;
81 TableImplPtr Keys::getTable() const
83 return m_table;
86 void Keys::addKey(KeyValuePtr key)
88 Assert(key);
89 Assert(key->getTable() == m_table);
90 Assert(key->isPrimaryKey());
92 KeyImpl* keyimpl = key->getKeyImpl().get();
93 m_keys[keyimpl] = key;
96 void Keys::addKey(KeyImplPtr keyimpl, value_type id)
98 Assert(keyimpl);
99 Assert(id);
100 Assert(keyimpl->getTable() == m_table);
102 KeyValuePtr key(new KeyValue(keyimpl, id));
104 m_keys[key->getKeyImpl().get()] = key;
107 std::string Keys::toString() const
109 std::string result;
111 for(KeyImplMap::const_iterator it = m_keys.begin(); it != m_keys.end(); it++)
113 if(it != m_keys.begin())
114 result.append(", ");
116 KeyValuePtr key = it->second;
117 result.append(String::Get()->fromInt(key->getIntegerValue()));
120 return result;
123 size_t Keys::size() const
125 return m_keys.size();
128 KeyValuePtr Keys::first() const
130 Assert(m_keys.size() == 1);
131 KeyValuePtr key = m_keys.begin()->second;
132 return key;
135 KeyImplMap::const_iterator Keys::begin() const
137 return m_keys.begin();
140 KeyImplMap::const_iterator Keys::end() const
142 return m_keys.end();
145 KeyImplMap::const_iterator Keys::find(KeyImplPtr key) const
147 Assert(key);
149 return m_keys.find(key.get());
152 void Keys::setDirty(KeysPtr oldKeys)
154 for(KeyImplMap::iterator it = m_keys.begin(); it != m_keys.end(); it++)
156 KeyValuePtr key = it->second;
157 KeyImplMap::const_iterator other = oldKeys->find(key->getKeyImpl());
159 // new key (previously empty)
160 if(other == oldKeys->end())
162 key->setDirty(true);
163 continue;
166 KeyValuePtr otherKey = other->second;
167 if(key->getIntegerValue() != otherKey->getIntegerValue())
168 key->setDirty(true);
172 Strings Keys::getDiff(KeysPtr orig) const
174 Strings result;
176 for(KeyImplMap::const_iterator it = m_keys.begin(); it != m_keys.end(); it++)
178 KeyValuePtr key = it->second;
179 Assert(key);
180 if(!key->isDirty())
181 continue;
183 KeyImplPtr keyimpl = key->getKeyImpl();
184 Assert(keyimpl);
186 KeyValuePtr origKey = orig->getKey(keyimpl);
187 Assert(origKey);
189 std::string line = "Changed key '";
190 line.append(keyimpl->getName());
191 line.append("' from '");
192 line.append(String::Get()->fromInt(origKey->getIntegerValue()));
193 line.append("' to '");
194 line.append(String::Get()->fromInt(key->getIntegerValue()));
195 line.append("'.");
197 result.push_back(line);
200 return result;