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"
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
39 /* below is the query interface to a table */
41 typedef struct tagMSIINSERTVIEW
51 static UINT
INSERT_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
53 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
55 TRACE("%p %d %d %p\n", iv
, row
, col
, val
);
57 return ERROR_FUNCTION_FAILED
;
61 * msi_query_merge_record
63 * Merge a value_list and a record to create a second record.
64 * Replace wildcard entries in the valuelist with values from the record
66 MSIRECORD
*msi_query_merge_record( UINT fields
, const column_info
*vl
, MSIRECORD
*rec
)
69 DWORD wildcard_count
= 1, i
;
71 merged
= MSI_CreateRecord( fields
);
72 for( i
=1; i
<= fields
; i
++ )
76 TRACE("Not enough elements in the list to insert\n");
79 switch( vl
->val
->type
)
82 TRACE("field %d -> %s\n", i
, debugstr_w(vl
->val
->u
.sval
));
83 MSI_RecordSetStringW( merged
, i
, vl
->val
->u
.sval
);
86 MSI_RecordSetInteger( merged
, i
, vl
->val
->u
.ival
);
91 MSI_RecordCopyField( rec
, wildcard_count
, merged
, i
);
95 ERR("Unknown expression type %d\n", vl
->val
->type
);
102 msiobj_release( &merged
->hdr
);
106 /* checks to see if the column order specified in the INSERT query
107 * matches the column order of the table
109 static BOOL
msi_columns_in_order(MSIINSERTVIEW
*iv
, UINT col_count
)
114 for (i
= 1; i
<= col_count
; i
++)
116 iv
->sv
->ops
->get_column_info(iv
->sv
, i
, &a
, NULL
, NULL
, NULL
);
117 iv
->table
->ops
->get_column_info(iv
->table
, i
, &b
, NULL
, NULL
, NULL
);
119 if (wcscmp( a
, b
)) return FALSE
;
124 /* rearranges the data in the record to be inserted based on column order,
125 * and pads the record for any missing columns in the INSERT query
127 static UINT
msi_arrange_record(MSIINSERTVIEW
*iv
, MSIRECORD
**values
)
130 UINT col_count
, val_count
;
134 r
= iv
->table
->ops
->get_dimensions(iv
->table
, NULL
, &col_count
);
135 if (r
!= ERROR_SUCCESS
)
138 val_count
= MSI_RecordGetFieldCount(*values
);
140 /* check to see if the columns are arranged already
141 * to avoid unnecessary copying
143 if (col_count
== val_count
&& msi_columns_in_order(iv
, col_count
))
144 return ERROR_SUCCESS
;
146 padded
= MSI_CreateRecord(col_count
);
148 return ERROR_OUTOFMEMORY
;
150 for (colidx
= 1; colidx
<= val_count
; colidx
++)
152 r
= iv
->sv
->ops
->get_column_info(iv
->sv
, colidx
, &a
, NULL
, NULL
, NULL
);
153 if (r
!= ERROR_SUCCESS
)
156 for (i
= 1; i
<= col_count
; i
++)
158 r
= iv
->table
->ops
->get_column_info(iv
->table
, i
, &b
, NULL
,
160 if (r
!= ERROR_SUCCESS
)
165 MSI_RecordCopyField(*values
, colidx
, padded
, i
);
170 msiobj_release(&(*values
)->hdr
);
172 return ERROR_SUCCESS
;
175 msiobj_release(&padded
->hdr
);
179 static BOOL
row_has_null_primary_keys(MSIINSERTVIEW
*iv
, MSIRECORD
*row
)
181 UINT r
, i
, col_count
, type
;
183 r
= iv
->table
->ops
->get_dimensions( iv
->table
, NULL
, &col_count
);
184 if (r
!= ERROR_SUCCESS
)
187 for (i
= 1; i
<= col_count
; i
++)
189 r
= iv
->table
->ops
->get_column_info(iv
->table
, i
, NULL
, &type
,
191 if (r
!= ERROR_SUCCESS
)
194 if (!(type
& MSITYPE_KEY
))
197 if (MSI_RecordIsNull(row
, i
))
204 static UINT
INSERT_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
206 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
207 UINT r
, row
= -1, col_count
= 0;
209 MSIRECORD
*values
= NULL
;
211 TRACE("%p %p\n", iv
, record
);
215 return ERROR_FUNCTION_FAILED
;
217 r
= sv
->ops
->execute( sv
, 0 );
218 TRACE("sv execute returned %x\n", r
);
222 r
= sv
->ops
->get_dimensions( sv
, NULL
, &col_count
);
227 * Merge the wildcard values into the list of values provided
228 * in the query, and create a record containing both.
230 values
= msi_query_merge_record( col_count
, iv
->vals
, record
);
234 r
= msi_arrange_record( iv
, &values
);
235 if( r
!= ERROR_SUCCESS
)
238 /* rows with NULL primary keys are inserted at the beginning of the table */
239 if( row_has_null_primary_keys( iv
, values
) )
242 r
= iv
->table
->ops
->insert_row( iv
->table
, values
, row
, iv
->bIsTemp
);
246 msiobj_release( &values
->hdr
);
252 static UINT
INSERT_close( struct tagMSIVIEW
*view
)
254 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
261 return ERROR_FUNCTION_FAILED
;
263 return sv
->ops
->close( sv
);
266 static UINT
INSERT_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
268 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
271 TRACE("%p %p %p\n", iv
, rows
, cols
);
275 return ERROR_FUNCTION_FAILED
;
277 return sv
->ops
->get_dimensions( sv
, rows
, cols
);
280 static UINT
INSERT_get_column_info( struct tagMSIVIEW
*view
, UINT n
, LPCWSTR
*name
,
281 UINT
*type
, BOOL
*temporary
, LPCWSTR
*table_name
)
283 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
286 TRACE("%p %d %p %p %p %p\n", iv
, n
, name
, type
, temporary
, table_name
);
290 return ERROR_FUNCTION_FAILED
;
292 return sv
->ops
->get_column_info( sv
, n
, name
, type
, temporary
, table_name
);
295 static UINT
INSERT_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIRECORD
*rec
, UINT row
)
297 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
299 TRACE("%p %d %p\n", iv
, eModifyMode
, rec
);
301 return ERROR_FUNCTION_FAILED
;
304 static UINT
INSERT_delete( struct tagMSIVIEW
*view
)
306 MSIINSERTVIEW
*iv
= (MSIINSERTVIEW
*)view
;
313 sv
->ops
->delete( sv
);
314 msiobj_release( &iv
->db
->hdr
);
317 return ERROR_SUCCESS
;
320 static const MSIVIEWOPS insert_ops
=
332 INSERT_get_dimensions
,
333 INSERT_get_column_info
,
343 static UINT
count_column_info( const column_info
*ci
)
346 for ( ; ci
; ci
= ci
->next
)
351 UINT
INSERT_CreateView( MSIDATABASE
*db
, MSIVIEW
**view
, LPCWSTR table
,
352 column_info
*columns
, column_info
*values
, BOOL temp
)
354 MSIINSERTVIEW
*iv
= NULL
;
356 MSIVIEW
*tv
= NULL
, *sv
= NULL
;
360 /* there should be one value for each column */
361 if ( count_column_info( columns
) != count_column_info(values
) )
362 return ERROR_BAD_QUERY_SYNTAX
;
364 r
= TABLE_CreateView( db
, table
, &tv
);
365 if( r
!= ERROR_SUCCESS
)
368 r
= SELECT_CreateView( db
, &sv
, tv
, columns
);
369 if( r
!= ERROR_SUCCESS
)
372 tv
->ops
->delete( tv
);
376 iv
= msi_alloc_zero( sizeof *iv
);
378 return ERROR_FUNCTION_FAILED
;
380 /* fill the structure */
381 iv
->view
.ops
= &insert_ops
;
382 msiobj_addref( &db
->hdr
);
388 *view
= (MSIVIEW
*) iv
;
390 return ERROR_SUCCESS
;