msi: Reimplement joins to allow joining any number of tables, each of arbitrary size.

This commit is contained in:
James Hawkins 2007-08-01 14:25:45 -07:00 committed by Alexandre Julliard
parent 4f6a93b9c8
commit 2e6ed06fdc
4 changed files with 200 additions and 172 deletions

View file

@ -23,7 +23,6 @@
#include "windef.h" #include "windef.h"
#include "winbase.h" #include "winbase.h"
#include "winerror.h" #include "winerror.h"
#include "wine/debug.h"
#include "msi.h" #include "msi.h"
#include "msiquery.h" #include "msiquery.h"
#include "objbase.h" #include "objbase.h"
@ -31,157 +30,150 @@
#include "msipriv.h" #include "msipriv.h"
#include "query.h" #include "query.h"
#include "wine/debug.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(msidb); WINE_DEFAULT_DEBUG_CHANNEL(msidb);
typedef struct tagJOINTABLE
{
struct list entry;
MSIVIEW *view;
UINT columns;
UINT rows;
UINT next_rows;
} JOINTABLE;
typedef struct tagMSIJOINVIEW typedef struct tagMSIJOINVIEW
{ {
MSIVIEW view; MSIVIEW view;
MSIDATABASE *db; MSIDATABASE *db;
MSIVIEW *left, *right; struct list tables;
UINT left_count, right_count; UINT columns;
UINT left_rows, right_rows; UINT rows;
} MSIJOINVIEW; } MSIJOINVIEW;
static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val ) static UINT JOIN_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
MSIVIEW *table; JOINTABLE *table;
UINT cols = 0;
UINT prev_rows = 1;
TRACE("%p %d %d %p\n", jv, row, col, val ); TRACE("%d, %d\n", row, col);
if( !jv->left || !jv->right ) if (col == 0 || col > jv->columns)
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( (col==0) || (col>(jv->left_count + jv->right_count)) ) if (row >= jv->rows)
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( row >= (jv->left_rows * jv->right_rows) ) LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
return ERROR_FUNCTION_FAILED;
if( col <= jv->left_count )
{ {
table = jv->left; if (col <= cols + table->columns)
row = (row/jv->right_rows);
}
else
{ {
table = jv->right; row = (row % (jv->rows / table->next_rows)) / prev_rows;
row = (row % jv->right_rows); col -= cols;
col -= jv->left_count; break;
} }
return table->ops->fetch_int( table, row, col, val ); prev_rows = table->rows;
cols += table->columns;
}
return table->view->ops->fetch_int( table->view, row, col, val );
} }
static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm) static UINT JOIN_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm)
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
MSIVIEW *table; JOINTABLE *table;
UINT cols = 0;
UINT prev_rows = 1;
TRACE("%p %d %d %p\n", jv, row, col, stm ); TRACE("%p %d %d %p\n", jv, row, col, stm );
if( !jv->left || !jv->right ) if (col == 0 || col > jv->columns)
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( (col==0) || (col>(jv->left_count + jv->right_count)) ) if (row >= jv->rows)
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( row >= jv->left_rows * jv->right_rows ) LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
return ERROR_FUNCTION_FAILED;
if( row <= jv->left_count )
{ {
table = jv->left; if (col <= cols + table->columns)
row = (row/jv->right_rows);
}
else
{ {
table = jv->right; row = (row % (jv->rows / table->next_rows)) / prev_rows;
row = (row % jv->right_rows); col -= cols;
col -= jv->left_count; break;
} }
return table->ops->fetch_stream( table, row, col, stm ); prev_rows = table->rows;
cols += table->columns;
}
return table->view->ops->fetch_stream( table->view, row, col, stm );
} }
static UINT JOIN_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec ) static UINT JOIN_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; FIXME("(%p, %d, %p): stub!\n", view, row, rec);
MSIVIEW *table;
TRACE("%p %d %p\n", jv, row, rec );
if( !jv->left || !jv->right )
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( row >= jv->left_rows * jv->right_rows )
return ERROR_FUNCTION_FAILED;
if( row <= jv->left_count )
{
table = jv->left;
row = (row/jv->right_rows);
}
else
{
table = jv->right;
row = (row % jv->right_rows);
}
return table->ops->get_row(table, row, rec);
} }
static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record ) static UINT JOIN_execute( struct tagMSIVIEW *view, MSIRECORD *record )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
UINT r, *ldata = NULL, *rdata = NULL; JOINTABLE *table;
UINT r, rows;
TRACE("%p %p\n", jv, record); TRACE("%p %p\n", jv, record);
if( !jv->left || !jv->right ) LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
return ERROR_FUNCTION_FAILED;
r = jv->left->ops->execute( jv->left, NULL );
if (r != ERROR_SUCCESS)
return r;
r = jv->right->ops->execute( jv->right, NULL );
if (r != ERROR_SUCCESS)
return r;
/* get the number of rows in each table */
r = jv->left->ops->get_dimensions( jv->left, &jv->left_rows, NULL );
if( r != ERROR_SUCCESS )
{ {
ERR("can't get left table dimensions\n"); table->view->ops->execute(table->view, NULL);
goto end;
r = table->view->ops->get_dimensions(table->view, &table->rows, NULL);
if (r != ERROR_SUCCESS)
{
ERR("failed to get table dimensions\n");
return r;
} }
r = jv->right->ops->get_dimensions( jv->right, &jv->right_rows, NULL ); /* each table must have at least one row */
if( r != ERROR_SUCCESS ) if (table->rows == 0)
{ {
ERR("can't get right table dimensions\n"); jv->rows = 0;
goto end; return ERROR_SUCCESS;
} }
end: if (jv->rows == 0)
msi_free( ldata ); jv->rows = table->rows;
msi_free( rdata ); else
jv->rows *= table->rows;
}
return r; rows = jv->rows;
LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
{
rows /= table->rows;
table->next_rows = rows;
}
return ERROR_SUCCESS;
} }
static UINT JOIN_close( struct tagMSIVIEW *view ) static UINT JOIN_close( struct tagMSIVIEW *view )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
JOINTABLE *table;
TRACE("%p\n", jv ); TRACE("%p\n", jv );
if( !jv->left || !jv->right ) LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
return ERROR_FUNCTION_FAILED; {
table->view->ops->close(table->view);
jv->left->ops->close( jv->left ); }
jv->right->ops->close( jv->right );
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@ -192,16 +184,11 @@ static UINT JOIN_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols
TRACE("%p %p %p\n", jv, rows, cols ); TRACE("%p %p %p\n", jv, rows, cols );
if( cols ) if (cols)
*cols = jv->left_count + jv->right_count; *cols = jv->columns;
if( rows ) if (rows)
{ *rows = jv->rows;
if( !jv->left || !jv->right )
return ERROR_FUNCTION_FAILED;
*rows = jv->left_rows * jv->right_rows;
}
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@ -210,48 +197,46 @@ static UINT JOIN_get_column_info( struct tagMSIVIEW *view,
UINT n, LPWSTR *name, UINT *type ) UINT n, LPWSTR *name, UINT *type )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
JOINTABLE *table;
UINT cols = 0;
TRACE("%p %d %p %p\n", jv, n, name, type ); TRACE("%p %d %p %p\n", jv, n, name, type );
if( !jv->left || !jv->right ) if (n == 0 || n > jv->columns)
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( (n==0) || (n>(jv->left_count + jv->right_count)) ) LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
{
if (n <= cols + table->columns)
return table->view->ops->get_column_info(table->view, n - cols, name, type);
cols += table->columns;
}
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
if( n <= jv->left_count )
return jv->left->ops->get_column_info( jv->left, n, name, type );
n = n - jv->left_count;
return jv->right->ops->get_column_info( jv->right, n, name, type );
} }
static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, static UINT JOIN_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
MSIRECORD *rec, UINT row ) MSIRECORD *rec, UINT row )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; TRACE("%p %d %p\n", view, eModifyMode, rec);
TRACE("%p %d %p\n", jv, eModifyMode, rec );
return ERROR_FUNCTION_FAILED; return ERROR_FUNCTION_FAILED;
} }
static UINT JOIN_delete( struct tagMSIVIEW *view ) static UINT JOIN_delete( struct tagMSIVIEW *view )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
JOINTABLE *table;
TRACE("%p\n", jv ); TRACE("%p\n", jv );
if( jv->left ) LIST_FOR_EACH_ENTRY(table, &jv->tables, JOINTABLE, entry)
jv->left->ops->delete( jv->left ); {
jv->left = NULL; table->view->ops->delete(table->view);
table->view = NULL;
}
if( jv->right ) msi_free(jv);
jv->right->ops->delete( jv->right );
jv->right = NULL;
msi_free( jv );
return ERROR_SUCCESS; return ERROR_SUCCESS;
} }
@ -260,10 +245,27 @@ static UINT JOIN_find_matching_rows( struct tagMSIVIEW *view, UINT col,
UINT val, UINT *row, MSIITERHANDLE *handle ) UINT val, UINT *row, MSIITERHANDLE *handle )
{ {
MSIJOINVIEW *jv = (MSIJOINVIEW*)view; MSIJOINVIEW *jv = (MSIJOINVIEW*)view;
UINT i, row_value;
FIXME("%p, %d, %u, %p\n", jv, col, val, *handle); TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
return ERROR_FUNCTION_FAILED; if (col == 0 || col > jv->columns)
return ERROR_INVALID_PARAMETER;
for (i = (UINT)*handle; i < jv->rows; i++)
{
if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
continue;
if (row_value == val)
{
*row = i;
(*(UINT *)handle) = i + 1;
return ERROR_SUCCESS;
}
}
return ERROR_NO_MORE_ITEMS;
} }
static const MSIVIEWOPS join_ops = static const MSIVIEWOPS join_ops =
@ -287,13 +289,14 @@ static const MSIVIEWOPS join_ops =
NULL, NULL,
}; };
UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables )
LPCWSTR left, LPCWSTR right )
{ {
MSIJOINVIEW *jv = NULL; MSIJOINVIEW *jv = NULL;
UINT r = ERROR_SUCCESS; UINT r = ERROR_SUCCESS;
JOINTABLE *table;
LPWSTR ptr;
TRACE("%p (%s,%s)\n", jv, debugstr_w(left), debugstr_w(right) ); TRACE("%p (%s)\n", jv, debugstr_w(tables) );
jv = msi_alloc_zero( sizeof *jv ); jv = msi_alloc_zero( sizeof *jv );
if( !jv ) if( !jv )
@ -302,35 +305,42 @@ UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view,
/* fill the structure */ /* fill the structure */
jv->view.ops = &join_ops; jv->view.ops = &join_ops;
jv->db = db; jv->db = db;
jv->columns = 0;
jv->rows = 0;
/* create the tables to join */ list_init(&jv->tables);
r = TABLE_CreateView( db, left, &jv->left );
while (*tables)
{
if ((ptr = strchrW(tables, ' ')))
*ptr = '\0';
table = msi_alloc(sizeof(JOINTABLE));
if (!table)
return ERROR_OUTOFMEMORY;
r = TABLE_CreateView( db, tables, &table->view );
if( r != ERROR_SUCCESS ) if( r != ERROR_SUCCESS )
{ {
ERR("can't create left table\n"); ERR("can't create table\n");
goto end; goto end;
} }
r = TABLE_CreateView( db, right, &jv->right ); r = table->view->ops->get_dimensions( table->view, NULL, &table->columns );
if( r != ERROR_SUCCESS ) if( r != ERROR_SUCCESS )
{ {
ERR("can't create right table\n"); ERR("can't get table dimensions\n");
goto end; goto end;
} }
/* get the number of columns in each table */ jv->columns += table->columns;
r = jv->left->ops->get_dimensions( jv->left, NULL, &jv->left_count );
if( r != ERROR_SUCCESS )
{
ERR("can't get left table dimensions\n");
goto end;
}
r = jv->right->ops->get_dimensions( jv->right, NULL, &jv->right_count ); list_add_head( &jv->tables, &table->entry );
if( r != ERROR_SUCCESS )
{ if (!ptr)
ERR("can't get right table dimensions\n"); break;
goto end;
tables = ptr + 1;
} }
*view = &jv->view; *view = &jv->view;

View file

@ -119,8 +119,7 @@ UINT UPDATE_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR table,
UINT DELETE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table ); UINT DELETE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table );
UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, UINT JOIN_CreateView( MSIDATABASE *db, MSIVIEW **view, LPWSTR tables );
LPCWSTR left, LPCWSTR right );
UINT ALTER_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR name, column_info *colinfo, int hold ); UINT ALTER_CreateView( MSIDATABASE *db, MSIVIEW **view, LPCWSTR name, column_info *colinfo, int hold );

