9c1fcbad39165790395ad6562f41263cac17f1d2
[reactos.git] / rostests / winetests / msi / db.c
1 /*
2 * Copyright (C) 2005 Mike McCormack for CodeWeavers
3 *
4 * A test program for MSI database files.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #define COBJMACROS
22
23 #include <stdio.h>
24
25 #include <windows.h>
26 #include <objidl.h>
27 #include <msi.h>
28 #include <msidefs.h>
29 #include <msiquery.h>
30
31 #include "wine/test.h"
32
33 static const char *msifile = "winetest-db.msi";
34 static const char *msifile2 = "winetst2-db.msi";
35 static const char *mstfile = "winetst-db.mst";
36 static const WCHAR msifileW[] = {'w','i','n','e','t','e','s','t','-','d','b','.','m','s','i',0};
37 static const WCHAR msifile2W[] = {'w','i','n','e','t','s','t','2','-','d','b','.','m','s','i',0};
38
39 static void test_msidatabase(void)
40 {
41 MSIHANDLE hdb = 0, hdb2 = 0;
42 UINT res;
43
44 DeleteFileW(msifileW);
45
46 res = MsiOpenDatabaseW( msifileW, msifile2W, &hdb );
47 ok( res == ERROR_OPEN_FAILED, "expected failure\n");
48
49 res = MsiOpenDatabaseW( msifileW, (LPWSTR)0xff, &hdb );
50 ok( res == ERROR_INVALID_PARAMETER, "expected failure\n");
51
52 res = MsiCloseHandle( hdb );
53 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
54
55 /* create an empty database */
56 res = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb );
57 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
58
59 res = MsiDatabaseCommit( hdb );
60 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
61
62 ok( GetFileAttributesA( msifile ) != INVALID_FILE_ATTRIBUTES, "database should exist\n");
63
64 res = MsiCloseHandle( hdb );
65 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
66 res = MsiOpenDatabaseW( msifileW, msifile2W, &hdb2 );
67 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
68
69 ok( GetFileAttributesA( msifile2 ) != INVALID_FILE_ATTRIBUTES, "database should exist\n");
70
71 res = MsiDatabaseCommit( hdb2 );
72 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
73
74 res = MsiCloseHandle( hdb2 );
75 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
76
77 res = MsiOpenDatabaseW( msifileW, msifile2W, &hdb2 );
78 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
79
80 res = MsiCloseHandle( hdb2 );
81 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
82
83 ok( GetFileAttributesA( msifile2 ) == INVALID_FILE_ATTRIBUTES, "uncommitted database should not exist\n");
84
85 res = MsiOpenDatabaseW( msifileW, msifile2W, &hdb2 );
86 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
87
88 res = MsiDatabaseCommit( hdb2 );
89 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
90
91 res = MsiCloseHandle( hdb2 );
92 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
93
94 ok( GetFileAttributesA( msifile2 ) != INVALID_FILE_ATTRIBUTES, "committed database should exist\n");
95
96 res = MsiOpenDatabaseW( msifileW, MSIDBOPEN_READONLY, &hdb );
97 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
98
99 res = MsiDatabaseCommit( hdb );
100 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
101
102 res = MsiCloseHandle( hdb );
103 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
104
105 res = MsiOpenDatabaseW( msifileW, MSIDBOPEN_DIRECT, &hdb );
106 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
107
108 res = MsiCloseHandle( hdb );
109 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
110
111 res = MsiOpenDatabaseW( msifileW, MSIDBOPEN_TRANSACT, &hdb );
112 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
113
114 res = MsiCloseHandle( hdb );
115 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
116 ok( GetFileAttributesA( msifile ) != INVALID_FILE_ATTRIBUTES, "database should exist\n");
117
118 /* MSIDBOPEN_CREATE deletes the database if MsiCommitDatabase isn't called */
119 res = MsiOpenDatabaseW( msifileW, MSIDBOPEN_CREATE, &hdb );
120 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
121
122 ok( GetFileAttributesA( msifile ) != INVALID_FILE_ATTRIBUTES, "database should exist\n");
123
124 res = MsiCloseHandle( hdb );
125 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
126
127 ok( GetFileAttributesA( msifile ) == INVALID_FILE_ATTRIBUTES, "database should exist\n");
128
129 res = MsiOpenDatabaseW( msifileW, MSIDBOPEN_CREATE, &hdb );
130 ok( res == ERROR_SUCCESS , "Failed to open database\n" );
131
132 res = MsiDatabaseCommit( hdb );
133 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
134
135 ok( GetFileAttributesA( msifile ) != INVALID_FILE_ATTRIBUTES, "database should exist\n");
136
137 res = MsiCloseHandle( hdb );
138 ok( res == ERROR_SUCCESS , "Failed to close database\n" );
139
140 res = DeleteFileA( msifile2 );
141 ok( res == TRUE, "Failed to delete database\n" );
142
143 res = DeleteFileA( msifile );
144 ok( res == TRUE, "Failed to delete database\n" );
145 }
146
147 static UINT do_query(MSIHANDLE hdb, const char *query, MSIHANDLE *phrec)
148 {
149 MSIHANDLE hview = 0;
150 UINT r, ret;
151
152 if (phrec)
153 *phrec = 0;
154
155 /* open a select query */
156 r = MsiDatabaseOpenViewA(hdb, query, &hview);
157 if (r != ERROR_SUCCESS)
158 return r;
159 r = MsiViewExecute(hview, 0);
160 if (r != ERROR_SUCCESS)
161 return r;
162 ret = MsiViewFetch(hview, phrec);
163 r = MsiViewClose(hview);
164 if (r != ERROR_SUCCESS)
165 return r;
166 r = MsiCloseHandle(hview);
167 if (r != ERROR_SUCCESS)
168 return r;
169 return ret;
170 }
171
172 static UINT run_query( MSIHANDLE hdb, MSIHANDLE hrec, const char *query )
173 {
174 MSIHANDLE hview = 0;
175 UINT r;
176
177 r = MsiDatabaseOpenViewA(hdb, query, &hview);
178 if( r != ERROR_SUCCESS )
179 return r;
180
181 r = MsiViewExecute(hview, hrec);
182 if( r == ERROR_SUCCESS )
183 r = MsiViewClose(hview);
184 MsiCloseHandle(hview);
185 return r;
186 }
187
188 static UINT run_queryW( MSIHANDLE hdb, MSIHANDLE hrec, const WCHAR *query )
189 {
190 MSIHANDLE hview = 0;
191 UINT r;
192
193 r = MsiDatabaseOpenViewW(hdb, query, &hview);
194 if( r != ERROR_SUCCESS )
195 return r;
196
197 r = MsiViewExecute(hview, hrec);
198 if( r == ERROR_SUCCESS )
199 r = MsiViewClose(hview);
200 MsiCloseHandle(hview);
201 return r;
202 }
203
204 static UINT create_component_table( MSIHANDLE hdb )
205 {
206 return run_query( hdb, 0,
207 "CREATE TABLE `Component` ( "
208 "`Component` CHAR(72) NOT NULL, "
209 "`ComponentId` CHAR(38), "
210 "`Directory_` CHAR(72) NOT NULL, "
211 "`Attributes` SHORT NOT NULL, "
212 "`Condition` CHAR(255), "
213 "`KeyPath` CHAR(72) "
214 "PRIMARY KEY `Component`)" );
215 }
216
217 static UINT create_custom_action_table( MSIHANDLE hdb )
218 {
219 return run_query( hdb, 0,
220 "CREATE TABLE `CustomAction` ( "
221 "`Action` CHAR(72) NOT NULL, "
222 "`Type` SHORT NOT NULL, "
223 "`Source` CHAR(72), "
224 "`Target` CHAR(255) "
225 "PRIMARY KEY `Action`)" );
226 }
227
228 static UINT create_directory_table( MSIHANDLE hdb )
229 {
230 return run_query( hdb, 0,
231 "CREATE TABLE `Directory` ( "
232 "`Directory` CHAR(255) NOT NULL, "
233 "`Directory_Parent` CHAR(255), "
234 "`DefaultDir` CHAR(255) NOT NULL "
235 "PRIMARY KEY `Directory`)" );
236 }
237
238 static UINT create_feature_components_table( MSIHANDLE hdb )
239 {
240 return run_query( hdb, 0,
241 "CREATE TABLE `FeatureComponents` ( "
242 "`Feature_` CHAR(38) NOT NULL, "
243 "`Component_` CHAR(72) NOT NULL "
244 "PRIMARY KEY `Feature_`, `Component_` )" );
245 }
246
247 static UINT create_std_dlls_table( MSIHANDLE hdb )
248 {
249 return run_query( hdb, 0,
250 "CREATE TABLE `StdDlls` ( "
251 "`File` CHAR(255) NOT NULL, "
252 "`Binary_` CHAR(72) NOT NULL "
253 "PRIMARY KEY `File` )" );
254 }
255
256 static UINT create_binary_table( MSIHANDLE hdb )
257 {
258 return run_query( hdb, 0,
259 "CREATE TABLE `Binary` ( "
260 "`Name` CHAR(72) NOT NULL, "
261 "`Data` CHAR(72) NOT NULL "
262 "PRIMARY KEY `Name` )" );
263 }
264
265 #define make_add_entry(type, qtext) \
266 static UINT add##_##type##_##entry( MSIHANDLE hdb, const char *values ) \
267 { \
268 char insert[] = qtext; \
269 char *query; \
270 UINT sz, r; \
271 sz = strlen(values) + sizeof insert; \
272 query = HeapAlloc(GetProcessHeap(),0,sz); \
273 sprintf(query,insert,values); \
274 r = run_query( hdb, 0, query ); \
275 HeapFree(GetProcessHeap(), 0, query); \
276 return r; \
277 }
278
279 make_add_entry(component,
280 "INSERT INTO `Component` "
281 "(`Component`, `ComponentId`, `Directory_`, "
282 "`Attributes`, `Condition`, `KeyPath`) VALUES( %s )")
283
284 make_add_entry(custom_action,
285 "INSERT INTO `CustomAction` "
286 "(`Action`, `Type`, `Source`, `Target`) VALUES( %s )")
287
288 make_add_entry(feature_components,
289 "INSERT INTO `FeatureComponents` "
290 "(`Feature_`, `Component_`) VALUES( %s )")
291
292 make_add_entry(std_dlls,
293 "INSERT INTO `StdDlls` (`File`, `Binary_`) VALUES( %s )")
294
295 make_add_entry(binary,
296 "INSERT INTO `Binary` (`Name`, `Data`) VALUES( %s )")
297
298 static void test_msiinsert(void)
299 {
300 MSIHANDLE hdb = 0, hview = 0, hview2 = 0, hrec = 0;
301 UINT r;
302 const char *query;
303 char buf[80];
304 DWORD sz;
305
306 DeleteFileA(msifile);
307
308 /* just MsiOpenDatabase should not create a file */
309 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
310 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
311
312 /* create a table */
313 query = "CREATE TABLE `phone` ( "
314 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
315 "PRIMARY KEY `id`)";
316 r = MsiDatabaseOpenViewA(hdb, query, &hview);
317 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
318 r = MsiViewExecute(hview, 0);
319 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
320 r = MsiViewClose(hview);
321 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
322 r = MsiCloseHandle(hview);
323 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
324
325 query = "SELECT * FROM phone WHERE number = '8675309'";
326 r = MsiDatabaseOpenViewA(hdb, query, &hview2);
327 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
328 r = MsiViewExecute(hview2, 0);
329 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
330 r = MsiViewFetch(hview2, &hrec);
331 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch produced items\n");
332
333 /* insert a value into it */
334 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
335 "VALUES('1', 'Abe', '8675309')";
336 r = MsiDatabaseOpenViewA(hdb, query, &hview);
337 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
338 r = MsiViewExecute(hview, 0);
339 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
340 r = MsiViewClose(hview);
341 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
342 r = MsiCloseHandle(hview);
343 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
344
345 r = MsiViewFetch(hview2, &hrec);
346 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch produced items\n");
347 r = MsiViewExecute(hview2, 0);
348 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
349 r = MsiViewFetch(hview2, &hrec);
350 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %u\n", r);
351
352 r = MsiCloseHandle(hrec);
353 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
354 r = MsiViewClose(hview2);
355 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
356 r = MsiCloseHandle(hview2);
357 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
358
359 query = "SELECT * FROM `phone` WHERE `id` = 1";
360 r = do_query(hdb, query, &hrec);
361 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
362
363 /* check the record contains what we put in it */
364 r = MsiRecordGetFieldCount(hrec);
365 ok(r == 3, "record count wrong\n");
366
367 r = MsiRecordIsNull(hrec, 0);
368 ok(r == FALSE, "field 0 not null\n");
369
370 r = MsiRecordGetInteger(hrec, 1);
371 ok(r == 1, "field 1 contents wrong\n");
372 sz = sizeof buf;
373 r = MsiRecordGetStringA(hrec, 2, buf, &sz);
374 ok(r == ERROR_SUCCESS, "field 2 content fetch failed\n");
375 ok(!strcmp(buf,"Abe"), "field 2 content incorrect\n");
376 sz = sizeof buf;
377 r = MsiRecordGetStringA(hrec, 3, buf, &sz);
378 ok(r == ERROR_SUCCESS, "field 3 content fetch failed\n");
379 ok(!strcmp(buf,"8675309"), "field 3 content incorrect\n");
380
381 r = MsiCloseHandle(hrec);
382 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
383
384 /* open a select query */
385 hrec = 100;
386 query = "SELECT * FROM `phone` WHERE `id` >= 10";
387 r = do_query(hdb, query, &hrec);
388 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
389 ok(hrec == 0, "hrec should be null\n");
390
391 r = MsiCloseHandle(hrec);
392 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
393
394 query = "SELECT * FROM `phone` WHERE `id` < 0";
395 r = do_query(hdb, query, &hrec);
396 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
397
398 query = "SELECT * FROM `phone` WHERE `id` <= 0";
399 r = do_query(hdb, query, &hrec);
400 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
401
402 query = "SELECT * FROM `phone` WHERE `id` <> 1";
403 r = do_query(hdb, query, &hrec);
404 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
405
406 query = "SELECT * FROM `phone` WHERE `id` > 10";
407 r = do_query(hdb, query, &hrec);
408 ok(r == ERROR_NO_MORE_ITEMS, "MsiViewFetch failed\n");
409
410 /* now try a few bad INSERT xqueries */
411 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
412 "VALUES(?, ?)";
413 r = MsiDatabaseOpenViewA(hdb, query, &hview);
414 ok(r == ERROR_BAD_QUERY_SYNTAX, "MsiDatabaseOpenView failed\n");
415
416 /* construct a record to insert */
417 hrec = MsiCreateRecord(4);
418 r = MsiRecordSetInteger(hrec, 1, 2);
419 ok(r == ERROR_SUCCESS, "MsiRecordSetInteger failed\n");
420 r = MsiRecordSetStringA(hrec, 2, "Adam");
421 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
422 r = MsiRecordSetStringA(hrec, 3, "96905305");
423 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
424
425 /* insert another value, using a record and wildcards */
426 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
427 "VALUES(?, ?, ?)";
428 r = MsiDatabaseOpenViewA(hdb, query, &hview);
429 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
430
431 if (r == ERROR_SUCCESS)
432 {
433 r = MsiViewExecute(hview, hrec);
434 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
435 r = MsiViewClose(hview);
436 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
437 r = MsiCloseHandle(hview);
438 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
439 }
440 r = MsiCloseHandle(hrec);
441 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
442
443 r = MsiViewFetch(0, NULL);
444 ok(r == ERROR_INVALID_PARAMETER, "MsiViewFetch failed\n");
445
446 r = MsiDatabaseCommit(hdb);
447 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
448
449 r = MsiCloseHandle(hdb);
450 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
451
452 r = DeleteFileA(msifile);
453 ok(r == TRUE, "file didn't exist after commit\n");
454 }
455
456 static void test_msidecomposedesc(void)
457 {
458 UINT (WINAPI *pMsiDecomposeDescriptorA)(LPCSTR, LPCSTR, LPSTR, LPSTR, DWORD *);
459 char prod[MAX_FEATURE_CHARS+1], comp[MAX_FEATURE_CHARS+1], feature[MAX_FEATURE_CHARS+1];
460 const char *desc;
461 UINT r;
462 DWORD len;
463 HMODULE hmod;
464
465 hmod = GetModuleHandleA("msi.dll");
466 pMsiDecomposeDescriptorA = (void*)GetProcAddress(hmod, "MsiDecomposeDescriptorA");
467 if (!pMsiDecomposeDescriptorA)
468 return;
469
470 /* test a valid feature descriptor */
471 desc = "']gAVn-}f(ZXfeAR6.jiFollowTheWhiteRabbit>3w2x^IGfe?CxI5heAvk.";
472 len = 0;
473 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
474 ok(r == ERROR_SUCCESS, "returned an error\n");
475 ok(len == strlen(desc), "length was wrong\n");
476 ok(strcmp(prod,"{90110409-6000-11D3-8CFE-0150048383C9}")==0, "product wrong\n");
477 ok(strcmp(feature,"FollowTheWhiteRabbit")==0, "feature wrong\n");
478 ok(strcmp(comp,"{A7CD68DB-EF74-49C8-FBB2-A7C463B2AC24}")==0,"component wrong\n");
479
480 /* test an invalid feature descriptor with too many characters */
481 desc = "']gAVn-}f(ZXfeAR6.ji"
482 "ThisWillFailIfTheresMoreThanAGuidsChars>"
483 "3w2x^IGfe?CxI5heAvk.";
484 len = 0;
485 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
486 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
487
488 /*
489 * Test a valid feature descriptor with the
490 * maximum number of characters and some trailing characters.
491 */
492 desc = "']gAVn-}f(ZXfeAR6.ji"
493 "ThisWillWorkIfTheresLTEThanAGuidsChars>"
494 "3w2x^IGfe?CxI5heAvk."
495 "extra";
496 len = 0;
497 r = pMsiDecomposeDescriptorA(desc, prod, feature, comp, &len);
498 ok(r == ERROR_SUCCESS, "returned wrong error\n");
499 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
500
501 len = 0;
502 r = pMsiDecomposeDescriptorA(desc, prod, feature, NULL, &len);
503 ok(r == ERROR_SUCCESS, "returned wrong error\n");
504 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
505
506 len = 0;
507 r = pMsiDecomposeDescriptorA(desc, prod, NULL, NULL, &len);
508 ok(r == ERROR_SUCCESS, "returned wrong error\n");
509 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
510
511 len = 0;
512 r = pMsiDecomposeDescriptorA(desc, NULL, NULL, NULL, &len);
513 ok(r == ERROR_SUCCESS, "returned wrong error\n");
514 ok(len == (strlen(desc) - strlen("extra")), "length wrong\n");
515
516 len = 0;
517 r = pMsiDecomposeDescriptorA(NULL, NULL, NULL, NULL, &len);
518 ok(r == ERROR_INVALID_PARAMETER, "returned wrong error\n");
519 ok(len == 0, "length wrong\n");
520
521 r = pMsiDecomposeDescriptorA(desc, NULL, NULL, NULL, NULL);
522 ok(r == ERROR_SUCCESS, "returned wrong error\n");
523 }
524
525 static UINT try_query_param( MSIHANDLE hdb, LPCSTR szQuery, MSIHANDLE hrec )
526 {
527 MSIHANDLE htab = 0;
528 UINT res;
529
530 res = MsiDatabaseOpenViewA( hdb, szQuery, &htab );
531 if(res == ERROR_SUCCESS )
532 {
533 UINT r;
534
535 r = MsiViewExecute( htab, hrec );
536 if(r != ERROR_SUCCESS )
537 res = r;
538
539 r = MsiViewClose( htab );
540 if(r != ERROR_SUCCESS )
541 res = r;
542
543 r = MsiCloseHandle( htab );
544 if(r != ERROR_SUCCESS )
545 res = r;
546 }
547 return res;
548 }
549
550 static UINT try_query( MSIHANDLE hdb, LPCSTR szQuery )
551 {
552 return try_query_param( hdb, szQuery, 0 );
553 }
554
555 static UINT try_insert_query( MSIHANDLE hdb, LPCSTR szQuery )
556 {
557 MSIHANDLE hrec = 0;
558 UINT r;
559
560 hrec = MsiCreateRecord( 1 );
561 MsiRecordSetStringA( hrec, 1, "Hello");
562
563 r = try_query_param( hdb, szQuery, hrec );
564
565 MsiCloseHandle( hrec );
566 return r;
567 }
568
569 static void test_msibadqueries(void)
570 {
571 MSIHANDLE hdb = 0;
572 UINT r;
573
574 DeleteFileA(msifile);
575
576 /* just MsiOpenDatabase should not create a file */
577 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
578 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
579
580 r = MsiDatabaseCommit( hdb );
581 ok(r == ERROR_SUCCESS , "Failed to commit database\n");
582
583 r = MsiCloseHandle( hdb );
584 ok(r == ERROR_SUCCESS , "Failed to close database\n");
585
586 /* open it readonly */
587 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_READONLY, &hdb );
588 ok(r == ERROR_SUCCESS , "Failed to open database r/o\n");
589
590 /* add a table to it */
591 r = try_query( hdb, "select * from _Tables");
592 ok(r == ERROR_SUCCESS , "query 1 failed\n");
593
594 r = MsiCloseHandle( hdb );
595 ok(r == ERROR_SUCCESS , "Failed to close database r/o\n");
596
597 /* open it read/write */
598 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_TRANSACT, &hdb );
599 ok(r == ERROR_SUCCESS , "Failed to open database r/w\n");
600
601 /* a bunch of test queries that fail with the native MSI */
602
603 r = try_query( hdb, "CREATE TABLE");
604 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2a return code\n");
605
606 r = try_query( hdb, "CREATE TABLE `a`");
607 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2b return code\n");
608
609 r = try_query( hdb, "CREATE TABLE `a` ()");
610 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2c return code\n");
611
612 r = try_query( hdb, "CREATE TABLE `a` (`b`)");
613 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2d return code\n");
614
615 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) )");
616 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2e return code\n");
617
618 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL)");
619 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2f return code\n");
620
621 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY)");
622 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2g return code\n");
623
624 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
625 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2h return code\n");
626
627 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY)");
628 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2i return code\n");
629
630 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY 'b')");
631 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2j return code\n");
632
633 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
634 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2k return code\n");
635
636 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b')");
637 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2l return code\n");
638
639 r = try_query( hdb, "CREATE TABLE `a` (`b` CHA(72) NOT NULL PRIMARY KEY `b`)");
640 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2m return code\n");
641
642 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(-1) NOT NULL PRIMARY KEY `b`)");
643 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2n return code\n");
644
645 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(720) NOT NULL PRIMARY KEY `b`)");
646 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2o return code\n");
647
648 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL KEY `b`)");
649 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
650
651 r = try_query( hdb, "CREATE TABLE `a` (`` CHAR(72) NOT NULL PRIMARY KEY `b`)");
652 ok(r == ERROR_BAD_QUERY_SYNTAX , "invalid query 2p return code\n");
653
654 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
655 ok(r == ERROR_SUCCESS , "valid query 2z failed\n");
656
657 r = try_query( hdb, "CREATE TABLE `a` (`b` CHAR(72) NOT NULL PRIMARY KEY `b`)");
658 ok(r == ERROR_BAD_QUERY_SYNTAX , "created same table again\n");
659
660 r = try_query( hdb, "CREATE TABLE `aa` (`b` CHAR(72) NOT NULL, `c` "
661 "CHAR(72), `d` CHAR(255) NOT NULL LOCALIZABLE PRIMARY KEY `b`)");
662 ok(r == ERROR_SUCCESS , "query 4 failed\n");
663
664 r = MsiDatabaseCommit( hdb );
665 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
666
667 r = try_query( hdb, "CREATE TABLE `blah` (`foo` CHAR(72) NOT NULL "
668 "PRIMARY KEY `foo`)");
669 ok(r == ERROR_SUCCESS , "query 4 failed\n");
670
671 r = try_insert_query( hdb, "insert into a ( `b` ) VALUES ( ? )");
672 ok(r == ERROR_SUCCESS , "failed to insert record in db\n");
673
674 r = MsiDatabaseCommit( hdb );
675 ok(r == ERROR_SUCCESS , "Failed to commit database after write\n");
676
677 r = try_query( hdb, "CREATE TABLE `boo` (`foo` CHAR(72) NOT NULL "
678 "PRIMARY KEY `ba`)");
679 ok(r != ERROR_SUCCESS , "query 5 succeeded\n");
680
681 r = try_query( hdb,"CREATE TABLE `bee` (`foo` CHAR(72) NOT NULL )");
682 ok(r != ERROR_SUCCESS , "query 6 succeeded\n");
683
684 r = try_query( hdb, "CREATE TABLE `temp` (`t` CHAR(72) NOT NULL "
685 "PRIMARY KEY `t`)");
686 ok(r == ERROR_SUCCESS , "query 7 failed\n");
687
688 r = try_query( hdb, "CREATE TABLE `c` (`b` CHAR NOT NULL PRIMARY KEY `b`)");
689 ok(r == ERROR_SUCCESS , "query 8 failed\n");
690
691 r = try_query( hdb, "select * from c");
692 ok(r == ERROR_SUCCESS , "query failed\n");
693
694 r = try_query( hdb, "select * from c where b = 'x");
695 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
696
697 r = try_query( hdb, "select * from c where b = 'x'");
698 ok(r == ERROR_SUCCESS, "query failed\n");
699
700 r = try_query( hdb, "select * from 'c'");
701 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
702
703 r = try_query( hdb, "select * from ''");
704 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
705
706 r = try_query( hdb, "select * from c where b = x");
707 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
708
709 r = try_query( hdb, "select * from c where b = \"x\"");
710 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
711
712 r = try_query( hdb, "select * from c where b = 'x'");
713 ok(r == ERROR_SUCCESS, "query failed\n");
714
715 r = try_query( hdb, "select * from c where b = '\"x'");
716 ok(r == ERROR_SUCCESS, "query failed\n");
717
718 if (0) /* FIXME: this query causes trouble with other tests */
719 {
720 r = try_query( hdb, "select * from c where b = '\\\'x'");
721 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
722 }
723
724 r = try_query( hdb, "select * from 'c'");
725 ok(r == ERROR_BAD_QUERY_SYNTAX, "query failed\n");
726
727 r = try_query( hdb, "select `c`.`b` from `c` order by `c`.`order`");
728 ok( r == ERROR_BAD_QUERY_SYNTAX, "query failed: %u\n", r );
729
730 r = try_query( hdb, "select `c`.b` from `c`");
731 ok( r == ERROR_SUCCESS, "query failed: %u\n", r );
732
733 r = try_query( hdb, "select `c`.`b from `c`");
734 ok( r == ERROR_BAD_QUERY_SYNTAX, "query failed: %u\n", r );
735
736 r = try_query( hdb, "select `c`.b from `c`");
737 ok( r == ERROR_SUCCESS, "query failed: %u\n", r );
738
739 r = try_query( hdb, "select `c.`b` from `c`");
740 ok( r == ERROR_BAD_QUERY_SYNTAX, "query failed: %u\n", r );
741
742 r = try_query( hdb, "select c`.`b` from `c`");
743 ok( r == ERROR_SUCCESS, "query failed: %u\n", r );
744
745 r = try_query( hdb, "select c.`b` from `c`");
746 ok( r == ERROR_SUCCESS, "query failed: %u\n", r );
747
748 r = try_query( hdb, "select `c`.`b` from c`");
749 ok( r == ERROR_SUCCESS, "query failed: %u\n", r );
750
751 r = try_query( hdb, "select `c`.`b` from `c");
752 ok( r == ERROR_BAD_QUERY_SYNTAX, "query failed: %u\n", r );
753
754 r = try_query( hdb, "select `c`.`b` from c");
755 ok( r == ERROR_SUCCESS, "query failed: %u\n", r );
756
757 r = try_query( hdb, "CREATE TABLE `\5a` (`b` CHAR NOT NULL PRIMARY KEY `b`)" );
758 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
759
760 r = try_query( hdb, "SELECT * FROM \5a" );
761 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
762
763 r = try_query( hdb, "CREATE TABLE `a\5` (`b` CHAR NOT NULL PRIMARY KEY `b`)" );
764 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
765
766 r = try_query( hdb, "SELECT * FROM a\5" );
767 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
768
769 r = try_query( hdb, "CREATE TABLE `-a` (`b` CHAR NOT NULL PRIMARY KEY `b`)" );
770 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
771
772 r = try_query( hdb, "SELECT * FROM -a" );
773 todo_wine ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
774
775 r = try_query( hdb, "CREATE TABLE `a-` (`b` CHAR NOT NULL PRIMARY KEY `b`)" );
776 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
777
778 r = try_query( hdb, "SELECT * FROM a-" );
779 ok( r == ERROR_SUCCESS , "query failed: %u\n", r );
780
781 r = MsiCloseHandle( hdb );
782 ok(r == ERROR_SUCCESS , "Failed to close database transact\n");
783
784 r = DeleteFileA( msifile );
785 ok(r == TRUE, "file didn't exist after commit\n");
786 }
787
788 static void test_viewmodify(void)
789 {
790 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
791 UINT r;
792 MSIDBERROR err;
793 const char *query;
794 char buffer[0x100];
795 DWORD sz;
796
797 DeleteFileA(msifile);
798
799 /* just MsiOpenDatabase should not create a file */
800 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
801 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
802
803 query = "CREATE TABLE `phone` ( "
804 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
805 "PRIMARY KEY `id`)";
806 r = run_query( hdb, 0, query );
807 ok(r == ERROR_SUCCESS, "query failed\n");
808
809 query = "CREATE TABLE `_Validation` ( "
810 "`Table` CHAR(32) NOT NULL, `Column` CHAR(32) NOT NULL, "
811 "`Nullable` CHAR(4) NOT NULL, `MinValue` INT, `MaxValue` INT, "
812 "`KeyTable` CHAR(255), `KeyColumn` SHORT, `Category` CHAR(32), "
813 "`Set` CHAR(255), `Description` CHAR(255) PRIMARY KEY `Table`, `Column`)";
814 r = run_query( hdb, 0, query );
815 ok(r == ERROR_SUCCESS, "query failed\n");
816
817 query = "INSERT INTO `_Validation` ( `Table`, `Column`, `Nullable` ) "
818 "VALUES('phone', 'id', 'N')";
819 r = run_query( hdb, 0, query );
820 ok(r == ERROR_SUCCESS, "query failed\n");
821
822 /* check what the error function reports without doing anything */
823 sz = 0;
824 /* passing NULL as the 3rd param make function to crash on older platforms */
825 err = MsiViewGetErrorA( 0, NULL, &sz );
826 ok(err == MSIDBERROR_INVALIDARG, "MsiViewGetError return\n");
827
828 /* open a view */
829 query = "SELECT * FROM `phone`";
830 r = MsiDatabaseOpenViewA(hdb, query, &hview);
831 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
832
833 /* see what happens with a good hview and bad args */
834 err = MsiViewGetErrorA( hview, NULL, NULL );
835 ok(err == MSIDBERROR_INVALIDARG || err == MSIDBERROR_NOERROR,
836 "MsiViewGetError returns %u (expected -3)\n", err);
837 err = MsiViewGetErrorA( hview, buffer, NULL );
838 ok(err == MSIDBERROR_INVALIDARG, "MsiViewGetError return\n");
839
840 /* see what happens with a zero length buffer */
841 sz = 0;
842 buffer[0] = 'x';
843 err = MsiViewGetErrorA( hview, buffer, &sz );
844 ok(err == MSIDBERROR_MOREDATA, "MsiViewGetError return\n");
845 ok(buffer[0] == 'x', "buffer cleared\n");
846 ok(sz == 0, "size not zero\n");
847
848 /* ok this one is strange */
849 sz = 0;
850 err = MsiViewGetErrorA( hview, NULL, &sz );
851 ok(err == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
852 ok(sz == 0, "size not zero\n");
853
854 /* see if it really has an error */
855 sz = sizeof buffer;
856 buffer[0] = 'x';
857 err = MsiViewGetErrorA( hview, buffer, &sz );
858 ok(err == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
859 ok(buffer[0] == 0, "buffer not cleared\n");
860 ok(sz == 0, "size not zero\n");
861
862 r = MsiViewExecute(hview, 0);
863 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
864
865 /* try some invalid records */
866 r = MsiViewModify(hview, MSIMODIFY_INSERT, 0 );
867 ok(r == ERROR_INVALID_HANDLE, "MsiViewModify failed\n");
868 r = MsiViewModify(hview, -1, 0 );
869 ok(r == ERROR_INVALID_HANDLE, "MsiViewModify failed\n");
870
871 /* try an small record */
872 hrec = MsiCreateRecord(1);
873 r = MsiViewModify(hview, -1, hrec );
874 ok(r == ERROR_INVALID_DATA, "MsiViewModify failed\n");
875
876 sz = sizeof buffer;
877 buffer[0] = 'x';
878 err = MsiViewGetErrorA( hview, buffer, &sz );
879 ok(err == MSIDBERROR_NOERROR, "MsiViewGetError return\n");
880 ok(buffer[0] == 0, "buffer not cleared\n");
881 ok(sz == 0, "size not zero\n");
882
883 r = MsiCloseHandle(hrec);
884 ok(r == ERROR_SUCCESS, "failed to close record\n");
885
886 /* insert a valid record */
887 hrec = MsiCreateRecord(3);
888
889 r = MsiRecordSetInteger(hrec, 1, 1);
890 ok(r == ERROR_SUCCESS, "failed to set integer\n");
891 r = MsiRecordSetStringA(hrec, 2, "bob");
892 ok(r == ERROR_SUCCESS, "failed to set string\n");
893 r = MsiRecordSetStringA(hrec, 3, "7654321");
894 ok(r == ERROR_SUCCESS, "failed to set string\n");
895
896 r = MsiViewExecute(hview, 0);
897 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
898 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
899 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
900
901 /* validate it */
902 r = MsiViewExecute(hview, 0);
903 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
904
905 r = MsiViewModify(hview, MSIMODIFY_VALIDATE_NEW, hrec );
906 ok(r == ERROR_INVALID_DATA, "MsiViewModify failed %u\n", r);
907
908 sz = sizeof buffer;
909 buffer[0] = 'x';
910 err = MsiViewGetErrorA( hview, buffer, &sz );
911 ok(err == MSIDBERROR_DUPLICATEKEY, "MsiViewGetError returned %u\n", err);
912 ok(!strcmp(buffer, "id"), "expected \"id\" c, got \"%s\"\n", buffer);
913 ok(sz == 2, "size not 2\n");
914
915 /* insert the same thing again */
916 r = MsiViewExecute(hview, 0);
917 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
918
919 /* should fail ... */
920 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
921 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
922
923 /* try to merge the same record */
924 r = MsiViewExecute(hview, 0);
925 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
926 r = MsiViewModify(hview, MSIMODIFY_MERGE, hrec );
927 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
928
929 r = MsiCloseHandle(hrec);
930 ok(r == ERROR_SUCCESS, "failed to close record\n");
931
932 /* try merging a new record */
933 hrec = MsiCreateRecord(3);
934
935 r = MsiRecordSetInteger(hrec, 1, 10);
936 ok(r == ERROR_SUCCESS, "failed to set integer\n");
937 r = MsiRecordSetStringA(hrec, 2, "pepe");
938 ok(r == ERROR_SUCCESS, "failed to set string\n");
939 r = MsiRecordSetStringA(hrec, 3, "7654321");
940 ok(r == ERROR_SUCCESS, "failed to set string\n");
941
942 r = MsiViewModify(hview, MSIMODIFY_MERGE, hrec );
943 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
944 r = MsiViewExecute(hview, 0);
945 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
946
947 r = MsiCloseHandle(hrec);
948 ok(r == ERROR_SUCCESS, "failed to close record\n");
949
950 r = MsiViewClose(hview);
951 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
952 r = MsiCloseHandle(hview);
953 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
954
955 query = "SELECT * FROM `phone`";
956 r = MsiDatabaseOpenViewA(hdb, query, &hview);
957 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
958
959 r = MsiViewExecute(hview, 0);
960 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
961
962 r = MsiViewFetch(hview, &hrec);
963 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
964
965 r = MsiRecordGetInteger(hrec, 1);
966 ok(r == 1, "Expected 1, got %d\n", r);
967
968 sz = sizeof(buffer);
969 r = MsiRecordGetStringA(hrec, 2, buffer, &sz);
970 ok(r == ERROR_SUCCESS, "MsiRecordGetString failed\n");
971 ok(!lstrcmpA(buffer, "bob"), "Expected bob, got %s\n", buffer);
972
973 sz = sizeof(buffer);
974 r = MsiRecordGetStringA(hrec, 3, buffer, &sz);
975 ok(r == ERROR_SUCCESS, "MsiRecordGetString failed\n");
976 ok(!lstrcmpA(buffer, "7654321"), "Expected 7654321, got %s\n", buffer);
977
978 /* update the view, non-primary key */
979 r = MsiRecordSetStringA(hrec, 3, "3141592");
980 ok(r == ERROR_SUCCESS, "MsiRecordSetString failed\n");
981
982 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
983 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
984
985 /* do it again */
986 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
987 ok(r == ERROR_SUCCESS, "MsiViewModify failed: %d\n", r);
988
989 /* update the view, primary key */
990 r = MsiRecordSetInteger(hrec, 1, 5);
991 ok(r == ERROR_SUCCESS, "MsiRecordSetInteger failed\n");
992
993 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
994 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
995
996 r = MsiCloseHandle(hrec);
997 ok(r == ERROR_SUCCESS, "failed to close record\n");
998
999 r = MsiViewClose(hview);
1000 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1001 r = MsiCloseHandle(hview);
1002 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1003
1004 query = "SELECT * FROM `phone`";
1005 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1006 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1007
1008 r = MsiViewExecute(hview, 0);
1009 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1010
1011 r = MsiViewFetch(hview, &hrec);
1012 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1013
1014 r = MsiRecordGetInteger(hrec, 1);
1015 ok(r == 1, "Expected 1, got %d\n", r);
1016
1017 sz = sizeof(buffer);
1018 r = MsiRecordGetStringA(hrec, 2, buffer, &sz);
1019 ok(r == ERROR_SUCCESS, "MsiRecordGetString failed\n");
1020 ok(!lstrcmpA(buffer, "bob"), "Expected bob, got %s\n", buffer);
1021
1022 sz = sizeof(buffer);
1023 r = MsiRecordGetStringA(hrec, 3, buffer, &sz);
1024 ok(r == ERROR_SUCCESS, "MsiRecordGetString failed\n");
1025 ok(!lstrcmpA(buffer, "3141592"), "Expected 3141592, got %s\n", buffer);
1026
1027 r = MsiCloseHandle(hrec);
1028 ok(r == ERROR_SUCCESS, "failed to close record\n");
1029
1030 /* use a record that doesn't come from a view fetch */
1031 hrec = MsiCreateRecord(3);
1032 ok(hrec != 0, "MsiCreateRecord failed\n");
1033
1034 r = MsiRecordSetInteger(hrec, 1, 3);
1035 ok(r == ERROR_SUCCESS, "failed to set integer\n");
1036 r = MsiRecordSetStringA(hrec, 2, "jane");
1037 ok(r == ERROR_SUCCESS, "failed to set string\n");
1038 r = MsiRecordSetStringA(hrec, 3, "112358");
1039 ok(r == ERROR_SUCCESS, "failed to set string\n");
1040
1041 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
1042 ok(r == ERROR_FUNCTION_FAILED, "Expected ERROR_FUNCTION_FAILED, got %d\n", r);
1043
1044 r = MsiCloseHandle(hrec);
1045 ok(r == ERROR_SUCCESS, "failed to close record\n");
1046
1047 /* use a record that doesn't come from a view fetch, primary key matches */
1048 hrec = MsiCreateRecord(3);
1049 ok(hrec != 0, "MsiCreateRecord failed\n");
1050
1051 r = MsiRecordSetInteger(hrec, 1, 1);
1052 ok(r == ERROR_SUCCESS, "failed to set integer\n");
1053 r = MsiRecordSetStringA(hrec, 2, "jane");
1054 ok(r == ERROR_SUCCESS, "failed to set string\n");
1055 r = MsiRecordSetStringA(hrec, 3, "112358");
1056 ok(r == ERROR_SUCCESS, "failed to set string\n");
1057
1058 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
1059 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
1060
1061 r = MsiCloseHandle(hrec);
1062 ok(r == ERROR_SUCCESS, "failed to close record\n");
1063
1064 hrec = MsiCreateRecord(3);
1065
1066 r = MsiRecordSetInteger(hrec, 1, 2);
1067 ok(r == ERROR_SUCCESS, "failed to set integer\n");
1068 r = MsiRecordSetStringA(hrec, 2, "nick");
1069 ok(r == ERROR_SUCCESS, "failed to set string\n");
1070 r = MsiRecordSetStringA(hrec, 3, "141421");
1071 ok(r == ERROR_SUCCESS, "failed to set string\n");
1072
1073 r = MsiViewExecute(hview, 0);
1074 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1075 r = MsiViewModify(hview, MSIMODIFY_INSERT_TEMPORARY, hrec );
1076 ok(r == ERROR_SUCCESS, "MsiViewModify failed\n");
1077
1078 r = MsiCloseHandle(hrec);
1079 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1080 r = MsiViewClose(hview);
1081 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1082 r = MsiCloseHandle(hview);
1083 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1084
1085 query = "SELECT * FROM `phone` WHERE `id` = 1";
1086 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1087 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1088 r = MsiViewExecute(hview, 0);
1089 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1090 r = MsiViewFetch(hview, &hrec);
1091 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1092
1093 /* change the id to match the second row */
1094 r = MsiRecordSetInteger(hrec, 1, 2);
1095 ok(r == ERROR_SUCCESS, "failed to set integer\n");
1096 r = MsiRecordSetStringA(hrec, 2, "jerry");
1097 ok(r == ERROR_SUCCESS, "failed to set string\n");
1098
1099 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
1100 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
1101
1102 r = MsiCloseHandle(hrec);
1103 ok(r == ERROR_SUCCESS, "failed to close record\n");
1104 r = MsiViewClose(hview);
1105 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1106 r = MsiCloseHandle(hview);
1107 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1108
1109 /* broader search */
1110 query = "SELECT * FROM `phone` ORDER BY `id`";
1111 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1112 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1113 r = MsiViewExecute(hview, 0);
1114 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1115 r = MsiViewFetch(hview, &hrec);
1116 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1117
1118 /* change the id to match the second row */
1119 r = MsiRecordSetInteger(hrec, 1, 2);
1120 ok(r == ERROR_SUCCESS, "failed to set integer\n");
1121 r = MsiRecordSetStringA(hrec, 2, "jerry");
1122 ok(r == ERROR_SUCCESS, "failed to set string\n");
1123
1124 r = MsiViewModify(hview, MSIMODIFY_UPDATE, hrec);
1125 ok(r == ERROR_FUNCTION_FAILED, "MsiViewModify failed\n");
1126
1127 r = MsiCloseHandle(hrec);
1128 ok(r == ERROR_SUCCESS, "failed to close record\n");
1129 r = MsiViewClose(hview);
1130 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1131 r = MsiCloseHandle(hview);
1132 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1133
1134 r = MsiCloseHandle( hdb );
1135 ok(r == ERROR_SUCCESS, "MsiOpenDatabase close failed\n");
1136 }
1137
1138 static MSIHANDLE create_db(void)
1139 {
1140 MSIHANDLE hdb = 0;
1141 UINT res;
1142
1143 DeleteFileW(msifileW);
1144
1145 /* create an empty database */
1146 res = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb );
1147 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
1148 if( res != ERROR_SUCCESS )
1149 return hdb;
1150
1151 res = MsiDatabaseCommit( hdb );
1152 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
1153
1154 return hdb;
1155 }
1156
1157 static void test_getcolinfo(void)
1158 {
1159 MSIHANDLE hdb, hview = 0, rec = 0;
1160 UINT r;
1161 DWORD sz;
1162 char buffer[0x20];
1163
1164 /* create an empty db */
1165 hdb = create_db();
1166 ok( hdb, "failed to create db\n");
1167
1168 /* tables should be present */
1169 r = MsiDatabaseOpenViewA(hdb, "select * from _Tables", &hview);
1170 ok( r == ERROR_SUCCESS, "failed to open query\n");
1171
1172 r = MsiViewExecute(hview, 0);
1173 ok( r == ERROR_SUCCESS, "failed to execute query\n");
1174
1175 /* check that NAMES works */
1176 rec = 0;
1177 r = MsiViewGetColumnInfo( hview, MSICOLINFO_NAMES, &rec );
1178 ok( r == ERROR_SUCCESS, "failed to get names\n");
1179 sz = sizeof buffer;
1180 r = MsiRecordGetStringA(rec, 1, buffer, &sz );
1181 ok( r == ERROR_SUCCESS, "failed to get string\n");
1182 ok( !strcmp(buffer,"Name"), "_Tables has wrong column name\n");
1183 r = MsiCloseHandle( rec );
1184 ok( r == ERROR_SUCCESS, "failed to close record handle\n");
1185
1186 /* check that TYPES works */
1187 rec = 0;
1188 r = MsiViewGetColumnInfo( hview, MSICOLINFO_TYPES, &rec );
1189 ok( r == ERROR_SUCCESS, "failed to get names\n");
1190 sz = sizeof buffer;
1191 r = MsiRecordGetStringA(rec, 1, buffer, &sz );
1192 ok( r == ERROR_SUCCESS, "failed to get string\n");
1193 ok( !strcmp(buffer,"s64"), "_Tables has wrong column type\n");
1194 r = MsiCloseHandle( rec );
1195 ok( r == ERROR_SUCCESS, "failed to close record handle\n");
1196
1197 /* check that invalid values fail */
1198 rec = 0;
1199 r = MsiViewGetColumnInfo( hview, 100, &rec );
1200 ok( r == ERROR_INVALID_PARAMETER, "wrong error code\n");
1201 ok( rec == 0, "returned a record\n");
1202
1203 r = MsiViewGetColumnInfo( hview, MSICOLINFO_TYPES, NULL );
1204 ok( r == ERROR_INVALID_PARAMETER, "wrong error code\n");
1205
1206 r = MsiViewGetColumnInfo( 0, MSICOLINFO_TYPES, &rec );
1207 ok( r == ERROR_INVALID_HANDLE, "wrong error code\n");
1208
1209 r = MsiViewClose(hview);
1210 ok( r == ERROR_SUCCESS, "failed to close view\n");
1211 r = MsiCloseHandle(hview);
1212 ok( r == ERROR_SUCCESS, "failed to close view handle\n");
1213 r = MsiCloseHandle(hdb);
1214 ok( r == ERROR_SUCCESS, "failed to close database\n");
1215 }
1216
1217 static MSIHANDLE get_column_info(MSIHANDLE hdb, const char *query, MSICOLINFO type)
1218 {
1219 MSIHANDLE hview = 0, rec = 0;
1220 UINT r;
1221
1222 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1223 if( r != ERROR_SUCCESS )
1224 return r;
1225
1226 r = MsiViewExecute(hview, 0);
1227 if( r == ERROR_SUCCESS )
1228 {
1229 MsiViewGetColumnInfo( hview, type, &rec );
1230 }
1231 MsiViewClose(hview);
1232 MsiCloseHandle(hview);
1233 return rec;
1234 }
1235
1236 static UINT get_columns_table_type(MSIHANDLE hdb, const char *table, UINT field)
1237 {
1238 MSIHANDLE hview = 0, rec = 0;
1239 UINT r, type = 0;
1240 char query[0x100];
1241
1242 sprintf(query, "select * from `_Columns` where `Table` = '%s'", table );
1243
1244 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1245 if( r != ERROR_SUCCESS )
1246 return r;
1247
1248 r = MsiViewExecute(hview, 0);
1249 if( r == ERROR_SUCCESS )
1250 {
1251 while (1)
1252 {
1253 r = MsiViewFetch( hview, &rec );
1254 if( r != ERROR_SUCCESS)
1255 break;
1256 r = MsiRecordGetInteger( rec, 2 );
1257 if (r == field)
1258 type = MsiRecordGetInteger( rec, 4 );
1259 MsiCloseHandle( rec );
1260 }
1261 }
1262 MsiViewClose(hview);
1263 MsiCloseHandle(hview);
1264 return type;
1265 }
1266
1267 static BOOL check_record( MSIHANDLE rec, UINT field, LPCSTR val )
1268 {
1269 CHAR buffer[0x20];
1270 UINT r;
1271 DWORD sz;
1272
1273 sz = sizeof buffer;
1274 r = MsiRecordGetStringA( rec, field, buffer, &sz );
1275 return (r == ERROR_SUCCESS ) && !strcmp(val, buffer);
1276 }
1277
1278 static void test_viewgetcolumninfo(void)
1279 {
1280 MSIHANDLE hdb = 0, rec;
1281 UINT r;
1282
1283 hdb = create_db();
1284 ok( hdb, "failed to create db\n");
1285
1286 r = run_query( hdb, 0,
1287 "CREATE TABLE `Properties` "
1288 "( `Property` CHAR(255), "
1289 " `Value` CHAR(1), "
1290 " `Intvalue` INT, "
1291 " `Integervalue` INTEGER, "
1292 " `Shortvalue` SHORT, "
1293 " `Longvalue` LONG, "
1294 " `Longcharvalue` LONGCHAR, "
1295 " `Charvalue` CHAR, "
1296 " `Localizablevalue` CHAR LOCALIZABLE "
1297 " PRIMARY KEY `Property`)" );
1298 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
1299
1300 /* check the column types */
1301 rec = get_column_info( hdb, "select * from `Properties`", MSICOLINFO_TYPES );
1302 ok( rec, "failed to get column info record\n" );
1303
1304 ok( check_record( rec, 1, "S255"), "wrong record type\n");
1305 ok( check_record( rec, 2, "S1"), "wrong record type\n");
1306 ok( check_record( rec, 3, "I2"), "wrong record type\n");
1307 ok( check_record( rec, 4, "I2"), "wrong record type\n");
1308 ok( check_record( rec, 5, "I2"), "wrong record type\n");
1309 ok( check_record( rec, 6, "I4"), "wrong record type\n");
1310 ok( check_record( rec, 7, "S0"), "wrong record type\n");
1311 ok( check_record( rec, 8, "S0"), "wrong record type\n");
1312 ok( check_record( rec, 9, "L0"), "wrong record type\n");
1313
1314 MsiCloseHandle( rec );
1315
1316 /* check the type in _Columns */
1317 ok( 0x3dff == get_columns_table_type(hdb, "Properties", 1 ), "_columns table wrong\n");
1318 ok( 0x1d01 == get_columns_table_type(hdb, "Properties", 2 ), "_columns table wrong\n");
1319 ok( 0x1502 == get_columns_table_type(hdb, "Properties", 3 ), "_columns table wrong\n");
1320 ok( 0x1502 == get_columns_table_type(hdb, "Properties", 4 ), "_columns table wrong\n");
1321 ok( 0x1502 == get_columns_table_type(hdb, "Properties", 5 ), "_columns table wrong\n");
1322 ok( 0x1104 == get_columns_table_type(hdb, "Properties", 6 ), "_columns table wrong\n");
1323 ok( 0x1d00 == get_columns_table_type(hdb, "Properties", 7 ), "_columns table wrong\n");
1324 ok( 0x1d00 == get_columns_table_type(hdb, "Properties", 8 ), "_columns table wrong\n");
1325 ok( 0x1f00 == get_columns_table_type(hdb, "Properties", 9 ), "_columns table wrong\n");
1326
1327 /* now try the names */
1328 rec = get_column_info( hdb, "select * from `Properties`", MSICOLINFO_NAMES );
1329 ok( rec, "failed to get column info record\n" );
1330
1331 ok( check_record( rec, 1, "Property"), "wrong record type\n");
1332 ok( check_record( rec, 2, "Value"), "wrong record type\n");
1333 ok( check_record( rec, 3, "Intvalue"), "wrong record type\n");
1334 ok( check_record( rec, 4, "Integervalue"), "wrong record type\n");
1335 ok( check_record( rec, 5, "Shortvalue"), "wrong record type\n");
1336 ok( check_record( rec, 6, "Longvalue"), "wrong record type\n");
1337 ok( check_record( rec, 7, "Longcharvalue"), "wrong record type\n");
1338 ok( check_record( rec, 8, "Charvalue"), "wrong record type\n");
1339 ok( check_record( rec, 9, "Localizablevalue"), "wrong record type\n");
1340
1341 MsiCloseHandle( rec );
1342
1343 r = run_query( hdb, 0,
1344 "CREATE TABLE `Binary` "
1345 "( `Name` CHAR(255), `Data` OBJECT PRIMARY KEY `Name`)" );
1346 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
1347
1348 /* check the column types */
1349 rec = get_column_info( hdb, "select * from `Binary`", MSICOLINFO_TYPES );
1350 ok( rec, "failed to get column info record\n" );
1351
1352 ok( check_record( rec, 1, "S255"), "wrong record type\n");
1353 ok( check_record( rec, 2, "V0"), "wrong record type\n");
1354
1355 MsiCloseHandle( rec );
1356
1357 /* check the type in _Columns */
1358 ok( 0x3dff == get_columns_table_type(hdb, "Binary", 1 ), "_columns table wrong\n");
1359 ok( 0x1900 == get_columns_table_type(hdb, "Binary", 2 ), "_columns table wrong\n");
1360
1361 /* now try the names */
1362 rec = get_column_info( hdb, "select * from `Binary`", MSICOLINFO_NAMES );
1363 ok( rec, "failed to get column info record\n" );
1364
1365 ok( check_record( rec, 1, "Name"), "wrong record type\n");
1366 ok( check_record( rec, 2, "Data"), "wrong record type\n");
1367 MsiCloseHandle( rec );
1368
1369 r = run_query( hdb, 0,
1370 "CREATE TABLE `UIText` "
1371 "( `Key` CHAR(72) NOT NULL, `Text` CHAR(255) LOCALIZABLE PRIMARY KEY `Key`)" );
1372 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
1373
1374 ok( 0x2d48 == get_columns_table_type(hdb, "UIText", 1 ), "_columns table wrong\n");
1375 ok( 0x1fff == get_columns_table_type(hdb, "UIText", 2 ), "_columns table wrong\n");
1376
1377 rec = get_column_info( hdb, "select * from `UIText`", MSICOLINFO_NAMES );
1378 ok( rec, "failed to get column info record\n" );
1379 ok( check_record( rec, 1, "Key"), "wrong record type\n");
1380 ok( check_record( rec, 2, "Text"), "wrong record type\n");
1381 MsiCloseHandle( rec );
1382
1383 rec = get_column_info( hdb, "select * from `UIText`", MSICOLINFO_TYPES );
1384 ok( rec, "failed to get column info record\n" );
1385 ok( check_record( rec, 1, "s72"), "wrong record type\n");
1386 ok( check_record( rec, 2, "L255"), "wrong record type\n");
1387 MsiCloseHandle( rec );
1388
1389 MsiCloseHandle( hdb );
1390 }
1391
1392 static void test_msiexport(void)
1393 {
1394 MSIHANDLE hdb = 0, hview = 0;
1395 UINT r;
1396 const char *query;
1397 char path[MAX_PATH];
1398 const char file[] = "phone.txt";
1399 HANDLE handle;
1400 char buffer[0x100];
1401 DWORD length;
1402 const char expected[] =
1403 "id\tname\tnumber\r\n"
1404 "I2\tS32\tS32\r\n"
1405 "phone\tid\r\n"
1406 "1\tAbe\t8675309\r\n";
1407
1408 DeleteFileW(msifileW);
1409
1410 /* just MsiOpenDatabase should not create a file */
1411 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
1412 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
1413
1414 /* create a table */
1415 query = "CREATE TABLE `phone` ( "
1416 "`id` INT, `name` CHAR(32), `number` CHAR(32) "
1417 "PRIMARY KEY `id`)";
1418 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1419 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1420 r = MsiViewExecute(hview, 0);
1421 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1422 r = MsiViewClose(hview);
1423 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1424 r = MsiCloseHandle(hview);
1425 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1426
1427 /* insert a value into it */
1428 query = "INSERT INTO `phone` ( `id`, `name`, `number` )"
1429 "VALUES('1', 'Abe', '8675309')";
1430 r = MsiDatabaseOpenViewA(hdb, query, &hview);
1431 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1432 r = MsiViewExecute(hview, 0);
1433 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1434 r = MsiViewClose(hview);
1435 ok(r == ERROR_SUCCESS, "MsiViewClose failed\n");
1436 r = MsiCloseHandle(hview);
1437 ok(r == ERROR_SUCCESS, "MsiCloseHandle failed\n");
1438
1439 GetCurrentDirectoryA(MAX_PATH, path);
1440
1441 r = MsiDatabaseExportA(hdb, "phone", path, file);
1442 ok(r == ERROR_SUCCESS, "MsiDatabaseExport failed\n");
1443
1444 MsiCloseHandle(hdb);
1445
1446 lstrcatA(path, "\\");
1447 lstrcatA(path, file);
1448
1449 /* check the data that was written */
1450 length = 0;
1451 memset(buffer, 0, sizeof buffer);
1452 handle = CreateFileA(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
1453 if (handle != INVALID_HANDLE_VALUE)
1454 {
1455 ReadFile(handle, buffer, sizeof buffer, &length, NULL);
1456 CloseHandle(handle);
1457 DeleteFileA(path);
1458 }
1459 else
1460 ok(0, "failed to open file %s\n", path);
1461
1462 ok( length == strlen(expected), "length of data wrong\n");
1463 ok( !lstrcmpA(buffer, expected), "data doesn't match\n");
1464 DeleteFileA(msifile);
1465 }
1466
1467 static void test_longstrings(void)
1468 {
1469 const char insert_query[] =
1470 "INSERT INTO `strings` ( `id`, `val` ) VALUES('1', 'Z')";
1471 char *str;
1472 MSIHANDLE hdb = 0, hview = 0, hrec = 0;
1473 DWORD len;
1474 UINT r;
1475 const DWORD STRING_LENGTH = 0x10005;
1476
1477 DeleteFileW(msifileW);
1478 /* just MsiOpenDatabase should not create a file */
1479 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
1480 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
1481
1482 /* create a table */
1483 r = try_query( hdb,
1484 "CREATE TABLE `strings` ( `id` INT, `val` CHAR(0) PRIMARY KEY `id`)");
1485 ok(r == ERROR_SUCCESS, "query failed\n");
1486
1487 /* try to insert a very long string */
1488 str = HeapAlloc(GetProcessHeap(), 0, STRING_LENGTH+sizeof insert_query);
1489 len = strchr(insert_query, 'Z') - insert_query;
1490 strcpy(str, insert_query);
1491 memset(str+len, 'Z', STRING_LENGTH);
1492 strcpy(str+len+STRING_LENGTH, insert_query+len+1);
1493 r = try_query( hdb, str );
1494 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1495
1496 HeapFree(GetProcessHeap(), 0, str);
1497
1498 r = MsiDatabaseCommit(hdb);
1499 ok(r == ERROR_SUCCESS, "MsiDatabaseCommit failed\n");
1500 MsiCloseHandle(hdb);
1501
1502 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_READONLY, &hdb);
1503 ok(r == ERROR_SUCCESS, "MsiOpenDatabase failed\n");
1504
1505 r = MsiDatabaseOpenViewA(hdb, "select * from `strings` where `id` = 1", &hview);
1506 ok(r == ERROR_SUCCESS, "MsiDatabaseOpenView failed\n");
1507
1508 r = MsiViewExecute(hview, 0);
1509 ok(r == ERROR_SUCCESS, "MsiViewExecute failed\n");
1510
1511 r = MsiViewFetch(hview, &hrec);
1512 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1513
1514 MsiViewClose(hview);
1515 MsiCloseHandle(hview);
1516
1517 r = MsiRecordGetStringA(hrec, 2, NULL, &len);
1518 ok(r == ERROR_SUCCESS, "MsiViewFetch failed\n");
1519 ok(len == STRING_LENGTH, "string length wrong\n");
1520
1521 MsiCloseHandle(hrec);
1522 MsiCloseHandle(hdb);
1523 DeleteFileA(msifile);
1524 }
1525
1526 static void create_file_data(LPCSTR name, LPCSTR data, DWORD size)
1527 {
1528 HANDLE file;
1529 DWORD written;
1530
1531 file = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
1532 if (file == INVALID_HANDLE_VALUE)
1533 return;
1534
1535 WriteFile(file, data, strlen(data), &written, NULL);
1536 WriteFile(file, "\n", strlen("\n"), &written, NULL);
1537
1538 if (size)
1539 {
1540 SetFilePointer(file, size, NULL, FILE_BEGIN);
1541 SetEndOfFile(file);
1542 }
1543
1544 CloseHandle(file);
1545 }
1546
1547 #define create_file(name) create_file_data(name, name, 0)
1548
1549 static void test_streamtable(void)
1550 {
1551 MSIHANDLE hdb = 0, rec, view, hsi;
1552 char file[MAX_PATH];
1553 char buf[MAX_PATH];
1554 DWORD size;
1555 UINT r;
1556
1557 hdb = create_db();
1558 ok( hdb, "failed to create db\n");
1559
1560 r = run_query( hdb, 0,
1561 "CREATE TABLE `Properties` "
1562 "( `Property` CHAR(255), `Value` CHAR(1) PRIMARY KEY `Property`)" );
1563 ok( r == ERROR_SUCCESS , "Failed to create table\n" );
1564
1565 r = run_query( hdb, 0,
1566 "INSERT INTO `Properties` "
1567 "( `Value`, `Property` ) VALUES ( 'Prop', 'value' )" );
1568 ok( r == ERROR_SUCCESS, "Failed to add to table\n" );
1569
1570 r = MsiDatabaseCommit( hdb );
1571 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
1572
1573 MsiCloseHandle( hdb );
1574
1575 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_TRANSACT, &hdb );
1576 ok( r == ERROR_SUCCESS , "Failed to open database\n" );
1577
1578 /* check the column types */
1579 rec = get_column_info( hdb, "select * from `_Streams`", MSICOLINFO_TYPES );
1580 ok( rec, "failed to get column info record\n" );
1581
1582 ok( check_record( rec, 1, "s62"), "wrong record type\n");
1583 ok( check_record( rec, 2, "V0"), "wrong record type\n");
1584
1585 MsiCloseHandle( rec );
1586
1587 /* now try the names */
1588 rec = get_column_info( hdb, "select * from `_Streams`", MSICOLINFO_NAMES );
1589 ok( rec, "failed to get column info record\n" );
1590
1591 ok( check_record( rec, 1, "Name"), "wrong record type\n");
1592 ok( check_record( rec, 2, "Data"), "wrong record type\n");
1593
1594 MsiCloseHandle( rec );
1595
1596 r = MsiDatabaseOpenViewA( hdb,
1597 "SELECT * FROM `_Streams` WHERE `Name` = '\5SummaryInformation'", &view );
1598 ok( r == ERROR_SUCCESS, "Failed to open database view: %u\n", r );
1599
1600 r = MsiViewExecute( view, 0 );
1601 ok( r == ERROR_SUCCESS, "Failed to execute view: %u\n", r );
1602
1603 r = MsiViewFetch( view, &rec );
1604 ok( r == ERROR_NO_MORE_ITEMS, "Unexpected result: %u\n", r );
1605
1606 MsiCloseHandle( rec );
1607 MsiViewClose( view );
1608 MsiCloseHandle( view );
1609
1610 /* create a summary information stream */
1611 r = MsiGetSummaryInformationA( hdb, NULL, 1, &hsi );
1612 ok( r == ERROR_SUCCESS, "Failed to get summary information handle: %u\n", r );
1613
1614 r = MsiSummaryInfoSetPropertyA( hsi, PID_SECURITY, VT_I4, 2, NULL, NULL );
1615 ok( r == ERROR_SUCCESS, "Failed to set property: %u\n", r );
1616
1617 r = MsiSummaryInfoPersist( hsi );
1618 ok( r == ERROR_SUCCESS, "Failed to save summary information: %u\n", r );
1619
1620 MsiCloseHandle( hsi );
1621
1622 r = MsiDatabaseOpenViewA( hdb,
1623 "SELECT * FROM `_Streams` WHERE `Name` = '\5SummaryInformation'", &view );
1624 ok( r == ERROR_SUCCESS, "Failed to open database view: %u\n", r );
1625
1626 r = MsiViewExecute( view, 0 );
1627 ok( r == ERROR_SUCCESS, "Failed to execute view: %u\n", r );
1628
1629 r = MsiViewFetch( view, &rec );
1630 ok( r == ERROR_SUCCESS, "Unexpected result: %u\n", r );
1631
1632 MsiCloseHandle( rec );
1633 MsiViewClose( view );
1634 MsiCloseHandle( view );
1635
1636 /* insert a file into the _Streams table */
1637 create_file( "test.txt" );
1638
1639 rec = MsiCreateRecord( 2 );
1640 MsiRecordSetStringA( rec, 1, "data" );
1641
1642 r = MsiRecordSetStreamA( rec, 2, "test.txt" );
1643 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r);
1644
1645 DeleteFileA("test.txt");
1646
1647 r = MsiDatabaseOpenViewA( hdb,
1648 "INSERT INTO `_Streams` ( `Name`, `Data` ) VALUES ( ?, ? )", &view );
1649 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1650
1651 r = MsiViewExecute( view, rec );
1652 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1653
1654 MsiCloseHandle( rec );
1655 MsiViewClose( view );
1656 MsiCloseHandle( view );
1657
1658 /* insert another one */
1659 create_file( "test1.txt" );
1660
1661 rec = MsiCreateRecord( 2 );
1662 MsiRecordSetStringA( rec, 1, "data1" );
1663
1664 r = MsiRecordSetStreamA( rec, 2, "test1.txt" );
1665 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r);
1666
1667 DeleteFileA("test1.txt");
1668
1669 r = MsiDatabaseOpenViewA( hdb,
1670 "INSERT INTO `_Streams` ( `Name`, `Data` ) VALUES ( ?, ? )", &view );
1671 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1672
1673 r = MsiViewExecute( view, rec );
1674 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1675
1676 MsiCloseHandle( rec );
1677 MsiViewClose( view );
1678 MsiCloseHandle( view );
1679
1680 /* try again */
1681 create_file( "test1.txt" );
1682
1683 rec = MsiCreateRecord( 2 );
1684 MsiRecordSetStringA( rec, 1, "data1" );
1685
1686 r = MsiRecordSetStreamA( rec, 2, "test1.txt" );
1687 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r );
1688
1689 DeleteFileA( "test1.txt" );
1690
1691 r = MsiDatabaseOpenViewA( hdb,
1692 "INSERT INTO `_Streams` ( `Name`, `Data` ) VALUES ( ?, ? )", &view );
1693 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r );
1694
1695 r = MsiViewExecute( view, rec );
1696 ok( r == ERROR_FUNCTION_FAILED, "got %u\n", r );
1697
1698 MsiCloseHandle( rec );
1699 MsiViewClose( view );
1700 MsiCloseHandle( view );
1701
1702 r = MsiDatabaseOpenViewA( hdb,
1703 "SELECT `Name`, `Data` FROM `_Streams` WHERE `Name` = 'data'", &view );
1704 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1705
1706 r = MsiViewExecute( view, 0 );
1707 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1708
1709 r = MsiViewFetch( view, &rec );
1710 ok( r == ERROR_SUCCESS, "Failed to fetch record: %d\n", r);
1711
1712 size = MAX_PATH;
1713 r = MsiRecordGetStringA( rec, 1, file, &size );
1714 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r);
1715 ok( !lstrcmpA(file, "data"), "Expected 'data', got %s\n", file);
1716
1717 size = MAX_PATH;
1718 memset(buf, 0, MAX_PATH);
1719 r = MsiRecordReadStream( rec, 2, buf, &size );
1720 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r);
1721 ok( !lstrcmpA(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf);
1722
1723 MsiCloseHandle( rec );
1724 MsiViewClose( view );
1725 MsiCloseHandle( view );
1726
1727 r = MsiDatabaseOpenViewA( hdb,
1728 "SELECT `Name`, `Data` FROM `_Streams` WHERE `Name` = 'data1'", &view );
1729 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1730
1731 r = MsiViewExecute( view, 0 );
1732 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1733
1734 r = MsiViewFetch( view, &rec );
1735 ok( r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
1736
1737 size = MAX_PATH;
1738 r = MsiRecordGetStringA( rec, 1, file, &size );
1739 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r);
1740 ok( !lstrcmpA(file, "data1"), "Expected 'data1', got %s\n", file);
1741
1742 size = MAX_PATH;
1743 memset(buf, 0, MAX_PATH);
1744 r = MsiRecordReadStream( rec, 2, buf, &size );
1745 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r);
1746 ok( !lstrcmpA(buf, "test1.txt\n"), "Expected 'test1.txt\\n', got %s\n", buf);
1747
1748 MsiCloseHandle( rec );
1749 MsiViewClose( view );
1750 MsiCloseHandle( view );
1751
1752 /* perform an update */
1753 create_file( "test2.txt" );
1754 rec = MsiCreateRecord( 1 );
1755
1756 r = MsiRecordSetStreamA( rec, 1, "test2.txt" );
1757 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r);
1758
1759 DeleteFileA("test2.txt");
1760
1761 r = MsiDatabaseOpenViewA( hdb,
1762 "UPDATE `_Streams` SET `Data` = ? WHERE `Name` = 'data1'", &view );
1763 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1764
1765 r = MsiViewExecute( view, rec );
1766 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1767
1768 MsiCloseHandle( rec );
1769 MsiViewClose( view );
1770 MsiCloseHandle( view );
1771
1772 r = MsiDatabaseOpenViewA( hdb,
1773 "SELECT `Name`, `Data` FROM `_Streams` WHERE `Name` = 'data1'", &view );
1774 ok( r == ERROR_SUCCESS, "Failed to open database view: %d\n", r);
1775
1776 r = MsiViewExecute( view, 0 );
1777 ok( r == ERROR_SUCCESS, "Failed to execute view: %d\n", r);
1778
1779 r = MsiViewFetch( view, &rec );
1780 ok( r == ERROR_SUCCESS, "Failed to fetch record: %d\n", r);
1781
1782 size = MAX_PATH;
1783 r = MsiRecordGetStringA( rec, 1, file, &size );
1784 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r);
1785 ok( !lstrcmpA(file, "data1"), "Expected 'data1', got %s\n", file);
1786
1787 size = MAX_PATH;
1788 memset(buf, 0, MAX_PATH);
1789 r = MsiRecordReadStream( rec, 2, buf, &size );
1790 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r);
1791 ok( !lstrcmpA(buf, "test2.txt\n"), "Expected 'test2.txt\\n', got %s\n", buf);
1792
1793 MsiCloseHandle( rec );
1794 MsiViewClose( view );
1795 MsiCloseHandle( view );
1796 MsiCloseHandle( hdb );
1797 DeleteFileA(msifile);
1798 }
1799
1800 static void test_binary(void)
1801 {
1802 MSIHANDLE hdb = 0, rec;
1803 char file[MAX_PATH];
1804 char buf[MAX_PATH];
1805 DWORD size;
1806 LPCSTR query;
1807 UINT r;
1808
1809 /* insert a file into the Binary table */
1810 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb );
1811 ok( r == ERROR_SUCCESS , "Failed to open database\n" );
1812
1813 query = "CREATE TABLE `Binary` ( `Name` CHAR(72) NOT NULL, `ID` INT NOT NULL, `Data` OBJECT PRIMARY KEY `Name`, `ID`)";
1814 r = run_query( hdb, 0, query );
1815 ok( r == ERROR_SUCCESS, "Cannot create Binary table: %d\n", r );
1816
1817 create_file( "test.txt" );
1818 rec = MsiCreateRecord( 1 );
1819 r = MsiRecordSetStreamA( rec, 1, "test.txt" );
1820 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r);
1821 DeleteFileA( "test.txt" );
1822
1823 /* try a name that exceeds maximum OLE stream name length */
1824 query = "INSERT INTO `Binary` ( `Name`, `ID`, `Data` ) VALUES ( 'encryption.dll.CB4E6205_F99A_4C51_ADD4_184506EFAB87', 10000, ? )";
1825 r = run_query( hdb, rec, query );
1826 ok( r == ERROR_SUCCESS, "Insert into Binary table failed: %d\n", r );
1827
1828 r = MsiCloseHandle( rec );
1829 ok( r == ERROR_SUCCESS , "Failed to close record handle\n" );
1830
1831 r = MsiDatabaseCommit( hdb );
1832 ok( r == ERROR_FUNCTION_FAILED , "got %u\n", r );
1833
1834 r = MsiCloseHandle( hdb );
1835 ok( r == ERROR_SUCCESS , "Failed to close database\n" );
1836
1837 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb );
1838 ok( r == ERROR_SUCCESS , "Failed to open database\n" );
1839
1840 query = "CREATE TABLE `Binary` ( `Name` CHAR(72) NOT NULL, `ID` INT NOT NULL, `Data` OBJECT PRIMARY KEY `Name`, `ID`)";
1841 r = run_query( hdb, 0, query );
1842 ok( r == ERROR_SUCCESS, "Cannot create Binary table: %d\n", r );
1843
1844 create_file( "test.txt" );
1845 rec = MsiCreateRecord( 1 );
1846 r = MsiRecordSetStreamA( rec, 1, "test.txt" );
1847 ok( r == ERROR_SUCCESS, "Failed to add stream data to the record: %d\n", r );
1848 DeleteFileA( "test.txt" );
1849
1850 query = "INSERT INTO `Binary` ( `Name`, `ID`, `Data` ) VALUES ( 'filename1', 1, ? )";
1851 r = run_query( hdb, rec, query );
1852 ok( r == ERROR_SUCCESS, "Insert into Binary table failed: %d\n", r );
1853
1854 query = "INSERT INTO `Binary` ( `Name`, `ID`, `Data` ) VALUES ( 'filename1', 1, ? )";
1855 r = run_query( hdb, rec, query );
1856 ok( r == ERROR_FUNCTION_FAILED, "got %u\n", r );
1857
1858 r = MsiCloseHandle( rec );
1859 ok( r == ERROR_SUCCESS , "Failed to close record handle\n" );
1860
1861 r = MsiDatabaseCommit( hdb );
1862 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
1863
1864 r = MsiCloseHandle( hdb );
1865 ok( r == ERROR_SUCCESS , "Failed to close database\n" );
1866
1867 /* read file from the Stream table */
1868 r = MsiOpenDatabaseW( msifileW, MSIDBOPEN_READONLY, &hdb );
1869 ok( r == ERROR_SUCCESS , "Failed to open database\n" );
1870
1871 query = "SELECT * FROM `_Streams`";
1872 r = do_query( hdb, query, &rec );
1873 ok( r == ERROR_SUCCESS, "SELECT query failed: %d\n", r );
1874
1875 size = MAX_PATH;
1876 r = MsiRecordGetStringA( rec, 1, file, &size );
1877 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r );
1878 ok( !lstrcmpA(file, "Binary.filename1.1"), "Expected 'Binary.filename1.1', got %s\n", file );
1879
1880 size = MAX_PATH;
1881 memset( buf, 0, MAX_PATH );
1882 r = MsiRecordReadStream( rec, 2, buf, &size );
1883 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r );
1884 ok( !lstrcmpA(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf );
1885
1886 r = MsiCloseHandle( rec );
1887 ok( r == ERROR_SUCCESS , "Failed to close record handle\n" );
1888
1889 /* read file from the Binary table */
1890 query = "SELECT * FROM `Binary`";
1891 r = do_query( hdb, query, &rec );
1892 ok( r == ERROR_SUCCESS, "SELECT query failed: %d\n", r );
1893
1894 size = MAX_PATH;
1895 r = MsiRecordGetStringA( rec, 1, file, &size );
1896 ok( r == ERROR_SUCCESS, "Failed to get string: %d\n", r );
1897 ok( !lstrcmpA(file, "filename1"), "Expected 'filename1', got %s\n", file );
1898
1899 size = MAX_PATH;
1900 memset( buf, 0, MAX_PATH );
1901 r = MsiRecordReadStream( rec, 3, buf, &size );
1902 ok( r == ERROR_SUCCESS, "Failed to get stream: %d\n", r );
1903 ok( !lstrcmpA(buf, "test.txt\n"), "Expected 'test.txt\\n', got %s\n", buf );
1904
1905 r = MsiCloseHandle( rec );
1906 ok( r == ERROR_SUCCESS , "Failed to close record handle\n" );
1907
1908 r = MsiCloseHandle( hdb );
1909 ok( r == ERROR_SUCCESS , "Failed to close database\n" );
1910
1911 DeleteFileA( msifile );
1912 }
1913
1914 static void test_where_not_in_selected(void)
1915 {
1916 MSIHANDLE hdb = 0, rec, view;
1917 LPCSTR query;
1918 UINT r;
1919
1920 hdb = create_db();
1921 ok( hdb, "failed to create db\n");
1922
1923 r = run_query(hdb, 0,
1924 "CREATE TABLE `IESTable` ("
1925 "`Action` CHAR(64), "
1926 "`Condition` CHAR(64), "
1927 "`Sequence` LONG PRIMARY KEY `Sequence`)");
1928 ok( r == S_OK, "Cannot create IESTable table: %d\n", r);
1929
1930 r = run_query(hdb, 0,
1931 "CREATE TABLE `CATable` ("
1932 "`Action` CHAR(64), "
1933 "`Type` LONG PRIMARY KEY `Type`)");
1934 ok( r == S_OK, "Cannot create CATable table: %d\n", r);
1935
1936 r = run_query(hdb, 0, "INSERT INTO `IESTable` "
1937 "( `Action`, `Condition`, `Sequence`) "
1938 "VALUES ( 'clean', 'cond4', 4)");
1939 ok( r == S_OK, "cannot add entry to IESTable table:%d\n", r );
1940
1941 r = run_query(hdb, 0, "INSERT INTO `IESTable` "
1942 "( `Action`, `Condition`, `Sequence`) "
1943 "VALUES ( 'depends', 'cond1', 1)");
1944 ok( r == S_OK, "cannot add entry to IESTable table:%d\n", r );
1945
1946 r = run_query(hdb, 0, "INSERT INTO `IESTable` "
1947 "( `Action`, `Condition`, `Sequence`) "
1948 "VALUES ( 'build', 'cond2', 2)");
1949 ok( r == S_OK, "cannot add entry to IESTable table:%d\n", r );
1950
1951 r = run_query(hdb, 0, "INSERT INTO `IESTable` "
1952 "( `Action`, `Condition`, `Sequence`) "
1953 "VALUES ( 'build2', 'cond6', 6)");
1954 ok( r == S_OK, "cannot add entry to IESTable table:%d\n", r );
1955
1956 r = run_query(hdb, 0, "INSERT INTO `IESTable` "
1957 "( `Action`, `Condition`, `Sequence`) "
1958 "VALUES ( 'build', 'cond3', 3)");
1959 ok(r == S_OK, "cannot add entry to IESTable table:%d\n", r );
1960
1961 r = run_query(hdb, 0, "INSERT INTO `CATable` "
1962 "( `Action`, `Type` ) "
1963 "VALUES ( 'build', 32)");
1964 ok(r == S_OK, "cannot add entry to CATable table:%d\n", r );
1965
1966 r = run_query(hdb, 0, "INSERT INTO `CATable` "
1967 "( `Action`, `Type` ) "
1968 "VALUES ( 'depends', 64)");
1969 ok(r == S_OK, "cannot add entry to CATable table:%d\n", r );
1970
1971 r = run_query(hdb, 0, "INSERT INTO `CATable` "
1972 "( `Action`, `Type` ) "
1973 "VALUES ( 'clean', 63)");
1974 ok(r == S_OK, "cannot add entry to CATable table:%d\n", r );
1975
1976 r = run_query(hdb, 0, "INSERT INTO `CATable` "
1977 "( `Action`, `Type` ) "
1978 "VALUES ( 'build2', 34)");
1979 ok(r == S_OK, "cannot add entry to CATable table:%d\n", r );
1980 query = "Select IESTable.Condition from CATable, IESTable where "
1981 "CATable.Action = IESTable.Action and CATable.Type = 32";
1982 r = MsiDatabaseOpenViewA(hdb, query, &view);
1983 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
1984
1985 r = MsiViewExecute(view, 0);
1986 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
1987
1988 r = MsiViewFetch(view, &rec);
1989 ok( r == ERROR_SUCCESS, "failed to fetch view: %d\n", r );
1990
1991 ok( check_record( rec, 1, "cond2"), "wrong condition\n");
1992
1993 MsiCloseHandle( rec );
1994 r = MsiViewFetch(view, &rec);
1995 ok( r == ERROR_SUCCESS, "failed to fetch view: %d\n", r );
1996
1997 ok( check_record( rec, 1, "cond3"), "wrong condition\n");
1998
1999 MsiCloseHandle( rec );
2000 MsiViewClose(view);
2001 MsiCloseHandle(view);
2002
2003 MsiCloseHandle( hdb );
2004 DeleteFileA(msifile);
2005 }
2006
2007
2008 static void test_where(void)
2009 {
2010 MSIHANDLE hdb = 0, rec, view;
2011 LPCSTR query;
2012 UINT r;
2013 DWORD size;
2014 CHAR buf[MAX_PATH];
2015 UINT count;
2016
2017 hdb = create_db();
2018 ok( hdb, "failed to create db\n");
2019
2020 r = run_query( hdb, 0,
2021 "CREATE TABLE `Media` ("
2022 "`DiskId` SHORT NOT NULL, "
2023 "`LastSequence` LONG, "
2024 "`DiskPrompt` CHAR(64) LOCALIZABLE, "
2025 "`Cabinet` CHAR(255), "
2026 "`VolumeLabel` CHAR(32), "
2027 "`Source` CHAR(72) "
2028 "PRIMARY KEY `DiskId`)" );
2029 ok( r == S_OK, "cannot create Media table: %d\n", r );
2030
2031 r = run_query( hdb, 0, "INSERT INTO `Media` "
2032 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
2033 "VALUES ( 1, 0, '', 'zero.cab', '', '' )" );
2034 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
2035
2036 r = run_query( hdb, 0, "INSERT INTO `Media` "
2037 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
2038 "VALUES ( 2, 1, '', 'one.cab', '', '' )" );
2039 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
2040
2041 r = run_query( hdb, 0, "INSERT INTO `Media` "
2042 "( `DiskId`, `LastSequence`, `DiskPrompt`, `Cabinet`, `VolumeLabel`, `Source` ) "
2043 "VALUES ( 3, 2, '', 'two.cab', '', '' )" );
2044 ok( r == S_OK, "cannot add file to the Media table: %d\n", r );
2045
2046 query = "SELECT * FROM `Media`";
2047 r = do_query(hdb, query, &rec);
2048 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %d\n", r);
2049 ok( check_record( rec, 4, "zero.cab"), "wrong cabinet\n");
2050 MsiCloseHandle( rec );
2051
2052 query = "SELECT * FROM `Media` WHERE `LastSequence` >= 1";
2053 r = do_query(hdb, query, &rec);
2054 ok(r == ERROR_SUCCESS, "MsiViewFetch failed: %d\n", r);
2055 ok( check_record( rec, 4, "one.cab"), "wrong cabinet\n");
2056
2057 r = MsiRecordGetInteger(rec, 1);
2058 ok( 2 == r, "field wrong\n");
2059 r = MsiRecordGetInteger(rec, 2);
2060 ok( 1 == r, "field wrong\n");
2061 MsiCloseHandle( rec );
2062
2063 query = "SELECT `DiskId` FROM `Media` WHERE `LastSequence` >= 1 AND DiskId >= 0";
2064 r = MsiDatabaseOpenViewA(hdb, query, &view);
2065 ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
2066
2067 r = MsiViewExecute(view, 0);
2068 ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
2069
2070 r = MsiViewFetch(view, &rec);
2071 ok( r == ERROR_SUCCESS, "failed to fetch view: %d\n", r );
2072
2073 count = MsiRecordGetFieldCount( rec );
2074 ok( count == 1, "Expected 1 record fields, got %d\n", count );
2075
2076 size = MAX_PATH;
2077 r = MsiRecordGetStringA( rec, 1, buf, &size );
2078 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2079 ok( !lstrcmpA( buf, "2" ), "For (row %d, column 1) expected '%d', got %s\n", 0, 2, buf );
2080 MsiCloseHandle( rec );
2081
2082 r = MsiViewFetch(view, &rec);
2083 ok( r == ERROR_SUCCESS, "failed to fetch view: %d\n", r );
2084
2085 size = MAX_PATH;
2086 r = MsiRecordGetStringA( rec, 1, buf, &size );
2087 ok( r == ERROR_SUCCESS, "failed to get record string: %d\n", r );
2088 ok( !lstrcmpA( buf, "3" ), "For (row %d, column 1) expected '%d', got %s\n", 1, 3, buf );
2089 MsiCloseHandle( rec );
2090
2091 r = MsiViewFetch(view, &rec);
2092 ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
2093
2094 MsiViewClose(view);
2095 MsiCloseHandle(view);
2096
2097 MsiCloseHandle( rec );
2098
2099 rec = 0;
2100 query = "SELECT * FROM `Media` WHERE `DiskPrompt` IS NULL";
2101 r = do_query(hdb, query, &rec);
2102 ok( r == ERROR_SUCCESS, "query failed: %d\n", r );
2103 MsiCloseHandle( rec );
2104
2105 rec = 0;
2106 query = "SELECT * FROM `Media` WHERE `DiskPrompt` < 'Cabinet'";
2107 r = do_query(hdb, query, &rec);
2108 ok( r == ERROR_BAD_QUERY_SYNTAX, "query failed: %d\n", r );
2109 MsiCloseHandle( rec );
2110
2111 rec = 0;
2112 query = "SELECT * FROM `Media` WHERE `DiskPrompt` > 'Cabinet'";
2113 r = do_query(hdb, query, &rec);
2114 ok( r == ERROR_BAD_QUERY_SYNTAX, "query failed: %d\n", r );
2115 MsiCloseHandle( rec );
2116
2117 rec = 0;
2118 query = "SELECT * FROM `Media` WHERE `DiskPrompt` <> 'Cabinet'";
2119 r = do_query(hdb, query, &rec);
2120 ok( r == ERROR_SUCCESS, "query failed: %d\n", r );
2121 MsiCloseHandle( rec );
2122
2123 rec = 0;
2124 query = "SELECT * FROM `Media` WHERE `DiskPrompt` = 'Cabinet'";
2125 r = do_query(hdb, query, &rec);
2126 ok( r == ERROR_NO_MORE_ITEMS, "query failed: %d\n", r );
2127 MsiCloseHandle( rec );
2128
2129 rec = MsiCreateRecord(1);
2130 MsiRecordSetStringA(rec, 1, "");
2131
2132 query = "SELECT * FROM `Media` WHERE `DiskPrompt` = ?";
2133 r = MsiDatabaseOpenViewA(hdb, query, &view);
2134 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2135 r = MsiViewExecute(view, rec);
2136 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2137
2138 MsiCloseHandle(rec);
2139
2140 r = MsiViewFetch(view, &rec);
2141 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2142
2143 MsiCloseHandle(rec);
2144 MsiViewClose(view);
2145 MsiCloseHandle(view);
2146
2147 MsiCloseHandle( hdb );
2148 DeleteFileA(msifile);
2149 }
2150
2151 static CHAR CURR_DIR[MAX_PATH];
2152
2153 static const CHAR test_data[] = "FirstPrimaryColumn\tSecondPrimaryColumn\tShortInt\tShortIntNullable\tLongInt\tLongIntNullable\tString\tLocalizableString\tLocalizableStringNullable\n"
2154 "s255\ti2\ti2\tI2\ti4\tI4\tS255\tS0\ts0\n"
2155 "TestTable\tFirstPrimaryColumn\n"
2156 "stringage\t5\t2\t\t2147483640\t-2147483640\tanother string\tlocalizable\tduh\n";
2157
2158 static const CHAR two_primary[] = "PrimaryOne\tPrimaryTwo\n"
2159 "s255\ts255\n"
2160 "TwoPrimary\tPrimaryOne\tPrimaryTwo\n"
2161 "papaya\tleaf\n"
2162 "papaya\tflower\n";
2163
2164 static const CHAR endlines1[] = "A\tB\tC\tD\tE\tF\r\n"
2165 "s72\ts72\ts72\ts72\ts72\ts72\n"
2166 "Table\tA\r\n"
2167 "a\tb\tc\td\te\tf\n"
2168 "g\th\ti\t\rj\tk\tl\r\n";
2169
2170 static const CHAR endlines2[] = "A\tB\tC\tD\tE\tF\r"
2171 "s72\ts72\ts72\ts72\ts72\ts72\n"
2172 "Table2\tA\r\n"
2173 "a\tb\tc\td\te\tf\n"
2174 "g\th\ti\tj\tk\tl\r\n";
2175
2176 static const CHAR suminfo[] = "PropertyId\tValue\n"
2177 "i2\tl255\n"
2178 "_SummaryInformation\tPropertyId\n"
2179 "1\t1252\n"
2180 "2\tInstaller Database\n"
2181 "3\tInstaller description\n"
2182 "4\tWineHQ\n"
2183 "5\tInstaller\n"
2184 "6\tInstaller comments\n"
2185 "7\tIntel;1033,2057\n"
2186 "9\t{12345678-1234-1234-1234-123456789012}\n"
2187 "12\t2009/04/12 15:46:11\n"
2188 "13\t2009/04/12 15:46:11\n"
2189 "14\t200\n"
2190 "15\t2\n"
2191 "18\tVim\n"
2192 "19\t2\n";
2193
2194 static void write_file(const CHAR *filename, const char *data, int data_size)
2195 {
2196 DWORD size;
2197
2198 HANDLE hf = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
2199 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2200 WriteFile(hf, data, data_size, &size, NULL);
2201 CloseHandle(hf);
2202 }
2203
2204 static UINT add_table_to_db(MSIHANDLE hdb, LPCSTR table_data)
2205 {
2206 UINT r;
2207
2208 write_file("temp_file", table_data, (lstrlenA(table_data) - 1) * sizeof(char));
2209 r = MsiDatabaseImportA(hdb, CURR_DIR, "temp_file");
2210 DeleteFileA("temp_file");
2211
2212 return r;
2213 }
2214
2215 static void test_suminfo_import(void)
2216 {
2217 MSIHANDLE hdb, hsi, view = 0;
2218 LPCSTR query;
2219 UINT r, count, size, type;
2220 char str_value[50];
2221 INT int_value;
2222 FILETIME ft_value;
2223
2224 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
2225
2226 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
2227 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2228
2229 r = add_table_to_db(hdb, suminfo);
2230 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2231
2232 /* _SummaryInformation is not imported as a regular table... */
2233
2234 query = "SELECT * FROM `_SummaryInformation`";
2235 r = MsiDatabaseOpenViewA(hdb, query, &view);
2236 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %u\n", r);
2237 MsiCloseHandle(view);
2238
2239 /* ...its data is added to the special summary information stream */
2240
2241 r = MsiGetSummaryInformationA(hdb, NULL, 0, &hsi);
2242 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2243
2244 r = MsiSummaryInfoGetPropertyCount(hsi, &count);
2245 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2246 ok(count == 14, "Expected 14, got %u\n", count);
2247
2248 r = MsiSummaryInfoGetPropertyA(hsi, PID_CODEPAGE, &type, &int_value, NULL, NULL, NULL);
2249 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2250 ok(type == VT_I2, "Expected VT_I2, got %u\n", type);
2251 ok(int_value == 1252, "Expected 1252, got %d\n", int_value);
2252
2253 size = sizeof(str_value);
2254 r = MsiSummaryInfoGetPropertyA(hsi, PID_TITLE, &type, NULL, NULL, str_value, &size);
2255 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2256 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2257 ok(size == 18, "Expected 18, got %u\n", size);
2258 ok(!strcmp(str_value, "Installer Database"),
2259 "Expected \"Installer Database\", got %s\n", str_value);
2260
2261 size = sizeof(str_value);
2262 r = MsiSummaryInfoGetPropertyA(hsi, PID_SUBJECT, &type, NULL, NULL, str_value, &size);
2263 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2264 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2265 ok(!strcmp(str_value, "Installer description"),
2266 "Expected \"Installer description\", got %s\n", str_value);
2267
2268 size = sizeof(str_value);
2269 r = MsiSummaryInfoGetPropertyA(hsi, PID_AUTHOR, &type, NULL, NULL, str_value, &size);
2270 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2271 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2272 ok(!strcmp(str_value, "WineHQ"),
2273 "Expected \"WineHQ\", got %s\n", str_value);
2274
2275 size = sizeof(str_value);
2276 r = MsiSummaryInfoGetPropertyA(hsi, PID_KEYWORDS, &type, NULL, NULL, str_value, &size);
2277 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2278 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2279 ok(!strcmp(str_value, "Installer"),
2280 "Expected \"Installer\", got %s\n", str_value);
2281
2282 size = sizeof(str_value);
2283 r = MsiSummaryInfoGetPropertyA(hsi, PID_COMMENTS, &type, NULL, NULL, str_value, &size);
2284 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2285 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2286 ok(!strcmp(str_value, "Installer comments"),
2287 "Expected \"Installer comments\", got %s\n", str_value);
2288
2289 size = sizeof(str_value);
2290 r = MsiSummaryInfoGetPropertyA(hsi, PID_TEMPLATE, &type, NULL, NULL, str_value, &size);
2291 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2292 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2293 ok(!strcmp(str_value, "Intel;1033,2057"),
2294 "Expected \"Intel;1033,2057\", got %s\n", str_value);
2295
2296 size = sizeof(str_value);
2297 r = MsiSummaryInfoGetPropertyA(hsi, PID_REVNUMBER, &type, NULL, NULL, str_value, &size);
2298 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2299 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2300 ok(!strcmp(str_value, "{12345678-1234-1234-1234-123456789012}"),
2301 "Expected \"{12345678-1234-1234-1234-123456789012}\", got %s\n", str_value);
2302
2303 r = MsiSummaryInfoGetPropertyA(hsi, PID_CREATE_DTM, &type, NULL, &ft_value, NULL, NULL);
2304 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2305 ok(type == VT_FILETIME, "Expected VT_FILETIME, got %u\n", type);
2306
2307 r = MsiSummaryInfoGetPropertyA(hsi, PID_LASTSAVE_DTM, &type, NULL, &ft_value, NULL, NULL);
2308 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2309 ok(type == VT_FILETIME, "Expected VT_FILETIME, got %u\n", type);
2310
2311 r = MsiSummaryInfoGetPropertyA(hsi, PID_PAGECOUNT, &type, &int_value, NULL, NULL, NULL);
2312 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2313 ok(type == VT_I4, "Expected VT_I4, got %u\n", type);
2314 ok(int_value == 200, "Expected 200, got %d\n", int_value);
2315
2316 r = MsiSummaryInfoGetPropertyA(hsi, PID_WORDCOUNT, &type, &int_value, NULL, NULL, NULL);
2317 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2318 ok(type == VT_I4, "Expected VT_I4, got %u\n", type);
2319 ok(int_value == 2, "Expected 2, got %d\n", int_value);
2320
2321 r = MsiSummaryInfoGetPropertyA(hsi, PID_SECURITY, &type, &int_value, NULL, NULL, NULL);
2322 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2323 ok(type == VT_I4, "Expected VT_I4, got %u\n", type);
2324 ok(int_value == 2, "Expected 2, got %d\n", int_value);
2325
2326 size = sizeof(str_value);
2327 r = MsiSummaryInfoGetPropertyA(hsi, PID_APPNAME, &type, NULL, NULL, str_value, &size);
2328 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %u\n", r);
2329 ok(type == VT_LPSTR, "Expected VT_LPSTR, got %u\n", type);
2330 ok(!strcmp(str_value, "Vim"), "Expected \"Vim\", got %s\n", str_value);
2331
2332 MsiCloseHandle(hsi);
2333 MsiCloseHandle(hdb);
2334 DeleteFileA(msifile);
2335 }
2336
2337 static void test_msiimport(void)
2338 {
2339 MSIHANDLE hdb, view, rec;
2340 LPCSTR query;
2341 UINT r, count;
2342 signed int i;
2343
2344 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
2345
2346 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
2347 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2348
2349 r = add_table_to_db(hdb, test_data);
2350 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2351
2352 r = add_table_to_db(hdb, two_primary);
2353 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2354
2355 r = add_table_to_db(hdb, endlines1);
2356 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2357
2358 r = add_table_to_db(hdb, endlines2);
2359 ok(r == ERROR_FUNCTION_FAILED, "Expected ERROR_FUNCTION_FAILED, got %d\n", r);
2360
2361 query = "SELECT * FROM `TestTable`";
2362 r = MsiDatabaseOpenViewA(hdb, query, &view);
2363 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2364
2365 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
2366 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2367 count = MsiRecordGetFieldCount(rec);
2368 ok(count == 9, "Expected 9, got %d\n", count);
2369 ok(check_record(rec, 1, "FirstPrimaryColumn"), "Expected FirstPrimaryColumn\n");
2370 ok(check_record(rec, 2, "SecondPrimaryColumn"), "Expected SecondPrimaryColumn\n");
2371 ok(check_record(rec, 3, "ShortInt"), "Expected ShortInt\n");
2372 ok(check_record(rec, 4, "ShortIntNullable"), "Expected ShortIntNullalble\n");
2373 ok(check_record(rec, 5, "LongInt"), "Expected LongInt\n");
2374 ok(check_record(rec, 6, "LongIntNullable"), "Expected LongIntNullalble\n");
2375 ok(check_record(rec, 7, "String"), "Expected String\n");
2376 ok(check_record(rec, 8, "LocalizableString"), "Expected LocalizableString\n");
2377 ok(check_record(rec, 9, "LocalizableStringNullable"), "Expected LocalizableStringNullable\n");
2378 MsiCloseHandle(rec);
2379
2380 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
2381 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2382 count = MsiRecordGetFieldCount(rec);
2383 ok(count == 9, "Expected 9, got %d\n", count);
2384 ok(check_record(rec, 1, "s255"), "Expected s255\n");
2385 ok(check_record(rec, 2, "i2"), "Expected i2\n");
2386 ok(check_record(rec, 3, "i2"), "Expected i2\n");
2387 ok(check_record(rec, 4, "I2"), "Expected I2\n");
2388 ok(check_record(rec, 5, "i4"), "Expected i4\n");
2389 ok(check_record(rec, 6, "I4"), "Expected I4\n");
2390 ok(check_record(rec, 7, "S255"), "Expected S255\n");
2391 ok(check_record(rec, 8, "S0"), "Expected S0\n");
2392 ok(check_record(rec, 9, "s0"), "Expected s0\n");
2393 MsiCloseHandle(rec);
2394
2395 query = "SELECT * FROM `TestTable`";
2396 r = do_query(hdb, query, &rec);
2397 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2398 ok(check_record(rec, 1, "stringage"), "Expected 'stringage'\n");
2399 ok(check_record(rec, 7, "another string"), "Expected 'another string'\n");
2400 ok(check_record(rec, 8, "localizable"), "Expected 'localizable'\n");
2401 ok(check_record(rec, 9, "duh"), "Expected 'duh'\n");
2402
2403 i = MsiRecordGetInteger(rec, 2);
2404 ok(i == 5, "Expected 5, got %d\n", i);
2405
2406 i = MsiRecordGetInteger(rec, 3);
2407 ok(i == 2, "Expected 2, got %d\n", i);
2408
2409 i = MsiRecordGetInteger(rec, 4);
2410 ok(i == MSI_NULL_INTEGER, "Expected MSI_NULL_INTEGER, got %d\n", i);
2411
2412 i = MsiRecordGetInteger(rec, 5);
2413 ok(i == 2147483640, "Expected 2147483640, got %d\n", i);
2414
2415 i = MsiRecordGetInteger(rec, 6);
2416 ok(i == -2147483640, "Expected -2147483640, got %d\n", i);
2417
2418 MsiCloseHandle(rec);
2419 MsiViewClose(view);
2420 MsiCloseHandle(view);
2421
2422 query = "SELECT * FROM `TwoPrimary`";
2423 r = MsiDatabaseOpenViewA(hdb, query, &view);
2424 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2425
2426 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
2427 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2428 count = MsiRecordGetFieldCount(rec);
2429 ok(count == 2, "Expected 2, got %d\n", count);
2430 ok(check_record(rec, 1, "PrimaryOne"), "Expected PrimaryOne\n");
2431 ok(check_record(rec, 2, "PrimaryTwo"), "Expected PrimaryTwo\n");
2432
2433 MsiCloseHandle(rec);
2434
2435 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
2436 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2437 count = MsiRecordGetFieldCount(rec);
2438 ok(count == 2, "Expected 2, got %d\n", count);
2439 ok(check_record(rec, 1, "s255"), "Expected s255\n");
2440 ok(check_record(rec, 2, "s255"), "Expected s255\n");
2441 MsiCloseHandle(rec);
2442
2443 r = MsiViewExecute(view, 0);
2444 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2445
2446 r = MsiViewFetch(view, &rec);
2447 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2448
2449 ok(check_record(rec, 1, "papaya"), "Expected 'papaya'\n");
2450 ok(check_record(rec, 2, "leaf"), "Expected 'leaf'\n");
2451
2452 MsiCloseHandle(rec);
2453
2454 r = MsiViewFetch(view, &rec);
2455 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2456
2457 ok(check_record(rec, 1, "papaya"), "Expected 'papaya'\n");
2458 ok(check_record(rec, 2, "flower"), "Expected 'flower'\n");
2459
2460 MsiCloseHandle(rec);
2461
2462 r = MsiViewFetch(view, &rec);
2463 ok(r == ERROR_NO_MORE_ITEMS,
2464 "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
2465
2466 r = MsiViewClose(view);
2467 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2468
2469 MsiCloseHandle(view);
2470
2471 query = "SELECT * FROM `Table`";
2472 r = MsiDatabaseOpenViewA(hdb, query, &view);
2473 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2474
2475 r = MsiViewGetColumnInfo(view, MSICOLINFO_NAMES, &rec);
2476 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2477 count = MsiRecordGetFieldCount(rec);
2478 ok(count == 6, "Expected 6, got %d\n", count);
2479 ok(check_record(rec, 1, "A"), "Expected A\n");
2480 ok(check_record(rec, 2, "B"), "Expected B\n");
2481 ok(check_record(rec, 3, "C"), "Expected C\n");
2482 ok(check_record(rec, 4, "D"), "Expected D\n");
2483 ok(check_record(rec, 5, "E"), "Expected E\n");
2484 ok(check_record(rec, 6, "F"), "Expected F\n");
2485 MsiCloseHandle(rec);
2486
2487 r = MsiViewGetColumnInfo(view, MSICOLINFO_TYPES, &rec);
2488 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2489 count = MsiRecordGetFieldCount(rec);
2490 ok(count == 6, "Expected 6, got %d\n", count);
2491 ok(check_record(rec, 1, "s72"), "Expected s72\n");
2492 ok(check_record(rec, 2, "s72"), "Expected s72\n");
2493 ok(check_record(rec, 3, "s72"), "Expected s72\n");
2494 ok(check_record(rec, 4, "s72"), "Expected s72\n");
2495 ok(check_record(rec, 5, "s72"), "Expected s72\n");
2496 ok(check_record(rec, 6, "s72"), "Expected s72\n");
2497 MsiCloseHandle(rec);
2498
2499 MsiViewClose(view);
2500 MsiCloseHandle(view);
2501
2502 query = "SELECT * FROM `Table`";
2503 r = MsiDatabaseOpenViewA(hdb, query, &view);
2504 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2505
2506 r = MsiViewExecute(view, 0);
2507 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2508
2509 r = MsiViewFetch(view, &rec);
2510 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2511 ok(check_record(rec, 1, "a"), "Expected 'a'\n");
2512 ok(check_record(rec, 2, "b"), "Expected 'b'\n");
2513 ok(check_record(rec, 3, "c"), "Expected 'c'\n");
2514 ok(check_record(rec, 4, "d"), "Expected 'd'\n");
2515 ok(check_record(rec, 5, "e"), "Expected 'e'\n");
2516 ok(check_record(rec, 6, "f"), "Expected 'f'\n");
2517
2518 MsiCloseHandle(rec);
2519
2520 r = MsiViewFetch(view, &rec);
2521 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2522 ok(check_record(rec, 1, "g"), "Expected 'g'\n");
2523 ok(check_record(rec, 2, "h"), "Expected 'h'\n");
2524 ok(check_record(rec, 3, "i"), "Expected 'i'\n");
2525 ok(check_record(rec, 4, "j"), "Expected 'j'\n");
2526 ok(check_record(rec, 5, "k"), "Expected 'k'\n");
2527 ok(check_record(rec, 6, "l"), "Expected 'l'\n");
2528
2529 MsiCloseHandle(rec);
2530
2531 r = MsiViewFetch(view, &rec);
2532 ok(r == ERROR_NO_MORE_ITEMS,
2533 "Expected ERROR_NO_MORE_ITEMS, got %d\n", r);
2534
2535 MsiViewClose(view);
2536 MsiCloseHandle(view);
2537 MsiCloseHandle(hdb);
2538 DeleteFileA(msifile);
2539 }
2540
2541 static const CHAR bin_import_dat[] = "Name\tData\r\n"
2542 "s72\tV0\r\n"
2543 "Binary\tName\r\n"
2544 "filename1\tfilename1.ibd\r\n";
2545
2546 static void test_binary_import(void)
2547 {
2548 MSIHANDLE hdb = 0, rec;
2549 char file[MAX_PATH];
2550 char buf[MAX_PATH];
2551 char path[MAX_PATH];
2552 DWORD size;
2553 LPCSTR query;
2554 UINT r;
2555
2556 /* create files to import */
2557 write_file("bin_import.idt", bin_import_dat,
2558 (sizeof(bin_import_dat) - 1) * sizeof(char));
2559 CreateDirectoryA("bin_import", NULL);
2560 create_file_data("bin_import/filename1.ibd", "just some words", 15);
2561
2562 /* import files into database */
2563 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_CREATE, &hdb);
2564 ok( r == ERROR_SUCCESS , "Failed to open database\n");
2565
2566 GetCurrentDirectoryA(MAX_PATH, path);
2567 r = MsiDatabaseImportA(hdb, path, "bin_import.idt");
2568 ok(r == ERROR_SUCCESS , "Failed to import Binary table\n");
2569
2570 /* read file from the Binary table */
2571 query = "SELECT * FROM `Binary`";
2572 r = do_query(hdb, query, &rec);
2573 ok(r == ERROR_SUCCESS, "SELECT query failed: %d\n", r);
2574
2575 size = MAX_PATH;
2576 r = MsiRecordGetStringA(rec, 1, file, &size);
2577 ok(r == ERROR_SUCCESS, "Failed to get string: %d\n", r);
2578 ok(!lstrcmpA(file, "filename1"), "Expected 'filename1', got %s\n", file);
2579
2580 size = MAX_PATH;
2581 memset(buf, 0, MAX_PATH);
2582 r = MsiRecordReadStream(rec, 2, buf, &size);
2583 ok(r == ERROR_SUCCESS, "Failed to get stream: %d\n", r);
2584 ok(!lstrcmpA(buf, "just some words"), "Expected 'just some words', got %s\n", buf);
2585
2586 r = MsiCloseHandle(rec);
2587 ok(r == ERROR_SUCCESS , "Failed to close record handle\n");
2588
2589 r = MsiCloseHandle(hdb);
2590 ok(r == ERROR_SUCCESS , "Failed to close database\n");
2591
2592 DeleteFileA("bin_import/filename1.ibd");
2593 RemoveDirectoryA("bin_import");
2594 DeleteFileA("bin_import.idt");
2595 }
2596
2597 static void test_markers(void)
2598 {
2599 MSIHANDLE hdb, rec;
2600 LPCSTR query;
2601 UINT r;
2602
2603 hdb = create_db();
2604 ok( hdb, "failed to create db\n");
2605
2606 rec = MsiCreateRecord(3);
2607 MsiRecordSetStringA(rec, 1, "Table");
2608 MsiRecordSetStringA(rec, 2, "Apples");
2609 MsiRecordSetStringA(rec, 3, "Oranges");
2610
2611 /* try a legit create */
2612 query = "CREATE TABLE `Table` ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
2613 r = run_query(hdb, 0, query);
2614 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2615 MsiCloseHandle(rec);
2616
2617 /* try table name as marker */
2618 rec = MsiCreateRecord(1);
2619 MsiRecordSetStringA(rec, 1, "Fable");
2620 query = "CREATE TABLE `?` ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
2621 r = run_query(hdb, rec, query);
2622 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2623
2624 /* verify that we just created a table called '?', not 'Fable' */
2625 r = try_query(hdb, "SELECT * from `Fable`");
2626 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2627
2628 r = try_query(hdb, "SELECT * from `?`");
2629 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2630
2631 /* try table name as marker without backticks */
2632 MsiRecordSetStringA(rec, 1, "Mable");
2633 query = "CREATE TABLE ? ( `One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
2634 r = run_query(hdb, rec, query);
2635 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2636
2637 /* try one column name as marker */
2638 MsiRecordSetStringA(rec, 1, "One");
2639 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`)";
2640 r = run_query(hdb, rec, query);
2641 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2642 MsiCloseHandle(rec);
2643
2644 /* try column names as markers */
2645 rec = MsiCreateRecord(2);
2646 MsiRecordSetStringA(rec, 1, "One");
2647 MsiRecordSetStringA(rec, 2, "Two");
2648 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `One`)";
2649 r = run_query(hdb, rec, query);
2650 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2651 MsiCloseHandle(rec);
2652
2653 /* try names with backticks */
2654 rec = MsiCreateRecord(3);
2655 MsiRecordSetStringA(rec, 1, "One");
2656 MsiRecordSetStringA(rec, 2, "Two");
2657 MsiRecordSetStringA(rec, 3, "One");
2658 query = "CREATE TABLE `Mable` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `?`)";
2659 r = run_query(hdb, rec, query);
2660 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2661
2662 /* try names with backticks, minus definitions */
2663 query = "CREATE TABLE `Mable` ( `?`, `?` PRIMARY KEY `?`)";
2664 r = run_query(hdb, rec, query);
2665 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2666
2667 /* try names without backticks */
2668 query = "CREATE TABLE `Mable` ( ? SHORT NOT NULL, ? CHAR(255) PRIMARY KEY ?)";
2669 r = run_query(hdb, rec, query);
2670 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2671 MsiCloseHandle(rec);
2672
2673 /* try one long marker */
2674 rec = MsiCreateRecord(1);
2675 MsiRecordSetStringA(rec, 1, "`One` SHORT NOT NULL, `Two` CHAR(255) PRIMARY KEY `One`");
2676 query = "CREATE TABLE `Mable` ( ? )";
2677 r = run_query(hdb, rec, query);
2678 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2679 MsiCloseHandle(rec);
2680
2681 /* try all names as markers */
2682 rec = MsiCreateRecord(4);
2683 MsiRecordSetStringA(rec, 1, "Mable");
2684 MsiRecordSetStringA(rec, 2, "One");
2685 MsiRecordSetStringA(rec, 3, "Two");
2686 MsiRecordSetStringA(rec, 4, "One");
2687 query = "CREATE TABLE `?` ( `?` SHORT NOT NULL, `?` CHAR(255) PRIMARY KEY `?`)";
2688 r = run_query(hdb, rec, query);
2689 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2690 MsiCloseHandle(rec);
2691
2692 /* try a legit insert */
2693 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( 5, 'hello' )";
2694 r = run_query(hdb, 0, query);
2695 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2696
2697 r = try_query(hdb, "SELECT * from `Table`");
2698 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2699
2700 /* try values as markers */
2701 rec = MsiCreateRecord(2);
2702 MsiRecordSetInteger(rec, 1, 4);
2703 MsiRecordSetStringA(rec, 2, "hi");
2704 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, '?' )";
2705 r = run_query(hdb, rec, query);
2706 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2707 MsiCloseHandle(rec);
2708
2709 /* try column names and values as markers */
2710 rec = MsiCreateRecord(4);
2711 MsiRecordSetStringA(rec, 1, "One");
2712 MsiRecordSetStringA(rec, 2, "Two");
2713 MsiRecordSetInteger(rec, 3, 5);
2714 MsiRecordSetStringA(rec, 4, "hi");
2715 query = "INSERT INTO `Table` ( `?`, `?` ) VALUES ( ?, '?' )";
2716 r = run_query(hdb, rec, query);
2717 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2718 MsiCloseHandle(rec);
2719
2720 /* try column names as markers */
2721 rec = MsiCreateRecord(2);
2722 MsiRecordSetStringA(rec, 1, "One");
2723 MsiRecordSetStringA(rec, 2, "Two");
2724 query = "INSERT INTO `Table` ( `?`, `?` ) VALUES ( 3, 'yellow' )";
2725 r = run_query(hdb, rec, query);
2726 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2727 MsiCloseHandle(rec);
2728
2729 /* try table name as a marker */
2730 rec = MsiCreateRecord(1);
2731 MsiRecordSetStringA(rec, 1, "Table");
2732 query = "INSERT INTO `?` ( `One`, `Two` ) VALUES ( 2, 'green' )";
2733 r = run_query(hdb, rec, query);
2734 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2735 MsiCloseHandle(rec);
2736
2737 /* try table name and values as markers */
2738 rec = MsiCreateRecord(3);
2739 MsiRecordSetStringA(rec, 1, "Table");
2740 MsiRecordSetInteger(rec, 2, 10);
2741 MsiRecordSetStringA(rec, 3, "haha");
2742 query = "INSERT INTO `?` ( `One`, `Two` ) VALUES ( ?, '?' )";
2743 r = run_query(hdb, rec, query);
2744 ok(r == ERROR_FUNCTION_FAILED, "Expected ERROR_FUNCTION_FAILED, got %d\n", r);
2745 MsiCloseHandle(rec);
2746
2747 /* try all markers */
2748 rec = MsiCreateRecord(5);
2749 MsiRecordSetStringA(rec, 1, "Table");
2750 MsiRecordSetStringA(rec, 1, "One");
2751 MsiRecordSetStringA(rec, 1, "Two");
2752 MsiRecordSetInteger(rec, 2, 10);
2753 MsiRecordSetStringA(rec, 3, "haha");
2754 query = "INSERT INTO `?` ( `?`, `?` ) VALUES ( ?, '?' )";
2755 r = run_query(hdb, rec, query);
2756 ok(r == ERROR_BAD_QUERY_SYNTAX, "Expected ERROR_BAD_QUERY_SYNTAX, got %d\n", r);
2757 MsiCloseHandle(rec);
2758
2759 /* insert an integer as a string */
2760 rec = MsiCreateRecord(2);
2761 MsiRecordSetStringA(rec, 1, "11");
2762 MsiRecordSetStringA(rec, 2, "hi");
2763 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, '?' )";
2764 r = run_query(hdb, rec, query);
2765 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2766 MsiCloseHandle(rec);
2767
2768 /* leave off the '' for the string */
2769 rec = MsiCreateRecord(2);
2770 MsiRecordSetInteger(rec, 1, 12);
2771 MsiRecordSetStringA(rec, 2, "hi");
2772 query = "INSERT INTO `Table` ( `One`, `Two` ) VALUES ( ?, ? )";
2773 r = run_query(hdb, rec, query);
2774 ok(r == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", r);
2775 MsiCloseHandle(rec);
2776
2777 MsiCloseHandle(hdb);
2778 DeleteFileA(msifile);
2779 }
2780
2781 #define MY_NVIEWS 4000 /* Largest installer I've seen uses < 2000 */
2782 static void test_handle_limit(void)
2783 {
2784 int i;
2785 MSIHANDLE hdb;
2786 MSIHANDLE hviews[MY_NVIEWS];
2787 UINT r;
2788
2789 /* create an empty db */
2790 hdb = create_db();
2791 ok( hdb, "failed to create db\n");
2792
2793 memset(hviews, 0, sizeof(hviews));
2794
2795 for (i=0; i<MY_NVIEWS; i++) {
2796 static char szQueryBuf[256] = "SELECT * from `_Tables`";
2797 hviews[i] = 0xdeadbeeb;
2798 r = MsiDatabaseOpenViewA(hdb, szQueryBuf, &hviews[i]);
2799 if( r != ERROR_SUCCESS || hviews[i] == 0xdeadbeeb ||
2800 hviews[i] == 0 || (i && (hviews[i] == hviews[i-1])))
2801 break;
2802 }
2803
2804 ok( i == MY_NVIEWS, "problem opening views\n");
2805
2806 for (i=0; i<MY_NVIEWS; i++) {
2807 if (hviews[i] != 0 && hviews[i] != 0xdeadbeeb) {
2808 MsiViewClose(hviews[i]);
2809 r = MsiCloseHandle(hviews[i]);
2810 if (r != ERROR_SUCCESS)
2811 break;
2812 }
2813 }
2814
2815 ok( i == MY_NVIEWS, "problem closing views\n");
2816
2817 r = MsiCloseHandle(hdb);
2818 ok( r == ERROR_SUCCESS, "failed to close database\n");
2819 }
2820
2821 static void generate_transform(void)
2822 {
2823 MSIHANDLE hdb1, hdb2, hrec;
2824 LPCSTR query;
2825 UINT r;
2826
2827 /* start with two identical databases */
2828 CopyFileA(msifile2, msifile, FALSE);
2829
2830 r = MsiOpenDatabaseW(msifileW, MSIDBOPEN_TRANSACT, &hdb1 );
2831 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
2832
2833 r = MsiDatabaseCommit( hdb1 );
2834 ok( r == ERROR_SUCCESS , "Failed to commit database\n" );
2835
2836 r = MsiOpenDatabaseW(msifile2W, MSIDBOPEN_READONLY, &hdb2 );
2837 ok( r == ERROR_SUCCESS , "Failed to create database\n" );
2838
2839 /* the transform between two identical database should be empty */
2840 r = MsiDatabaseGenerateTransformA(hdb1, hdb2, NULL, 0, 0);
2841 todo_wine {
2842 ok( r == ERROR_NO_DATA, "return code %d, should be ERROR_NO_DATA\n", r );
2843 }
2844
2845 query = "CREATE TABLE `AAR` ( `BAR` SHORT NOT NULL, `CAR` CHAR(255) PRIMARY KEY `CAR`)";
2846 r = run_query(hdb1, 0, query);
2847 ok(r == ERROR_SUCCESS, "failed to add table\n");
2848
2849 query = "INSERT INTO `AAR` ( `BAR`, `CAR` ) VALUES ( 1, 'vw' )";
2850 r = run_query(hdb1, 0, query);
2851 ok(r == ERROR_SUCCESS, "failed to add row 1\n");
2852
2853 query = "INSERT INTO `AAR` ( `BAR`, `CAR` ) VALUES ( 2, 'bmw' )";
2854 r = run_query(hdb1, 0, query);
2855 ok(r == ERROR_SUCCESS, "failed to add row 2\n");
2856
2857 query = "UPDATE `MOO` SET `OOO` = 'c' WHERE `NOO` = 1";
2858 r = run_query(hdb1, 0, query);
2859 ok(r == ERROR_SUCCESS, "failed to modify row\n");
2860
2861 query = "DELETE FROM `MOO` WHERE `NOO` = 3";
2862 r = run_query(hdb1, 0, query);
2863 ok(r == ERROR_SUCCESS, "failed to delete row\n");
2864
2865 hrec = MsiCreateRecord(2);
2866 r = MsiRecordSetInteger(hrec, 1, 1);
2867 ok(r == ERROR_SUCCESS, "failed to set integer\n");
2868
2869 write_file("testdata.bin", "naengmyon", 9);
2870 r = MsiRecordSetStreamA(hrec, 2, "testdata.bin");
2871 ok(r == ERROR_SUCCESS, "failed to set stream\n");
2872
2873 query = "INSERT INTO `BINARY` ( `ID`, `BLOB` ) VALUES ( ?, ? )";
2874 r = run_query(hdb1, hrec, query);
2875 ok(r == ERROR_SUCCESS, "failed to add row with blob\n");
2876
2877 MsiCloseHandle(hrec);
2878
2879 query = "ALTER TABLE `MOO` ADD `COW` INTEGER";
2880 r = run_query(hdb1, 0, query);
2881 ok(r == ERROR_SUCCESS, "failed to add column\n");
2882
2883 query = "ALTER TABLE `MOO` ADD `PIG` INTEGER";
2884 r = run_query(hdb1, 0, query);
2885 ok(r == ERROR_SUCCESS, "failed to add column\n");
2886
2887 query = "UPDATE `MOO` SET `PIG` = 5 WHERE `NOO` = 1";
2888 r = run_query(hdb1, 0, query);
2889 ok(r == ERROR_SUCCESS, "failed to modify row\n");
2890
2891 query = "CREATE TABLE `Property` ( `Property` CHAR(72) NOT NULL, "
2892 "`Value` CHAR(0) PRIMARY KEY `Property`)";
2893 r = run_query(hdb1, 0, query);
2894 ok(r == ERROR_SUCCESS, "failed to add property table\n");
2895
2896 query = "INSERT INTO `Property` ( `Property`, `Value` ) VALUES ( 'prop', 'val' )";
2897 r = run_query(hdb1, 0, query);
2898 ok(r == ERROR_SUCCESS, "failed to add property\n");
2899
2900 /* database needs to be committed */
2901 MsiDatabaseCommit(hdb1);
2902
2903 r = MsiDatabaseGenerateTransformA(hdb1, hdb2, mstfile, 0, 0);
2904 ok( r == ERROR_SUCCESS, "return code %d, should be ERROR_SUCCESS\n", r );
2905
2906 MsiCloseHandle( hdb1 );
2907 MsiCloseHandle( hdb2 );
2908
2909 DeleteFileA("testdata.bin");
2910 }
2911
2912 /* data for generating a transform */
2913
2914 /* tables transform names - encoded as they would be in an msi database file */
2915 static const WCHAR name1[] = { 0x4840, 0x3a8a, 0x481b, 0 }; /* AAR */
2916 static const WCHAR name2[] = { 0x4840, 0x3b3f, 0x43f2, 0x4438, 0x45b1, 0 }; /* _Columns */
2917 static const WCHAR name3[] = { 0x4840, 0x3f7f, 0x4164, 0x422f, 0x4836, 0 }; /* _Tables */
2918 static const WCHAR name4[] = { 0x4840, 0x3f3f, 0x4577, 0x446c, 0x3b6a, 0x45e4, 0x4824, 0 }; /* _StringData */
2919 static const WCHAR name5[] = { 0x4840, 0x3f3f, 0x4577, 0x446c, 0x3e6a, 0x44b2, 0x482f, 0 }; /* _StringPool */
2920 static const WCHAR name6[] = { 0x4840, 0x3e16, 0x4818, 0}; /* MOO */
2921 static const WCHAR name7[] = { 0x4840, 0x3c8b, 0x3a97, 0x409b, 0 }; /* BINARY */
2922 static const WCHAR name8[] = { 0x3c8b, 0x3a97, 0x409b, 0x387e, 0 }; /* BINARY.1 */
2923 static const WCHAR name9[] = { 0x4840, 0x4559, 0x44f2, 0x4568, 0x4737, 0 }; /* Property */
2924
2925 /* data in each table */
2926 static const WCHAR data1[] = { /* AAR */
2927 0x0201, 0x0008, 0x8001, /* 0x0201 = add row (1), two shorts */
2928 0x0201, 0x0009, 0x8002,
2929 };
2930 static const WCHAR data2[] = { /* _Columns */
2931 0x0401, 0x0001, 0x8003, 0x0002, 0x9502,
2932 0x0401, 0x0001, 0x8004, 0x0003, 0x9502,
2933 0x0401, 0x0005, 0x0000, 0x0006, 0xbdff, /* 0x0401 = add row (1), 4 shorts */
2934 0x0401, 0x0005, 0x0000, 0x0007, 0x8502,
2935 0x0401, 0x000a, 0x0000, 0x000a, 0xad48,
2936 0x0401, 0x000a, 0x0000, 0x000b, 0x9d00,
2937 };
2938 static const WCHAR data3[] = { /* _Tables */
2939 0x0101, 0x0005, /* 0x0101 = add row (1), 1 short */
2940 0x0101, 0x000a,
2941 };
2942 static const char data4[] = /* _StringData */
2943 "MOOCOWPIGcAARCARBARvwbmwPropertyValuepropval"; /* all the strings squashed together */
2944 static const WCHAR data5[] = { /* _StringPool */
2945 /* len, refs */
2946 0, 0, /* string 0 '' */
2947 3, 2, /* string 1 'MOO' */
2948 3, 1, /* string 2 'COW' */
2949 3, 1, /* string 3 'PIG' */
2950 1, 1, /* string 4 'c' */
2951 3, 3, /* string 5 'AAR' */
2952 3, 1, /* string 6 'CAR' */
2953 3, 1, /* string 7 'BAR' */
2954 2, 1, /* string 8 'vw' */
2955 3, 1, /* string 9 'bmw' */
2956 8, 4, /* string 10 'Property' */
2957 5, 1, /* string 11 'Value' */
2958 4, 1, /* string 12 'prop' */
2959 3, 1, /* string 13 'val' */
2960 };
2961 /* update row, 0x0002 is a bitmask of present column data, keys are excluded */
2962 static const WCHAR data6[] = { /* MOO */
2963 0x000a, 0x8001, 0x0004, 0x8005, /* update row */
2964 0x0000, 0x8003, /* delete row */
2965 };
2966
2967 static const WCHAR data7[] = { /* BINARY */
2968 0x0201, 0x8001, 0x0001,
2969 };
2970
2971 static const char data8[] = /* stream data for the BINARY table */
2972 "naengmyon";
2973
2974 static const WCHAR data9[] = { /* Property */
2975 0x0201, 0x000c, 0x000d,
2976 };
2977
2978 static const struct {
2979 LPCWSTR name;
2980 const void *data;
2981 DWORD size;
2982 } table_transform_data[] =
2983 {
2984 { name1, data1, sizeof data1 },
2985 { name2, data2, sizeof data2 },
2986 { name3, data3, sizeof data3 },
2987 { name4, data4, sizeof data4 - 1 },
2988 { name5, data5, sizeof data5 },
2989 { name6, data6, sizeof data6 },
2990 { name7, data7, sizeof data7 },
2991 { name8, data8, sizeof data8 - 1 },
2992 { name9, data9, sizeof data9 },
2993 };
2994
2995 #define NUM_TRANSFORM_TABLES (sizeof table_transform_data/sizeof table_transform_data[0])
2996
2997 static void generate_transform_manual(void)
2998 {
2999 IStorage *stg = NULL;
3000 IStream *stm;
3001 WCHAR name[0x20];
3002 HRESULT r;
3003 DWORD i, count;
3004 const DWORD mode = STGM_CREATE|STGM_READWRITE|STGM_DIRECT|STGM_SHARE_EXCLUSIVE;
3005
3006 const CLSID CLSID_MsiTransform = { 0xc1082,0,0,{0xc0,0,0,0,0,0,0,0x46}};
3007
3008 MultiByteToWideChar(CP_ACP, 0, mstfile, -1, name, 0x20);
3009
3010 r = StgCreateDocfile(name, mode, 0, &stg);
3011 ok(r == S_OK, "failed to create storage\n");
3012 if (!stg)
3013 return;
3014
3015 r = IStorage_SetClass( stg, &CLSID_MsiTransform );
3016 ok(r == S_OK, "failed to set storage type\n");
3017
3018 for (i=0; i<NUM_TRANSFORM_TABLES; i++)
3019 {
3020 r = IStorage_CreateStream( stg, table_transform_data[i].name,
3021 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm );
3022 if (FAILED(r))
3023 {
3024 ok(0, "failed to create stream %08x\n", r);
3025 continue;
3026 }
3027
3028 r = IStream_Write( stm, table_transform_data[i].data,
3029 table_transform_data[i].size, &count );
3030 if (FAILED(r) || count != table_transform_data[i].size)
3031 ok(0, "failed to write stream\n");
3032 IStream_Release(stm);
3033 }
3034
3035 IStorage_Release(stg);
3036 }
3037
3038 static UINT set_summary_info(MSIHANDLE hdb)
3039 {
3040 UINT res;
3041 MSIHANDLE suminfo;
3042
3043 /* build summary info */
3044 res = MsiGetSummaryInformationA(hdb, NULL, 7, &suminfo);
3045 ok( res == ERROR_SUCCESS , "Failed to open summaryinfo\n" );
3046
3047 res = MsiSummaryInfoSetPropertyA(suminfo,2, VT_LPSTR, 0,NULL,
3048 "Installation Database");
3049 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3050
3051 res = MsiSummaryInfoSetPropertyA(suminfo,3, VT_LPSTR, 0,NULL,
3052 "Installation Database");
3053 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3054
3055 res = MsiSummaryInfoSetPropertyA(suminfo,4, VT_LPSTR, 0,NULL,
3056 "Wine Hackers");
3057 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3058
3059 res = MsiSummaryInfoSetPropertyA(suminfo,7, VT_LPSTR, 0,NULL,
3060 ";1033,2057");
3061 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3062
3063 res = MsiSummaryInfoSetPropertyA(suminfo,9, VT_LPSTR, 0,NULL,
3064 "{913B8D18-FBB6-4CAC-A239-C74C11E3FA74}");
3065 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3066
3067 res = MsiSummaryInfoSetPropertyA(suminfo, 14, VT_I4, 100, NULL, NULL);
3068 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3069
3070 res = MsiSummaryInfoSetPropertyA(suminfo, 15, VT_I4, 0, NULL, NULL);
3071 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3072
3073 res = MsiSummaryInfoPersist(suminfo);
3074 ok( res == ERROR_SUCCESS , "Failed to make summary info persist\n" );
3075
3076 res = MsiCloseHandle( suminfo);
3077 ok( res == ERROR_SUCCESS , "Failed to close suminfo\n" );
3078
3079 return res;
3080 }
3081
3082 static MSIHANDLE create_package_db(const WCHAR *filename)
3083 {
3084 MSIHANDLE hdb = 0;
3085 UINT res;
3086
3087 DeleteFileW(msifileW);
3088
3089 /* create an empty database */
3090 res = MsiOpenDatabaseW(filename, MSIDBOPEN_CREATE, &hdb );
3091 ok( res == ERROR_SUCCESS , "Failed to create database\n" );
3092 if( res != ERROR_SUCCESS )
3093 return hdb;
3094
3095 res = MsiDatabaseCommit( hdb );
3096 ok( res == ERROR_SUCCESS , "Failed to commit database\n" );
3097
3098 res = set_summary_info(hdb);
3099 ok( res == ERROR_SUCCESS , "Failed to set summary info\n" );
3100
3101 res = create_directory_table(hdb);
3102 ok( res == ERROR_SUCCESS , "Failed to create directory table\n" );
3103
3104 return hdb;
3105 }
3106
3107 static UINT package_from_db(MSIHANDLE hdb, MSIHANDLE *handle)
3108 {
3109 UINT res;
3110 CHAR szPackage[12];
3111 MSIHANDLE hPackage;
3112
3113 sprintf(szPackage, "#%u", hdb);
3114 res = MsiOpenPackageA(szPackage, &hPackage);
3115 if (res != ERROR_SUCCESS)
3116 return res;
3117
3118 res = MsiCloseHandle(hdb);
3119 if (res != ERROR_SUCCESS)
3120 {
3121 MsiCloseHandle(hPackage);
3122 return res;