From 9d547523eb8abf32d05a829df9e4414729669191 Mon Sep 17 00:00:00 2001 From: CaS Date: Wed, 23 Nov 2005 09:25:05 +0000 Subject: [PATCH] Added test program git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/sqlclient/trunk@22083 72102866-910b-0410-8b05-ffd578937521 --- SQLite.m | 13 +++++- testSQLite.m | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 testSQLite.m diff --git a/SQLite.m b/SQLite.m index d1167b8..893f56b 100644 --- a/SQLite.m +++ b/SQLite.m @@ -211,7 +211,7 @@ char *statement; int result; sqlite3_stmt *prepared; - const char *prepEnd; + const char *stmtEnd; /* * Ensure we have a working connection. @@ -225,7 +225,7 @@ statement = (char*)[stmt UTF8String]; result = sqlite3_prepare((sqlite3 *)extra, - statement, -1, &prepared, &prepEnd); + statement, strlen(statement), &prepared, &stmtEnd); if (result != SQLITE_OK) { [NSException raise: SQLException @@ -349,5 +349,14 @@ static char hex[16] = "0123456789ABCDEF"; return 3 + [blob length] * 2; } +- (NSString*) quote: (id)obj +{ + if ([obj isKindOfClass: [NSDate class]] == YES) + { + obj = [NSNumber numberWithDouble: [obj timeIntervalSinceReferenceDate]]; + } + return [super quote: obj]; +} + @end diff --git a/testSQLite.m b/testSQLite.m new file mode 100644 index 0000000..4ce7b12 --- /dev/null +++ b/testSQLite.m @@ -0,0 +1,129 @@ +/** + Copyright (C) 2005 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: April 2005 + + This file is part of the SQLClient Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. + + $Date$ $Revision$ + */ + +#include +#include "SQLClient.h" + +int +main() +{ + CREATE_AUTORELEASE_POOL(pool); + SQLClient *db; + NSUserDefaults *defs; + NSMutableArray *records; + SQLRecord *record; + unsigned char dbuf[256]; + unsigned int i; + NSData *data; + + defs = [NSUserDefaults standardUserDefaults]; + [defs registerDefaults: + [NSDictionary dictionaryWithObjectsAndKeys: + [NSDictionary dictionaryWithObjectsAndKeys: + [NSDictionary dictionaryWithObjectsAndKeys: + @"test", @"Database", + @"", @"User", + @"", @"Password", + @"SQLite", @"ServerType", + nil], + @"test", + nil], + @"SQLClientReferences", + nil] + ]; + + for (i = 0; i < 256; i++) + { + dbuf[i] = i; + } + data = [NSData dataWithBytes: dbuf length: i]; + + db = [SQLClient clientWithConfiguration: nil name: @"test"]; + [db setDurationLogging: 0]; + + NS_DURING + [db execute: @"drop table xxx", nil]; + NS_HANDLER + NS_ENDHANDLER + + [db execute: @"create table xxx ( " + @"k char(40), " + @"char1 char(1), " + @"intval int, " + @"realval real, " + @"b blob)", + nil]; + + [db execute: @"insert into xxx " + @"(k, char1, intval, realval, b) " + @"values (" + @"'hello', " + @"'X', " + @"1, " + @"9.99, ", + data, + @")", + nil]; + + [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1]]; + + [db execute: @"insert into xxx " + @"(k, char1, intval, realval, b) " + @"values (" + @"'hello', " + @"'X', " + @"1, ", + @"12345.6789, ", + [NSData dataWithBytes: "" length: 0], @")", + nil]; + + records = [db query: @"select * from xxx", nil]; + [db execute: @"drop table xxx", nil]; + + if ([records count] != 2) + { + NSLog(@"Expected 2 records but got %u", [records count]); + } + else + { + record = [records objectAtIndex: 0]; + if ([[record objectForKey: @"b"] isEqual: data] == NO) + { + NSLog(@"Retrieved data does not match saved data %@ %@", + data, [record objectForKey: @"b"]); + } + record = [records objectAtIndex: 1]; + if ([[record objectForKey: @"b"] isEqual: [NSData data]] == NO) + { + NSLog(@"Retrieved empty data does not match saved data"); + } + } + + NSLog(@"Records - %@", records); + + RELEASE(pool); + return 0; +} +