mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-04-05 20:31:00 +08:00
Added simple support for count(*) to SQL parser to quickly get number of rows in table
This commit is contained in:
parent
0e508c82d8
commit
16bf355d6b
@ -31,6 +31,7 @@
|
||||
typedef struct {
|
||||
MdbHandle *mdb;
|
||||
int all_columns;
|
||||
int sel_count;
|
||||
unsigned int num_columns;
|
||||
GPtrArray *columns;
|
||||
unsigned int num_tables;
|
||||
@ -79,6 +80,7 @@ extern MdbSQLSarg *mdb_sql_alloc_sarg();
|
||||
extern MdbHandle *mdb_sql_open(MdbSQL *sql, char *db_name);
|
||||
extern int mdb_sql_add_sarg(MdbSQL *sql, char *col_name, int op, char *constant);
|
||||
extern void mdb_sql_all_columns(MdbSQL *sql);
|
||||
extern void mdb_sql_sel_count(MdbSQL *sql);
|
||||
extern int mdb_sql_add_column(MdbSQL *sql, char *column_name);
|
||||
extern int mdb_sql_add_table(MdbSQL *sql, char *table_name);
|
||||
extern void mdb_sql_dump(MdbSQL *sql);
|
||||
|
@ -45,6 +45,7 @@ null { return NUL; }
|
||||
(<=) { return LTEQ; }
|
||||
(>=) { return GTEQ; }
|
||||
like { return LIKE; }
|
||||
count { return COUNT; }
|
||||
[ \t\r] ;
|
||||
|
||||
\"[^"]*\"\" {
|
||||
|
@ -316,7 +316,7 @@ mdb_sql_dump_node(MdbSargNode *node, int level)
|
||||
printf(" < %d\n", node->value.i);
|
||||
break;
|
||||
case MDB_GT:
|
||||
printf(" < %d\n", node->value.i);
|
||||
printf(" > %d\n", node->value.i);
|
||||
break;
|
||||
case MDB_LIKE:
|
||||
printf(" like %s\n", node->value.s);
|
||||
@ -419,6 +419,12 @@ mdb_sql_all_columns(MdbSQL *sql)
|
||||
{
|
||||
sql->all_columns=1;
|
||||
}
|
||||
void
|
||||
mdb_sql_sel_count(MdbSQL *sql)
|
||||
{
|
||||
sql->sel_count=1;
|
||||
}
|
||||
|
||||
int mdb_sql_add_column(MdbSQL *sql, char *column_name)
|
||||
{
|
||||
MdbSQLColumn *c;
|
||||
@ -508,6 +514,7 @@ void mdb_sql_reset(MdbSQL *sql)
|
||||
sql->sarg_stack = NULL;
|
||||
|
||||
sql->all_columns = 0;
|
||||
sql->sel_count = 0;
|
||||
sql->max_rows = -1;
|
||||
}
|
||||
static void print_break(int sz, int first)
|
||||
@ -690,6 +697,26 @@ int found = 0;
|
||||
return;
|
||||
}
|
||||
mdb_read_columns(table);
|
||||
|
||||
if (sql->sel_count && !sql->sarg_tree) {
|
||||
MdbTableDef *ttable = mdb_create_temp_table(mdb, "#count");
|
||||
char tmpstr[32];
|
||||
gchar row_cnt[32];
|
||||
unsigned char row_buffer[MDB_PGSIZE];
|
||||
MdbField fields[1];
|
||||
int row_size, tmpsiz;
|
||||
|
||||
mdb_sql_add_temp_col(sql, ttable, 0, "count", MDB_TEXT, 30, 0);
|
||||
sprintf(tmpstr,"%d",table->num_rows);
|
||||
tmpsiz = mdb_ascii2unicode(mdb, tmpstr, 0, row_cnt, 32);
|
||||
mdb_fill_temp_field(&fields[0],row_cnt, tmpsiz, 0,0,0,0);
|
||||
row_size = mdb_pack_row(ttable, row_buffer, 1, fields);
|
||||
mdb_add_row_to_pg(ttable,row_buffer, row_size);
|
||||
ttable->num_rows++;
|
||||
sql->cur_table = ttable;
|
||||
return;
|
||||
}
|
||||
|
||||
mdb_read_indices(table);
|
||||
mdb_rewind_table(table);
|
||||
|
||||
|
@ -41,8 +41,8 @@ static MdbSQL *g_sql;
|
||||
|
||||
|
||||
|
||||
%token <name> IDENT NAME PATH STRING NUMBER
|
||||
%token SELECT FROM WHERE CONNECT DISCONNECT TO LIST TABLES AND OR NOT
|
||||
%token <name> IDENT NAME PATH STRING NUMBER
|
||||
%token SELECT FROM WHERE CONNECT DISCONNECT TO LIST TABLES AND OR NOT COUNT
|
||||
%token DESCRIBE TABLE
|
||||
%token LTEQ GTEQ LIKE IS NUL
|
||||
|
||||
@ -146,11 +146,13 @@ table:
|
||||
;
|
||||
|
||||
column_list:
|
||||
'*' { mdb_sql_all_columns(_mdb_sql(NULL)); }
|
||||
COUNT '(' '*' ')' { mdb_sql_sel_count(_mdb_sql(NULL)); }
|
||||
| '*' { mdb_sql_all_columns(_mdb_sql(NULL)); }
|
||||
| column
|
||||
| column ',' column_list
|
||||
;
|
||||
|
||||
|
||||
column:
|
||||
identifier { mdb_sql_add_column(_mdb_sql(NULL), $1); free($1); }
|
||||
;
|
||||
|
Loading…
Reference in New Issue
Block a user