sync msi to wine 1.1.31
[reactos.git] / reactos / dll / win32 / msi / table.c
index 8d416f2..86cd4be 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include <stdarg.h>
+#include <assert.h>
 
 #define COBJMACROS
 #define NONAMELESSUNION
@@ -34,7 +35,6 @@
 #include "winnls.h"
 #include "msipriv.h"
 #include "query.h"
-#include "assert.h"
 
 #include "wine/debug.h"
 #include "wine/unicode.h"
@@ -42,6 +42,7 @@
 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
 
 #define MSITABLE_HASH_TABLE_SIZE 37
+#define LONG_STR_BYTES 3
 
 typedef struct tagMSICOLUMNHASHENTRY
 {
@@ -52,19 +53,33 @@ typedef struct tagMSICOLUMNHASHENTRY
 
 typedef struct tagMSICOLUMNINFO
 {
-    LPCWSTR tablename;
+    LPWSTR tablename;
     UINT   number;
-    LPCWSTR colname;
+    LPWSTR colname;
     UINT   type;
     UINT   offset;
+    INT    ref_count;
+    BOOL   temporary;
     MSICOLUMNHASHENTRY **hash_table;
 } MSICOLUMNINFO;
 
+typedef struct tagMSIORDERINFO
+{
+    UINT *reorder;
+    UINT num_cols;
+    UINT cols[1];
+} MSIORDERINFO;
+
 struct tagMSITABLE
 {
-    USHORT **data;
+    BYTE **data;
+    BOOL *data_persistent;
     UINT row_count;
     struct list entry;
+    MSICOLUMNINFO *colinfo;
+    UINT col_count;
+    MSICONDITION persistent;
+    INT ref_count;
     WCHAR name[1];
 };
 
@@ -78,21 +93,51 @@ static const WCHAR szStringData[] = {
 static const WCHAR szStringPool[] = {
     '_','S','t','r','i','n','g','P','o','o','l',0 };
 
+/* information for default tables */
+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 };
+
+static const MSICOLUMNINFO _Columns_cols[4] = {
+    { szColumns, 1, szTable,  MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
+    { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2,     2, 0, 0, NULL },
+    { szColumns, 3, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
+    { szColumns, 4, szType,   MSITYPE_VALID | 2,                   6, 0, 0, NULL },
+};
+
+static const MSICOLUMNINFO _Tables_cols[1] = {
+    { szTables,  1, szName,   MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
+};
+
 #define MAX_STREAM_NAME 0x1f
 
 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
        MSICOLUMNINFO **pcols, UINT *pcount );
+static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
+       DWORD count );
 static UINT get_tablecolumns( MSIDATABASE *db,
        LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
+static UINT table_find_insert_idx (MSIVIEW *view, LPCWSTR name, INT *pidx);
 
-static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
+static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
 {
+    if( MSITYPE_IS_BINARY(col->type) )
+        return 2;
+
     if( col->type & MSITYPE_STRING )
+        return db->bytes_per_strref;
+
+    if( (col->type & 0xff) <= 2)
         return 2;
-    if( (col->type & 0xff) > 4 )
+
+    if( (col->type & 0xff) != 4 )
         ERR("Invalid column size!\n");
-    return col->type & 0xff;
+
+    return 4;
 }
 
 static int utf2mime(int x)
@@ -141,7 +186,7 @@ static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
             if( next && (next<0x80) )
             {
                 next = utf2mime(next);
-                if( next >= 0  )
+                if( next != -1 )
                 {
                      next += 0x3ffffc0;
                      ch += (next<<6);
@@ -169,7 +214,7 @@ static int mime2utf(int x)
     return '_';
 }
 
-static BOOL decode_streamname(LPWSTR in, LPWSTR out)
+BOOL decode_streamname(LPCWSTR in, LPWSTR out)
 {
     WCHAR ch;
     DWORD count = 0;
@@ -217,14 +262,15 @@ void enum_stream_names( IStorage *stg )
         decode_streamname( stat.pwcsName, name );
         TRACE("stream %2d -> %s %s\n", n,
               debugstr_w(stat.pwcsName), debugstr_w(name) );
+        CoTaskMemFree( stat.pwcsName );
         n++;
     }
 
     IEnumSTATSTG_Release( stgenum );
 }
 
-static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
-                              USHORT **pdata, UINT *psz )
+UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
+                       BYTE **pdata, UINT *psz )
 {
     HRESULT r;
     UINT ret = ERROR_FUNCTION_FAILED;
@@ -234,7 +280,7 @@ static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
     STATSTG stat;
     LPWSTR encname;
 
-    encname = encode_streamname(TRUE, stname);
+    encname = encode_streamname(table, stname);
 
     TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
 
@@ -287,7 +333,7 @@ end:
     return ret;
 }
 
-UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
+static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
 {
     LPWSTR encname;
     HRESULT r;
@@ -370,8 +416,8 @@ end:
     return ret;
 }
 
-static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
-                               LPVOID data, UINT sz )
+UINT write_stream_data( IStorage *stg, LPCWSTR stname,
+                        LPCVOID data, UINT sz, BOOL bTable )
 {
     HRESULT r;
     UINT ret = ERROR_FUNCTION_FAILED;
@@ -381,7 +427,7 @@ static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
     LARGE_INTEGER pos;
     LPWSTR encname;
 
-    encname = encode_streamname(TRUE, stname );
+    encname = encode_streamname(bTable, stname );
     r = IStorage_OpenStream( stg, encname, NULL, 
             STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
     if( FAILED(r) )
@@ -412,11 +458,14 @@ static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
         goto end;
     }
 
-    r = IStream_Write(stm, data, sz, &count );
-    if( FAILED( r ) || ( count != sz ) )
+    if (sz)
     {
-        WARN("Failed to Write\n");
-        goto end;
+        r = IStream_Write(stm, data, sz, &count );
+        if( FAILED( r ) || ( count != sz ) )
+        {
+            WARN("Failed to Write\n");
+            goto end;
+        }
     }
 
     ret = ERROR_SUCCESS;
@@ -429,46 +478,39 @@ end:
 
 static void free_table( MSITABLE *table )
 {
-    int i;
+    UINT i;
     for( i=0; i<table->row_count; i++ )
         msi_free( table->data[i] );
     msi_free( table->data );
+    msi_free( table->data_persistent );
+    msi_free_colinfo( table->colinfo, table->col_count );
+    msi_free( table->colinfo );
     msi_free( table );
 }
 
-static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
+static UINT msi_table_get_row_size( MSIDATABASE *db,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 );
+    return last_col->offset + bytes_per_column( db, last_col );
 }
 
 /* add this table to the list of cached tables in the database */
-static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
-                                    const MSICOLUMNINFO *cols, UINT num_cols )
+static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
 {
-    MSITABLE *t;
-    USHORT *rawdata = NULL;
+    BYTE *rawdata = NULL;
     UINT rawsize = 0, i, j, row_size = 0;
 
-    TRACE("%s\n",debugstr_w(name));
-
-    /* nonexistent tables should be interpreted as empty tables */
-    t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
-    if( !t )
-        return t;
-
-    row_size = msi_table_get_row_size( cols, num_cols );
+    TRACE("%s\n",debugstr_w(t->name));
 
-    t->row_count = 0;
-    t->data = NULL;
-    lstrcpyW( t->name, name );
+    row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
 
     /* if we can't read the table, just assume that it's empty */
-    read_stream_data( stg, name, &rawdata, &rawsize );
+    read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
     if( !rawdata )
-        return t;
+        return ERROR_SUCCESS;
 
     TRACE("Read %d bytes\n", rawsize );
 
@@ -482,6 +524,9 @@ static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
     t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
     if( !t->data )
         goto err;
+    t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
+    if ( !t->data_persistent )
+        goto err;
 
     /* transpose all the data */
     TRACE("Transposing data from %d rows\n", t->row_count );
@@ -490,34 +535,30 @@ static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
         t->data[i] = msi_alloc( row_size );
         if( !t->data[i] )
             goto err;
+        t->data_persistent[i] = TRUE;
 
-        for( j=0; j<num_cols; j++ )
+        for( j=0; j<t->col_count; j++ )
         {
-            UINT ofs = cols[j].offset/2;
-            UINT n = bytes_per_column( &cols[j] );
+            UINT ofs = t->colinfo[j].offset;
+            UINT n = bytes_per_column( db, &t->colinfo[j] );
+            UINT k;
 
-            switch( n )
+            if ( n != 2 && n != 3 && n != 4 )
             {
-            case 2:
-                t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
-                break;
-            case 4:
-                t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
-                t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
-                break;
-            default:
                 ERR("oops - unknown column width %d\n", n);
                 goto err;
             }
+
+            for ( k = 0; k < n; k++ )
+                t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
         }
     }
 
     msi_free( rawdata );
-    return t;
+    return ERROR_SUCCESS;
 err:
     msi_free( rawdata );
