示例#1
0
int main(int argc, char** argv)
{
	Poco::JSON::Object::Ptr obj;

	std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/";
	Poco::Path filePath(dir, "input");

	std::ostringstream ostr;

	if ( filePath.isFile() )
	{
		Poco::File inputFile(filePath);
		if ( inputFile.exists() )
		{
			Poco::FileInputStream fis(filePath.toString());
			Poco::StreamCopier::copyStream(fis, ostr);
		}
		else
		{
			std::cout << filePath.toString() << " doesn't exist!" << std::endl;
			return 1; 
		}
	}

	std::cout << std::setw(25) << "POCO JSON";

	std::string jsonStr = ostr.str();
	
	try
	{
		Poco::JSON::DefaultHandler handler;
		Poco::JSON::Parser parser;
		parser.setHandler(&handler);
		Poco::Timestamp time1;
		parser.parse(jsonStr);
		Poco::DynamicAny result = handler.result();
		Poco::Timestamp time2;
		printTimeDiff(time1, time2);

		if ( result.type() == typeid(Poco::JSON::Object::Ptr) )
		{
			obj = result.extract<Poco::JSON::Object::Ptr>();
		}

		//Serialize to string
		std::ostringstream out;
		Poco::Timestamp time3;
		obj->stringify(out);
		Poco::Timestamp time4;
		printTimeDiff(time3, time4);
		std::cout << std::endl;
	}
	catch(Poco::JSON::JSONException jsone)
	{
		std::cout << jsone.message() << std::endl;
	}

	return 0;
}
示例#2
0
Array::Array(const Poco::DynamicAny& any)
{
  if ( any.type() == typeid(Array) )
  {
    *this = any.extract<Array>();
  }
}
示例#3
0
	void ResultSetBinding::TransformValue(size_t index, SharedValue result)
	{
		MetaColumn::ColumnDataType type = rs->columnType(index);
		Poco::DynamicAny value = rs->value(index);
		
		if (value.isEmpty())
		{
			result->SetNull();
		}
		else if (type == MetaColumn::FDT_STRING)
		{
			std::string str;
			value.convert(str);
			result->SetString(str);
		}
		else if (type == MetaColumn::FDT_BOOL)
		{
			bool v = false;
			value.convert(v);
			result->SetBool(v);
		}
		else if (type == MetaColumn::FDT_FLOAT || type == MetaColumn::FDT_DOUBLE)
		{
			float f = 0;
			value.convert(f);
			result->SetDouble(f);
		}
		else if (type == MetaColumn::FDT_BLOB || type == MetaColumn::FDT_UNKNOWN)
		{
			std::string str;
			value.convert(str);
			result->SetString(str);
		}
		else
		{
			// the rest of these are ints:
			// FDT_INT8,
			// FDT_UINT8,
			// FDT_INT16,
			// FDT_UINT16,
			// FDT_INT32,
			// FDT_UINT32,
			// FDT_INT64,
			// FDT_UINT64,
			int i;
			value.convert(i);
			result->SetInt(i);
		}
	}
