-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeanLoader_impl.hpp
70 lines (56 loc) · 1.76 KB
/
BeanLoader_impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef BeanLoader_IMPL_HPP_INCLUDED
#define BeanLoader_IMPL_HPP_INCLUDED
namespace hiberlite{
template<class AV>
void LoadBean::notifyInitWalk(AV& av)
{
while(!stmt.empty())
stmt.pop();
sqlid_t curId=av.getRootId();
std::string query="SELECT * FROM "+av.getScope().table()
+" WHERE "+HIBERLITE_PRIMARY_KEY_COLUMN+"="+Transformer::toSQLiteValue(curId)+";";
SQLiteSelect sel(av.getConnection(), query);
if(!sel.step())
throw database_error("Id " + std::to_string(curId) + " not found in table " + av.getScope().table());
stmt.push( std::make_pair(sel,curId) );
}
template<class AV>
void LoadBean::notifyDoneWalk(AV& av)
{
while(!stmt.empty())
stmt.pop();
}
template<class AV, class E, class S>
void LoadBean::act(AV& av, collection_nvp<E,S> nvp )
{
av.diveTable(nvp.name);
std::string tab=av.getScope().table();
std::vector<sqlid_t> children=Database::dbSelectChildIds(av.getConnection(), tab, stmt.top().second);
nvp.stream.startLoadFromDb();
for(size_t i=0;i<children.size();i++){
E* entry;
construct(av,&entry,static_cast<unsigned int>(0));
std::string query="SELECT * FROM "+tab
+" WHERE "+HIBERLITE_PRIMARY_KEY_COLUMN+"="+Transformer::toSQLiteValue(children[i])+";";
SQLiteSelect sel(av.getConnection(), query);
if(!sel.step())
throw database_error("id not found in the database");
stmt.push( std::make_pair(sel,children[i]) );
sql_nvp<E> el("item", *entry);
av & el;
stmt.pop();
nvp.stream.putNext(*entry);
destroy(*this, entry, static_cast<unsigned int>(0));
}
av.pop();
}
template<class C>
C* BeanLoader::loadBean(bean_key key)
{
C* bean;
construct(*this, &bean, static_cast<unsigned int>(0));
startWalk(*bean, key);
return bean;
}
} //namespace hiberlite
#endif // BEANUPDATER_IMPL_HPP_INCLUDED