-    free_table( t );
-    return NULL;
+    return ERROR_FUNCTION_FAILED;
 }
 
 void free_cached_tables( MSIDATABASE *db )
@@ -553,6 +594,8 @@ static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO
     if( r != ERROR_SUCCESS )
         return r;
 
+    *pcount = column_count;
+
     /* if there's no columns, there's no table */
     if( column_count == 0 )
         return ERROR_INVALID_PARAMETER;
@@ -571,310 +614,292 @@ static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO
     }
 
     *pcols = columns;
-    *pcount = column_count;
 
     return r;
 }
 
-static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
-                            const MSICOLUMNINFO *cols, UINT num_cols )
+UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
+                       MSICONDITION persistent, MSITABLE **table_ret)
 {
+    UINT r, nField;
+    MSIVIEW *tv = NULL;
+    MSIRECORD *rec = NULL;
+    column_info *col;
     MSITABLE *table;
+    UINT i;
+    INT idx;
 
-    /* first, see if the table is cached */
-    table = find_cached_table( db, name );
-    if( table )
-        return table;
-
-    table = read_table_from_storage( db->storage, name, cols, num_cols );
-    if( table )
-        list_add_head( &db->tables, &table->entry );
-
-    return table;
-}
+    /* only add tables that don't exist already */
+    if( TABLE_Exists(db, name ) )
+    {
+        WARN("table %s exists\n", debugstr_w(name));
+        return ERROR_BAD_QUERY_SYNTAX;
+    }
 
-static UINT save_table( MSIDATABASE *db, MSITABLE *t )
-{
-    USHORT *rawdata = NULL, *p;
-    UINT rawsize, r, i, j, row_size, num_cols = 0;
-    MSICOLUMNINFO *cols = NULL;
+    table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
+    if( !table )
+        return ERROR_FUNCTION_FAILED;
 
-    TRACE("Saving %s\n", debugstr_w( t->name ) );
+    table->ref_count = 1;
+    table->row_count = 0;
+    table->data = NULL;
+    table->data_persistent = NULL;
+    table->colinfo = NULL;
+    table->col_count = 0;
+    table->persistent = persistent;
+    lstrcpyW( table->name, name );
 
-    r = table_get_column_info( db, t->name, &cols, &num_cols );
-    if( r != ERROR_SUCCESS )
-        return r;
-    
-    row_size = msi_table_get_row_size( cols, num_cols );
+    for( col = col_info; col; col = col->next )
+        table->col_count++;
 
-    rawsize = t->row_count * row_size;
-    rawdata = msi_alloc_zero( rawsize );
-    if( !rawdata )
+    table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
+    if (!table->colinfo)
     {
-        r = ERROR_NOT_ENOUGH_MEMORY;
-        goto err;
+        free_table( table );
+        return ERROR_FUNCTION_FAILED;
     }
 
-    p = rawdata;
-    for( i=0; i<num_cols; i++ )
+    for( i = 0, col = col_info; col; i++, col = col->next )
     {
-        for( j=0; j<t->row_count; j++ )
-        {
-            UINT offset = cols[i].offset;
+        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;
+        table->colinfo[ i ].ref_count = 0;
+        table->colinfo[ i ].hash_table = NULL;
+        table->colinfo[ i ].temporary = col->temporary;
+    }
+    table_calc_column_offsets( db, table->colinfo, table->col_count);
 
-            *p++ = t->data[j][offset/2];
-            if( 4 == bytes_per_column( &cols[i] ) )
-                *p++ = t->data[j][offset/2+1];
-        }
+    r = TABLE_CreateView( db, szTables, &tv );
+    TRACE("CreateView returned %x\n", r);
+    if( r )
+    {
+        free_table( table );
+        return r;
     }
 
-    TRACE("writing %d bytes\n", rawsize);
-    r = write_stream_data( db->storage, t->name, rawdata, rawsize );
+    r = tv->ops->execute( tv, 0 );
+    TRACE("tv execute returned %x\n", r);
+    if( r )
+        goto err;
 
-err:
-    msi_free_colinfo( cols, num_cols );
-    msi_free( cols );
-    msi_free( rawdata );
+    rec = MSI_CreateRecord( 1 );
+    if( !rec )
+        goto err;
 
-    return r;
-}
+    r = MSI_RecordSetStringW( rec, 1, name );
+    if( r )
+        goto err;
 
