<!--#
Test type 5: Database updates
TechEmpower Web Framework Benchmarks
-->
<!--#declaration
#include "world.h"

static bool             bpgsql;
static World*           pworld_update;
static UOrmSession*     psql_update;
static UOrmStatement*   pstmt;
static UOrmStatement*   pstmt1;
static UVector<World*>* pvworld_update;

static void usp_fork_update()
{
   U_TRACE(5, "::usp_fork_update()")

   U_NEW(UOrmSession, psql_update, UOrmSession(U_CONSTANT_TO_PARAM("hello_world")));

   if (psql_update->isReady() == false)
      {
      U_WARNING("usp_fork_update(): we cound't connect to db");

      delete psql_update;
             psql_update = U_NULLPTR;

      return;
      }

   U_NEW(World,           pworld_update,  World);
   U_NEW(UVector<World*>, pvworld_update, UVector<World*>(500));

   U_NEW(UOrmStatement, pstmt, UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));

   pstmt->use( pworld_update->id);
   pstmt->into(pworld_update->randomNumber);

   if ((bpgsql = UOrmDriver::isPGSQL()) == false)
      {
      U_NEW(UOrmStatement, pstmt1, UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("UPDATE World SET randomNumber = ? WHERE id = ?")));

      pstmt1->use(pworld_update->randomNumber, pworld_update->id);
      }
// else *psql_update << "SET synchronous_commit TO OFF";
}

#ifdef DEBUG
static void usp_end_update()
{
   U_TRACE(5, "::usp_end_update()")

   if (pstmt)
      {
      delete pstmt;
      delete pworld_update;
      delete pvworld_update;

      if (bpgsql == false) delete pstmt1;

      delete psql_update;
      }
}
#endif
-->
<!--#header
Content-Type: application/json
-->
<!--#code
char* ptr;
World* pworld;
char query[8192];
int i = 0, num_queries = UHTTP::getFormFirstNumericValue(1, 500);

(void) UClientImage_Base::wbuffer->reserve(36U * num_queries);

if (bpgsql)
   {
   (void) memcpy(query,          "UPDATE World SET randomNumber = v.randomNumber FROM (VALUES",
                 U_CONSTANT_SIZE("UPDATE World SET randomNumber = v.randomNumber FROM (VALUES"));
   ptr = query + U_CONSTANT_SIZE("UPDATE World SET randomNumber = v.randomNumber FROM (VALUES");
   }

while (true)
   {
   pworld_update->id = u_get_num_random(10000-1);

   pstmt->execute();

   pworld_update->randomNumber = u_get_num_random(10000-1);

   if (bpgsql == false) pstmt1->execute();

   U_NEW(World, pworld, World(*pworld_update));

   pvworld_update->push_back(pworld);

   if (++i == num_queries) break;
   }

#ifdef AS_cpoll_cppsp_DO
char* p = UClientImage_Base::wbuffer->pend();

*p++ = '[';
#endif

if (bpgsql) pvworld_update->sort(World::cmp_obj);

i = 0;

while (true)
   {
   if (bpgsql)
      {
      pworld = pvworld_update->at(i);

      *ptr++ = '(';

      ptr = u_num2str32(pworld->id, ptr);

      *ptr++ = ',';

      ptr = u_num2str32(pworld->randomNumber, ptr);

      u_put_unalignedp16(ptr, U_MULTICHAR_CONSTANT16(')',','));
                         ptr += 2;
      }

#ifdef AS_cpoll_cppsp_DO
   u_put_unalignedp32(p,   U_MULTICHAR_CONSTANT32('{','"','i','d'));
   u_put_unalignedp16(p+4, U_MULTICHAR_CONSTANT16('"',':'));

   p = u_num2str32(pworld->id, p+6);

   u_put_unalignedp64(p,   U_MULTICHAR_CONSTANT64(',','"','r','a','n','d','o','m'));
   u_put_unalignedp64(p+8, U_MULTICHAR_CONSTANT64('N','u','m','b','e','r','"',':'));

   p = u_num2str32(pworld->randomNumber, p+16);

   *p++ = '}';
#endif

   if (++i == num_queries) break;

#ifdef AS_cpoll_cppsp_DO
   *p++ = ',';
#endif
   }

#ifdef AS_cpoll_cppsp_DO
*p++ = ']';

UClientImage_Base::wbuffer->size_adjust(p);
#else
USP_OBJ_JSON_stringify(*pvworld_update);
#endif

pvworld_update->clear();

if (bpgsql)
   {
   (void) memcpy(ptr-1,                 ") AS v (id,randomNumber) WHERE World.id = v.id;",
                        U_CONSTANT_SIZE(") AS v (id,randomNumber) WHERE World.id = v.id;")+1);

   if (psql_update->query(query, ptr + U_CONSTANT_SIZE(") AS v (id,randomNumber) WHERE World.id = v.id;") - query) == false) UHTTP::setInternalError(); 
   }
-->
