GnuCashew ~ Web Application compatible with GnuCash sql data files.
GCW
Loading...
Searching...
No Matches
Vars.cpp
Go to the documentation of this file.
1#line 2 "src/Dbo/Vars.cpp"
2
3#include <Wt/Json/Parser.h>
4#include <Wt/Json/Object.h>
5#include <Wt/Json/Serializer.h>
6
7#include "../App.h"
8#include "Vars.h"
9
10
11const char * GCW::Dbo::Vars::s_tableName = "gcw_vars";
12
13
14auto
16get( const std::string & _keyValue, const std::string & _cfyValue, bool _add )-> GCW::Dbo::Vars::Item::Ptr
17{
19
20 /*
21 ** Build a 'where' that has both the key and cfy
22 ** fields represented.
23 **
24 */
25 auto where =
26 Wt::WString( "\"keyField\" = '{1}'" )
27 .arg( _keyValue )
28 .toUTF8()
29 ;
30
31 if( _cfyValue != "*" )
32 where +=
33 Wt::WString( " AND \"cfyField\" = '{1}'" )
34 .arg( _cfyValue )
35 .toUTF8()
36 ;
37
38 /*
39 ** find the item (or try to)
40 **
41 */
42 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
43 retVal =
44 GCW::app()-> gnucashew_session().find< GCW::Dbo::Vars::Item >()
45 .where( where )
46 .resultValue()
47 ;
48
49 /*
50 ** if an item could not be found then create one, setting in
51 ** the key and cfy if available.
52 **
53 */
54 if( !retVal && _add )
55 {
56 retVal =
57 GCW::app()-> gnucashew_session().addNew< GCW::Dbo::Vars::Item >()
58 ;
59
60 retVal.modify()-> setKeyField( _keyValue );
61
62 if( _cfyValue != "*" )
63 retVal.modify()-> setCfyField( _cfyValue );
64
65 retVal.flush();
66
67 } // endif( !retVal )
68
69 return retVal;
70
71} // endget( const std::string & _keyValue, const std::string & _cfyValue )-> GCW::Dbo::Vars::Item::Ptr
72
73auto
75getByCfy( const std::string & _cfyValue )-> GCW::Dbo::Vars::Item::Vector
76{
78
79 Wt::Dbo::Transaction t( GCW::app()-> gnucashew_session() );
80 auto items =
81 GCW::app()-> gnucashew_session().find< GCW::Dbo::Vars::Item >()
82 .where( "\"cfyField\" = '" + _cfyValue + "'" )
83 .resultList()
84 ;
85
86 for( auto item : items )
87 retVal.push_back( item );
88
89 return retVal;
90
91} // endget( const std::string & _keyValue, const std::string & _cfyValue )-> GCW::Dbo::Vars::Item::Ptr
92
93auto
95getVarString( const std::string & _field ) const-> std::string
96{
97 std::string retVal;
98
99 if( _field != "" )
100 {
101 Wt::Json::Object jobj;
102 try {
103 Wt::Json::parse( varField(), jobj );
104 }
105 catch( std::exception & _e )
106 {
107 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << _e.what() << std::endl;
108 }
109
110 try {
111 retVal = jobj.get( _field ).orIfNull("");
112 }
113 catch( std::exception & _e )
114 {
115 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << _e.what() << std::endl;
116 }
117
118 }
119
120 return retVal;
121
122} // endgetVarString( const std::string & _field ) const-> std::string
123
124auto
126getVarInt( const std::string & _field ) const-> int
127{
128 int retVal = 0;
129
130 if( _field != "" )
131 {
132 Wt::Json::Object jobj;
133 try {
134 Wt::Json::parse( varField(), jobj );
135 }
136 catch( std::exception & _e )
137 {
138 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << _e.what() << std::endl;
139 }
140
141 try {
142 retVal = jobj.get( _field ).orIfNull( 0 );
143 }
144 catch( std::exception & _e )
145 {
146 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << _e.what() << std::endl;
147 }
148
149 }
150
151 return retVal;
152
153} // endgetVarInt( const std::string & _field ) const-> int
154
155auto
157setVar( const std::string & _field, const char * _value )-> void
158{
159 setVar( _field, std::string( _value ) );
160
161} // endsetVar( const std::string & _field, const char * _value )-> void
162
163auto
165setVar( const std::string & _field, const Wt::WString & _value )-> void
166{
167 setVar( _field, _value.toUTF8() );
168
169} // endsetVar( const std::string & _field, const Wt::WString & _value )-> void
170
171auto
173setVar( const std::string & _field, const std::string & _value )-> void
174{
175 /*
176 ** If there is no field specified, then there's nothing
177 ** to do.
178 **
179 */
180 if( _field == "" )
181 return;
182
183 Wt::Json::Object jobj;
184 try {
185 Wt::Json::parse( varField(), jobj );
186
187 }
188 catch( std::exception & e )
189 {
190 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
191 }
192
193 jobj[_field] = Wt::WString( _value );
194
195 setVarField( Wt::Json::serialize( jobj ) );
196
197} // endsetVar( const std::string & _field, const std::string & _value )-> void
198
199auto
201setVar( const std::string & _field, int _value )-> void
202{
203 /*
204 ** If there is no field specified, then there's nothing
205 ** to do.
206 **
207 */
208 if( _field == "" )
209 return;
210
211 Wt::Json::Object jobj;
212 try {
213 Wt::Json::parse( varField(), jobj );
214 }
215 catch( std::exception & e )
216 {
217 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
218 }
219
220 jobj[_field] = _value;
221
222 setVarField( Wt::Json::serialize( jobj ) );
223
224} // endsetVar( const std::string & _field, int _value )-> void
225
226
227
std::vector< Ptr > Vector
Definition BaseItem.h:41
Variables Item Class.
Definition Vars.h:36
int getVarInt(const std::string &_field) const
Definition Vars.cpp:126
void setVar(const std::string &_field, const char *_value)
Definition Vars.cpp:157
std::string getVarString(const std::string &_field) const
Definition Vars.cpp:95
C * modify() const
void flush() const
const Value & get(const std::string &name) const
const WString & orIfNull(const WString &v) const
std::string toUTF8() const
WString & arg(const std::wstring &value)
void parse(const std::string &input, Value &result, bool validateUTF8=true)
std::string serialize(const Object &obj, int indentation=1)
auto getByCfy(const std::string &_cfyValue) -> GCW::Dbo::Vars::Item::Vector
Definition Vars.cpp:75
auto get(const std::string &_keyValue, const std::string &_cfyValue="*", bool _add=true) -> GCW::Dbo::Vars::Item::Ptr
Definition Vars.cpp:16
const char * s_tableName
Definition Vars.cpp:11
App * app()
Definition App.cpp:75