fortune_old 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <%!-lmysqlclient%><%#
  2. #include <mysql/mysql.h>
  3. #include <json/json.h>
  4. #include "connectioninfo.H"
  5. using namespace CP;
  6. using namespace cppsp;
  7. MYSQL* db=NULL;
  8. bool comp_string(const String& x, const String& y) {
  9. if (x.len == 0 || y.len == 0) return x.len < y.len;
  10. int complen = x.len < y.len ? x.len : y.len;
  11. int tmp = memcmp(x.d, y.d, complen);
  12. if (tmp == 0) return x.len < y.len;
  13. else return tmp < 0;
  14. }
  15. %><%
  16. if(db==NULL) {
  17. db=doConnect(NULL);
  18. mysql_set_character_set(db, "utf8");
  19. mysql_options(db, MYSQL_SET_CHARSET_NAME, "utf8");
  20. }
  21. mysql_query(db, "SELECT id, message FROM Fortune;");
  22. MYSQL_RES *sqlres = mysql_store_result(db);
  23. if (sqlres==NULL) throw runtime_error(mysql_error(db));
  24. MYSQL_ROW row;
  25. struct fortune
  26. {
  27. String message;
  28. int id;
  29. bool operator<(const fortune& other) const {
  30. return comp_string(message,other.message);
  31. }
  32. };
  33. vector<fortune> fortunes;
  34. while( (row=mysql_fetch_row(sqlres)) ){
  35. fortunes.push_back({sp->addString(row[1]),atoi(row[0])});
  36. }
  37. mysql_free_result(sqlres);
  38. fortunes.push_back({"Additional fortune added at request time.",0});
  39. sort(fortunes.begin(),fortunes.end());
  40. response->headers["Server"]="cppsp/0.2";
  41. %>
  42. <!DOCTYPE html>
  43. <html>
  44. <head><title>Fortunes</title></head>
  45. <body>
  46. <table>
  47. <tr><th>id</th><th>message</th></tr>
  48. <%
  49. for(int i=0;i<fortunes.size();i++) {
  50. %>
  51. <tr><td><%=fortunes[i].id%></td><td><%htmlEscape(fortunes[i].message,output);%></td></tr><%
  52. }
  53. %>
  54. </table>
  55. </body>
  56. </html>