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

src/data/Connection.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/data/Command.h>
00018 #include <spl/data/Connection.h>
00019 
00020 #ifdef _WINDOWS
00021 #include <spl/configwin32.h>
00022 #else
00023 #include <spl/autoconf/config.h>
00024 #endif
00025 
00026 Transaction::Transaction()
00027 {
00028 }
00029 
00030 Transaction::~Transaction()
00031 {
00032 }
00033 
00034 Command::Command()
00035 : m_prmIdx(), m_prm(), m_cmdtxt()
00036 {
00037 }
00038 
00039 Command::Command(const String& cmdtxt)
00040 : m_prmIdx(), m_prm(), m_cmdtxt(cmdtxt)
00041 {
00042 }
00043 
00044 Command::Command(const Command& cmd)
00045 : m_prmIdx(), m_prm(), m_cmdtxt()
00046 {
00047         *this = cmd;
00048 }
00049 
00050 Command::~Command()
00051 {
00052         //int count = m_prm.Count();
00053         //for ( int x = 0; x < count; x++ )
00054         //{
00055         //      delete m_prm.ElementAt(x);
00056         //}
00057 }
00058 
00059 Command& Command::operator =(const Command& cmd)
00060 {
00061         Clear();
00062 
00063         m_cmdtxt = cmd.m_cmdtxt;
00064 
00065         int count = cmd.m_prm.Count();
00066         for ( int x = 0; x < count; x++ )
00067         {
00068                 CommandParameterPtr prm = CommandParameterPtr(new CommandParameter(*cmd.m_prm.ElementAt(x)));
00069                 m_prmIdx.Set( String(prm->Name()), prm );
00070                 m_prm.Add( prm );
00071         }
00072 
00073         return *this;
00074 }
00075 
00076 void Command::Clear()
00077 {
00078         //int count = m_prm.Count();
00079         //for ( int x = 0; x < count; x++ )
00080         //{
00081         //      delete m_prm.ElementAt(x);
00082         //}
00083         m_prm.Clear();
00084         m_prmIdx.Clear();
00085 }
00086 
00087 void Command::CommandTextSet(const String& txt)
00088 {
00089         m_cmdtxt = txt;
00090 }
00091 
00092 CommandParameterPtr Command::CreateParameter(const String& name, int type, int direction, int len)
00093 {
00094         CommandParameterPtr prm = CommandParameterPtr(new CommandParameter(name, type, direction, len));
00095         m_prmIdx.Set( prm->Name(), prm );
00096         m_prm.Add( prm );
00097         ValidateMem();
00098         return prm;
00099 }
00100 
00101 CommandParameterPtr Command::CreateParameter(const String& name, int type, int direction)
00102 {
00103         CommandParameterPtr prm = CommandParameterPtr(new CommandParameter(name, type, direction, 4));
00104         m_prmIdx.Set( prm->Name(), prm );
00105         m_prm.Add( prm );
00106         return prm;
00107 }
00108 
00109 CommandParameterPtr Command::CreateParameter(const String& name, const String& value)
00110 {
00111         CommandParameterPtr prm = CreateParameter(name, DbSqlType::SQL_TYPE_VARCHAR,ParameterDirection::PARAM_DIR_IN, value.Length());
00112         prm->Set(value);
00113         return prm;
00114 }
00115 
00116 CommandParameterPtr Command::CreateParameter(const String& name, int32 value)
00117 {
00118         CommandParameterPtr prm = CreateParameter(name, DbSqlType::SQL_TYPE_INT32,ParameterDirection::PARAM_DIR_IN, 4);
00119         prm->Set(value);
00120         return prm;
00121 }
00122 
00123 CommandParameterPtr Command::CreateParameter(const String& name, int8 value)
00124 {
00125         CommandParameterPtr prm = CreateParameter(name, DbSqlType::SQL_TYPE_INT8,ParameterDirection::PARAM_DIR_IN, 1);
00126         prm->Set(value);
00127         return prm;
00128 }
00129 
00130 CommandParameterPtr Command::CreateParameter(const String& name, float32 value)
00131 {
00132         CommandParameterPtr prm = CreateParameter(name, DbSqlType::SQL_TYPE_FLOAT32,ParameterDirection::PARAM_DIR_IN, 4);
00133         prm->Set(value);
00134         return prm;
00135 }
00136 
00137 CommandParameterPtr Command::CreateParameter(const String& name, float64 value)
00138 {
00139         CommandParameterPtr prm = CreateParameter(name, DbSqlType::SQL_TYPE_FLOAT64,ParameterDirection::PARAM_DIR_IN, 8);
00140         prm->Set(value);
00141         return prm;
00142 }
00143 
00144 CommandParameterPtr Command::GetParameter(const String& name) const
00145 {
00146         return m_prmIdx.Get(name);
00147 }
00148 
00149 void Command::Prepare()
00150 {
00151         throw new NotImplementedException ();
00152 }
00153 
00154 int Command::ExecuteNonQuery()
00155 {
00156         throw new NotImplementedException ();
00157 }
00158 
00159 RecordSetPtr Command::ExecuteQuery()
00160 {
00161         throw new NotImplementedException ();
00162 }
00163 
00164 #if defined(DEBUG)
00165 void Command::CheckMem() const
00166 {
00167         m_cmdtxt.CheckMem();
00168         m_prmIdx.CheckMem();
00169         m_prm.CheckMem();
00170         
00171         int count = m_prm.Count();
00172         for ( int x = 0; x < count; x++ )
00173         {
00174                 CommandParameterPtr prm = m_prm.ElementAt(x);
00175                 prm.CheckMem();
00176         }
00177 }
00178 
00179 void Command::ValidateMem() const
00180 {
00181         m_cmdtxt.ValidateMem();
00182         m_prmIdx.ValidateMem();
00183         m_prm.ValidateMem();
00184         
00185         int count = m_prm.Count();
00186         for ( int x = 0; x < count; x++ )
00187         {
00188                 CommandParameterPtr prm = m_prm.ElementAt(x);
00189                 prm.ValidateMem();
00190         }
00191 }
00192 #endif
00193 
00194 Connection::Connection
00195 (
00196         const String& serverOrIP, 
00197         const String& database, 
00198         const String& uid, 
00199         const String& pw
00200 )
00201 : m_host(serverOrIP), m_database(database), m_uid(uid), m_pw(pw)
00202 {
00203 }
00204 
00205 Connection::~Connection()
00206 {
00207 }
00208 
00209 #include <spl/data/MySqlConnection.h>
00210 
00211 ConnectionPtr Connection::GetConnection(const String& connectString)
00212 {
00213         String provider;
00214         String server;
00215         String database;
00216         String uid;
00217         String pw;
00218 
00219         String cs(connectString);
00220         RefCountPtr<Vector<StringPtr> > parts = cs.Split(";");
00221         int count = parts->Count();
00222         int idx;
00223         for ( int x = 0; x < count; x++ )
00224         {
00225                 StringPtr part = parts->ElementAt(x);
00226                 if ( 0 > (idx = part->IndexOf('=')) )
00227                 {
00228                         throw new SqlException("Invalid connection string");
00229                 }
00230                 StringPtr key = part->Substring(0, idx)->Trim()->ToLower();
00231                 if ( key->Equals("provider") )
00232                 {
00233                         provider = *part->Substring(idx+1)->Trim();
00234                 }
00235                 else if ( key->Equals("server") )
00236                 {
00237                         server = *part->Substring(idx+1)->Trim();
00238                 }
00239                 else if ( key->Equals("database") )
00240                 {
00241                         database = *part->Substring(idx+1)->Trim();
00242                 }
00243                 else if ( key->Equals("uid") )
00244                 {
00245                         uid = *part->Substring(idx+1)->Trim();
00246                 }
00247                 else if ( key->Equals("user id") )
00248                 {
00249                         uid = *part->Substring(idx+1)->Trim();
00250                 }
00251                 else if ( key->Equals("pw") )
00252                 {
00253                         pw = *part->Substring(idx+1)->Trim();
00254                 }
00255                 else if ( key->Equals("pwd") )
00256                 {
00257                         pw = *part->Substring(idx+1)->Trim();
00258                 }
00259                 else if ( key->Equals("password") )
00260                 {
00261                         pw = *part->Substring(idx+1)->Trim();
00262                 }
00263                 else
00264                 {
00265                         throw new SqlException("Unknown connect string key");
00266                 }
00267         }
00268 
00269         if ( provider.Length() == 0 )
00270         {
00271                 throw new SqlException("provider key is required");
00272         }
00273         if ( server.Length() == 0 )
00274         {
00275                 throw new SqlException("server key is required");
00276         }
00277         if ( database.Length() == 0 )
00278         {
00279                 throw new SqlException("database key is required");
00280         }
00281         if ( uid.Length() == 0 )
00282         {
00283                 throw new SqlException("user id key is required");
00284         }
00285         if ( pw.Length() == 0 )
00286         {
00287                 throw new SqlException("pw key is required");
00288         }
00289         ConnectionPtr con;
00290         if ( provider.Equals("mysql") )
00291         {
00292 #if defined(HAVE_MYSQL_H) || defined(HAVE_MYSQL_MYSQL_H)
00293                 con = ConnectionPtr(new MySqlConnection(server, database, uid, pw));
00294 #else
00295                 throw new InvalidArgumentException("mysql isn't available on your system (on windows, enable HAVE_MYSQL_H in spl/configwin32.h).");
00296 #endif
00297         }
00298         else if ( provider.Equals("mssql") )
00299         {
00300 //#if defined(HAVE_SQLFRONT_H) && defined(HAVE_SQLDB_H)
00301 //              throw new NotImplementedException("mssql support isn't complete yet");
00302 //#else
00303 //              throw new InvalidArgumentException("mssql isn't available on your system (on windows, enable HAVE_SQLFRONT_H and HAVE_SQLDB_H in spl/configwin32.h).");
00304 //#endif
00305                 throw new NotImplementedException("mssql is not longer supported.");
00306         }
00307         else
00308         {
00309                 throw new InvalidArgumentException("Unsupported provider; must be mysql or mssql");
00310         }
00311 
00312         return con;
00313 }
00314 
00315 #if defined(DEBUG)
00316 void Connection::CheckMem() const
00317 {
00318         m_host.CheckMem();
00319         m_database.CheckMem();
00320         m_uid.CheckMem();
00321         m_pw.CheckMem();
00322 }
00323 
00324 void Connection::ValidateMem() const
00325 {
00326         m_host.ValidateMem();
00327         m_database.ValidateMem();
00328         m_uid.ValidateMem();
00329         m_pw.ValidateMem();
00330 }
00331 #endif
00332