- Merge from trunk
[reactos.git] / dll / win32 / msi / table.c
index 13c61d4..3e456ed 100644 (file)
@@ -42,7 +42,6 @@
 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
 
 #define MSITABLE_HASH_TABLE_SIZE 37
-#define LONG_STR_BYTES 3
 
 typedef struct tagMSICOLUMNHASHENTRY
 {
@@ -59,6 +58,7 @@ typedef struct tagMSICOLUMNINFO
     UINT   type;
     UINT   offset;
     INT    ref_count;
+    BOOL   temporary;
     MSICOLUMNHASHENTRY **hash_table;
 } MSICOLUMNINFO;
 
@@ -72,22 +72,16 @@ typedef struct tagMSIORDERINFO
 struct tagMSITABLE
 {
     BYTE **data;
+    BOOL *data_persistent;
     UINT row_count;
-    BYTE **nonpersistent_data;
-    UINT nonpersistent_row_count;
     struct list entry;
     MSICOLUMNINFO *colinfo;
     UINT col_count;
-    BOOL persistent;
+    MSICONDITION persistent;
     INT ref_count;
     WCHAR name[1];
 };
 
-typedef struct tagMSITRANSFORM {
-    struct list entry;
-    IStorage *stg;
-} MSITRANSFORM;
-
 static const WCHAR szStringData[] = {
     '_','S','t','r','i','n','g','D','a','t','a',0 };
 static const WCHAR szStringPool[] = {
@@ -101,46 +95,42 @@ 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 };
 
-/* These tables are written into (the .hash_table part).
- * Do not mark them const.
- */
-static MSICOLUMNINFO _Columns_cols[4] = {
-    { szColumns, 1, szTable,  MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, NULL },
-    { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2,     2, 0, NULL },
-    { szColumns, 3, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, NULL },
-    { szColumns, 4, szType,   MSITYPE_VALID | 2,                   6, 0, NULL },
+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 MSICOLUMNINFO _Tables_cols[1] = {
-    { szTables,  1, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 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( MSICOLUMNINFO *colinfo, DWORD count );
+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 );
 
-
-void msi_table_set_strref(UINT bytes_per_strref)
-{
-    _Columns_cols[0].offset = 0;
-    _Columns_cols[1].offset = bytes_per_strref;
-    _Columns_cols[2].offset = _Columns_cols[1].offset + sizeof(USHORT);
-    _Columns_cols[3].offset = _Columns_cols[2].offset + bytes_per_strref;
-}
-
-static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
+static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col, UINT bytes_per_strref )
 {
     if( MSITYPE_IS_BINARY(col->type) )
         return 2;
+
     if( col->type & MSITYPE_STRING )
-        return _Columns_cols[1].offset;
-    if( (col->type & 0xff) > 4 )
+        return bytes_per_strref;
+
+    if( (col->type & 0xff) <= 2)
+        return 2;
+
+    if( (col->type & 0xff) != 4 )
         ERR("Invalid column size!\n");
-    return col->type & 0xff;
+
+    return 4;
 }
 
 static int utf2mime(int x)
@@ -336,89 +326,6 @@ end:
     return ret;
 }
 
-UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
-{
-    LPWSTR encname;
-    HRESULT r;
-
-    encname = encode_streamname(FALSE, stname);
-
-    TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
-
-    r = IStorage_OpenStream(db->storage, encname, NULL, 
-            STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
-    if( FAILED( r ) )
-    {
-        MSITRANSFORM *transform;
-
-        LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
-        {
-            TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
-            r = IStorage_OpenStream( transform->stg, encname, NULL, 
-                    STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
-            if (SUCCEEDED(r))
-                break;
-        }
-    }
-
-    msi_free( encname );
-
-    return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
-}
-
-UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
-                              USHORT **pdata, UINT *psz )
-{
-    HRESULT r;
-    UINT ret = ERROR_FUNCTION_FAILED;
-    VOID *data;
-    ULONG sz, count;
-    IStream *stm = NULL;
-    STATSTG stat;
-
-    r = db_get_raw_stream( db, stname, &stm );
-    if( r != ERROR_SUCCESS)
-        return ret;
-    r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
-    if( FAILED( r ) )
-    {
-        WARN("open stream failed r = %08x!\n", r);
-        goto end;
-    }
-
-    if( stat.cbSize.QuadPart >> 32 )
-    {
-        WARN("Too big!\n");
-        goto end;
-    }
-        
-    sz = stat.cbSize.QuadPart;
-    data = msi_alloc( sz );
-    if( !data )
-    {
-        WARN("couldn't allocate memory r=%08x!\n", r);
-        ret = ERROR_NOT_ENOUGH_MEMORY;
-        goto end;
-    }
-        
-    r = IStream_Read(stm, data, sz, &count );
-    if( FAILED( r ) || ( count != sz ) )
-    {
-        msi_free( data );
-        WARN("read stream failed r = %08x!\n", r);
-        goto end;
-    }
-
-    *pdata = data;
-    *psz = sz;
-    ret = ERROR_SUCCESS;
-
-end:
-    IStream_Release( stm );
-
-    return ret;
-}
-
 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
                         LPCVOID data, UINT sz, BOOL bTable )
 {
@@ -485,35 +392,39 @@ static void free_table( MSITABLE *table )
     for( i=0; i<table->row_count; i++ )
         msi_free( table->data[i] );
     msi_free( table->data );
-    for( i=0; i<table->nonpersistent_row_count; i++ )
-        msi_free( table->nonpersistent_data[i] );
-    msi_free( table->nonpersistent_data );
-    if( (table->colinfo != _Tables_cols) &&
-        (table->colinfo != _Columns_cols) )
-    {
-        msi_free_colinfo( table->colinfo, table->col_count );
-        msi_free( table->colinfo );
-    }
+    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, UINT bytes_per_strref )
 {
-    const MSICOLUMNINFO *last_col = &cols[count-1];
+    const MSICOLUMNINFO *last_col;
+
     if (!count)
         return 0;
-    return last_col->offset + bytes_per_column( last_col );
+
+    if (bytes_per_strref != LONG_STR_BYTES)
+    {
+        UINT i, size = 0;
+        for (i = 0; i < count; i++) size += bytes_per_column( db, &cols[i], bytes_per_strref );
+        return size;
+    }
+    last_col = &cols[count - 1];
+    return last_col->offset + bytes_per_column( db, last_col, bytes_per_strref );
 }
 
 /* add this table to the list of cached tables in the database */
