int exec() {

      vf array;

      // query fortunes
      mysql_query(db.connection(), "SELECT id, message FROM Fortune;");
      MYSQL_RES *sqlres = mysql_store_result(db.connection());
      if (!sqlres) throw runtime_error(mysql_error(db.connection()));
      MYSQL_ROW row;
      while( (row=mysql_fetch_row(sqlres)) ){
        array.push_back(Fortune(atoi(row[0]),row[1]));
      }
      mysql_free_result(sqlres);

      // add on the fly
      array.push_back(Fortune(0,"Additional fortune added at request time."));

      // sort
      sort(array.begin(),array.end());

      // output html
      res << "<!DOCTYPE html>"
          "<html>"
          "<head><title>Fortunes</title></head>"
          "<body>"
          "<table>"
          "<tr><th>id</th><th>message</th></tr>";
      for(unsigned z=0;z<array.size();++z) {
        Fortune &az = array[z];
        res << "<tr>"
               "<td>" << az.id << "</td>"
               "<td>" << xml::escape(az.message) << "</td>"
               "</tr>";
      }
      res << "</table>"
             "</body>"
             "</html>";

      return res.end();
    }
Example #2
0
    int exec() {

      id = rand()%10000;

      // reset statement for reuse
      if(mysql_stmt_reset(stmt)) throw runtime_error(mysql_stmt_error(stmt));

      // create and use input parameter
      memset(param, 0, sizeof(param));
      param[0].buffer_type = MYSQL_TYPE_LONG;
      param[0].buffer = (char *)&id;
      param[0].buffer_length = sizeof(id);
      param[0].is_null = 0;
      param[0].length = NULL;
      if(mysql_stmt_bind_param(stmt,param)) throw runtime_error(mysql_stmt_error(stmt));

      // create and fill result buffer
      memset(results, 0, sizeof(results));
      results[0].buffer_type= MYSQL_TYPE_LONG;
      results[0].buffer = &randomNumber;
      results[0].buffer_length = sizeof(randomNumber);
      results[0].is_null = 0;
      results[0].length = &len1;
      if(mysql_stmt_bind_result(stmt, results)) throw runtime_error(mysql_stmt_error(stmt));

      // execute statement
      if(mysql_stmt_execute(stmt)) throw runtime_error(mysql_stmt_error(stmt));
      if(mysql_stmt_fetch(stmt)) randomNumber = 0;

      // output json
      JsonObj obj;
      obj.add("id",id);
      obj.add("randomNumber",randomNumber);
      res << obj.to_s();

      return res.end();
    }
    int exec() {

      int n = res.get_i("queries=");
      if(n<1) n = 1;
      if(n>500) n = 500;

      vw array;

      while(n--) {

        id = rand()%10000;
        newNumber = rand()%10000+1;

        // reset statement for reuse
        if(mysql_stmt_reset(stmt)) throw runtime_error(mysql_stmt_error(stmt));
        if(mysql_stmt_reset(ustmt)) throw runtime_error(mysql_stmt_error(ustmt));

        // create and use input parameter
        memset(param, 0, sizeof(param));
        param[0].buffer_type = MYSQL_TYPE_LONG;
        param[0].buffer = (char *)&id;
        param[0].buffer_length = sizeof(id);
        param[0].is_null = 0;
        param[0].length = NULL;
        if(mysql_stmt_bind_param(stmt,param)) throw runtime_error(mysql_stmt_error(stmt));

        // create and fill result buffer
        memset(results, 0, sizeof(results));
        results[0].buffer_type= MYSQL_TYPE_LONG;
        results[0].buffer = &randomNumber;
        results[0].buffer_length = sizeof(randomNumber);
        results[0].is_null = 0;
        results[0].length = &len1;
        if(mysql_stmt_bind_result(stmt, results)) throw runtime_error(mysql_stmt_error(stmt));

        // create and use update parameter
        memset(param_update, 0, sizeof(param_update));
        param_update[0].buffer_type = MYSQL_TYPE_LONG;
        param_update[0].buffer = (char *)&newNumber;
        param_update[0].buffer_length = sizeof(newNumber);
        param_update[0].is_null = 0;
        param_update[0].length = 0;
        param_update[1].buffer_type = MYSQL_TYPE_LONG;
        param_update[1].buffer = (char *)&id;
        param_update[1].buffer_length = sizeof(id);
        param_update[1].is_null = 0;
        param_update[1].length = 0;

        // execute statement
        if(mysql_stmt_execute(stmt)) throw runtime_error(mysql_stmt_error(stmt));
        if(mysql_stmt_bind_param(ustmt,param_update)) throw runtime_error(mysql_stmt_error(ustmt));
        if(mysql_stmt_fetch(stmt)) randomNumber = 0;

        // add to array (required by specification)
        array.push_back(World(id,newNumber));
      }

      // output json
      res << '[';
      for(unsigned z=0;z<array.size();++z) {
        if(z>0) res << ',';
        World &az = array[z];
        JsonObj obj;
        obj.add("id",az.id);
        obj.add("randomNumber",az.randomNumber);
        res << obj.to_s();
      }
      res << ']';

      return res.end();
    }
Example #4
0
 int exec() {
   JsonObj obj;
   obj.add("message","Hello, World!");
   res << obj.to_s();
   return res.end();
 }