msi: Ref count temporary columns and release them when necessary.
[wine.git] / dlls / msi / tests / db.c
blob58c3f221ef6823f335fe37e151b4736ef591386e
1 /*
2 * Copyright (C) 2005 Mike McCormack for CodeWeavers
4 * A test program for MSI database files.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdio.h>
25 #include <windows.h>
26 #include <msi.h>
27 #include <msiquery.h>
29 #include <objidl.h>
31 #include "wine/test.h"
33 static const char *msifile = "winetest.msi";
34 static const char *msifile2 = "winetst2.msi";
35 static const char *mstfile = "winetst.mst";
37 static void test_msidatabase(void)
39 MSIHANDLE hdb = 0, hdb2 = 0;
40 UINT res;
42 DeleteFile(msifile);
44 res = MsiOpenDatabase( msifile, msifile2, &hdb );
45 ok( res == ERROR_OPEN_FAILED, "expected failure\n");
47 res = MsiOpenDatabase( msifile, (LPSTR) 0xff, &hdb );
48 ok( res == ERROR_INVALID_PARAMETER, "expected failure\n");
50 res = MsiCloseHandle( hdb );
51 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
53 /* create an empty database */
54 res = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb );
55 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
57 res = MsiDatabaseCommit( hdb );
58 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
60 ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes( msifile ), "database should exist\n");
62 res = MsiCloseHandle( hdb );
63 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
64 res = MsiOpenDatabase( msifile, msifile2, &hdb2 );
65 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
67 ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes( msifile2 ), "database should exist\n");
69 res = MsiDatabaseCommit( hdb2 );
70 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
72 res = MsiCloseHandle( hdb2 );
73 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
75 res = MsiOpenDatabase( msifile, msifile2, &hdb2 );
76 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
78 res = MsiCloseHandle( hdb2 );
79 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
81 ok( INVALID_FILE_ATTRIBUTES == GetFileAttributes( msifile2 ), "uncommitted database should not exist\n");
83 res = MsiOpenDatabase( msifile, msifile2, &hdb2 );
84 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
86 res = MsiDatabaseCommit( hdb2 );
87 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
89 res = MsiCloseHandle( hdb2 );
90 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
92 ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes( msifile2 ), "committed database should exist\n");
94 res = MsiOpenDatabase( msifile, MSIDBOPEN_READONLY, &hdb );
95 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
97 res = MsiCloseHandle( hdb );
98 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
100 res = MsiOpenDatabase( msifile, MSIDBOPEN_DIRECT, &hdb );
101 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
103 res = MsiCloseHandle( hdb );
104 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
106 res = MsiOpenDatabase( msifile, MSIDBOPEN_TRANSACT, &hdb );
107 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
109 res = MsiCloseHandle( hdb );
110 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
111 ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes( msifile ), "database should exist\n");
113 /* MSIDBOPEN_CREATE deletes the database if MsiCommitDatabase isn't called */
114 res = MsiOpenDatabase( msifile, MSIDBOPEN_CREATE, &hdb );
115 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
117 ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes( msifile ), "database should exist\n");
119 res = MsiCloseHandle( hdb );
120 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
122 ok( INVALID_FILE_ATTRIBUTES == GetFileAttributes( msifile ), "database should exist\n");
124 res = MsiOpenDatabase( msifile, MSIDBOPEN_CREATE, &hdb );
125 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
127 res = MsiDatabaseCommit( hdb );
128 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
130 ok( INVALID_FILE_ATTRIBUTES != GetFileAttributes( msifile ), "database should exist\n");
132 res = MsiCloseHandle( hdb );
133 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
135 res = DeleteFile( msifile2 );
136 ok( res == TRUE, "Failed to delete database\n" );
138 res = DeleteFile( msifile );
139 ok( res == TRUE, "Failed to delete database\n" );
142 static UINT do_query(MSIHANDLE hdb, const char *query, MSIHANDLE *phrec)
144 MSIHANDLE hview = 0;
145 UINT r, ret;
147 /* open a select query */
148 r = MsiDatabaseOpenView(hdb, query, &hview);
149 if (r != ERROR_SUCCESS)
150 return r;
151 r = MsiViewExecute(hview, 0);
152 if (r != ERROR_SUCCESS)
153 return r;
154 ret = MsiViewFetch(hview, phrec);
155 r = MsiViewClose(hview);
156 if (r != ERROR_SUCCESS)
157 return r;
158 r = MsiCloseHandle(hview);
159 if (r != ERROR_SUCCESS)
160 return r;
161 return ret;
164 static UINT run_query( MSIHANDLE hdb, MSIHANDLE hrec, const char *query )
166 MSIHANDLE hview = 0;
167 UINT r;
169 r = MsiDatabaseOpenView(hdb, query, &hview);
170 if( r != ERROR_SUCCESS )
171 return r;
173 r = MsiViewExecute(hview, hrec);
174 if( r == ERROR_SUCCESS )
175 r = MsiViewClose(hview);
176 MsiCloseHandle(hview);
177 return r;
180 static UINT create_component_table( MSIHANDLE hdb )
182 return run_query( hdb, 0,
183 "CREATE TABLE `Component` ( "
184 "`Component` CHAR(72) NOT NULL, "
185 "`ComponentId` CHAR(38), "
186 "`Directory_` CHAR(72) NOT NULL, "
187 "`Attributes` SHORT NOT NULL, "
188 "`Condition` CHAR(255), "
189 "`KeyPath` CHAR(72) "
190 "PRIMARY KEY `Component`)" );
193 static UINT create_feature_components_table( MSIHANDLE hdb )
195 return run_query( hdb, 0,
196 "CREATE TABLE `FeatureComponents` ( "
197 "`Feature_` CHAR(38) NOT NULL, "
198 "`Component_` CHAR(72) NOT NULL "
199 "PRIMARY KEY `Feature_`, `Component_` )" );
202 static UINT create_std_dlls_table( MSIHANDLE hdb )
204 return run_query( hdb, 0,
205 "CREATE TABLE `StdDlls` ( "
206 "`File` CHAR(255) NOT NULL, "
207 "`Binary_` CHAR(72) NOT NULL "
208 "PRIMARY KEY `File` )" );
211 static UINT create_binary_table( MSIHANDLE hdb )
213 return run_query( hdb, 0,
214 "CREATE TABLE `Binary` ( "
215 "`Name` CHAR(72) NOT NULL, "
216 "`Data` CHAR(72) NOT NULL "
217 "PRIMARY KEY `Name` )" );
220 static UINT add_component_entry( MSIHANDLE hdb, const char *values )
222 char insert[] = "INSERT INTO `Component` "
223 "(`Component`, `ComponentId`, `Directory_`, `Attributes`, `Condition`, `KeyPath`) "
224 "VALUES( %s )";
225 char *query;
226 UINT sz, r;
228 sz = strlen(values) + sizeof insert;
229 query = HeapAlloc(GetProcessHeap(),0,sz);
230 sprintf(query,insert,values);
231 r = run_query( hdb, 0, query );
232 HeapFree(GetProcessHeap(), 0, query);
233 return r;
236 static UINT add_feature_components_entry( MSIHANDLE hdb, const char *values )
238 char insert[] = "INSERT INTO `FeatureComponents` "
239 "(`Feature_`, `Component_`) "
240 "VALUES( %s )";
241 char *query;
242 UINT sz, r;
244 sz = strlen(values) + sizeof insert;
245 query = HeapAlloc(GetProcessHeap(),0,sz);
246 sprintf(query,insert,values);
247 r = run_query( hdb, 0, query );
248 HeapFree(GetProcessHeap(), 0, query);
249 return r;
252 static UINT add_std_dlls_entry( MSIHANDLE hdb, const char *values )
254 char insert[] = "INSERT INTO `StdDlls` "
255 "(`File`, `Binary_`) "
256 "VALUES( %s )";
257 char *query;
258 UINT sz, r;
260 sz = strlen(values) + sizeof insert;
261 query = HeapAlloc(GetProcessHeap(),0,sz);
262 sprintf(query,insert,values);
263 r = run_query( hdb, 0, query );
264 HeapFree(GetProcessHeap(), 0, query);
265 return r;
268 static UINT add_binary_entry( MSIHANDLE hdb, const char *values )
270 char insert[] = "INSERT INTO `Binary` "
271 "(`Name`, `Data`) "
272 "VALUES( %s )";
273 char *query;
274 UINT sz, r;
276 sz = strlen(values) + sizeof insert;
277 query = HeapAlloc(GetProcessHeap(),0,sz);
278 sprintf(query,insert,values);
279 r = run_query( hdb, 0, query );
280 HeapFree(GetProcessHeap(), 0, query);
281 return r;
284 static void test_msiinsert(void)
286 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
287 UINT r;
288 const char *query;
289 char buf[80];
290 DWORD sz;
292 DeleteFile(msifile);
294 /* just MsiOpenDatabase should not create a file */
295 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
296 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
298 /* create a table */
299 query = "CREATE TABLE `phone` ( "
300 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
301 "PRIMARY KEY `id`)";
302 r = MsiDatabaseOpenView(hdb, query, &hview);
303 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
304 r = MsiViewExecute(hview, 0);
305 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
306 r = MsiViewClose(hview);
307 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
308 r = MsiCloseHandle(hview);
309 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
311 /* insert a value into it */
312 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
313 "VALUES('1', 'Abe', '8675309')";
314 r = MsiDatabaseOpenView(hdb, query, &hview);
315 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
316 r = MsiViewExecute(hview, 0);
317 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
318 r = MsiViewClose(hview);
319 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
320 r = MsiCloseHandle(hview);
321 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
323 query = "SELECT * FROM `phone` WHERE `id` = 1";
324 r = do_query(hdb, query, &hrec);
325 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
327 /* check the record contains what we put in it */
328 r = MsiRecordGetFieldCount(hrec);
329 ok(r == 3, "record count wrong\n");
331 todo_wine {
332 r = MsiRecordIsNull(hrec, 0);
333 ok(r == FALSE, "field 0 not null\n");
336 r = MsiRecordGetInteger(hrec, 1);
337 ok(r == 1, "field 1 contents wrong\n");
338 sz = sizeof buf;
339 r = MsiRecordGetString(hrec, 2, buf, &sz);
340 ok(r == ERROR_SUCCESS, "field 2 content fetch failed\n");
341 ok(!strcmp(buf,"Abe"), "field 2 content incorrect\n");
342 sz = sizeof buf;
343 r = MsiRecordGetString(hrec, 3, buf, &sz);
344 ok(r == ERROR_SUCCESS, "field 3 content fetch failed\n");
345 ok(!strcmp(buf,"8675309"), "field 3 content incorrect\n");
347 r = MsiCloseHandle(hrec);
348 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
350 /* open a select query */
351 hrec = 100;
352 query = "SELECT * FROM `phone` WHERE `id` >= 10";
353 r = do_query(hdb, query, &hrec);
354 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
355 ok(hrec == 0, "hrec should be null\n");
357 r = MsiCloseHandle(hrec);
358 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
360 query = "SELECT * FROM `phone` WHERE `id` < 0";
361 r = do_query(hdb, query, &hrec);
362 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
364 query = "SELECT * FROM `phone` WHERE `id` <= 0";
365 r = do_query(hdb, query, &hrec);
366 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
368 query = "SELECT * FROM `phone` WHERE `id` <> 1";
369 r = do_query(hdb, query, &hrec);
370 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
372 query = "SELECT * FROM `phone` WHERE `id` > 10";
373 r = do_query(hdb, query, &hrec);
374 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
376 /* now try a few bad INSERT xqueries */
377 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
378 "VALUES(?, ?)";
379 r = MsiDatabaseOpenView(hdb, query, &hview);
380 ok(r == ERROR_BAD_QUERY_SYNTAX, "MsiDatabaseOpenView failed\n");
382 /* construct a record to insert */
383 hrec = MsiCreateRecord(4);
384 r = MsiRecordSetInteger(hrec, 1, 2);
385 ok(r == ERROR_SUCCESS, "MsiRecordSetInteger failed\n");
386 r = MsiRecordSetString(hrec, 2, "Adam");
387 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
388 r = MsiRecordSetString(hrec, 3, "96905305");
389 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
391 /* insert another value, using a record and wildcards */
392 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
393 "VALUES(?, ?, ?)";
394 r = MsiDatabaseOpenView(hdb, query, &hview);
395 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
397 if (r == ERROR_SUCCESS)
399 r = MsiViewExecute(hview, hrec);
400 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
401 r = MsiViewClose(hview);
402 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
403 r = MsiCloseHandle(hview);
404 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
406 r = MsiCloseHandle(hrec);
407 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
409 r = MsiViewFetch(0, NULL);
410 ok(r == ERROR_INVALID_PARAMETER, "MsiViewFetch failed\n");
412 r = MsiDatabaseCommit(hdb);
413 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
415 r = MsiCloseHandle(hdb);
416 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
418 r = DeleteFile(msifile);
419 ok(r == TRUE, "file didn't exist after commit\n");
422 typedef UINT (WINAPI *fnMsiDecomposeDescriptorA)(LPCSTR, LPCSTR, LPSTR, LPSTR, DWORD *);
423 static fnMsiDecomposeDescriptorA pMsiDecomposeDescriptorA;
425 static void test_msidecomposedesc(void)
427 char prod[MAX_FEATURE_CHARS+1], comp[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1];
428 const char *desc;
429 UINT r;
430 DWORD len;
431 HMODULE hmod;
433 hmod = GetModuleHandle("msi.dll");
434 pMsiDecomposeDescriptorA = (fnMsiDecomposeDescriptorA)
435 GetProcAddress(hmod, "MsiDecomposeDescriptorA");
436 if (!pMsiDecomposeDescriptorA)
437 return;
439 /* test a valid feature descriptor */
440 desc = "']gAVn-}f(ZXfeAR6.jiFollowTheWhiteRabbit>3w2x^IGfe?CxI5heAvk.";
441 len = 0;
442 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
443 ok(r == ERROR_SUCCESS, "returned an error\n");
444 ok(len == strlen(desc), "length was wrong\n");
445 ok(strcmp(prod,"{90110409-6000-11D3-8CFE-0150048383C9}")==0, "product wrong\n");
446 ok(strcmp(feature,"FollowTheWhiteRabbit")==0, "feature wrong\n");
447 ok(strcmp(comp,"{A7CD68DB-EF74-49C8-FBB2-A7C463B2AC24}")==0,"component wrong\n");
449 /* test an invalid feature descriptor with too many characters */
450 desc = "']gAVn-}f(ZXfeAR6.ji"
451 "ThisWillFailIfTheresMoreThanAGuidsChars>"
452 "3w2x^IGfe?CxI5heAvk.";
453 len = 0;
454 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
455 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
458 * Test a valid feature descriptor with the
459 * maximum number of characters and some trailing characters.
461 desc = "']gAVn-}f(ZXfeAR6.ji"
462 "ThisWillWorkIfTheresLTEThanAGuidsChars>"
463 "3w2x^IGfe?CxI5heAvk."
464 "extra";
465 len = 0;
466 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
467 ok(r == ERROR_SUCCESS, "returned wrong error\n");
468 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
470 len = 0;
471 r = pMsiDecomposeDescriptorA(desc, prod, feature, NULL, &len);
472 ok(r == ERROR_SUCCESS, "returned wrong error\n");
473 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
475 len = 0;
476 r = pMsiDecomposeDescriptorA(desc, prod, NULL, NULL, &len);
477 ok(r == ERROR_SUCCESS, "returned wrong error\n");
478 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
480 len = 0;
481 r = pMsiDecomposeDescriptorA(desc, NULL, NULL, NULL, &len);
482 ok(r == ERROR_SUCCESS, "returned wrong error\n");
483 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
485 len = 0;
486 r = pMsiDecomposeDescriptorA(NULL, NULL, NULL, NULL, &len);
487 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
488 ok(len == 0, "length wrong\n");
491 static UINT try_query_param( MSIHANDLE hdb, LPCSTR szQuery, MSIHANDLE hrec )
493 MSIHANDLE htab = 0;
494 UINT res;
496 res = MsiDatabaseOpenView( hdb, szQuery, &htab );
497 if(res == ERROR_SUCCESS )
499 UINT r;
501 r = MsiViewExecute( htab, hrec );
502 if(r != ERROR_SUCCESS )
503 res = r;
505 r = MsiViewClose( htab );
506 if(r != ERROR_SUCCESS )
507 res = r;
509 r = MsiCloseHandle( htab );
510 if(r != ERROR_SUCCESS )
511 res = r;
513 return res;
516 static UINT try_query( MSIHANDLE hdb, LPCSTR szQuery )
518 return try_query_param( hdb, szQuery, 0 );
521 static UINT try_insert_query( MSIHANDLE hdb, LPCSTR szQuery )
523 MSIHANDLE hrec = 0;
524 UINT r;
526 hrec = MsiCreateRecord( 1 );
527 MsiRecordSetString( hrec, 1, "Hello");
529 r = try_query_param( hdb, szQuery, hrec );
531 MsiCloseHandle( hrec );
532 return r;
535 static void test_msibadqueries(void)
537 MSIHANDLE hdb = 0;
538 UINT r;
540 DeleteFile(msifile);
542 /* just MsiOpenDatabase should not create a file */
543 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
544 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
546 r = MsiDatabaseCommit( hdb );
547 ok(r == ERROR_SUCCESS , "Failed to commit database\n");
549 r = MsiCloseHandle( hdb );
550 ok(r == ERROR_SUCCESS , "Failed to close database\n");
552 /* open it readonly */
553 r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb );
554 ok(r == ERROR_SUCCESS , "Failed to open database r/o\n");
556 /* add a table to it */
557 r = try_query( hdb, "select * from _Tables");
558 ok(r == ERROR_SUCCESS , "query 1 failed\n");
560 r = MsiCloseHandle( hdb );
561 ok(r == ERROR_SUCCESS , "Failed to close database r/o\n");
563 /* open it read/write */
564 r = MsiOpenDatabase(msifile, MSIDBOPEN_TRANSACT, &hdb );
565 ok(r == ERROR_SUCCESS , "Failed to open database r/w\n");
567 /* a bunch of test queries that fail with the native MSI */
569 r = try_query( hdb, "CREATE TABLE");
570 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2a return code\n");
572 r = try_query( hdb, "CREATE TABLE `a`");
573 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2b return code\n");
575 r = try_query( hdb, "CREATE TABLE `a` ()");
576 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2c return code\n");
578 r = try_query( hdb, "CREATE TABLE `a` (`b`)");
579 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2d return code\n");
581 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) )");
582 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2e return code\n");
584 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL)");
585 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2f return code\n");
587 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY)");
588 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2g return code\n");
590 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
591 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2h return code\n");
593 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
594 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2i return code\n");
596 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY 'b')");
597 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2j return code\n");
599 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
600 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2k return code\n");
602 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
603 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2l return code\n");
605 r = try_query( hdb, "CREATE TABLE `a` (`b` CHA(72) NOT NULL PRIMARY KEY `b`)");
606 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2m return code\n");
608 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(-1) NOT NULL PRIMARY KEY `b`)");
609 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2n return code\n");
611 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(720) NOT NULL PRIMARY KEY `b`)");
612 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2o return code\n");
614 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL KEY `b`)");
615 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
617 r = try_query( hdb, "CREATE TABLE `a` (`` CHAR(72) NOT NULL PRIMARY KEY `b`)");
618 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
620 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
621 ok(r == ERROR_SUCCESS , "valid query 2z failed\n");
623 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
624 ok(r == ERROR_BAD_QUERY_SYNTAX , "created same table again\n");
626 r = try_query( hdb, "CREATE TABLE `aa` (`b` CHAR(72) NOT NULL, `c` "
627 "CHAR(72), `d` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `b`)");
628 ok(r == ERROR_SUCCESS , "query 4 failed\n");
630 r = MsiDatabaseCommit( hdb );
631 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
633 r = try_query( hdb, "CREATE TABLE `blah` (`foo` CHAR(72) NOT NULL "
634 "PRIMARY KEY `foo`)");
635 ok(r == ERROR_SUCCESS , "query 4 failed\n");
637 r = try_insert_query( hdb, "insert into a ( `b` ) VALUES ( ? )");
638 ok(r == ERROR_SUCCESS , "failed to insert record in db\n");
640 r = MsiDatabaseCommit( hdb );
641 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
643 r = try_query( hdb, "CREATE TABLE `boo` (`foo` CHAR(72) NOT NULL "
644 "PRIMARY KEY `ba`)");
645 ok(r != ERROR_SUCCESS , "query 5 succeeded\n");
647 r = try_query( hdb,"CREATE TABLE `bee` (`foo` CHAR(72) NOT NULL )");
648 ok(r != ERROR_SUCCESS , "query 6 succeeded\n");
650 r = try_query( hdb, "CREATE TABLE `temp` (`t` CHAR(72) NOT NULL "
651 "PRIMARY KEY `t`)");
652 ok(r == ERROR_SUCCESS , "query 7 failed\n");
654 r = try_query( hdb, "CREATE TABLE `c` (`b` CHAR NOT NULL PRIMARY KEY `b`)");
655 ok(r == ERROR_SUCCESS , "query 8 failed\n");
657 r = try_query( hdb, "select * from c");
658 ok(r == ERROR_SUCCESS , "query failed\n");
660 r = try_query( hdb, "select * from c where b = 'x");
661 todo_wine ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
663 r = try_query( hdb, "select * from 'c'");
664 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
666 r = try_query( hdb, "select * from ''");
667 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
669 r = try_query( hdb, "select * from c where b = x");
670 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
672 r = try_query( hdb, "select * from c where b = \"x\"");
673 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
675 r = try_query( hdb, "select * from c where b = 'x'");
676 ok(r == ERROR_SUCCESS, "query failed\n");
678 r = try_query( hdb, "select * from c where b = '\"x'");
679 ok(r == ERROR_SUCCESS, "query failed\n");
681 if (0) /* FIXME: this query causes trouble with other tests */
683 r = try_query( hdb, "select * from c where b = '\\\'x'");
684 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
687 r = try_query( hdb, "select * from 'c'");
688 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
690 r = MsiCloseHandle( hdb );
691 ok(r == ERROR_SUCCESS , "Failed to close database transact\n");
693 r = DeleteFile( msifile );
694 ok(r == TRUE, "file didn't exist after commit\n");
697 static void test_viewmodify(void)
699 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
700 UINT r;
701 const char *query;
702 char buffer[0x100];
703 DWORD sz;
705 DeleteFile(msifile);
707 /* just MsiOpenDatabase should not create a file */
708 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
709 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
711 query = "CREATE TABLE `phone` ( "
712 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
713 "PRIMARY KEY `id`)";
714 r = run_query( hdb, 0, query );
715 ok(r == ERROR_SUCCESS, "query failed\n");
717 /* check what the error function reports without doing anything */
718 sz = 0;
719 /* passing NULL as the 3rd param make function to crash on older platforms */
720 r = MsiViewGetError( 0, NULL, &sz );
721 ok(r == MSIDBERROR_INVALIDARG, "MsiViewGetError return\n");
723 /* open a view */
724 query = "SELECT * FROM `phone`";
725 r = MsiDatabaseOpenView(hdb, query, &hview);
726 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
728 /* see what happens with a good hview and bad args */
729 r = MsiViewGetError( hview, NULL, NULL );
730 ok(r == MSIDBERROR_INVALIDARG || r == MSIDBERROR_NOERROR,
731 "MsiViewGetError returns %u (expected -3)\n", r);
732 r = MsiViewGetError( hview, buffer, NULL );
733 ok(r == MSIDBERROR_INVALIDARG, "MsiViewGetError return\n");
735 /* see what happens with a zero length buffer */
736 sz = 0;
737 buffer[0] = 'x';
738 r = MsiViewGetError( hview, buffer, &sz );
739 ok(r == MSIDBERROR_MOREDATA, "MsiViewGetError return\n");
740 ok(buffer[0] == 'x', "buffer cleared\n");
741 ok(sz == 0, "size not zero\n");
743 /* ok this one is strange */
744 sz = 0;
745 r = MsiViewGetError( hview, NULL, &sz );
746 ok(r == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
747 ok(sz == 0, "size not zero\n");
749 /* see if it really has an error */
750 sz = sizeof buffer;
751 buffer[0] = 'x';
752 r = MsiViewGetError( hview, buffer, &sz );
753 ok(r == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
754 ok(buffer[0] == 0, "buffer not cleared\n");
755 ok(sz == 0, "size not zero\n");
757 r = MsiViewExecute(hview, 0);
758 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
760 /* try some invalid records */
761 r = MsiViewModify(hview, MSIMODIFY_INSERT, 0 );
762 ok(r == ERROR_INVALID_HANDLE, "MsiViewModify failed\n");
763 r = MsiViewModify(hview, -1, 0 );
764 ok(r == ERROR_INVALID_HANDLE, "MsiViewModify failed\n");
766 /* try an small record */
767 hrec = MsiCreateRecord(1);
768 r = MsiViewModify(hview, -1, hrec );
769 ok(r == ERROR_INVALID_DATA, "MsiViewModify failed\n");
771 r = MsiCloseHandle(hrec);
772 ok(r == ERROR_SUCCESS, "failed to close record\n");
774 /* insert a valid record */
775 hrec = MsiCreateRecord(3);
777 r = MsiRecordSetInteger(hrec, 2, 1);
778 ok(r == ERROR_SUCCESS, "failed to set integer\n");
779 r = MsiRecordSetString(hrec, 2, "bob");
780 ok(r == ERROR_SUCCESS, "failed to set integer\n");
781 r = MsiRecordSetString(hrec, 3, "7654321");
782 ok(r == ERROR_SUCCESS, "failed to set integer\n");
784 r = MsiViewExecute(hview, 0);
785 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
786 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
787 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
789 /* insert the same thing again */
790 r = MsiViewExecute(hview, 0);
791 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
793 /* should fail ... */
794 todo_wine {
795 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
796 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
799 r = MsiCloseHandle(hrec);
800 ok(r == ERROR_SUCCESS, "failed to close record\n");
802 r = MsiViewClose(hview);
803 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
804 r = MsiCloseHandle(hview);
805 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
807 r = MsiCloseHandle( hdb );
808 ok(r == ERROR_SUCCESS, "MsiOpenDatabase close failed\n");
811 static MSIHANDLE create_db(void)
813 MSIHANDLE hdb = 0;
814 UINT res;
816 DeleteFile(msifile);
818 /* create an empty database */
819 res = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb );
820 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
821 if( res != ERROR_SUCCESS )
822 return hdb;
824 res = MsiDatabaseCommit( hdb );
825 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
827 return hdb;
830 static void test_getcolinfo(void)
832 MSIHANDLE hdb, hview = 0, rec = 0;
833 UINT r;
834 DWORD sz;
835 char buffer[0x20];
837 /* create an empty db */
838 hdb = create_db();
839 ok( hdb, "failed to create db\n");
841 /* tables should be present */
842 r = MsiDatabaseOpenView(hdb, "select * from _Tables", &hview);
843 ok( r == ERROR_SUCCESS, "failed to open query\n");
845 r = MsiViewExecute(hview, 0);
846 ok( r == ERROR_SUCCESS, "failed to execute query\n");
848 /* check that NAMES works */
849 rec = 0;
850 r = MsiViewGetColumnInfo( hview, MSICOLINFO_NAMES, &rec );
851 ok( r == ERROR_SUCCESS, "failed to get names\n");
852 sz = sizeof buffer;
853 r = MsiRecordGetString(rec, 1, buffer, &sz );
854 ok( r == ERROR_SUCCESS, "failed to get string\n");
855 ok( !strcmp(buffer,"Name"), "_Tables has wrong column name\n");
856 r = MsiCloseHandle( rec );
857 ok( r == ERROR_SUCCESS, "failed to close record handle\n");
859 /* check that TYPES works */
860 rec = 0;
861 r = MsiViewGetColumnInfo( hview, MSICOLINFO_TYPES, &rec );
862 ok( r == ERROR_SUCCESS, "failed to get names\n");
863 sz = sizeof buffer;
864 r = MsiRecordGetString(rec, 1, buffer, &sz );
865 ok( r == ERROR_SUCCESS, "failed to get string\n");
866 ok( !strcmp(buffer,"s64"), "_Tables has wrong column type\n");
867 r = MsiCloseHandle( rec );
868 ok( r == ERROR_SUCCESS, "failed to close record handle\n");
870 /* check that invalid values fail */
871 rec = 0;
872 r = MsiViewGetColumnInfo( hview, 100, &rec );
873 ok( r == ERROR_INVALID_PARAMETER, "wrong error code\n");
874 ok( rec == 0, "returned a record\n");
876 r = MsiViewGetColumnInfo( hview, MSICOLINFO_TYPES, NULL );
877 ok( r == ERROR_INVALID_PARAMETER, "wrong error code\n");
879 r = MsiViewGetColumnInfo( 0, MSICOLINFO_TYPES, &rec );
880 ok( r == ERROR_INVALID_HANDLE, "wrong error code\n");
882 r = MsiViewClose(hview);
883 ok( r == ERROR_SUCCESS, "failed to close view\n");
884 r = MsiCloseHandle(hview);
885 ok( r == ERROR_SUCCESS, "failed to close view handle\n");
886 r = MsiCloseHandle(hdb);
887 ok( r == ERROR_SUCCESS, "failed to close database\n");
890 static MSIHANDLE get_column_info(MSIHANDLE hdb, const char *query, MSICOLINFO type)
892 MSIHANDLE hview = 0, rec = 0;
893 UINT r;
895 r = MsiDatabaseOpenView(hdb, query, &hview);
896 if( r != ERROR_SUCCESS )
897 return r;
899 r = MsiViewExecute(hview, 0);
900 if( r == ERROR_SUCCESS )
902 MsiViewGetColumnInfo( hview, type, &rec );
903 MsiViewClose(hview);
905 MsiCloseHandle(hview);
906 return rec;
909 static UINT get_columns_table_type(MSIHANDLE hdb, const char *table, UINT field)
911 MSIHANDLE hview = 0, rec = 0;
912 UINT r, type = 0;
913 char query[0x100];
915 sprintf(query, "select * from `_Columns` where `Table` = '%s'", table );
917 r = MsiDatabaseOpenView(hdb, query, &hview);
918 if( r != ERROR_SUCCESS )
919 return r;
921 r = MsiViewExecute(hview, 0);
922 if( r == ERROR_SUCCESS )
924 while (1)
926 r = MsiViewFetch( hview, &rec );
927 if( r != ERROR_SUCCESS)
928 break;
929 r = MsiRecordGetInteger( rec, 2 );
930 if (r == field)
931 type = MsiRecordGetInteger( rec, 4 );
932 MsiCloseHandle( rec );
935 MsiViewClose(hview);
937 MsiCloseHandle(hview);
938 return type;
941 static BOOL check_record( MSIHANDLE rec, UINT field, LPCSTR val )
943 CHAR buffer[0x20];
944 UINT r;
945 DWORD sz;
947 sz = sizeof buffer;
948 r = MsiRecordGetString( rec, field, buffer, &sz );
949 return (r == ERROR_SUCCESS ) && !strcmp(val, buffer);
952 static void test_viewgetcolumninfo(void)
954 MSIHANDLE hdb = 0, rec;
955 UINT r;
957 hdb = create_db();
958 ok( hdb, "failed to create db\n");
960 r = run_query( hdb, 0,
961 "CREATE TABLE `Properties` "
962 "( `Property` CHAR(255), `Value` CHAR(1) PRIMARY KEY `Property`)" );
963 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
965 /* check the column types */
966 rec = get_column_info( hdb, "select * from `Properties`", MSICOLINFO_TYPES );
967 ok( rec, "failed to get column info record\n" );
969 ok( check_record( rec, 1, "S255"), "wrong record type\n");
970 ok( check_record( rec, 2, "S1"), "wrong record type\n");
972 MsiCloseHandle( rec );
974 /* check the type in _Columns */
975 ok( 0x3dff == get_columns_table_type(hdb, "Properties", 1 ), "_columns table wrong\n");
976 ok( 0x1d01 == get_columns_table_type(hdb, "Properties", 2 ), "_columns table wrong\n");
978 /* now try the names */
979 rec = get_column_info( hdb, "select * from `Properties`", MSICOLINFO_NAMES );
980 ok( rec, "failed to get column info record\n" );
982 ok( check_record( rec, 1, "Property"), "wrong record type\n");
983 ok( check_record( rec, 2, "Value"), "wrong record type\n");
985 MsiCloseHandle( rec );
987 r = run_query( hdb, 0,
988 "CREATE TABLE `Binary` "
989 "( `Name` CHAR(255), `Data` OBJECT PRIMARY KEY `Name`)" );
990 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
992 /* check the column types */
993 rec = get_column_info( hdb, "select * from `Binary`", MSICOLINFO_TYPES );
994 ok( rec, "failed to get column info record\n" );
996 ok( check_record( rec, 1, "S255"), "wrong record type\n");
997 ok( check_record( rec, 2, "V0"), "wrong record type\n");
999 MsiCloseHandle( rec );
1001 /* check the type in _Columns */
1002 ok( 0x3dff == get_columns_table_type(hdb, "Binary", 1 ), "_columns table wrong\n");
1003 ok( 0x1900 == get_columns_table_type(hdb, "Binary", 2 ), "_columns table wrong\n");
1005 /* now try the names */
1006 rec = get_column_info( hdb, "select * from `Binary`", MSICOLINFO_NAMES );
1007 ok( rec, "failed to get column info record\n" );
1009 ok( check_record( rec, 1, "Name"), "wrong record type\n");
1010 ok( check_record( rec, 2, "Data"), "wrong record type\n");
1011 MsiCloseHandle( rec );
1013 r = run_query( hdb, 0,
1014 "CREATE TABLE `UIText` "
1015 "( `Key` CHAR(72) NOT NULL, `Text` CHAR(255) LOCALIZABLE PRIMARY KEY `Key`)" );
1016 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
1018 ok( 0x2d48 == get_columns_table_type(hdb, "UIText", 1 ), "_columns table wrong\n");
1019 ok( 0x1fff == get_columns_table_type(hdb, "UIText", 2 ), "_columns table wrong\n");
1021 rec = get_column_info( hdb, "select * from `UIText`", MSICOLINFO_NAMES );
1022 ok( rec, "failed to get column info record\n" );
1023 ok( check_record( rec, 1, "Key"), "wrong record type\n");
1024 ok( check_record( rec, 2, "Text"), "wrong record type\n");
1025 MsiCloseHandle( rec );
1027 rec = get_column_info( hdb, "select * from `UIText`", MSICOLINFO_TYPES );
1028 ok( rec, "failed to get column info record\n" );
1029 ok( check_record( rec, 1, "s72"), "wrong record type\n");
1030 ok( check_record( rec, 2, "L255"), "wrong record type\n");
1031 MsiCloseHandle( rec );
1033 MsiCloseHandle( hdb );
1036 static void test_msiexport(void)
1038 MSIHANDLE hdb = 0, hview = 0;
1039 UINT r;
1040 const char *query;
1041 char path[MAX_PATH];
1042 const char file[] = "phone.txt";
1043 HANDLE handle;
1044 char buffer[0x100];
1045 DWORD length;
1046 const char expected[] =
1047 "id\tname\tnumber\r\n"
1048 "I2\tS32\tS32\r\n"
1049 "phone\tid\r\n"
1050 "1\tAbe\t8675309\r\n";
1052 DeleteFile(msifile);
1054 /* just MsiOpenDatabase should not create a file */
1055 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
1056 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
1058 /* create a table */
1059 query = "CREATE TABLE `phone` ( "
1060 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
1061 "PRIMARY KEY `id`)";
1062 r = MsiDatabaseOpenView(hdb, query, &hview);
1063 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1064 r = MsiViewExecute(hview, 0);
1065 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1066 r = MsiViewClose(hview);
1067 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1068 r = MsiCloseHandle(hview);
1069 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1071 /* insert a value into it */
1072 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
1073 "VALUES('1', 'Abe', '8675309')";
1074 r = MsiDatabaseOpenView(hdb, query, &hview);
1075 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1076 r = MsiViewExecute(hview, 0);
1077 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1078 r = MsiViewClose(hview);
1079 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1080 r = MsiCloseHandle(hview);
1081 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1083 GetCurrentDirectory(MAX_PATH, path);
1085 r = MsiDatabaseExport(hdb, "phone", path, file);
1086 ok(r == ERROR_SUCCESS, "MsiDatabaseExport failed\n");
1088 MsiCloseHandle(hdb);
1090 lstrcat(path, "\\");
1091 lstrcat(path, file);
1093 /* check the data that was written */
1094 length = 0;
1095 memset(buffer, 0, sizeof buffer);
1096 handle = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1097 if (handle != INVALID_HANDLE_VALUE)
1099 ReadFile(handle, buffer, sizeof buffer, &length, NULL);
1100 CloseHandle(handle);
1101 DeleteFile(path);
1103 else
1104 ok(0, "failed to open file %s\n", path);
1106 ok( length == strlen(expected), "length of data wrong\n");
1107 ok( !lstrcmp(buffer, expected), "data doesn't match\n");
1108 DeleteFile(msifile);
1111 static void test_longstrings(void)
1113 const char insert_query[] =
1114 "INSERT INTO `strings` ( `id`, `val` ) VALUES('1', 'Z')";
1115 char *str;
1116 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
1117 DWORD len;
1118 UINT r;
1119 const DWORD STRING_LENGTH = 0x10005;
1121 DeleteFile(msifile);
1122 /* just MsiOpenDatabase should not create a file */
1123 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
1124 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
1126 /* create a table */
1127 r = try_query( hdb,
1128 "CREATE TABLE `strings` ( `id` INT, `val` CHAR(0) PRIMARY KEY `id`)");
1129 ok(r == ERROR_SUCCESS, "query failed\n");
1131 /* try a insert a very long string */
1132 str = HeapAlloc(GetProcessHeap(), 0, STRING_LENGTH+sizeof insert_query);
1133 len = strchr(insert_query, 'Z') - insert_query;
1134 strcpy(str, insert_query);
1135 memset(str+len, 'Z', STRING_LENGTH);
1136 strcpy(str+len+STRING_LENGTH, insert_query+len+1);
1137 r = try_query( hdb, str );
1138 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1140 HeapFree(GetProcessHeap(), 0, str);
1142 MsiDatabaseCommit(hdb);
1143 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
1144 MsiCloseHandle(hdb);
1146 r = MsiOpenDatabase(msifile, MSIDBOPEN_READONLY, &hdb);
1147 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
1149 r = MsiDatabaseOpenView(hdb, "select * from `strings` where `id` = 1", &hview);
1150 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1152 r = MsiViewExecute(hview, 0);
1153 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1155 r = MsiViewFetch(hview, &hrec);
1156 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1158 MsiCloseHandle(hview);
1160 r = MsiRecordGetString(hrec, 2, NULL, &len);
1161 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1162 ok(len == STRING_LENGTH, "string length wrong\n");
1164 MsiCloseHandle(hrec);
1165 MsiCloseHandle(hdb);
1166 DeleteFile(msifile);
1169 static void create_file(const CHAR *name)
1171 HANDLE file;
1172 DWORD written;
1174 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
1175 ok(file != INVALID_HANDLE_VALUE, "Failure to open file %s\n", name);
1176 WriteFile(file, name, strlen(name), &written, NULL);
1177 WriteFile(file, "\n", strlen("\n"), &written, NULL);
1178 CloseHandle(file);
1181 static void test_streamtable(void)
1183 MSIHANDLE hdb = 0, rec, view;
1184 char file[MAX_PATH];
1185 char buf[MAX_PATH];
1186 DWORD size;
1187 UINT r;
1189 hdb = create_db();
1190 ok( hdb, "failed to create db\n");
1192 r = run_query( hdb, 0,
1193 "CREATE TABLE `Properties` "
1194 "( `Property` CHAR(255), `Value` CHAR(1) PRIMARY KEY `Property`)" );
1195 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
1197 r = run_query( hdb, 0,
1198 "INSERT INTO `Properties` "
1199 "( `Value`, `Property` ) VALUES ( 'Prop', 'value' )" );
1200 ok( r == ERROR_SUCCESS, "Failed to add to table\n" );
1202 r = MsiDatabaseCommit( hdb );
1203 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
1205 MsiCloseHandle( hdb );
1207 r = MsiOpenDatabase(msifile, MSIDBOPEN_TRANSACT, &hdb );
1208 ok( r == ERROR_SUCCESS , "Failed to open database\n" );
1210 /* check the column types */
1211 rec = get_column_info( hdb, "select * from `_Streams`", MSICOLINFO_TYPES );
1212 ok( rec, "failed to get column info record\n" );
1214 ok( check_record( rec, 1, "s62"), "wrong record type\n");
1215 ok( check_record( rec, 2, "V0"), "wrong record type\n");
1217 MsiCloseHandle( rec );
1219 /* now try the names */
1220 rec = get_column_info( hdb, "select * from `_Streams`", MSICOLINFO_NAMES );
1221 ok( rec, "failed to get column info record\n" );
1223 ok( check_record( rec, 1, "Name"), "wrong record type\n");
1224 ok( check_record( rec, 2, "Data"), "wrong record type\n");
1226 MsiCloseHandle( rec );
1228 /* insert a file into the _Streams table */
1229 create_file( "test.txt" );
1231 rec = MsiCreateRecord( 2 );
1232 MsiRecordSetString( rec, 1, "data" );
1234 r = MsiRecordSetStream( rec, 2, "test.txt" );
1235 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r);
1237 DeleteFile("test.txt");
1239 r = MsiDatabaseOpenView( hdb,
1240 "INSERT INTO `_Streams` ( `Name`, `Data` ) VALUES ( ?, ? )", &view );
1241 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1243 r = MsiViewExecute( view, rec );
1244 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1246 MsiCloseHandle( rec );
1247 MsiCloseHandle( view );
1249 r = MsiDatabaseOpenView( hdb,
1250 "SELECT `Name`, `Data` FROM `_Streams`", &view );
1251 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1253 r = MsiViewExecute( view, 0 );
1254 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1256 r = MsiViewFetch( view, &rec );
1257 ok( r == ERROR_SUCCESS, "Failed to fetch record: %d\n", r);
1259 size = MAX_PATH;
1260 r = MsiRecordGetString( rec, 1, file, &size );
1261 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r);
1262 ok( !lstrcmp(file, "data"), "Expected 'data', got %s\n", file);
1264 size = MAX_PATH;
1265 memset(buf, 0, MAX_PATH);
1266 r = MsiRecordReadStream( rec, 2, buf, &size );
1267 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r);
1268 ok( !lstrcmp(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf);
1270 MsiCloseHandle( rec );
1272 r = MsiViewFetch( view, &rec );
1273 ok( r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
1275 MsiCloseHandle( view );
1276 MsiCloseHandle( hdb );
1277 DeleteFile(msifile);
1280 static void test_where(void)
1282 MSIHANDLE hdb = 0, rec, view;
1283 LPCSTR query;
1284 UINT r;
1285 DWORD size;
1286 CHAR buf[MAX_PATH];
1287 UINT count;
1289 hdb = create_db();
1290 ok( hdb, "failed to create db\n");
1292 r = run_query( hdb, 0,
1293 "CREATE TABLE `Media` ("
1294 "`DiskId` SHORT NOT NULL, "
1295 "`LastSequence` LONG, "
1296 "`DiskPrompt` CHAR(64) LOCALIZABLE, "
1297 "`Cabinet` CHAR(255), "
1298 "`VolumeLabel` CHAR(32), "
1299 "`Source` CHAR(72) "
1300 "PRIMARY KEY `DiskId`)" );
1301 ok( r == S_OK, "cannot create Media table: %d\n", r );
1303 r = run_query( hdb, 0, "INSERT INTO `Media` "
1304 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
1305 "VALUES ( 1, 0, '', 'zero.cab', '', '' )" );
1306 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
1308 r = run_query( hdb, 0, "INSERT INTO `Media` "
1309 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
1310 "VALUES ( 2, 1, '', 'one.cab', '', '' )" );
1311 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
1313 r = run_query( hdb, 0, "INSERT INTO `Media` "
1314 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
1315 "VALUES ( 3, 2, '', 'two.cab', '', '' )" );
1316 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
1318 query = "SELECT * FROM `Media`";
1319 r = do_query(hdb, query, &rec);
1320 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %d\n", r);
1321 ok( check_record( rec, 4, "zero.cab"), "wrong cabinet\n");
1322 MsiCloseHandle( rec );
1324 query = "SELECT * FROM `Media` WHERE `LastSequence` >= 1";
1325 r = do_query(hdb, query, &rec);
1326 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %d\n", r);
1327 ok( check_record( rec, 4, "one.cab"), "wrong cabinet\n");
1329 r = MsiRecordGetInteger(rec, 1);
1330 ok( 2 == r, "field wrong\n");
1331 r = MsiRecordGetInteger(rec, 2);
1332 ok( 1 == r, "field wrong\n");
1333 MsiCloseHandle( rec );
1335 query = "SELECT `DiskId` FROM `Media` WHERE `LastSequence` >= 1 AND DiskId >= 0";
1336 r = MsiDatabaseOpenView(hdb, query, &view);
1337 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
1339 r = MsiViewExecute(view, 0);
1340 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
1342 r = MsiViewFetch(view, &rec);
1343 ok( r == ERROR_SUCCESS, "failed to fetch view: %d\n", r );
1345 count = MsiRecordGetFieldCount( rec );
1346 ok( count == 1, "Expected 1 record fields, got %d\n", count );
1348 size = MAX_PATH;
1349 r = MsiRecordGetString( rec, 1, buf, &size );
1350 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
1351 ok( !lstrcmp( buf, "2" ),
1352 "For (row %d, column 1) expected '%d', got %s\n", 0, 2, buf );
1353 MsiCloseHandle( rec );
1355 r = MsiViewFetch(view, &rec);
1356 ok( r == ERROR_SUCCESS, "failed to fetch view: %d\n", r );
1358 size = MAX_PATH;
1359 r = MsiRecordGetString( rec, 1, buf, &size );
1360 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
1361 ok( !lstrcmp( buf, "3" ),
1362 "For (row %d, column 1) expected '%d', got %s\n", 1, 3, buf );
1363 MsiCloseHandle( rec );
1365 r = MsiViewFetch(view, &rec);
1366 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
1368 MsiViewClose(view);
1369 MsiCloseHandle(view);
1371 MsiCloseHandle( rec );
1373 rec = 0;
1374 query = "SELECT * FROM `Media` WHERE `DiskPrompt` IS NULL";
1375 r = do_query(hdb, query, &rec);
1376 ok( r == ERROR_SUCCESS, "query failed: %d\n", r );
1377 MsiCloseHandle( rec );
1379 MsiCloseHandle( hdb );
1380 DeleteFile(msifile);
1383 static CHAR CURR_DIR[MAX_PATH];
1385 static const CHAR test_data[] = "FirstPrimaryColumn\tSecondPrimaryColumn\tShortInt\tShortIntNullable\tLongInt\tLongIntNullable\tString\tLocalizableString\tLocalizableStringNullable\n"
1386 "s255\ti2\ti2\tI2\ti4\tI4\tS255\tS0\ts0\n"
1387 "TestTable\tFirstPrimaryColumn\n"
1388 "stringage\t5\t2\t\t2147483640\t-2147483640\tanother string\tlocalizable\tduh\n";
1390 static const CHAR two_primary[] = "PrimaryOne\tPrimaryTwo\n"
1391 "s255\ts255\n"
1392 "TwoPrimary\tPrimaryOne\tPrimaryTwo\n"
1393 "papaya\tleaf\n"
1394 "papaya\tflower\n";
1396 static void write_file(const CHAR *filename, const char *data, int data_size)
1398 DWORD size;
1400 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
1401 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1403 WriteFile(hf, data, data_size, &size, NULL);
1404 CloseHandle(hf);
1407 static UINT add_table_to_db(MSIHANDLE hdb, LPCSTR table_data)
1409 UINT r;
1411 write_file("temp_file", table_data, (lstrlen(table_data) - 1) * sizeof(char));
1412 r = MsiDatabaseImportA(hdb, CURR_DIR, "temp_file");
1413 DeleteFileA("temp_file");
1415 return r;
1418 static void test_msiimport(void)
1420 MSIHANDLE hdb, view, rec;
1421 LPCSTR query;
1422 UINT r, count;
1423 signed int i;
1425 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
1427 r = MsiOpenDatabaseA(msifile, MSIDBOPEN_CREATE, &hdb);
1428 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1430 r = add_table_to_db(hdb, test_data);
1431 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1433 r = add_table_to_db(hdb, two_primary);
1434 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1436 query = "SELECT * FROM `TestTable`";
1437 r = MsiDatabaseOpenView(hdb, query, &view);
1438 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1440 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
1441 count = MsiRecordGetFieldCount(rec);
1442 ok(count == 9, "Expected 9, got %d\n", count);
1443 ok(check_record(rec, 1, "FirstPrimaryColumn"), "Expected FirstPrimaryColumn\n");
1444 ok(check_record(rec, 2, "SecondPrimaryColumn"), "Expected SecondPrimaryColumn\n");
1445 ok(check_record(rec, 3, "ShortInt"), "Expected ShortInt\n");
1446 ok(check_record(rec, 4, "ShortIntNullable"), "Expected ShortIntNullalble\n");
1447 ok(check_record(rec, 5, "LongInt"), "Expected LongInt\n");
1448 ok(check_record(rec, 6, "LongIntNullable"), "Expected LongIntNullalble\n");
1449 ok(check_record(rec, 7, "String"), "Expected String\n");
1450 ok(check_record(rec, 8, "LocalizableString"), "Expected LocalizableString\n");
1451 ok(check_record(rec, 9, "LocalizableStringNullable"), "Expected LocalizableStringNullable\n");
1452 MsiCloseHandle(rec);
1454 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
1455 count = MsiRecordGetFieldCount(rec);
1456 ok(count == 9, "Expected 9, got %d\n", count);
1457 ok(check_record(rec, 1, "s255"), "Expected s255\n");
1458 ok(check_record(rec, 2, "i2"), "Expected i2\n");
1459 ok(check_record(rec, 3, "i2"), "Expected i2\n");
1460 ok(check_record(rec, 4, "I2"), "Expected I2\n");
1461 ok(check_record(rec, 5, "i4"), "Expected i4\n");
1462 ok(check_record(rec, 6, "I4"), "Expected I4\n");
1463 ok(check_record(rec, 7, "S255"), "Expected S255\n");
1464 ok(check_record(rec, 8, "S0"), "Expected S0\n");
1465 ok(check_record(rec, 9, "s0"), "Expected s0\n");
1466 MsiCloseHandle(rec);
1468 query = "SELECT * FROM `TestTable`";
1469 r = do_query(hdb, query, &rec);
1470 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1471 ok(check_record(rec, 1, "stringage"), "Expected 'stringage'\n");
1472 ok(check_record(rec, 7, "another string"), "Expected 'another string'\n");
1473 ok(check_record(rec, 8, "localizable"), "Expected 'localizable'\n");
1474 ok(check_record(rec, 9, "duh"), "Expected 'duh'\n");
1476 i = MsiRecordGetInteger(rec, 2);
1477 ok(i == 5, "Expected 5, got %d\n", i);
1479 i = MsiRecordGetInteger(rec, 3);
1480 ok(i == 2, "Expected 2, got %d\n", i);
1482 i = MsiRecordGetInteger(rec, 4);
1483 ok(i == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", i);
1485 i = MsiRecordGetInteger(rec, 5);
1486 ok(i == 2147483640, "Expected 2147483640, got %d\n", i);
1488 i = MsiRecordGetInteger(rec, 6);
1489 ok(i == -2147483640, "Expected -2147483640, got %d\n", i);
1491 MsiCloseHandle(rec);
1492 MsiCloseHandle(view);
1494 query = "SELECT * FROM `TwoPrimary`";
1495 r = MsiDatabaseOpenView(hdb, query, &view);
1496 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1498 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
1499 count = MsiRecordGetFieldCount(rec);
1500 ok(count == 2, "Expected 2, got %d\n", count);
1501 ok(check_record(rec, 1, "PrimaryOne"), "Expected PrimaryOne\n");
1502 ok(check_record(rec, 2, "PrimaryTwo"), "Expected PrimaryTwo\n");
1504 MsiCloseHandle(rec);
1506 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
1507 count = MsiRecordGetFieldCount(rec);
1508 ok(count == 2, "Expected 2, got %d\n", count);
1509 ok(check_record(rec, 1, "s255"), "Expected s255\n");
1510 ok(check_record(rec, 2, "s255"), "Expected s255\n");
1511 MsiCloseHandle(rec);
1513 r = MsiViewExecute(view, 0);
1514 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1516 r = MsiViewFetch(view, &rec);
1517 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1519 ok(check_record(rec, 1, "papaya"), "Expected 'papaya'\n");
1520 ok(check_record(rec, 2, "leaf"), "Expected 'leaf'\n");
1522 MsiCloseHandle(rec);
1524 r = MsiViewFetch(view, &rec);
1525 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1527 ok(check_record(rec, 1, "papaya"), "Expected 'papaya'\n");
1528 ok(check_record(rec, 2, "flower"), "Expected 'flower'\n");
1530 MsiCloseHandle(rec);
1532 r = MsiViewFetch(view, &rec);
1533 ok(r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
1535 r = MsiViewClose(view);
1536 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1538 MsiCloseHandle(view);
1539 MsiCloseHandle(hdb);
1540 DeleteFileA(msifile);
1543 static void test_markers(void)
1545 MSIHANDLE hdb, rec;
1546 LPCSTR query;
1547 UINT r;
1549 hdb = create_db();
1550 ok( hdb, "failed to create db\n");
1552 rec = MsiCreateRecord(3);
1553 MsiRecordSetString(rec, 1, "Table");
1554 MsiRecordSetString(rec, 2, "Apples");
1555 MsiRecordSetString(rec, 3, "Oranges");
1557 /* try a legit create */
1558 query = "CREATE TABLE `Table` ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1559 r = run_query(hdb, 0, query);
1560 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1561 MsiCloseHandle(rec);
1563 /* try table name as marker */
1564 rec = MsiCreateRecord(1);
1565 MsiRecordSetString(rec, 1, "Fable");
1566 query = "CREATE TABLE `?` ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1567 r = run_query(hdb, rec, query);
1568 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1570 /* verify that we just created a table called '?', not 'Fable' */
1571 r = try_query(hdb, "SELECT * from `Fable`");
1572 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1574 r = try_query(hdb, "SELECT * from `?`");
1575 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1577 /* try table name as marker without backticks */
1578 MsiRecordSetString(rec, 1, "Mable");
1579 query = "CREATE TABLE ? ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1580 r = run_query(hdb, rec, query);
1581 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1583 /* try one column name as marker */
1584 MsiRecordSetString(rec, 1, "One");
1585 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
1586 r = run_query(hdb, rec, query);
1587 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1588 MsiCloseHandle(rec);
1590 /* try column names as markers */
1591 rec = MsiCreateRecord(2);
1592 MsiRecordSetString(rec, 1, "One");
1593 MsiRecordSetString(rec, 2, "Two");
1594 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `One`)";
1595 r = run_query(hdb, rec, query);
1596 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1597 MsiCloseHandle(rec);
1599 /* try names with backticks */
1600 rec = MsiCreateRecord(3);
1601 MsiRecordSetString(rec, 1, "One");
1602 MsiRecordSetString(rec, 2, "Two");
1603 MsiRecordSetString(rec, 3, "One");
1604 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `?`)";
1605 r = run_query(hdb, rec, query);
1606 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1608 /* try names with backticks, minus definitions */
1609 query = "CREATE TABLE `Mable` ( `?`, `?` PRIMARY KEY `?`)";
1610 r = run_query(hdb, rec, query);
1611 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1613 /* try names without backticks */
1614 query = "CREATE TABLE `Mable` ( ? SHORT NOT NULL, ? CHAR(255) PRIMARY KEY ?)";
1615 r = run_query(hdb, rec, query);
1616 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1617 MsiCloseHandle(rec);
1619 /* try one long marker */
1620 rec = MsiCreateRecord(1);
1621 MsiRecordSetString(rec, 1, "`One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`");
1622 query = "CREATE TABLE `Mable` ( ? )";
1623 r = run_query(hdb, rec, query);
1624 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1625 MsiCloseHandle(rec);
1627 /* try all names as markers */
1628 rec = MsiCreateRecord(4);
1629 MsiRecordSetString(rec, 1, "Mable");
1630 MsiRecordSetString(rec, 2, "One");
1631 MsiRecordSetString(rec, 3, "Two");
1632 MsiRecordSetString(rec, 4, "One");
1633 query = "CREATE TABLE `?` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `?`)";
1634 r = run_query(hdb, rec, query);
1635 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1636 MsiCloseHandle(rec);
1638 /* try a legit insert */
1639 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( 5, 'hello' )";
1640 r = run_query(hdb, 0, query);
1641 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1643 r = try_query(hdb, "SELECT * from `Table`");
1644 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1646 /* try values as markers */
1647 rec = MsiCreateRecord(2);
1648 MsiRecordSetInteger(rec, 1, 4);
1649 MsiRecordSetString(rec, 2, "hi");
1650 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, '?' )";
1651 r = run_query(hdb, rec, query);
1652 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1653 MsiCloseHandle(rec);
1655 /* try column names and values as markers */
1656 rec = MsiCreateRecord(4);
1657 MsiRecordSetString(rec, 1, "One");
1658 MsiRecordSetString(rec, 2, "Two");
1659 MsiRecordSetInteger(rec, 3, 5);
1660 MsiRecordSetString(rec, 4, "hi");
1661 query = "INSERT INTO `Table` ( `?`, `?` ) VALUES ( ?, '?' )";
1662 r = run_query(hdb, rec, query);
1663 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1664 MsiCloseHandle(rec);
1666 /* try column names as markers */
1667 rec = MsiCreateRecord(2);
1668 MsiRecordSetString(rec, 1, "One");
1669 MsiRecordSetString(rec, 2, "Two");
1670 query = "INSERT INTO `Table` ( `?`, `?` ) VALUES ( 3, 'yellow' )";
1671 r = run_query(hdb, rec, query);
1672 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1673 MsiCloseHandle(rec);
1675 /* try table name as a marker */
1676 rec = MsiCreateRecord(1);
1677 MsiRecordSetString(rec, 1, "Table");
1678 query = "INSERT INTO `?` ( `One`, `Two` ) VALUES ( 2, 'green' )";
1679 r = run_query(hdb, rec, query);
1680 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1681 MsiCloseHandle(rec);
1683 /* try table name and values as markers */
1684 rec = MsiCreateRecord(3);
1685 MsiRecordSetString(rec, 1, "Table");
1686 MsiRecordSetInteger(rec, 2, 10);
1687 MsiRecordSetString(rec, 3, "haha");
1688 query = "INSERT INTO `?` ( `One`, `Two` ) VALUES ( ?, '?' )";
1689 r = run_query(hdb, rec, query);
1690 ok(r == ERROR_FUNCTION_FAILED, "Expected ERROR_FUNCTION_FAILED, got %d\n", r);
1691 MsiCloseHandle(rec);
1693 /* try all markers */
1694 rec = MsiCreateRecord(5);
1695 MsiRecordSetString(rec, 1, "Table");
1696 MsiRecordSetString(rec, 1, "One");
1697 MsiRecordSetString(rec, 1, "Two");
1698 MsiRecordSetInteger(rec, 2, 10);
1699 MsiRecordSetString(rec, 3, "haha");
1700 query = "INSERT INTO `?` ( `?`, `?` ) VALUES ( ?, '?' )";
1701 r = run_query(hdb, rec, query);
1702 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
1703 MsiCloseHandle(rec);
1705 /* insert an integer as a string */
1706 rec = MsiCreateRecord(2);
1707 MsiRecordSetString(rec, 1, "11");
1708 MsiRecordSetString(rec, 2, "hi");
1709 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, '?' )";
1710 r = run_query(hdb, rec, query);
1711 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1712 MsiCloseHandle(rec);
1714 /* leave off the '' for the string */
1715 rec = MsiCreateRecord(2);
1716 MsiRecordSetInteger(rec, 1, 12);
1717 MsiRecordSetString(rec, 2, "hi");
1718 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, ? )";
1719 r = run_query(hdb, rec, query);
1720 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1721 MsiCloseHandle(rec);
1723 MsiCloseHandle(hdb);
1724 DeleteFileA(msifile);
1727 #define MY_NVIEWS 4000 /* Largest installer I've seen uses < 2k */
1728 static void test_handle_limit(void)
1730 int i;
1731 MSIHANDLE hdb;
1732 MSIHANDLE hviews[MY_NVIEWS];
1733 UINT r;
1735 /* create an empty db */
1736 hdb = create_db();
1737 ok( hdb, "failed to create db\n");
1739 memset(hviews, 0, sizeof(hviews));
1741 for (i=0; i<MY_NVIEWS; i++) {
1742 static char szQueryBuf[256] = "SELECT * from `_Tables`";
1743 hviews[i] = 0xdeadbeeb;
1744 r = MsiDatabaseOpenView(hdb, szQueryBuf, &hviews[i]);
1745 if( r != ERROR_SUCCESS || hviews[i] == 0xdeadbeeb ||
1746 hviews[i] == 0 || (i && (hviews[i] == hviews[i-1])))
1747 break;
1750 ok( i == MY_NVIEWS, "problem opening views\n");
1752 for (i=0; i<MY_NVIEWS; i++) {
1753 if (hviews[i] != 0 && hviews[i] != 0xdeadbeeb) {
1754 r = MsiCloseHandle(hviews[i]);
1755 if (r != ERROR_SUCCESS)
1756 break;
1760 ok( i == MY_NVIEWS, "problem closing views\n");
1762 r = MsiCloseHandle(hdb);
1763 ok( r == ERROR_SUCCESS, "failed to close database\n");
1766 static void generate_transform(void)
1768 MSIHANDLE hdb1, hdb2, hrec;
1769 LPCSTR query;
1770 UINT r;
1772 /* start with two identical databases */
1773 CopyFile(msifile2, msifile, FALSE);
1775 r = MsiOpenDatabase(msifile, MSIDBOPEN_TRANSACT, &hdb1 );
1776 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
1778 r = MsiDatabaseCommit( hdb1 );
1779 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
1781 r = MsiOpenDatabase(msifile2, MSIDBOPEN_READONLY, &hdb2 );
1782 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
1784 /* the transform between two identical database should be empty */
1785 r = MsiDatabaseGenerateTransform(hdb1, hdb2, NULL, 0, 0);
1786 todo_wine {
1787 ok( r == ERROR_NO_DATA, "return code %d, should be ERROR_NO_DATA\n", r );
1790 query = "CREATE TABLE `AAR` ( `BAR` SHORT NOT NULL, `CAR` CHAR(255) PRIMARY KEY `CAR`)";
1791 r = run_query(hdb1, 0, query);
1792 ok(r == ERROR_SUCCESS, "failed to add table\n");
1794 query = "INSERT INTO `AAR` ( `BAR`, `CAR` ) VALUES ( 1, 'vw' )";
1795 r = run_query(hdb1, 0, query);
1796 ok(r == ERROR_SUCCESS, "failed to add row 1\n");
1798 query = "INSERT INTO `AAR` ( `BAR`, `CAR` ) VALUES ( 2, 'bmw' )";
1799 r = run_query(hdb1, 0, query);
1800 ok(r == ERROR_SUCCESS, "failed to add row 2\n");
1802 query = "UPDATE `MOO` SET `OOO` = 'c' WHERE `NOO` = 1";
1803 r = run_query(hdb1, 0, query);
1804 ok(r == ERROR_SUCCESS, "failed to modify row\n");
1806 query = "DELETE FROM `MOO` WHERE `NOO` = 3";
1807 r = run_query(hdb1, 0, query);
1808 ok(r == ERROR_SUCCESS, "failed to delete row\n");
1810 hrec = MsiCreateRecord(2);
1811 r = MsiRecordSetInteger(hrec, 1, 1);
1812 ok(r == ERROR_SUCCESS, "failed to set integer\n");
1814 write_file("testdata.bin", "naengmyon", 9);
1815 r = MsiRecordSetStream(hrec, 2, "testdata.bin");
1816 ok(r == ERROR_SUCCESS, "failed to set stream\n");
1818 query = "INSERT INTO `BINARY` ( `ID`, `BLOB` ) VALUES ( ?, ? )";
1819 r = run_query(hdb1, hrec, query);
1820 ok(r == ERROR_SUCCESS, "failed to add row with blob\n");
1822 MsiCloseHandle(hrec);
1824 query = "ALTER TABLE `MOO` ADD `COW` INTEGER";
1825 r = run_query(hdb1, 0, query);
1826 ok(r == ERROR_SUCCESS, "failed to add column\n");
1828 query = "ALTER TABLE `MOO` ADD `PIG` INTEGER";
1829 r = run_query(hdb1, 0, query);
1830 ok(r == ERROR_SUCCESS, "failed to add column\n");
1832 query = "UPDATE `MOO` SET `PIG` = 5 WHERE `NOO` = 1";
1833 r = run_query(hdb1, 0, query);
1834 ok(r == ERROR_SUCCESS, "failed to modify row\n");
1836 query = "CREATE TABLE `Property` ( `Property` CHAR(72) NOT NULL, "
1837 "`Value` CHAR(0) PRIMARY KEY `Property`)";
1838 r = run_query(hdb1, 0, query);
1839 ok(r == ERROR_SUCCESS, "failed to add property table\n");
1841 query = "INSERT INTO `Property` ( `Property`, `Value` ) VALUES ( 'prop', 'val' )";
1842 r = run_query(hdb1, 0, query);
1843 ok(r == ERROR_SUCCESS, "failed to add property\n");
1845 /* database needs to be committed */
1846 MsiDatabaseCommit(hdb1);
1848 r = MsiDatabaseGenerateTransform(hdb1, hdb2, mstfile, 0, 0);
1849 ok( r == ERROR_SUCCESS, "return code %d, should be ERROR_SUCCESS\n", r );
1851 MsiCloseHandle( hdb1 );
1852 MsiCloseHandle( hdb2 );
1854 DeleteFile("testdata.bin");
1857 /* data for generating a transform */
1859 /* tables transform names - encoded as they would be in an msi database file */
1860 static const WCHAR name1[] = { 0x4840, 0x3a8a, 0x481b, 0 }; /* AAR */
1861 static const WCHAR name2[] = { 0x4840, 0x3b3f, 0x43f2, 0x4438, 0x45b1, 0 }; /* _Columns */
1862 static const WCHAR name3[] = { 0x4840, 0x3f7f, 0x4164, 0x422f, 0x4836, 0 }; /* _Tables */
1863 static const WCHAR name4[] = { 0x4840, 0x3f3f, 0x4577, 0x446c, 0x3b6a, 0x45e4, 0x4824, 0 }; /* _StringData */
1864 static const WCHAR name5[] = { 0x4840, 0x3f3f, 0x4577, 0x446c, 0x3e6a, 0x44b2, 0x482f, 0 }; /* _StringPool */
1865 static const WCHAR name6[] = { 0x4840, 0x3e16, 0x4818, 0}; /* MOO */
1866 static const WCHAR name7[] = { 0x4840, 0x3c8b, 0x3a97, 0x409b, 0 }; /* BINARY */
1867 static const WCHAR name8[] = { 0x3c8b, 0x3a97, 0x409b, 0x387e, 0 }; /* BINARY.1 */
1868 static const WCHAR name9[] = { 0x4840, 0x4559, 0x44f2, 0x4568, 0x4737, 0 }; /* Property */
1870 /* data in each table */
1871 static const WCHAR data1[] = { /* AAR */
1872 0x0201, 0x0008, 0x8001, /* 0x0201 = add row (1), two shorts */
1873 0x0201, 0x0009, 0x8002,
1875 static const WCHAR data2[] = { /* _Columns */
1876 0x0401, 0x0001, 0x8003, 0x0002, 0x9502,
1877 0x0401, 0x0001, 0x8004, 0x0003, 0x9502,
1878 0x0401, 0x0005, 0x0000, 0x0006, 0xbdff, /* 0x0401 = add row (1), 4 shorts */
1879 0x0401, 0x0005, 0x0000, 0x0007, 0x8502,
1880 0x0401, 0x000a, 0x0000, 0x000a, 0xad48,
1881 0x0401, 0x000a, 0x0000, 0x000b, 0x9d00,
1883 static const WCHAR data3[] = { /* _Tables */
1884 0x0101, 0x0005, /* 0x0101 = add row (1), 1 short */
1885 0x0101, 0x000a,
1887 static const char data4[] = /* _StringData */
1888 "MOOCOWPIGcAARCARBARvwbmwPropertyValuepropval"; /* all the strings squashed together */
1889 static const WCHAR data5[] = { /* _StringPool */
1890 /* len, refs */
1891 0, 0, /* string 0 '' */
1892 3, 2, /* string 1 'MOO' */
1893 3, 1, /* string 2 'COW' */
1894 3, 1, /* string 3 'PIG' */
1895 1, 1, /* string 4 'c' */
1896 3, 3, /* string 5 'AAR' */
1897 3, 1, /* string 6 'CAR' */
1898 3, 1, /* string 7 'BAR' */
1899 2, 1, /* string 8 'vw' */
1900 3, 1, /* string 9 'bmw' */
1901 8, 4, /* string 10 'Property' */
1902 5, 1, /* string 11 'Value' */
1903 4, 1, /* string 12 'prop' */
1904 3, 1, /* string 13 'val' */
1906 /* update row, 0x0002 is a bitmask of present column data, keys are excluded */
1907 static const WCHAR data6[] = { /* MOO */
1908 0x000a, 0x8001, 0x0004, 0x8005, /* update row */
1909 0x0000, 0x8003, /* delete row */
1912 static const WCHAR data7[] = { /* BINARY */
1913 0x0201, 0x8001, 0x0001,
1916 static const char data8[] = /* stream data for the BINARY table */
1917 "naengmyon";
1919 static const WCHAR data9[] = { /* Property */
1920 0x0201, 0x000c, 0x000d,
1923 static const struct {
1924 LPCWSTR name;
1925 const void *data;
1926 DWORD size;
1927 } table_transform_data[] =
1929 { name1, data1, sizeof data1 },
1930 { name2, data2, sizeof data2 },
1931 { name3, data3, sizeof data3 },
1932 { name4, data4, sizeof data4 - 1 },
1933 { name5, data5, sizeof data5 },
1934 { name6, data6, sizeof data6 },
1935 { name7, data7, sizeof data7 },
1936 { name8, data8, sizeof data8 - 1 },
1937 { name9, data9, sizeof data9 },
1940 #define NUM_TRANSFORM_TABLES (sizeof table_transform_data/sizeof table_transform_data[0])
1942 static void generate_transform_manual(void)
1944 IStorage *stg = NULL;
1945 IStream *stm;
1946 WCHAR name[0x20];
1947 HRESULT r;
1948 DWORD i, count;
1949 const DWORD mode = STGM_CREATE|STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE;
1951 const CLSID CLSID_MsiTransform = { 0xc1082,0,0,{0xc0,0,0,0,0,0,0,0x46}};
1953 MultiByteToWideChar(CP_ACP, 0, mstfile, -1, name, 0x20);
1955 r = StgCreateDocfile(name, mode, 0, &stg);
1956 ok(r == S_OK, "failed to create storage\n");
1957 if (!stg)
1958 return;
1960 r = IStorage_SetClass( stg, &CLSID_MsiTransform );
1961 ok(r == S_OK, "failed to set storage type\n");
1963 for (i=0; i<NUM_TRANSFORM_TABLES; i++)
1965 r = IStorage_CreateStream( stg, table_transform_data[i].name,
1966 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
1967 if (FAILED(r))
1969 ok(0, "failed to create stream %08x\n", r);
1970 continue;
1973 r = IStream_Write( stm, table_transform_data[i].data,
1974 table_transform_data[i].size, &count );
1975 if (FAILED(r) || count != table_transform_data[i].size)
1976 ok(0, "failed to write stream\n");
1977 IStream_Release(stm);
1980 IStorage_Release(stg);
1983 static UINT set_summary_info(MSIHANDLE hdb)
1985 UINT res;
1986 MSIHANDLE suminfo;
1988 /* build summmary info */
1989 res = MsiGetSummaryInformation(hdb, NULL, 7, &suminfo);
1990 ok( res == ERROR_SUCCESS , "Failed to open summaryinfo\n" );
1992 res = MsiSummaryInfoSetProperty(suminfo,2, VT_LPSTR, 0,NULL,
1993 "Installation Database");
1994 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
1996 res = MsiSummaryInfoSetProperty(suminfo,3, VT_LPSTR, 0,NULL,
1997 "Installation Database");
1998 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
2000 res = MsiSummaryInfoSetProperty(suminfo,4, VT_LPSTR, 0,NULL,
2001 "Wine Hackers");
2002 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
2004 res = MsiSummaryInfoSetProperty(suminfo,7, VT_LPSTR, 0,NULL,
2005 ";1033");
2006 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
2008 res = MsiSummaryInfoSetProperty(suminfo,9, VT_LPSTR, 0,NULL,
2009 "{913B8D18-FBB6-4CAC-A239-C74C11E3FA74}");
2010 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
2012 res = MsiSummaryInfoSetProperty(suminfo, 14, VT_I4, 100, NULL, NULL);
2013 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
2015 res = MsiSummaryInfoSetProperty(suminfo, 15, VT_I4, 0, NULL, NULL);
2016 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
2018 res = MsiSummaryInfoPersist(suminfo);
2019 ok( res == ERROR_SUCCESS , "Failed to make summary info persist\n" );
2021 res = MsiCloseHandle( suminfo);
2022 ok( res == ERROR_SUCCESS , "Failed to close suminfo\n" );
2024 return res;
2027 static MSIHANDLE create_package_db(LPCSTR filename)
2029 MSIHANDLE hdb = 0;
2030 UINT res;
2032 DeleteFile(msifile);
2034 /* create an empty database */
2035 res = MsiOpenDatabase(filename, MSIDBOPEN_CREATE, &hdb );
2036 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
2037 if( res != ERROR_SUCCESS )
2038 return hdb;
2040 res = MsiDatabaseCommit( hdb );
2041 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
2043 res = set_summary_info(hdb);
2045 res = run_query( hdb, 0,
2046 "CREATE TABLE `Directory` ( "
2047 "`Directory` CHAR(255) NOT NULL, "
2048 "`Directory_Parent` CHAR(255), "
2049 "`DefaultDir` CHAR(255) NOT NULL "
2050 "PRIMARY KEY `Directory`)" );
2051 ok( res == ERROR_SUCCESS , "Failed to create directory table\n" );
2053 return hdb;
2056 static void test_try_transform(void)
2058 MSIHANDLE hdb, hview, hrec, hpkg;
2059 LPCSTR query;
2060 UINT r;
2061 DWORD sz;
2062 char buffer[MAX_PATH];
2064 DeleteFile(msifile);
2065 DeleteFile(msifile2);
2066 DeleteFile(mstfile);
2068 /* create the database */
2069 hdb = create_package_db(msifile2);
2070 ok(hdb, "Failed to create package db\n");
2072 query = "CREATE TABLE `MOO` ( `NOO` SHORT NOT NULL, `OOO` CHAR(255) PRIMARY KEY `NOO`)";
2073 r = run_query(hdb, 0, query);
2074 ok(r == ERROR_SUCCESS, "failed to add table\n");
2076 query = "INSERT INTO `MOO` ( `NOO`, `OOO` ) VALUES ( 1, 'a' )";
2077 r = run_query(hdb, 0, query);
2078 ok(r == ERROR_SUCCESS, "failed to add row\n");
2080 query = "INSERT INTO `MOO` ( `NOO`, `OOO` ) VALUES ( 2, 'b' )";
2081 r = run_query(hdb, 0, query);
2082 ok(r == ERROR_SUCCESS, "failed to add row\n");
2084 query = "INSERT INTO `MOO` ( `NOO`, `OOO` ) VALUES ( 3, 'c' )";
2085 r = run_query(hdb, 0, query);
2086 ok(r == ERROR_SUCCESS, "failed to add row\n");
2088 query = "CREATE TABLE `BINARY` ( `ID` SHORT NOT NULL, `BLOB` OBJECT PRIMARY KEY `ID`)";
2089 r = run_query(hdb, 0, query);
2090 ok(r == ERROR_SUCCESS, "failed to add table\n");
2092 hrec = MsiCreateRecord(2);
2093 r = MsiRecordSetInteger(hrec, 1, 2);
2094 ok(r == ERROR_SUCCESS, "failed to set integer\n");
2096 write_file("testdata.bin", "lamyon", 6);
2097 r = MsiRecordSetStream(hrec, 2, "testdata.bin");
2098 ok(r == ERROR_SUCCESS, "failed to set stream\n");
2100 query = "INSERT INTO `BINARY` ( `ID`, `BLOB` ) VALUES ( ?, ? )";
2101 r = run_query(hdb, hrec, query);
2102 ok(r == ERROR_SUCCESS, "failed to add row with blob\n");
2104 MsiCloseHandle(hrec);
2106 r = MsiDatabaseCommit( hdb );
2107 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
2109 MsiCloseHandle( hdb );
2110 DeleteFileA("testdata.bin");
2113 * Both these generate an equivilent transform,
2114 * but the first doesn't work in Wine yet
2115 * because MsiDatabaseGenerateTransform is unimplemented.
2117 if (0)
2118 generate_transform();
2119 else
2120 generate_transform_manual();
2122 r = MsiOpenDatabase(msifile2, MSIDBOPEN_DIRECT, &hdb );
2123 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
2125 r = MsiDatabaseApplyTransform( hdb, mstfile, 0 );
2126 ok( r == ERROR_SUCCESS, "return code %d, should be ERROR_SUCCESS\n", r );
2128 MsiDatabaseCommit( hdb );
2130 /* check new values */
2131 hrec = 0;
2132 query = "select `BAR`,`CAR` from `AAR` where `BAR` = 1 AND `CAR` = 'vw'";
2133 r = do_query(hdb, query, &hrec);
2134 ok(r == ERROR_SUCCESS, "select query failed\n");
2135 MsiCloseHandle(hrec);
2137 query = "select `BAR`,`CAR` from `AAR` where `BAR` = 2 AND `CAR` = 'bmw'";
2138 hrec = 0;
2139 r = do_query(hdb, query, &hrec);
2140 ok(r == ERROR_SUCCESS, "select query failed\n");
2141 MsiCloseHandle(hrec);
2143 /* check updated values */
2144 hrec = 0;
2145 query = "select `NOO`,`OOO` from `MOO` where `NOO` = 1 AND `OOO` = 'c'";
2146 r = do_query(hdb, query, &hrec);
2147 ok(r == ERROR_SUCCESS, "select query failed\n");
2148 MsiCloseHandle(hrec);
2150 /* check unchanged value */
2151 hrec = 0;
2152 query = "select `NOO`,`OOO` from `MOO` where `NOO` = 2 AND `OOO` = 'b'";
2153 r = do_query(hdb, query, &hrec);
2154 ok(r == ERROR_SUCCESS, "select query failed\n");
2155 MsiCloseHandle(hrec);
2157 /* check deleted value */
2158 hrec = 0;
2159 query = "select * from `MOO` where `NOO` = 3";
2160 r = do_query(hdb, query, &hrec);
2161 ok(r == ERROR_NO_MORE_ITEMS, "select query failed\n");
2162 if (hrec) MsiCloseHandle(hrec);
2164 /* check added stream */
2165 hrec = 0;
2166 query = "select `BLOB` from `BINARY` where `ID` = 1";
2167 r = do_query(hdb, query, &hrec);
2168 ok(r == ERROR_SUCCESS, "select query failed\n");
2170 /* check the contents of the stream */
2171 sz = sizeof buffer;
2172 r = MsiRecordReadStream( hrec, 1, buffer, &sz );
2173 ok(r == ERROR_SUCCESS, "read stream failed\n");
2174 ok(!memcmp(buffer, "naengmyon", 9), "stream data was wrong\n");
2175 ok(sz == 9, "stream data was wrong size\n");
2176 if (hrec) MsiCloseHandle(hrec);
2178 /* check the validity of the table with a deleted row */
2179 hrec = 0;
2180 query = "select * from `MOO`";
2181 r = MsiDatabaseOpenView(hdb, query, &hview);
2182 ok(r == ERROR_SUCCESS, "open view failed\n");
2184 r = MsiViewExecute(hview, 0);
2185 ok(r == ERROR_SUCCESS, "view execute failed\n");
2187 r = MsiViewFetch(hview, &hrec);
2188 ok(r == ERROR_SUCCESS, "view fetch failed\n");
2190 r = MsiRecordGetInteger(hrec, 1);
2191 ok(r == 1, "Expected 1, got %d\n", r);
2193 sz = sizeof buffer;
2194 r = MsiRecordGetString(hrec, 2, buffer, &sz);
2195 ok(r == ERROR_SUCCESS, "record get string failed\n");
2196 ok(!lstrcmpA(buffer, "c"), "Expected c, got %s\n", buffer);
2198 r = MsiRecordGetInteger(hrec, 3);
2199 ok(r == 0x80000000, "Expected 0x80000000, got %d\n", r);
2201 r = MsiRecordGetInteger(hrec, 4);
2202 ok(r == 5, "Expected 5, got %d\n", r);
2204 MsiCloseHandle(hrec);
2206 r = MsiViewFetch(hview, &hrec);
2207 ok(r == ERROR_SUCCESS, "view fetch failed\n");
2209 r = MsiRecordGetInteger(hrec, 1);
2210 ok(r == 2, "Expected 2, got %d\n", r);
2212 sz = sizeof buffer;
2213 r = MsiRecordGetString(hrec, 2, buffer, &sz);
2214 ok(r == ERROR_SUCCESS, "record get string failed\n");
2215 ok(!lstrcmpA(buffer, "b"), "Expected b, got %s\n", buffer);
2217 r = MsiRecordGetInteger(hrec, 3);
2218 ok(r == 0x80000000, "Expected 0x80000000, got %d\n", r);
2220 r = MsiRecordGetInteger(hrec, 4);
2221 ok(r == 0x80000000, "Expected 0x80000000, got %d\n", r);
2223 MsiCloseHandle(hrec);
2225 r = MsiViewFetch(hview, &hrec);
2226 ok(r == ERROR_NO_MORE_ITEMS, "view fetch succeeded\n");
2228 MsiCloseHandle(hrec);
2229 MsiCloseHandle(hview);
2230 MsiCloseHandle(hdb);
2232 /* check that the property was added */
2233 r = MsiOpenPackage(msifile2, &hpkg);
2234 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2236 sz = MAX_PATH;
2237 r = MsiGetProperty(hpkg, "prop", buffer, &sz);
2238 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2239 ok(!lstrcmp(buffer, "val"), "Expected val, got %s\n", buffer);
2241 MsiCloseHandle(hpkg);
2243 DeleteFile(msifile);
2244 DeleteFile(msifile2);
2245 DeleteFile(mstfile);
2248 struct join_res
2250 const CHAR one[MAX_PATH];
2251 const CHAR two[MAX_PATH];
2254 struct join_res_4col
2256 const CHAR one[MAX_PATH];
2257 const CHAR two[MAX_PATH];
2258 const CHAR three[MAX_PATH];
2259 const CHAR four[MAX_PATH];
2262 static const struct join_res join_res_first[] =
2264 { "alveolar", "procerus" },
2265 { "septum", "procerus" },
2266 { "septum", "nasalis" },
2267 { "ramus", "nasalis" },
2268 { "malar", "mentalis" },
2271 static const struct join_res join_res_second[] =
2273 { "nasal", "septum" },
2274 { "mandible", "ramus" },
2277 static const struct join_res join_res_third[] =
2279 { "msvcp.dll", "abcdefgh" },
2280 { "msvcr.dll", "ijklmnop" },
2283 static const struct join_res join_res_fourth[] =
2285 { "msvcp.dll.01234", "single.dll.31415" },
2288 static const struct join_res join_res_fifth[] =
2290 { "malar", "procerus" },
2293 static const struct join_res join_res_sixth[] =
2295 { "malar", "procerus" },
2296 { "malar", "procerus" },
2297 { "malar", "nasalis" },
2298 { "malar", "nasalis" },
2299 { "malar", "nasalis" },
2300 { "malar", "mentalis" },
2303 static const struct join_res join_res_seventh[] =
2305 { "malar", "nasalis" },
2306 { "malar", "nasalis" },
2307 { "malar", "nasalis" },
2310 static const struct join_res_4col join_res_eighth[] =
2312 { "msvcp.dll", "msvcp.dll.01234", "msvcp.dll.01234", "abcdefgh" },
2313 { "msvcr.dll", "msvcr.dll.56789", "msvcp.dll.01234", "abcdefgh" },
2314 { "msvcp.dll", "msvcp.dll.01234", "msvcr.dll.56789", "ijklmnop" },
2315 { "msvcr.dll", "msvcr.dll.56789", "msvcr.dll.56789", "ijklmnop" },
2316 { "msvcp.dll", "msvcp.dll.01234", "single.dll.31415", "msvcp.dll" },
2317 { "msvcr.dll", "msvcr.dll.56789", "single.dll.31415", "msvcp.dll" },
2320 static void test_join(void)
2322 MSIHANDLE hdb, hview, hrec;
2323 LPCSTR query;
2324 CHAR buf[MAX_PATH];
2325 UINT r, count;
2326 DWORD size, i;
2327 BOOL data_correct;
2329 hdb = create_db();
2330 ok( hdb, "failed to create db\n");
2332 r = create_component_table( hdb );
2333 ok( r == ERROR_SUCCESS, "cannot create Component table: %d\n", r );
2335 r = add_component_entry( hdb, "'zygomatic', 'malar', 'INSTALLDIR', 0, '', ''" );
2336 ok( r == ERROR_SUCCESS, "cannot add component: %d\n", r );
2338 r = add_component_entry( hdb, "'maxilla', 'alveolar', 'INSTALLDIR', 0, '', ''" );
2339 ok( r == ERROR_SUCCESS, "cannot add component: %d\n", r );
2341 r = add_component_entry( hdb, "'nasal', 'septum', 'INSTALLDIR', 0, '', ''" );
2342 ok( r == ERROR_SUCCESS, "cannot add component: %d\n", r );
2344 r = add_component_entry( hdb, "'mandible', 'ramus', 'INSTALLDIR', 0, '', ''" );
2345 ok( r == ERROR_SUCCESS, "cannot add component: %d\n", r );
2347 r = create_feature_components_table( hdb );
2348 ok( r == ERROR_SUCCESS, "cannot create FeatureComponents table: %d\n", r );
2350 r = add_feature_components_entry( hdb, "'procerus', 'maxilla'" );
2351 ok( r == ERROR_SUCCESS, "cannot add feature components: %d\n", r );
2353 r = add_feature_components_entry( hdb, "'procerus', 'nasal'" );
2354 ok( r == ERROR_SUCCESS, "cannot add feature components: %d\n", r );
2356 r = add_feature_components_entry( hdb, "'nasalis', 'nasal'" );
2357 ok( r == ERROR_SUCCESS, "cannot add feature components: %d\n", r );
2359 r = add_feature_components_entry( hdb, "'nasalis', 'mandible'" );
2360 ok( r == ERROR_SUCCESS, "cannot add feature components: %d\n", r );
2362 r = add_feature_components_entry( hdb, "'nasalis', 'notacomponent'" );
2363 ok( r == ERROR_SUCCESS, "cannot add feature components: %d\n", r );
2365 r = add_feature_components_entry( hdb, "'mentalis', 'zygomatic'" );
2366 ok( r == ERROR_SUCCESS, "cannot add feature components: %d\n", r );
2368 r = create_std_dlls_table( hdb );
2369 ok( r == ERROR_SUCCESS, "cannot create StdDlls table: %d\n", r );
2371 r = add_std_dlls_entry( hdb, "'msvcp.dll', 'msvcp.dll.01234'" );
2372 ok( r == ERROR_SUCCESS, "cannot add std dlls: %d\n", r );
2374 r = add_std_dlls_entry( hdb, "'msvcr.dll', 'msvcr.dll.56789'" );
2375 ok( r == ERROR_SUCCESS, "cannot add std dlls: %d\n", r );
2377 r = create_binary_table( hdb );
2378 ok( r == ERROR_SUCCESS, "cannot create Binary table: %d\n", r );
2380 r = add_binary_entry( hdb, "'msvcp.dll.01234', 'abcdefgh'" );
2381 ok( r == ERROR_SUCCESS, "cannot add binary: %d\n", r );
2383 r = add_binary_entry( hdb, "'msvcr.dll.56789', 'ijklmnop'" );
2384 ok( r == ERROR_SUCCESS, "cannot add binary: %d\n", r );
2386 r = add_binary_entry( hdb, "'single.dll.31415', 'msvcp.dll'" );
2387 ok( r == ERROR_SUCCESS, "cannot add binary: %d\n", r );
2389 query = "SELECT `Component`.`ComponentId`, `FeatureComponents`.`Feature_` "
2390 "FROM `Component`, `FeatureComponents` "
2391 "WHERE `Component`.`Component` = `FeatureComponents`.`Component_` "
2392 "ORDER BY `Feature_`";
2393 r = MsiDatabaseOpenView(hdb, query, &hview);
2394 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2396 r = MsiViewExecute(hview, 0);
2397 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2399 i = 0;
2400 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2402 count = MsiRecordGetFieldCount( hrec );
2403 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2405 size = MAX_PATH;
2406 r = MsiRecordGetString( hrec, 1, buf, &size );
2407 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2408 ok( !lstrcmp( buf, join_res_first[i].one ),
2409 "For (row %d, column 1) expected '%s', got %s\n", i, join_res_first[i].one, buf );
2411 size = MAX_PATH;
2412 r = MsiRecordGetString( hrec, 2, buf, &size );
2413 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2414 ok( !lstrcmp( buf, join_res_first[i].two ),
2415 "For (row %d, column 2) expected '%s', got %s\n", i, join_res_first[i].two, buf );
2417 i++;
2418 MsiCloseHandle(hrec);
2421 ok( i == 5, "Expected 5 rows, got %d\n", i );
2422 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2424 MsiViewClose(hview);
2425 MsiCloseHandle(hview);
2427 /* try a join without a WHERE condition */
2428 query = "SELECT `Component`.`ComponentId`, `FeatureComponents`.`Feature_` "
2429 "FROM `Component`, `FeatureComponents` ";
2430 r = MsiDatabaseOpenView(hdb, query, &hview);
2431 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2433 r = MsiViewExecute(hview, 0);
2434 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2436 i = 0;
2437 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2439 i++;
2440 MsiCloseHandle(hrec);
2442 ok( i == 24, "Expected 24 rows, got %d\n", i );
2444 MsiViewClose(hview);
2445 MsiCloseHandle(hview);
2447 query = "SELECT DISTINCT Component, ComponentId FROM FeatureComponents, Component "
2448 "WHERE FeatureComponents.Component_=Component.Component "
2449 "AND (Feature_='nasalis') ORDER BY Feature_";
2450 r = MsiDatabaseOpenView(hdb, query, &hview);
2451 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2453 r = MsiViewExecute(hview, 0);
2454 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2456 i = 0;
2457 data_correct = TRUE;
2458 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2460 count = MsiRecordGetFieldCount( hrec );
2461 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2463 size = MAX_PATH;
2464 r = MsiRecordGetString( hrec, 1, buf, &size );
2465 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2466 if( lstrcmp( buf, join_res_second[i].one ))
2467 data_correct = FALSE;
2469 size = MAX_PATH;
2470 r = MsiRecordGetString( hrec, 2, buf, &size );
2471 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2472 if( lstrcmp( buf, join_res_second[i].two ))
2473 data_correct = FALSE;
2475 i++;
2476 MsiCloseHandle(hrec);
2479 ok( data_correct, "data returned in the wrong order\n");
2481 ok( i == 2, "Expected 2 rows, got %d\n", i );
2482 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2484 MsiViewClose(hview);
2485 MsiCloseHandle(hview);
2487 query = "SELECT `StdDlls`.`File`, `Binary`.`Data` "
2488 "FROM `StdDlls`, `Binary` "
2489 "WHERE `StdDlls`.`Binary_` = `Binary`.`Name` "
2490 "ORDER BY `File`";
2491 r = MsiDatabaseOpenView(hdb, query, &hview);
2492 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2494 r = MsiViewExecute(hview, 0);
2495 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2497 i = 0;
2498 data_correct = TRUE;
2499 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2501 count = MsiRecordGetFieldCount( hrec );
2502 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2504 size = MAX_PATH;
2505 r = MsiRecordGetString( hrec, 1, buf, &size );
2506 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2507 if( lstrcmp( buf, join_res_third[i].one ) )
2508 data_correct = FALSE;
2510 size = MAX_PATH;
2511 r = MsiRecordGetString( hrec, 2, buf, &size );
2512 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2513 if( lstrcmp( buf, join_res_third[i].two ) )
2514 data_correct = FALSE;
2516 i++;
2517 MsiCloseHandle(hrec);
2519 ok( data_correct, "data returned in the wrong order\n");
2521 ok( i == 2, "Expected 2 rows, got %d\n", i );
2523 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2525 MsiViewClose(hview);
2526 MsiCloseHandle(hview);
2528 query = "SELECT `StdDlls`.`Binary_`, `Binary`.`Name` "
2529 "FROM `StdDlls`, `Binary` "
2530 "WHERE `StdDlls`.`File` = `Binary`.`Data` "
2531 "ORDER BY `Name`";
2532 r = MsiDatabaseOpenView(hdb, query, &hview);
2533 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2535 r = MsiViewExecute(hview, 0);
2536 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2538 i = 0;
2539 data_correct = TRUE;
2540 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2542 count = MsiRecordGetFieldCount( hrec );
2543 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2545 size = MAX_PATH;
2546 r = MsiRecordGetString( hrec, 1, buf, &size );
2547 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2548 if( lstrcmp( buf, join_res_fourth[i].one ))
2549 data_correct = FALSE;
2551 size = MAX_PATH;
2552 r = MsiRecordGetString( hrec, 2, buf, &size );
2553 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2554 if( lstrcmp( buf, join_res_fourth[i].two ))
2555 data_correct = FALSE;
2557 i++;
2558 MsiCloseHandle(hrec);
2560 ok( data_correct, "data returned in the wrong order\n");
2562 ok( i == 1, "Expected 1 rows, got %d\n", i );
2563 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2565 MsiViewClose(hview);
2566 MsiCloseHandle(hview);
2568 query = "SELECT `Component`.`ComponentId`, `FeatureComponents`.`Feature_` "
2569 "FROM `Component`, `FeatureComponents` "
2570 "WHERE `Component`.`Component` = 'zygomatic' "
2571 "AND `FeatureComponents`.`Component_` = 'maxilla' "
2572 "ORDER BY `Feature_`";
2573 r = MsiDatabaseOpenView(hdb, query, &hview);
2574 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2576 r = MsiViewExecute(hview, 0);
2577 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2579 i = 0;
2580 data_correct = TRUE;
2581 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2583 count = MsiRecordGetFieldCount( hrec );
2584 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2586 size = MAX_PATH;
2587 r = MsiRecordGetString( hrec, 1, buf, &size );
2588 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2589 if( lstrcmp( buf, join_res_fifth[i].one ))
2590 data_correct = FALSE;
2592 size = MAX_PATH;
2593 r = MsiRecordGetString( hrec, 2, buf, &size );
2594 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2595 if( lstrcmp( buf, join_res_fifth[i].two ))
2596 data_correct = FALSE;
2598 i++;
2599 MsiCloseHandle(hrec);
2601 ok( data_correct, "data returned in the wrong order\n");
2603 ok( i == 1, "Expected 1 rows, got %d\n", i );
2604 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2606 MsiViewClose(hview);
2607 MsiCloseHandle(hview);
2609 query = "SELECT `Component`.`ComponentId`, `FeatureComponents`.`Feature_` "
2610 "FROM `Component`, `FeatureComponents` "
2611 "WHERE `Component` = 'zygomatic' "
2612 "ORDER BY `Feature_`";
2613 r = MsiDatabaseOpenView(hdb, query, &hview);
2614 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2616 r = MsiViewExecute(hview, 0);
2617 todo_wine
2619 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2622 i = 0;
2623 data_correct = TRUE;
2624 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2626 count = MsiRecordGetFieldCount( hrec );
2627 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2629 size = MAX_PATH;
2630 r = MsiRecordGetString( hrec, 1, buf, &size );
2631 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2632 if( lstrcmp( buf, join_res_sixth[i].one ))
2633 data_correct = FALSE;
2635 size = MAX_PATH;
2636 r = MsiRecordGetString( hrec, 2, buf, &size );
2637 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2638 if( lstrcmp( buf, join_res_sixth[i].two ))
2639 data_correct = FALSE;
2641 i++;
2642 MsiCloseHandle(hrec);
2644 ok( data_correct, "data returned in the wrong order\n");
2646 todo_wine
2648 ok( i == 6, "Expected 6 rows, got %d\n", i );
2650 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2652 MsiViewClose(hview);
2653 MsiCloseHandle(hview);
2655 query = "SELECT `Component`.`ComponentId`, `FeatureComponents`.`Feature_` "
2656 "FROM `Component`, `FeatureComponents` "
2657 "WHERE `Component` = 'zygomatic' "
2658 "AND `Feature_` = 'nasalis' "
2659 "ORDER BY `Feature_`";
2660 r = MsiDatabaseOpenView(hdb, query, &hview);
2661 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2663 r = MsiViewExecute(hview, 0);
2664 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2666 i = 0;
2667 data_correct = TRUE;
2668 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2670 count = MsiRecordGetFieldCount( hrec );
2671 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2673 size = MAX_PATH;
2674 r = MsiRecordGetString( hrec, 1, buf, &size );
2675 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2676 if( lstrcmp( buf, join_res_seventh[i].one ))
2677 data_correct = FALSE;
2679 size = MAX_PATH;
2680 r = MsiRecordGetString( hrec, 2, buf, &size );
2681 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2682 if( lstrcmp( buf, join_res_seventh[i].two ))
2683 data_correct = FALSE;
2685 i++;
2686 MsiCloseHandle(hrec);
2689 ok( data_correct, "data returned in the wrong order\n");
2690 ok( i == 3, "Expected 3 rows, got %d\n", i );
2691 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2693 MsiViewClose(hview);
2694 MsiCloseHandle(hview);
2696 query = "SELECT `StdDlls`.`File`, `Binary`.`Data` "
2697 "FROM `StdDlls`, `Binary` ";
2698 r = MsiDatabaseOpenView(hdb, query, &hview);
2699 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2701 r = MsiViewExecute(hview, 0);
2702 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2704 i = 0;
2705 data_correct = TRUE;
2706 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2708 count = MsiRecordGetFieldCount( hrec );
2709 ok( count == 2, "Expected 2 record fields, got %d\n", count );
2711 size = MAX_PATH;
2712 r = MsiRecordGetString( hrec, 1, buf, &size );
2713 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2714 if( lstrcmp( buf, join_res_eighth[i].one ))
2715 data_correct = FALSE;
2717 size = MAX_PATH;
2718 r = MsiRecordGetString( hrec, 2, buf, &size );
2719 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2720 if( lstrcmp( buf, join_res_eighth[i].four ))
2721 data_correct = FALSE;
2723 i++;
2724 MsiCloseHandle(hrec);
2727 todo_wine ok( data_correct, "data returned in the wrong order\n");
2728 ok( i == 6, "Expected 6 rows, got %d\n", i );
2729 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2731 MsiViewClose(hview);
2732 MsiCloseHandle(hview);
2734 query = "SELECT * FROM `StdDlls`, `Binary` ";
2735 r = MsiDatabaseOpenView(hdb, query, &hview);
2736 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2738 r = MsiViewExecute(hview, 0);
2739 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2741 i = 0;
2742 data_correct = TRUE;
2743 while ((r = MsiViewFetch(hview, &hrec)) == ERROR_SUCCESS)
2745 count = MsiRecordGetFieldCount( hrec );
2746 ok( count == 4, "Expected 4 record fields, got %d\n", count );
2748 size = MAX_PATH;
2749 r = MsiRecordGetString( hrec, 1, buf, &size );
2750 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2751 if( lstrcmp( buf, join_res_eighth[i].one ))
2752 data_correct = FALSE;
2754 size = MAX_PATH;
2755 r = MsiRecordGetString( hrec, 2, buf, &size );
2756 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2757 if( lstrcmp( buf, join_res_eighth[i].two ))
2758 data_correct = FALSE;
2760 size = MAX_PATH;
2761 r = MsiRecordGetString( hrec, 3, buf, &size );
2762 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2763 if( lstrcmp( buf, join_res_eighth[i].three ))
2764 data_correct = FALSE;
2766 size = MAX_PATH;
2767 r = MsiRecordGetString( hrec, 4, buf, &size );
2768 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2769 if( lstrcmp( buf, join_res_eighth[i].four ))
2770 data_correct = FALSE;
2772 i++;
2773 MsiCloseHandle(hrec);
2775 todo_wine ok( data_correct, "data returned in the wrong order\n");
2777 ok( i == 6, "Expected 6 rows, got %d\n", i );
2778 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2780 MsiViewClose(hview);
2781 MsiCloseHandle(hview);
2783 MsiCloseHandle(hdb);
2784 DeleteFile(msifile);
2787 static void test_temporary_table(void)
2789 MSICONDITION cond;
2790 MSIHANDLE hdb = 0, view = 0, rec;
2791 const char *query;
2792 UINT r;
2793 char buf[0x10];
2794 DWORD sz;
2796 cond = MsiDatabaseIsTablePersistent(0, NULL);
2797 ok( cond == MSICONDITION_ERROR, "wrong return condition\n");
2799 hdb = create_db();
2800 ok( hdb, "failed to create db\n");
2802 cond = MsiDatabaseIsTablePersistent(hdb, NULL);
2803 ok( cond == MSICONDITION_ERROR, "wrong return condition\n");
2805 todo_wine {
2806 cond = MsiDatabaseIsTablePersistent(hdb, "_Tables");
2807 ok( cond == MSICONDITION_NONE, "wrong return condition\n");
2809 cond = MsiDatabaseIsTablePersistent(hdb, "_Columns");
2810 ok( cond == MSICONDITION_NONE, "wrong return condition\n");
2813 cond = MsiDatabaseIsTablePersistent(hdb, "_Streams");
2814 ok( cond == MSICONDITION_NONE, "wrong return condition\n");
2816 query = "CREATE TABLE `P` ( `B` SHORT NOT NULL, `C` CHAR(255) PRIMARY KEY `C`)";
2817 r = run_query(hdb, 0, query);
2818 ok(r == ERROR_SUCCESS, "failed to add table\n");
2820 cond = MsiDatabaseIsTablePersistent(hdb, "P");
2821 ok( cond == MSICONDITION_TRUE, "wrong return condition\n");
2823 query = "CREATE TABLE `P2` ( `B` SHORT NOT NULL, `C` CHAR(255) PRIMARY KEY `C`) HOLD";
2824 r = run_query(hdb, 0, query);
2825 ok(r == ERROR_SUCCESS, "failed to add table\n");
2827 cond = MsiDatabaseIsTablePersistent(hdb, "P2");
2828 ok( cond == MSICONDITION_TRUE, "wrong return condition\n");
2830 query = "CREATE TABLE `T` ( `B` SHORT NOT NULL TEMPORARY, `C` CHAR(255) TEMPORARY PRIMARY KEY `C`) HOLD";
2831 r = run_query(hdb, 0, query);
2832 ok(r == ERROR_SUCCESS, "failed to add table\n");
2834 cond = MsiDatabaseIsTablePersistent(hdb, "T");
2835 ok( cond == MSICONDITION_FALSE, "wrong return condition\n");
2837 query = "CREATE TABLE `T2` ( `B` SHORT NOT NULL TEMPORARY, `C` CHAR(255) TEMPORARY PRIMARY KEY `C`)";
2838 r = run_query(hdb, 0, query);
2839 ok(r == ERROR_SUCCESS, "failed to add table\n");
2841 todo_wine {
2842 cond = MsiDatabaseIsTablePersistent(hdb, "T2");
2843 ok( cond == MSICONDITION_NONE, "wrong return condition\n");
2846 query = "CREATE TABLE `T3` ( `B` SHORT NOT NULL TEMPORARY, `C` CHAR(255) PRIMARY KEY `C`)";
2847 r = run_query(hdb, 0, query);
2848 ok(r == ERROR_SUCCESS, "failed to add table\n");
2850 cond = MsiDatabaseIsTablePersistent(hdb, "T3");
2851 ok( cond == MSICONDITION_TRUE, "wrong return condition\n");
2853 todo_wine {
2854 query = "CREATE TABLE `T4` ( `B` SHORT NOT NULL, `C` CHAR(255) TEMPORARY PRIMARY KEY `C`)";
2855 r = run_query(hdb, 0, query);
2856 ok(r == ERROR_FUNCTION_FAILED, "failed to add table\n");
2858 cond = MsiDatabaseIsTablePersistent(hdb, "T4");
2859 ok( cond == MSICONDITION_NONE, "wrong return condition\n");
2862 query = "CREATE TABLE `T5` ( `B` SHORT NOT NULL TEMP, `C` CHAR(255) TEMP PRIMARY KEY `C`) HOLD";
2863 r = run_query(hdb, 0, query);
2864 ok(r == ERROR_BAD_QUERY_SYNTAX, "failed to add table\n");
2866 query = "select * from `T`";
2867 r = MsiDatabaseOpenView(hdb, query, &view);
2868 ok(r == ERROR_SUCCESS, "failed to query table\n");
2869 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
2870 ok(r == ERROR_SUCCESS, "failed to get column info\n");
2872 sz = sizeof buf;
2873 r = MsiRecordGetString(rec, 1, buf, &sz);
2874 ok(r == ERROR_SUCCESS, "failed to get string\n");
2875 todo_wine ok( 0 == strcmp("G255", buf), "wrong column type\n");
2877 sz = sizeof buf;
2878 r = MsiRecordGetString(rec, 2, buf, &sz);
2879 ok(r == ERROR_SUCCESS, "failed to get string\n");
2880 todo_wine ok( 0 == strcmp("j2", buf), "wrong column type\n");
2882 MsiCloseHandle( rec );
2883 MsiCloseHandle( view );
2885 /* query the table data */
2886 rec = 0;
2887 r = do_query(hdb, "select * from `_Tables` where `Name` = 'T'", &rec);
2888 ok( r == ERROR_SUCCESS, "temporary table exists in _Tables\n");
2889 MsiCloseHandle( rec );
2891 /* query the column data */
2892 rec = 0;
2893 r = do_query(hdb, "select * from `_Columns` where `Table` = 'T' AND `Name` = 'B'", &rec);
2894 ok( r == ERROR_NO_MORE_ITEMS, "temporary table exists in _Columns\n");
2895 if (rec) MsiCloseHandle( rec );
2897 r = do_query(hdb, "select * from `_Columns` where `Table` = 'T' AND `Name` = 'C'", &rec);
2898 ok( r == ERROR_NO_MORE_ITEMS, "temporary table exists in _Columns\n");
2899 if (rec) MsiCloseHandle( rec );
2901 MsiCloseHandle( hdb );
2903 DeleteFile(msifile);
2906 static void test_alter(void)
2908 MSICONDITION cond;
2909 MSIHANDLE hdb = 0;
2910 const char *query;
2911 UINT r;
2913 hdb = create_db();
2914 ok( hdb, "failed to create db\n");
2916 query = "CREATE TABLE `T` ( `B` SHORT NOT NULL TEMPORARY, `C` CHAR(255) TEMPORARY PRIMARY KEY `C`) HOLD";
2917 r = run_query(hdb, 0, query);
2918 ok(r == ERROR_SUCCESS, "failed to add table\n");
2920 cond = MsiDatabaseIsTablePersistent(hdb, "T");
2921 ok( cond == MSICONDITION_FALSE, "wrong return condition\n");
2923 query = "ALTER TABLE `T` HOLD";
2924 r = run_query(hdb, 0, query);
2925 ok(r == ERROR_SUCCESS, "failed to hold table %d\n", r);
2927 query = "ALTER TABLE `T` FREE";
2928 r = run_query(hdb, 0, query);
2929 ok(r == ERROR_SUCCESS, "failed to free table\n");
2931 query = "ALTER TABLE `T` FREE";
2932 r = run_query(hdb, 0, query);
2933 ok(r == ERROR_SUCCESS, "failed to free table\n");
2935 query = "ALTER TABLE `T` FREE";
2936 r = run_query(hdb, 0, query);
2937 ok(r == ERROR_BAD_QUERY_SYNTAX, "failed to free table\n");
2939 query = "ALTER TABLE `T` HOLD";
2940 r = run_query(hdb, 0, query);
2941 ok(r == ERROR_BAD_QUERY_SYNTAX, "failed to hold table %d\n", r);
2943 /* table T is removed */
2944 query = "SELECT * FROM `T`";
2945 r = run_query(hdb, 0, query);
2946 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2948 /* create the table again */
2949 query = "CREATE TABLE `U` ( `A` INTEGER, `B` INTEGER PRIMARY KEY `B`)";
2950 r = run_query(hdb, 0, query);
2951 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2953 /* up the ref count */
2954 query = "ALTER TABLE `U` HOLD";
2955 r = run_query(hdb, 0, query);
2956 ok(r == ERROR_SUCCESS, "failed to free table\n");
2958 /* add column, no data type */
2959 query = "ALTER TABLE `U` ADD `C`";
2960 r = run_query(hdb, 0, query);
2961 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2963 query = "ALTER TABLE `U` ADD `C` INTEGER";
2964 r = run_query(hdb, 0, query);
2965 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2967 /* add column C again */
2968 query = "ALTER TABLE `U` ADD `C` INTEGER";
2969 r = run_query(hdb, 0, query);
2970 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2972 query = "ALTER TABLE `U` ADD `D` INTEGER TEMPORARY";
2973 r = run_query(hdb, 0, query);
2974 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2976 query = "INSERT INTO `U` ( `A`, `B`, `C`, `D` ) VALUES ( 1, 2, 3, 4 )";
2977 r = run_query(hdb, 0, query);
2978 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2980 query = "ALTER TABLE `U` ADD `D` INTEGER TEMPORARY HOLD";
2981 r = run_query(hdb, 0, query);
2982 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2984 query = "INSERT INTO `U` ( `A`, `B`, `C`, `D` ) VALUES ( 5, 6, 7, 8 )";
2985 r = run_query(hdb, 0, query);
2986 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2988 query = "SELECT * FROM `U` WHERE `D` = 8";
2989 r = run_query(hdb, 0, query);
2990 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2992 query = "ALTER TABLE `U` ADD `D` INTEGER TEMPORARY FREE";
2993 r = run_query(hdb, 0, query);
2994 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2996 query = "ALTER COLUMN `D` FREE";
2997 r = run_query(hdb, 0, query);
2998 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3000 /* drop the ref count */
3001 query = "ALTER TABLE `U` FREE";
3002 r = run_query(hdb, 0, query);
3003 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3005 /* table is not empty */
3006 query = "SELECT * FROM `U`";
3007 r = run_query(hdb, 0, query);
3008 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3010 /* column D is removed */
3011 query = "SELECT * FROM `U` WHERE `D` = 8";
3012 r = run_query(hdb, 0, query);
3013 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3015 query = "INSERT INTO `U` ( `A`, `B`, `C`, `D` ) VALUES ( 9, 10, 11, 12 )";
3016 r = run_query(hdb, 0, query);
3017 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3019 /* add the column again */
3020 query = "ALTER TABLE `U` ADD `E` INTEGER TEMPORARY HOLD";
3021 r = run_query(hdb, 0, query);
3022 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3024 /* up the ref count */
3025 query = "ALTER TABLE `U` HOLD";
3026 r = run_query(hdb, 0, query);
3027 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3029 query = "INSERT INTO `U` ( `A`, `B`, `C`, `E` ) VALUES ( 13, 14, 15, 16 )";
3030 r = run_query(hdb, 0, query);
3031 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3033 query = "SELECT * FROM `U` WHERE `E` = 16";
3034 r = run_query(hdb, 0, query);
3035 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3037 /* drop the ref count */
3038 query = "ALTER TABLE `U` FREE";
3039 r = run_query(hdb, 0, query);
3040 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3042 query = "INSERT INTO `U` ( `A`, `B`, `C`, `E` ) VALUES ( 17, 18, 19, 20 )";
3043 r = run_query(hdb, 0, query);
3044 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3046 query = "SELECT * FROM `U` WHERE `E` = 20";
3047 r = run_query(hdb, 0, query);
3048 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3050 /* drop the ref count */
3051 query = "ALTER TABLE `U` FREE";
3052 r = run_query(hdb, 0, query);
3053 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3055 /* table still exists */
3056 query = "SELECT * FROM `U`";
3057 r = run_query(hdb, 0, query);
3058 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3060 /* col E is removed */
3061 query = "SELECT * FROM `U` WHERE `E` = 20";
3062 r = run_query(hdb, 0, query);
3063 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3065 query = "INSERT INTO `U` ( `A`, `B`, `C`, `E` ) VALUES ( 20, 21, 22, 23 )";
3066 r = run_query(hdb, 0, query);
3067 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3069 /* drop the ref count once more */
3070 query = "ALTER TABLE `U` FREE";
3071 r = run_query(hdb, 0, query);
3072 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3074 /* table still exists */
3075 query = "SELECT * FROM `U`";
3076 r = run_query(hdb, 0, query);
3077 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3079 MsiCloseHandle( hdb );
3080 DeleteFile(msifile);
3083 static void test_integers(void)
3085 MSIHANDLE hdb = 0, view = 0, rec = 0;
3086 DWORD count, i;
3087 const char *query;
3088 UINT r;
3090 /* just MsiOpenDatabase should not create a file */
3091 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
3092 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
3094 /* create a table */
3095 query = "CREATE TABLE `integers` ( "
3096 "`one` SHORT, `two` INT, `three` INTEGER, `four` LONG, "
3097 "`five` SHORT NOT NULL, `six` INT NOT NULL, "
3098 "`seven` INTEGER NOT NULL, `eight` LONG NOT NULL "
3099 "PRIMARY KEY `one`)";
3100 r = MsiDatabaseOpenView(hdb, query, &view);
3101 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
3102 r = MsiViewExecute(view, 0);
3103 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
3104 r = MsiViewClose(view);
3105 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3106 r = MsiCloseHandle(view);
3107 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3109 query = "SELECT * FROM `integers`";
3110 r = MsiDatabaseOpenView(hdb, query, &view);
3111 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3113 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
3114 count = MsiRecordGetFieldCount(rec);
3115 ok(count == 8, "Expected 8, got %d\n", count);
3116 ok(check_record(rec, 1, "one"), "Expected one\n");
3117 ok(check_record(rec, 2, "two"), "Expected two\n");
3118 ok(check_record(rec, 3, "three"), "Expected three\n");
3119 ok(check_record(rec, 4, "four"), "Expected four\n");
3120 ok(check_record(rec, 5, "five"), "Expected five\n");
3121 ok(check_record(rec, 6, "six"), "Expected six\n");
3122 ok(check_record(rec, 7, "seven"), "Expected seven\n");
3123 ok(check_record(rec, 8, "eight"), "Expected eight\n");
3124 MsiCloseHandle(rec);
3126 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
3127 count = MsiRecordGetFieldCount(rec);
3128 ok(count == 8, "Expected 8, got %d\n", count);
3129 ok(check_record(rec, 1, "I2"), "Expected I2\n");
3130 ok(check_record(rec, 2, "I2"), "Expected I2\n");
3131 ok(check_record(rec, 3, "I2"), "Expected I2\n");
3132 ok(check_record(rec, 4, "I4"), "Expected I4\n");
3133 ok(check_record(rec, 5, "i2"), "Expected i2\n");
3134 ok(check_record(rec, 6, "i2"), "Expected i2\n");
3135 ok(check_record(rec, 7, "i2"), "Expected i2\n");
3136 ok(check_record(rec, 8, "i4"), "Expected i4\n");
3137 MsiCloseHandle(rec);
3139 MsiViewClose(view);
3140 MsiCloseHandle(view);
3142 /* insert values into it, NULL where NOT NULL is specified */
3143 query = "INSERT INTO `integers` ( `one`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight` )"
3144 "VALUES('', '', '', '', '', '', '', '')";
3145 r = MsiDatabaseOpenView(hdb, query, &view);
3146 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3147 r = MsiViewExecute(view, 0);
3148 ok(r == ERROR_FUNCTION_FAILED, "Expected ERROR_FUNCTION_FAILED, got %d\n", r);
3150 MsiViewClose(view);
3151 MsiCloseHandle(view);
3153 query = "SELECT * FROM `integers`";
3154 r = do_query(hdb, query, &rec);
3155 ok(r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
3157 r = MsiRecordGetFieldCount(rec);
3158 ok(r == -1, "record count wrong: %d\n", r);
3160 MsiCloseHandle(rec);
3162 /* insert legitimate values into it */
3163 query = "INSERT INTO `integers` ( `one`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight` )"
3164 "VALUES('', '2', '', '4', '5', '6', '7', '8')";
3165 r = MsiDatabaseOpenView(hdb, query, &view);
3166 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3167 r = MsiViewExecute(view, 0);
3168 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3170 query = "SELECT * FROM `integers`";
3171 r = do_query(hdb, query, &rec);
3172 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3174 r = MsiRecordGetFieldCount(rec);
3175 ok(r == 8, "record count wrong: %d\n", r);
3177 i = MsiRecordGetInteger(rec, 1);
3178 ok(i == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", i);
3179 i = MsiRecordGetInteger(rec, 3);
3180 ok(i == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", i);
3181 i = MsiRecordGetInteger(rec, 2);
3182 ok(i == 2, "Expected 2, got %d\n", i);
3183 i = MsiRecordGetInteger(rec, 4);
3184 ok(i == 4, "Expected 4, got %d\n", i);
3185 i = MsiRecordGetInteger(rec, 5);
3186 ok(i == 5, "Expected 5, got %d\n", i);
3187 i = MsiRecordGetInteger(rec, 6);
3188 ok(i == 6, "Expected 6, got %d\n", i);
3189 i = MsiRecordGetInteger(rec, 7);
3190 ok(i == 7, "Expected 7, got %d\n", i);
3191 i = MsiRecordGetInteger(rec, 8);
3192 ok(i == 8, "Expected 8, got %d\n", i);
3194 MsiCloseHandle(rec);
3195 MsiViewClose(view);
3196 MsiCloseHandle(view);
3198 r = MsiDatabaseCommit(hdb);
3199 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
3201 r = MsiCloseHandle(hdb);
3202 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3204 r = DeleteFile(msifile);
3205 ok(r == TRUE, "file didn't exist after commit\n");
3208 static void test_update(void)
3210 MSIHANDLE hdb = 0, view = 0, rec = 0;
3211 CHAR result[MAX_PATH];
3212 const char *query;
3213 DWORD size;
3214 UINT r;
3216 /* just MsiOpenDatabase should not create a file */
3217 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
3218 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
3220 /* create the Control table */
3221 query = "CREATE TABLE `Control` ( "
3222 "`Dialog_` CHAR(72) NOT NULL, `Control` CHAR(50) NOT NULL, `Type` SHORT NOT NULL, "
3223 "`X` SHORT NOT NULL, `Y` SHORT NOT NULL, `Width` SHORT NOT NULL, `Height` SHORT NOT NULL,"
3224 "`Attributes` LONG, `Property` CHAR(50), `Text` CHAR(0) LOCALIZABLE, "
3225 "`Control_Next` CHAR(50), `Help` CHAR(50) LOCALIZABLE PRIMARY KEY `Dialog_`, `Control`)";
3226 r = MsiDatabaseOpenView(hdb, query, &view);
3227 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
3228 r = MsiViewExecute(view, 0);
3229 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
3230 r = MsiViewClose(view);
3231 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3232 r = MsiCloseHandle(view);
3233 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3235 /* add a control */
3236 query = "INSERT INTO `Control` ( "
3237 "`Dialog_`, `Control`, `Type`, `X`, `Y`, `Width`, `Height`, "
3238 "`Property`, `Text`, `Control_Next`, `Help` )"
3239 "VALUES('ErrorDialog', 'ErrorText', '1', '5', '5', '5', '5', '', '', '', '')";
3240 r = MsiDatabaseOpenView(hdb, query, &view);
3241 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3242 r = MsiViewExecute(view, 0);
3243 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3244 r = MsiViewClose(view);
3245 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3246 r = MsiCloseHandle(view);
3247 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3249 /* add a second control */
3250 query = "INSERT INTO `Control` ( "
3251 "`Dialog_`, `Control`, `Type`, `X`, `Y`, `Width`, `Height`, "
3252 "`Property`, `Text`, `Control_Next`, `Help` )"
3253 "VALUES('ErrorDialog', 'Button', '1', '5', '5', '5', '5', '', '', '', '')";
3254 r = MsiDatabaseOpenView(hdb, query, &view);
3255 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3256 r = MsiViewExecute(view, 0);
3257 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3258 r = MsiViewClose(view);
3259 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3260 r = MsiCloseHandle(view);
3261 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3263 /* add a third control */
3264 query = "INSERT INTO `Control` ( "
3265 "`Dialog_`, `Control`, `Type`, `X`, `Y`, `Width`, `Height`, "
3266 "`Property`, `Text`, `Control_Next`, `Help` )"
3267 "VALUES('AnotherDialog', 'ErrorText', '1', '5', '5', '5', '5', '', '', '', '')";
3268 r = MsiDatabaseOpenView(hdb, query, &view);
3269 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3270 r = MsiViewExecute(view, 0);
3271 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3272 r = MsiViewClose(view);
3273 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3274 r = MsiCloseHandle(view);
3275 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3277 /* bad table */
3278 query = "UPDATE `NotATable` SET `Text` = 'this is text' WHERE `Dialog_` = 'ErrorDialog'";
3279 r = MsiDatabaseOpenView(hdb, query, &view);
3280 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3282 /* bad set column */
3283 query = "UPDATE `Control` SET `NotAColumn` = 'this is text' WHERE `Dialog_` = 'ErrorDialog'";
3284 r = MsiDatabaseOpenView(hdb, query, &view);
3285 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3287 /* bad where condition */
3288 query = "UPDATE `Control` SET `Text` = 'this is text' WHERE `NotAColumn` = 'ErrorDialog'";
3289 r = MsiDatabaseOpenView(hdb, query, &view);
3290 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
3292 /* just the dialog_ specified */
3293 query = "UPDATE `Control` SET `Text` = 'this is text' WHERE `Dialog_` = 'ErrorDialog'";
3294 r = MsiDatabaseOpenView(hdb, query, &view);
3295 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3296 r = MsiViewExecute(view, 0);
3297 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3298 r = MsiViewClose(view);
3299 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3300 r = MsiCloseHandle(view);
3301 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3303 /* check the modified text */
3304 query = "SELECT `Text` FROM `Control` WHERE `Control` = 'ErrorText'";
3305 r = MsiDatabaseOpenView(hdb, query, &view);
3306 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3307 r = MsiViewExecute(view, 0);
3308 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3310 r = MsiViewFetch(view, &rec);
3311 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3313 size = MAX_PATH;
3314 r = MsiRecordGetString(rec, 1, result, &size);
3315 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3316 ok(!lstrcmp(result, "this is text"), "Expected `this is text`, got %s\n", result);
3318 MsiCloseHandle(rec);
3320 r = MsiViewFetch(view, &rec);
3321 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3323 size = MAX_PATH;
3324 r = MsiRecordGetString(rec, 1, result, &size);
3325 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3326 ok(!lstrlen(result), "Expected an empty string, got %s\n", result);
3328 MsiCloseHandle(rec);
3330 r = MsiViewFetch(view, &rec);
3331 ok(r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
3333 r = MsiViewClose(view);
3334 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3335 r = MsiCloseHandle(view);
3336 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3338 /* dialog_ and control specified */
3339 query = "UPDATE `Control` SET `Text` = 'this is text' WHERE `Dialog_` = 'ErrorDialog' AND `Control` = 'ErrorText'";
3340 r = MsiDatabaseOpenView(hdb, query, &view);
3341 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3342 r = MsiViewExecute(view, 0);
3343 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3344 r = MsiViewClose(view);
3345 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3346 r = MsiCloseHandle(view);
3347 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3349 /* check the modified text */
3350 query = "SELECT `Text` FROM `Control` WHERE `Control` = 'ErrorText'";
3351 r = MsiDatabaseOpenView(hdb, query, &view);
3352 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3353 r = MsiViewExecute(view, 0);
3354 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3356 r = MsiViewFetch(view, &rec);
3357 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3359 size = MAX_PATH;
3360 r = MsiRecordGetString(rec, 1, result, &size);
3361 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3362 ok(!lstrcmp(result, "this is text"), "Expected `this is text`, got %s\n", result);
3364 MsiCloseHandle(rec);
3366 r = MsiViewFetch(view, &rec);
3367 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3369 size = MAX_PATH;
3370 r = MsiRecordGetString(rec, 1, result, &size);
3371 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3372 ok(!lstrlen(result), "Expected an empty string, got %s\n", result);
3374 MsiCloseHandle(rec);
3376 r = MsiViewFetch(view, &rec);
3377 ok(r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
3379 r = MsiViewClose(view);
3380 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3381 r = MsiCloseHandle(view);
3382 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3384 /* no where condition */
3385 query = "UPDATE `Control` SET `Text` = 'this is text'";
3386 r = MsiDatabaseOpenView(hdb, query, &view);
3387 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3388 r = MsiViewExecute(view, 0);
3389 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3390 r = MsiViewClose(view);
3391 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3392 r = MsiCloseHandle(view);
3393 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3395 /* check the modified text */
3396 query = "SELECT `Text` FROM `Control`";
3397 r = MsiDatabaseOpenView(hdb, query, &view);
3398 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3399 r = MsiViewExecute(view, 0);
3400 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3402 r = MsiViewFetch(view, &rec);
3403 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3405 size = MAX_PATH;
3406 r = MsiRecordGetString(rec, 1, result, &size);
3407 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3408 ok(!lstrcmp(result, "this is text"), "Expected `this is text`, got %s\n", result);
3410 MsiCloseHandle(rec);
3412 r = MsiViewFetch(view, &rec);
3413 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3415 size = MAX_PATH;
3416 r = MsiRecordGetString(rec, 1, result, &size);
3417 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3418 ok(!lstrcmp(result, "this is text"), "Expected `this is text`, got %s\n", result);
3420 MsiCloseHandle(rec);
3422 r = MsiViewFetch(view, &rec);
3423 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3425 size = MAX_PATH;
3426 r = MsiRecordGetString(rec, 1, result, &size);
3427 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3428 ok(!lstrcmp(result, "this is text"), "Expected `this is text`, got %s\n", result);
3430 MsiCloseHandle(rec);
3432 r = MsiViewFetch(view, &rec);
3433 ok(r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
3435 r = MsiViewClose(view);
3436 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3437 r = MsiCloseHandle(view);
3438 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3439 r = MsiDatabaseCommit(hdb);
3440 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
3441 r = MsiCloseHandle(hdb);
3442 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3444 DeleteFile(msifile);
3447 static void test_special_tables(void)
3449 const char *query;
3450 MSIHANDLE hdb = 0;
3451 UINT r;
3453 r = MsiOpenDatabase(msifile, MSIDBOPEN_CREATE, &hdb);
3454 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
3456 query = "CREATE TABLE `_Properties` ( "
3457 "`foo` INT NOT NULL, `bar` INT LOCALIZABLE PRIMARY KEY `foo`)";
3458 r = run_query(hdb, 0, query);
3459 ok(r == ERROR_SUCCESS, "failed to create table\n");
3461 query = "CREATE TABLE `_Streams` ( "
3462 "`foo` INT NOT NULL, `bar` INT LOCALIZABLE PRIMARY KEY `foo`)";
3463 r = run_query(hdb, 0, query);
3464 todo_wine ok(r == ERROR_BAD_QUERY_SYNTAX, "created _Streams table\n");
3466 query = "CREATE TABLE `_Tables` ( "
3467 "`foo` INT NOT NULL, `bar` INT LOCALIZABLE PRIMARY KEY `foo`)";
3468 r = run_query(hdb, 0, query);
3469 ok(r == ERROR_BAD_QUERY_SYNTAX, "created _Tables table\n");
3471 query = "CREATE TABLE `_Columns` ( "
3472 "`foo` INT NOT NULL, `bar` INT LOCALIZABLE PRIMARY KEY `foo`)";
3473 r = run_query(hdb, 0, query);
3474 ok(r == ERROR_BAD_QUERY_SYNTAX, "created _Columns table\n");
3476 r = MsiCloseHandle(hdb);
3477 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3480 static void test_select_markers(void)
3482 MSIHANDLE hdb = 0, rec, view, res;
3483 LPCSTR query;
3484 UINT r;
3485 DWORD size;
3486 CHAR buf[MAX_PATH];
3488 hdb = create_db();
3489 ok( hdb, "failed to create db\n");
3491 r = run_query(hdb, 0,
3492 "CREATE TABLE `Table` (`One` CHAR(72), `Two` CHAR(72), `Three` SHORT PRIMARY KEY `One`, `Two`, `Three`)");
3493 ok(r == S_OK, "cannot create table: %d\n", r);
3495 r = run_query(hdb, 0, "INSERT INTO `Table` "
3496 "( `One`, `Two`, `Three` ) VALUES ( 'apple', 'one', 1 )");
3497 ok(r == S_OK, "cannot add file to the Media table: %d\n", r);
3499 r = run_query(hdb, 0, "INSERT INTO `Table` "
3500 "( `One`, `Two`, `Three` ) VALUES ( 'apple', 'two', 1 )");
3501 ok(r == S_OK, "cannot add file to the Media table: %d\n", r);
3503 r = run_query(hdb, 0, "INSERT INTO `Table` "
3504 "( `One`, `Two`, `Three` ) VALUES ( 'apple', 'two', 2 )");
3505 ok(r == S_OK, "cannot add file to the Media table: %d\n", r);
3507 r = run_query(hdb, 0, "INSERT INTO `Table` "
3508 "( `One`, `Two`, `Three` ) VALUES ( 'banana', 'three', 3 )");
3509 ok(r == S_OK, "cannot add file to the Media table: %d\n", r);
3511 rec = MsiCreateRecord(2);
3512 MsiRecordSetString(rec, 1, "apple");
3513 MsiRecordSetString(rec, 2, "two");
3515 query = "SELECT * FROM `Table` WHERE `One`=? AND `Two`=? ORDER BY `Three`";
3516 r = MsiDatabaseOpenView(hdb, query, &view);
3517 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3519 r = MsiViewExecute(view, rec);
3520 todo_wine
3522 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3525 r = MsiViewFetch(view, &res);
3526 todo_wine
3528 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3531 size = MAX_PATH;
3532 r = MsiRecordGetString(res, 1, buf, &size);
3533 todo_wine
3535 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3536 ok(!lstrcmp(buf, "apple"), "Expected apple, got %s\n", buf);
3539 size = MAX_PATH;
3540 r = MsiRecordGetString(res, 2, buf, &size);
3541 todo_wine
3543 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3544 ok(!lstrcmp(buf, "two"), "Expected two, got %s\n", buf);
3547 r = MsiRecordGetInteger(res, 3);
3548 todo_wine
3550 ok(r == 1, "Expected 1, got %d\n", r);
3553 MsiCloseHandle(res);
3555 r = MsiViewFetch(view, &res);
3556 todo_wine
3558 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3561 size = MAX_PATH;
3562 r = MsiRecordGetString(res, 1, buf, &size);
3563 todo_wine
3565 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3566 ok(!lstrcmp(buf, "apple"), "Expected apple, got %s\n", buf);
3569 size = MAX_PATH;
3570 r = MsiRecordGetString(res, 2, buf, &size);
3571 todo_wine
3573 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
3574 ok(!lstrcmp(buf, "two"), "Expected two, got %s\n", buf);
3577 r = MsiRecordGetInteger(res, 3);
3578 todo_wine
3580 ok(r == 2, "Expected 2, got %d\n", r);
3583 MsiCloseHandle(res);
3585 r = MsiViewFetch(view, &res);
3586 ok(r == ERROR_NO_MORE_ITEMS, "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
3588 r = MsiViewClose(view);
3589 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
3590 r = MsiCloseHandle(view);
3591 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3592 r = MsiCloseHandle(hdb);
3593 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
3595 DeleteFile(msifile);
3598 START_TEST(db)
3600 test_msidatabase();
3601 test_msiinsert();
3602 test_msidecomposedesc();
3603 test_msibadqueries();
3604 test_viewmodify();
3605 test_viewgetcolumninfo();
3606 test_getcolinfo();
3607 test_msiexport();
3608 test_longstrings();
3609 test_streamtable();
3610 test_where();
3611 test_msiimport();
3612 test_markers();
3613 test_handle_limit();
3614 test_try_transform();
3615 test_join();
3616 test_temporary_table();
3617 test_alter();
3618 test_integers();
3619 test_update();
3620 test_special_tables();
3621 test_select_markers();