-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeanUpdater.h
157 lines (130 loc) · 3.27 KB
/
BeanUpdater.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef BEANUPDATER_H
#define BEANUPDATER_H
#include <stdio.h>
#include <string.h>
namespace hiberlite{
struct RowScope{
static const int FirstAtom=100;
std::vector<abstract_atom*> atoms;
std::string table;
std::string query;
sqlid_t id;
bool needComma;
template<class C>
inline void addAssignment(const std::string name, db_atom<C>& atom);
void addSimpleAssign(const std::string name, std::string value);
~RowScope();
};
class Transformer{
public:
static std::string toSQLiteValue(const double& val){
char s[64];
sprintf(s,"%lf",val);
return s;
}
static std::string toSQLiteValue(const float& val){
char s[64];
sprintf(s,"%f",val);
return s;
}
static std::string toSQLiteValue(const long double& val){
char s[64];
sprintf(s,"%Lf",val);
return s;
}
#define HIBERLITE_STRINGIFY_NUM(VALTYPE) \
static std::string toSQLiteValue(const VALTYPE &val){ \
char s[64]; \
char* p=s+63; \
*(p--)=0; \
VALTYPE tmp=val; \
bool needmin=0; \
if(tmp<(VALTYPE)0) \
needmin=1,tmp*=-1; \
do{ \
*(p--)='0' + (tmp%10); \
tmp/=10; \
}while(tmp); \
if(needmin) *(p--)='-'; \
return p+1; \
}
#define HIBERLITE_STRINGIFY_UNSIGNED_NUM(VALTYPE) \
static std::string toSQLiteValue(const VALTYPE &val){ \
char s[64]; \
char* p=s+63; \
*(p--)=0; \
VALTYPE tmp=val; \
do{ \
*(p--)='0' + (tmp%10); \
tmp/=10; \
}while(tmp); \
return p+1; \
}
HIBERLITE_STRINGIFY_NUM(int)
HIBERLITE_STRINGIFY_UNSIGNED_NUM(unsigned int)
HIBERLITE_STRINGIFY_NUM(char)
//HIBERLITE_STRINGIFY_NUM(unsigned char)
HIBERLITE_STRINGIFY_NUM(long long int)
HIBERLITE_STRINGIFY_UNSIGNED_NUM(unsigned long long int)
#undef HIBERLITE_STRINGIFY_NUM
static std::string toSQLiteValue(const unsigned char &val){
char s[64];
int i=63;
s[i--]=0;
unsigned char tmp=val;
do{
s[i--]='0' + (tmp%10);
tmp/=10;
}while(tmp);
return s+i+1;
}
static std::string toSQLiteValue(const char* val){
size_t n=strlen(val);
char* s=new char[4+2*n];
int x=sprintf(s,"X'");
for(unsigned int i=0;i<n;i++){
unsigned char cc=(unsigned char)val[i];
x+=sprintf(s+x,"%02X", (int)cc);
}
sprintf(s+x,"'");
std::string res(s);
delete[] s;
return res;
}
static std::string toSQLiteValue(const std::string val){
return toSQLiteValue(val.c_str());
}
static std::string toSQLiteValue(const bool val){
return val ? "1" : "0";
}
};
class UpdateBean{
public:
template<class AV>
inline void notifyInitWalk(AV& av);
template<class AV>
inline void notifyDoneWalk(AV& av);
template<class AV, class C>
inline void act(AV& av, db_atom<C> atom){
curRow()->addAssignment(av.getScope().prefix(), atom);
}
template<class AV, class E, class S>
inline void act(AV& av, collection_nvp<E,S> nvp );
protected:
std::stack< shared_res<RowScope> > rowStack;
RowScope* curRow();
void startRow(std::string table, sqlid_t rowid, sqlid_t parent_id, sqlid_t index);
void commitRow(shared_connection con, sqlid_t rowid);
};
class BeanUpdater : AVisitor<UpdateBean>
{
public:
BeanUpdater();
template<class C>
void update(bean_key k, C& bean);
protected:
UpdateBean actor;
private:
};
} //namespace hiberlite
#endif // BEANUPDATER_H