-HRESULT init_string_table( IStorage *stg )
-{
-    HRESULT r;
-    USHORT zero[2] = { 0, 0 };
-    ULONG count = 0;
-    IStream *stm = NULL;
-    LPWSTR encname;
+    r = table_find_insert_idx (tv, name, &idx);
+    if (r != ERROR_SUCCESS)
+       idx = -1;
 
-    encname = encode_streamname(TRUE, szStringPool );
+    r = tv->ops->insert_row( tv, rec, idx, persistent == MSICONDITION_FALSE );
+    TRACE("insert_row returned %x\n", r);
+    if( r )
+        goto err;
 
-    /* create the StringPool stream... add the zero string to it*/
-    r = IStorage_CreateStream( stg, encname,
-            STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
-    msi_free( encname );
-    if( r ) 
-    {
-        TRACE("Failed\n");
-        return r;
-    }
+    tv->ops->delete( tv );
+    tv = NULL;
 
-    r = IStream_Write(stm, zero, sizeof zero, &count );
-    IStream_Release( stm );
+    msiobj_release( &rec->hdr );
+    rec = NULL;
 
-    if( FAILED( r ) || ( count != sizeof zero ) )
+    if( persistent != MSICONDITION_FALSE )
     {
-        TRACE("Failed\n");
-        return E_FAIL;
+        /* add each column to the _Columns table */
+        r = TABLE_CreateView( db, szColumns, &tv );
+        if( r )
+            return r;
+
+        r = tv->ops->execute( tv, 0 );
+        TRACE("tv execute returned %x\n", r);
+        if( r )
+            goto err;
+
+        rec = MSI_CreateRecord( 4 );
+        if( !rec )
+            goto err;
+
+        r = MSI_RecordSetStringW( rec, 1, name );
+        if( r )
+            goto err;
+
+        /*
+         * 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 = table_find_insert_idx (tv, name, &idx);
+            if (r != ERROR_SUCCESS)
+                idx = -1;
+
+            r = tv->ops->insert_row( tv, rec, idx, FALSE );
+            if( r )
+                goto err;
+
+            nField++;
+        }
+        if( !col )
+            r = ERROR_SUCCESS;
     }
 
-    /* create the StringData stream... make it zero length */
-    encname = encode_streamname(TRUE, szStringData );
-    r = IStorage_CreateStream( stg, encname,
-            STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
-    msi_free( encname );
-    if( r ) 
+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)
     {
-        TRACE("Failed\n");
-        return E_FAIL;
+        list_add_head( &db->tables, &table->entry );
+        *table_ret = table;
     }
-    IStream_Release( stm );
+    else
+        free_table( table );
 
     return r;
 }
 
-string_table *load_string_table( IStorage *stg )
+static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
 {
-    string_table *st = NULL;
-    CHAR *data = NULL;
-    USHORT *pool = NULL;
-    UINT r, datasize = 0, poolsize = 0, codepage;
-    DWORD i, count, offset, len, n, refs;
-
-    r = read_stream_data( stg, szStringPool, &pool, &poolsize );
-    if( r != ERROR_SUCCESS)
-        goto end;
-    r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
-    if( r != ERROR_SUCCESS)
-        goto end;
-
-    count = poolsize/4;
-    if( poolsize > 4 )
-        codepage = pool[0] | ( pool[1] << 16 );
-    else
-        codepage = CP_ACP;
-    st = msi_init_stringtable( count, codepage );
+    MSITABLE *table;
+    UINT r;
 
-    offset = 0;
-    n = 1;
-    i = 1;
-    while( i<count )
+    /* first, see if the table is cached */
+    table = find_cached_table( db, name );
+    if( table )
     {
-        /* the string reference count is always the second word */
-        refs = pool[i*2+1];
+        *table_ret = table;
+        return ERROR_SUCCESS;
+    }
 
-        /* empty entries have two zeros, still have a string id */
-        if (pool[i*2] == 0 && refs == 0)
-        {
-            i++;
-            n++;
-            continue;
-        }
+    /* nonexistent tables should be interpreted as empty tables */
+    table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
+    if( !table )
+        return ERROR_FUNCTION_FAILED;
 
-        /*
-         * If a string is over 64k, the previous string entry is made null
-         * and its the high word of the length is inserted in the null string's
-         * reference count field.
-         */
-        if( pool[i*2] == 0)
-        {
-            len = (pool[i*2+3] << 16) + pool[i*2+2];
-            i += 2;
-        }
-        else
-        {
-            len = pool[i*2];
-            i += 1;
-        }
+    table->row_count = 0;
+    table->data = NULL;
+    table->data_persistent = NULL;
+    table->colinfo = NULL;
+    table->col_count = 0;
+    table->persistent = MSICONDITION_TRUE;
+    lstrcpyW( table->name, name );
 
-        if ( (offset + len) > datasize )
-        {
-            ERR("string table corrupt?\n");
-            break;
-        }
+    if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
+        table->persistent = MSICONDITION_NONE;
 
-        r = msi_addstring( st, n, data+offset, len, refs );
-        if( r != n )
-            ERR("Failed to add string %d\n", n );
-        n++;
-        offset += len;
+    r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
+    if (r != ERROR_SUCCESS)
+    {
+        free_table ( table );
+        return r;
     }
 
-    if ( datasize != offset )
-        ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
-
-    TRACE("Loaded %d strings\n", count);
-
-end:
-    msi_free( pool );
-    msi_free( data );
+    r = read_table_from_storage( db, table, db->storage );
+    if( r != ERROR_SUCCESS )
+    {
+        free_table( table );
+        return r;
+    }
 
-    return st;
+    list_add_head( &db->tables, &table->entry );
+    *table_ret = table;
+    return ERROR_SUCCESS;
 }
 
-static UINT save_string_table( MSIDATABASE *db )
+static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
 {
-    UINT i, count, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
-    UINT ret = ERROR_FUNCTION_FAILED;
-    CHAR *data = NULL;
-    USHORT *pool = NULL;
+    BYTE *rawdata = NULL, *p;
+    UINT rawsize, r, i, j, row_size;
 
-    TRACE("\n");
+    /* Nothing to do for non-persistent tables */
+    if( t->persistent == MSICONDITION_FALSE )
+        return ERROR_SUCCESS;
 
-    /* construct the new table in memory first */
-    count = msi_string_totalsize( db->strings, &datasize, &poolsize );
+    TRACE("Saving %s\n", debugstr_w( t->name ) );
 
-    TRACE("%u %u %u\n", count, datasize, poolsize );
+    row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
 
-    pool = msi_alloc( poolsize );
-    if( ! pool )
-    {
-        WARN("Failed to alloc pool %d bytes\n", poolsize );
-        goto err;
-    }
-    data = msi_alloc( datasize );
-    if( ! data )
+    rawsize = t->row_count * row_size;
+    rawdata = msi_alloc_zero( rawsize );
+    if( !rawdata )
     {
-        WARN("Failed to alloc data %d bytes\n", poolsize );
+        r = ERROR_NOT_ENOUGH_MEMORY;
         goto err;
     }
 
-    used = 0;
-    codepage = msi_string_get_codepage( db->strings );
-    pool[0]=codepage&0xffff;
-    pool[1]=(codepage>>16);
-    n = 1;
-    for( i=1; i<count; i++ )
+    rawsize = 0;
+    p = rawdata;
+    for( i=0; i<t->col_count; i++ )
     {
-        sz = datasize - used;
-        r = msi_id2stringA( db->strings, i, data+used, &sz );
-        if( r != ERROR_SUCCESS )
+        for( j=0; j<t->row_count; j++ )
         {
-            ERR("failed to fetch string\n");
-            sz = 0;
-        }
-        if( sz && (sz < (datasize - used ) ) )
-            sz--;
+            UINT offset = t->colinfo[i].offset;
 
-        if (sz)
-            pool[ n*2 + 1 ] = msi_id_refcount( db->strings, i );
-        else
-            pool[ n*2 + 1 ] = 0;
-        if (sz < 0x10000)
-        {
-            pool[ n*2 ] = sz;
-            n++;
-        }
-        else
-        {
-            pool[ n*2 ] = 0;
-            pool[ n*2 + 2 ] = sz&0xffff;
-            pool[ n*2 + 3 ] = (sz>>16);
-            n += 2;
-        }
-        used += sz;
-        if( used > datasize  )
-        {
-            ERR("oops overran %d >= %d\n", used, datasize);
-            goto err;
-        }
-    }
+            if (!t->data_persistent[j]) continue;
+            if (i == 0)
+                rawsize += row_size;
 
-    if( used != datasize )
-    {
-        ERR("oops used %d != datasize %d\n", used, datasize);
-        goto err;
+            *p++ = t->data[j][offset];
+            *p++ = t->data[j][offset + 1];
+            if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
+            {
+                *p++ = t->data[j][offset + 2];
+                *p++ = t->data[j][offset + 3];
+            }
+        }
     }
 
-    /* write the streams */
-    r = write_stream_data( db->storage, szStringData, data, datasize );
-    TRACE("Wrote StringData r=%08x\n", r);
-    if( r )
-        goto err;
-    r = write_stream_data( db->storage, szStringPool, pool, poolsize );
-    TRACE("Wrote StringPool r=%08x\n", r);
-    if( r )
-        goto err;
-
-    ret = ERROR_SUCCESS;
+    TRACE("writing %d bytes\n", rawsize);
+    r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
 
 err:
-    msi_free( data );
-    msi_free( pool );
+    msi_free( rawdata );
 
-    return ret;
+    return r;
 }
 
-/* information for default tables */
-static const WCHAR szTables[]  = { '_','T','a','b','l','e','s',0 };
-static const WCHAR szTable[]  = { 'T','a','b','l','e',0 };
-static const WCHAR szName[]    = { 'N','a','m','e',0 };
-static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
-static const WCHAR szColumn[]  = { 'C','o','l','u','m','n',0 };
-static const WCHAR szNumber[]  = { 'N','u','m','b','e','r',0 };
-static const WCHAR szType[]    = { 'T','y','p','e',0 };
+static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
+                                       DWORD count )
+{
+    DWORD i;
 
-static const MSICOLUMNINFO _Columns_cols[4] = {
-    { szColumns, 1, szTable,  MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
-    { szColumns, 2, szNumber, MSITYPE_VALID | 2,                   2 },
-    { szColumns, 3, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
-    { szColumns, 4, szType,   MSITYPE_VALID | 2,                   6 },
-};
-static const MSICOLUMNINFO _Tables_cols[1] = {
-    { szTables,  1, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
-};
+    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( db, &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);
+    }
+}
 
-static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
+static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
+                                     MSICOLUMNINFO *colinfo, UINT *sz)
 {
     const MSICOLUMNINFO *p;
     DWORD i, n;
@@ -899,13 +924,14 @@ static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT
     {
         if (colinfo && (i < *sz) )
         {
-            memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
+            colinfo[i] = p[i];
             colinfo[i].tablename = strdupW( p[i].tablename );
             colinfo[i].colname = strdupW( p[i].colname );
         }
         if( colinfo && (i >= *sz) )
             break;
     }
+    table_calc_column_offsets( db, colinfo, n );
     *sz = n;
     return ERROR_SUCCESS;
 }
@@ -916,17 +942,27 @@ static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
 
     for( i=0; i<count; i++ )
     {
-        msi_free( (LPWSTR) colinfo[i].tablename );
-        msi_free( (LPWSTR) colinfo[i].colname );
+        msi_free( colinfo[i].tablename );
+        msi_free( colinfo[i].colname );
         msi_free( colinfo[i].hash_table );
     }
 }
 
-static LPWSTR msi_makestring( MSIDATABASE *db, UINT stringid)
+static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
 {
     return strdupW(msi_string_lookup_id( db->strings, stringid ));
 }
 