-static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
+static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
 {
     BYTE *rawdata = NULL;
-    UINT rawsize = 0, i, j, row_size = 0;
+    UINT rawsize = 0, i, j, row_size, row_size_mem;
 
     TRACE("%s\n",debugstr_w(t->name));
 
-    row_size = msi_table_get_row_size( t->colinfo, t->col_count );
+    row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, db->bytes_per_strref );
+    row_size_mem = msi_table_get_row_size( db, t->colinfo, t->col_count, LONG_STR_BYTES );
 
     /* if we can't read the table, just assume that it's empty */
     read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
@@ -532,19 +443,25 @@ static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
     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 );
-    for( i=0; i<t->row_count; i++ )
+    for (i = 0; i < t->row_count; i++)
     {
-        t->data[i] = msi_alloc( row_size );
+        UINT ofs = 0, ofs_mem = 0;
+
+        t->data[i] = msi_alloc( row_size_mem );
         if( !t->data[i] )
             goto err;
+        t->data_persistent[i] = TRUE;
 
-        for( j=0; j<t->col_count; j++ )
+        for (j = 0; j < t->col_count; j++)
         {
-            UINT ofs = t->colinfo[j].offset;
-            UINT n = bytes_per_column( &t->colinfo[j] );
+            UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
+            UINT n = bytes_per_column( db, &t->colinfo[j], db->bytes_per_strref );
             UINT k;
 
             if ( n != 2 && n != 3 && n != 4 )
@@ -552,9 +469,23 @@ static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
                 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];
+            if (t->colinfo[j].type & MSITYPE_STRING && n < m)
+            {
+                for (k = 0; k < m; k++)
+                {
+                    if (k < n)
+                        t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
+                    else
+                        t->data[i][ofs_mem + k] = 0;
+                }
+            }
+            else
+            {
+                for (k = 0; k < n; k++)
+                    t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
+            }
+            ofs_mem += m;
+            ofs += n;
         }
     }
 
@@ -598,6 +529,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;
@@ -616,13 +549,12 @@ static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO
     }
 
     *pcols = columns;
-    *pcount = column_count;
 
     return r;
 }
 
 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
