• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/data/MySqlCommand.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/text/StringBuffer.h>
00018 #include <spl/data/MySqlConnection.h>
00019 
00020 #if defined(HAVE_MYSQL_H) || defined(HAVE_MYSQL_MYSQL_H)
00021 
00022 #ifdef HAVE_MYSQL_H
00023 #include <mysql.h>
00024 #else
00025 #include <mysql/mysql.h>
00026 #endif
00027 
00028 extern RecordSet *_LoadMySqlRecordSet(MYSQL *mysql, MYSQL_RES *result);
00029 
00030 MySqlCommand::MySqlCommand(MySqlConnection *mysql)
00031 : Command(), m_mysql(mysql)
00032 {
00033 }
00034 
00035 MySqlCommand::MySqlCommand(MySqlConnection *mysql, const String& cmdtxt)
00036 : Command(cmdtxt), m_mysql(mysql)
00037 {
00038 }
00039 
00040 MySqlCommand::~MySqlCommand()
00041 {
00042 }
00043 
00044 String MySqlCommand::ToSQL()
00045 {
00046         StringBuffer buf(255);
00047 
00048         buf.Append("CALL ");
00049         buf.Append(&m_cmdtxt);
00050 
00051         int count = m_prm.Count();
00052         for ( int x = 0; x < count; x++ )
00053         {
00054                 CommandParameterPtr& prm = m_prm.ElementAtRef(x);
00055                 prm.ValidateMem();
00056                 if ( x > 0 )
00057                 {
00058                         buf.Append(',');
00059                 }
00060                 if ( !prm->IsNumeric() )
00061                 {
00062                         buf.Append('\'');
00063                 }
00064                 buf.Append(prm->ToString()->GetChars());
00065                 if ( !prm->IsNumeric() )
00066                 {
00067                         buf.Append('\'');
00068                 }
00069         }
00070         return *buf.ToString();
00071 }
00072 
00073 void MySqlCommand::CommandTextSet(const String& txt)
00074 {
00075         m_cmdtxt.Set(txt);
00076 }
00077 
00078 void MySqlCommand::Prepare()
00079 {
00080         throw new NotImplementedException();
00081 }
00082 
00083 int MySqlCommand::ExecuteNonQuery()
00084 {
00085         String sql = ToSQL();
00086         return m_mysql->ExecuteNonQuery(sql.GetChars());
00087 }
00088 
00089 RecordSetPtr MySqlCommand::ExecuteQuery()
00090 {
00091         String sql = ToSQL();
00092         return m_mysql->ExecuteQuery(sql.GetChars());
00093 }
00094 
00095 #if defined(DEBUG)
00096 void MySqlCommand::CheckMem() const
00097 {
00098         Command::CheckMem();
00099 }
00100 
00101 void MySqlCommand::ValidateMem() const
00102 {
00103         Command::ValidateMem();
00104 }
00105 #endif
00106 
00107 #endif