+static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
+{
+    UINT ret = 0, i;
+
+    for (i = 0; i < bytes; i++)
+        ret += (data[row][col + i] << i * 8);
+
+    return ret;
+}
+
 static UINT get_tablecolumns( MSIDATABASE *db,
        LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
 {
@@ -936,12 +972,12 @@ static UINT get_tablecolumns( MSIDATABASE *db,
     TRACE("%s\n", debugstr_w(szTableName));
 
     /* first check if there is a default table with that name */
-    r = get_defaulttablecolumns( szTableName, colinfo, sz );
+    r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
     if( ( r == ERROR_SUCCESS ) && *sz )
         return r;
 
-    table = get_table( db, szColumns, _Columns_cols, 4 );
-    if( !table )
+    r = get_table( db, szColumns, &table );
+    if( r != ERROR_SUCCESS )
     {
         ERR("couldn't load _Columns table\n");
         return ERROR_FUNCTION_FAILED;
@@ -957,17 +993,19 @@ static UINT get_tablecolumns( MSIDATABASE *db,
 
     TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
 
+    /* Note: _Columns table doesn't have non-persistent data */
+
     /* if maxcount is non-zero, assume it's exactly right for this table */
     memset( colinfo, 0, maxcount*sizeof(*colinfo) );
     count = table->row_count;
     for( i=0; i<count; i++ )
     {
-        if( table->data[ i ][ 0 ] != table_id )
+        if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
             continue;
         if( colinfo )
         {
-            UINT id = table->data[ i ] [ 2 ];
-            UINT col = table->data[ i ][ 1 ] - (1<<15);
+            UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
+            UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
 
             /* check the column number is in range */
             if (col<1 || col>maxcount)
@@ -986,8 +1024,11 @@ static UINT get_tablecolumns( MSIDATABASE *db,
             colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
             colinfo[ col - 1 ].number = col;
             colinfo[ col - 1 ].colname = msi_makestring( db, id );
-            colinfo[ col - 1 ].type = table->data[ i ] [ 3 ] - (1<<15);
+            colinfo[ col - 1 ].type = read_table_int(table->data, i,
+                                                     table->colinfo[3].offset,
+                                                     sizeof(USHORT)) - (1<<15);
             colinfo[ col - 1 ].offset = 0;
+            colinfo[ col - 1 ].ref_count = 0;
             colinfo[ col - 1 ].hash_table = NULL;
         }
         n++;
@@ -995,40 +1036,56 @@ static UINT get_tablecolumns( MSIDATABASE *db,
 
     TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
 
-    if (maxcount && n != maxcount)
+    if (colinfo && n != maxcount)
     {
         ERR("missing column in table %s\n", debugstr_w(szTableName));
         msi_free_colinfo(colinfo, maxcount );
         return ERROR_FUNCTION_FAILED;
     }
 
-    /* calculate the offsets */
-    for( i=0; maxcount && (i<maxcount); 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);
-    }
+    table_calc_column_offsets( db, colinfo, n );
     *sz = n;
 
     return ERROR_SUCCESS;
 }
 
+static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
+{
+    MSITABLE *table;
+    UINT size, offset, old_count;
+    UINT n;
+
+    table = find_cached_table( db, name );
+    old_count = table->col_count;
+    msi_free( table->colinfo );
+    table->colinfo = NULL;
+
+    table_get_column_info( db, name, &table->colinfo, &table->col_count );
+    if (!table->col_count)
+        return;
+
+    size = msi_table_get_row_size( db, 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 );
+        if (old_count < table->col_count)
+            memset( &table->data[n][offset], 0, size - offset );
+    }
+}
+
 /* try to find the table name in the _Tables table */
-BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
+BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
 {
     UINT r, table_id = 0, i, count;
     MSITABLE *table = NULL;
 
-    if( !lstrcmpW( name, szTables ) )
-        return TRUE;
-    if( !lstrcmpW( name, szColumns ) )
+    static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
+    static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
+
+    if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
+        !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
         return TRUE;
 
     r = msi_string2idW( db->strings, name, &table_id );
@@ -1038,23 +1095,17 @@ BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
         return FALSE;
     }
 
-    table = get_table( db, szTables, _Tables_cols, 1 );
-    if( !table )
+    r = get_table( db, szTables, &table );
+    if( r != ERROR_SUCCESS )
     {
-        TRACE("table %s not available\n", debugstr_w(szTables));
+        ERR("table %s not available\n", debugstr_w(szTables));
         return FALSE;
     }
 
-    /* count = table->size/2; */
     count = table->row_count;
     for( i=0; i<count; i++ )
         if( table->data[ i ][ 0 ] == table_id )
-            break;
-
-    if (i!=count)
-        return TRUE;
-
-    TRACE("Searched %d tables, but %d was not found\n", count, table_id );
+            return TRUE;
 
     return FALSE;
 }
@@ -1067,6 +1118,7 @@ typedef struct tagMSITABLEVIEW
     MSIDATABASE   *db;
     MSITABLE      *table;
     MSICOLUMNINFO *columns;
+    MSIORDERINFO  *order;
     UINT           num_cols;
     UINT           row_size;
     WCHAR          name[1];
@@ -1075,7 +1127,7 @@ typedef struct tagMSITABLEVIEW
 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    UINT offset, num_rows, n;
+    UINT offset, n;
 
     if( !tv->table )
         return ERROR_INVALID_PARAMETER;
@@ -1084,8 +1136,7 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
         return ERROR_INVALID_PARAMETER;
 
     /* how many rows are there ? */
-    num_rows = tv->table->row_count;
-    if( row >= num_rows )
+    if( row >= tv->table->row_count )
         return ERROR_NO_MORE_ITEMS;
 
     if( tv->columns[col-1].offset >= tv->row_size )
@@ -1095,29 +1146,111 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
         return ERROR_FUNCTION_FAILED;
     }
 
-    offset = row + (tv->columns[col-1].offset/2) * num_rows;
-    n = bytes_per_column( &tv->columns[col-1] );
-    switch( n )
+    if (tv->order)
+        row = tv->order->reorder[row];
+
+    n = bytes_per_column( tv->db, &tv->columns[col-1] );
+    if (n != 2 && n != 3 && n != 4)
     {
-    case 4:
-        offset = tv->columns[col-1].offset/2;
-        *val = tv->table->data[row][offset] + 
-               (tv->table->data[row][offset + 1] << 16);
-        break;
-    case 2:
-        offset = tv->columns[col-1].offset/2;
-        *val = tv->table->data[row][offset];
-        break;
-    default:
         ERR("oops! what is %d bytes per column?\n", n );
         return ERROR_FUNCTION_FAILED;
     }
 
+    offset = tv->columns[col-1].offset;
+    *val = read_table_int(tv->table->data, row, offset, n);
+
     /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
 
     return ERROR_SUCCESS;
 }
 
+static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
+{
+    LPWSTR p, stname = NULL;
+    UINT i, r, type, ival;
+    DWORD len;
+    LPCWSTR sval;
+    MSIVIEW *view = (MSIVIEW *) tv;
+
+    TRACE("%p %d\n", tv, row);
+
+    len = lstrlenW( tv->name ) + 1;
+    stname = msi_alloc( len*sizeof(WCHAR) );
+    if ( !stname )
+    {
+       r = ERROR_OUTOFMEMORY;
+       goto err;
+    }
+
+    lstrcpyW( stname, tv->name );
+
+    for ( i = 0; i < tv->num_cols; i++ )
+    {
+        type = tv->columns[i].type;
+        if ( type & MSITYPE_KEY )
+        {
+            static const WCHAR szDot[] = { '.', 0 };
+
+            r = TABLE_fetch_int( view, row, i+1, &ival );
+            if ( r != ERROR_SUCCESS )
+                goto err;
+
+            if ( tv->columns[i].type & MSITYPE_STRING )
+            {
+                sval = msi_string_lookup_id( tv->db->strings, ival );
+                if ( !sval )
+                {
+                    r = ERROR_INVALID_PARAMETER;
+                    goto err;
+                }
+            }
+            else
+            {
+                static const WCHAR fmt[] = { '%','d',0 };
+                WCHAR number[0x20];
+                UINT n = bytes_per_column( tv->db, &tv->columns[i] );
+
+                switch( n )
+                {
+                case 2:
+                    sprintfW( number, fmt, ival^0x8000 );
+                    break;
+                case 4:
+                    sprintfW( number, fmt, ival^0x80000000 );
+                    break;
+                default:
+                    ERR( "oops - unknown column width %d\n", n );
+                    r = ERROR_FUNCTION_FAILED;
+                    goto err;
+                }
+                sval = number;
+            }
+
+            len += lstrlenW( szDot ) + lstrlenW( sval );
+            p = msi_realloc ( stname, len*sizeof(WCHAR) );
+            if ( !p )
+            {
+                r = ERROR_OUTOFMEMORY;
+                goto err;
+            }
+            stname = p;
+
+            lstrcatW( stname, szDot );
+            lstrcatW( stname, sval );
+        }
+        else
+           continue;
+    }
+
+    *pstname = stname;
+    return ERROR_SUCCESS;
+
+err:
+    msi_free( stname );
+    *pstname = NULL;
+    return r;
+}
+
 /*
  * 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
@@ -1126,58 +1259,19 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    UINT ival = 0, refcol = 0, r;
-    LPCWSTR sval;
-    LPWSTR full_name;
-    DWORD len;
-    static const WCHAR szDot[] = { '.', 0 };
-    WCHAR number[0x20];
+    UINT r;
+    LPWSTR full_name = NULL;
 
     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;
-
-    /* check the column value is in range */
-    if (ival < 0 || ival > tv->num_cols || ival == col)
-    {
-        ERR("bad column ref (%u) for stream\n", ival);
-        return ERROR_FUNCTION_FAILED;
-    }
-
-    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
+    r = msi_stream_name( tv, row, &full_name );
+    if ( r != ERROR_SUCCESS )
     {
-        static const WCHAR fmt[] = { '%','d',0 };
-        sprintfW( number, fmt, ival );
-        sval = number;
+        ERR("fetching stream, error = %d\n", r);
+        return r;
     }
 
-    len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
-    full_name = msi_alloc( len*sizeof(WCHAR) );
-    lstrcpyW( full_name, tv->name );
-    lstrcatW( full_name, szDot );
-    lstrcatW( full_name, sval );
-
     r = db_get_raw_stream( tv->db, full_name, stm );
     if( r )
         ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
@@ -1186,10 +1280,9 @@ static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, ISt
     return r;
 }
 
-static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
+static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
 {
-    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    UINT offset, n;
+    UINT offset, n, i;
 
     if( !tv->table )
         return ERROR_INVALID_PARAMETER;
@@ -1197,6 +1290,9 @@ static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val
     if( (col==0) || (col>tv->num_cols) )
         return ERROR_INVALID_PARAMETER;
 
+    if( row >= tv->table->row_count )
+        return ERROR_INVALID_PARAMETER;
+
     if( tv->columns[col-1].offset >= tv->row_size )
     {
         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
@@ -1204,32 +1300,178 @@ static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val
         return ERROR_FUNCTION_FAILED;
     }
 
-    n = bytes_per_column( &tv->columns[col-1] );
-    switch( n )
+    msi_free( tv->columns[col-1].hash_table );
+    tv->columns[col-1].hash_table = NULL;
+
+    n = bytes_per_column( tv->db, &tv->columns[col-1] );
+    if ( n != 2 && n != 3 && n != 4 )
     {
-    case 4:
-        offset = tv->columns[col-1].offset/2;
-        tv->table->data[row][offset]     = val & 0xffff;
-        tv->table->data[row][offset + 1] = (val>>16)&0xffff;
-        break;
-    case 2:
-        offset = tv->columns[col-1].offset/2;
-        tv->table->data[row][offset] = val;
-        break;
-    default:
         ERR("oops! what is %d bytes per column?\n", n );
         return ERROR_FUNCTION_FAILED;
     }
+
+    offset = tv->columns[col-1].offset;
+    for ( i = 0; i < n; i++ )
+        tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
+
     return ERROR_SUCCESS;
 }
 
-static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
+static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
+
+    if (!tv->table)
+        return ERROR_INVALID_PARAMETER;
+
+    if (tv->order)
+        row = tv->order->reorder[row];
+
+    return msi_view_get_row(tv->db, view, row, rec);
+}
+
+static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
+{
+    UINT r;
+    MSIQUERY *query = NULL;
+    MSIRECORD *rec = NULL;
+
+    static const WCHAR insert[] = {
+       'I','N','S','E','R','T',' ','I','N','T','O',' ',
+          '`','_','S','t','r','e','a','m','s','`',' ',
+         '(','`','N','a','m','e','`',',',
+             '`','D','a','t','a','`',')',' ',
+         'V','A','L','U','E','S',' ','(','?',',','?',')',0};
+
+    TRACE("%p %s %p\n", db, debugstr_w(name), data);
+
+    rec = MSI_CreateRecord( 2 );
+    if ( !rec )
+        return ERROR_OUTOFMEMORY;
+
+    r = MSI_RecordSetStringW( rec, 1, name );
+    if ( r != ERROR_SUCCESS )
+       goto err;
+
+    r = MSI_RecordSetIStream( rec, 2, data );
+    if ( r != ERROR_SUCCESS )
+       goto err;
+
+    r = MSI_DatabaseOpenViewW( db, insert, &query );
+    if ( r != ERROR_SUCCESS )
+       goto err;
+
+    r = MSI_ViewExecute( query, rec );
+
+err:
+    msiobj_release( &query->hdr );
+    msiobj_release( &rec->hdr );
+
+    return r;
+}
+
+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++ )
+    {
+        BOOL persistent;
+
+        /* only update the fields specified in the mask */
+        if ( !(mask&(1<<i)) )
+            continue;
+
+        persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
+                     (tv->table->data_persistent[row]);
+        /* FIXME: should we allow updating keys? */
+
+        val = 0;
+        if ( !MSI_RecordIsNull( rec, i + 1 ) )
+        {
+            if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
+            {
+                IStream *stm;
+                LPWSTR stname;
+
+                r = MSI_RecordGetIStream( rec, i + 1, &stm );
+                if ( r != ERROR_SUCCESS )
+                    return r;
+
+                r = msi_stream_name( tv, row, &stname );
+                if ( r != ERROR_SUCCESS )
+                {
+                    IStream_Release( stm );
+                    return r;
+                }
+
+                r = msi_addstreamW( tv->db, stname, stm );
+                IStream_Release( stm );
+                msi_free ( stname );
+
+                if ( r != ERROR_SUCCESS )
+                    return r;
+
+                val = 1; /* refers to the first key column */
+            }
+            else if ( tv->columns[i].type & MSITYPE_STRING )
+            {
+                LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
+                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;
+                }
+
+                val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
+                                      persistent ? StringPersistent : StringNonPersistent );
+            }
+            else if ( 2 == bytes_per_column( tv->db, &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;
+}
+
+static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    USHORT **p, *row;
+    BYTE **p, *row;
+    BOOL *b;
     UINT sz;
+    BYTE ***data_ptr;
+    BOOL **data_persist_ptr;
+    UINT *row_count;
 
-    TRACE("%p\n", view);
+    TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
 
     if( !tv->table )
         return ERROR_INVALID_PARAMETER;
@@ -1238,9 +1480,15 @@ static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
     if( !row )
         return ERROR_NOT_ENOUGH_MEMORY;
 
-    sz = (tv->table->row_count + 1) * sizeof (UINT*);
-    if( tv->table->data )
-        p = msi_realloc( tv->table->data, sz );
+    row_count = &tv->table->row_count;
+    data_ptr = &tv->table->data;
+    data_persist_ptr = &tv->table->data_persistent;
+    if (*num == -1)
+        *num = tv->table->row_count;
+
+    sz = (*row_count + 1) * sizeof (BYTE*);
+    if( *data_ptr )
+        p = msi_realloc( *data_ptr, sz );
     else
         p = msi_alloc( sz );
     if( !p )
@@ -1249,10 +1497,25 @@ static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
         return ERROR_NOT_ENOUGH_MEMORY;
     }
 