-                       BOOL persistent, MSITABLE **table_ret)
+                       MSICONDITION persistent, MSITABLE **table_ret)
 {
     UINT r, nField;
     MSIVIEW *tv = NULL;
@@ -645,8 +577,7 @@ UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
     table->ref_count = 1;
     table->row_count = 0;
     table->data = NULL;
-    table->nonpersistent_row_count = 0;
-    table->nonpersistent_data = NULL;
+    table->data_persistent = NULL;
     table->colinfo = NULL;
     table->col_count = 0;
     table->persistent = persistent;
@@ -671,8 +602,9 @@ UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
         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( table->colinfo, table->col_count);
+    table_calc_column_offsets( db, table->colinfo, table->col_count);
 
     r = TABLE_CreateView( db, szTables, &tv );
     TRACE("CreateView returned %x\n", r);
@@ -695,7 +627,7 @@ UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
     if( r )
         goto err;
 
-    r = tv->ops->insert_row( tv, rec, !persistent );
+    r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
     TRACE("insert_row returned %x\n", r);
     if( r )
         goto err;
@@ -706,7 +638,7 @@ UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
     msiobj_release( &rec->hdr );
     rec = NULL;
 
-    if( persistent )
+    if( persistent != MSICONDITION_FALSE )
     {
         /* add each column to the _Columns table */
         r = TABLE_CreateView( db, szColumns, &tv );
@@ -745,7 +677,7 @@ UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
             if( r )
                 goto err;
 
-            r = tv->ops->insert_row( tv, rec, FALSE );
+            r = tv->ops->insert_row( tv, rec, -1, FALSE );
             if( r )
                 goto err;
 
@@ -793,35 +725,23 @@ static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
 
     table->row_count = 0;
     table->data = NULL;
-    table->nonpersistent_row_count = 0;
-    table->nonpersistent_data = NULL;
+    table->data_persistent = NULL;
     table->colinfo = NULL;
     table->col_count = 0;
-    table->persistent = TRUE;
+    table->persistent = MSICONDITION_TRUE;
     lstrcpyW( table->name, name );
 
-    /* these two tables are special - we know the column types already */
-    if( !lstrcmpW( name, szColumns ) )
-    {
-        table->colinfo = _Columns_cols;
-        table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
-    }
-    else if( !lstrcmpW( name, szTables ) )
-    {
-        table->colinfo = _Tables_cols;
-        table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
-    }
-    else
+    if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
+        table->persistent = MSICONDITION_NONE;
+
+    r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
+    if (r != ERROR_SUCCESS)
     {
-        r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
-        if (r != ERROR_SUCCESS)
-        {
-            free_table ( table );
-            return r;
-        }
+        free_table ( table );
+        return r;
     }
 
-    r = read_table_from_storage( table, db->storage );
+    r = read_table_from_storage( db, table, db->storage );
     if( r != ERROR_SUCCESS )
     {
         free_table( table );
@@ -833,20 +753,38 @@ static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
     return ERROR_SUCCESS;
 }
 
-static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
+static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
 {
-    BYTE *rawdata = NULL, *p;
-    UINT rawsize, r, i, j, row_size;
+    UINT ret = 0, i;
+
+    for (i = 0; i < bytes; i++)
+        ret += data[row][col + i] << i * 8;
+
+    return ret;
+}
+
+static UINT save_table( MSIDATABASE *db, const MSITABLE *t, UINT bytes_per_strref )
+{
+    BYTE *rawdata = NULL;
+    UINT rawsize, r, i, j, row_size, row_count;
 
     /* Nothing to do for non-persistent tables */
-    if( !t->persistent )
+    if( t->persistent == MSICONDITION_FALSE )
         return ERROR_SUCCESS;
 
     TRACE("Saving %s\n", debugstr_w( t->name ) );
 
-    row_size = msi_table_get_row_size( t->colinfo, t->col_count );
-
-    rawsize = t->row_count * row_size;
+    row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, bytes_per_strref );
+    row_count = t->row_count;
+    for (i = 0; i < t->row_count; i++)
+    {
+        if (!t->data_persistent[i])
+        {
+            row_count = 1; /* yes, this is bizarre */
+            break;
+        }
+    }
+    rawsize = row_count * row_size;
     rawdata = msi_alloc_zero( rawsize );
     if( !rawdata )
     {
@@ -854,21 +792,42 @@ static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
         goto err;
     }
 
-    p = rawdata;
-    for( i=0; i<t->col_count; i++ )
+    rawsize = 0;
+    for (i = 0; i < t->row_count; i++)
     {
-        for( j=0; j<t->row_count; j++ )
+        UINT ofs = 0, ofs_mem = 0;
+
+        if (!t->data_persistent[i]) break;
+
+        for (j = 0; j < t->col_count; j++)
         {
-            UINT offset = t->colinfo[i].offset;
+            UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
+            UINT n = bytes_per_column( db, &t->colinfo[j], bytes_per_strref );
+            UINT k;
 
-            *p++ = t->data[j][offset];
-            *p++ = t->data[j][offset + 1];
-            if( 4 == bytes_per_column( &t->colinfo[i] ) )
+            if (n != 2 && n != 3 && n != 4)
+            {
+                ERR("oops - unknown column width %d\n", n);
+                goto err;
+            }
+            if (t->colinfo[j].type & MSITYPE_STRING && n < m)
+            {
+                UINT id = read_table_int( t->data, i, ofs_mem, LONG_STR_BYTES );
+                if (id > 1 << bytes_per_strref * 8)
+                {
+                    ERR("string id %u out of range\n", id);
+                    r = ERROR_FUNCTION_FAILED;
+                    goto err;
+                }
+            }
+            for (k = 0; k < n; k++)
             {
-                *p++ = t->data[j][offset + 2];
-                *p++ = t->data[j][offset + 3];
+                rawdata[ofs * row_count + i * n + k] = t->data[i][ofs_mem + k];
             }
+            ofs_mem += m;
+            ofs += n;
         }
+        rawsize += row_size;
     }
 
     TRACE("writing %d bytes\n", rawsize);
@@ -876,11 +835,10 @@ static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
 
 err:
     msi_free( rawdata );
-
     return r;
 }
 
-static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
+static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo, DWORD count )
 {
     DWORD i;
 
@@ -889,7 +847,7 @@ static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
          assert( (i+1) == colinfo[ i ].number );
          if (i)
              colinfo[i].offset = colinfo[ i - 1 ].offset
-                               + bytes_per_column( &colinfo[ i - 1 ] );
+                               + bytes_per_column( db, &colinfo[ i - 1 ], LONG_STR_BYTES );
          else
              colinfo[i].offset = 0;
          TRACE("column %d is [%s] with type %08x ofs %d\n",
@@ -898,7 +856,8 @@ static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
     }
 }
 
-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;
@@ -930,7 +889,7 @@ static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT
         if( colinfo && (i >= *sz) )
             break;
     }
-    table_calc_column_offsets( colinfo, n );
+    table_calc_column_offsets( db, colinfo, n );
     *sz = n;
     return ERROR_SUCCESS;
 }
