@@ -63,15 +63,23 @@ std::string WsjcppSqlBuilderHelpers::escapingStringValue(const std::string &sVal
6363}
6464
6565
66- WsjcppSqlQuery::WsjcppSqlQuery (WsjcppSqlBuilderType sqlType, const std::string &tableName)
67- : m_sqlType(sqlType), m_tableName(tableName) {
66+ WsjcppSqlQuery::WsjcppSqlQuery (WsjcppSqlQueryType sqlType, WsjcppSqlBuilder *builder , const std::string &tableName)
67+ : m_sqlType(sqlType), m_builder(builder), m_tableName(tableName) {
6868
6969}
7070
71- WsjcppSqlBuilderType WsjcppSqlQuery::sqlType () {
71+ WsjcppSqlQueryType WsjcppSqlQuery::sqlType () {
7272 return m_sqlType;
7373}
7474
75+ WsjcppSqlBuilder &WsjcppSqlQuery::builder () {
76+ return *m_builder;
77+ }
78+
79+ WsjcppSqlBuilder *WsjcppSqlQuery::builderRawPtr () {
80+ return m_builder;
81+ }
82+
7583const std::string &WsjcppSqlQuery::tableName () {
7684 return m_tableName;
7785}
@@ -175,19 +183,17 @@ std::string WsjcppSqlWhereCondition::sql() {
175183}
176184
177185// ---------------------------------------------------------------------
178- // WsjcppSqlBuilderUpdate
186+ // WsjcppSqlSelect
179187
180188WsjcppSqlSelect::WsjcppSqlSelect (const std::string &tableName, WsjcppSqlBuilder *builder)
181- : WsjcppSqlQuery(WsjcppSqlBuilderType ::SELECT, tableName) {
189+ : WsjcppSqlQuery(WsjcppSqlQueryType ::SELECT, builder , tableName) {
182190 // TODO multitype table names with AS
183- m_tableName = tableName;
184- m_builder = builder;
185191}
186192
187193WsjcppSqlSelect &WsjcppSqlSelect::colum (const std::string &col, const std::string &col_as) {
188194 auto it = std::find (m_columns.begin (), m_columns.end (), col);
189195 if (it != m_columns.end ()) {
190- m_builder-> addError (" Column '" + col + " ' already added to select" );
196+ builder (). addError (" Column '" + col + " ' already added to select" );
191197 } else {
192198 m_columns.push_back (col);
193199 m_columns_as[col] = col_as;
@@ -197,16 +203,12 @@ WsjcppSqlSelect &WsjcppSqlSelect::colum(const std::string &col, const std::strin
197203
198204WsjcppSqlWhere<WsjcppSqlSelect> &WsjcppSqlSelect::where () {
199205 if (!m_where) {
200- m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(nullptr , m_builder , this );
206+ m_where = std::make_shared<WsjcppSqlWhere<WsjcppSqlSelect>>(nullptr , builderRawPtr () , this );
201207 }
202208
203209 return *(m_where.get ());
204210}
205211
206- WsjcppSqlBuilder &WsjcppSqlSelect::compile () {
207- return *m_builder;
208- }
209-
210212std::string WsjcppSqlSelect::sql () {
211213 std::string ret = " SELECT " ;
212214 // TODO TOP OR LIMIT for different databases
@@ -226,45 +228,59 @@ std::string WsjcppSqlSelect::sql() {
226228 first = false ;
227229 }
228230 ret += " FROM " ;
229- ret += m_tableName ;
231+ ret += tableName () ;
230232 }
231233
232234 if (m_where) {
233235 ret += " WHERE " + m_where->sql ();
234236 }
235237
236- // TODO where
237238 // TODO group by
238239 // TODO order by
239240 return ret;
240241}
241242
243+ // ---------------------------------------------------------------------
244+ // WsjcppSqlInsert
245+
246+ WsjcppSqlInsert::WsjcppSqlInsert (const std::string &tableName, WsjcppSqlBuilder *builder)
247+ : WsjcppSqlQuery(WsjcppSqlQueryType::INSERT, builder, tableName) {
248+
249+ }
250+
251+
252+ std::string WsjcppSqlInsert::sql () {
253+ std::string ret = " INSERT INTO " + tableName ();
254+
255+ return ret;
256+ };
257+
242258// ---------------------------------------------------------------------
243259// WsjcppSqlBuilder
244260
245261WsjcppSqlSelect &WsjcppSqlBuilder::selectFrom (const std::string &tableName) {
246262 m_tableName = tableName;
247- m_nSqlType = WsjcppSqlBuilderType ::SELECT;
263+ m_nSqlType = WsjcppSqlQueryType ::SELECT;
248264 m_queries.push_back (std::make_shared<WsjcppSqlSelect>(m_tableName, this ));
249265 // TODO check must be select last one;
250266 return *(WsjcppSqlSelect *)(m_queries[m_queries.size () -1 ].get ());
251267}
252268
253269WsjcppSqlBuilder &WsjcppSqlBuilder::insertInto (const std::string &tableName) {
254270 m_tableName = tableName;
255- m_nSqlType = WsjcppSqlBuilderType ::INSERT;
271+ m_nSqlType = WsjcppSqlQueryType ::INSERT;
256272 return *this ;
257273}
258274
259275// WsjcppSqlBuilder &WsjcppSqlBuilder::makeUpdate(const std::string &tableName) {
260276// m_tableName = tableName;
261- // m_nSqlType = WsjcppSqlBuilderType ::UPDATE;
277+ // m_nSqlType = WsjcppSqlQueryType ::UPDATE;
262278// return *this;
263279// }
264280
265281// WsjcppSqlBuilder &WsjcppSqlBuilder::makeDelete(const std::string &tableName) {
266282// m_tableName = tableName;
267- // m_nSqlType = WsjcppSqlBuilderType ::DELETE;
283+ // m_nSqlType = WsjcppSqlQueryType ::DELETE;
268284// return *this;
269285// }
270286
0 commit comments