2003-08-13 03:27:48 +02:00
|
|
|
/*
|
|
|
|
* Implementation of the Microsoft Installer (msi.dll)
|
|
|
|
*
|
2005-09-22 12:34:07 +02:00
|
|
|
* Copyright 2002-2005 Mike McCormack for CodeWeavers
|
2003-08-13 03:27:48 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-08-13 03:27:48 +02:00
|
|
|
*/
|
|
|
|
|
2004-10-07 05:06:48 +02:00
|
|
|
#include <stdarg.h>
|
2006-11-12 19:43:16 +01:00
|
|
|
#include <assert.h>
|
2004-10-07 05:06:48 +02:00
|
|
|
|
|
|
|
#define COBJMACROS
|
2004-03-19 02:16:36 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "msi.h"
|
|
|
|
#include "msiquery.h"
|
|
|
|
#include "objbase.h"
|
|
|
|
#include "objidl.h"
|
|
|
|
#include "winnls.h"
|
2006-10-17 11:00:33 +02:00
|
|
|
#include "msipriv.h"
|
2003-08-13 03:27:48 +02:00
|
|
|
#include "query.h"
|
|
|
|
|
2006-10-17 11:00:33 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
|
2005-11-02 15:24:21 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(msidb);
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2006-03-18 17:12:15 +01:00
|
|
|
#define MSITABLE_HASH_TABLE_SIZE 37
|
2007-06-08 01:41:51 +02:00
|
|
|
#define LONG_STR_BYTES 3
|
2006-03-18 17:12:15 +01:00
|
|
|
|
|
|
|
typedef struct tagMSICOLUMNHASHENTRY
|
|
|
|
{
|
|
|
|
struct tagMSICOLUMNHASHENTRY *next;
|
|
|
|
UINT value;
|
|
|
|
UINT row;
|
|
|
|
} MSICOLUMNHASHENTRY;
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
typedef struct tagMSICOLUMNINFO
|
|
|
|
{
|
2007-10-17 14:14:21 +02:00
|
|
|
LPWSTR tablename;
|
2003-08-13 03:27:48 +02:00
|
|
|
UINT number;
|
2007-10-17 14:14:21 +02:00
|
|
|
LPWSTR colname;
|
2003-08-13 03:27:48 +02:00
|
|
|
UINT type;
|
|
|
|
UINT offset;
|
2007-07-21 03:41:16 +02:00
|
|
|
INT ref_count;
|
2006-03-18 17:12:15 +01:00
|
|
|
MSICOLUMNHASHENTRY **hash_table;
|
2003-08-13 03:27:48 +02:00
|
|
|
} MSICOLUMNINFO;
|
|
|
|
|
2007-12-18 02:35:31 +01:00
|
|
|
typedef struct tagMSIORDERINFO
|
|
|
|
{
|
|
|
|
UINT *reorder;
|
|
|
|
UINT num_cols;
|
|
|
|
UINT cols[1];
|
|
|
|
} MSIORDERINFO;
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
struct tagMSITABLE
|
|
|
|
{
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE **data;
|
2004-03-18 05:04:08 +01:00
|
|
|
UINT row_count;
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE **nonpersistent_data;
|
2007-04-23 09:24:41 +02:00
|
|
|
UINT nonpersistent_row_count;
|
2005-09-14 12:07:26 +02:00
|
|
|
struct list entry;
|
2007-04-23 09:20:21 +02:00
|
|
|
MSICOLUMNINFO *colinfo;
|
|
|
|
UINT col_count;
|
2007-04-24 12:00:44 +02:00
|
|
|
BOOL persistent;
|
2007-07-19 03:21:00 +02:00
|
|
|
INT ref_count;
|
2003-08-13 03:27:48 +02:00
|
|
|
WCHAR name[1];
|
2004-03-18 05:04:08 +01:00
|
|
|
};
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
typedef struct tagMSITRANSFORM {
|
|
|
|
struct list entry;
|
|
|
|
IStorage *stg;
|
|
|
|
} MSITRANSFORM;
|
|
|
|
|
2006-10-05 10:00:10 +02:00
|
|
|
static const WCHAR szStringData[] = {
|
|
|
|
'_','S','t','r','i','n','g','D','a','t','a',0 };
|
|
|
|
static const WCHAR szStringPool[] = {
|
|
|
|
'_','S','t','r','i','n','g','P','o','o','l',0 };
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
/* information for default tables */
|
2007-10-17 14:14:21 +02:00
|
|
|
static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
|
|
|
|
static WCHAR szTable[] = { 'T','a','b','l','e',0 };
|
|
|
|
static WCHAR szName[] = { 'N','a','m','e',0 };
|
|
|
|
static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
|
|
|
|
static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
|
|
|
|
static WCHAR szType[] = { 'T','y','p','e',0 };
|
2007-04-23 09:20:21 +02:00
|
|
|
|
2007-04-26 22:05:54 +02:00
|
|
|
/* These tables are written into (the .hash_table part).
|
|
|
|
* Do not mark them const.
|
|
|
|
*/
|
|
|
|
static MSICOLUMNINFO _Columns_cols[4] = {
|
2008-01-10 02:58:48 +01:00
|
|
|
{ szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, NULL },
|
|
|
|
{ szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, NULL },
|
|
|
|
{ szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, NULL },
|
|
|
|
{ szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, NULL },
|
2007-04-23 09:20:21 +02:00
|
|
|
};
|
2007-04-26 22:05:54 +02:00
|
|
|
static MSICOLUMNINFO _Tables_cols[1] = {
|
2008-01-10 02:58:48 +01:00
|
|
|
{ szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0, 0, NULL },
|
2007-04-23 09:20:21 +02:00
|
|
|
};
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
#define MAX_STREAM_NAME 0x1f
|
|
|
|
|
2004-03-18 05:04:08 +01:00
|
|
|
static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
|
|
|
|
MSICOLUMNINFO **pcols, UINT *pcount );
|
2007-04-23 09:22:27 +02:00
|
|
|
static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count );
|
2006-10-19 18:53:51 +02:00
|
|
|
static UINT get_tablecolumns( MSIDATABASE *db,
|
2004-03-18 05:04:08 +01:00
|
|
|
LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
|
2005-09-26 15:25:48 +02:00
|
|
|
static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-06-08 01:41:51 +02:00
|
|
|
|
|
|
|
void msi_table_set_strref(UINT bytes_per_strref)
|
|
|
|
{
|
|
|
|
_Columns_cols[0].offset = 0;
|
|
|
|
_Columns_cols[1].offset = bytes_per_strref;
|
|
|
|
_Columns_cols[2].offset = _Columns_cols[1].offset + sizeof(USHORT);
|
|
|
|
_Columns_cols[3].offset = _Columns_cols[2].offset + bytes_per_strref;
|
|
|
|
}
|
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2007-06-26 23:24:47 +02:00
|
|
|
if( MSITYPE_IS_BINARY(col->type) )
|
|
|
|
return 2;
|
2004-03-18 05:04:08 +01:00
|
|
|
if( col->type & MSITYPE_STRING )
|
2007-06-08 01:41:51 +02:00
|
|
|
return _Columns_cols[1].offset;
|
2004-03-18 05:04:08 +01:00
|
|
|
if( (col->type & 0xff) > 4 )
|
|
|
|
ERR("Invalid column size!\n");
|
|
|
|
return col->type & 0xff;
|
|
|
|
}
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
static int utf2mime(int x)
|
|
|
|
{
|
|
|
|
if( (x>='0') && (x<='9') )
|
|
|
|
return x-'0';
|
|
|
|
if( (x>='A') && (x<='Z') )
|
|
|
|
return x-'A'+10;
|
|
|
|
if( (x>='a') && (x<='z') )
|
|
|
|
return x-'a'+10+26;
|
|
|
|
if( x=='.' )
|
|
|
|
return 10+26+26;
|
|
|
|
if( x=='_' )
|
|
|
|
return 10+26+26+1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-25 12:22:01 +02:00
|
|
|
LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
DWORD count = MAX_STREAM_NAME;
|
|
|
|
DWORD ch, next;
|
2004-07-06 21:00:46 +02:00
|
|
|
LPWSTR out, p;
|
|
|
|
|
|
|
|
if( !bTable )
|
2005-05-11 14:58:22 +02:00
|
|
|
count = lstrlenW( in )+2;
|
2005-09-20 13:59:14 +02:00
|
|
|
out = msi_alloc( count*sizeof(WCHAR) );
|
2004-07-06 21:00:46 +02:00
|
|
|
p = out;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
if( bTable )
|
|
|
|
{
|
2004-07-06 21:00:46 +02:00
|
|
|
*p++ = 0x4840;
|
2003-08-13 03:27:48 +02:00
|
|
|
count --;
|
|
|
|
}
|
|
|
|
while( count -- )
|
|
|
|
{
|
|
|
|
ch = *in++;
|
|
|
|
if( !ch )
|
|
|
|
{
|
2004-07-06 21:00:46 +02:00
|
|
|
*p = ch;
|
|
|
|
return out;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
|
|
|
|
{
|
|
|
|
ch = utf2mime(ch) + 0x4800;
|
|
|
|
next = *in;
|
|
|
|
if( next && (next<0x80) )
|
|
|
|
{
|
|
|
|
next = utf2mime(next);
|
2007-12-26 22:44:22 +01:00
|
|
|
if( next != -1 )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
next += 0x3ffffc0;
|
|
|
|
ch += (next<<6);
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-07-06 21:00:46 +02:00
|
|
|
*p++ = ch;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
2004-07-06 21:00:46 +02:00
|
|
|
ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( out );
|
2004-07-06 21:00:46 +02:00
|
|
|
return NULL;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mime2utf(int x)
|
|
|
|
{
|
|
|
|
if( x<10 )
|
|
|
|
return x + '0';
|
|
|
|
if( x<(10+26))
|
|
|
|
return x - 10 + 'A';
|
|
|
|
if( x<(10+26+26))
|
|
|
|
return x - 10 - 26 + 'a';
|
|
|
|
if( x == (10+26+26) )
|
|
|
|
return '.';
|
|
|
|
return '_';
|
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
BOOL decode_streamname(LPCWSTR in, LPWSTR out)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
WCHAR ch;
|
|
|
|
DWORD count = 0;
|
|
|
|
|
|
|
|
while ( (ch = *in++) )
|
|
|
|
{
|
|
|
|
if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
|
|
|
|
{
|
|
|
|
if( ch >= 0x4800 )
|
|
|
|
ch = mime2utf(ch-0x4800);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch -= 0x3800;
|
|
|
|
*out++ = mime2utf(ch&0x3f);
|
|
|
|
count++;
|
|
|
|
ch = mime2utf((ch>>6)&0x3f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*out++ = ch;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*out = 0;
|
|
|
|
return count;
|
|
|
|
}
|
2004-06-29 05:41:28 +02:00
|
|
|
|
|
|
|
void enum_stream_names( IStorage *stg )
|
|
|
|
{
|
|
|
|
IEnumSTATSTG *stgenum = NULL;
|
|
|
|
HRESULT r;
|
|
|
|
STATSTG stat;
|
|
|
|
ULONG n, count;
|
|
|
|
WCHAR name[0x40];
|
|
|
|
|
|
|
|
r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
|
|
|
|
if( FAILED( r ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
|
|
|
|
if( FAILED( r ) || !count )
|
|
|
|
break;
|
|
|
|
decode_streamname( stat.pwcsName, name );
|
2006-10-05 06:41:22 +02:00
|
|
|
TRACE("stream %2d -> %s %s\n", n,
|
2005-01-20 20:57:22 +01:00
|
|
|
debugstr_w(stat.pwcsName), debugstr_w(name) );
|
2007-12-26 12:02:55 +01:00
|
|
|
CoTaskMemFree( stat.pwcsName );
|
2004-06-29 05:41:28 +02:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumSTATSTG_Release( stgenum );
|
|
|
|
}
|
|
|
|
|
2007-07-16 23:45:29 +02:00
|
|
|
UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
|
2007-06-26 23:25:28 +02:00
|
|
|
BYTE **pdata, UINT *psz )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
HRESULT r;
|
|
|
|
UINT ret = ERROR_FUNCTION_FAILED;
|
|
|
|
VOID *data;
|
|
|
|
ULONG sz, count;
|
2003-10-28 22:49:06 +01:00
|
|
|
IStream *stm = NULL;
|
|
|
|
STATSTG stat;
|
2004-07-06 21:00:46 +02:00
|
|
|
LPWSTR encname;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-07-16 23:45:29 +02:00
|
|
|
encname = encode_streamname(table, stname);
|
2004-03-18 05:04:08 +01:00
|
|
|
|
|
|
|
TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2004-03-18 05:04:08 +01:00
|
|
|
r = IStorage_OpenStream(stg, encname, NULL,
|
2003-10-28 22:49:06 +01:00
|
|
|
STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( encname );
|
2003-08-13 03:27:48 +02:00
|
|
|
if( FAILED( r ) )
|
|
|
|
{
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("open stream failed r = %08x - empty table?\n", r);
|
2003-10-28 22:49:06 +01:00
|
|
|
return ret;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
|
|
|
|
if( FAILED( r ) )
|
|
|
|
{
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("open stream failed r = %08x!\n", r);
|
2003-08-13 03:27:48 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( stat.cbSize.QuadPart >> 32 )
|
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("Too big!\n");
|
2003-08-13 03:27:48 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz = stat.cbSize.QuadPart;
|
2005-09-20 13:59:14 +02:00
|
|
|
data = msi_alloc( sz );
|
2003-08-13 03:27:48 +02:00
|
|
|
if( !data )
|
|
|
|
{
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("couldn't allocate memory r=%08x!\n", r);
|
2003-10-28 22:49:06 +01:00
|
|
|
ret = ERROR_NOT_ENOUGH_MEMORY;
|
2003-08-13 03:27:48 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = IStream_Read(stm, data, sz, &count );
|
2003-10-28 22:49:06 +01:00
|
|
|
if( FAILED( r ) || ( count != sz ) )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( data );
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("read stream failed r = %08x!\n", r);
|
2003-08-13 03:27:48 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2003-10-28 22:49:06 +01:00
|
|
|
*pdata = data;
|
|
|
|
*psz = sz;
|
|
|
|
ret = ERROR_SUCCESS;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
end:
|
2003-10-28 22:49:06 +01:00
|
|
|
IStream_Release( stm );
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-06-29 05:41:28 +02:00
|
|
|
UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
|
|
|
|
{
|
2004-07-06 21:00:46 +02:00
|
|
|
LPWSTR encname;
|
2004-06-29 05:41:28 +02:00
|
|
|
HRESULT r;
|
|
|
|
|
2004-07-06 21:00:46 +02:00
|
|
|
encname = encode_streamname(FALSE, stname);
|
2004-06-29 05:41:28 +02:00
|
|
|
|
|
|
|
TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
|
|
|
|
|
|
|
|
r = IStorage_OpenStream(db->storage, encname, NULL,
|
|
|
|
STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
|
|
|
|
if( FAILED( r ) )
|
|
|
|
{
|
2005-10-27 14:25:04 +02:00
|
|
|
MSITRANSFORM *transform;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
|
|
|
|
{
|
|
|
|
TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
|
|
|
|
r = IStorage_OpenStream( transform->stg, encname, NULL,
|
|
|
|
STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
|
|
|
|
if (SUCCEEDED(r))
|
|
|
|
break;
|
|
|
|
}
|
2004-06-29 05:41:28 +02:00
|
|
|
}
|
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
msi_free( encname );
|
|
|
|
|
|
|
|
return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
|
2004-06-29 05:41:28 +02:00
|
|
|
}
|
|
|
|
|
2004-07-10 00:25:34 +02:00
|
|
|
UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
|
2004-06-29 05:41:28 +02:00
|
|
|
USHORT **pdata, UINT *psz )
|
|
|
|
{
|
|
|
|
HRESULT r;
|
|
|
|
UINT ret = ERROR_FUNCTION_FAILED;
|
|
|
|
VOID *data;
|
|
|
|
ULONG sz, count;
|
|
|
|
IStream *stm = NULL;
|
|
|
|
STATSTG stat;
|
|
|
|
|
2004-07-10 00:25:34 +02:00
|
|
|
r = db_get_raw_stream( db, stname, &stm );
|
2004-06-29 05:41:28 +02:00
|
|
|
if( r != ERROR_SUCCESS)
|
2004-07-10 00:25:34 +02:00
|
|
|
return ret;
|
2004-06-29 05:41:28 +02:00
|
|
|
r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
|
|
|
|
if( FAILED( r ) )
|
|
|
|
{
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("open stream failed r = %08x!\n", r);
|
2004-06-29 05:41:28 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( stat.cbSize.QuadPart >> 32 )
|
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("Too big!\n");
|
2004-06-29 05:41:28 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
sz = stat.cbSize.QuadPart;
|
2005-09-20 13:59:14 +02:00
|
|
|
data = msi_alloc( sz );
|
2004-06-29 05:41:28 +02:00
|
|
|
if( !data )
|
|
|
|
{
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("couldn't allocate memory r=%08x!\n", r);
|
2004-06-29 05:41:28 +02:00
|
|
|
ret = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = IStream_Read(stm, data, sz, &count );
|
|
|
|
if( FAILED( r ) || ( count != sz ) )
|
|
|
|
{
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( data );
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("read stream failed r = %08x!\n", r);
|
2004-06-29 05:41:28 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pdata = data;
|
|
|
|
*psz = sz;
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
end:
|
|
|
|
IStream_Release( stm );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:17:40 +02:00
|
|
|
UINT write_stream_data( IStorage *stg, LPCWSTR stname,
|
2007-06-13 23:36:31 +02:00
|
|
|
LPCVOID data, UINT sz, BOOL bTable )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
|
|
|
HRESULT r;
|
|
|
|
UINT ret = ERROR_FUNCTION_FAILED;
|
|
|
|
ULONG count;
|
|
|
|
IStream *stm = NULL;
|
|
|
|
ULARGE_INTEGER size;
|
|
|
|
LARGE_INTEGER pos;
|
2004-07-06 21:00:46 +02:00
|
|
|
LPWSTR encname;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-04-25 12:22:01 +02:00
|
|
|
encname = encode_streamname(bTable, stname );
|
2004-03-19 02:16:36 +01:00
|
|
|
r = IStorage_OpenStream( stg, encname, NULL,
|
2004-03-18 05:04:08 +01:00
|
|
|
STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
|
2004-03-19 02:16:36 +01:00
|
|
|
if( FAILED(r) )
|
|
|
|
{
|
|
|
|
r = IStorage_CreateStream( stg, encname,
|
|
|
|
STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
|
|
|
|
}
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( encname );
|
2004-03-18 05:04:08 +01:00
|
|
|
if( FAILED( r ) )
|
|
|
|
{
|
2006-10-05 06:41:22 +02:00
|
|
|
WARN("open stream failed r = %08x\n", r);
|
2004-03-18 05:04:08 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
size.QuadPart = sz;
|
|
|
|
r = IStream_SetSize( stm, size );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( FAILED( r ) )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("Failed to SetSize\n");
|
2004-03-18 05:04:08 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos.QuadPart = 0;
|
|
|
|
r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( FAILED( r ) )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("Failed to Seek\n");
|
2004-03-18 05:04:08 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:17:40 +02:00
|
|
|
if (sz)
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2007-04-23 09:17:40 +02:00
|
|
|
r = IStream_Write(stm, data, sz, &count );
|
|
|
|
if( FAILED( r ) || ( count != sz ) )
|
|
|
|
{
|
|
|
|
WARN("Failed to Write\n");
|
|
|
|
goto end;
|
|
|
|
}
|
2004-03-18 05:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
end:
|
|
|
|
IStream_Release( stm );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-09-17 16:29:38 +02:00
|
|
|
static void free_table( MSITABLE *table )
|
|
|
|
{
|
2008-01-03 22:56:39 +01:00
|
|
|
UINT i;
|
2005-09-17 16:29:38 +02:00
|
|
|
for( i=0; i<table->row_count; i++ )
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( table->data[i] );
|
|
|
|
msi_free( table->data );
|
2007-04-23 09:24:41 +02:00
|
|
|
for( i=0; i<table->nonpersistent_row_count; i++ )
|
|
|
|
msi_free( table->nonpersistent_data[i] );
|
|
|
|
msi_free( table->nonpersistent_data );
|
2007-04-23 09:20:21 +02:00
|
|
|
if( (table->colinfo != _Tables_cols) &&
|
|
|
|
(table->colinfo != _Columns_cols) )
|
|
|
|
{
|
|
|
|
msi_free_colinfo( table->colinfo, table->col_count );
|
|
|
|
msi_free( table->colinfo );
|
|
|
|
}
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( table );
|
2005-09-17 16:29:38 +02:00
|
|
|
}
|
|
|
|
|
2005-09-26 15:25:48 +02:00
|
|
|
static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
|
|
|
|
{
|
|
|
|
const MSICOLUMNINFO *last_col = &cols[count-1];
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
return last_col->offset + bytes_per_column( last_col );
|
|
|
|
}
|
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
/* add this table to the list of cached tables in the database */
|
2007-04-23 09:20:21 +02:00
|
|
|
static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
|
2003-10-28 22:49:06 +01:00
|
|
|
{
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE *rawdata = NULL;
|
2005-09-22 12:34:07 +02:00
|
|
|
UINT rawsize = 0, i, j, row_size = 0;
|
2003-10-28 22:49:06 +01:00
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
TRACE("%s\n",debugstr_w(t->name));
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
row_size = msi_table_get_row_size( t->colinfo, t->col_count );
|
2003-10-28 22:49:06 +01:00
|
|
|
|
|
|
|
/* if we can't read the table, just assume that it's empty */
|
2007-07-16 23:45:29 +02:00
|
|
|
read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
|
2004-03-18 05:04:08 +01:00
|
|
|
if( !rawdata )
|
2007-04-23 09:20:21 +02:00
|
|
|
return ERROR_SUCCESS;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
|
|
|
TRACE("Read %d bytes\n", rawsize );
|
|
|
|
|
|
|
|
if( rawsize % row_size )
|
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("Table size is invalid %d/%d\n", rawsize, row_size );
|
2005-09-21 12:20:20 +02:00
|
|
|
goto err;
|
2004-03-18 05:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
t->row_count = rawsize / row_size;
|
2005-09-20 13:59:14 +02:00
|
|
|
t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
|
2004-03-18 05:04:08 +01:00
|
|
|
if( !t->data )
|
2005-09-17 16:29:38 +02:00
|
|
|
goto err;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
|
|
|
/* transpose all the data */
|
2006-07-26 23:52:56 +02:00
|
|
|
TRACE("Transposing data from %d rows\n", t->row_count );
|
2004-03-18 05:04:08 +01:00
|
|
|
for( i=0; i<t->row_count; i++ )
|
|
|
|
{
|
2005-09-20 13:59:14 +02:00
|
|
|
t->data[i] = msi_alloc( row_size );
|
2004-03-18 05:04:08 +01:00
|
|
|
if( !t->data[i] )
|
2005-09-17 16:29:38 +02:00
|
|
|
goto err;
|
2005-09-21 12:20:20 +02:00
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
for( j=0; j<t->col_count; j++ )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2007-06-08 01:41:15 +02:00
|
|
|
UINT ofs = t->colinfo[j].offset;
|
2007-04-23 09:20:21 +02:00
|
|
|
UINT n = bytes_per_column( &t->colinfo[j] );
|
2007-06-08 01:40:31 +02:00
|
|
|
UINT k;
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2007-06-08 01:41:51 +02:00
|
|
|
if ( n != 2 && n != 3 && n != 4 )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
|
|
|
ERR("oops - unknown column width %d\n", n);
|
2005-09-17 16:29:38 +02:00
|
|
|
goto err;
|
2004-03-18 05:04:08 +01:00
|
|
|
}
|
2007-06-08 01:40:31 +02:00
|
|
|
|
2007-06-08 01:41:15 +02:00
|
|
|
for ( k = 0; k < n; k++ )
|
|
|
|
t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
|
2004-03-18 05:04:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( rawdata );
|
2007-04-23 09:20:21 +02:00
|
|
|
return ERROR_SUCCESS;
|
2005-09-17 16:29:38 +02:00
|
|
|
err:
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( rawdata );
|
2007-04-23 09:20:21 +02:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_cached_tables( MSIDATABASE *db )
|
|
|
|
{
|
2005-09-14 12:07:26 +02:00
|
|
|
while( !list_empty( &db->tables ) )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-09-14 12:07:26 +02:00
|
|
|
MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2005-09-14 12:07:26 +02:00
|
|
|
list_remove( &t->entry );
|
2005-09-21 12:20:20 +02:00
|
|
|
free_table( t );
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
MSITABLE *t;
|
|
|
|
|
2005-09-14 12:07:26 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
|
2003-08-13 03:27:48 +02:00
|
|
|
if( !lstrcmpW( name, t->name ) )
|
2005-09-21 12:20:20 +02:00
|
|
|
return t;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2005-09-21 12:20:20 +02:00
|
|
|
return NULL;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
2004-03-18 05:04:08 +01:00
|
|
|
static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
|
|
|
|
{
|
2005-09-22 12:34:07 +02:00
|
|
|
UINT r, column_count = 0;
|
2004-03-18 05:04:08 +01:00
|
|
|
MSICOLUMNINFO *columns;
|
|
|
|
|
|
|
|
/* get the number of columns in this table */
|
|
|
|
column_count = 0;
|
|
|
|
r = get_tablecolumns( db, name, NULL, &column_count );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* if there's no columns, there's no table */
|
|
|
|
if( column_count == 0 )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
TRACE("Table %s found\n", debugstr_w(name) );
|
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
|
2004-03-18 05:04:08 +01:00
|
|
|
if( !columns )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
|
|
|
r = get_tablecolumns( db, name, columns, &column_count );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( columns );
|
2004-03-18 05:04:08 +01:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pcols = columns;
|
|
|
|
*pcount = column_count;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:21:32 +02:00
|
|
|
UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
|
|
|
|
BOOL persistent, MSITABLE **table_ret)
|
|
|
|
{
|
|
|
|
UINT r, nField;
|
|
|
|
MSIVIEW *tv = NULL;
|
|
|
|
MSIRECORD *rec = NULL;
|
|
|
|
column_info *col;
|
2007-04-23 09:22:27 +02:00
|
|
|
MSITABLE *table;
|
|
|
|
UINT i;
|
2007-04-23 09:21:32 +02:00
|
|
|
|
|
|
|
/* only add tables that don't exist already */
|
|
|
|
if( TABLE_Exists(db, name ) )
|
2007-04-23 09:24:41 +02:00
|
|
|
{
|
|
|
|
WARN("table %s exists\n", debugstr_w(name));
|
2007-04-23 09:21:32 +02:00
|
|
|
return ERROR_BAD_QUERY_SYNTAX;
|
2007-04-23 09:24:41 +02:00
|
|
|
}
|
2007-04-23 09:21:32 +02:00
|
|
|
|
2007-04-23 09:22:27 +02:00
|
|
|
table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
|
|
|
|
if( !table )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2007-07-19 03:21:00 +02:00
|
|
|
table->ref_count = 1;
|
2007-04-23 09:22:27 +02:00
|
|
|
table->row_count = 0;
|
|
|
|
table->data = NULL;
|
2007-04-23 09:24:41 +02:00
|
|
|
table->nonpersistent_row_count = 0;
|
|
|
|
table->nonpersistent_data = NULL;
|
2007-04-23 09:22:27 +02:00
|
|
|
table->colinfo = NULL;
|
|
|
|
table->col_count = 0;
|
2007-04-24 12:00:44 +02:00
|
|
|
table->persistent = persistent;
|
2007-04-23 09:22:27 +02:00
|
|
|
lstrcpyW( table->name, name );
|
|
|
|
|
|
|
|
for( col = col_info; col; col = col->next )
|
|
|
|
table->col_count++;
|
|
|
|
|
|
|
|
table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
|
|
|
|
if (!table->colinfo)
|
|
|
|
{
|
|
|
|
free_table( table );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0, col = col_info; col; i++, col = col->next )
|
|
|
|
{
|
|
|
|
table->colinfo[ i ].tablename = strdupW( col->table );
|
|
|
|
table->colinfo[ i ].number = i + 1;
|
|
|
|
table->colinfo[ i ].colname = strdupW( col->column );
|
|
|
|
table->colinfo[ i ].type = col->type;
|
|
|
|
table->colinfo[ i ].offset = 0;
|
2007-07-21 03:41:16 +02:00
|
|
|
table->colinfo[ i ].ref_count = 0;
|
2007-04-23 09:22:27 +02:00
|
|
|
table->colinfo[ i ].hash_table = NULL;
|
|
|
|
}
|
|
|
|
table_calc_column_offsets( table->colinfo, table->col_count);
|
|
|
|
|
2007-04-23 09:21:32 +02:00
|
|
|
r = TABLE_CreateView( db, szTables, &tv );
|
|
|
|
TRACE("CreateView returned %x\n", r);
|
|
|
|
if( r )
|
2007-04-23 09:22:27 +02:00
|
|
|
{
|
|
|
|
free_table( table );
|
2007-04-23 09:21:32 +02:00
|
|
|
return r;
|
2007-04-23 09:22:27 +02:00
|
|
|
}
|
2007-04-23 09:21:32 +02:00
|
|
|
|
|
|
|
r = tv->ops->execute( tv, 0 );
|
|
|
|
TRACE("tv execute returned %x\n", r);
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
rec = MSI_CreateRecord( 1 );
|
|
|
|
if( !rec )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
r = MSI_RecordSetStringW( rec, 1, name );
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
2007-04-23 09:25:17 +02:00
|
|
|
r = tv->ops->insert_row( tv, rec, !persistent );
|
2007-04-23 09:21:32 +02:00
|
|
|
TRACE("insert_row returned %x\n", r);
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
tv->ops->delete( tv );
|
|
|
|
tv = NULL;
|
|
|
|
|
|
|
|
msiobj_release( &rec->hdr );
|
2007-04-24 13:31:31 +02:00
|
|
|
rec = NULL;
|
2007-04-23 09:21:32 +02:00
|
|
|
|
2007-04-23 09:25:17 +02:00
|
|
|
if( persistent )
|
2007-04-23 09:21:32 +02:00
|
|
|
{
|
2007-04-23 09:25:17 +02:00
|
|
|
/* add each column to the _Columns table */
|
|
|
|
r = TABLE_CreateView( db, szColumns, &tv );
|
2007-04-23 09:21:32 +02:00
|
|
|
if( r )
|
2007-04-23 09:25:17 +02:00
|
|
|
return r;
|
2007-04-23 09:21:32 +02:00
|
|
|
|
2007-04-23 09:25:17 +02:00
|
|
|
r = tv->ops->execute( tv, 0 );
|
|
|
|
TRACE("tv execute returned %x\n", r);
|
2007-04-23 09:21:32 +02:00
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
2007-04-23 09:25:17 +02:00
|
|
|
rec = MSI_CreateRecord( 4 );
|
|
|
|
if( !rec )
|
2007-04-23 09:21:32 +02:00
|
|
|
goto err;
|
|
|
|
|
2007-04-23 09:25:17 +02:00
|
|
|
r = MSI_RecordSetStringW( rec, 1, name );
|
2007-04-23 09:21:32 +02:00
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
2007-04-23 09:25:17 +02:00
|
|
|
/*
|
|
|
|
* need to set the table, column number, col name and type
|
|
|
|
* for each column we enter in the table
|
|
|
|
*/
|
|
|
|
nField = 1;
|
|
|
|
for( col = col_info; col; col = col->next )
|
|
|
|
{
|
|
|
|
r = MSI_RecordSetInteger( rec, 2, nField );
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
r = MSI_RecordSetStringW( rec, 3, col->column );
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
r = MSI_RecordSetInteger( rec, 4, col->type );
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
r = tv->ops->insert_row( tv, rec, FALSE );
|
|
|
|
if( r )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
nField++;
|
|
|
|
}
|
|
|
|
if( !col )
|
|
|
|
r = ERROR_SUCCESS;
|
2007-04-23 09:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (rec)
|
|
|
|
msiobj_release( &rec->hdr );
|
|
|
|
/* FIXME: remove values from the string table on error */
|
|
|
|
if( tv )
|
|
|
|
tv->ops->delete( tv );
|
|
|
|
|
|
|
|
if (r == ERROR_SUCCESS)
|
2007-04-23 09:22:27 +02:00
|
|
|
{
|
|
|
|
list_add_head( &db->tables, &table->entry );
|
|
|
|
*table_ret = table;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
free_table( table );
|
|
|
|
|
2007-04-23 09:21:32 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-09-21 12:20:20 +02:00
|
|
|
MSITABLE *table;
|
2007-04-23 09:20:21 +02:00
|
|
|
UINT r;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
/* first, see if the table is cached */
|
2005-09-21 12:20:20 +02:00
|
|
|
table = find_cached_table( db, name );
|
|
|
|
if( table )
|
2007-04-23 09:20:21 +02:00
|
|
|
{
|
|
|
|
*table_ret = table;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
/* nonexistent tables should be interpreted as empty tables */
|
|
|
|
table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
|
|
|
|
if( !table )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
table->row_count = 0;
|
|
|
|
table->data = NULL;
|
2007-04-23 09:24:41 +02:00
|
|
|
table->nonpersistent_row_count = 0;
|
|
|
|
table->nonpersistent_data = NULL;
|
2007-04-23 09:20:21 +02:00
|
|
|
table->colinfo = NULL;
|
|
|
|
table->col_count = 0;
|
2007-04-24 12:00:44 +02:00
|
|
|
table->persistent = TRUE;
|
2007-04-23 09:20:21 +02:00
|
|
|
lstrcpyW( table->name, name );
|
|
|
|
|
|
|
|
/* these two tables are special - we know the column types already */
|
|
|
|
if( !lstrcmpW( name, szColumns ) )
|
|
|
|
{
|
2007-04-26 22:05:54 +02:00
|
|
|
table->colinfo = _Columns_cols;
|
2007-04-23 09:20:21 +02:00
|
|
|
table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
|
|
|
|
}
|
|
|
|
else if( !lstrcmpW( name, szTables ) )
|
|
|
|
{
|
2007-04-26 22:05:54 +02:00
|
|
|
table->colinfo = _Tables_cols;
|
2007-04-23 09:20:21 +02:00
|
|
|
table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
free_table ( table );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = read_table_from_storage( table, db->storage );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
free_table( table );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_head( &db->tables, &table->entry );
|
|
|
|
*table_ret = table;
|
|
|
|
return ERROR_SUCCESS;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE *rawdata = NULL, *p;
|
2007-04-23 09:20:21 +02:00
|
|
|
UINT rawsize, r, i, j, row_size;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2007-04-24 12:00:44 +02:00
|
|
|
/* Nothing to do for non-persistent tables */
|
|
|
|
if( !t->persistent )
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
TRACE("Saving %s\n", debugstr_w( t->name ) );
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
row_size = msi_table_get_row_size( t->colinfo, t->col_count );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
rawsize = t->row_count * row_size;
|
2005-09-20 13:59:14 +02:00
|
|
|
rawdata = msi_alloc_zero( rawsize );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !rawdata )
|
2005-09-26 15:25:48 +02:00
|
|
|
{
|
|
|
|
r = ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
goto err;
|
|
|
|
}
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
p = rawdata;
|
2007-04-23 09:20:21 +02:00
|
|
|
for( i=0; i<t->col_count; i++ )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
|
|
|
for( j=0; j<t->row_count; j++ )
|
|
|
|
{
|
2007-04-23 09:20:21 +02:00
|
|
|
UINT offset = t->colinfo[i].offset;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2007-06-08 01:41:15 +02:00
|
|
|
*p++ = t->data[j][offset];
|
|
|
|
*p++ = t->data[j][offset + 1];
|
2007-04-23 09:20:21 +02:00
|
|
|
if( 4 == bytes_per_column( &t->colinfo[i] ) )
|
2007-06-08 01:41:15 +02:00
|
|
|
{
|
|
|
|
*p++ = t->data[j][offset + 2];
|
|
|
|
*p++ = t->data[j][offset + 3];
|
|
|
|
}
|
2004-03-19 02:16:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("writing %d bytes\n", rawsize);
|
2007-04-25 12:22:01 +02:00
|
|
|
r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2005-09-26 15:25:48 +02:00
|
|
|
err:
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( rawdata );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-02-06 10:08:20 +01:00
|
|
|
static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for( i=0; colinfo && (i<count); i++ )
|
|
|
|
{
|
|
|
|
assert( (i+1) == colinfo[ i ].number );
|
|
|
|
if (i)
|
|
|
|
colinfo[i].offset = colinfo[ i - 1 ].offset
|
|
|
|
+ bytes_per_column( &colinfo[ i - 1 ] );
|
|
|
|
else
|
|
|
|
colinfo[i].offset = 0;
|
|
|
|
TRACE("column %d is [%s] with type %08x ofs %d\n",
|
|
|
|
colinfo[i].number, debugstr_w(colinfo[i].colname),
|
|
|
|
colinfo[i].type, colinfo[i].offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-09-22 12:34:07 +02:00
|
|
|
const MSICOLUMNINFO *p;
|
|
|
|
DWORD i, n;
|
|
|
|
|
|
|
|
TRACE("%s\n", debugstr_w(name));
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
if (!lstrcmpW( name, szTables ))
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-09-22 12:34:07 +02:00
|
|
|
p = _Tables_cols;
|
|
|
|
n = 1;
|
|
|
|
}
|
|
|
|
else if (!lstrcmpW( name, szColumns ))
|
|
|
|
{
|
|
|
|
p = _Columns_cols;
|
|
|
|
n = 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
|
|
|
/* duplicate the string data so we can free it in msi_free_colinfo */
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
if (colinfo && (i < *sz) )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2008-03-06 23:33:47 +01:00
|
|
|
colinfo[i] = p[i];
|
2005-09-22 12:34:07 +02:00
|
|
|
colinfo[i].tablename = strdupW( p[i].tablename );
|
|
|
|
colinfo[i].colname = strdupW( p[i].colname );
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
2005-09-22 12:34:07 +02:00
|
|
|
if( colinfo && (i >= *sz) )
|
2003-08-13 03:27:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2007-02-06 10:08:20 +01:00
|
|
|
table_calc_column_offsets( colinfo, n );
|
2003-08-13 03:27:48 +02:00
|
|
|
*sz = n;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
|
|
|
|
{
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
for( i=0; i<count; i++ )
|
|
|
|
{
|
2007-10-17 14:14:21 +02:00
|
|
|
msi_free( colinfo[i].tablename );
|
|
|
|
msi_free( colinfo[i].colname );
|
2006-03-18 17:12:15 +01:00
|
|
|
msi_free( colinfo[i].hash_table );
|
2005-09-22 12:34:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2006-09-08 05:25:11 +02:00
|
|
|
return strdupW(msi_string_lookup_id( db->strings, stringid ));
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
|
2007-06-08 01:40:31 +02:00
|
|
|
{
|
|
|
|
UINT ret = 0, i;
|
|
|
|
|
2007-06-08 01:41:15 +02:00
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
ret += (data[row][col + i] << i * 8);
|
2007-06-08 01:40:31 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-10-19 18:53:51 +02:00
|
|
|
static UINT get_tablecolumns( MSIDATABASE *db,
|
2003-08-13 03:27:48 +02:00
|
|
|
LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
|
|
|
|
{
|
|
|
|
UINT r, i, n=0, table_id, count, maxcount = *sz;
|
|
|
|
MSITABLE *table = NULL;
|
|
|
|
|
2006-10-19 18:53:51 +02:00
|
|
|
TRACE("%s\n", debugstr_w(szTableName));
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
/* first check if there is a default table with that name */
|
|
|
|
r = get_defaulttablecolumns( szTableName, colinfo, sz );
|
|
|
|
if( ( r == ERROR_SUCCESS ) && *sz )
|
|
|
|
return r;
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
r = get_table( db, szColumns, &table );
|
|
|
|
if( r != ERROR_SUCCESS )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-09-22 12:34:07 +02:00
|
|
|
ERR("couldn't load _Columns table\n");
|
2005-09-21 12:20:20 +02:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* convert table and column names to IDs from the string table */
|
2004-06-30 20:18:27 +02:00
|
|
|
r = msi_string2idW( db->strings, szTableName, &table_id );
|
2003-08-13 03:27:48 +02:00
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
|
2003-08-13 03:27:48 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-03-18 17:12:15 +01:00
|
|
|
TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
/* Note: _Columns table doesn't have non-persistent data */
|
|
|
|
|
2006-10-19 18:53:51 +02:00
|
|
|
/* if maxcount is non-zero, assume it's exactly right for this table */
|
|
|
|
memset( colinfo, 0, maxcount*sizeof(*colinfo) );
|
2004-03-18 05:04:08 +01:00
|
|
|
count = table->row_count;
|
2003-08-13 03:27:48 +02:00
|
|
|
for( i=0; i<count; i++ )
|
|
|
|
{
|
2007-06-08 01:41:51 +02:00
|
|
|
if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
|
2003-08-13 03:27:48 +02:00
|
|
|
continue;
|
|
|
|
if( colinfo )
|
|
|
|
{
|
2007-06-08 01:41:51 +02:00
|
|
|
UINT id = read_table_int(table->data, i, _Columns_cols[2].offset, db->bytes_per_strref);
|
|
|
|
UINT col = read_table_int(table->data, i, _Columns_cols[1].offset, sizeof(USHORT)) - (1<<15);
|
2006-10-19 18:53:51 +02:00
|
|
|
|
|
|
|
/* check the column number is in range */
|
|
|
|
if (col<1 || col>maxcount)
|
|
|
|
{
|
|
|
|
ERR("column %d out of range\n", col);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if this column was already set */
|
|
|
|
if (colinfo[ col - 1 ].number)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2006-10-19 18:53:51 +02:00
|
|
|
ERR("duplicate column %d\n", col);
|
|
|
|
continue;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
2006-10-19 18:53:51 +02:00
|
|
|
|
|
|
|
colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
|
|
|
|
colinfo[ col - 1 ].number = col;
|
|
|
|
colinfo[ col - 1 ].colname = msi_makestring( db, id );
|
2007-06-08 01:41:51 +02:00
|
|
|
colinfo[ col - 1 ].type = read_table_int(table->data, i, _Columns_cols[3].offset, sizeof(USHORT)) - (1<<15);
|
2006-10-19 18:53:51 +02:00
|
|
|
colinfo[ col - 1 ].offset = 0;
|
2007-07-21 03:41:16 +02:00
|
|
|
colinfo[ col - 1 ].ref_count = 0;
|
2006-10-19 18:53:51 +02:00
|
|
|
colinfo[ col - 1 ].hash_table = NULL;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
n++;
|
2006-10-19 18:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
|
|
|
|
|
2007-02-06 10:08:20 +01:00
|
|
|
if (colinfo && n != maxcount)
|
2006-10-19 18:53:51 +02:00
|
|
|
{
|
|
|
|
ERR("missing column in table %s\n", debugstr_w(szTableName));
|
|
|
|
msi_free_colinfo(colinfo, maxcount );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
2007-02-06 10:08:20 +01:00
|
|
|
table_calc_column_offsets( colinfo, n );
|
2003-08-13 03:27:48 +02:00
|
|
|
*sz = n;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-07-20 23:01:13 +02:00
|
|
|
static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
|
|
|
|
{
|
|
|
|
MSITABLE *table;
|
2007-10-17 14:13:53 +02:00
|
|
|
UINT size, offset, old_count;
|
2008-01-03 22:56:39 +01:00
|
|
|
UINT n;
|
2007-07-20 23:01:13 +02:00
|
|
|
|
|
|
|
table = find_cached_table( db, name );
|
2007-10-17 14:13:53 +02:00
|
|
|
old_count = table->col_count;
|
2007-07-20 23:01:13 +02:00
|
|
|
msi_free( table->colinfo );
|
|
|
|
table_get_column_info( db, name, &table->colinfo, &table->col_count );
|
|
|
|
|
|
|
|
size = msi_table_get_row_size( table->colinfo, table->col_count );
|
|
|
|
offset = table->colinfo[table->col_count - 1].offset;
|
|
|
|
|
|
|
|
for ( n = 0; n < table->row_count; n++ )
|
|
|
|
{
|
|
|
|
table->data[n] = msi_realloc( table->data[n], size );
|
2007-10-17 14:13:53 +02:00
|
|
|
if (old_count < table->col_count)
|
|
|
|
memset( &table->data[n][offset], 0, size - offset );
|
2007-07-20 23:01:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
/* try to find the table name in the _Tables table */
|
2007-04-21 09:33:10 +02:00
|
|
|
BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
UINT r, table_id = 0, i, count;
|
|
|
|
MSITABLE *table = NULL;
|
|
|
|
|
|
|
|
if( !lstrcmpW( name, szTables ) )
|
|
|
|
return TRUE;
|
|
|
|
if( !lstrcmpW( name, szColumns ) )
|
|
|
|
return TRUE;
|
|
|
|
|
2004-06-30 20:18:27 +02:00
|
|
|
r = msi_string2idW( db->strings, name, &table_id );
|
2003-08-13 03:27:48 +02:00
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
2004-03-19 02:16:36 +01:00
|
|
|
TRACE("Couldn't find id for %s\n", debugstr_w(name));
|
2003-08-13 03:27:48 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
r = get_table( db, szTables, &table );
|
|
|
|
if( r != ERROR_SUCCESS )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2007-04-23 09:20:21 +02:00
|
|
|
ERR("table %s not available\n", debugstr_w(szTables));
|
2003-08-13 03:27:48 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-03-18 05:04:08 +01:00
|
|
|
count = table->row_count;
|
2003-08-13 03:27:48 +02:00
|
|
|
for( i=0; i<count; i++ )
|
2004-03-18 05:04:08 +01:00
|
|
|
if( table->data[ i ][ 0 ] == table_id )
|
2003-08-13 03:27:48 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (i!=count)
|
|
|
|
return TRUE;
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
count = table->nonpersistent_row_count;
|
|
|
|
for( i=0; i<count; i++ )
|
|
|
|
if( table->nonpersistent_data[ i ][ 0 ] == table_id )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i!=count)
|
|
|
|
return TRUE;
|
|
|
|
|
2005-05-20 21:16:50 +02:00
|
|
|
TRACE("Searched %d tables, but %d was not found\n", count, table_id );
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* below is the query interface to a table */
|
|
|
|
|
|
|
|
typedef struct tagMSITABLEVIEW
|
|
|
|
{
|
|
|
|
MSIVIEW view;
|
|
|
|
MSIDATABASE *db;
|
|
|
|
MSITABLE *table;
|
|
|
|
MSICOLUMNINFO *columns;
|
2007-12-18 02:35:31 +01:00
|
|
|
MSIORDERINFO *order;
|
2003-08-13 03:27:48 +02:00
|
|
|
UINT num_cols;
|
|
|
|
UINT row_size;
|
|
|
|
WCHAR name[1];
|
|
|
|
} MSITABLEVIEW;
|
|
|
|
|
|
|
|
static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
2007-04-23 09:24:41 +02:00
|
|
|
UINT offset, n;
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE **data;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
if( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if( (col==0) || (col>tv->num_cols) )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/* how many rows are there ? */
|
2007-04-23 09:24:41 +02:00
|
|
|
if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
|
2003-08-13 03:27:48 +02:00
|
|
|
return ERROR_NO_MORE_ITEMS;
|
|
|
|
|
|
|
|
if( tv->columns[col-1].offset >= tv->row_size )
|
|
|
|
{
|
|
|
|
ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
|
|
|
|
ERR("%p %p\n", tv, tv->columns );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
2007-12-18 02:35:31 +01:00
|
|
|
if (tv->order)
|
|
|
|
row = tv->order->reorder[row];
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
if (row >= tv->table->row_count)
|
|
|
|
{
|
|
|
|
row -= tv->table->row_count;
|
|
|
|
data = tv->table->nonpersistent_data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data = tv->table->data;
|
2007-06-08 01:40:31 +02:00
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
n = bytes_per_column( &tv->columns[col-1] );
|
2007-06-08 01:41:51 +02:00
|
|
|
if (n != 2 && n != 3 && n != 4)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
ERR("oops! what is %d bytes per column?\n", n );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
2007-06-08 01:41:15 +02:00
|
|
|
offset = tv->columns[col-1].offset;
|
2007-06-08 01:40:31 +02:00
|
|
|
*val = read_table_int(data, row, offset, n);
|
|
|
|
|
2005-11-10 13:14:56 +01:00
|
|
|
/* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-06 20:56:12 +02:00
|
|
|
/*
|
|
|
|
* We need a special case for streams, as we need to reference column with
|
|
|
|
* the name of the stream in the same table, and the table name
|
|
|
|
* which may not be available at higher levels of the query
|
|
|
|
*/
|
|
|
|
static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
UINT ival = 0, refcol = 0, r;
|
2006-09-08 05:25:11 +02:00
|
|
|
LPCWSTR sval;
|
2004-07-10 00:25:34 +02:00
|
|
|
LPWSTR full_name;
|
|
|
|
DWORD len;
|
|
|
|
static const WCHAR szDot[] = { '.', 0 };
|
2006-10-17 11:00:33 +02:00
|
|
|
WCHAR number[0x20];
|
2004-07-06 20:56:12 +02:00
|
|
|
|
|
|
|
if( !view->ops->fetch_int )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The column marked with the type stream data seems to have a single number
|
|
|
|
* which references the column containing the name of the stream data
|
|
|
|
*
|
|
|
|
* Fetch the column to reference first.
|
|
|
|
*/
|
|
|
|
r = view->ops->fetch_int( view, row, col, &ival );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
return r;
|
|
|
|
|
2006-10-17 11:00:33 +02:00
|
|
|
/* check the column value is in range */
|
2008-01-01 12:21:52 +01:00
|
|
|
if (ival > tv->num_cols || ival == col)
|
2006-10-17 11:00:33 +02:00
|
|
|
{
|
|
|
|
ERR("bad column ref (%u) for stream\n", ival);
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
2004-07-06 20:56:12 +02:00
|
|
|
|
2006-10-17 11:00:33 +02:00
|
|
|
if ( tv->columns[ival - 1].type & MSITYPE_STRING )
|
|
|
|
{
|
|
|
|
/* now get the column with the name of the stream */
|
|
|
|
r = view->ops->fetch_int( view, row, ival, &refcol );
|
|
|
|
if ( r != ERROR_SUCCESS )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* lookup the string value from the string table */
|
|
|
|
sval = msi_string_lookup_id( tv->db->strings, refcol );
|
|
|
|
if ( !sval )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static const WCHAR fmt[] = { '%','d',0 };
|
|
|
|
sprintfW( number, fmt, ival );
|
|
|
|
sval = number;
|
|
|
|
}
|
2004-07-06 20:56:12 +02:00
|
|
|
|
2005-05-11 14:58:22 +02:00
|
|
|
len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
|
2005-09-20 13:59:14 +02:00
|
|
|
full_name = msi_alloc( len*sizeof(WCHAR) );
|
2005-05-11 14:58:22 +02:00
|
|
|
lstrcpyW( full_name, tv->name );
|
|
|
|
lstrcatW( full_name, szDot );
|
|
|
|
lstrcatW( full_name, sval );
|
2004-07-10 00:25:34 +02:00
|
|
|
|
|
|
|
r = db_get_raw_stream( tv->db, full_name, stm );
|
|
|
|
if( r )
|
|
|
|
ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( full_name );
|
2004-07-06 20:56:12 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-10-26 10:09:47 +02:00
|
|
|
static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
2007-06-08 01:40:31 +02:00
|
|
|
UINT offset, n, i;
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE **data;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
if( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if( (col==0) || (col>tv->num_cols) )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
if( tv->columns[col-1].offset >= tv->row_size )
|
|
|
|
{
|
|
|
|
ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
|
|
|
|
ERR("%p %p\n", tv, tv->columns );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:19:07 +02:00
|
|
|
msi_free( tv->columns[col-1].hash_table );
|
|
|
|
tv->columns[col-1].hash_table = NULL;
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
if (row >= tv->table->row_count)
|
|
|
|
{
|
|
|
|
row -= tv->table->row_count;
|
|
|
|
data = tv->table->nonpersistent_data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data = tv->table->data;
|
2007-06-08 01:40:31 +02:00
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
n = bytes_per_column( &tv->columns[col-1] );
|
2007-06-08 01:41:51 +02:00
|
|
|
if ( n != 2 && n != 3 && n != 4 )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
|
|
|
ERR("oops! what is %d bytes per column?\n", n );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
2007-06-08 01:40:31 +02:00
|
|
|
|
2007-06-08 01:41:15 +02:00
|
|
|
offset = tv->columns[col-1].offset;
|
|
|
|
for ( i = 0; i < n; i++ )
|
|
|
|
data[row][offset + i] = (val >> i * 8) & 0xff;
|
2007-06-08 01:40:31 +02:00
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-07-27 02:40:38 +02:00
|
|
|
static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
|
|
|
|
|
|
|
|
if (!tv->table)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2007-12-18 02:35:31 +01:00
|
|
|
if (tv->order)
|
|
|
|
row = tv->order->reorder[row];
|
|
|
|
|
2007-07-27 02:40:38 +02:00
|
|
|
return msi_view_get_row(tv->db, view, row, rec);
|
|
|
|
}
|
|
|
|
|
2006-10-26 10:09:47 +02:00
|
|
|
static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
UINT i, val, r = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if ( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
/* test if any of the mask bits are invalid */
|
|
|
|
if ( mask >= (1<<tv->num_cols) )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
for ( i = 0; i < tv->num_cols; i++ )
|
|
|
|
{
|
2007-04-23 09:27:37 +02:00
|
|
|
BOOL persistent;
|
|
|
|
|
2006-10-26 10:09:47 +02:00
|
|
|
/* only update the fields specified in the mask */
|
|
|
|
if ( !(mask&(1<<i)) )
|
|
|
|
continue;
|
|
|
|
|
2007-04-23 09:27:37 +02:00
|
|
|
/* if row >= tv->table->row_count then it is a non-persistent row */
|
|
|
|
persistent = tv->table->persistent && (row < tv->table->row_count);
|
2006-10-26 10:09:47 +02:00
|
|
|
/* FIXME: should we allow updating keys? */
|
|
|
|
|
|
|
|
val = 0;
|
|
|
|
if ( !MSI_RecordIsNull( rec, i + 1 ) )
|
|
|
|
{
|
|
|
|
if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
|
|
|
|
{
|
|
|
|
val = 1; /* refers to the first key column */
|
|
|
|
}
|
|
|
|
else if ( tv->columns[i].type & MSITYPE_STRING )
|
|
|
|
{
|
|
|
|
LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
|
2007-11-05 10:48:15 +01:00
|
|
|
UINT ival, x;
|
|
|
|
|
|
|
|
r = msi_string2idW(tv->db->strings, sval, &ival);
|
|
|
|
if (r == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
TABLE_fetch_int(&tv->view, row, i + 1, &x);
|
|
|
|
if (ival == x)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:27:37 +02:00
|
|
|
val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
|
|
|
|
persistent ? StringPersistent : StringNonPersistent );
|
2006-10-26 10:09:47 +02:00
|
|
|
}
|
|
|
|
else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
|
|
|
|
{
|
|
|
|
val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
|
|
|
|
if ( val & 0xffff0000 )
|
|
|
|
{
|
|
|
|
ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INT ival = MSI_RecordGetInteger( rec, i + 1 );
|
|
|
|
val = ival ^ 0x80000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = TABLE_set_int( tv, row, i+1, val );
|
|
|
|
if ( r != ERROR_SUCCESS )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE **p, *row;
|
2004-03-19 02:16:36 +01:00
|
|
|
UINT sz;
|
2007-06-08 01:41:15 +02:00
|
|
|
BYTE ***data_ptr;
|
2007-04-23 09:24:41 +02:00
|
|
|
UINT *row_count;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
if( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2005-09-20 13:59:14 +02:00
|
|
|
row = msi_alloc_zero( tv->row_size );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !row )
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
if( temporary )
|
|
|
|
{
|
|
|
|
row_count = &tv->table->nonpersistent_row_count;
|
|
|
|
data_ptr = &tv->table->nonpersistent_data;
|
|
|
|
*num = tv->table->row_count + tv->table->nonpersistent_row_count;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
row_count = &tv->table->row_count;
|
|
|
|
data_ptr = &tv->table->data;
|
|
|
|
*num = tv->table->row_count;
|
|
|
|
}
|
|
|
|
|
2007-06-08 01:41:15 +02:00
|
|
|
sz = (*row_count + 1) * sizeof (BYTE*);
|
2007-04-23 09:24:41 +02:00
|
|
|
if( *data_ptr )
|
|
|
|
p = msi_realloc( *data_ptr, sz );
|
2004-03-19 02:16:36 +01:00
|
|
|
else
|
2005-09-20 13:59:14 +02:00
|
|
|
p = msi_alloc( sz );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( !p )
|
2004-12-10 16:24:52 +01:00
|
|
|
{
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( row );
|
2004-03-19 02:16:36 +01:00
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
2004-12-10 16:24:52 +01:00
|
|
|
}
|
2004-03-19 02:16:36 +01:00
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
*data_ptr = p;
|
|
|
|
(*data_ptr)[*row_count] = row;
|
|
|
|
(*row_count)++;
|
2004-03-19 02:16:36 +01:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-10 00:25:34 +02:00
|
|
|
static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
|
2004-07-10 00:25:34 +02:00
|
|
|
TRACE("%p %p\n", tv, record);
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
TRACE("There are %d columns\n", tv->num_cols );
|
2005-08-24 13:10:23 +02:00
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT TABLE_close( struct tagMSIVIEW *view )
|
|
|
|
{
|
|
|
|
TRACE("%p\n", view );
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
|
|
|
|
TRACE("%p %p %p\n", view, rows, cols );
|
|
|
|
|
|
|
|
if( cols )
|
|
|
|
*cols = tv->num_cols;
|
|
|
|
if( rows )
|
|
|
|
{
|
|
|
|
if( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
2007-04-23 09:24:41 +02:00
|
|
|
*rows = tv->table->row_count + tv->table->nonpersistent_row_count;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
|
|
|
|
UINT n, LPWSTR *name, UINT *type )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
|
|
|
|
TRACE("%p %d %p %p\n", tv, n, name, type );
|
|
|
|
|
|
|
|
if( ( n == 0 ) || ( n > tv->num_cols ) )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if( name )
|
|
|
|
{
|
|
|
|
*name = strdupW( tv->columns[n-1].colname );
|
|
|
|
if( !*name )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
if( type )
|
|
|
|
*type = tv->columns[n-1].type;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-10-29 13:07:29 +02:00
|
|
|
static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
|
2005-08-24 13:10:23 +02:00
|
|
|
|
|
|
|
static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
|
|
|
|
{
|
2006-08-31 10:06:20 +02:00
|
|
|
UINT r, row, i;
|
2005-08-24 13:10:23 +02:00
|
|
|
|
2006-08-31 10:06:20 +02:00
|
|
|
/* check there's no null values where they're not allowed */
|
|
|
|
for( i = 0; i < tv->num_cols; i++ )
|
|
|
|
{
|
|
|
|
if ( tv->columns[i].type & MSITYPE_NULLABLE )
|
|
|
|
continue;
|
|
|
|
|
2006-10-17 10:48:53 +02:00
|
|
|
if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
|
|
|
|
TRACE("skipping binary column\n");
|
|
|
|
else if ( tv->columns[i].type & MSITYPE_STRING )
|
2006-08-31 10:06:20 +02:00
|
|
|
{
|
|
|
|
LPCWSTR str;
|
|
|
|
|
|
|
|
str = MSI_RecordGetString( rec, i+1 );
|
|
|
|
if (str == NULL || str[0] == 0)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UINT n;
|
|
|
|
|
|
|
|
n = MSI_RecordGetInteger( rec, i+1 );
|
|
|
|
if (n == MSI_NULL_INTEGER)
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check there's no duplicate keys */
|
2005-10-29 13:07:29 +02:00
|
|
|
r = msi_table_find_row( tv, rec, &row );
|
2006-08-31 10:06:20 +02:00
|
|
|
if (r == ERROR_SUCCESS)
|
2007-10-18 05:53:54 +02:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
2006-08-31 10:06:20 +02:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
2005-08-24 13:10:23 +02:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
UINT r, row = -1;
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
|
2005-10-27 14:25:04 +02:00
|
|
|
|
2006-08-31 10:06:20 +02:00
|
|
|
/* check that the key is unique - can we find a matching row? */
|
|
|
|
r = table_validate_new( tv, rec );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
r = table_create_new_row( view, &row, temporary );
|
2005-10-27 14:25:04 +02:00
|
|
|
TRACE("insert_row returned %08x\n", r);
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
return r;
|
|
|
|
|
2006-10-26 10:09:47 +02:00
|
|
|
return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
|
2007-06-18 22:49:31 +02:00
|
|
|
static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
2007-11-05 10:35:13 +01:00
|
|
|
UINT r, num_rows, num_cols, i;
|
2007-12-22 04:04:18 +01:00
|
|
|
BYTE **data;
|
2007-06-18 22:49:31 +02:00
|
|
|
|
|
|
|
TRACE("%p %d\n", tv, row);
|
|
|
|
|
|
|
|
if ( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
r = TABLE_get_dimensions( view, &num_rows, &num_cols );
|
|
|
|
if ( r != ERROR_SUCCESS )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if ( row >= num_rows )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2007-12-22 04:04:18 +01:00
|
|
|
if ( row < tv->table->row_count )
|
|
|
|
{
|
|
|
|
num_rows = tv->table->row_count;
|
|
|
|
tv->table->row_count--;
|
|
|
|
data = tv->table->data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
num_rows = tv->table->nonpersistent_row_count;
|
|
|
|
row -= tv->table->row_count;
|
|
|
|
tv->table->nonpersistent_row_count--;
|
|
|
|
data = tv->table->nonpersistent_data;
|
|
|
|
}
|
2007-06-18 22:49:31 +02:00
|
|
|
|
2008-01-09 09:36:25 +01:00
|
|
|
/* reset the hash tables */
|
|
|
|
for (i = 0; i < tv->num_cols; i++)
|
|
|
|
{
|
|
|
|
msi_free( tv->columns[i].hash_table );
|
|
|
|
tv->columns[i].hash_table = NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-18 22:49:31 +02:00
|
|
|
if ( row == num_rows - 1 )
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
2007-11-05 10:35:13 +01:00
|
|
|
for (i = row + 1; i < num_rows; i++)
|
2007-12-22 04:04:18 +01:00
|
|
|
memcpy(data[i - 1], data[i], tv->row_size);
|
2007-11-05 10:35:13 +01:00
|
|
|
|
2007-06-18 22:49:31 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-07-27 02:40:38 +02:00
|
|
|
static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
|
2007-07-24 04:20:51 +02:00
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
|
2007-07-27 02:40:38 +02:00
|
|
|
UINT r, new_row;
|
2007-07-24 04:20:51 +02:00
|
|
|
|
|
|
|
/* FIXME: MsiViewFetch should set rec index 0 to some ID that
|
|
|
|
* sets the fetched record apart from other records
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!tv->table)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2007-07-27 02:40:38 +02:00
|
|
|
r = msi_table_find_row(tv, rec, &new_row);
|
2007-07-24 04:20:51 +02:00
|
|
|
if (r != ERROR_SUCCESS)
|
2007-07-27 02:40:38 +02:00
|
|
|
{
|
|
|
|
ERR("can't find row to modify\n");
|
2007-10-18 05:54:39 +02:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
2007-07-27 02:40:38 +02:00
|
|
|
}
|
2007-07-24 04:20:51 +02:00
|
|
|
|
|
|
|
/* the row cannot be changed */
|
2007-07-27 02:40:38 +02:00
|
|
|
if (row != new_row + 1)
|
2007-07-24 04:20:51 +02:00
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2007-07-27 02:40:38 +02:00
|
|
|
return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
|
2007-07-24 04:20:51 +02:00
|
|
|
}
|
|
|
|
|
2007-11-05 10:35:13 +01:00
|
|
|
static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
|
|
|
|
UINT row, r;
|
|
|
|
|
|
|
|
r = msi_table_find_row(tv, rec, &row);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return TABLE_delete_row(view, row);
|
|
|
|
}
|
|
|
|
|
2008-02-14 06:36:44 +01:00
|
|
|
static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
|
|
|
|
{
|
|
|
|
MSIRECORD *curr;
|
|
|
|
UINT r, i, count;
|
|
|
|
|
|
|
|
r = TABLE_get_row(view, row - 1, &curr);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
count = MSI_RecordGetFieldCount(rec);
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
MSI_RecordCopyField(curr, i + 1, rec, i + 1);
|
|
|
|
|
|
|
|
msiobj_release(&curr->hdr);
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-02-08 14:44:25 +01:00
|
|
|
static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
|
2007-07-27 02:40:38 +02:00
|
|
|
MSIRECORD *rec, UINT row)
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
2005-08-24 13:10:23 +02:00
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
UINT r;
|
|
|
|
|
|
|
|
TRACE("%p %d %p\n", view, eModifyMode, rec );
|
|
|
|
|
|
|
|
switch (eModifyMode)
|
|
|
|
{
|
2007-11-05 10:35:13 +01:00
|
|
|
case MSIMODIFY_DELETE:
|
|
|
|
r = modify_delete_row( view, rec );
|
|
|
|
break;
|
2005-08-24 13:10:23 +02:00
|
|
|
case MSIMODIFY_VALIDATE_NEW:
|
|
|
|
r = table_validate_new( tv, rec );
|
|
|
|
break;
|
|
|
|
|
2007-12-22 04:04:18 +01:00
|
|
|
case MSIMODIFY_INSERT:
|
|
|
|
r = table_validate_new( tv, rec );
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
break;
|
|
|
|
r = TABLE_insert_row( view, rec, FALSE );
|
|
|
|
break;
|
|
|
|
|
2005-08-24 13:10:23 +02:00
|
|
|
case MSIMODIFY_INSERT_TEMPORARY:
|
|
|
|
r = table_validate_new( tv, rec );
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
break;
|
2007-04-23 09:24:41 +02:00
|
|
|
r = TABLE_insert_row( view, rec, TRUE );
|
2005-08-24 13:10:23 +02:00
|
|
|
break;
|
|
|
|
|
2008-02-14 06:36:44 +01:00
|
|
|
case MSIMODIFY_REFRESH:
|
|
|
|
r = msi_refresh_record( view, rec, row );
|
|
|
|
break;
|
|
|
|
|
2007-07-24 04:20:51 +02:00
|
|
|
case MSIMODIFY_UPDATE:
|
2007-07-27 02:40:38 +02:00
|
|
|
r = msi_table_update( view, rec, row );
|
2007-07-24 04:20:51 +02:00
|
|
|
break;
|
|
|
|
|
2005-08-24 13:10:23 +02:00
|
|
|
case MSIMODIFY_ASSIGN:
|
|
|
|
case MSIMODIFY_REPLACE:
|
|
|
|
case MSIMODIFY_MERGE:
|
|
|
|
case MSIMODIFY_VALIDATE:
|
|
|
|
case MSIMODIFY_VALIDATE_FIELD:
|
|
|
|
case MSIMODIFY_VALIDATE_DELETE:
|
|
|
|
FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
|
|
|
|
r = ERROR_CALL_NOT_IMPLEMENTED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
r = ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static UINT TABLE_delete( struct tagMSIVIEW *view )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
|
|
|
|
TRACE("%p\n", view );
|
|
|
|
|
|
|
|
tv->table = NULL;
|
|
|
|
tv->columns = NULL;
|
|
|
|
|
2007-12-18 23:49:37 +01:00
|
|
|
if (tv->order)
|
|
|
|
{
|
|
|
|
msi_free( tv->order->reorder );
|
|
|
|
msi_free( tv->order );
|
|
|
|
tv->order = NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( tv );
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-03-18 17:12:15 +01:00
|
|
|
static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
|
|
|
|
UINT val, UINT *row, MSIITERHANDLE *handle )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
const MSICOLUMNHASHENTRY *entry;
|
|
|
|
|
|
|
|
TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
|
|
|
|
|
|
|
|
if( !tv->table )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if( (col==0) || (col > tv->num_cols) )
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if( !tv->columns[col-1].hash_table )
|
|
|
|
{
|
|
|
|
UINT i;
|
2007-04-23 09:24:41 +02:00
|
|
|
UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
|
2006-03-18 17:12:15 +01:00
|
|
|
MSICOLUMNHASHENTRY **hash_table;
|
|
|
|
MSICOLUMNHASHENTRY *new_entry;
|
|
|
|
|
|
|
|
if( tv->columns[col-1].offset >= tv->row_size )
|
|
|
|
{
|
|
|
|
ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
|
|
|
|
ERR("%p %p\n", tv, tv->columns );
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate contiguous memory for the table and its entries so we
|
|
|
|
* don't have to do an expensive cleanup */
|
|
|
|
hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
|
|
|
|
num_rows * sizeof(MSICOLUMNHASHENTRY));
|
|
|
|
if (!hash_table)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
|
|
|
|
tv->columns[col-1].hash_table = hash_table;
|
|
|
|
|
|
|
|
new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
|
|
|
|
|
|
|
|
for (i = 0; i < num_rows; i++, new_entry++)
|
|
|
|
{
|
2007-06-08 01:37:48 +02:00
|
|
|
UINT row_value;
|
|
|
|
|
|
|
|
if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
|
2006-03-18 17:12:15 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
new_entry->next = NULL;
|
|
|
|
new_entry->value = row_value;
|
|
|
|
new_entry->row = i;
|
|
|
|
if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
|
|
|
|
{
|
|
|
|
MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
|
|
|
|
while (prev_entry->next)
|
|
|
|
prev_entry = prev_entry->next;
|
|
|
|
prev_entry->next = new_entry;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !*handle )
|
|
|
|
entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
|
|
|
|
else
|
2006-11-06 13:02:47 +01:00
|
|
|
entry = (*handle)->next;
|
2006-03-18 17:12:15 +01:00
|
|
|
|
|
|
|
while (entry && entry->value != val)
|
|
|
|
entry = entry->next;
|
|
|
|
|
2006-11-06 13:02:47 +01:00
|
|
|
*handle = entry;
|
2006-03-18 17:12:15 +01:00
|
|
|
if (!entry)
|
|
|
|
return ERROR_NO_MORE_ITEMS;
|
|
|
|
|
|
|
|
*row = entry->row;
|
2007-12-18 02:35:31 +01:00
|
|
|
|
2006-03-18 17:12:15 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-07-19 03:21:00 +02:00
|
|
|
static UINT TABLE_add_ref(struct tagMSIVIEW *view)
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
2008-01-03 22:56:39 +01:00
|
|
|
UINT i;
|
2007-07-19 03:21:00 +02:00
|
|
|
|
|
|
|
TRACE("%p %d\n", view, tv->table->ref_count);
|
|
|
|
|
2007-07-21 03:41:16 +02:00
|
|
|
for (i = 0; i < tv->table->col_count; i++)
|
|
|
|
{
|
|
|
|
if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
|
|
|
|
InterlockedIncrement(&tv->table->colinfo[i].ref_count);
|
|
|
|
}
|
|
|
|
|
2007-07-19 03:21:00 +02:00
|
|
|
return InterlockedIncrement(&tv->table->ref_count);
|
|
|
|
}
|
|
|
|
|
2007-07-21 03:41:16 +02:00
|
|
|
static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
2007-07-24 04:19:15 +02:00
|
|
|
MSIRECORD *rec = NULL;
|
2007-07-21 03:41:16 +02:00
|
|
|
MSIVIEW *columns = NULL;
|
|
|
|
UINT row, r;
|
|
|
|
|
|
|
|
rec = MSI_CreateRecord(2);
|
|
|
|
if (!rec)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
MSI_RecordSetStringW(rec, 1, table);
|
|
|
|
MSI_RecordSetInteger(rec, 2, number);
|
|
|
|
|
|
|
|
r = TABLE_CreateView(tv->db, szColumns, &columns);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
r = TABLE_delete_row(columns, row);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
msi_update_table_columns(tv->db, table);
|
|
|
|
|
|
|
|
done:
|
|
|
|
msiobj_release(&rec->hdr);
|
2007-07-24 04:19:15 +02:00
|
|
|
if (columns) columns->ops->delete(columns);
|
2007-07-21 03:41:16 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-07-19 03:21:00 +02:00
|
|
|
static UINT TABLE_release(struct tagMSIVIEW *view)
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
|
|
|
INT ref = tv->table->ref_count;
|
2008-01-03 22:56:39 +01:00
|
|
|
UINT i, r;
|
2007-07-19 03:21:00 +02:00
|
|
|
|
|
|
|
TRACE("%p %d\n", view, ref);
|
|
|
|
|
2007-07-21 03:41:16 +02:00
|
|
|
for (i = 0; i < tv->table->col_count; i++)
|
|
|
|
{
|
|
|
|
if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
|
|
|
|
{
|
|
|
|
ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
|
|
|
|
if (ref == 0)
|
|
|
|
{
|
|
|
|
r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
|
|
|
|
tv->table->colinfo[i].number);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-19 03:21:00 +02:00
|
|
|
ref = InterlockedDecrement(&tv->table->ref_count);
|
|
|
|
if (ref == 0)
|
|
|
|
{
|
|
|
|
if (!tv->table->row_count)
|
|
|
|
{
|
|
|
|
list_remove(&tv->table->entry);
|
|
|
|
free_table(tv->table);
|
|
|
|
TABLE_delete(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2007-07-21 03:41:16 +02:00
|
|
|
static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
|
|
|
|
LPCWSTR column, UINT type, BOOL hold)
|
2007-07-20 23:01:13 +02:00
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
|
2007-07-21 03:41:16 +02:00
|
|
|
MSITABLE *msitable;
|
2007-07-20 23:01:13 +02:00
|
|
|
MSIRECORD *rec;
|
2007-07-21 03:41:16 +02:00
|
|
|
UINT r, i;
|
2007-07-20 23:01:13 +02:00
|
|
|
|
|
|
|
rec = MSI_CreateRecord(4);
|
|
|
|
if (!rec)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
MSI_RecordSetStringW(rec, 1, table);
|
|
|
|
MSI_RecordSetInteger(rec, 2, number);
|
|
|
|
MSI_RecordSetStringW(rec, 3, column);
|
|
|
|
MSI_RecordSetInteger(rec, 4, type);
|
|
|
|
|
|
|
|
r = TABLE_insert_row(&tv->view, rec, FALSE);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
msi_update_table_columns(tv->db, table);
|
|
|
|
|
2007-07-21 03:41:16 +02:00
|
|
|
if (!hold)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
msitable = find_cached_table(tv->db, table);
|
|
|
|
for (i = 0; i < msitable->col_count; i++)
|
|
|
|
{
|
|
|
|
if (!lstrcmpW(msitable->colinfo[i].colname, column))
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&msitable->colinfo[i].ref_count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-20 23:01:13 +02:00
|
|
|
done:
|
|
|
|
msiobj_release(&rec->hdr);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-12-18 02:35:31 +01:00
|
|
|
static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
|
|
|
|
{
|
|
|
|
UINT n, r, count;
|
|
|
|
|
|
|
|
r = TABLE_get_dimensions(view, NULL, &count);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (order->num_cols >= count)
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
|
|
|
r = VIEW_find_column(view, name, &n);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
order->cols[order->num_cols] = n;
|
|
|
|
TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
|
|
|
|
|
|
|
|
order->num_cols++;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
|
|
|
|
UINT a, UINT b, UINT *swap)
|
|
|
|
{
|
|
|
|
UINT r, i, a_val = 0, b_val = 0;
|
|
|
|
|
|
|
|
*swap = 0;
|
|
|
|
for (i = 0; i < order->num_cols; i++)
|
|
|
|
{
|
|
|
|
r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (a_val != b_val)
|
|
|
|
{
|
|
|
|
if (a_val > b_val)
|
|
|
|
*swap = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
|
|
|
|
UINT left, UINT right)
|
|
|
|
{
|
|
|
|
UINT r, i, j, temp;
|
|
|
|
UINT swap = 0, center = (left + right) / 2;
|
|
|
|
UINT *array = order->reorder;
|
|
|
|
|
|
|
|
if (left == right)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
/* sort the left half */
|
|
|
|
r = order_mergesort(view, order, left, center);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* sort the right half */
|
|
|
|
r = order_mergesort(view, order, center + 1, right);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
|
|
|
|
{
|
|
|
|
r = order_compare(view, order, array[i], array[j], &swap);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (swap)
|
|
|
|
{
|
|
|
|
temp = array[j];
|
|
|
|
memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
|
|
|
|
array[i] = temp;
|
|
|
|
j++;
|
|
|
|
center++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
|
|
|
|
{
|
|
|
|
UINT i, swap, r;
|
|
|
|
|
|
|
|
for (i = 1; i < num_rows; i++)
|
|
|
|
{
|
|
|
|
r = order_compare(view, order, order->reorder[i - 1],
|
|
|
|
order->reorder[i], &swap);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!swap)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ERR("Bad order! %d\n", i);
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
|
|
|
|
MSIORDERINFO *order;
|
|
|
|
column_info *ptr;
|
|
|
|
UINT r, i;
|
|
|
|
UINT rows, cols;
|
|
|
|
|
|
|
|
TRACE("sorting table %s\n", debugstr_w(tv->name));
|
|
|
|
|
|
|
|
r = TABLE_get_dimensions(view, &rows, &cols);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
2008-04-02 10:47:24 +02:00
|
|
|
if (rows == 0)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
2007-12-18 02:35:31 +01:00
|
|
|
order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
|
|
|
|
if (!order)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for (ptr = columns; ptr; ptr = ptr->next)
|
|
|
|
order_add_column(view, order, ptr->column);
|
|
|
|
|
|
|
|
order->reorder = msi_alloc(rows * sizeof(UINT));
|
|
|
|
if (!order->reorder)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for (i = 0; i < rows; i++)
|
|
|
|
order->reorder[i] = i;
|
|
|
|
|
|
|
|
r = order_mergesort(view, order, 0, rows - 1);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = order_verify(view, order, rows);
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
tv->order = order;
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-06-10 12:02:39 +02:00
|
|
|
static const MSIVIEWOPS table_ops =
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
TABLE_fetch_int,
|
2004-07-06 20:56:12 +02:00
|
|
|
TABLE_fetch_stream,
|
2007-07-27 02:40:38 +02:00
|
|
|
TABLE_get_row,
|
2006-10-26 10:09:47 +02:00
|
|
|
TABLE_set_row,
|
2004-03-19 02:16:36 +01:00
|
|
|
TABLE_insert_row,
|
2007-06-18 22:49:31 +02:00
|
|
|
TABLE_delete_row,
|
2003-08-13 03:27:48 +02:00
|
|
|
TABLE_execute,
|
|
|
|
TABLE_close,
|
|
|
|
TABLE_get_dimensions,
|
|
|
|
TABLE_get_column_info,
|
|
|
|
TABLE_modify,
|
2006-03-18 17:12:15 +01:00
|
|
|
TABLE_delete,
|
2007-07-19 03:21:00 +02:00
|
|
|
TABLE_find_matching_rows,
|
|
|
|
TABLE_add_ref,
|
|
|
|
TABLE_release,
|
2007-07-20 23:01:13 +02:00
|
|
|
TABLE_add_column,
|
2007-07-21 03:41:16 +02:00
|
|
|
TABLE_remove_column,
|
2007-12-18 02:35:31 +01:00
|
|
|
TABLE_sort,
|
2003-08-13 03:27:48 +02:00
|
|
|
};
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
|
2003-08-13 03:27:48 +02:00
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv ;
|
2007-04-23 09:20:21 +02:00
|
|
|
UINT r, sz;
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2007-04-25 12:22:01 +02:00
|
|
|
static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
|
2008-07-08 06:59:07 +02:00
|
|
|
static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
|
2007-04-25 12:22:01 +02:00
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
TRACE("%p %s %p\n", db, debugstr_w(name), view );
|
|
|
|
|
2007-04-25 12:22:01 +02:00
|
|
|
if ( !lstrcmpW( name, Streams ) )
|
|
|
|
return STREAMS_CreateView( db, view );
|
2008-07-08 06:59:07 +02:00
|
|
|
else if ( !lstrcmpW( name, Storages ) )
|
|
|
|
return STORAGES_CreateView( db, view );
|
2007-04-25 12:22:01 +02:00
|
|
|
|
2003-08-13 03:27:48 +02:00
|
|
|
sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
|
2005-09-20 13:59:14 +02:00
|
|
|
tv = msi_alloc_zero( sz );
|
2003-08-13 03:27:48 +02:00
|
|
|
if( !tv )
|
|
|
|
return ERROR_FUNCTION_FAILED;
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
r = get_table( db, name, &tv->table );
|
2003-08-13 03:27:48 +02:00
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
2005-09-20 13:59:14 +02:00
|
|
|
msi_free( tv );
|
2007-04-23 09:20:21 +02:00
|
|
|
WARN("table not found\n");
|
|
|
|
return r;
|
2003-08-13 03:27:48 +02:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:20:21 +02:00
|
|
|
TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
/* fill the structure */
|
|
|
|
tv->view.ops = &table_ops;
|
|
|
|
tv->db = db;
|
2007-04-23 09:20:21 +02:00
|
|
|
tv->columns = tv->table->colinfo;
|
|
|
|
tv->num_cols = tv->table->col_count;
|
|
|
|
tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
|
2003-08-13 03:27:48 +02:00
|
|
|
|
2005-09-22 12:34:07 +02:00
|
|
|
TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
|
2003-08-13 03:27:48 +02:00
|
|
|
|
|
|
|
*view = (MSIVIEW*) tv;
|
|
|
|
lstrcpyW( tv->name, name );
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2004-03-18 05:04:08 +01:00
|
|
|
|
|
|
|
UINT MSI_CommitTables( MSIDATABASE *db )
|
|
|
|
{
|
|
|
|
UINT r;
|
|
|
|
MSITABLE *table = NULL;
|
|
|
|
|
2004-03-19 02:16:36 +01:00
|
|
|
TRACE("%p\n",db);
|
|
|
|
|
2007-04-23 09:17:40 +02:00
|
|
|
r = msi_save_string_table( db->strings, db->storage );
|
2004-03-19 02:16:36 +01:00
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("failed to save string table r=%08x\n",r);
|
2004-03-19 02:16:36 +01:00
|
|
|
return r;
|
|
|
|
}
|
2004-03-18 05:04:08 +01:00
|
|
|
|
2005-09-14 12:07:26 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
|
2004-03-18 05:04:08 +01:00
|
|
|
{
|
|
|
|
r = save_table( db, table );
|
|
|
|
if( r != ERROR_SUCCESS )
|
2004-03-19 02:16:36 +01:00
|
|
|
{
|
2005-05-20 21:16:50 +02:00
|
|
|
WARN("failed to save table %s (r=%08x)\n",
|
2004-03-19 02:16:36 +01:00
|
|
|
debugstr_w(table->name), r);
|
2004-03-18 05:04:08 +01:00
|
|
|
return r;
|
2004-03-19 02:16:36 +01:00
|
|
|
}
|
2004-03-18 05:04:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* force everything to reload next time */
|
|
|
|
free_cached_tables( db );
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2005-10-26 15:56:33 +02:00
|
|
|
|
2006-09-13 11:46:13 +02:00
|
|
|
MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
|
|
|
|
{
|
2007-04-24 12:00:44 +02:00
|
|
|
MSITABLE *t;
|
|
|
|
UINT r;
|
|
|
|
|
|
|
|
TRACE("%p %s\n", db, debugstr_w(table));
|
|
|
|
|
2006-09-13 11:46:13 +02:00
|
|
|
if (!table)
|
|
|
|
return MSICONDITION_ERROR;
|
|
|
|
|
2007-04-24 12:00:44 +02:00
|
|
|
r = get_table( db, table, &t );
|
|
|
|
if (r != ERROR_SUCCESS)
|
2007-04-21 09:33:10 +02:00
|
|
|
return MSICONDITION_NONE;
|
|
|
|
|
2007-04-24 12:00:44 +02:00
|
|
|
if (t->persistent)
|
|
|
|
return MSICONDITION_TRUE;
|
|
|
|
else
|
|
|
|
return MSICONDITION_FALSE;
|
2006-09-13 11:46:13 +02:00
|
|
|
}
|
|
|
|
|
2007-06-26 23:25:28 +02:00
|
|
|
static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
|
|
|
|
{
|
|
|
|
UINT ret = 0, i;
|
|
|
|
|
|
|
|
for (i = 0; i < bytes; i++)
|
|
|
|
ret += (data[col + i] << i * 8);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
|
2007-06-26 23:25:28 +02:00
|
|
|
const BYTE *rawdata, UINT bytes_per_strref )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
|
|
|
UINT i, val, ofs = 0;
|
2007-06-26 23:25:28 +02:00
|
|
|
USHORT mask;
|
2005-10-27 14:25:04 +02:00
|
|
|
MSICOLUMNINFO *columns = tv->columns;
|
|
|
|
MSIRECORD *rec;
|
|
|
|
|
2007-06-26 23:25:28 +02:00
|
|
|
mask = rawdata[0] | (rawdata[1] << 8);
|
|
|
|
rawdata += 2;
|
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
rec = MSI_CreateRecord( tv->num_cols );
|
|
|
|
if( !rec )
|
|
|
|
return rec;
|
|
|
|
|
2006-10-15 17:19:54 +02:00
|
|
|
TRACE("row ->\n");
|
2005-10-27 14:25:04 +02:00
|
|
|
for( i=0; i<tv->num_cols; i++ )
|
|
|
|
{
|
|
|
|
if ( (mask&1) && (i>=(mask>>8)) )
|
|
|
|
break;
|
|
|
|
/* all keys must be present */
|
|
|
|
if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
|
|
|
|
continue;
|
|
|
|
|
2007-06-26 23:25:28 +02:00
|
|
|
if( (columns[i].type & MSITYPE_STRING) &&
|
|
|
|
! MSITYPE_IS_BINARY(tv->columns[i].type) )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
2007-06-26 23:25:28 +02:00
|
|
|
LPCWSTR sval;
|
|
|
|
|
|
|
|
val = read_raw_int(rawdata, ofs, bytes_per_strref);
|
|
|
|
sval = msi_string_lookup_id( st, val );
|
|
|
|
MSI_RecordSetStringW( rec, i+1, sval );
|
|
|
|
TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
|
|
|
|
ofs += bytes_per_strref;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UINT n = bytes_per_column( &columns[i] );
|
|
|
|
switch( n )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
2007-06-26 23:25:28 +02:00
|
|
|
case 2:
|
|
|
|
val = read_raw_int(rawdata, ofs, n);
|
2006-10-06 10:35:13 +02:00
|
|
|
if (val)
|
|
|
|
MSI_RecordSetInteger( rec, i+1, val^0x8000 );
|
2006-10-06 10:37:06 +02:00
|
|
|
TRACE(" field %d [0x%04x]\n", i+1, val );
|
2007-06-26 23:25:28 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
val = read_raw_int(rawdata, ofs, n);
|
|
|
|
if (val)
|
|
|
|
MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
|
|
|
|
TRACE(" field %d [0x%08x]\n", i+1, val );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR("oops - unknown column width %d\n", n);
|
|
|
|
break;
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
2007-06-26 23:25:28 +02:00
|
|
|
ofs += n;
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_record( MSIRECORD *rec )
|
|
|
|
{
|
|
|
|
UINT i, n;
|
|
|
|
|
|
|
|
n = MSI_RecordGetFieldCount( rec );
|
|
|
|
for( i=1; i<=n; i++ )
|
|
|
|
{
|
|
|
|
LPCWSTR sval = MSI_RecordGetString( rec, i );
|
|
|
|
|
|
|
|
if( MSI_RecordIsNull( rec, i ) )
|
2006-04-07 13:40:12 +02:00
|
|
|
TRACE("row -> []\n");
|
2005-10-27 14:25:04 +02:00
|
|
|
else if( (sval = MSI_RecordGetString( rec, i )) )
|
2006-04-07 13:40:12 +02:00
|
|
|
TRACE("row -> [%s]\n", debugstr_w(sval));
|
2005-10-27 14:25:04 +02:00
|
|
|
else
|
2006-04-07 13:40:12 +02:00
|
|
|
TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
|
|
|
LPCWSTR sval;
|
|
|
|
UINT i;
|
|
|
|
|
|
|
|
for( i=0; i<(rawsize/2); i++ )
|
|
|
|
{
|
|
|
|
sval = msi_string_lookup_id( st, rawdata[i] );
|
|
|
|
MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
|
|
|
LPCWSTR str;
|
|
|
|
UINT i, r, *data;
|
|
|
|
|
|
|
|
data = msi_alloc( tv->num_cols *sizeof (UINT) );
|
|
|
|
for( i=0; i<tv->num_cols; i++ )
|
|
|
|
{
|
|
|
|
data[i] = 0;
|
|
|
|
|
|
|
|
if ( ~tv->columns[i].type & MSITYPE_KEY )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* turn the transform column value into a row value */
|
|
|
|
if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
|
|
|
|
! MSITYPE_IS_BINARY(tv->columns[i].type) )
|
|
|
|
{
|
|
|
|
str = MSI_RecordGetString( rec, i+1 );
|
|
|
|
r = msi_string2idW( tv->db->strings, str, &data[i] );
|
|
|
|
|
|
|
|
/* if there's no matching string in the string table,
|
|
|
|
these keys can't match any record, so fail now. */
|
|
|
|
if( ERROR_SUCCESS != r )
|
|
|
|
{
|
|
|
|
msi_free( data );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2006-10-12 14:28:26 +02:00
|
|
|
{
|
2005-10-27 14:25:04 +02:00
|
|
|
data[i] = MSI_RecordGetInteger( rec, i+1 );
|
2006-10-12 14:28:26 +02:00
|
|
|
if ((tv->columns[i].type&0xff) == 2)
|
|
|
|
data[i] += 0x8000;
|
|
|
|
else
|
|
|
|
data[i] += 0x80000000;
|
|
|
|
}
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2007-06-13 23:36:31 +02:00
|
|
|
static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
|
|
|
UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
|
|
|
|
|
|
|
|
for( i=0; i<tv->num_cols; i++ )
|
|
|
|
{
|
|
|
|
if ( ~tv->columns[i].type & MSITYPE_KEY )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* turn the transform column value into a row value */
|
|
|
|
r = TABLE_fetch_int( &tv->view, row, i+1, &x );
|
|
|
|
if ( r != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
ERR("TABLE_fetch_int shouldn't fail here\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if this key matches, move to the next column */
|
|
|
|
if ( x != data[i] )
|
|
|
|
{
|
|
|
|
ret = ERROR_FUNCTION_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
|
|
|
|
{
|
|
|
|
UINT i, r = ERROR_FUNCTION_FAILED, *data;
|
|
|
|
|
|
|
|
data = msi_record_to_row( tv, rec );
|
|
|
|
if( !data )
|
|
|
|
return r;
|
2007-04-23 09:24:41 +02:00
|
|
|
for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
|
|
|
r = msi_row_matches( tv, i, data );
|
|
|
|
if( r == ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
*row = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msi_free( data );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct list entry;
|
|
|
|
LPWSTR name;
|
|
|
|
} TRANSFORMDATA;
|
|
|
|
|
2005-10-26 15:56:33 +02:00
|
|
|
static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
|
2007-07-10 04:34:41 +02:00
|
|
|
string_table *st, TRANSFORMDATA *transform,
|
2007-06-26 23:25:28 +02:00
|
|
|
UINT bytes_per_strref )
|
2005-10-26 15:56:33 +02:00
|
|
|
{
|
2005-10-27 14:25:04 +02:00
|
|
|
UINT rawsize = 0;
|
2007-06-26 23:25:28 +02:00
|
|
|
BYTE *rawdata = NULL;
|
2005-10-27 14:25:04 +02:00
|
|
|
MSITABLEVIEW *tv = NULL;
|
|
|
|
UINT r, n, sz, i, mask;
|
|
|
|
MSIRECORD *rec = NULL;
|
2006-10-06 10:36:37 +02:00
|
|
|
UINT colcol = 0;
|
2006-10-19 18:53:32 +02:00
|
|
|
WCHAR coltable[32];
|
2007-07-10 20:26:44 +02:00
|
|
|
LPWSTR name;
|
|
|
|
|
|
|
|
if (!transform)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
name = transform->name;
|
2005-10-27 14:25:04 +02:00
|
|
|
|
2006-10-19 18:53:32 +02:00
|
|
|
coltable[0] = 0;
|
2005-10-27 14:25:04 +02:00
|
|
|
TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
|
|
|
|
|
2006-10-10 12:19:37 +02:00
|
|
|
/* read the transform data */
|
2007-07-16 23:45:29 +02:00
|
|
|
read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
|
2006-10-06 10:35:44 +02:00
|
|
|
if ( !rawdata )
|
|
|
|
{
|
|
|
|
TRACE("table %s empty\n", debugstr_w(name) );
|
|
|
|
return ERROR_INVALID_TABLE;
|
|
|
|
}
|
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
/* create a table view */
|
|
|
|
r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
r = tv->view.ops->execute( &tv->view, NULL );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
|
|
|
|
debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
|
|
|
|
|
|
|
|
/* interpret the data */
|
|
|
|
r = ERROR_SUCCESS;
|
2007-06-26 23:25:28 +02:00
|
|
|
for( n=0; n < rawsize; )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
2007-06-26 23:25:28 +02:00
|
|
|
mask = rawdata[n] | (rawdata[n+1] << 8);
|
2005-10-27 14:25:04 +02:00
|
|
|
|
|
|
|
if (mask&1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* if the low bit is set, columns are continuous and
|
|
|
|
* the number of columns is specified in the high byte
|
|
|
|
*/
|
2007-06-26 23:25:28 +02:00
|
|
|
sz = 2;
|
|
|
|
for( i=0; i<tv->num_cols; i++ )
|
|
|
|
{
|
|
|
|
if( (tv->columns[i].type & MSITYPE_STRING) &&
|
|
|
|
! MSITYPE_IS_BINARY(tv->columns[i].type) )
|
|
|
|
sz += bytes_per_strref;
|
|
|
|
else
|
|
|
|
sz += bytes_per_column( &tv->columns[i] );
|
|
|
|
}
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2007-06-26 23:25:28 +02:00
|
|
|
* If the low bit is not set, mask is a bitmask.
|
2005-10-27 14:25:04 +02:00
|
|
|
* Excepting for key fields, which are always present,
|
|
|
|
* each bit indicates that a field is present in the transform record.
|
|
|
|
*
|
2007-06-26 23:25:28 +02:00
|
|
|
* mask == 0 is a special case ... only the keys will be present
|
2005-10-27 14:25:04 +02:00
|
|
|
* and it means that this row should be deleted.
|
|
|
|
*/
|
|
|
|
sz = 2;
|
|
|
|
for( i=0; i<tv->num_cols; i++ )
|
|
|
|
{
|
|
|
|
if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
|
2007-06-26 23:25:28 +02:00
|
|
|
{
|
|
|
|
if( (tv->columns[i].type & MSITYPE_STRING) &&
|
|
|
|
! MSITYPE_IS_BINARY(tv->columns[i].type) )
|
|
|
|
sz += bytes_per_strref;
|
|
|
|
else
|
|
|
|
sz += bytes_per_column( &tv->columns[i] );
|
|
|
|
}
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check we didn't run of the end of the table */
|
|
|
|
if ( (n+sz) > rawsize )
|
|
|
|
{
|
|
|
|
ERR("borked.\n");
|
2007-06-26 23:25:28 +02:00
|
|
|
dump_table( st, (USHORT *)rawdata, rawsize );
|
2005-10-27 14:25:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-06-26 23:25:28 +02:00
|
|
|
rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
|
2005-10-27 14:25:04 +02:00
|
|
|
if (rec)
|
|
|
|
{
|
2006-10-12 11:55:53 +02:00
|
|
|
if ( mask & 1 )
|
2005-10-27 14:25:04 +02:00
|
|
|
{
|
2007-07-10 04:34:41 +02:00
|
|
|
WCHAR table[32];
|
|
|
|
DWORD sz = 32;
|
|
|
|
UINT number = MSI_NULL_INTEGER;
|
|
|
|
|
2006-10-12 11:55:53 +02:00
|
|
|
TRACE("inserting record\n");
|
2006-10-06 10:36:37 +02:00
|
|
|
|
|
|
|
if (!lstrcmpW(name, szColumns))
|
|
|
|
{
|
2006-10-19 18:53:32 +02:00
|
|
|
MSI_RecordGetStringW( rec, 1, table, &sz );
|
2007-07-10 04:34:41 +02:00
|
|
|
number = MSI_RecordGetInteger( rec, 2 );
|
2006-10-19 18:53:32 +02:00
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
/*
|
|
|
|
* Native msi seems writes nul into the Number (2nd) column of
|
|
|
|
* the _Columns table, only when the columns are from a new table
|
|
|
|
*/
|
|
|
|
if ( number == MSI_NULL_INTEGER )
|
2006-10-19 18:53:32 +02:00
|
|
|
{
|
2007-07-10 04:34:41 +02:00
|
|
|
/* reset the column number on a new table */
|
|
|
|
if ( lstrcmpW(coltable, table) )
|
|
|
|
{
|
|
|
|
colcol = 0;
|
|
|
|
lstrcpyW( coltable, table );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fix nul column numbers */
|
|
|
|
MSI_RecordSetInteger( rec, 2, ++colcol );
|
2006-10-19 18:53:32 +02:00
|
|
|
}
|
2006-10-06 10:36:37 +02:00
|
|
|
}
|
|
|
|
|
2007-04-23 09:24:41 +02:00
|
|
|
r = TABLE_insert_row( &tv->view, rec, FALSE );
|
2006-10-12 11:55:53 +02:00
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
ERR("insert row failed\n");
|
2007-07-10 04:34:41 +02:00
|
|
|
|
|
|
|
if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
|
|
|
|
msi_update_table_columns( db, table );
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-12 11:55:53 +02:00
|
|
|
UINT row = 0;
|
|
|
|
|
|
|
|
r = msi_table_find_row( tv, rec, &row );
|
|
|
|
if (r != ERROR_SUCCESS)
|
|
|
|
ERR("no matching row to transform\n");
|
|
|
|
else if ( mask )
|
|
|
|
{
|
2006-10-15 17:19:54 +02:00
|
|
|
TRACE("modifying row [%d]:\n", row);
|
2006-10-26 10:09:47 +02:00
|
|
|
TABLE_set_row( &tv->view, row, rec, mask );
|
2006-10-12 11:55:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-15 17:19:54 +02:00
|
|
|
TRACE("deleting row [%d]:\n", row);
|
2007-06-18 22:49:31 +02:00
|
|
|
TABLE_delete_row( &tv->view, row );
|
2006-10-12 11:55:53 +02:00
|
|
|
}
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
2006-04-07 13:40:12 +02:00
|
|
|
if( TRACE_ON(msidb) ) dump_record( rec );
|
2005-10-27 14:25:04 +02:00
|
|
|
msiobj_release( &rec->hdr );
|
|
|
|
}
|
|
|
|
|
2007-06-26 23:25:28 +02:00
|
|
|
n += sz;
|
2005-10-27 14:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
/* no need to free the table, it's associated with the database */
|
|
|
|
msi_free( rawdata );
|
|
|
|
if( tv )
|
|
|
|
tv->view.ops->delete( &tv->view );
|
|
|
|
|
2005-10-26 15:56:33 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* msi_table_apply_transform
|
|
|
|
*
|
|
|
|
* Enumerate the table transforms in a transform storage and apply each one.
|
|
|
|
*/
|
|
|
|
UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
|
|
|
|
{
|
2007-07-10 04:34:41 +02:00
|
|
|
struct list transforms;
|
2005-10-26 15:56:33 +02:00
|
|
|
IEnumSTATSTG *stgenum = NULL;
|
2007-07-10 04:34:41 +02:00
|
|
|
TRANSFORMDATA *transform;
|
|
|
|
TRANSFORMDATA *tables = NULL, *columns = NULL;
|
2005-10-26 15:56:33 +02:00
|
|
|
HRESULT r;
|
|
|
|
STATSTG stat;
|
|
|
|
string_table *strings;
|
|
|
|
UINT ret = ERROR_FUNCTION_FAILED;
|
2007-06-26 23:25:28 +02:00
|
|
|
UINT bytes_per_strref;
|
2005-10-26 15:56:33 +02:00
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
TRACE("%p %p\n", db, stg );
|
|
|
|
|
2007-06-26 23:25:28 +02:00
|
|
|
strings = msi_load_string_table( stg, &bytes_per_strref );
|
2005-10-26 15:56:33 +02:00
|
|
|
if( !strings )
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
|
|
|
|
if( FAILED( r ) )
|
|
|
|
goto end;
|
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
list_init(&transforms);
|
|
|
|
|
|
|
|
while ( TRUE )
|
|
|
|
{
|
|
|
|
MSITABLEVIEW *tv = NULL;
|
|
|
|
WCHAR name[0x40];
|
|
|
|
ULONG count = 0;
|
|
|
|
|
|
|
|
r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
|
|
|
|
if ( FAILED( r ) || !count )
|
|
|
|
break;
|
|
|
|
|
|
|
|
decode_streamname( stat.pwcsName, name );
|
2007-12-26 12:02:55 +01:00
|
|
|
CoTaskMemFree( stat.pwcsName );
|
2007-07-10 04:34:41 +02:00
|
|
|
if ( name[0] != 0x4840 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( !lstrcmpW( name+1, szStringPool ) ||
|
|
|
|
!lstrcmpW( name+1, szStringData ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
|
|
|
|
if ( !transform )
|
|
|
|
break;
|
|
|
|
|
|
|
|
list_add_tail( &transforms, &transform->entry );
|
|
|
|
|
|
|
|
transform->name = strdupW( name + 1 );
|
|
|
|
|
|
|
|
if ( !lstrcmpW( transform->name, szTables ) )
|
|
|
|
tables = transform;
|
|
|
|
else if (!lstrcmpW( transform->name, szColumns ) )
|
|
|
|
columns = transform;
|
|
|
|
|
|
|
|
TRACE("transform contains stream %s\n", debugstr_w(name));
|
|
|
|
|
|
|
|
/* load the table */
|
|
|
|
r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = tv->view.ops->execute( &tv->view, NULL );
|
|
|
|
if( r != ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
tv->view.ops->delete( &tv->view );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tv->view.ops->delete( &tv->view );
|
|
|
|
}
|
|
|
|
|
2006-10-06 10:35:44 +02:00
|
|
|
/*
|
2006-11-01 07:09:33 +01:00
|
|
|
* Apply _Tables and _Columns transforms first so that
|
2006-10-06 10:35:44 +02:00
|
|
|
* the table metadata is correct, and empty tables exist.
|
|
|
|
*/
|
2007-07-10 04:34:41 +02:00
|
|
|
ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
|
2006-10-06 10:35:44 +02:00
|
|
|
if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
|
|
|
|
goto end;
|
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
|
2006-10-06 10:35:44 +02:00
|
|
|
if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
|
|
|
|
goto end;
|
|
|
|
|
2005-10-26 15:56:33 +02:00
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
while ( !list_empty( &transforms ) )
|
2005-10-26 15:56:33 +02:00
|
|
|
{
|
2007-07-10 04:34:41 +02:00
|
|
|
transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
|
2006-10-06 10:35:44 +02:00
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
if ( lstrcmpW( transform->name, szColumns ) &&
|
|
|
|
lstrcmpW( transform->name, szTables ) &&
|
|
|
|
ret == ERROR_SUCCESS )
|
|
|
|
{
|
|
|
|
ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
|
|
|
|
}
|
2006-10-05 10:00:46 +02:00
|
|
|
|
2007-07-10 04:34:41 +02:00
|
|
|
list_remove( &transform->entry );
|
|
|
|
msi_free( transform->name );
|
|
|
|
msi_free( transform );
|
2005-10-26 15:56:33 +02:00
|
|
|
}
|
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
if ( ret == ERROR_SUCCESS )
|
2006-11-01 07:09:23 +01:00
|
|
|
append_storage_to_db( db, stg );
|
2005-10-27 14:25:04 +02:00
|
|
|
|
2005-10-26 15:56:33 +02:00
|
|
|
end:
|
|
|
|
if ( stgenum )
|
|
|
|
IEnumSTATSTG_Release( stgenum );
|
|
|
|
if ( strings )
|
|
|
|
msi_destroy_stringtable( strings );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2005-10-27 14:25:04 +02:00
|
|
|
|
2006-11-01 07:09:23 +01:00
|
|
|
void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
|
|
|
|
{
|
|
|
|
MSITRANSFORM *t;
|
|
|
|
|
|
|
|
t = msi_alloc( sizeof *t );
|
|
|
|
t->stg = stg;
|
|
|
|
IStorage_AddRef( stg );
|
|
|
|
list_add_tail( &db->transforms, &t->entry );
|
|
|
|
}
|
|
|
|
|
2005-10-27 14:25:04 +02:00
|
|
|
void msi_free_transforms( MSIDATABASE *db )
|
|
|
|
{
|
|
|
|
while( !list_empty( &db->transforms ) )
|
|
|
|
{
|
|
|
|
MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
|
|
|
|
MSITRANSFORM, entry );
|
|
|
|
list_remove( &t->entry );
|
|
|
|
IStorage_Release( t->stg );
|
|
|
|
msi_free( t );
|
|
|
|
}
|
|
|
|
}
|