@@ -952,16 +911,6 @@ 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)
 {
@@ -971,7 +920,7 @@ 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;
 
@@ -999,12 +948,12 @@ static UINT get_tablecolumns( MSIDATABASE *db,
     count = table->row_count;
     for( i=0; i<count; i++ )
     {
-        if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
+        if( read_table_int(table->data, i, 0, LONG_STR_BYTES) != table_id )
             continue;
         if( colinfo )
         {
-            UINT id = read_table_int(table->data, i, _Columns_cols[2].offset, db->bytes_per_strref);
-            UINT col = read_table_int(table->data, i, _Columns_cols[1].offset, sizeof(USHORT)) - (1<<15);
+            UINT id = read_table_int(table->data, i, table->colinfo[2].offset, LONG_STR_BYTES);
+            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)
@@ -1023,7 +972,9 @@ 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 = read_table_int(table->data, i, _Columns_cols[3].offset, sizeof(USHORT)) - (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;
@@ -1040,7 +991,7 @@ static UINT get_tablecolumns( MSIDATABASE *db,
         return ERROR_FUNCTION_FAILED;
     }
 
-    table_calc_column_offsets( colinfo, n );
+    table_calc_column_offsets( db, colinfo, n );
     *sz = n;
 
     return ERROR_SUCCESS;
@@ -1049,15 +1000,24 @@ static UINT get_tablecolumns( MSIDATABASE *db,
 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
 {
     MSITABLE *table;
+    LPWSTR tablename;
     UINT size, offset, old_count;
     UINT n;
 
-    table = find_cached_table( db, name );
+    /* We may free name in msi_free_colinfo. */
+    tablename = strdupW( name );
+
+    table = find_cached_table( db, tablename );
     old_count = table->col_count;
+    msi_free_colinfo( table->colinfo, table->col_count );
     msi_free( table->colinfo );
-    table_get_column_info( db, name, &table->colinfo, &table->col_count );
+    table->colinfo = NULL;
+
+    table_get_column_info( db, tablename, &table->colinfo, &table->col_count );
+    if (!table->col_count)
+        goto done;
 
-    size = msi_table_get_row_size( table->colinfo, table->col_count );
+    size = msi_table_get_row_size( db, table->colinfo, table->col_count, LONG_STR_BYTES );
     offset = table->colinfo[table->col_count - 1].offset;
 
     for ( n = 0; n < table->row_count; n++ )
@@ -1066,17 +1026,22 @@ static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
         if (old_count < table->col_count)
             memset( &table->data[n][offset], 0, size - offset );
     }
+
+done:
+    msi_free(tablename);
 }
 
 /* try to find the table name in the _Tables table */
 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
 {
-    UINT r, table_id = 0, i, count;
-    MSITABLE *table = NULL;
+    UINT r, table_id, i;
+    MSITABLE *table;
 
-    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 );
@@ -1093,23 +1058,11 @@ BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
         return FALSE;
     }
 
-    count = table->row_count;
-    for( i=0; i<count; i++ )
-        if( table->data[ i ][ 0 ] == table_id )
-            break;
-
-    if (i!=count)
-        return TRUE;
-
-    count = table->nonpersistent_row_count;
-    for( i=0; i<count; i++ )
-        if( table->nonpersistent_data[ i ][ 0 ] == table_id )
-            break;
-
-    if (i!=count)
-        return TRUE;
-
-    TRACE("Searched %d tables, but %d was not found\n", count, table_id );
+    for( i = 0; i < table->row_count; i++ )
+    {
+        if( read_table_int( table->data, i, 0, LONG_STR_BYTES ) == table_id )
+            return TRUE;
+    }
 
     return FALSE;
 }
@@ -1132,7 +1085,6 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
     UINT offset, n;
-    BYTE **data;
 
     if( !tv->table )
         return ERROR_INVALID_PARAMETER;
@@ -1141,7 +1093,7 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
         return ERROR_INVALID_PARAMETER;
 
     /* how many rows are there ? */
-    if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
+    if( row >= tv->table->row_count )
         return ERROR_NO_MORE_ITEMS;
 
     if( tv->columns[col-1].offset >= tv->row_size )
@@ -1154,15 +1106,7 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
     if (tv->order)
         row = tv->order->reorder[row];
 
-    if (row >= tv->table->row_count)
-    {
-        row -= tv->table->row_count;
-        data = tv->table->nonpersistent_data;
-    }
-    else
-        data = tv->table->data;
-
-    n = bytes_per_column( &tv->columns[col-1] );
+    n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
     if (n != 2 && n != 3 && n != 4)
     {
         ERR("oops! what is %d bytes per column?\n", n );
@@ -1170,13 +1114,98 @@ static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *
     }
 
     offset = tv->columns[col-1].offset;
-    *val = read_table_int(data, row, offset, n);
+    *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 )
+        {
+            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], LONG_STR_BYTES );
+
+                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
@@ -1185,70 +1214,32 @@ 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 encname, 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 > 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 );
+    encname = encode_streamname( FALSE, full_name );
+    r = db_get_raw_stream( tv->db, encname, stm );
     if( r )
         ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
-    msi_free( full_name );
 
