Make the msistring_makehash return something other than just 1 or
[wine] / dlls / msi / insert.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 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 tagMSIINSERTVIEW
42 {
43     MSIVIEW          view;
44     MSIDATABASE     *db;
45     BOOL             bIsTemp;
46     MSIVIEW         *sv;
47     value_list      *vals;   /* looks like these may be ignored... */
48 } MSIINSERTVIEW;
49
50 static UINT INSERT_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
51 {
52     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
53
54     TRACE("%p %d %d %p\n", iv, row, col, val );
55
56     return ERROR_FUNCTION_FAILED;
57 }
58
59 static UINT INSERT_execute( struct tagMSIVIEW *view, MSIRECORD *record )
60 {
61     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
62     UINT n, type, val, r, row, col_count = 0;
63     MSIVIEW *sv;
64
65     TRACE("%p %p\n", iv, record );
66
67     sv = iv->sv;
68     if( !sv )
69         return ERROR_FUNCTION_FAILED;
70
71     r = sv->ops->execute( sv, 0 );
72     TRACE("tv execute returned %x\n", r);
73     if( r )
74         return r;
75
76     r = sv->ops->get_dimensions( sv, NULL, &col_count );
77     if( r )
78         goto err;
79
80     n = MSI_RecordGetFieldCount( record );
81     if( n != col_count )
82     {
83         ERR("Number of fields do not match\n");
84         goto err;
85     }
86
87     row = -1;
88     r = sv->ops->insert_row( sv, &row );
89     TRACE("insert_row returned %x\n", r);
90     if( r )
91         goto err;
92
93     for( n = 1; n <= col_count; n++ )
94     {
95         r = sv->ops->get_column_info( sv, n, NULL, &type );
96         if( r )
97             break;
98
99         if( type & MSITYPE_STRING )
100         {
101             const WCHAR *str = MSI_RecordGetString( record, n );
102             val = msi_addstringW( iv->db->strings, 0, str, -1, 1 );
103         }
104         else
105         {
106             val = MSI_RecordGetInteger( record, n );
107             val |= 0x8000;
108         }
109         r = sv->ops->set_int( sv, row, n, val );
110         if( r )
111             break;
112     }
113
114 err:
115     return ERROR_SUCCESS;
116 }
117
118
119 static UINT INSERT_close( struct tagMSIVIEW *view )
120 {
121     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
122     MSIVIEW *sv;
123
124     TRACE("%p\n", iv);
125
126     sv = iv->sv;
127     if( !sv )
128         return ERROR_FUNCTION_FAILED;
129
130     return sv->ops->close( sv );
131 }
132
133 static UINT INSERT_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
134 {
135     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
136     MSIVIEW *sv;
137
138     TRACE("%p %p %p\n", iv, rows, cols );
139
140     sv = iv->sv;
141     if( !sv )
142         return ERROR_FUNCTION_FAILED;
143
144     return sv->ops->get_dimensions( sv, rows, cols );
145 }
146
147 static UINT INSERT_get_column_info( struct tagMSIVIEW *view,
148                 UINT n, LPWSTR *name, UINT *type )
149 {
150     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
151     MSIVIEW *sv;
152
153     TRACE("%p %d %p %p\n", iv, n, name, type );
154
155     sv = iv->sv;
156     if( !sv )
157         return ERROR_FUNCTION_FAILED;
158
159     return sv->ops->get_column_info( sv, n, name, type );
160 }
161
162 static UINT INSERT_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
163 {
164     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
165
166     TRACE("%p %d %ld\n", iv, eModifyMode, hrec );
167
168     return ERROR_FUNCTION_FAILED;
169 }
170
171 static UINT INSERT_delete( struct tagMSIVIEW *view )
172 {
173     MSIINSERTVIEW *iv = (MSIINSERTVIEW*)view;
174     MSIVIEW *sv;
175
176     TRACE("%p\n", iv );
177
178     sv = iv->sv;
179     if( sv )
180         sv->ops->delete( sv );
181     delete_value_list( iv->vals );
182     HeapFree( GetProcessHeap(), 0, iv );
183     msiobj_release( &iv->db->hdr );
184
185     return ERROR_SUCCESS;
186 }
187
188
189 MSIVIEWOPS insert_ops =
190 {
191     INSERT_fetch_int,
192     NULL,
193     NULL,
194     NULL,
195     INSERT_execute,
196     INSERT_close,
197     INSERT_get_dimensions,
198     INSERT_get_column_info,
199     INSERT_modify,
200     INSERT_delete
201 };
202
203 UINT INSERT_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR table,
204                         string_list *columns, value_list *values, BOOL temp )
205 {
206     MSIINSERTVIEW *iv = NULL;
207     UINT r;
208     MSIVIEW *tv = NULL, *sv = NULL;
209
210     TRACE("%p\n", iv );
211
212     r = TABLE_CreateView( db, table, &tv );
213     if( r != ERROR_SUCCESS )
214         return r;
215
216     r = SELECT_CreateView( db, &sv, tv, columns );
217     if( r != ERROR_SUCCESS )
218     {
219         if( tv )
220             tv->ops->delete( tv );
221         return r;
222     }
223     
224     iv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *iv );
225     if( !iv )
226         return ERROR_FUNCTION_FAILED;
227
228     /* fill the structure */
229     iv->view.ops = &insert_ops;
230     msiobj_addref( &db->hdr );
231     iv->db = db;
232     iv->vals = values;
233     iv->bIsTemp = temp;
234     iv->sv = sv;
235     *view = (MSIVIEW*) iv;
236
237     return ERROR_SUCCESS;
238 }