-    tv->table->data = p;
-    tv->table->data[tv->table->row_count] = row;
-    *num = tv->table->row_count;
-    tv->table->row_count++;
+    sz = (*row_count + 1) * sizeof (BOOL);
+    if( *data_persist_ptr )
+        b = msi_realloc( *data_persist_ptr, sz );
+    else
+        b = msi_alloc( sz );
+    if( !b )
+    {
+        msi_free( row );
+        msi_free( p );
+        return ERROR_NOT_ENOUGH_MEMORY;
+    }
+
+    *data_ptr = p;
+    (*data_ptr)[*row_count] = row;
+
+    *data_persist_ptr = b;
+    (*data_persist_ptr)[*row_count] = !temporary;
+
+    (*row_count)++;
 
     return ERROR_SUCCESS;
 }
@@ -1264,23 +1527,13 @@ static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
     TRACE("%p %p\n", tv, record);
 
     TRACE("There are %d columns\n", tv->num_cols );
-    tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
-    if( !tv->table )
-        return ERROR_FUNCTION_FAILED;
 
     return ERROR_SUCCESS;
 }
 
 static UINT TABLE_close( struct tagMSIVIEW *view )
 {
-    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-
     TRACE("%p\n", view );
-
-    if( !tv->table )
-        return ERROR_FUNCTION_FAILED;
-
-    tv->table = NULL;
     
     return ERROR_SUCCESS;
 }
@@ -1304,7 +1557,7 @@ static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *col
 }
 
 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
-                UINT n, LPWSTR *name, UINT *type )
+                UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
 
@@ -1319,9 +1572,13 @@ static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
         if( !*name )
             return ERROR_FUNCTION_FAILED;
     }
+
     if( type )
         *type = tv->columns[n-1].type;
 
+    if( temporary )
+        *temporary = tv->columns[n-1].temporary;
+
     return ERROR_SUCCESS;
 }
 
@@ -1360,101 +1617,195 @@ static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
     /* check there's no duplicate keys */
     r = msi_table_find_row( tv, rec, &row );
     if (r == ERROR_SUCCESS)
-        return ERROR_INVALID_DATA;
+        return ERROR_FUNCTION_FAILED;
 
     return ERROR_SUCCESS;
 }
 