+    msi_free( full_name );
+    msi_free( encname );
     return r;
 }
 
 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
 {
     UINT offset, n, i;
-    BYTE **data;
 
     if( !tv->table )
         return ERROR_INVALID_PARAMETER;
@@ -1256,7 +1247,7 @@ static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
     if( (col==0) || (col>tv->num_cols) )
         return ERROR_INVALID_PARAMETER;
 
-    if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
+    if( row >= tv->table->row_count )
         return ERROR_INVALID_PARAMETER;
 
     if( tv->columns[col-1].offset >= tv->row_size )
@@ -1269,15 +1260,7 @@ static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
     msi_free( tv->columns[col-1].hash_table );
     tv->columns[col-1].hash_table = NULL;
 
-    if (row >= tv->table->row_count)
-    {
-        row -= tv->table->row_count;
-        data = tv->table->nonpersistent_data;
-    }
-    else
-        data = tv->table->data;
-
-    n = bytes_per_column( &tv->columns[col-1] );
+    n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
     if ( n != 2 && n != 3 && n != 4 )
     {
         ERR("oops! what is %d bytes per column?\n", n );
@@ -1286,7 +1269,7 @@ static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
 
     offset = tv->columns[col-1].offset;
     for ( i = 0; i < n; i++ )
-        data[row][offset + i] = (val >> i * 8) & 0xff;
+        tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
 
     return ERROR_SUCCESS;
 }
@@ -1304,6 +1287,88 @@ static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
     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 get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
+{
+    MSICOLUMNINFO columninfo;
+    UINT r;
+
+    if ( (iField <= 0) ||
+         (iField > tv->num_cols) ||
+          MSI_RecordIsNull( rec, iField ) )
+        return ERROR_FUNCTION_FAILED;
+
+    columninfo = tv->columns[ iField - 1 ];
+
+    if ( MSITYPE_IS_BINARY(columninfo.type) )
+    {
+        *pvalue = 1; /* refers to the first key column */
+    }
+    else if ( columninfo.type & MSITYPE_STRING )
+    {
+        LPCWSTR sval = MSI_RecordGetString( rec, iField );
+
+        r = msi_string2idW(tv->db->strings, sval, pvalue);
+        if (r != ERROR_SUCCESS)
+           return ERROR_NOT_FOUND;
+    }
+    else if ( bytes_per_column( tv->db, &columninfo, LONG_STR_BYTES ) == 2 )
+    {
+        *pvalue = 0x8000 + MSI_RecordGetInteger( rec, iField );
+        if ( *pvalue & 0xffff0000 )
+        {
+            ERR("field %u value %d out of range\n", iField, *pvalue - 0x8000);
+            return ERROR_FUNCTION_FAILED;
+        }
+    }
+    else
+    {
+        INT ival = MSI_RecordGetInteger( rec, iField );
+        *pvalue = ival ^ 0x80000000;
+    }
+
+    return ERROR_SUCCESS;
+}
+
 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
@@ -1324,46 +1389,62 @@ static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UI
         if ( !(mask&(1<<i)) )
             continue;
 
-        /* if row >= tv->table->row_count then it is a non-persistent row */
-        persistent = tv->table->persistent && (row < tv->table->row_count);
+        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 ) )
         {
+            r = get_table_value_from_record (tv, rec, i + 1, &val);
             if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
             {
-                val = 1; /* refers to the first key column */
-            }
-            else if ( tv->columns[i].type & MSITYPE_STRING )
-            {
-                LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
-                UINT ival, x;
+                IStream *stm;
+                LPWSTR stname;
+
+                if ( r != ERROR_SUCCESS )
+                    return ERROR_FUNCTION_FAILED;
 
-                r = msi_string2idW(tv->db->strings, sval, &ival);
-                if (r == ERROR_SUCCESS)
+                r = MSI_RecordGetIStream( rec, i + 1, &stm );
+                if ( r != ERROR_SUCCESS )
+                    return r;
+
+                r = msi_stream_name( tv, row, &stname );
+                if ( r != ERROR_SUCCESS )
                 {
-                    TABLE_fetch_int(&tv->view, row, i + 1, &x);
-                    if (ival == x)
-                        continue;
+                    IStream_Release( stm );
+                    return r;
                 }
 
-                val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
-                                      persistent ? StringPersistent : StringNonPersistent );
+                r = msi_addstreamW( tv->db, stname, stm );
+                IStream_Release( stm );
+                msi_free ( stname );
+
+                if ( r != ERROR_SUCCESS )
+                    return r;
             }
-            else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
+            else if ( tv->columns[i].type & MSITYPE_STRING )
             {
-                val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
-                if ( val & 0xffff0000 )
+                UINT x;
+
+                if ( r != ERROR_SUCCESS )
                 {
-                    ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
-                    return ERROR_FUNCTION_FAILED;
+                    LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
+                    val = msi_addstringW( tv->db->strings, sval, -1, 1,
+                      persistent ? StringPersistent : StringNonPersistent );
+
+                }
+                else
+                {
+                    TABLE_fetch_int(&tv->view, row, i + 1, &x);
+                    if (val == x)
+                        continue;
                 }
             }
             else
             {
-                INT ival = MSI_RecordGetInteger( rec, i + 1 );
-                val = ival ^ 0x80000000;
+                if ( r != ERROR_SUCCESS )
+                    return ERROR_FUNCTION_FAILED;
             }
         }
 
@@ -1378,8 +1459,10 @@ static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL tempo
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
     BYTE **p, *row;
+    BOOL *b;
     UINT sz;
     BYTE ***data_ptr;
+    BOOL **data_persist_ptr;
     UINT *row_count;
 
     TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
@@ -1391,18 +1474,11 @@ static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL tempo
     if( !row )
         return ERROR_NOT_ENOUGH_MEMORY;
 
-    if( temporary )
-    {
-        row_count = &tv->table->nonpersistent_row_count;
-        data_ptr = &tv->table->nonpersistent_data;
-        *num = tv->table->row_count + tv->table->nonpersistent_row_count;
-    }
-    else
-    {
-        row_count = &tv->table->row_count;
-        data_ptr = &tv->table->data;
+    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 )
@@ -1415,8 +1491,24 @@ static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL tempo
         return ERROR_NOT_ENOUGH_MEMORY;
     }
 
