[MSI]
[reactos.git] / reactos / dll / win32 / msi / alter.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2006 Mike McCormack
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 #include "msipriv.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
24
25 typedef struct tagMSIALTERVIEW
26 {
27 MSIVIEW view;
28 MSIDATABASE *db;
29 MSIVIEW *table;
30 column_info *colinfo;
31 INT hold;
32 } MSIALTERVIEW;
33
34 static UINT ALTER_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
35 {
36 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
37
38 TRACE("%p %d %d %p\n", av, row, col, val );
39
40 return ERROR_FUNCTION_FAILED;
41 }
42
43 static UINT ALTER_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
44 {
45 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
46
47 TRACE("%p %d %d %p\n", av, row, col, stm );
48
49 return ERROR_FUNCTION_FAILED;
50 }
51
52 static UINT ALTER_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
53 {
54 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
55
56 TRACE("%p %d %p\n", av, row, rec );
57
58 return av->table->ops->get_row(av->table, row, rec);
59 }
60
61 static UINT ITERATE_columns(MSIRECORD *row, LPVOID param)
62 {
63 (*(UINT *)param)++;
64 return ERROR_SUCCESS;
65 }
66
67 static BOOL check_column_exists(MSIDATABASE *db, LPCWSTR table, LPCWSTR column)
68 {
69 MSIQUERY *view;
70 MSIRECORD *rec;
71 UINT r;
72
73 static const WCHAR query[] = {
74 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
75 '`','_','C','o','l','u','m','n','s','`',' ','W','H','E','R','E',' ',
76 '`','T','a','b','l','e','`','=','\'','%','s','\'',' ','A','N','D',' ',
77 '`','N','a','m','e','`','=','\'','%','s','\'',0
78 };
79
80 r = MSI_OpenQuery(db, &view, query, table, column);
81 if (r != ERROR_SUCCESS)
82 return FALSE;
83
84 r = MSI_ViewExecute(view, NULL);
85 if (r != ERROR_SUCCESS)
86 goto done;
87
88 r = MSI_ViewFetch(view, &rec);
89 if (r == ERROR_SUCCESS)
90 msiobj_release(&rec->hdr);
91
92 done:
93 msiobj_release(&view->hdr);
94 return (r == ERROR_SUCCESS);
95 }
96
97 static UINT alter_add_column(MSIALTERVIEW *av)
98 {
99 UINT r, colnum = 1;
100 MSIQUERY *view;
101 MSIVIEW *columns;
102
103 static const WCHAR szColumns[] = {'_','C','o','l','u','m','n','s',0};
104 static const WCHAR query[] = {
105 'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
106 '`','_','C','o','l','u','m','n','s','`',' ','W','H','E','R','E',' ',
107 '`','T','a','b','l','e','`','=','\'','%','s','\'',' ','O','R','D','E','R',' ',
108 'B','Y',' ','`','N','u','m','b','e','r','`',0
109 };
110
111 r = TABLE_CreateView(av->db, szColumns, &columns);
112 if (r != ERROR_SUCCESS)
113 return r;
114
115 if (check_column_exists(av->db, av->colinfo->table, av->colinfo->column))
116 {
117 columns->ops->delete(columns);
118 return ERROR_BAD_QUERY_SYNTAX;
119 }
120
121 r = MSI_OpenQuery(av->db, &view, query, av->colinfo->table, av->colinfo->column);
122 if (r == ERROR_SUCCESS)
123 {
124 r = MSI_IterateRecords(view, NULL, ITERATE_columns, &colnum);
125 msiobj_release(&view->hdr);
126 if (r != ERROR_SUCCESS)
127 {
128 columns->ops->delete(columns);
129 return r;
130 }
131 }
132 r = columns->ops->add_column(columns, av->colinfo->table,
133 colnum, av->colinfo->column,
134 av->colinfo->type, (av->hold == 1));
135
136 columns->ops->delete(columns);
137 return r;
138 }
139
140 static UINT ALTER_execute( struct tagMSIVIEW *view, MSIRECORD *record )
141 {
142 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
143 UINT ref;
144
145 TRACE("%p %p\n", av, record);
146
147 if (av->hold == 1)
148 av->table->ops->add_ref(av->table);
149 else if (av->hold == -1)
150 {
151 ref = av->table->ops->release(av->table);
152 if (ref == 0)
153 av->table = NULL;
154 }
155
156 if (av->colinfo)
157 return alter_add_column(av);
158
159 return ERROR_SUCCESS;
160 }
161
162 static UINT ALTER_close( struct tagMSIVIEW *view )
163 {
164 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
165
166 TRACE("%p\n", av );
167
168 return ERROR_SUCCESS;
169 }
170
171 static UINT ALTER_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
172 {
173 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
174
175 TRACE("%p %p %p\n", av, rows, cols );
176
177 return ERROR_FUNCTION_FAILED;
178 }
179
180 static UINT ALTER_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
181 UINT *type, BOOL *temporary, LPCWSTR *table_name )
182 {
183 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
184
185 TRACE("%p %d %p %p %p %p\n", av, n, name, type, temporary, table_name );
186
187 return ERROR_FUNCTION_FAILED;
188 }
189
190 static UINT ALTER_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
191 MSIRECORD *rec, UINT row )
192 {
193 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
194
195 TRACE("%p %d %p\n", av, eModifyMode, rec );
196
197 return ERROR_FUNCTION_FAILED;
198 }
199
200 static UINT ALTER_delete( struct tagMSIVIEW *view )
201 {
202 MSIALTERVIEW *av = (MSIALTERVIEW*)view;
203
204 TRACE("%p\n", av );
205 if (av->table)
206 av->table->ops->delete( av->table );
207 msi_free( av );
208
209 return ERROR_SUCCESS;
210 }
211
212 static UINT ALTER_find_matching_rows( struct tagMSIVIEW *view, UINT col,
213 UINT val, UINT *row, MSIITERHANDLE *handle )
214 {
215 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
216
217 return ERROR_FUNCTION_FAILED;
218 }
219
220 static const MSIVIEWOPS alter_ops =
221 {
222 ALTER_fetch_int,
223 ALTER_fetch_stream,
224 ALTER_get_row,
225 NULL,
226 NULL,
227 NULL,
228 ALTER_execute,
229 ALTER_close,
230 ALTER_get_dimensions,
231 ALTER_get_column_info,
232 ALTER_modify,
233 ALTER_delete,
234 ALTER_find_matching_rows,
235 NULL,
236 NULL,
237 NULL,
238 NULL,
239 NULL,
240 NULL,
241 };
242
243 UINT ALTER_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR name, column_info *colinfo, int hold )
244 {
245 MSIALTERVIEW *av;
246 UINT r;
247
248 TRACE("%p %p %s %d\n", view, colinfo, debugstr_w(name), hold );
249
250 av = msi_alloc_zero( sizeof *av );
251 if( !av )
252 return ERROR_FUNCTION_FAILED;
253
254 r = TABLE_CreateView( db, name, &av->table );
255 if (r != ERROR_SUCCESS)
256 {
257 msi_free( av );
258 return r;
259 }
260
261 if (colinfo)
262 colinfo->table = name;
263
264 /* fill the structure */
265 av->view.ops = &alter_ops;
266 av->db = db;
267 av->hold = hold;
268 av->colinfo = colinfo;
269
270 *view = &av->view;
271
272 return ERROR_SUCCESS;
273 }