First public contribution.
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
21 ///////////////////////////////////////////////////////////////////////////////////////
23 RTest TheTest(_L("t_sqllang test"));
24 _LIT(KTestDir, "c:\\test\\");
26 _LIT(KTestDbName, "c:\\test\\t_sqllang_1.db");
27 _LIT(KTestDbName2, "c:\\test\\t_sqllang_2.db");
31 ///////////////////////////////////////////////////////////////////////////////////////
33 void DeleteTestFiles()
35 RSqlDatabase::Delete(KTestDbName2);
36 RSqlDatabase::Delete(KTestDbName);
39 ///////////////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////////////
41 //Test macros and functions
42 void Check(TInt aValue, TInt aLine)
47 TheTest(EFalse, aLine);
50 void Check(TInt aValue, TInt aExpected, TInt aLine)
52 if(aValue != aExpected)
55 RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
56 TheTest(EFalse, aLine);
59 void Check2(TInt64 aValue, TInt64 aExpected, TInt aLine)
61 if(aValue != aExpected)
64 RDebug::Print(_L("*** Expected error: %ld, got: %ld\r\n"), aExpected, aValue);
65 TheTest(EFalse, aLine);
68 #define TEST(arg) ::Check((arg), __LINE__)
69 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
70 #define TEST3(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__)
72 ///////////////////////////////////////////////////////////////////////////////////////
77 TInt err = fs.Connect();
80 err = fs.MkDir(KTestDir);
81 TEST(err == KErrNone || err == KErrAlreadyExists);
86 ///////////////////////////////////////////////////////////////////////////////////////
88 void PrintLastMsgIfError(TInt aErr)
90 if(aErr < 0 && SqlRetCodeClass(aErr) == ESqlDbError)
92 const TPtrC& msg = TheDb.LastErrorMessage();
93 RDebug::Print(_L("Last error msg: \"%S\"\r\n"), &msg);
98 @SYMTestCaseID SYSLIB-SQL-CT-1611
99 @SYMTestCaseDesc Create a table with INTEGER, SMALLINT, REAl, DOUBLE PRECISION, FLOAT, DECIMAL,...
100 columns. Insert some records, retrieve the column values, verify the column values,
101 try some mathematical operations with the column values, executed in the SQL
103 @SYMTestPriority High
104 @SYMTestActions Testing SQL engine behaviour with different numerical types.
105 Testing mathematical operations with numeric columns.
106 @SYMTestExpectedResults Test must not fail
110 void NumericDataTypesTest()
112 TEST2(TheDb.Create(KTestDbName), KErrNone);
114 //Create a table with all possible numeric field types
115 _LIT8(KSql1, "CREATE TABLE Tbl(A INTEGER, B SMALLINT, C REAL, D DOUBLE PRECISION, E FLOAT, \
116 F DECIMAL, G BIGINT, H TINYINT, I BIT, J NUMERIC, K MONEY, \
118 TInt err = TheDb.Exec(KSql1);
119 PrintLastMsgIfError(err);
121 //Insert one record in to the created table
122 _LIT8(KSql2, "INSERT INTO Tbl(A,B,C,D,E,F,G,H,I,J,K,L) VALUES(2000000000, 30000, 123.45, 0.912E+55,\
123 1.34E-14, 1234.5678, 32000000000, 100, 7, 23.123456, 2123678.56, 11.45)");
124 err = TheDb.Exec(KSql2);
125 PrintLastMsgIfError(err);
127 //Get the inserted record data
129 err = stmt.Prepare(TheDb, _L("SELECT * FROM TBL"));
130 PrintLastMsgIfError(err);
131 TEST2(err, KErrNone);
133 PrintLastMsgIfError(err);
134 TEST2(err, KSqlAtRow);
135 //Check column values
136 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("a"))) == 2000000000);
137 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("b"))) == 30000);
138 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("c"))) - 123.45) < 0.000001);
139 //The next column value test is not working! The extracted column value is 0.9120000000000002E+55
140 //I guess the reason is that SQLITE uses manifest typing and "DOUBLE PRECISION" is not in its keyword
141 //list. Maybe it is interpreted and stored as 4-byte float value.
142 //TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("d"))) - 0.912E+55) < 0.000001);
143 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("e"))) - 1.34E-14) < 0.000001);
144 TEST(stmt.ColumnReal(stmt.ColumnIndex(_L("f"))) == 1234.5678);
145 TEST(stmt.ColumnInt64(stmt.ColumnIndex(_L("g"))) == 32000000000LL);
146 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("h"))) == 100);
147 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("i"))) == 7);
148 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("j"))) - 23.123456) < 0.000001);
149 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("k"))) - 2123678.56) < 0.000001);
150 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("l"))) - 11.45) < 0.000001);
152 //The statement object has to be closed before TheDb.Exec() call,
153 //otherwise the reported error is "database table is locked"
154 //Insert second record in to the created table but inverse the column types!
155 _LIT8(KSql3, "INSERT INTO Tbl(A, B, C, D, E, F, G, H, I, J,K,L) VALUES(\
156 -2.5,1.1,12,23,45,111,5.6,7.9,1.1,2,6,7)");
157 err = TheDb.Exec(KSql3);
158 PrintLastMsgIfError(err);
160 //Get the inserted record data
161 err = stmt.Prepare(TheDb, _L("SELECT * FROM TBL"));
162 PrintLastMsgIfError(err);
163 TEST2(err, KErrNone);
164 TEST2(stmt.Next(), KSqlAtRow);
166 PrintLastMsgIfError(err);
167 TEST2(err, KSqlAtRow);
168 //No column value checking because SQLITE inverted the column types!
169 //Check column values
170 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("a"))) - (-2.5)) < 0.000001);
171 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("b"))) - 1.1) < 0.000001);
172 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("c"))) == 12);
173 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("d"))) == 23);
174 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("e"))) == 45);
175 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("f"))) == 111);
176 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("g"))) - 5.6) < 0.000001);
177 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("h"))) - 7.9) < 0.000001);
178 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("i"))) - 1.1) < 0.000001);
179 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("j"))) == 2);
180 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("k"))) == 6);
181 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("l"))) == 7);
184 //Insert third record in to the created table
185 _LIT8(KSql4, "INSERT INTO Tbl(A,B,C, D, E, F, G, H, I,J, K, L) VALUES(\
186 2,3,123.45,1.5,2.5,1.56,320,100,7,23.123,212.56,11.45)");
187 err = TheDb.Exec(KSql4);
188 PrintLastMsgIfError(err);
190 //Issue a "SELECT" statement doing there some arithmetic operations and comparisons on the column values.
191 err = stmt.Prepare(TheDb, _L("SELECT A, A-C AS A2, K + L AS A3, H*I AS A4, E/D AS A5 FROM TBL WHERE A > 0 AND A < 10"));
192 PrintLastMsgIfError(err);
193 TEST2(err, KErrNone);
194 TEST2(stmt.Next(), KSqlAtRow);
195 //Check column values
196 TEST2(stmt.ColumnInt(stmt.ColumnIndex(_L("a"))), 2);
197 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("a2"))) - (2-123.45)) < 0.000001);
198 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("a3"))) - (212.56+11.45)) < 0.000001);
199 TEST(stmt.ColumnInt(stmt.ColumnIndex(_L("a4"))) == 700);
200 TEST(Abs(stmt.ColumnReal(stmt.ColumnIndex(_L("a5"))) - (2.5/1.5)) < 0.000001);
201 //There should be no more records
202 TEST2(stmt.Next(), KSqlAtEnd);
206 (void)RSqlDatabase::Delete(KTestDbName);
210 @SYMTestCaseID SYSLIB-SQL-CT-1630
211 @SYMTestCaseDesc Built-in functions test.
212 abs(), coalesce(), ifnull(), last_insert_rowid(), length(), like(), lower(),
213 max(), min(), nullif(), quote(), random(), round(), sqlite_version(), substr(),
214 typeof(), upper(), avg(), count(), sun().
215 @SYMTestPriority High
216 @SYMTestActions Built-in functions test.
217 @SYMTestExpectedResults Test must not fail
221 void BuiltInFunctionsTest()
223 (void)RSqlDatabase::Delete(KTestDbName);
224 TInt err = TheDb.Create(KTestDbName);
225 TEST2(err, KErrNone);
227 _LIT(KCreateSql, "CREATE TABLE A(Id Integer, \
228 F1 Integer DEFAULT 0, \
229 F2 Integer Default NULL, \
230 F3 TEXT Default Null, \
231 F4 TEXT Default Null)");
232 err = TheDb.Exec(KCreateSql);
235 _LIT(KInsertSql1, "INSERT INTO A(Id, F1) VALUES(1, 100)");
236 err = TheDb.Exec(KInsertSql1);
239 _LIT(KInsertSql2, "INSERT INTO A(Id, F1) VALUES(2, 1)");
240 err = TheDb.Exec(KInsertSql2);
243 _LIT(KInsertSql3, "INSERT INTO A(Id, F3) VALUES(3, 'ABCD')");
244 err = TheDb.Exec(KInsertSql3);
247 _LIT(KInsertSql4, "INSERT INTO A(Id, F4) VALUES(4, 'DCBA')");
248 err = TheDb.Exec(KInsertSql4);
253 //abs() test --------------------------------------------------------
254 err = stmt.Prepare(TheDb, _L("SELECT * FROM A WHERE F1 > ABS(-10)"));
255 TEST2(err, KErrNone);
257 TEST2(err, KSqlAtRow);
258 TEST(stmt.ColumnInt(1) == 100);
261 //coalesce() test --------------------------------------------------------
262 err = stmt.Prepare(TheDb, _L("SELECT COALESCE(F3, F4) AS F FROM A"));
263 TEST2(err, KErrNone);
266 TEST2(err, KSqlAtRow);
267 TEST(stmt.IsNull(0));
270 TEST2(err, KSqlAtRow);
271 TEST(stmt.IsNull(0));
273 _LIT(KTextVal1, "ABCD");
275 TEST2(err, KSqlAtRow);
277 err = stmt.ColumnText(0, colVal);
278 TEST2(err, KErrNone);
279 TEST(colVal == KTextVal1);
281 _LIT(KTextVal2, "DCBA");
283 TEST2(err, KSqlAtRow);
284 err = stmt.ColumnText(0, colVal);
285 TEST2(err, KErrNone);
286 TEST(colVal == KTextVal2);
290 //ifnull() test --------------------------------------------------------
291 err = stmt.Prepare(TheDb, _L("SELECT IFNULL(F3, F4) AS F FROM A"));
292 TEST2(err, KErrNone);
295 TEST2(err, KSqlAtRow);
296 TEST(stmt.IsNull(0));
299 TEST2(err, KSqlAtRow);
300 TEST(stmt.IsNull(0));
303 TEST2(err, KSqlAtRow);
304 err = stmt.ColumnText(0, colVal);
305 TEST2(err, KErrNone);
306 TEST(colVal == KTextVal1);
309 TEST2(err, KSqlAtRow);
310 err = stmt.ColumnText(0, colVal);
311 TEST2(err, KErrNone);
312 TEST(colVal == KTextVal2);
316 //last_insert_rowid() test --------------------------------------------------------
317 err = stmt.Prepare(TheDb, _L("SELECT last_insert_rowid() AS F"));
318 TEST2(err, KErrNone);
320 TEST2(err, KSqlAtRow);
321 TInt64 colVal64 = stmt.ColumnInt64(0);
322 RDebug::Print(_L("Last insert row id=%d\r\n"), (TInt)colVal64);
325 //length() test --------------------------------------------------------
326 err = stmt.Prepare(TheDb, _L("SELECT length(F4) AS L FROM A WHERE ID >= 3"));
327 TEST2(err, KErrNone);
330 TEST2(err, KSqlAtRow);
331 TEST(stmt.ColumnInt(0) == 0);
334 TEST2(err, KSqlAtRow);
335 TEST(stmt.ColumnInt(0) == 4);
339 //like() test --------------------------------------------------------
340 err = stmt.Prepare(TheDb, _L("SELECT Id FROM A WHERE F4 LIKE 'DC%'"));
341 TEST2(err, KErrNone);
343 TEST2(err, KSqlAtRow);
344 TEST(stmt.ColumnInt(0) == 4);
347 _LIT(KInsertSql5, "INSERT INTO A(Id, F4) VALUES(5, 'ab%cd')");
348 err = TheDb.Exec(KInsertSql5);
351 err = stmt.Prepare(TheDb, _L("SELECT Id FROM A WHERE F4 LIKE 'ab/%cd' ESCAPE '/'"));
352 TEST2(err, KErrNone);
354 TEST2(err, KSqlAtRow);
355 TEST(stmt.ColumnInt(0) == 5);
358 err = stmt.Prepare(TheDb, _L8("SELECT Id FROM A WHERE F4 LIKE 'ab/%cd' ESCAPE '/'"));
359 TEST2(err, KErrNone);
361 TEST2(err, KSqlAtRow);
362 TEST(stmt.ColumnInt(0) == 5);
365 //lower() test --------------------------------------------------------
366 err = stmt.Prepare(TheDb, _L("SELECT LOWER(F3) FROM A WHERE F3 = 'ABCD'"));
367 TEST2(err, KErrNone);
369 TEST2(err, KSqlAtRow);
370 err = stmt.ColumnText(0, colVal);
371 TEST2(err, KErrNone);
372 _LIT(KTextVal3, "abcd");
373 TEST(colVal == KTextVal3);
376 //max() test --------------------------------------------------------
377 err = stmt.Prepare(TheDb, _L("SELECT MAX(F1) AS M FROM A"));
378 TEST2(err, KErrNone);
380 TEST2(err, KSqlAtRow);
381 TEST(stmt.ColumnInt(0) == 100);
384 //min() test --------------------------------------------------------
385 err = stmt.Prepare(TheDb, _L("SELECT MIN(F1) AS M FROM A"));
386 TEST2(err, KErrNone);
388 TEST2(err, KSqlAtRow);
389 TEST(stmt.ColumnInt(0) == 0);
392 //nullif() test --------------------------------------------------------
393 err = stmt.Prepare(TheDb, _L("SELECT NULLIF(3, 4) AS M"));
394 TEST2(err, KErrNone);
396 TEST2(err, KSqlAtRow);
397 TEST(stmt.ColumnInt(0) == 3);
400 err = stmt.Prepare(TheDb, _L("SELECT NULLIF(4, 4) AS M"));
401 TEST2(err, KErrNone);
403 TEST2(err, KSqlAtRow);
404 TEST(stmt.IsNull(0));
407 //quote() test --------------------------------------------------------
408 err = stmt.Prepare(TheDb, _L("SELECT QUOTE(F4) AS M FROM A WHERE Id = 5"));
409 TEST2(err, KErrNone);
411 TEST2(err, KSqlAtRow);
412 err = stmt.ColumnText(0, colVal);
413 TEST2(err, KErrNone);
414 _LIT(KTextVal4, "'ab%cd'");
415 TEST(colVal == KTextVal4);
418 //random() test --------------------------------------------------------
419 err = stmt.Prepare(TheDb, _L("SELECT * FROM A WHERE Id > RANDOM(*)"));
420 TEST2(err, KErrNone);
423 //round() test --------------------------------------------------------
424 err = stmt.Prepare(TheDb, _L("SELECT ROUND(5.4) AS D"));
425 TEST2(err, KErrNone);
427 TEST2(err, KSqlAtRow);
428 TEST(Abs(stmt.ColumnReal(0) - 5.0) < 0.000001);
431 err = stmt.Prepare(TheDb, _L("SELECT ROUND(5.4321, 2) AS D"));
432 TEST2(err, KErrNone);
434 TEST2(err, KSqlAtRow);
435 TEST(Abs(stmt.ColumnReal(0) - 5.43) < 0.000001);
438 //sqlite_version() test --------------------------------------------------------
439 err = stmt.Prepare(TheDb, _L("SELECT sqlite_version(*) AS V"));
440 TEST2(err, KErrNone);
442 TEST2(err, KSqlAtRow);
443 err = stmt.ColumnText(0, colVal);
444 TEST2(err, KErrNone);
445 RDebug::Print(_L("Database engine version: \"%S\"\r\n"), &colVal);
448 //substr() test --------------------------------------------------------
449 err = stmt.Prepare(TheDb, _L("SELECT SUBSTR('abcd', 2, 2) AS S"));
450 TEST2(err, KErrNone);
452 TEST2(err, KSqlAtRow);
453 err = stmt.ColumnText(0, colVal);
454 TEST2(err, KErrNone);
455 _LIT(KTextVal7, "bc");
456 TEST(colVal == KTextVal7);
459 err = stmt.Prepare(TheDb, _L("SELECT SUBSTR('abcd', -3, 2) AS S"));
460 TEST2(err, KErrNone);
462 TEST2(err, KSqlAtRow);
463 err = stmt.ColumnText(0, colVal);
464 TEST2(err, KErrNone);
465 TEST(colVal == KTextVal7);
468 //typeof() test --------------------------------------------------------
469 _LIT(KInsertSql6, "INSERT INTO A(Id, F2) VALUES(6, 2)");
470 err = TheDb.Exec(KInsertSql6);
473 err = stmt.Prepare(TheDb, _L("SELECT TYPEOF(F1 + F2) AS T FROM A WHERE ID > 4"));
474 TEST2(err, KErrNone);
477 TEST2(err, KSqlAtRow);
478 err = stmt.ColumnText(0, colVal);
479 TEST2(err, KErrNone);
480 _LIT(KTextVal8, "null");
481 TEST(colVal == KTextVal8);
484 TEST2(err, KSqlAtRow);
485 err = stmt.ColumnText(0, colVal);
486 TEST2(err, KErrNone);
487 _LIT(KTextVal9, "integer");
488 TEST(colVal == KTextVal9);
492 //upper() test --------------------------------------------------------
493 err = stmt.Prepare(TheDb, _L("SELECT UPPER('ghjk') AS U"));
494 TEST2(err, KErrNone);
496 TEST2(err, KSqlAtRow);
497 err = stmt.ColumnText(0, colVal);
498 TEST2(err, KErrNone);
499 _LIT(KTextVal10, "GHJK");
500 TEST(colVal == KTextVal10);
503 //avg() test --------------------------------------------------------
504 err = stmt.Prepare(TheDb, _L("SELECT AVG(F2) AS F FROM A"));
505 TEST2(err, KErrNone);
507 TEST2(err, KSqlAtRow);
508 TEST(Abs(stmt.ColumnReal(0) - 2) < 0.000001);
511 //count() test --------------------------------------------------------
512 err = stmt.Prepare(TheDb, _L("SELECT COUNT(F2) AS F FROM A"));
513 TEST2(err, KErrNone);
515 TEST2(err, KSqlAtRow);
516 TEST(stmt.ColumnInt(0) == 1);
519 err = stmt.Prepare(TheDb, _L("SELECT COUNT(*) AS F FROM A"));
520 TEST2(err, KErrNone);
522 TEST2(err, KSqlAtRow);
523 TEST(stmt.ColumnInt(0) == 6);
526 //sum() test --------------------------------------------------------
527 err = stmt.Prepare(TheDb, _L("SELECT SUM(F2) AS S FROM A"));
528 TEST2(err, KErrNone);
530 TEST2(err, KSqlAtRow);
531 TEST(Abs(stmt.ColumnReal(0) - 2) < 0.000001);
534 // ----------------------------------
536 (void)RSqlDatabase::Delete(KTestDbName);
540 @SYMTestCaseID SYSLIB-SQL-CT-1632
541 @SYMTestCaseDesc "ATTACH DATABASE" test.
542 The test creates two databases. The main database has
543 Account(Id Integer, PersonId Integer, Value Integer) table.
544 The attached database has Person(Id Integer, Name TEXT) table.
545 The test inserts some records with a valid relation between them in both tables.
546 Then the test opens tha main database and attaches the second database to the first one.
547 The test prepares and executes SQL statement which retrieves column values from both
548 Account and Person tables. The test checks the column values.
549 @SYMTestPriority High
550 @SYMTestActions "ATTACH DATABASE" test.
551 @SYMTestExpectedResults Test must not fail
555 void AttachDatabaseTest()
557 // ------------------------------------------------------------
559 TInt err = db2.Create(KTestDbName2);
560 TEST2(err, KErrNone);
562 err = db2.Exec(_L("CREATE TABLE Person(Id Integer, Name TEXT)"));
565 err = db2.Exec(_L("INSERT INTO Person(Id, Name) VALUES(1, 'A')"));
568 err = db2.Exec(_L("INSERT INTO Person(Id, Name) VALUES(2, 'B')"));
571 err = db2.Exec(_L("INSERT INTO Person(Id, Name) VALUES(3, 'C')"));
576 // ------------------------------------------------------------
577 (void)RSqlDatabase::Delete(KTestDbName);
578 err = TheDb.Create(KTestDbName);
579 TEST2(err, KErrNone);
581 err = TheDb.Exec(_L("CREATE TABLE Account(Id Integer, PersonId Integer, Value Integer)"));
584 err = TheDb.Exec(_L("INSERT INTO ACCOUNT(Id, PersonId, Value) VALUES(1, 2, 20)"));
587 err = TheDb.Exec(_L("INSERT INTO ACCOUNT(Id, PersonId, Value) VALUES(2, 1, 10)"));
590 err = TheDb.Exec(_L("INSERT INTO ACCOUNT(Id, PersonId, Value) VALUES(3, 2, 25)"));
593 err = TheDb.Exec(_L("INSERT INTO ACCOUNT(Id, PersonId, Value) VALUES(4, 3, 30)"));
598 // ------------------------------------------------------------
599 err = TheDb.Open(KTestDbName);
600 TEST2(err, KErrNone);
603 sql.Copy(_L("ATTACH DATABASE '"));
604 sql.Append(KTestDbName2);
605 sql.Append(_L("' AS DB2"));
606 err = TheDb.Exec(sql);
610 err = stmt.Prepare(TheDb, _L("SELECT Account.Value, DB2.Person.Name FROM Account, DB2.Person \
611 WHERE Account.PersonId = DB2.Person.Id"));
612 TEST2(err, KErrNone);
620 TEST2(err, KSqlAtRow);
621 err = stmt.ColumnText(1, personName);
622 TEST2(err, KErrNone);
623 TEST(personName == KName2);
624 TEST(stmt.ColumnInt(0) == 20);
627 TEST2(err, KSqlAtRow);
628 err = stmt.ColumnText(1, personName);
629 TEST2(err, KErrNone);
630 TEST(personName == KName1);
631 TEST(stmt.ColumnInt(0) == 10);
634 TEST2(err, KSqlAtRow);
635 err = stmt.ColumnText(1, personName);
636 TEST2(err, KErrNone);
637 TEST(personName == KName2);
638 TEST(stmt.ColumnInt(0) == 25);
641 TEST2(err, KSqlAtRow);
642 err = stmt.ColumnText(1, personName);
643 TEST2(err, KErrNone);
644 TEST(personName == KName3);
645 TEST(stmt.ColumnInt(0) == 30);
649 err = TheDb.Exec(_L("DETACH DATABASE DB2"));
652 // ------------------------------------------------------------
654 (void)RSqlDatabase::Delete(KTestDbName2);
655 (void)RSqlDatabase::Delete(KTestDbName);
659 @SYMTestCaseID SYSLIB-SQL-UT-3502
660 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
661 The test creates a test table with INT32, INT64, DOUBLE, TEXT, and BINARY columns
662 and inserts couple of records there. Then the test prepares a SELECT statement,
663 retrieves all table rows, and to retrieve each column value, calls RSqlStatement::ColumnInt().
664 The column values are carefully chosen, so some of the ColumnInt() calls have to round
665 or clamp the returned value.
666 Summary: the test checks the ColumnInt() behaviour when the column value type is not INT32 and
667 when the column value is too big or too small and cannot fit in 32 bits.
668 @SYMTestPriority High
669 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
670 @SYMTestExpectedResults Test must not fail
675 (void)RSqlDatabase::Delete(KTestDbName);
676 TInt err = TheDb.Create(KTestDbName);
677 TEST2(err, KErrNone);
678 err = TheDb.Exec(_L("CREATE TABLE A(I32 INTEGER,I64 INTEGER,D DOUBLE,T TEXT,B BINARY)"));
681 err = TheDb.Exec(_L("INSERT INTO A VALUES(-5,-5000000000,-10000000000.0,'AAA',x'1122FF')"));
683 err = TheDb.Exec(_L("INSERT INTO A VALUES(5,5000000000,10000000000.0,NULL,NULL)"));
685 err = TheDb.Exec(_L("INSERT INTO A VALUES(5,2000000000,1000000000.0,NULL,NULL)"));
687 err = TheDb.Exec(_L("INSERT INTO A VALUES(NULL,NULL,1.1234567890123456E+317,NULL,NULL)"));
691 err = stmt.Prepare(TheDb, _L("SELECT I32,I64,D,T,B FROM A"));
692 TEST2(err, KErrNone);
695 TEST2(err, KSqlAtRow);
696 TInt val = stmt.ColumnInt(0);
697 TEST2(val, -5); //"-5" is a 32-bit integer value
698 val = stmt.ColumnInt(1);
699 TEST2(val, KMinTInt); //"-5000000000" is a 64-bit integer, will be clamped to KMinTInt
700 val = stmt.ColumnInt(2);
701 TEST2(val, KMinTInt); //"-10000000000.0" is a 64-bit double, will be rounded to the nearest 32-bit integer
702 val = stmt.ColumnInt(3);
703 TEST2(val, 0); //"AAA" is a text string, cannot be converted to a 32-bit integer
704 val = stmt.ColumnInt(4);
705 TEST2(val, 0); //"1122FF" is a hex binary, cannot be converted to a 32-bit integer
708 TEST2(err, KSqlAtRow);
709 val = stmt.ColumnInt(0);
710 TEST2(val, 5); //"5" is a 32-bit integer value
711 val = stmt.ColumnInt(1);
712 TEST2(val, KMaxTInt); //"5000000000" is a 64-bit integer, will be clamped to KMaxTInt
713 val = stmt.ColumnInt(2);
714 TEST2(val, KMaxTInt); //"10000000000.0" is a 64-bit double, will be rounded to the nearest 32-bit integer
715 val = stmt.ColumnInt(3);
716 TEST2(val, 0); //NULL column value
717 val = stmt.ColumnInt(4);
718 TEST2(val, 0); //NULL column value
721 TEST2(err, KSqlAtRow);
722 val = stmt.ColumnInt(0);
724 val = stmt.ColumnInt(1);
725 TEST2(val, 2000000000);
726 val = stmt.ColumnInt(2);
727 TEST2(val, 1000000000);
730 TEST2(err, KSqlAtRow);
731 val = stmt.ColumnInt(2);
732 TEST2(val, KMinTInt); //"1.1234567890123456E+317" is too big and cannot fit in a 64-bit double
737 err = RSqlDatabase::Delete(KTestDbName);
738 TEST2(err, KErrNone);
742 @SYMTestCaseID SYSLIB-SQL-UT-3503
743 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
744 The test creates a test table with INT32, INT64, DOUBLE, TEXT, and BINARY columns
745 and inserts couple of records there. Then the test prepares a SELECT statement,
746 retrieves all table rows, and to retrieve each column value, calls RSqlStatement::ColumnInt64().
747 The column values are carefully chosen, so some of the ColumnInt64() calls have to round
748 or clamp the returned value.
749 Summary: the test checks the ColumnInt64() behaviour when the column value type is not INT64 and
750 when the column value is too big or too small and cannot be presented as 64-bits integer.
751 @SYMTestPriority High
752 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
753 @SYMTestExpectedResults Test must not fail
756 void ColumnInt64Test()
758 (void)RSqlDatabase::Delete(KTestDbName);
759 TInt err = TheDb.Create(KTestDbName);
760 TEST2(err, KErrNone);
761 err = TheDb.Exec(_L("CREATE TABLE A(I32 INTEGER,I64 INTEGER,D DOUBLE,T TEXT,B BINARY)"));
764 err = TheDb.Exec(_L("INSERT INTO A VALUES(-5,-5000000000,-10000000000.0,'AAA',x'1122FF')"));
766 err = TheDb.Exec(_L("INSERT INTO A VALUES(5,5000000000,10000000000.0,NULL,NULL)"));
768 err = TheDb.Exec(_L("INSERT INTO A VALUES(5,2000000000,1000000000.0,NULL,NULL)"));
770 err = TheDb.Exec(_L("INSERT INTO A VALUES(NULL,NULL,1.1234567890123456E+317,NULL,NULL)"));
774 err = stmt.Prepare(TheDb, _L("SELECT I32,I64,D,T,B FROM A"));
775 TEST2(err, KErrNone);
778 TEST2(err, KSqlAtRow);
779 TInt64 val = stmt.ColumnInt64(0);
780 TEST3(val, -5); //"-5" is a 32-bit integer value
781 val = stmt.ColumnInt64(1);
782 TEST3(val, -5000000000LL); //"-5000000000" is a 64-bit integer
783 val = stmt.ColumnInt64(2);
784 TEST3(val, -10000000000LL); //"-10000000000.0" is a 64-bit double, will be rounded to the nearest 64-bit integer
785 val = stmt.ColumnInt64(3);
786 TEST3(val, 0); //"AAA" is a text string, cannot be converted to a 64-bit integer
787 val = stmt.ColumnInt64(4);
788 TEST3(val, 0); //"1122FF" is a hex binary, cannot be converted to a 64-bit integer
791 TEST2(err, KSqlAtRow);
792 val = stmt.ColumnInt64(0);
793 TEST3(val, 5); //"5" is a 32-bit integer value
794 val = stmt.ColumnInt64(1);
795 TEST3(val, 5000000000LL); //"5000000000" is a 64-bit integer
796 val = stmt.ColumnInt64(2);
797 TEST3(val, 10000000000LL); //"10000000000.0" is a 64-bit double, will be rounded to the nearest 64-bit integer
798 val = stmt.ColumnInt64(3);
799 TEST3(val, 0); //NULL column value
800 val = stmt.ColumnInt64(4);
801 TEST3(val, 0); //NULL column value
804 TEST2(err, KSqlAtRow);
805 val = stmt.ColumnInt64(0);
807 val = stmt.ColumnInt64(1);
808 TEST3(val, 2000000000);
809 val = stmt.ColumnInt64(2);
810 TEST3(val, 1000000000);
813 TEST2(err, KSqlAtRow);
814 val = stmt.ColumnInt64(2);
815 TEST3(val, KMinTInt64); //"1.1234567890123456E+317" is too big and cannot fit in a 64-bit double
820 err = RSqlDatabase::Delete(KTestDbName);
821 TEST2(err, KErrNone);
825 @SYMTestCaseID SYSLIB-SQL-UT-3504
826 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
827 The test creates a test table with INT32, INT64, DOUBLE, TEXT, and BINARY columns
828 and inserts couple of records there. Then the test prepares a SELECT statement,
829 retrieves all table rows, and to retrieve each column value, calls RSqlStatement::ColumnReal().
830 Summary: the test checks the ColumnReal() behaviour when the column value type is not DOUBLE and
831 when the column value is too big or too small and cannot be presented as 64-bits double.
832 @SYMTestPriority High
833 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
834 @SYMTestExpectedResults Test must not fail
837 void ColumnRealTest()
839 (void)RSqlDatabase::Delete(KTestDbName);
840 TInt err = TheDb.Create(KTestDbName);
841 TEST2(err, KErrNone);
842 err = TheDb.Exec(_L("CREATE TABLE A(I32 INTEGER,I64 INTEGER,D DOUBLE,T TEXT,B BINARY)"));
845 err = TheDb.Exec(_L("INSERT INTO A VALUES(-5,-5000000000,-10000000000.0,'AAA',x'1122FF')"));
847 err = TheDb.Exec(_L("INSERT INTO A VALUES(5,5000000000,10000000000.0,NULL,NULL)"));
849 err = TheDb.Exec(_L("INSERT INTO A VALUES(5,2000000000,1000000000.0,NULL,NULL)"));
851 err = TheDb.Exec(_L("INSERT INTO A VALUES(NULL,NULL,1.1234567890123456E+317,NULL,NULL)"));
854 const TReal KEpsilon = 0.000001;
857 err = stmt.Prepare(TheDb, _L("SELECT I32,I64,D,T,B FROM A"));
858 TEST2(err, KErrNone);
861 TEST2(err, KSqlAtRow);
862 TReal val = stmt.ColumnReal(0);
863 TEST(Abs(val - (-5)) < KEpsilon);
864 val = stmt.ColumnReal(1);
865 TEST(Abs(val - (-5000000000LL)) < KEpsilon);
866 val = stmt.ColumnReal(2);
867 TEST(Abs(val - (-10000000000LL)) < KEpsilon);
868 val = stmt.ColumnReal(3);
869 TEST(Abs(val) < 0.0001); //"AAA" is a text string, cannot be converted to a 64-bit double
870 val = stmt.ColumnReal(4);
871 TEST(Abs(val) < 0.0001); //"1122FF" is a hex binary, cannot be converted to a 64-bit double
874 TEST2(err, KSqlAtRow);
875 val = stmt.ColumnReal(0);
876 TEST(Abs(val - 5) < KEpsilon);
877 val = stmt.ColumnReal(1);
878 TEST(Abs(val - 5000000000LL) < KEpsilon);
879 val = stmt.ColumnReal(2);
880 TEST(Abs(val - 10000000000LL) < KEpsilon);
881 val = stmt.ColumnReal(3);
882 TEST(Abs(val) < KEpsilon); //NULL column value
883 val = stmt.ColumnReal(4);
884 TEST(Abs(val) < KEpsilon); //NULL column value
887 TEST2(err, KSqlAtRow);
888 val = stmt.ColumnReal(0);
889 TEST(Abs(val - 5) < KEpsilon);
890 val = stmt.ColumnReal(1);
891 TEST(Abs(val - 2000000000) < KEpsilon);
892 val = stmt.ColumnReal(2);
893 TEST(Abs(val - 1000000000) < KEpsilon);
896 TEST2(err, KSqlAtRow);
897 val = stmt.ColumnReal(2);
898 TEST(Math::IsInfinite(val)); //"1.1234567890123456E+317" is too big and cannot fit in a 64-bit double
903 err = RSqlDatabase::Delete(KTestDbName);
904 TEST2(err, KErrNone);
908 @SYMTestCaseID SYSLIB-SQL-UT-3505
909 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
910 The test creates a test table with INT32, INT64, DOUBLE, TEXT, and BINARY columns
911 and inserts a record there. Then the test prepares a SELECT statement,
912 retrieves all table rows, and to retrieve each column value, calls RSqlStatement::ColumnText().
913 Summary: the test checks the ColumnText() behaviour when the column value type is not TEXT.
914 (In all non-TEXT cases the ,ethod is expected to return KNullDesC8)
915 @SYMTestPriority High
916 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
917 @SYMTestExpectedResults Test must not fail
920 void ColumnTextTest()
922 (void)RSqlDatabase::Delete(KTestDbName);
923 TInt err = TheDb.Create(KTestDbName);
924 TEST2(err, KErrNone);
925 err = TheDb.Exec(_L("CREATE TABLE A(I32 INTEGER,I64 INTEGER,D DOUBLE,T TEXT,B BINARY)"));
928 err = TheDb.Exec(_L("INSERT INTO A VALUES(-5,-5000000000,-10000000000.0,'AAA',x'1122FF')"));
932 err = stmt.Prepare(TheDb, _L("SELECT I32,I64,D,T,B FROM A"));
933 TEST2(err, KErrNone);
936 TEST2(err, KSqlAtRow);
938 err = stmt.ColumnText(0, val);
939 TEST2(err, KErrNone);
940 TEST(val == KNullDesC);
941 err = stmt.ColumnText(1, val);
942 TEST2(err, KErrNone);
943 TEST(val == KNullDesC);
944 err = stmt.ColumnText(2, val);
945 TEST2(err, KErrNone);
946 TEST(val == KNullDesC);
947 err = stmt.ColumnText(3, val);
948 TEST2(err, KErrNone);
949 TEST2(val.Length(), 3);
950 TEST(val == _L("AAA"));
951 err = stmt.ColumnText(4, val);
952 TEST2(err, KErrNone);
953 TEST(val == KNullDesC);
958 err = RSqlDatabase::Delete(KTestDbName);
959 TEST2(err, KErrNone);
963 @SYMTestCaseID SYSLIB-SQL-UT-3506
964 @SYMTestCaseDesc Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
965 The test creates a test table with INT32, INT64, DOUBLE, TEXT, and BINARY columns
966 and inserts a record there. Then the test prepares a SELECT statement,
967 retrieves all table rows, and to retrieve each column value, calls RSqlStatement::ColumnBinary().
968 Summary: the test checks the ColumnBinary() behaviour when the column value type is not BINARY.
969 (In all non-BINARY cases the method is expected to return KNullDesC8)
970 @SYMTestPriority High
971 @SYMTestActions Test for DEF109100: SQL, code coverage for TSqlBufRIterator is very low.
972 @SYMTestExpectedResults Test must not fail
975 void ColumnBinaryTest()
977 (void)RSqlDatabase::Delete(KTestDbName);
978 TInt err = TheDb.Create(KTestDbName);
979 TEST2(err, KErrNone);
980 err = TheDb.Exec(_L("CREATE TABLE A(I32 INTEGER,I64 INTEGER,D DOUBLE,T TEXT,B BINARY)"));
983 err = TheDb.Exec(_L("INSERT INTO A VALUES(-5,-5000000000,-10000000000.0,'AAA',x'1122FF')"));
987 err = stmt.Prepare(TheDb, _L("SELECT I32,I64,D,T,B FROM A"));
988 TEST2(err, KErrNone);
991 TEST2(err, KSqlAtRow);
993 err = stmt.ColumnBinary(0, val);
994 TEST2(err, KErrNone);
995 TEST(val == KNullDesC8);
996 err = stmt.ColumnBinary(1, val);
997 TEST2(err, KErrNone);
998 TEST(val == KNullDesC8);
999 err = stmt.ColumnBinary(2, val);
1000 TEST2(err, KErrNone);
1001 TEST(val == KNullDesC8);
1002 err = stmt.ColumnBinary(3, val);
1003 TEST2(err, KErrNone);
1004 TEST(val == KNullDesC8);
1005 err = stmt.ColumnBinary(4, val);
1006 TEST2(err, KErrNone);
1007 TEST2(val.Length(), 3);
1008 TEST(val[0] == 0x11);
1009 TEST(val[1] == 0x22);
1010 TEST(val[2] == 0xFF);
1015 err = RSqlDatabase::Delete(KTestDbName);
1016 TEST2(err, KErrNone);
1021 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1611 E011 numeric data types test "));
1022 NumericDataTypesTest();
1024 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1630 Built-in functions test "));
1025 BuiltInFunctionsTest();
1027 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-CT-1632 \"Attach database\" test "));
1028 AttachDatabaseTest();
1030 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3502 \"RSqlStatement::ColumnInt()\" test "));
1033 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3503 \"RSqlStatement::ColumnInt64()\" test "));
1036 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3504 \"RSqlStatement::ColumnReal()\" test "));
1039 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3505 \"RSqlStatement::ColumnText()\" test "));
1042 TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-3506 \"RSqlStatement::ColumnBinary()\" test "));
1050 CTrapCleanup* tc = CTrapCleanup::New();
1056 TRAPD(err, DoTestsL());
1058 TEST2(err, KErrNone);
1067 User::Heap().Check();