+    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;
@@ -1452,14 +1544,15 @@ static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *col
     {
         if( !tv->table )
             return ERROR_INVALID_PARAMETER;
-        *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
+        *rows = tv->table->row_count;
     }
 
     return ERROR_SUCCESS;
 }
 
 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
-                UINT n, LPWSTR *name, UINT *type )
+                UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
+                LPWSTR *table_name )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
 
@@ -1474,9 +1567,20 @@ static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
         if( !*name )
             return ERROR_FUNCTION_FAILED;
     }
+
+    if( table_name )
+    {
+        *table_name = strdupW( tv->columns[n-1].tablename );
+        if( !*table_name )
+            return ERROR_FUNCTION_FAILED;
+    }
+
     if( type )
         *type = tv->columns[n-1].type;
 
+    if( temporary )
+        *temporary = tv->columns[n-1].temporary;
+
     return ERROR_SUCCESS;
 }
 
@@ -1520,10 +1624,66 @@ static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
     return ERROR_SUCCESS;
 }
 
-static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
+static int compare_record( MSITABLEVIEW *tv, UINT row, MSIRECORD *rec )
+{
+    UINT r, i, ivalue, x;
+
+    for (i = 0; i < tv->num_cols; i++ )
+    {
+        r = get_table_value_from_record( tv, rec, i + 1, &ivalue );
+        if (r != ERROR_SUCCESS)
+            return 1;
+
+        r = TABLE_fetch_int( &tv->view, row, i + 1, &x );
+        if (r != ERROR_SUCCESS)
+        {
+            WARN("TABLE_fetch_int should not fail here %u\n", r);
+            return -1;
+        }
+        if (ivalue > x)
+        {
+            return 1;
+        }
+        else if (ivalue == x)
+        {
+            if (i < tv->num_cols - 1) continue;
+            return 0;
+        }
+        else
+            return -1;
+    }
+    return 1;
+}
+
+static int find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec )
+{
+    int idx, c, low = 0, high = tv->table->row_count - 1;
+
+    TRACE("%p %p\n", tv, rec);
+
+    while (low <= high)
+    {
+        idx = (low + high) / 2;
+        c = compare_record( tv, idx, rec );
+
+        if (c < 0)
+            high = idx - 1;
+        else if (c > 0)
+            low = idx + 1;
+        else
+        {
+            TRACE("found %u\n", idx);
+            return idx;
+        }
+    }
+    TRACE("found %u\n", high + 1);
+    return high + 1;
+}
+
+static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
-    UINT r, row = -1;
+    UINT i, r;
 
     TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
 
@@ -1532,11 +1692,24 @@ static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temp
     if( r != ERROR_SUCCESS )
         return ERROR_FUNCTION_FAILED;
 
+    if (row == -1)
+        row = find_insert_index( tv, rec );
+
     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];
+    }
+
+    /* Re-set the persistence flag */
+    tv->table->data_persistent[row] = !temporary;
     return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
 }
 
@@ -1544,7 +1717,6 @@ static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
 {
     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
     UINT r, num_rows, num_cols, i;
-    BYTE **data;
 
     TRACE("%p %d\n", tv, row);
 
@@ -1558,19 +1730,8 @@ static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
     if ( row >= num_rows )
         return ERROR_FUNCTION_FAILED;
 
-    if ( row < tv->table->row_count )
-    {
-        num_rows = tv->table->row_count;
-        tv->table->row_count--;
-        data = tv->table->data;
-    }
-    else
-    {
-        num_rows = tv->table->nonpersistent_row_count;
-        row -= tv->table->row_count;
-        tv->table->nonpersistent_row_count--;
-        data = tv->table->nonpersistent_data;
-    }
+    num_rows = tv->table->row_count;
+    tv->table->row_count--;
 
     /* reset the hash tables */
     for (i = 0; i < tv->num_cols; i++)
@@ -1579,11 +1740,13 @@ static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
         tv->columns[i].hash_table = NULL;
     }
 
-    if ( row == num_rows - 1 )
-        return ERROR_SUCCESS;
-
     for (i = row + 1; i < num_rows; i++)
-        memcpy(data[i - 1], data[i], tv->row_size);
+    {
+        memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
+        tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
+    }
+
+    msi_free(tv->table->data[num_rows - 1]);
 
     return ERROR_SUCCESS;
 }
@@ -1614,6 +1777,21 @@ static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
     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;
@@ -1635,6 +1813,9 @@ static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT ro
     if (r != ERROR_SUCCESS)
         return r;
 
+    /* Close the original record */
+    MSI_CloseRecord(&rec->hdr);
+
     count = MSI_RecordGetFieldCount(rec);
     for (i = 0; i < count; i++)
         MSI_RecordCopyField(curr, i + 1, rec, i + 1);
@@ -1664,14 +1845,14 @@ static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
         r = table_validate_new( tv, rec );
         if (r != ERROR_SUCCESS)
             break;
-        r = TABLE_insert_row( view, rec, FALSE );
+        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, TRUE );
+        r = TABLE_insert_row( view, rec, -1, TRUE );
         break;
 
     case MSIMODIFY_REFRESH:
@@ -1683,6 +1864,9 @@ static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
         break;
 
     case MSIMODIFY_ASSIGN:
+        r = msi_table_assign( view, rec );
+        break;
+
     case MSIMODIFY_REPLACE:
     case MSIMODIFY_MERGE:
     case MSIMODIFY_VALIDATE:
@@ -1737,7 +1921,7 @@ static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
     if( !tv->columns[col-1].hash_table )
     {
         UINT i;
-        UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
+        UINT num_rows = tv->table->row_count;
         MSICOLUMNHASHENTRY **hash_table;
         MSICOLUMNHASHENTRY *new_entry;
 
@@ -1845,7 +2029,7 @@ static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT num
 
 done:
     msiobj_release(&rec->hdr);
-    if (columns) columns->ops->delete(columns);
+    columns->ops->delete(columns);
     return r;
 }
 
@@ -1903,7 +2087,7 @@ static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number
     MSI_RecordSetStringW(rec, 3, column);
     MSI_RecordSetInteger(rec, 4, type);
 
-    r = TABLE_insert_row(&tv->view, rec, FALSE);
+    r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
     if (r != ERROR_SUCCESS)
         goto done;
 
@@ -1930,6 +2114,7 @@ done:
 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
 {
     UINT n, r, count;
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
 
     r = TABLE_get_dimensions(view, NULL, &count);
     if (r != ERROR_SUCCESS)
@@ -1938,7 +2123,7 @@ static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWS
     if (order->num_cols >= count)
         return ERROR_FUNCTION_FAILED;
 
-    r = VIEW_find_column(view, name, &n);
+    r = VIEW_find_column(view, name, tv->name, &n);
     if (r != ERROR_SUCCESS)
         return r;
 
@@ -2081,6 +2266,52 @@ static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
     return ERROR_SUCCESS;
 }
 
+static UINT TABLE_drop(struct tagMSIVIEW *view)
+{
+    MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
+    MSIVIEW *tables = NULL;
+    MSIRECORD *rec = NULL;
+    UINT r, row;
+    INT i;
+
+    TRACE("dropping table %s\n", debugstr_w(tv->name));
+
+    for (i = tv->table->col_count - 1; i >= 0; i--)
+    {
+        r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
+                                tv->table->colinfo[i].number);
+        if (r != ERROR_SUCCESS)
+            return r;
+    }
+
+    rec = MSI_CreateRecord(1);
+    if (!rec)
+        return ERROR_OUTOFMEMORY;
+
+    MSI_RecordSetStringW(rec, 1, tv->name);
+
+    r = TABLE_CreateView(tv->db, szTables, &tables);
+    if (r != ERROR_SUCCESS)
+        return r;
+
+    r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
+    if (r != ERROR_SUCCESS)
+        goto done;
+
+    r = TABLE_delete_row(tables, row);
+    if (r != ERROR_SUCCESS)
+        goto done;
+
+    list_remove(&tv->table->entry);
+    free_table(tv->table);
+
+done:
+    msiobj_release(&rec->hdr);
+    tables->ops->delete(tables);
+
+    return r;
+}
+
 static const MSIVIEWOPS table_ops =
 {
     TABLE_fetch_int,
@@ -2101,6 +2332,7 @@ static const MSIVIEWOPS table_ops =
     TABLE_add_column,
     TABLE_remove_column,
     TABLE_sort,
+    TABLE_drop,
 };
 
 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
@@ -2138,7 +2370,7 @@ UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
     tv->db = db;
     tv->columns = tv->table->colinfo;
     tv->num_cols = tv->table->col_count;
-    tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
+    tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
 
     TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
 
@@ -2150,12 +2382,13 @@ UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
 
 UINT MSI_CommitTables( MSIDATABASE *db )
 {
-    UINT r;
+    UINT r, bytes_per_strref;
+    HRESULT hr;
     MSITABLE *table = NULL;
 
     TRACE("%p\n",db);
 
-    r = msi_save_string_table( db->strings, db->storage );
+    r = msi_save_string_table( db->strings, db->storage, &bytes_per_strref );
     if( r != ERROR_SUCCESS )
     {
         WARN("failed to save string table r=%08x\n",r);
@@ -2164,7 +2397,7 @@ UINT MSI_CommitTables( MSIDATABASE *db )
 
     LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
     {
-        r = save_table( db, table );
+        r = save_table( db, table, bytes_per_strref );
         if( r != ERROR_SUCCESS )
         {
             WARN("failed to save table %s (r=%08x)\n",
@@ -2176,7 +2409,13 @@ UINT MSI_CommitTables( MSIDATABASE *db )
     /* force everything to reload next time */
     free_cached_tables( db );
 
-    return ERROR_SUCCESS;
+    hr = IStorage_Commit( db->storage, 0 );
+    if (FAILED( hr ))
+    {
+        WARN("failed to commit changes 0x%08x\n", hr);
+        r = ERROR_FUNCTION_FAILED;
+    }
+    return r;
 }
 
 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
@@ -2193,10 +2432,7 @@ MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
     if (r != ERROR_SUCCESS)
         return MSICONDITION_NONE;
 
-    if (t->persistent)
-        return MSICONDITION_TRUE;
-    else
-        return MSICONDITION_FALSE;
+    return t->persistent;
 }
 
 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
@@ -2209,7 +2445,66 @@ static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
     return ret;
 }
 
+static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
+{
+    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( const MSITABLEVIEW *tv, const string_table *st,
+                                            IStorage *stg,
                                             const BYTE *rawdata, UINT bytes_per_strref )
 {
     UINT i, val, ofs = 0;
@@ -2233,8 +2528,28 @@ static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string
         if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
             continue;
 
-        if( (columns[i].type & MSITYPE_STRING) &&
-            ! MSITYPE_IS_BINARY(tv->columns[i].type) )
+        if( MSITYPE_IS_BINARY(tv->columns[i].type) )
+        {
+            LPWSTR encname;
+            IStream *stm = NULL;
+            UINT r;
+
+            ofs += bytes_per_column( tv->db, &columns[i], bytes_per_strref );
+
+            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;
 
@@ -2246,13 +2561,13 @@ static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string
         }
         else
         {
-            UINT n = bytes_per_column( &columns[i] );
+            UINT n = bytes_per_column( tv->db, &columns[i], bytes_per_strref );
             switch( n )
             {
             case 2:
                 val = read_raw_int(rawdata, ofs, n);
                 if (val)
-                    MSI_RecordSetInteger( rec, i+1, val^0x8000 );
+                    MSI_RecordSetInteger( rec, i+1, val-0x8000 );
                 TRACE(" field %d [0x%04x]\n", i+1, val );
                 break;
             case 4:
@@ -2332,7 +2647,10 @@ static UINT* msi_record_to_row( const 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;
@@ -2378,7 +2696,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 + tv->table->nonpersistent_row_count; i++ )
+    for( i = 0; i < tv->table->row_count; i++ )
     {
         r = msi_row_matches( tv, i, data );
         if( r == ERROR_SUCCESS )
@@ -2457,7 +2775,7 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
                     ! MSITYPE_IS_BINARY(tv->columns[i].type) )
                     sz += bytes_per_strref;
                 else
-                    sz += bytes_per_column( &tv->columns[i] );
+                    sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
             }
         }
         else
@@ -2479,7 +2797,7 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
                         ! MSITYPE_IS_BINARY(tv->columns[i].type) )
                         sz += bytes_per_strref;
                     else
-                        sz += bytes_per_column( &tv->columns[i] );
+                        sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
                 }
             }
         }
