@@ -362,6 +362,15 @@ typedef struct
362
362
#define META_COMMAND 2
363
363
#define MAX_ARGS 10
364
364
365
+ typedef enum MetaCommand
366
+ {
367
+ META_NONE , /* not a known meta-command */
368
+ META_SET , /* \set */
369
+ META_SETSHELL , /* \setshell */
370
+ META_SHELL , /* \shell */
371
+ META_SLEEP /* \sleep */
372
+ } MetaCommand ;
373
+
365
374
typedef enum QueryMode
366
375
{
367
376
QUERY_SIMPLE , /* simple query */
@@ -378,6 +387,7 @@ typedef struct
378
387
char * line ; /* text of command line */
379
388
int command_num ; /* unique index of this Command struct */
380
389
int type ; /* command type (SQL_COMMAND or META_COMMAND) */
390
+ MetaCommand meta ; /* meta command identifier, or META_NONE */
381
391
int argc ; /* number of command words */
382
392
char * argv [MAX_ARGS ]; /* command word list */
383
393
PgBenchExpr * expr ; /* parsed expression, if needed */
@@ -1721,6 +1731,29 @@ evaluateExpr(TState *thread, CState *st, PgBenchExpr *expr, PgBenchValue *retval
1721
1731
}
1722
1732
}
1723
1733
1734
+ /*
1735
+ * Convert command name to meta-command enum identifier
1736
+ */
1737
+ static MetaCommand
1738
+ getMetaCommand (const char * cmd )
1739
+ {
1740
+ MetaCommand mc ;
1741
+
1742
+ if (cmd == NULL )
1743
+ mc = META_NONE ;
1744
+ else if (pg_strcasecmp (cmd , "set" ) == 0 )
1745
+ mc = META_SET ;
1746
+ else if (pg_strcasecmp (cmd , "setshell" ) == 0 )
1747
+ mc = META_SETSHELL ;
1748
+ else if (pg_strcasecmp (cmd , "shell" ) == 0 )
1749
+ mc = META_SHELL ;
1750
+ else if (pg_strcasecmp (cmd , "sleep" ) == 0 )
1751
+ mc = META_SLEEP ;
1752
+ else
1753
+ mc = META_NONE ;
1754
+ return mc ;
1755
+ }
1756
+
1724
1757
/*
1725
1758
* Run a shell command. The result is assigned to the variable if not NULL.
1726
1759
* Return true if succeeded, or false on error.
@@ -2214,7 +2247,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
2214
2247
fprintf (stderr , "\n" );
2215
2248
}
2216
2249
2217
- if (pg_strcasecmp ( argv [ 0 ], "sleep" ) == 0 )
2250
+ if (command -> meta == META_SLEEP )
2218
2251
{
2219
2252
/*
2220
2253
* A \sleep doesn't execute anything, we just get the
@@ -2240,7 +2273,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
2240
2273
}
2241
2274
else
2242
2275
{
2243
- if (pg_strcasecmp ( argv [ 0 ], "set" ) == 0 )
2276
+ if (command -> meta == META_SET )
2244
2277
{
2245
2278
PgBenchExpr * expr = command -> expr ;
2246
2279
PgBenchValue result ;
@@ -2259,7 +2292,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
2259
2292
break ;
2260
2293
}
2261
2294
}
2262
- else if (pg_strcasecmp ( argv [ 0 ], "setshell" ) == 0 )
2295
+ else if (command -> meta == META_SETSHELL )
2263
2296
{
2264
2297
bool ret = runShellCommand (st , argv [1 ], argv + 2 , argc - 2 );
2265
2298
@@ -2279,7 +2312,7 @@ doCustom(TState *thread, CState *st, StatsData *agg)
2279
2312
/* succeeded */
2280
2313
}
2281
2314
}
2282
- else if (pg_strcasecmp ( argv [ 0 ], "shell" ) == 0 )
2315
+ else if (command -> meta == META_SHELL )
2283
2316
{
2284
2317
bool ret = runShellCommand (st , NULL , argv + 1 , argc - 1 );
2285
2318
@@ -3023,6 +3056,7 @@ process_sql_command(PQExpBuffer buf, const char *source)
3023
3056
my_command = (Command * ) pg_malloc0 (sizeof (Command ));
3024
3057
my_command -> command_num = num_commands ++ ;
3025
3058
my_command -> type = SQL_COMMAND ;
3059
+ my_command -> meta = META_NONE ;
3026
3060
initSimpleStats (& my_command -> stats );
3027
3061
3028
3062
/*
@@ -3091,7 +3125,10 @@ process_backslash_command(PsqlScanState sstate, const char *source)
3091
3125
my_command -> argv [j ++ ] = pg_strdup (word_buf .data );
3092
3126
my_command -> argc ++ ;
3093
3127
3094
- if (pg_strcasecmp (my_command -> argv [0 ], "set" ) == 0 )
3128
+ /* ... and convert it to enum form */
3129
+ my_command -> meta = getMetaCommand (my_command -> argv [0 ]);
3130
+
3131
+ if (my_command -> meta == META_SET )
3095
3132
{
3096
3133
/* For \set, collect var name, then lex the expression. */
3097
3134
yyscan_t yyscanner ;
@@ -3146,7 +3183,7 @@ process_backslash_command(PsqlScanState sstate, const char *source)
3146
3183
expr_scanner_offset (sstate ),
3147
3184
true);
3148
3185
3149
- if (pg_strcasecmp ( my_command -> argv [ 0 ], "sleep" ) == 0 )
3186
+ if (my_command -> meta == META_SLEEP )
3150
3187
{
3151
3188
if (my_command -> argc < 2 )
3152
3189
syntax_error (source , lineno , my_command -> line , my_command -> argv [0 ],
@@ -3187,20 +3224,21 @@ process_backslash_command(PsqlScanState sstate, const char *source)
3187
3224
my_command -> argv [2 ], offsets [2 ] - start_offset );
3188
3225
}
3189
3226
}
3190
- else if (pg_strcasecmp ( my_command -> argv [ 0 ], "setshell" ) == 0 )
3227
+ else if (my_command -> meta == META_SETSHELL )
3191
3228
{
3192
3229
if (my_command -> argc < 3 )
3193
3230
syntax_error (source , lineno , my_command -> line , my_command -> argv [0 ],
3194
3231
"missing argument" , NULL , -1 );
3195
3232
}
3196
- else if (pg_strcasecmp ( my_command -> argv [ 0 ], "shell" ) == 0 )
3233
+ else if (my_command -> meta == META_SHELL )
3197
3234
{
3198
3235
if (my_command -> argc < 2 )
3199
3236
syntax_error (source , lineno , my_command -> line , my_command -> argv [0 ],
3200
3237
"missing command" , NULL , -1 );
3201
3238
}
3202
3239
else
3203
3240
{
3241
+ /* my_command->meta == META_NONE */
3204
3242
syntax_error (source , lineno , my_command -> line , my_command -> argv [0 ],
3205
3243
"invalid command" , NULL , -1 );
3206
3244
}
@@ -4592,12 +4630,12 @@ threadRun(void *arg)
4592
4630
timeout .tv_usec = min_usec % 1000000 ;
4593
4631
nsocks = select (maxsock + 1 , & input_mask , NULL , NULL , & timeout );
4594
4632
}
4595
- else /* nothing active, simple sleep */
4633
+ else /* nothing active, simple sleep */
4596
4634
{
4597
4635
pg_usleep (min_usec );
4598
4636
}
4599
4637
}
4600
- else /* no explicit delay, select without timeout */
4638
+ else /* no explicit delay, select without timeout */
4601
4639
{
4602
4640
nsocks = select (maxsock + 1 , & input_mask , NULL , NULL , NULL );
4603
4641
}
@@ -4614,8 +4652,10 @@ threadRun(void *arg)
4614
4652
goto done ;
4615
4653
}
4616
4654
}
4617
- else /* min_usec == 0, i.e. something needs to be executed */
4655
+ else
4618
4656
{
4657
+ /* min_usec == 0, i.e. something needs to be executed */
4658
+
4619
4659
/* If we didn't call select(), don't try to read any data */
4620
4660
FD_ZERO (& input_mask );
4621
4661
}
0 commit comments