-static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
-                                  UINT row, UINT mask )
+static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
 {
-    UINT i, val, r = ERROR_SUCCESS;
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
+    UINT i, r;
 
-    TRACE("%p %p %u %08x\n", tv, rec, row, mask );
+    TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
 
-    for( i = 0; i < tv->num_cols; i++ )
-    {
-        /* set keys or values specified in the mask */
-        if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
-            continue;
+    /* 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;
 
-        if( (tv->columns[i].type & MSITYPE_STRING) &&
-            ! MSITYPE_IS_BINARY(tv->columns[i].type) )
-        {
-            const WCHAR *str = MSI_RecordGetString( rec, i+1 );
-            val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
-        }
-        else
-        {
-            val = MSI_RecordGetInteger( rec, i+1 );
-            if ( 2 == bytes_per_column( &tv->columns[i] ) )
-                val ^= 0x8000;
-            else
-                val ^= 0x80000000;
-        }
-        r = TABLE_set_int( &tv->view, row, i+1, val );
-        if( r )
-            break;
+    r = table_create_new_row( view, &row, temporary );
+    TRACE("insert_row returned %08x\n", r);
+    if( r != ERROR_SUCCESS )
+        return r;
+
+    /* shift the rows to make room for the new row */
+    for (i = tv->table->row_count - 1; i > row; i--)
+    {
+        memmove(&(tv->table->data[i][0]),
+                &(tv->table->data[i - 1][0]), tv->row_size);
+        tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
     }
 
-    return r;
+    /* Re-set the persistence flag */
+    tv->table->data_persistent[row] = !temporary;
+    return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
 }
 
-static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
+static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    UINT r, row = -1;
+    UINT r, num_rows, num_cols, i;
 
-    TRACE("%p %p\n", tv, rec );
+    TRACE("%p %d\n", tv, row);
 
-    /* check that the key is unique - can we find a matching row? */
-    r = table_validate_new( tv, rec );
-    if( r != ERROR_SUCCESS )
+    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;
 
-    r = table_create_new_row( view, &row );
-    TRACE("insert_row returned %08x\n", r);
-    if( r != ERROR_SUCCESS )
+    num_rows = tv->table->row_count;
+    tv->table->row_count--;
+
+    /* 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;
+    }
+
+    if ( row == num_rows - 1 )
+        return ERROR_SUCCESS;
+
+    for (i = row + 1; i < num_rows; i++)
+    {
+        memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
+        tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
+    }
+
+    return ERROR_SUCCESS;
+}
+
+static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
+    UINT r, new_row;
+
+    /* 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;
+
+    r = msi_table_find_row(tv, rec, &new_row);
+    if (r != ERROR_SUCCESS)
+    {
+        ERR("can't find row to modify\n");
+        return ERROR_FUNCTION_FAILED;
+    }
+
+    /* the row cannot be changed */
+    if (row != new_row + 1)
+        return ERROR_FUNCTION_FAILED;
+
+    return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
+}
+
+static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
+    UINT r, row;
+
+    if (!tv->table)
+        return ERROR_INVALID_PARAMETER;
+
+    r = msi_table_find_row(tv, rec, &row);
+    if (r == ERROR_SUCCESS)
+        return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
+    else
+        return TABLE_insert_row( view, rec, -1, FALSE );
+}
+
+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);
+}
+
+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;
 
-    return msi_table_modify_row( tv, rec, row, ~0 );
+    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;
 }
 
 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
-                MSIRECORD *rec)
+                          MSIRECORD *rec, UINT row)
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
     UINT r;
 
     TRACE("%p %d %p\n", view, eModifyMode, rec );
 
-    if (!tv->table)
-    {
-        r = TABLE_execute( view, NULL );
-        if( r )
-            return r;
-    }
-
     switch (eModifyMode)
     {
+    case MSIMODIFY_DELETE:
+        r = modify_delete_row( view, rec );
+        break;
     case MSIMODIFY_VALIDATE_NEW:
         r = table_validate_new( tv, rec );
         break;
 
+    case MSIMODIFY_INSERT:
+        r = table_validate_new( tv, rec );
+        if (r != ERROR_SUCCESS)
+            break;
+        r = TABLE_insert_row( view, rec, -1, FALSE );
+        break;
+
     case MSIMODIFY_INSERT_TEMPORARY:
         r = table_validate_new( tv, rec );
         if (r != ERROR_SUCCESS)
             break;
-        r = TABLE_insert_row( view, rec );
+        r = TABLE_insert_row( view, rec, -1, TRUE );
         break;
 
     case MSIMODIFY_REFRESH:
-    case MSIMODIFY_INSERT:
+        r = msi_refresh_record( view, rec, row );
+        break;
+
     case MSIMODIFY_UPDATE:
+        r = msi_table_update( view, rec, row );
+        break;
+
     case MSIMODIFY_ASSIGN:
+        r = msi_table_assign( view, rec );
+        break;
+
     case MSIMODIFY_REPLACE:
     case MSIMODIFY_MERGE:
-    case MSIMODIFY_DELETE:
     case MSIMODIFY_VALIDATE:
     case MSIMODIFY_VALIDATE_FIELD:
     case MSIMODIFY_VALIDATE_DELETE:
@@ -1476,176 +1827,487 @@ static UINT TABLE_delete( struct tagMSIVIEW *view )
     TRACE("%p\n", view );
 
     tv->table = NULL;
+    tv->columns = NULL;
 
-    if( tv->columns )
+    if (tv->order)
     {
-        msi_free_colinfo( tv->columns, tv->num_cols );
-        msi_free( tv->columns );
+        msi_free( tv->order->reorder );
+        msi_free( tv->order );
+        tv->order = NULL;
     }
-    tv->columns = NULL;
 
-    msi_free( tv );
+    msi_free( tv );
+
+    return ERROR_SUCCESS;
+}
+
+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;
+        UINT num_rows = tv->table->row_count;
+        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++)
+        {
+            UINT row_value;
+
+            if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
+                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
+        entry = (*handle)->next;
+
+    while (entry && entry->value != val)
+        entry = entry->next;
+
+    *handle = entry;
+    if (!entry)
+        return ERROR_NO_MORE_ITEMS;
+
+    *row = entry->row;
+
+    return ERROR_SUCCESS;
+}
+
+static UINT TABLE_add_ref(struct tagMSIVIEW *view)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
+    UINT i;
+
+    TRACE("%p %d\n", view, tv->table->ref_count);
+
+    for (i = 0; i < tv->table->col_count; i++)
+    {
+        if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
+            InterlockedIncrement(&tv->table->colinfo[i].ref_count);
+    }
+
+    return InterlockedIncrement(&tv->table->ref_count);
+}
+
+static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
+    MSIRECORD *rec = NULL;
+    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);
+    columns->ops->delete(columns);
+    return r;
+}
+
+static UINT TABLE_release(struct tagMSIVIEW *view)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
+    INT ref = tv->table->ref_count;
+    UINT i, r;
+
+    TRACE("%p %d\n", view, ref);
+
+    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;
+            }
+        }
+    }
+
+    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;
+}
+
+static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
+                             LPCWSTR column, UINT type, BOOL hold)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
+    MSITABLE *msitable;
+    MSIRECORD *rec;
+    UINT r, i;
+
+    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, -1, FALSE);
+    if (r != ERROR_SUCCESS)
+        goto done;
+
+    msi_update_table_columns(tv->db, table);
+
+    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;
+        }
+    }
+
+done:
+    msiobj_release(&rec->hdr);
+    return r;
+}
+
+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;
+
+    if (rows == 0)
+        return ERROR_SUCCESS;
+
+    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;
 }
 
-static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
-    UINT val, UINT *row, MSIITERHANDLE *handle )
+static UINT TABLE_drop(struct tagMSIVIEW *view)
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    const MSICOLUMNHASHENTRY *entry;
-
-    TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
-
-    if( !tv->table )
-        return ERROR_INVALID_PARAMETER;
+    MSIVIEW *tables = NULL;
+    MSIRECORD *rec = NULL;
+    UINT r, row;
+    INT i;
 
-    if( (col==0) || (col > tv->num_cols) )
-        return ERROR_INVALID_PARAMETER;
+    TRACE("dropping table %s\n", debugstr_w(tv->name));
 
-    if( !tv->columns[col-1].hash_table )
+    for (i = tv->table->col_count - 1; i >= 0; i--)
     {
-        UINT i;
-        UINT num_rows = tv->table->row_count;
-        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;
+        r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
+                                tv->table->colinfo[i].number);
+        if (r != ERROR_SUCCESS)
+            return r;
+    }
 
-        memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
-        tv->columns[col-1].hash_table = hash_table;
+    rec = MSI_CreateRecord(1);
+    if (!rec)
+        return ERROR_OUTOFMEMORY;
 
-        new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
+    MSI_RecordSetStringW(rec, 1, tv->name);
 
-        for (i = 0; i < num_rows; i++, new_entry++)
-        {
-            UINT row_value, n;
-            UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
-            n = bytes_per_column( &tv->columns[col-1] );
-            switch( n )
-            {
-            case 4:
-                offset = tv->columns[col-1].offset/2;
-                row_value = tv->table->data[i][offset] + 
-                    (tv->table->data[i][offset + 1] << 16);
-                break;
-            case 2:
-                offset = tv->columns[col-1].offset/2;
-                row_value = tv->table->data[i][offset];
-                break;
-            default:
-                ERR("oops! what is %d bytes per column?\n", n );
-                continue;
-            }
+    r = TABLE_CreateView(tv->db, szTables, &tables);
+    if (r != ERROR_SUCCESS)
+        return r;
 
-            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;
-        }
-    }
+    r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
+    if (r != ERROR_SUCCESS)
+        goto done;
 
-    if( !*handle )
-        entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
-    else
-        entry = ((const MSICOLUMNHASHENTRY *)*handle)->next;
+    r = TABLE_delete_row(tables, row);
+    if (r != ERROR_SUCCESS)
+        goto done;
 
-    while (entry && entry->value != val)
-        entry = entry->next;
+    list_remove(&tv->table->entry);
+    free_table(tv->table);
+    TABLE_delete(view);
 
