|
@@ -5,6 +5,14 @@
|
|
|
|
|
|
#include <ulib/orm/orm.h>
|
|
#include <ulib/orm/orm.h>
|
|
#include <ulib/json/value.h>
|
|
#include <ulib/json/value.h>
|
|
|
|
+#include <ulib/utility/uhttp.h>
|
|
|
|
+#include <ulib/orm/orm_driver.h>
|
|
|
|
+#include <ulib/utility/xml_escape.h>
|
|
|
|
+#include <ulib/net/server/client_image.h>
|
|
|
|
+
|
|
|
|
+#ifdef U_STATIC_ORM_DRIVER_PGSQL
|
|
|
|
+# include <ulib/orm/driver/orm_driver_pgsql.h>
|
|
|
|
+#endif
|
|
|
|
|
|
class Fortune {
|
|
class Fortune {
|
|
public:
|
|
public:
|
|
@@ -18,14 +26,9 @@ public:
|
|
uint32_t id;
|
|
uint32_t id;
|
|
UString message;
|
|
UString message;
|
|
|
|
|
|
- Fortune()
|
|
|
|
|
|
+ Fortune(uint32_t _id) : id(_id), message(101U)
|
|
{
|
|
{
|
|
- U_TRACE_CTOR(5, Fortune, "")
|
|
|
|
-
|
|
|
|
- // coverity[uninit_ctor]
|
|
|
|
-# ifdef U_COVERITY_FALSE_POSITIVE
|
|
|
|
- id = 0;
|
|
|
|
-# endif
|
|
|
|
|
|
+ U_TRACE_CTOR(5, Fortune, "%u", _id)
|
|
}
|
|
}
|
|
|
|
|
|
Fortune(uint32_t _id, const UString& _message) : id(_id), message(_message)
|
|
Fortune(uint32_t _id, const UString& _message) : id(_id), message(_message)
|
|
@@ -33,7 +36,7 @@ public:
|
|
U_TRACE_CTOR(5, Fortune, "%u,%V", _id, _message.rep)
|
|
U_TRACE_CTOR(5, Fortune, "%u,%V", _id, _message.rep)
|
|
}
|
|
}
|
|
|
|
|
|
- Fortune(const Fortune& f) : id(f.id), message((void*)U_STRING_TO_PARAM(f.message))
|
|
|
|
|
|
+ Fortune(const Fortune& f) : id(f.id), message(f.message)
|
|
{
|
|
{
|
|
U_TRACE_CTOR(5, Fortune, "%p", &f)
|
|
U_TRACE_CTOR(5, Fortune, "%p", &f)
|
|
|
|
|
|
@@ -105,21 +108,168 @@ public:
|
|
stmt->bindResult(U_ORM_TYPE_HANDLER(message, UString));
|
|
stmt->bindResult(U_ORM_TYPE_HANDLER(message, UString));
|
|
}
|
|
}
|
|
|
|
|
|
-#ifdef DEBUG
|
|
|
|
- const char* dump(bool breset) const
|
|
|
|
|
|
+ static uint32_t uid;
|
|
|
|
+ static UString* pmessage;
|
|
|
|
+ static UVector<Fortune*>* pvfortune;
|
|
|
|
+
|
|
|
|
+ static UOrmSession* psql_fortune;
|
|
|
|
+ static UOrmStatement* pstmt_fortune;
|
|
|
|
+
|
|
|
|
+ static void replace(uint32_t i, uint32_t _id, const char* msg, uint32_t len)
|
|
|
|
+ {
|
|
|
|
+ U_TRACE(0, "Fortune::replace(%u,%u,%.*S,%u)", i, _id, len, msg, len)
|
|
|
|
+
|
|
|
|
+ U_INTERNAL_ASSERT_POINTER(pvfortune)
|
|
|
|
+
|
|
|
|
+ Fortune* elem = pvfortune->at(i);
|
|
|
|
+
|
|
|
|
+ elem->id = _id;
|
|
|
|
+
|
|
|
|
+ UXMLEscape::encode(msg, len, elem->message);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static void replace(uint32_t i) { replace(i, uid, U_STRING_TO_PARAM(*pmessage)); }
|
|
|
|
+ static void replace(uint32_t i, uint32_t _id) { replace(i, _id, U_STRING_TO_PARAM(*pmessage)); }
|
|
|
|
+ static void replace(uint32_t i, const UString& message) { replace(i, i+1, U_STRING_TO_PARAM(message)); }
|
|
|
|
+ static void replace(uint32_t i, uint32_t _id, const UString& message) { replace(i, _id, U_STRING_TO_PARAM(message)); }
|
|
|
|
+
|
|
|
|
+ static void doQuery(vPF handlerQuery)
|
|
|
|
+ {
|
|
|
|
+ U_TRACE(0, "Fortune::doQuery(%p)", handlerQuery)
|
|
|
|
+
|
|
|
|
+ U_INTERNAL_ASSERT_POINTER(pvfortune)
|
|
|
|
+
|
|
|
|
+ char* pwbuffer = UClientImage_Base::wbuffer->data();
|
|
|
|
+
|
|
|
|
+ u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('<','!','d','o','c','t','y','p'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+8, U_MULTICHAR_CONSTANT64('e',' ','h','t','m','l','>','<'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+16, U_MULTICHAR_CONSTANT64('h','t','m','l','>','<','h','e'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+24, U_MULTICHAR_CONSTANT64('a','d','>','<','t','i','t','l'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+32, U_MULTICHAR_CONSTANT64('e','>','F','o','r','t','u','n'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+40, U_MULTICHAR_CONSTANT64('e','s','<','/','t','i','t','l'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+48, U_MULTICHAR_CONSTANT64('e','>','<','/','h','e','a','d'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+56, U_MULTICHAR_CONSTANT64('>','<','b','o','d','y','>','<'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+64, U_MULTICHAR_CONSTANT64('t','a','b','l','e','>','<','t'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+72, U_MULTICHAR_CONSTANT64('r','>','<','t','h','>','i','d'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+80, U_MULTICHAR_CONSTANT64('<','/','t','h','>','<','t','h'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+88, U_MULTICHAR_CONSTANT64('>','m','e','s','s','a','g','e'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+96, U_MULTICHAR_CONSTANT64('<','/','t','h','>','<','/','t'));
|
|
|
|
+ u_put_unalignedp16(pwbuffer+104, U_MULTICHAR_CONSTANT16('r','>'));
|
|
|
|
+
|
|
|
|
+ pwbuffer += U_CONSTANT_SIZE("<!doctype html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>");
|
|
|
|
+
|
|
|
|
+ handlerQuery();
|
|
|
|
+
|
|
|
|
+ Fortune* elem = pvfortune->last();
|
|
|
|
+
|
|
|
|
+ elem->id = 0;
|
|
|
|
+ elem->message.rep->replace(U_CONSTANT_TO_PARAM("Additional fortune added at request time."));
|
|
|
|
+
|
|
|
|
+ pvfortune->sort(Fortune::cmp_obj);
|
|
|
|
+
|
|
|
|
+ for (uint32_t sz, i = 0, n = pvfortune->size(); i < n; ++i)
|
|
|
|
+ {
|
|
|
|
+ elem = pvfortune->at(i);
|
|
|
|
+
|
|
|
|
+ u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('<','t','r','>','<','t','d','>'));
|
|
|
|
+
|
|
|
|
+ pwbuffer = u_num2str32(elem->id, pwbuffer+8);
|
|
|
|
+
|
|
|
|
+ u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('<','/','t','d','>','<','t','d'));
|
|
|
|
+ pwbuffer += 8;
|
|
|
|
+
|
|
|
|
+ *pwbuffer++ = '>';
|
|
|
|
+
|
|
|
|
+ (void) memcpy(pwbuffer, elem->message.data(), sz = elem->message.size());
|
|
|
|
+ pwbuffer += sz;
|
|
|
|
+
|
|
|
|
+ u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('<','/','t','d','>','<','/','t'));
|
|
|
|
+ u_put_unalignedp16(pwbuffer+8, U_MULTICHAR_CONSTANT16('r','>'));
|
|
|
|
+ pwbuffer += 8+2;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ u_put_unalignedp64(pwbuffer, U_MULTICHAR_CONSTANT64('<','/','t','a','b','l','e','>'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+8, U_MULTICHAR_CONSTANT64('<','/','b','o','d','y','>','<'));
|
|
|
|
+ u_put_unalignedp64(pwbuffer+16, U_MULTICHAR_CONSTANT64('/','h','t','m','l','>','\0','\0'));
|
|
|
|
+
|
|
|
|
+ UClientImage_Base::wbuffer->size_adjust(pwbuffer + U_CONSTANT_SIZE("</table></body></html>"));
|
|
|
|
+
|
|
|
|
+ UHTTP::mime_index = U_html;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static void handlerFork()
|
|
{
|
|
{
|
|
- *UObjectIO::os << "id " << id << '\n'
|
|
|
|
- << "message (UString " << (void*)&message << ')';
|
|
|
|
|
|
+ U_TRACE_NO_PARAM(0, "Fortune::handlerFork()")
|
|
|
|
+
|
|
|
|
+ U_NEW_STRING(pmessage, UString(101U));
|
|
|
|
|
|
- if (breset)
|
|
|
|
|
|
+ U_NEW(UVector<Fortune*>, pvfortune, UVector<Fortune*>);
|
|
|
|
+
|
|
|
|
+ Fortune* elem;
|
|
|
|
+
|
|
|
|
+ for (uint32_t i = 0; i < 13; ++i)
|
|
{
|
|
{
|
|
- UObjectIO::output();
|
|
|
|
|
|
+ U_NEW(Fortune, elem, Fortune(i+1));
|
|
|
|
+
|
|
|
|
+ pvfortune->push(elem);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ static void handlerForkSql()
|
|
|
|
+ {
|
|
|
|
+ U_TRACE_NO_PARAM(0, "Fortune::handlerForkSql()")
|
|
|
|
+
|
|
|
|
+ if (psql_fortune == U_NULLPTR)
|
|
|
|
+ {
|
|
|
|
+ U_NEW(UOrmSession, psql_fortune, UOrmSession(U_CONSTANT_TO_PARAM("fortune")));
|
|
|
|
+
|
|
|
|
+ if (psql_fortune->isReady() == false)
|
|
|
|
+ {
|
|
|
|
+ U_WARNING("Fortune::handlerForkSql(): we cound't connect to db");
|
|
|
|
+
|
|
|
|
+ U_DELETE(psql_fortune)
|
|
|
|
+
|
|
|
|
+ psql_fortune = U_NULLPTR;
|
|
|
|
|
|
- return UObjectIO::buffer_output;
|
|
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ U_NEW(UOrmStatement, pstmt_fortune, UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune")));
|
|
|
|
+
|
|
|
|
+ handlerFork();
|
|
|
|
+
|
|
|
|
+ pstmt_fortune->into(uid, *pmessage);
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
- return U_NULLPTR;
|
|
|
|
|
|
+#ifdef DEBUG
|
|
|
|
+ static void handlerEnd()
|
|
|
|
+ {
|
|
|
|
+ U_TRACE_NO_PARAM(0, "Fortune::handlerEnd()")
|
|
|
|
+
|
|
|
|
+ U_INTERNAL_ASSERT_POINTER(pmessage)
|
|
|
|
+ U_INTERNAL_ASSERT_POINTER(pvfortune)
|
|
|
|
+
|
|
|
|
+ U_DELETE(pmessage)
|
|
|
|
+ U_DELETE(pvfortune)
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ static void handlerEndSql()
|
|
|
|
+ {
|
|
|
|
+ U_TRACE_NO_PARAM(0, "Fortune::handlerEndSql()")
|
|
|
|
+
|
|
|
|
+ if (pstmt_fortune)
|
|
|
|
+ {
|
|
|
|
+ handlerEnd();
|
|
|
|
+
|
|
|
|
+ U_DELETE(psql_fortune)
|
|
|
|
+ U_DELETE(pstmt_fortune)
|
|
|
|
+
|
|
|
|
+ pstmt_fortune = U_NULLPTR;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const char* dump(bool breset) const;
|
|
#endif
|
|
#endif
|
|
|
|
|
|
private:
|
|
private:
|