Report win2k service pack 4 instead of 3.
[wine] / dlls / msi / select.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-2004 Mike McCormack for CodeWeavers
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "objbase.h"
30 #include "objidl.h"
31 #include "msipriv.h"
32 #include "winnls.h"
33
34 #include "query.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msi);
37
38
39 /* below is the query interface to a table */
40
41 typedef struct tagMSISELECTVIEW
42 {
43     MSIVIEW        view;
44     MSIDATABASE   *db;
45     MSIVIEW       *table;
46     UINT           num_cols;
47     UINT           max_cols;
48     UINT           cols[1];
49 } MSISELECTVIEW;
50
51 static UINT SELECT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
52 {
53     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
54
55     TRACE("%p %d %d %p\n", sv, row, col, val );
56
57     if( !sv->table )
58          return ERROR_FUNCTION_FAILED;
59
60     if( (col==0) || (col>sv->num_cols) )
61          return ERROR_FUNCTION_FAILED;
62
63     col = sv->cols[ col - 1 ];
64
65     return sv->table->ops->fetch_int( sv->table, row, col, val );
66 }
67
68 static UINT SELECT_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
69 {
70     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
71
72     TRACE("%p %d %d %p\n", sv, row, col, stm );
73
74     if( !sv->table )
75          return ERROR_FUNCTION_FAILED;
76
77     if( (col==0) || (col>sv->num_cols) )
78          return ERROR_FUNCTION_FAILED;
79
80     col = sv->cols[ col - 1 ];
81
82     return sv->table->ops->fetch_stream( sv->table, row, col, stm );
83 }
84
85 static UINT SELECT_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
86 {
87     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
88
89     TRACE("%p %d %d %04x\n", sv, row, col, val );
90
91     if( !sv->table )
92          return ERROR_FUNCTION_FAILED;
93
94     if( (col==0) || (col>sv->num_cols) )
95          return ERROR_FUNCTION_FAILED;
96
97     col = sv->cols[ col - 1 ];
98
99     return sv->table->ops->set_int( sv->table, row, col, val );
100 }
101
102 static UINT SELECT_insert_row( struct tagMSIVIEW *view, UINT *num )  
103 {
104     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
105
106     TRACE("%p %p\n", sv, num );
107
108     if( !sv->table )
109          return ERROR_FUNCTION_FAILED;
110
111     return sv->table->ops->insert_row( sv->table, num );
112 }
113
114 static UINT SELECT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
115 {
116     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
117
118     TRACE("%p %p\n", sv, record);
119
120     if( !sv->table )
121          return ERROR_FUNCTION_FAILED;
122
123     return sv->table->ops->execute( sv->table, record );
124 }
125
126 static UINT SELECT_close( struct tagMSIVIEW *view )
127 {
128     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
129
130     TRACE("%p\n", sv );
131
132     if( !sv->table )
133          return ERROR_FUNCTION_FAILED;
134
135     return sv->table->ops->close( sv->table );
136 }
137
138 static UINT SELECT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
139 {
140     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
141
142     TRACE("%p %p %p\n", sv, rows, cols );
143
144     if( !sv->table )
145          return ERROR_FUNCTION_FAILED;
146
147     if( cols )
148         *cols = sv->num_cols;
149
150     return sv->table->ops->get_dimensions( sv->table, rows, NULL );
151 }
152
153 static UINT SELECT_get_column_info( struct tagMSIVIEW *view,
154                 UINT n, LPWSTR *name, UINT *type )
155 {
156     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
157
158     TRACE("%p %d %p %p\n", sv, n, name, type );
159
160     if( !sv->table )
161          return ERROR_FUNCTION_FAILED;
162
163     if( (n==0) || (n>sv->num_cols) )
164          return ERROR_FUNCTION_FAILED;
165
166     n = sv->cols[ n - 1 ];
167
168     return sv->table->ops->get_column_info( sv->table, n, name, type );
169 }
170
171 static UINT SELECT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
172 {
173     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
174
175     TRACE("%p %d %ld\n", sv, eModifyMode, hrec );
176
177     if( !sv->table )
178          return ERROR_FUNCTION_FAILED;
179
180     return sv->table->ops->modify( sv->table, eModifyMode, hrec );
181 }
182
183 static UINT SELECT_delete( struct tagMSIVIEW *view )
184 {
185     MSISELECTVIEW *sv = (MSISELECTVIEW*)view;
186
187     TRACE("%p\n", sv );
188
189     if( sv->table )
190         sv->table->ops->delete( sv->table );
191
192     HeapFree( GetProcessHeap(), 0, sv );
193
194     return ERROR_SUCCESS;
195 }
196
197
198 MSIVIEWOPS select_ops =
199 {
200     SELECT_fetch_int,
201     SELECT_fetch_stream,
202     SELECT_set_int,
203     SELECT_insert_row,
204     SELECT_execute,
205     SELECT_close,
206     SELECT_get_dimensions,
207     SELECT_get_column_info,
208     SELECT_modify,
209     SELECT_delete
210 };
211
212 static UINT SELECT_AddColumn( MSISELECTVIEW *sv, LPWSTR name )
213 {
214     UINT r, n=0;
215     MSIVIEW *table;
216
217     TRACE("%p adding %s\n", sv, debugstr_w( name ) );
218
219     if( sv->view.ops != &select_ops )
220         return ERROR_FUNCTION_FAILED;
221
222     table = sv->table;
223     if( !table )
224         return ERROR_FUNCTION_FAILED;
225     if( !table->ops->get_dimensions )
226         return ERROR_FUNCTION_FAILED;
227     if( !table->ops->get_column_info )
228         return ERROR_FUNCTION_FAILED;
229
230     if( sv->num_cols >= sv->max_cols )
231         return ERROR_FUNCTION_FAILED;
232
233     r = VIEW_find_column( table, name, &n );
234     if( r != ERROR_SUCCESS )
235         return r;
236
237     sv->cols[sv->num_cols] = n;
238     TRACE("Translating column %s from %d -> %d\n", 
239           debugstr_w( name ), sv->num_cols, n);
240
241     sv->num_cols++;
242
243     return ERROR_SUCCESS;
244 }
245
246 UINT SELECT_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
247                         string_list *columns )
248 {
249     MSISELECTVIEW *sv = NULL;
250     UINT count = 0, r;
251
252     TRACE("%p\n", sv );
253
254     r = table->ops->get_dimensions( table, NULL, &count );
255     if( r != ERROR_SUCCESS )
256     {
257         ERR("can't get table dimensions\n");
258         return r;
259     }
260
261     sv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
262                     sizeof *sv + count*sizeof (UINT) );
263     if( !sv )
264         return ERROR_FUNCTION_FAILED;
265     
266     /* fill the structure */
267     sv->view.ops = &select_ops;
268     sv->db = db;
269     sv->table = table;
270     sv->num_cols = 0;
271     sv->max_cols = count;
272
273     while( columns )
274     {
275         r = SELECT_AddColumn( sv, columns->string );
276         if( r )
277             break;
278         columns = columns->next;
279     }
280
281     if( r != ERROR_SUCCESS )
282     {
283         sv->view.ops->delete( &sv->view );
284         sv = NULL;
285     }
286
287     *view = &sv->view;
288
289     return r;
290 }