-    *handle = (MSIITERHANDLE)entry;
-    if (!entry)
-        return ERROR_NO_MORE_ITEMS;
+done:
+    msiobj_release(&rec->hdr);
+    tables->ops->delete(tables);
 
-    *row = entry->row;
-    return ERROR_SUCCESS;
+    return r;
 }
 
-
 static const MSIVIEWOPS table_ops =
 {
     TABLE_fetch_int,
     TABLE_fetch_stream,
-    TABLE_set_int,
+    TABLE_get_row,
+    TABLE_set_row,
     TABLE_insert_row,
+    TABLE_delete_row,
     TABLE_execute,
     TABLE_close,
     TABLE_get_dimensions,
     TABLE_get_column_info,
     TABLE_modify,
     TABLE_delete,
-    TABLE_find_matching_rows
+    TABLE_find_matching_rows,
+    TABLE_add_ref,
+    TABLE_release,
+    TABLE_add_column,
+    TABLE_remove_column,
+    TABLE_sort,
+    TABLE_drop,
 };
 
 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
 {
     MSITABLEVIEW *tv ;
-    UINT r, sz, column_count;
-    MSICOLUMNINFO *columns;
-
-    TRACE("%p %s %p\n", db, debugstr_w(name), view );
+    UINT r, sz;
 
-    /* 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;
+    static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
+    static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
 
-    /* if there's no columns, there's no table */
-    if( column_count == 0 )
-        return ERROR_INVALID_PARAMETER;
+    TRACE("%p %s %p\n", db, debugstr_w(name), view );
 
-    TRACE("Table found\n");
+    if ( !lstrcmpW( name, Streams ) )
+        return STREAMS_CreateView( db, view );
+    else if ( !lstrcmpW( name, Storages ) )
+        return STORAGES_CreateView( db, view );
 
     sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
     tv = msi_alloc_zero( sz );
     if( !tv )
         return ERROR_FUNCTION_FAILED;
-    
-    columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
-    if( !columns )
-    {
-        msi_free( tv );
-        return ERROR_FUNCTION_FAILED;
-    }
 
-    r = get_tablecolumns( db, name, columns, &column_count );
+    r = get_table( db, name, &tv->table );
     if( r != ERROR_SUCCESS )
     {
-        msi_free( columns );
         msi_free( tv );
-        return ERROR_FUNCTION_FAILED;
+        WARN("table not found\n");
+        return r;
     }
 
-    TRACE("Table has %d columns\n", column_count);
+    TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
 
     /* fill the structure */
     tv->view.ops = &table_ops;
     tv->db = db;
-    tv->columns = columns;
-    tv->num_cols = column_count;
-    tv->table = NULL;
-    tv->row_size = msi_table_get_row_size( columns, column_count );
+    tv->columns = tv->table->colinfo;
+    tv->num_cols = tv->table->col_count;
+    tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
 
     TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
 
@@ -1662,7 +2324,7 @@ UINT MSI_CommitTables( MSIDATABASE *db )
 
     TRACE("%p\n",db);
 
-    r = save_string_table( db );
+    r = msi_save_string_table( db->strings, db->storage );
     if( r != ERROR_SUCCESS )
     {
         WARN("failed to save string table r=%08x\n",r);
@@ -1688,19 +2350,102 @@ UINT MSI_CommitTables( MSIDATABASE *db )
 
 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
 {
+    MSITABLE *t;
+    UINT r;
+
+    TRACE("%p %s\n", db, debugstr_w(table));
+
     if (!table)
         return MSICONDITION_ERROR;
 
-    return MSICONDITION_FALSE;
+    r = get_table( db, table, &t );
+    if (r != ERROR_SUCCESS)
+        return MSICONDITION_NONE;
+
+    return t->persistent;
+}
+
+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;
+}
+
+static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
+{
+    static const WCHAR szDot[] = { '.', 0 };
+    LPWSTR stname = NULL, sval, p;
+    DWORD len;
+    UINT i, r;
+
+    TRACE("%p %p\n", tv, rec);
+
+    len = lstrlenW( tv->name ) + 1;
+    stname = msi_alloc( len*sizeof(WCHAR) );
+    if ( !stname )
+    {
+       r = ERROR_OUTOFMEMORY;
+       goto err;
+    }
+
+    lstrcpyW( stname, tv->name );
+
+    for ( i = 0; i < tv->num_cols; i++ )
+    {
+        if ( tv->columns[i].type & MSITYPE_KEY )
+        {
+            sval = msi_dup_record_field( rec, i + 1 );
+            if ( !sval )
+            {
+                r = ERROR_OUTOFMEMORY;
+                goto err;
+            }
+
+            len += lstrlenW( szDot ) + lstrlenW ( sval );
+            p = msi_realloc ( stname, len*sizeof(WCHAR) );
+            if ( !p )
+            {
+                r = ERROR_OUTOFMEMORY;
+                goto err;
+            }
+            stname = p;
+
+            lstrcatW( stname, szDot );
+            lstrcatW( stname, sval );
+
+            msi_free( sval );
+        }
+        else
+            continue;
+    }
+
+    *pstname = encode_streamname( FALSE, stname );
+    msi_free( stname );
+
+    return ERROR_SUCCESS;
+
+err:
+    msi_free ( stname );
+    *pstname = NULL;
+    return r;
 }
 
-static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
+static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
+                                            IStorage *stg,
+                                            const BYTE *rawdata, UINT bytes_per_strref )
 {
     UINT i, val, ofs = 0;
-    USHORT mask = *rawdata++;
+    USHORT mask;
     MSICOLUMNINFO *columns = tv->columns;
     MSIRECORD *rec;
 
+    mask = rawdata[0] | (rawdata[1] << 8);
+    rawdata += 2;
+
     rec = MSI_CreateRecord( tv->num_cols );
     if( !rec )
         return rec;
@@ -1708,43 +2453,66 @@ static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st,
     TRACE("row ->\n");
     for( i=0; i<tv->num_cols; i++ )
     {
-        UINT n = bytes_per_column( &columns[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;
 
-        switch( n )
+        if( MSITYPE_IS_BINARY(tv->columns[i].type) )
         {
-        case 2:
-            val = rawdata[ofs];
-            if( (columns[i].type & MSITYPE_STRING) &&
-                ! MSITYPE_IS_BINARY(tv->columns[i].type) )
-            {
-                LPCWSTR sval = msi_string_lookup_id( st, val );
-                MSI_RecordSetStringW( rec, i+1, sval );
-                TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
-            }
-            else
+            LPWSTR encname;
+            IStream *stm = NULL;
+            UINT r;
+
+            ofs += bytes_per_column( tv->db, &columns[i] );
+
+            r = msi_record_encoded_stream_name( tv, rec, &encname );
+            if ( r != ERROR_SUCCESS )
+                return NULL;
+
+            r = IStorage_OpenStream( stg, encname, NULL,
+                     STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
+            msi_free( encname );
+            if ( r != ERROR_SUCCESS )
+                return NULL;
+
+            MSI_RecordSetStream( rec, i+1, stm );
+            TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
+        }
+        else if( columns[i].type & MSITYPE_STRING )
+        {
+            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( tv->db, &columns[i] );
+            switch( n )
             {
+            case 2:
+                val = read_raw_int(rawdata, ofs, n);
                 if (val)
                     MSI_RecordSetInteger( rec, i+1, val^0x8000 );
                 TRACE(" field %d [0x%04x]\n", i+1, val );
+                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;
             }
-            break;
-        case 4:
-            val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
-            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;
+            ofs += n;
         }
-        ofs += n/2;
     }
     return rec;
 }
@@ -1767,7 +2535,7 @@ static void dump_record( MSIRECORD *rec )
     }
 }
 
-static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
+static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
 {
     LPCWSTR sval;
     UINT i;
@@ -1779,7 +2547,7 @@ static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
     }
 }
 
