Нет простого способа AFAIK. Если цель состоит в том, чтобы поддерживать высокий коэффициент кэша операторов (т. Е. Не создавать инструкцию для каждого количества параметров), вы можете сделать следующее:
NULL никогда не соответствует чему-либо, поэтому он оптимизируется разработчиком плана SQL.
Логику легко автоматизировать при передаче списка в функцию DAO:
while( i < param.size() ) {
ps.setString(i+1,param.get(i));
i++;
}
while( i < MAX_PARAMS ) {
ps.setNull(i+1,Types.VARCHAR);
i++;
}
Используя ThorsSerizlier и тот же ввод, что и.
Предположим, что вход input.json
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include "ThorSerialize/Traits.h"
#include <iostream>
#include <fstream>
struct Data
{
std::vector<std::map<std::string, int>> values; // The values input
};
ThorsAnvil_MakeTrait(Data, values); // Describes what can be serialized
// There is a definition for all standard
// types in SerUtil.h
int main()
{
std::ifstream dataStream("input.json");
Data data;
// Automatically reads data into C++ structure.
// Directly from a stream.
dataStream >> ThorsAnvil::Serialize::jsonImport(data);
// Converts a C++ structure into JSON for output to a stream.
std::cout << ThorsAnvil::Serialize::jsonExport(data);
}
Предположим, что вход input.json
Вот упрощенный метод с использованием Boost Spirit Qi:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <fstream>
#include <map>
namespace {
using Element = std::map<std::string, int>;
struct Array : std::vector<Element> { };
std::istream& operator>>(std::istream& is, Array& into) {
using namespace boost::spirit::qi;
using it = boost::spirit::istream_iterator;
rule<it, std::string()> s;
rule<it, Element(), space_type> r, e;
s = '"' >> ~char_('"') >> '"';
r = (s >> ':' >> int_) % ',';
e = '{' >> r >> '}';
return is >> phrase_match('{'
>> lit("\"values\"") >> ':' >> '[' >> (e % ',') >> ']'
>> '}', space, into);
}
}
int main() {
std::ifstream ifs("input.json");
ifs.unsetf(std::ios::skipws);
Array array;
if (ifs >> array) {
std::cout << "Parsed " << array.size() << " elements:\n";
for (auto& e : array) {
std::cout << "\n--------------------\n{ ";
for (auto& kv : e)
std::cout << "\"" << kv.first << "\": " << kv.second << ", ";
std::cout << " }\n";
}
} else {
std::cout << "Parsing failed\n";
}
}
Печатает
std::istream& {anonymous}::operator>>(std::istream&, {anonymous}::Array&)
Parsed 13 elements:
--------------------
{ "A": 1, "B": 10, }
--------------------
{ "C": 3, "D": 12, }
--------------------
{ "E": 5, "F": 14, }
--------------------
{ "G": 7, "H": 16, }
--------------------
{ "I": 9, "J": 18, }
--------------------
{ "K": 11, "L": 20, }
--------------------
{ "M": 13, "N": 22, }
--------------------
{ "O": 15, "P": 24, }
--------------------
{ "Q": 17, "R": 26, }
--------------------
{ "S": 19, "T": 28, }
--------------------
{ "U": 21, "V": 30, }
--------------------
{ "W": 23, "X": 32, }
--------------------
{ "Y": 25, "Z": 34, }
То же дело:
namespace {
using Element = std::map<std::string, int>;
struct Array : std::vector<Element> { };
namespace parser {
using namespace boost::spirit::x3;
rule<struct rule_key_t, std::string> s;
rule<struct rule_element_t, Element> r;
rule<struct rule_braced_t, Element> e;
auto s_def = '"' >> ~char_('"') >> '"';
auto r_def = (s >> ':' >> int_) % ',';
auto e_def = '{' >> r >> '}';
BOOST_SPIRIT_DEFINE(s,r,e)
}
std::istream& operator>>(std::istream& is, Array& into) {
using namespace parser;
boost::spirit::istream_iterator f(is), l;
if (!phrase_parse(f, l, '{'
>> lit("\"values\"") >> ':' >> '[' >> (e % ',') >> ']'
>> '}', space, into))
{
is.setstate(is.rdstate() | std::ios::failbit);
}
return is;
}
}
Тот же выход с тем же main()
Это несколько отличается , Я решил не реализовывать operator>>
, потому что Boost Property на самом деле этого не дает.
#include <boost/property_tree/json_parser.hpp>
#include <fstream>
#include <iostream>
#include <map>
namespace {
using Element = std::map<std::string, int>;
struct Array : std::vector<Element> { };
Array read(std::string fname) {
std::ifstream ifs(fname);
Array into;
using namespace boost::property_tree;
ptree pt;
read_json(ifs, pt);
for (auto& entry : pt.get_child("values")) {
Element e;
for (auto& property : entry.second)
e[property.first] = property.second.get_value(0);
into.push_back(std::move(e));
}
return into;
}
}
int main() {
try {
auto array = read("input.json");
std::cout << "Parsed " << array.size() << " elements:\n";
for (auto& e : array) {
std::cout << "--------------------\n{ ";
for (auto& kv : e)
std::cout << "\"" << kv.first << "\": " << kv.second << ", ";
std::cout << " }\n";
}
} catch (...) {
std::cout << "Parsing failed\n";
}
}
Конечно, вывод снова, тот же, что и раньше.