SQLStatementImpl.hh

00001 //********************************************************************
00002 //*** ResourcePool/SQL/SQLStatementImpl.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: SQLStatementImpl.hh,v 1.1 2009-05-04 13:20:43 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 #ifndef INCLUDE_SQLSTATEMENTIMPL_HH
00025 #define INCLUDE_SQLSTATEMENTIMPL_HH
00026 
00027 #include <string>
00028 #include <vector>
00029 
00030 #include "Exceptions/Exception.hh"
00031 #include "mm/gc_ptr.hh"
00032 #include "SQLStatement.hh"
00033 
00034 namespace fatalmind {
00035 namespace SQL {
00036 
00037 class SQLParser; // Fwd
00038 
00039 class SQLStatementImpl: public usg_cnt_t<DefaultThreadedModel> { // TODO, TM
00040     public:
00043         class Parameter {
00044             public:
00045                 Parameter() 
00046                 : name()
00047                 , type(SQLStatement::Parameter::UNKNOWN)
00048                 {
00049                 }
00050             
00051                 std::string getName() const {
00052                     return name;
00053                 }
00054 
00055                 int getType() const {
00056                     return type;
00057                 }
00058 
00059             private:
00060                 std::string name;
00061                 int type;
00062 
00063                 void setName(const char* const n, int length) {
00064                     name = std::string(n, length);
00065                 }
00066     
00067                 void setType(const int t) {
00068                     type = t;
00069                 }
00070 
00071                 void reset() {
00072                     name.clear();
00073                     type = SQLStatement::Parameter::UNKNOWN;
00074                 }
00075             
00076             friend
00077                 class SQLParser;
00078         };
00079 
00080         SQLStatementImpl();
00081 
00082         SQLStatementImpl(const std::string& sql);
00083         SQLStatementImpl(const char*);
00084 
00085         void parse();
00086         
00087         unsigned int getParamCount() const;
00088         std::string getNameFor(const int pos) const;
00089         int getTypeFor(const int pos) const;
00090         const std::string& getSQL() const;
00091         const std::string& getSQLFragment(const int pos) const;
00092 
00093         bool operator==(const SQLStatementImpl& rho) {
00094             return sql == rho.sql;
00095         };
00096         bool operator!=(const SQLStatementImpl& rho) {
00097             return sql != rho.sql;
00098         };
00099 
00100     private:
00101         // the parser gives two absolute positions
00102         // for substr we need the start position and the length
00103         void addFragment(const int begin, const int end) {
00104             int length = end - begin;
00105             fragments.push_back(sql.substr(begin, length));
00106         }
00107 
00108         void addParam(const Parameter& p) {
00109             parameters.push_back(p);
00110         }
00111 
00112         void buildNameMap();
00113 
00114         std::string sql;
00115         std::vector<std::string>          fragments;
00116         std::vector<class Parameter>      parameters;
00117     friend
00118         class SQLParser;
00119 
00120         template<class charT, class traits>
00121         friend
00122                 std::basic_ostream<charT, traits>& operator<<(std::basic_ostream<charT, traits>& s, const SQLStatementImpl& rho);
00123 };
00124 
00125 } // namespace SQL
00126 } // namespace fatalmind
00127 
00128 template<class charT, class traits>
00129 std::basic_ostream<charT, traits>& fatalmind::SQL::operator<<(std::basic_ostream<charT, traits>& s, const fatalmind::SQL::SQLStatementImpl& rho) {
00130     s << rho.sql;
00131     return s;
00132 }
00133 
00134 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3