atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[wine] / dlls / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "wine/unicode.h"
28 #include "msi.h"
29 #include "msiquery.h"
30 #include "objbase.h"
31 #include "objidl.h"
32 #include "msipriv.h"
33 #include "winnls.h"
34
35 #include "query.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
38
39
40 /* below is the query interface to a table */
41
42 typedef struct tagMSICREATEVIEW
43 {
44     MSIVIEW          view;
45     MSIDATABASE     *db;
46     LPCWSTR          name;
47     BOOL             bIsTemp;
48     BOOL             hold;
49     column_info     *col_info;
50 } MSICREATEVIEW;
51
52 static UINT CREATE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
53 {
54     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
55
56     TRACE("%p %d %d %p\n", cv, row, col, val );
57
58     return ERROR_FUNCTION_FAILED;
59 }
60
61 static UINT CREATE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
62 {
63     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
64     BOOL persist = (cv->bIsTemp) ? MSICONDITION_FALSE : MSICONDITION_TRUE;
65
66     TRACE("%p Table %s (%s)\n", cv, debugstr_w(cv->name),
67           cv->bIsTemp?"temporary":"permanent");
68
69     if (cv->bIsTemp && !cv->hold)
70         return ERROR_SUCCESS;
71
72     return msi_create_table( cv->db, cv->name, cv->col_info, persist );
73 }
74
75 static UINT CREATE_close( struct tagMSIVIEW *view )
76 {
77     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
78
79     TRACE("%p\n", cv);
80
81     return ERROR_SUCCESS;
82 }
83
84 static UINT CREATE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
85 {
86     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
87
88     TRACE("%p %p %p\n", cv, rows, cols );
89
90     return ERROR_FUNCTION_FAILED;
91 }
92
93 static UINT CREATE_get_column_info( struct tagMSIVIEW *view, UINT n, LPCWSTR *name,
94                                     UINT *type, BOOL *temporary, LPCWSTR *table_name )
95 {
96     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
97
98     TRACE("%p %d %p %p %p %p\n", cv, n, name, type, temporary, table_name );
99
100     return ERROR_FUNCTION_FAILED;
101 }
102
103 static UINT CREATE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
104                            MSIRECORD *rec, UINT row)
105 {
106     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
107
108     TRACE("%p %d %p\n", cv, eModifyMode, rec );
109
110     return ERROR_FUNCTION_FAILED;
111 }
112
113 static UINT CREATE_delete( struct tagMSIVIEW *view )
114 {
115     MSICREATEVIEW *cv = (MSICREATEVIEW*)view;
116
117     TRACE("%p\n", cv );
118
119     msiobj_release( &cv->db->hdr );
120     msi_free( cv );
121
122     return ERROR_SUCCESS;
123 }
124
125 static const MSIVIEWOPS create_ops =
126 {
127     CREATE_fetch_int,
128     NULL,
129     NULL,
130     NULL,
131     NULL,
132     NULL,
133     CREATE_execute,
134     CREATE_close,
135     CREATE_get_dimensions,
136     CREATE_get_column_info,
137     CREATE_modify,
138     CREATE_delete,
139     NULL,
140     NULL,
141     NULL,
142     NULL,
143     NULL,
144     NULL,
145     NULL,
146 };
147
148 static UINT check_columns( const column_info *col_info )
149 {
150     const column_info *c1, *c2;
151
152     /* check for two columns with the same name */
153     for( c1 = col_info; c1; c1 = c1->next )
154         for( c2 = c1->next; c2; c2 = c2->next )
155             if (!strcmpW( c1->column, c2->column ))
156                 return ERROR_BAD_QUERY_SYNTAX;
157
158     return ERROR_SUCCESS;
159 }
160
161 UINT CREATE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
162                         column_info *col_info, BOOL hold )
163 {
164     MSICREATEVIEW *cv = NULL;
165     UINT r;
166     column_info *col;
167     BOOL temp = TRUE;
168     BOOL tempprim = FALSE;
169
170     TRACE("%p\n", cv );
171
172     r = check_columns( col_info );
173     if( r != ERROR_SUCCESS )
174         return r;
175
176     cv = msi_alloc_zero( sizeof *cv );
177     if( !cv )
178         return ERROR_FUNCTION_FAILED;
179
180     for( col = col_info; col; col = col->next )
181     {
182         if (!col->table)
183             col->table = table;
184
185         if( !col->temporary )
186             temp = FALSE;
187         else if ( col->type & MSITYPE_KEY )
188             tempprim = TRUE;
189     }
190
191     if ( !temp && tempprim )
192     {
193         msi_free( cv );
194         return ERROR_FUNCTION_FAILED;
195     }
196
197     /* fill the structure */
198     cv->view.ops = &create_ops;
199     msiobj_addref( &db->hdr );
200     cv->db = db;
201     cv->name = table;
202     cv->col_info = col_info;
203     cv->bIsTemp = temp;
204     cv->hold = hold;
205     *view = (MSIVIEW*) cv;
206
207     return ERROR_SUCCESS;
208 }