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 retVal = jobj.get( _field ).orIfNull("");
111
112 }
113
114 return retVal;
115
116} // endgetVarString( const std::string & _field ) const-> std::string
117
118auto
120getVarInt( const std::string & _field ) const-> int
121{
122 int retVal = 0;
123
124 if( _field != "" )
125 {
126 Wt::Json::Object jobj;
127 try {
128 Wt::Json::parse( varField(), jobj );
129 }
130 catch( std::exception & e )
131 {
132 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
133 }
134
135 retVal = jobj.get( _field ).orIfNull( 0 );
136
137 }
138
139 return retVal;
140
141} // endgetVarInt( const std::string & _field ) const-> int
142
143auto
145setVar( const std::string & _field, const char * _value )-> void
146{
147 setVar( _field, std::string( _value ) );
148
149} // endsetVar( const std::string & _field, const char * _value )-> void
150
151auto
153setVar( const std::string & _field, const Wt::WString & _value )-> void
154{
155 setVar( _field, _value.toUTF8() );
156
157} // endsetVar( const std::string & _field, const Wt::WString & _value )-> void
158
159auto
161setVar( const std::string & _field, const std::string & _value )-> void
162{
163 /*
164 ** If there is no field specified, then there's nothing
165 ** to do.
166 **
167 */
168 if( _field == "" )
169 return;
170
171 Wt::Json::Object jobj;
172 try {
173 Wt::Json::parse( varField(), jobj );
174
175 }
176 catch( std::exception & e )
177 {
178 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
179 }
180
181 jobj[_field] = Wt::WString( _value );
182
183 setVarField( Wt::Json::serialize( jobj ) );
184
185} // endsetVar( const std::string & _field, const std::string & _value )-> void
186
187auto
189setVar( const std::string & _field, int _value )-> void
190{
191 /*
192 ** If there is no field specified, then there's nothing
193 ** to do.
194 **
195 */
196 if( _field == "" )
197 return;
198
199 Wt::Json::Object jobj;
200 try {
201 Wt::Json::parse( varField(), jobj );
202 }
203 catch( std::exception & e )
204 {
205 std::cout << __FILE__ << ":" << __LINE__ << " id:" << this-> id() << " " << e.what() << std::endl;
206 }
207
208 jobj[_field] = _value;
209
210 setVarField( Wt::Json::serialize( jobj ) );
211
212} // endsetVar( const std::string & _field, int _value )-> void
213
214
215
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:120
void setVar(const std::string &_field, const char *_value)
Definition Vars.cpp:145
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