#include "cgilet.h"

using namespace cgid;

extern "C" {
extern void * create_cgi();
}

class PersonTest: public CGILet {
public:
   void process_request(tcp_stream & out, FormData & form_data, Connection * conn) {
      if(form_data["embed"] != "true")
         out << "Content-type: text/html\r\n\r\n";

      if(conn) {
         Statement * p_statement = conn->get_statement();

         if(p_statement) {
            ResultSet * p_result = p_statement->execute("SELECT ID,NAME,AGE FROM PERSON");

            if(p_result) {
               out << "ID\tNAME\tAGE<BR>\n";

               while(p_result->has_more()) {
                  out << p_result->get_string(0) << "\t"
                      << p_result->get_string(1) << "\t"
                      << p_result->get_string(2) << "<BR>\n";

                  cout << p_result->get_string(0) << "\t"
                       << p_result->get_string(1) << "\t"
                       << p_result->get_string(2) << "\n";

                  p_result->next();
               }

               delete p_result;
            }

            delete p_statement;
         }
      }

      out << "<BR><BR>\n";
      out << "GET:<BR>\n";
      out << "<FORM METHOD=GET ACTION=/gateway.php>\n"
          << "<INPUT TYPE=HIDDEN NAME=cgilet VALUE=hello>\n"
          << "Name: <INPUT TYPE=TEXT NAME=name><BR><INPUT TYPE=SUBMIT><BR>\n"
          << "</FORM>\n\n";

      out << "POST:<BR>\n";
      out << "<FORM METHOD=POST ACTION=/gateway.php>\n"
          << "<INPUT TYPE=HIDDEN NAME=cgilet VALUE=hello>\n"
          << "Name: <INPUT TYPE=TEXT NAME=name><BR><INPUT TYPE=SUBMIT><BR>\n"
          << "</FORM>\n\n";

      out << "PERSON:<BR>\n";
      out << "<FORM METHOD=GET ACTION=/gateway.php>\n"
          << "<INPUT TYPE=HIDDEN NAME=cgilet VALUE=person>\n"
          << "<INPUT TYPE=SUBMIT><BR>\n"
          << "</FORM>\n\n";
   }
};

void * create_cgi() {
   return new PersonTest();
}
