[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[patch] Add BookODBC sample
Iluxa,
Here is a sample of database application.
It uses ODBC to PostgreSQL db Books.
It displays all the existing records from "books" table, and allows user
to Add/Edit them
Y.
diff --git a/BooksODBC/BooksODBC.cpp b/BooksODBC/BooksODBC.cpp
new file mode 100644
index 0000000..6bd90e2
--- /dev/null
+++ b/BooksODBC/BooksODBC.cpp
@@ -0,0 +1,188 @@
+/***************************************************************************
+ * Copyright (C) 2004 by Ilya A. Volynets-Evenbakh *
+ * ilya@total-knowledge.com *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#include "BooksODBC.h"
+#include <sptk3/CException.h>
+#include <sptk3/CQuery.h>
+#include <cppserv/servletconfig.h>
+
+using namespace std;
+
+void BooksODBCServlet::service(cserv::ServletRequest& req, cserv::ServletResponse& resp)
+{
+ cserv::ServletOutputStream &out=resp.getOutputStream();
+ renderHeader(out);
+
+ std::string strDSN=getServletConfig().getInitParameter("DSN");
+ std::string strUID=getServletConfig().getInitParameter("UID");
+ std::string strPWD=getServletConfig().getInitParameter("PWD");
+ if(strDSN.empty())
+ strDSN="Books";
+ if(strUID.empty())
+ strUID="";
+ if(strPWD.empty())
+ strPWD="";
+ CODBCDatabase db("DSN="+strDSN+";UID="+strUID+";PWD="+strPWD);
+ try {
+ out<<"Openning the database... ";
+ db.open();
+ out<<"Driver description: " <<db.driverDescription().c_str() <<"<br><br>";
+
+ std::string strSubmit=req.getParameter("Submit");
+ if ((strSubmit=="Edit") || (strSubmit=="Add")) {
+ btnAddEdit(req, resp, out, db);
+ }
+ else {
+ if (strSubmit=="Save") {
+ btnSave(req, resp, out, db);
+ }
+ showFullTable(req, out, db);
+ }
+ out<<"Closing the database... ";
+ db.close();
+ out<<"Ok.<br>";
+ }
+ catch (exception& e) {
+ out<<"<br>Error: " <<e.what();
+ out<<"<br>Sorry, you have to fix your database connection.<br>";
+ }
+
+ renderFooter(out);
+}
+
+void BooksODBCServlet::renderHeader(std::ostream& out)
+{
+ out<<"<HTML><HEAD><TITLE>BooksODBC CPPSERV Servlet</TITLE></HEAD>"
+ "<BODY><H1>BooksODBC</H1>"
+ "<P>I am sample servlet running in "
+ "<A HREF=\"http://www.total-knowledge.com/progs/cppserv\">CPPSERV</A>"
+ "</P><P>";
+}
+
+void BooksODBCServlet::renderFooter(std::ostream& out)
+{
+ out<<"</P></BODY></HTML>";
+}
+
+void BooksODBCServlet::btnAddEdit(cserv::ServletRequest& req,
+ cserv::ServletResponse& resp,
+ std::ostream& out,
+ CODBCDatabase& db)
+{
+ std::string strQuery;
+ std::string strTitle = "";
+ std::string strAuthor = "";
+ std::string strComments = "";
+ std::string strBookID=req.getParameter("BookID");
+ out<<"<form action=\"" <<req.getRequestURI() <<"\" name=f><table border=0 cellspacing=2 cellpadding=2 width=100%>";
+ if (! strBookID.empty()) { // Edit: provide curent content of the row for edditing
+ out<<"<tr><td>Book ID:</td><td>" <<strBookID <<"</td></tr>";
+ strQuery="select title, author, comments from books where id="+strBookID;
+
+ CQuery qrySelectBookID(&db,strQuery);
+ qrySelectBookID.open();
+ if ( qrySelectBookID.eof() ) {
+ out<<"<FONT color=\"red\">Invalid book ID</FONT>.<BR>";
+ return; //we must find BookID record, because there is no deletion in our program
+ }
+ CField& titleField = qrySelectBookID["title"];
+ CField& authorField = qrySelectBookID["author"];
+ CField& commentsField = qrySelectBookID["comments"];
+ strTitle = titleField.asString();
+ strAuthor = authorField.asString();
+ strComments = commentsField.asString();
+ }
+ out<<"<tr><td>Title: </td><td><input maxlength=30 size=30 name=Title value=\""+strTitle+"\"></td></tr>"
+ "<tr><td>Author: </td><td><input maxlength=30 size=30 name=Author value=\""+strAuthor+"\"></td></tr>"
+ "<tr><td>Comments: </td><td><input maxlength=50 size=50 name=Comments value=\""+strComments+"\"></td></tr>"
+ "<tr><td><input type=hidden name=BookID value=" <<strBookID <<"></td>"
+ "<td><input type=submit value=\"Cancel\" name=Submit> "
+ "<input type=submit value=\"Save\" name=Submit></td></tr></form>";
+}
+
+void BooksODBCServlet::btnSave(cserv::ServletRequest& req,
+ cserv::ServletResponse& resp,
+ std::ostream& out,
+ CODBCDatabase& db)
+{
+ std::string strQuery;
+ std::string strTitle=req.getParameter("Title");
+ std::string strAuthor=req.getParameter("Author");
+ std::string strComments=req.getParameter("Comments");
+ std::string strBookID=req.getParameter("BookID");
+ if (strBookID.empty()) { // Add
+ strQuery="insert into books(title, author, comments) values('" +strTitle+"', '"+strAuthor+"', '"+strComments+"')";
+ }
+ else { //Edit
+ strQuery="update books set title='"+strTitle+"', author='"+strAuthor+"', comments='"+strComments+"' where id="+strBookID;
+ }
+ out << strQuery <<"<br>";
+ CQuery qrySave(&db, strQuery);
+ qrySave.exec();
+ out << "<font color=red>Table 'books' was updated successfuly</font><br><br>";
+}
+
+void BooksODBCServlet::showFullTable(cserv::ServletRequest& req, std::ostream& out, CODBCDatabase& db)
+{
+ int id;
+ std::string strTitle;
+ std::string strAuthor;
+ std::string strComments;
+ // Defining the queries
+ CQuery qrySelectAllBooks(&db,"select * from books");
+ qrySelectAllBooks.open();
+ out<<"<table border=5 cellspacing=0 cellpadding=0 width=100%>"
+ " <tr>"
+ " <th>Book ID</th>"
+ " <th>Author</th>"
+ " <th>Title</th>"
+ " <th>Comments</th>"
+ " <th>Action</th>"
+ " </tr>";
+
+ CField& idField = qrySelectAllBooks[0];
+ CField& titleField = qrySelectAllBooks["title"];
+ CField& authorField = qrySelectAllBooks["author"];
+ CField& commentsField = qrySelectAllBooks["comments"];
+
+ while ( ! qrySelectAllBooks.eof() ) {
+ id = idField.asInteger();
+ strTitle = titleField.asString();
+ strAuthor = authorField.asString();
+ strComments = commentsField.asString();
+
+ out <<"<tr>"
+ "<td>" <<id <<"</td>"
+ "<td>" <<strAuthor.c_str() <<"</td>"
+ "<td>" <<strTitle.c_str() <<"</td>"
+ "<td>" <<strComments.c_str() <<"</td>"
+ "<td><form action=\"" <<req.getRequestURI() <<"\" name=f>"
+ "<input type=hidden name=BookID value=" <<id <<">"
+ "<input type=submit value=\"Edit\" name=Submit></form>"
+ "</td></tr>";
+
+ qrySelectAllBooks.fetch();
+ }
+ qrySelectAllBooks.close();
+ out <<"</table><center><form action=\"" <<req.getRequestURI() <<"\" name=f>"
+ "<input type=submit value=\"Add\" name=Submit>"
+ "</form></center>";
+}
+
+DECLARE_SERVLET(BooksODBCServlet)
diff --git a/BooksODBC/BooksODBC.h b/BooksODBC/BooksODBC.h
new file mode 100644
index 0000000..9739436
--- /dev/null
+++ b/BooksODBC/BooksODBC.h
@@ -0,0 +1,55 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Yelena E. Volynets. *
+ * yvolynets@total-knowledge.com *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#ifndef BOOKSODBC_H
+#define BOOKSODBC_H
+
+#include <cppserv/servlet.h>
+#include <cppserv/servletrequest.h>
+#include <cppserv/servletresponse.h>
+#include <cppserv/servletoutputstream.h>
+
+
+#include <sptk3/CODBCDatabase.h>
+
+/**
+This sample demonstrates web db edditing using ODBC to PostgreSQL db Books.
+It displays all the existing records from "books" table, and allows user to Add/Edit them
+@author Yelena Volynets
+*/
+class BooksODBCServlet : public cserv::Servlet
+{
+private:
+ void renderHeader(std::ostream&);
+ void renderFooter(std::ostream&);
+ void btnAddEdit(cserv::ServletRequest& req,
+ cserv::ServletResponse& resp,
+ std::ostream& out,
+ CODBCDatabase& db);
+ void btnSave(cserv::ServletRequest& req,
+ cserv::ServletResponse& resp,
+ std::ostream& out,
+ CODBCDatabase& db);
+ void showFullTable(cserv::ServletRequest& req, std::ostream& out, CODBCDatabase& db);
+
+public:
+ virtual void service(cserv::ServletRequest& req, cserv::ServletResponse& resp);
+};
+
+#endif
diff --git a/BooksODBC/Makefile.am b/BooksODBC/Makefile.am
new file mode 100644
index 0000000..66c30dc
--- /dev/null
+++ b/BooksODBC/Makefile.am
@@ -0,0 +1,6 @@
+INCLUDES= $(all_includes)
+
+
+pkglib_LTLIBRARIES = BooksODBCServlet.la
+BooksODBCServlet_la_SOURCES = BooksODBC.cpp
+BooksODBCServlet_la_LDFLAGS = -module -lspdb3
diff --git a/Makefile.am b/Makefile.am
index 18d9208..29e0092 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,4 +2,4 @@
# have all needed files, that a GNU package needs
AUTOMAKE_OPTIONS = foreign 1.4
-SUBDIRS = hello fileupload redirect cookies session headers database sharedata params
+SUBDIRS = hello fileupload redirect cookies session headers database BooksODBC sharedata params
diff --git a/configure.in b/configure.in
index 7413af9..7d11a4e 100644
--- a/configure.in
+++ b/configure.in
@@ -7,4 +7,4 @@ AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL
-AC_OUTPUT(Makefile hello/Makefile fileupload/Makefile redirect/Makefile cookies/Makefile session/Makefile headers/Makefile database/Makefile sharedata/Makefile params/Makefile)
+AC_OUTPUT(Makefile hello/Makefile fileupload/Makefile redirect/Makefile cookies/Makefile session/Makefile headers/Makefile database/Makefile BooksODBC/Makefile sharedata/Makefile params/Makefile)
diff --git a/engine.xml b/engine.xml
index c7cf09a..b08b2b1 100644
--- a/engine.xml
+++ b/engine.xml
@@ -1,15 +1,18 @@
<?xml version="1.0"?>
-<port value="9004"/>
<app name="test">
- <sessiontimeout value="500"/>
<servlet name="HelloServlet" dso="./hello/.libs/HelloServlet.so"/>
<servlet name="FileUploadServlet" dso="./fileupload/.libs/FileUploadServlet.so"/>
<servlet name="RedirectServlet" dso="./redirect/.libs/RedirectServlet.so"/>
<servlet name="CookiesServlet" dso="./cookies/.libs/CookiesServlet.so"/>
<servlet name="SessionServlet" dso="./session/.libs/SessionServlet.so"/>
<servlet name="HeadersServlet" dso="./headers/.libs/HeadersServlet.so"/>
- <servlet name="DatabaseServlet" dso="./database/.libs/DatabaseServlet.so">
+<!-- <servlet name="DatabaseServlet" dso="./database/.libs/DatabaseServlet.so">
<parameter name="database" value="testdb.sqlite"/>
+ </servlet> -->
+ <servlet name="BooksODBCServlet" dso="./BooksODBC/.libs/BooksODBCServlet.so">
+ <parameter name="DSN" value="Set your value here"/>
+ <parameter name="UID" value="Set your value here"/>
+ <parameter name="PWD" value="Set ypur value here"/>
</servlet>
<servlet name="ShareDataServlet1" dso="./sharedata/.libs/ShareDataServlet1.so"/>
<servlet name="ShareDataServlet2" dso="./sharedata/.libs/ShareDataServlet2.so"/>