b22a2aac61f30f26cc1fc775d15f1ce08b9342d3
[reactos.git] / reactos / lib / msi / create.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 tagMSICREATEVIEW
42 {
43 MSIVIEW view;
44 MSIDATABASE *db;
45 LPWSTR name;
46 BOOL bIsTemp;
47 create_col_info *col_info;
48 } MSICREATEVIEW;
49
50 static UINT CREATE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
51 {
52 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
53
54 TRACE("%p %d %d %p\n", cv, row, col, val );
55
56 return ERROR_FUNCTION_FAILED;
57 }
58
59 static UINT CREATE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
60 {
61 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
62 create_col_info *col;
63 UINT r, nField, row, table_val, column_val;
64 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
65 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
66 MSIVIEW *tv = NULL;
67
68 TRACE("%p Table %s (%s)\n", cv, debugstr_w(cv->name),
69 cv->bIsTemp?"temporary":"permanent");
70
71 /* only add tables that don't exist already */
72 if( TABLE_Exists(cv->db, cv->name ) )
73 return ERROR_BAD_QUERY_SYNTAX;
74
75 /* add the name to the _Tables table */
76 table_val = msi_addstringW( cv->db->strings, 0, cv->name, -1, 1 );
77 TRACE("New string %s -> %d\n", debugstr_w( cv->name ), table_val );
78 if( table_val < 0 )
79 return ERROR_FUNCTION_FAILED;
80
81 r = TABLE_CreateView( cv->db, szTables, &tv );
82 TRACE("CreateView returned %x\n", r);
83 if( r )
84 return r;
85
86 r = tv->ops->execute( tv, 0 );
87 TRACE("tv execute returned %x\n", r);
88 if( r )
89 return r;
90
91 row = -1;
92 r = tv->ops->insert_row( tv, &row );
93 TRACE("insert_row returned %x\n", r);
94 if( r )
95 goto err;
96
97 r = tv->ops->set_int( tv, row, 1, table_val );
98 if( r )
99 goto err;
100 tv->ops->delete( tv );
101 tv = NULL;
102
103 /* add each column to the _Columns table */
104 r = TABLE_CreateView( cv->db, szColumns, &tv );
105 if( r )
106 return r;
107
108 r = tv->ops->execute( tv, 0 );
109 TRACE("tv execute returned %x\n", r);
110 if( r )
111 return r;
112
113 /*
114 * need to set the table, column number, col name and type
115 * for each column we enter in the table
116 */
117 nField = 1;
118 for( col = cv->col_info; col; col = col->next )
119 {
120 row = -1;
121 r = tv->ops->insert_row( tv, &row );
122 if( r )
123 goto err;
124
125 column_val = msi_addstringW( cv->db->strings, 0, col->colname, -1, 1 );
126 TRACE("New string %s -> %d\n", debugstr_w( col->colname ), column_val );
127 if( column_val < 0 )
128 break;
129
130 /* add the string again here so we increase the reference count */
131 table_val = msi_addstringW( cv->db->strings, 0, cv->name, -1, 1 );
132 if( table_val < 0 )
133 break;
134
135 r = tv->ops->set_int( tv, row, 1, table_val );
136 if( r )
137 break;
138
139 r = tv->ops->set_int( tv, row, 2, 0x8000|nField );
140 if( r )
141 break;
142
143 r = tv->ops->set_int( tv, row, 3, column_val );
144 if( r )
145 break;
146
147 r = tv->ops->set_int( tv, row, 4, 0x8000|col->type );
148 if( r )
149 break;
150
151 nField++;
152 }
153 if( !col )
154 r = ERROR_SUCCESS;
155
156 err:
157 /* FIXME: remove values from the string table on error */
158 if( tv )
159 tv->ops->delete( tv );
160 return r;
161 }
162
163 static UINT CREATE_close( struct tagMSIVIEW *view )
164 {
165 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
166
167 TRACE("%p\n", cv);
168
169 return ERROR_SUCCESS;
170 }
171
172 static UINT CREATE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
173 {
174 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
175
176 TRACE("%p %p %p\n", cv, rows, cols );
177
178 return ERROR_FUNCTION_FAILED;
179 }
180
181 static UINT CREATE_get_column_info( struct tagMSIVIEW *view,
182 UINT n, LPWSTR *name, UINT *type )
183 {
184 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
185
186 TRACE("%p %d %p %p\n", cv, n, name, type );
187
188 return ERROR_FUNCTION_FAILED;
189 }
190
191 static UINT CREATE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
192 MSIRECORD *rec)
193 {
194 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
195
196 TRACE("%p %d %p\n", cv, eModifyMode, rec );
197
198 return ERROR_FUNCTION_FAILED;
199 }
200
201 static UINT CREATE_delete( struct tagMSIVIEW *view )
202 {
203 MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
204 create_col_info *col;
205
206 TRACE("%p\n", cv );
207
208 col = cv->col_info;
209 while( col )
210 {
211 create_col_info *t = col;
212 col = col->next;
213 HeapFree( GetProcessHeap(), 0, t->colname );
214 HeapFree( GetProcessHeap(), 0, t );
215 }
216 msiobj_release( &cv->db->hdr );
217 HeapFree( GetProcessHeap(), 0, cv->name );
218 HeapFree( GetProcessHeap(), 0, cv );
219
220 return ERROR_SUCCESS;
221 }
222
223
224 MSIVIEWOPS create_ops =
225 {
226 CREATE_fetch_int,
227 NULL,
228 NULL,
229 NULL,
230 CREATE_execute,
231 CREATE_close,
232 CREATE_get_dimensions,
233 CREATE_get_column_info,
234 CREATE_modify,
235 CREATE_delete
236 };
237
238 UINT CREATE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR table,
239 create_col_info *col_info, BOOL temp )
240 {
241 MSICREATEVIEW *cv = NULL;
242
243 TRACE("%p\n", cv );
244
245 cv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *cv );
246 if( !cv )
247 return ERROR_FUNCTION_FAILED;
248
249 /* fill the structure */
250 cv->view.ops = &create_ops;
251 msiobj_addref( &db->hdr );
252 cv->db = db;
253 cv->name = table; /* FIXME: strdupW it? */
254 cv->col_info = col_info;
255 cv->bIsTemp = temp;
256 *view = (MSIVIEW*) cv;
257
258 return ERROR_SUCCESS;
259 }