/home/martin/workspace/OpenStreetNav/src/sqllib/strings/KeyValFilterTranslator.cpp
Go to the documentation of this file.
00001 /*
00002  * KeyValFilterTranslator.cpp
00003  *
00004  *  Created on: Dec 5, 2011
00005  *      Author: martin
00006  */
00007 
00008 #include "KeyValFilterTranslator.h"
00009 
00010 namespace sqllib
00011 {
00012 
00013 KeyValFilterTranslator::KeyValFilterTranslator(std::string const& cols, std::string const& tables, std::string const& where, std::string const& kvtable, std::vector<std::string> const& types, std::string const& order_by):
00014     cols(cols),
00015     tables(tables),
00016     where_cond(where),
00017     kvtable(kvtable),
00018     types(types),
00019     order_by(order_by)
00020 {
00021 }
00022 
00023 boost::property_tree::ptree KeyValFilterTranslator::translate(const boost::property_tree::ptree& input)
00024 {
00025     boost::property_tree::ptree ret;
00026     ret.put("type", "union");
00027     boost::property_tree::ptree retchldrn;
00028     for (auto it = input.get_child("entries").begin(); it != input.get_child("entries").end(); ++it)
00029     {
00030         boost::property_tree::ptree const& entry = it->second;
00031         boost::property_tree::ptree chld;
00032         boost::property_tree::ptree add;
00033         if (entry.find("add") != entry.not_found())
00034             add = entry.get_child("add");
00035         chld.put("type", "intersect");
00036         boost::property_tree::ptree chldrn;
00037         for (auto it2 = entry.get_child("elements").begin(); it2 != entry.get_child("elements").end(); ++it2)
00038         {
00039             boost::property_tree::ptree el;
00040             el.put("type", "simple");
00041             std::ostringstream s;
00042             s << "SELECT " << cols;
00043             unsigned int i = 0;
00044             for (auto it3 = add.begin(); it3 != add.end(); ++it3)
00045             {
00046                 s << ", " << it3->second.data();
00047                 if (i < types.size())
00048                     s << "::" << types[i];
00049                 i++;
00050                 s << " AS " + it3->first;
00051             }
00052             std::string k = it2->second.get<std::string>("key", "");
00053             if (k == "*")
00054                 k = "";
00055             std::string v = it2->second.get<std::string>("value", "");
00056             if (v == "*")
00057                 v = "";
00058             if (v == "" && k == "" && where_cond == "")
00059                 s << " FROM " << tables;
00060             else
00061             {
00062                 s << " FROM " << tables << " WHERE " << where_cond;
00063                 if (k != "")
00064                 {
00065                     if (where_cond != "")
00066                         s << " AND ";
00067                     s << kvtable << ".Key=" << "'" << k << "'";
00068                 }
00069                 if (v != "")
00070                 {
00071                     if (where_cond != "" || k != "")
00072                         s << " AND ";
00073                     s << kvtable << ".Value=" << "'" << v << "'";
00074                 }
00075             }
00076             el.put("query", s.str());
00077             chldrn.add_child("child", el);
00078         }
00079         chld.put_child("children", chldrn);
00080         retchldrn.add_child("child", chld);
00081     }
00082     ret.put_child("children", retchldrn);
00083     ret.put("ending", order_by);
00084     return ret;
00085 }
00086 
00087 } /* namespace sqllib */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines