2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2004 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
40 /* below is the query interface to a table */
42 typedef struct tagMSIINSERTVIEW
52 static UINT
INSERT_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
54 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
56 TRACE("%p %d %d %p\n", iv
, row
, col
, val
);
58 return ERROR_FUNCTION_FAILED
;
62 * msi_query_merge_record
64 * Merge a value_list and a record to create a second record.
65 * Replace wildcard entries in the valuelist with values from the record
67 MSIRECORD
*msi_query_merge_record( UINT fields
, const column_info
*vl
, MSIRECORD
*rec
)
70 DWORD wildcard_count
= 1, i
;
72 merged
= MSI_CreateRecord( fields
);
73 for( i
=1; i
<= fields
; i
++ )
77 TRACE("Not enough elements in the list to insert\n");
80 switch( vl
->val
->type
)
83 TRACE("field %d -> %s\n", i
, debugstr_w(vl
->val
->u
.sval
));
84 MSI_RecordSetStringW( merged
, i
, vl
->val
->u
.sval
);
87 MSI_RecordSetInteger( merged
, i
, vl
->val
->u
.ival
);
92 MSI_RecordCopyField( rec
, wildcard_count
, merged
, i
);
96 ERR("Unknown expression type %d\n", vl
->val
->type
);
103 msiobj_release( &merged
->hdr
);
107 /* checks to see if the column order specified in the INSERT query
108 * matches the column order of the table
110 static BOOL
msi_columns_in_order(MSIINSERTVIEW
*iv
, UINT col_count
)
116 for (i
= 1; i
<= col_count
; i
++)
118 iv
->sv
->ops
->get_column_info(iv
->sv
, i
, &a
, NULL
, NULL
, NULL
);
119 iv
->table
->ops
->get_column_info(iv
->table
, i
, &b
, NULL
, NULL
, NULL
);
121 res
= strcmpW( a
, b
);
132 /* rearranges the data in the record to be inserted based on column order,
133 * and pads the record for any missing columns in the INSERT query
135 static UINT
msi_arrange_record(MSIINSERTVIEW
*iv
, MSIRECORD
**values
)
138 UINT col_count
, val_count
;
143 r
= iv
->table
->ops
->get_dimensions(iv
->table
, NULL
, &col_count
);
144 if (r
!= ERROR_SUCCESS
)
147 val_count
= MSI_RecordGetFieldCount(*values
);
149 /* check to see if the columns are arranged already
150 * to avoid unnecessary copying
152 if (col_count
== val_count
&& msi_columns_in_order(iv
, col_count
))
153 return ERROR_SUCCESS
;
155 padded
= MSI_CreateRecord(col_count
);
157 return ERROR_OUTOFMEMORY
;
159 for (colidx
= 1; colidx
<= val_count
; colidx
++)
161 r
= iv
->sv
->ops
->get_column_info(iv
->sv
, colidx
, &a
, NULL
, NULL
, NULL
);
162 if (r
!= ERROR_SUCCESS
)
165 for (i
= 1; i
<= col_count
; i
++)
167 r
= iv
->table
->ops
->get_column_info(iv
->table
, i
, &b
, NULL
,
169 if (r
!= ERROR_SUCCESS
)
172 res
= strcmpW( a
, b
);
177 MSI_RecordCopyField(*values
, colidx
, padded
, i
);
185 msiobj_release(&(*values
)->hdr
);
188 return ERROR_SUCCESS
;
191 msiobj_release(&padded
->hdr
);
195 static BOOL
row_has_null_primary_keys(MSIINSERTVIEW
*iv
, MSIRECORD
*row
)
197 UINT r
, i
, col_count
, type
;
199 r
= iv
->table
->ops
->get_dimensions( iv
->table
, NULL
, &col_count
);
200 if (r
!= ERROR_SUCCESS
)
203 for (i
= 1; i
<= col_count
; i
++)
205 r
= iv
->table
->ops
->get_column_info(iv
->table
, i
, NULL
, &type
,
207 if (r
!= ERROR_SUCCESS
)
210 if (!(type
& MSITYPE_KEY
))
213 if (MSI_RecordIsNull(row
, i
))
220 static UINT
INSERT_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
222 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
223 UINT r
, row
= -1, col_count
= 0;
225 MSIRECORD
*values
= NULL
;
227 TRACE("%p %p\n", iv
, record
);
231 return ERROR_FUNCTION_FAILED
;
233 r
= sv
->ops
->execute( sv
, 0 );
234 TRACE("sv execute returned %x\n", r
);
238 r
= sv
->ops
->get_dimensions( sv
, NULL
, &col_count
);
243 * Merge the wildcard values into the list of values provided
244 * in the query, and create a record containing both.
246 values
= msi_query_merge_record( col_count
, iv
->vals
, record
);
250 r
= msi_arrange_record( iv
, &values
);
251 if( r
!= ERROR_SUCCESS
)
254 /* rows with NULL primary keys are inserted at the beginning of the table */
255 if( row_has_null_primary_keys( iv
, values
) )
258 r
= iv
->table
->ops
->insert_row( iv
->table
, values
, row
, iv
->bIsTemp
);
262 msiobj_release( &values
->hdr
);
268 static UINT
INSERT_close( struct tagMSIVIEW
*view
)
270 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
277 return ERROR_FUNCTION_FAILED
;
279 return sv
->ops
->close( sv
);
282 static UINT
INSERT_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
284 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
287 TRACE("%p %p %p\n", iv
, rows
, cols
);
291 return ERROR_FUNCTION_FAILED
;
293 return sv
->ops
->get_dimensions( sv
, rows
, cols
);
296 static UINT
INSERT_get_column_info( struct tagMSIVIEW
*view
,
297 UINT n
, LPWSTR
*name
, UINT
*type
, BOOL
*temporary
,
300 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
303 TRACE("%p %d %p %p %p %p\n", iv
, n
, name
, type
, temporary
, table_name
);
307 return ERROR_FUNCTION_FAILED
;
309 return sv
->ops
->get_column_info( sv
, n
, name
, type
, temporary
, table_name
);
312 static UINT
INSERT_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
314 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
316 TRACE("%p %d %p\n", iv
, eModifyMode
, rec
);
318 return ERROR_FUNCTION_FAILED
;
321 static UINT
INSERT_delete( struct tagMSIVIEW
*view
)
323 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
330 sv
->ops
->delete( sv
);
331 msiobj_release( &iv
->db
->hdr
);
334 return ERROR_SUCCESS
;
337 static UINT
INSERT_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
338 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
340 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
342 return ERROR_FUNCTION_FAILED
;
346 static const MSIVIEWOPS insert_ops
=
356 INSERT_get_dimensions
,
357 INSERT_get_column_info
,
360 INSERT_find_matching_rows
,
369 static UINT
count_column_info( const column_info
*ci
)
372 for ( ; ci
; ci
= ci
->next
)
377 UINT
INSERT_CreateView( MSIDATABASE
*db
, MSIVIEW
**view
, LPCWSTR table
,
378 column_info
*columns
, column_info
*values
, BOOL temp
)
380 MSIINSERTVIEW
*iv
= NULL
;
382 MSIVIEW
*tv
= NULL
, *sv
= NULL
;
386 /* there should be one value for each column */
387 if ( count_column_info( columns
) != count_column_info(values
) )
388 return ERROR_BAD_QUERY_SYNTAX
;
390 r
= TABLE_CreateView( db
, table
, &tv
);
391 if( r
!= ERROR_SUCCESS
)
394 r
= SELECT_CreateView( db
, &sv
, tv
, columns
);
395 if( r
!= ERROR_SUCCESS
)
398 tv
->ops
->delete( tv
);
402 iv
= msi_alloc_zero( sizeof *iv
);
404 return ERROR_FUNCTION_FAILED
;
406 /* fill the structure */
407 iv
->view
.ops
= &insert_ops
;
408 msiobj_addref( &db
->hdr
);
414 *view
= (MSIVIEW
*) iv
;
416 return ERROR_SUCCESS
;