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 ***************************************************************************/
24 #include "TableImpl.h"
25 #include "FieldImpl.h"
27 #include "StringUtilities.h"
29 Keys::Keys(TableImplPtr table
) :
35 Keys::Keys(TableImplPtr table
, cstring initstring
) :
43 for(FieldImplVector::const_iterator it
= m_table
->begin(); it
!= m_table
->end(); it
++)
45 FieldImplPtr field
= *it
;
48 if(!field
->isPrimaryKey())
51 KeyImplPtr keyimpl
= boost::static_pointer_cast
<KeyImpl
>(field
);
54 std::string keystring
= p
.getword();
55 int value
= atoi(keystring
.c_str());
56 KeyValuePtr
key(new KeyValue(keyimpl
, value
));
61 catch(std::exception
& e
)
63 throw std::invalid_argument("Keys::Keys(), could not parse initstring.");
72 KeyValuePtr
Keys::getKey(KeyImplPtr key
) const
75 KeyImplMap::const_iterator it
= m_keys
.find(key
.get());
76 Assert(it
!= m_keys
.end());
81 TableImplPtr
Keys::getTable() const
86 void Keys::addKey(KeyValuePtr 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
)
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
111 for(KeyImplMap::const_iterator it
= m_keys
.begin(); it
!= m_keys
.end(); it
++)
113 if(it
!= m_keys
.begin())
116 KeyValuePtr key
= it
->second
;
117 result
.append(String::Get()->fromInt(key
->getIntegerValue()));
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
;
135 KeyImplMap::const_iterator
Keys::begin() const
137 return m_keys
.begin();
140 KeyImplMap::const_iterator
Keys::end() const
145 KeyImplMap::const_iterator
Keys::find(KeyImplPtr key
) const
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())
166 KeyValuePtr otherKey
= other
->second
;
167 if(key
->getIntegerValue() != otherKey
->getIntegerValue())
172 Strings
Keys::getDiff(KeysPtr orig
) const
176 for(KeyImplMap::const_iterator it
= m_keys
.begin(); it
!= m_keys
.end(); it
++)
178 KeyValuePtr key
= it
->second
;
183 KeyImplPtr keyimpl
= key
->getKeyImpl();
186 KeyValuePtr origKey
= orig
->getKey(keyimpl
);
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()));
197 result
.push_back(line
);