@@ -2492,66 +2810,75 @@ static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
             break;
         }
 
-        rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
+        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;
+            WCHAR table[32];
+            DWORD sz = 32;
+            UINT number = MSI_NULL_INTEGER;
+            UINT row = 0;
 
-                TRACE("inserting record\n");
-
-                if (!lstrcmpW(name, szColumns))
+            if (!lstrcmpW( name, szColumns ))
+            {
+                MSI_RecordGetStringW( rec, 1, table, &sz );
+                number = MSI_RecordGetInteger( rec, 2 );
+
+                /*
+                 * 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 )
                 {
-                    MSI_RecordGetStringW( rec, 1, table, &sz );
-                    number = MSI_RecordGetInteger( rec, 2 );
-
-                    /*
-                     * 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 )
+                    /* reset the column number on a new table */
+                    if (lstrcmpW( 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 );
+                        colcol = 0;
+                        lstrcpyW( coltable, table );
                     }
+
+                    /* fix nul column numbers */
+                    MSI_RecordSetInteger( rec, 2, ++colcol );
                 }
+            }
 
-                r = TABLE_insert_row( &tv->view, rec, FALSE );
-                if (r != ERROR_SUCCESS)
-                    ERR("insert row failed\n");
+            if (TRACE_ON(msidb)) dump_record( rec );
 
-                if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
-                    msi_update_table_columns( db, table );
-            }
-            else
+            r = msi_table_find_row( tv, rec, &row );
+            if (r == ERROR_SUCCESS)
             {
-                UINT row = 0;
-
-                r = msi_table_find_row( tv, rec, &row );
-                if (r != ERROR_SUCCESS)
-                    ERR("no matching row to transform\n");
-                else if ( mask )
+                if (!mask)
+                {
+                    TRACE("deleting row [%d]:\n", row);
+                    r = TABLE_delete_row( &tv->view, row );
+                    if (r != ERROR_SUCCESS)
+                        WARN("failed to delete row %u\n", r);
+                }
+                else if (mask & 1)
                 {
-                    TRACE("modifying row [%d]:\n", row);
-                    TABLE_set_row( &tv->view, row, rec, mask );
+                    TRACE("modifying full row [%d]:\n", row);
+                    r = TABLE_set_row( &tv->view, row, rec, (1 << tv->num_cols) - 1 );
+                    if (r != ERROR_SUCCESS)
+                        WARN("failed to modify row %u\n", r);
                 }
                 else
                 {
-                    TRACE("deleting row [%d]:\n", row);
-                    TABLE_delete_row( &tv->view, row );
+                    TRACE("modifying masked row [%d]:\n", row);
+                    r = TABLE_set_row( &tv->view, row, rec, mask );
+                    if (r != ERROR_SUCCESS)
+                        WARN("failed to modify row %u\n", r);
                 }
             }
-            if( TRACE_ON(msidb) ) dump_record( rec );
+            else
+            {
+                TRACE("inserting row\n");
+                r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
+                if (r != ERROR_SUCCESS)
+                    WARN("failed to insert row %u\n", r);
+            }
+
+            if (number != MSI_NULL_INTEGER && !lstrcmpW( name, szColumns ))
+                msi_update_table_columns( db, table );
+
             msiobj_release( &rec->hdr );
         }
 
@@ -2686,25 +3013,3 @@ 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 ) )
-    {
-        MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
-                                      MSITRANSFORM, entry );
-        list_remove( &t->entry );
-        IStorage_Release( t->stg );
-        msi_free( t );
-    }
-}