View file

@ -53,6 +53,7 @@ static LPWSTR SQL_getstring( void *info, const struct sql_str *str );
static INT SQL_getint( void *info ); static INT SQL_getint( void *info );
static int sql_lex( void *SQL_lval, SQL_input *info ); static int sql_lex( void *SQL_lval, SQL_input *info );
static LPWSTR parser_add_table( LPWSTR list, LPWSTR table );
static void *parser_alloc( void *info, unsigned int sz ); static void *parser_alloc( void *info, unsigned int sz );
static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column ); static column_info *parser_alloc_column( void *info, LPCWSTR table, LPCWSTR column );
@ -101,7 +102,7 @@ static struct expr * EXPR_wildcard( void *info );
%nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION %nonassoc END_OF_FILE ILLEGAL SPACE UNCLOSED_STRING COMMENT FUNCTION
COLUMN AGG_FUNCTION. COLUMN AGG_FUNCTION.
%type <string> table id %type <string> table tablelist id
%type <column_list> selcollist column column_and_type column_def table_def %type <column_list> selcollist column column_and_type column_def table_def
%type <column_list> column_assignment update_assign_list constlist %type <column_list> column_assignment update_assign_list constlist
%type <query> query from fromtable selectfrom unorderedsel %type <query> query from fromtable selectfrom unorderedsel
@ -466,18 +467,32 @@ fromtable:
if( r != ERROR_SUCCESS || !$$ ) if( r != ERROR_SUCCESS || !$$ )
YYABORT; YYABORT;
} }
| TK_FROM table TK_COMMA table | TK_FROM tablelist
{ {
SQL_input* sql = (SQL_input*) info; SQL_input* sql = (SQL_input*) info;
UINT r; UINT r;
/* only support inner joins on two tables */ r = JOIN_CreateView( sql->db, &$$, $2 );
r = JOIN_CreateView( sql->db, &$$, $2, $4 ); msi_free( $2 );
if( r != ERROR_SUCCESS ) if( r != ERROR_SUCCESS )
YYABORT; YYABORT;
} }
; ;
tablelist:
table
{
$$ = strdupW($1);
}
|
table TK_COMMA tablelist
{
$$ = parser_add_table($3, $1);
if (!$$)
YYABORT;
}
;
expr: expr:
TK_LP expr TK_RP TK_LP expr TK_RP
{ {
@ -663,6 +678,19 @@ number:
%% %%
static LPWSTR parser_add_table(LPWSTR list, LPWSTR table)
{
DWORD size = lstrlenW(list) + lstrlenW(table) + 2;
static const WCHAR space[] = {' ',0};
list = msi_realloc(list, size * sizeof(WCHAR));
if (!list) return NULL;
lstrcatW(list, space);
lstrcatW(list, table);
return list;
}
static void *parser_alloc( void *info, unsigned int sz ) static void *parser_alloc( void *info, unsigned int sz )
{ {
SQL_input* sql = (SQL_input*) info; SQL_input* sql = (SQL_input*) info;

View file

@ -2890,10 +2890,7 @@ static void test_join(void)
ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r ); ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
r = MsiViewExecute(hview, 0); r = MsiViewExecute(hview, 0);
todo_wine
{
ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r ); ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
}
i = 0; i = 0;
data_correct = TRUE; data_correct = TRUE;
@ -2919,10 +2916,7 @@ static void test_join(void)
} }
ok( data_correct, "data returned in the wrong order\n"); ok( data_correct, "data returned in the wrong order\n");
todo_wine
{
ok( i == 6, "Expected 6 rows, got %d\n", i ); ok( i == 6, "Expected 6 rows, got %d\n", i );
}
ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r ); ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
MsiViewClose(hview); MsiViewClose(hview);
@ -3000,7 +2994,7 @@ static void test_join(void)
MsiCloseHandle(hrec); MsiCloseHandle(hrec);
} }
todo_wine ok( data_correct, "data returned in the wrong order\n"); ok( data_correct, "data returned in the wrong order\n");
ok( i == 6, "Expected 6 rows, got %d\n", i ); ok( i == 6, "Expected 6 rows, got %d\n", i );
ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r ); ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
@ -3048,7 +3042,7 @@ static void test_join(void)
i++; i++;
MsiCloseHandle(hrec); MsiCloseHandle(hrec);
} }
todo_wine ok( data_correct, "data returned in the wrong order\n"); ok( data_correct, "data returned in the wrong order\n");
ok( i == 6, "Expected 6 rows, got %d\n", i ); ok( i == 6, "Expected 6 rows, got %d\n", i );
ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r ); ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
@ -3058,10 +3052,10 @@ static void test_join(void)
query = "SELECT * FROM `One`, `Two`, `Three` "; query = "SELECT * FROM `One`, `Two`, `Three` ";
r = MsiDatabaseOpenView(hdb, query, &hview); r = MsiDatabaseOpenView(hdb, query, &hview);
todo_wine ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r ); ok( r == ERROR_SUCCESS, "failed to open view: %d\n", r );
r = MsiViewExecute(hview, 0); r = MsiViewExecute(hview, 0);
todo_wine ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r ); ok( r == ERROR_SUCCESS, "failed to execute view: %d\n", r );
i = 0; i = 0;
data_correct = TRUE; data_correct = TRUE;
@ -3099,11 +3093,8 @@ static void test_join(void)
} }
ok( data_correct, "data returned in the wrong order\n"); ok( data_correct, "data returned in the wrong order\n");
todo_wine
{
ok( i == 6, "Expected 6 rows, got %d\n", i ); ok( i == 6, "Expected 6 rows, got %d\n", i );
ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r ); ok( r == ERROR_NO_MORE_ITEMS, "expected no more items: %d\n", r );
}
MsiViewClose(hview); MsiViewClose(hview);
MsiCloseHandle(hview); MsiCloseHandle(hview);