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

test/SqlLiteTest.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/SqlLiteConnection.h>
00018 
00019 #ifdef DEBUG
00020 
00021 #include <spl/io/File.h>
00022 #include <spl/Log.h>
00023 
00024 static void _TestSqlLite1()
00025 {
00026         const char *filename = "test.sqlite";
00027 
00028         if (File::Exists(filename))
00029         {
00030                 File::Delete(filename);
00031         }
00032 
00033         {
00034                 SqlLiteConnection con(filename);
00035                 con.Open();
00036 
00037                 DEBUG_CLEAR_MEM_CHECK_POINTS();
00038                 con.CheckMem();
00039                 DEBUG_DUMP_MEM_LEAKS();
00040                 UNIT_ASSERT_MEM_NOTED("SQLite3 1.1");
00041 
00042                 con.ExecuteNonQuery("CREATE TABLE data (PK INT PRIMARY KEY, NAME VARCHAR(255));");
00043 
00044                 TransactionPtr trans = con.BeginTransaction();
00045                 con.ExecuteNonQuery("INSERT INTO data (PK, NAME) VALUES (1, 'bob');");
00046                 con.ExecuteNonQuery("INSERT INTO data (PK, NAME) VALUES (2, 'bill');");
00047                 trans->Rollback();
00048 
00049                 RecordSetPtr rs = con.ExecuteQuery("SELECT * FROM data;");
00050                 UNIT_ASSERT("rollback failed", rs->RowCount() == 0);
00051 
00052                 trans = con.BeginTransaction();
00053                 con.ExecuteNonQuery("INSERT INTO data (PK, NAME) VALUES (1, 'bob');");
00054                 con.ExecuteNonQuery("INSERT INTO data (PK, NAME) VALUES (2, 'bill');");
00055                 trans->Commit();
00056 
00057                 rs = con.ExecuteQuery("SELECT * FROM data;");
00058                 UNIT_ASSERT("inserts failed", rs->RowCount() == 2);
00059                 UNIT_ASSERT("1,1 = 1", rs->GetColumn(0)->GetInt32(0) == 1);
00060                 UNIT_ASSERT("2,1 = 2", rs->GetColumn(0)->GetInt32(1) == 2);
00061                 UNIT_ASSERT("1,2 = 'bob'", rs->GetColumn(1)->GetVarchar(0)->Equals("bob"));
00062                 UNIT_ASSERT("2,2 = 'bill'", rs->GetColumn(1)->GetVarchar(1)->Equals("bill"));
00063 
00064                 CommandPtr cmd = con.CreateCommand("SELECT * FROM data WHERE PK = @pk;");
00065                 cmd->CreateParameter("@pk", DbSqlType::SQL_TYPE_INT32, ParameterDirection::PARAM_DIR_IN, 4)->Set(1);
00066                 
00067                 rs = cmd->ExecuteQuery();
00068                 UNIT_ASSERT("command parameter failed", rs->RowCount() == 1);
00069                 UNIT_ASSERT("1,1 = 1", rs->GetColumn(0)->GetInt32(0) == 1);
00070                 UNIT_ASSERT("1,2 = 'bob'", rs->GetColumn(1)->GetVarchar(0)->Equals("bob"));
00071 
00072                 cmd.Release();
00073                 con.Close();
00074         }
00075 
00076         File::Delete(filename);
00077 
00078         DEBUG_CLEAR_MEM_CHECK_POINTS();
00079         DEBUG_DUMP_MEM_LEAKS();
00080         UNIT_ASSERT_MEM_NOTED("SQLite3 1.2");
00081         Log::SWriteOkFail( "SQLite3 test 1" );
00082 }
00083 
00084 void _TestSqlLite()
00085 {
00086         _TestSqlLite1();
00087         DEBUG_CLEAR_MEM_CHECK_POINTS();
00088         DEBUG_DUMP_MEM_LEAKS();
00089 }
00090 
00091 #endif