示例#4
0
void Database::Execute(const ValueList& args, KValueRef result)
{
    args.VerifyException("execute", "s");

    if (!session)
        throw ValueException::FromString("Tried to call execute, but database was closed.");

    std::string sql(args.GetString(0));
    GetLogger()->Debug("Execute called with %s", sql.c_str());
    
    Statement select(*this->session);
    
    try
    {
        ValueBinding binding;
        
        select << sql;
        
        if (args.size()>1)
        {
            
            for (size_t c=1;c<args.size();c++)
            {
                KValueRef anarg = args.at(c);
                if (anarg->IsList())
                {
                    KListRef list = anarg->ToList();
                    for (size_t a=0;a<list->Size();a++)
                    {
                        KValueRef arg = list->At(a);
                        binding.convert(select,arg);
                    }
                }
                else
                {
                    binding.convert(select,anarg);
                }
            }
        }
        Poco::UInt32 count = select.execute();

        GetLogger()->Debug("sql returned: %d rows for result",count);

        this->SetInt("rowsAffected",count);

        // get the row insert id
        Statement ss(*this->session);
        ss << "select last_insert_rowid()", now;
        RecordSet rr(ss);
        Poco::DynamicAny value = rr.value(0);
        int i;
        value.convert(i);
        this->SetInt("lastInsertRowId",i);

        
        if (count > 0)
        {
            RecordSet rs(select);
            KObjectRef r = new ResultSet(rs);
            result->SetObject(r);
        }
        else
        {
            KObjectRef r = new ResultSet();
            result->SetObject(r);
        }
    }
    catch (Poco::Data::DataException &e)
    {
        GetLogger()->Error("Exception executing: %s, Error was: %s", sql.c_str(),
            e.what());
        throw ValueException::FromString(e.what());
    }
}
示例#5
0
int main(int argc, char **argv)
{
    Poco::Stopwatch sw;

    std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/";
    Poco::Path  filePath(dir, "input.big.json");

    std::ostringstream ostr;

    if (filePath.isFile()) {
        Poco::File inputFile(filePath);

        if (inputFile.exists()) {
            sw.start();
            Poco::FileInputStream fis(filePath.toString());
            Poco::StreamCopier::copyStream(fis, ostr);
            sw.stop();
        } else {
            std::cout << filePath.toString() << " doesn't exist!" << std::endl;
            return 1;
        }
    }

    std::cout << "JSON Benchmark" << std::endl;
    std::cout << "==============" << std::endl;

    std::string jsonStr = ostr.str();
    std::cout << "Total of " << jsonStr.size() << " bytes," << std::endl << "loaded in " << sw.elapsed() << " [us]," << std::endl;

    std::cout << std::endl << "POCO JSON barebone parse" << std::endl;
    Poco::JSON::Parser sparser(0);
    sw.restart();
    sparser.parse(jsonStr);
    sw.stop();
    std::cout << "---------------------------------" << std::endl;
    std::cout << "[std::string] parsed in " << sw.elapsed() << " [us]" << std::endl;
    std::cout << "---------------------------------" << std::endl;

    Poco::JSON::Parser  iparser(0);
    std::istringstream  istr(jsonStr);
    sw.restart();
    iparser.parse(istr);
    sw.stop();
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "[std::istringstream] parsed in " << sw.elapsed() << " [us]" << std::endl;
    std::cout << "----------------------------------------" << std::endl;

    std::cout << std::endl << "POCO JSON Handle/Stringify" << std::endl;
    try
    {
        Poco::JSON::Parser sparser;
        sw.restart();
        sparser.parse(jsonStr);
        Poco::DynamicAny result = sparser.result();
        sw.stop();
        std::cout << "-----------------------------------------" << std::endl;
        std::cout << "[std::string] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "-----------------------------------------" << std::endl;

        Poco::JSON::Parser  isparser;
        std::istringstream  istr(jsonStr);
        sw.restart();
        isparser.parse(istr);
        result = isparser.result();
        sw.stop();
        std::cout << "------------------------------------------------" << std::endl;
        std::cout << "[std::istringstream] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "------------------------------------------------" << std::endl;

        // Serialize to string
        Poco::JSON::Object::Ptr obj;

        if (result.type() == typeid(Poco::JSON::Object::Ptr)) {
            obj = result.extract <Poco::JSON::Object::Ptr> ();
        }

        std::ostringstream out;
        sw.restart();
        obj->stringify(out);
        sw.stop();
        std::cout << "-----------------------------------" << std::endl;
        std::cout << "stringified in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "-----------------------------------" << std::endl;
        std::cout << std::endl;
    }
    catch(Poco::JSON::JSONException jsone)
    {
        std::cout << jsone.message() << std::endl;
    }

    return 0;
}