-static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
+static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
 {
     LPCWSTR str;
     UINT i, r, *data;
@@ -1810,7 +2578,10 @@ static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
         else
         {
             data[i] = MSI_RecordGetInteger( rec, i+1 );
-            if ((tv->columns[i].type&0xff) == 2)
+
+            if (data[i] == MSI_NULL_INTEGER)
+                data[i] = 0;
+            else if ((tv->columns[i].type&0xff) == 2)
                 data[i] += 0x8000;
             else
                 data[i] += 0x80000000;
@@ -1819,7 +2590,7 @@ static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
     return data;
 }
 
-static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
+static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
 {
     UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
 
@@ -1856,7 +2627,7 @@ static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
     data = msi_record_to_row( tv, rec );
     if( !data )
         return r;
-    for( i=0; i<tv->table->row_count; i++ )
+    for( i = 0; i < tv->table->row_count; i++ )
     {
         r = msi_row_matches( tv, i, data );
         if( r == ERROR_SUCCESS )
@@ -1869,30 +2640,35 @@ static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
     return r;
 }
 
-static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
+typedef struct
 {
-    UINT i;
-    for( i=1; i<=tv->num_cols; i++ )
-        tv->view.ops->set_int( &tv->view, row, i, 0 );
-    return ERROR_SUCCESS;
-}
+    struct list entry;
+    LPWSTR name;
+} TRANSFORMDATA;
 
 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
-                                      string_table *st, LPCWSTR name )
+                                      string_table *st, TRANSFORMDATA *transform,
+                                      UINT bytes_per_strref )
 {
     UINT rawsize = 0;
-    USHORT *rawdata = NULL;
+    BYTE *rawdata = NULL;
     MSITABLEVIEW *tv = NULL;
     UINT r, n, sz, i, mask;
     MSIRECORD *rec = NULL;
     UINT colcol = 0;
     WCHAR coltable[32];
+    LPWSTR name;
+
+    if (!transform)
+        return ERROR_SUCCESS;
+
+    name = transform->name;
 
     coltable[0] = 0;
     TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
 
     /* read the transform data */
-    read_stream_data( stg, name, &rawdata, &rawsize );
+    read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
     if ( !rawdata )
     {
         TRACE("table %s empty\n", debugstr_w(name) );
@@ -1913,9 +2689,9 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
 
     /* interpret the data */
     r = ERROR_SUCCESS;
-    for( n=0; n < (rawsize/2);  )
+    for( n=0; n < rawsize;  )
     {
-        mask = rawdata[n];
+        mask = rawdata[n] | (rawdata[n+1] << 8);
 
         if (mask&1)
         {
@@ -1923,23 +2699,37 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
              * if the low bit is set, columns are continuous and
              * the number of columns is specified in the high byte
              */
-            sz = 2 + tv->row_size;
+            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->db, &tv->columns[i] );
+            }
         }
         else
         {
             /*
-             * If the low bit is not set, rowdata[n] is a bitmask.
+             * If the low bit is not set, mask is a bitmask.
              * Excepting for key fields, which are always present,
              *  each bit indicates that a field is present in the transform record.
              *
-             * rawdata[n] == 0 is a special case ... only the keys will be present
+             * mask == 0 is a special case ... only the keys will be present
              * 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))
-                    sz += bytes_per_column( &tv->columns[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->db, &tv->columns[i] );
+                }
             }
         }
 
@@ -1947,43 +2737,50 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
         if ( (n+sz) > rawsize )
         {
             ERR("borked.\n");
-            dump_table( st, rawdata, rawsize );
+            dump_table( st, (USHORT *)rawdata, rawsize );
             break;
         }
 
-        rec = msi_get_transform_record( tv, st, &rawdata[n] );
+        rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
         if (rec)
         {
             if ( mask & 1 )
             {
+                WCHAR table[32];
+                DWORD sz = 32;
+                UINT number = MSI_NULL_INTEGER;
+
                 TRACE("inserting record\n");
 
-                /*
-                 * Native msi seems writes nul into the
-                 * Number (2nd) column of the _Columns table.
-                 * Not sure that it's deliberate...
-                 */
                 if (!lstrcmpW(name, szColumns))
                 {
-                    WCHAR table[32];
-                    DWORD sz = 32;
-
                     MSI_RecordGetStringW( rec, 1, table, &sz );
+                    number = MSI_RecordGetInteger( rec, 2 );
 
-                    /* reset the column number on a new table */
-                    if ( lstrcmpW(coltable, table) )
+                    /*
+                     * 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 )
                     {
-                        colcol = 0;
-                        lstrcpyW( coltable, table );
+                        /* 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 );
                     }
-
-                    /* fix nul column numbers */
-                    MSI_RecordSetInteger( rec, 2, ++colcol );
                 }
 
-                r = TABLE_insert_row( &tv->view, rec );
+                r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
                 if (r != ERROR_SUCCESS)
-                    ERR("insert row failed\n");
+                    WARN("insert row failed\n");
+
+                if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
+                    msi_update_table_columns( db, table );
             }
             else
             {
@@ -1991,23 +2788,23 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
 
                 r = msi_table_find_row( tv, rec, &row );
                 if (r != ERROR_SUCCESS)
-                    ERR("no matching row to transform\n");
+                    WARN("no matching row to transform\n");
                 else if ( mask )
                 {
                     TRACE("modifying row [%d]:\n", row);
-                    msi_table_modify_row( tv, rec, row, mask );
+                    TABLE_set_row( &tv->view, row, rec, mask );
                 }
                 else
                 {
                     TRACE("deleting row [%d]:\n", row);
-                    msi_delete_row( tv, row );
+                    TABLE_delete_row( &tv->view, row );
                 }
             }
             if( TRACE_ON(msidb) ) dump_record( rec );
             msiobj_release( &rec->hdr );
         }
 
-        n += sz/2;
+        n += sz;
     }
 
 err:
@@ -2026,17 +2823,19 @@ err:
  */
 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
 {
+    struct list transforms;
     IEnumSTATSTG *stgenum = NULL;
+    TRANSFORMDATA *transform;
+    TRANSFORMDATA *tables = NULL, *columns = NULL;
     HRESULT r;
     STATSTG stat;
-    ULONG count;
-    WCHAR name[0x40];
     string_table *strings;
     UINT ret = ERROR_FUNCTION_FAILED;
+    UINT bytes_per_strref;
 
     TRACE("%p %p\n", db, stg );
 
-    strings = load_string_table( stg );
+    strings = msi_load_string_table( stg, &bytes_per_strref );
     if( !strings )
         goto end;
 
@@ -2044,52 +2843,90 @@ UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
     if( FAILED( r ) )
         goto end;
 
-    /*
-     * Apply _Tables and _Coluimns transforms first so that
-     * the table metadata is correct, and empty tables exist.
-     */
-    ret = msi_table_load_transform( db, stg, strings, szTables );
-    if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
-        goto end;
-
-    ret = msi_table_load_transform( db, stg, strings, szColumns );
-    if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
-        goto end;
+    list_init(&transforms);
 
-    ret = ERROR_SUCCESS;
-
-    while( r == ERROR_SUCCESS )
+    while ( TRUE )
     {
-        count = 0;
+        MSITABLEVIEW *tv = NULL;
+        WCHAR name[0x40];
+        ULONG count = 0;
+
         r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
-        if( FAILED( r ) || !count )
+        if ( FAILED( r ) || !count )
             break;
 
         decode_streamname( stat.pwcsName, name );
+        CoTaskMemFree( stat.pwcsName );
         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));
 
-        if ( !lstrcmpW( name+1, szStringPool ) ||
-             !lstrcmpW( name+1, szStringData ) ||
-             !lstrcmpW( name+1, szColumns ) ||
-             !lstrcmpW( name+1, szTables ) )
+        /* 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;
+        }
 
-        ret = msi_table_load_transform( db, stg, strings, name+1 );
+        tv->view.ops->delete( &tv->view );
     }
 
-    if ( ret == ERROR_SUCCESS )
+    /*
+     * Apply _Tables and _Columns transforms first so that
+     * the table metadata is correct, and empty tables exist.
+     */
+    ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
+    if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
+        goto end;
+
+    ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
+    if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
+        goto end;
+
+    ret = ERROR_SUCCESS;
+
+    while ( !list_empty( &transforms ) )
     {
-        MSITRANSFORM *t;
+        transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
+
+        if ( lstrcmpW( transform->name, szColumns ) &&
+             lstrcmpW( transform->name, szTables ) &&
+             ret == ERROR_SUCCESS )
+        {
+            ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
+        }
 
-        t = msi_alloc( sizeof *t );
-        t->stg = stg;
-        IStorage_AddRef( stg );
-        list_add_tail( &db->transforms, &t->entry );
+        list_remove( &transform->entry );
+        msi_free( transform->name );
+        msi_free( transform );
     }
 
+    if ( ret == ERROR_SUCCESS )
+        append_storage_to_db( db, stg );
+
 end:
     if ( stgenum )
         IEnumSTATSTG_Release( stgenum );
@@ -2099,6 +2936,16 @@ end:
     return ret;
 }
 
+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 );
+}
+
 void msi_free_transforms( MSIDATABASE *db )
 {
     while( !list_empty( &db->transforms ) )
@@ -2110,3 +2957,29 @@ void msi_free_transforms( MSIDATABASE *db )
         msi_free( t );
     }
 }
+
+static UINT table_find_insert_idx (MSIVIEW *view, LPCWSTR name, INT *pidx)
+{
+    UINT r, name_id, row_id;
+    INT idx;
+    MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
+
+    TRACE ("%p %s\n", view, debugstr_w(name));
+
+    r = msi_string2idW(tv->db->strings, name, &name_id);
+    if (r != ERROR_SUCCESS)
+    {
+        *pidx = -1;
+        return r;
+    }
+
+    for( idx = 0; idx < tv->table->row_count; idx++ )
+    {
+        r = TABLE_fetch_int( &tv->view, idx, 1, &row_id );
+        if (row_id > name_id)
+            break;
+    }
+
+    *pidx = idx;
+    return ERROR_SUCCESS;
+}