1
/* Copyright (C) 2000-2001, 2003-2004, 2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
23
typedef unsigned char uchar;
24
static void die(char* fmt, ...);
25
static void safe_query(MYSQL* mysql, char* query, int read_ok);
26
static void run_query_batch(int* order, int num_queries);
27
static void permute(int *order, int num_queries);
28
static void permute_aux(int *order, int num_queries, int* fixed);
29
static void dump_result(MYSQL* mysql, char* query);
43
MYSQL lock, sel, del_ins;
45
struct query queries[] =
47
{&del_ins, "insert delayed into foo values(1)", 1, 0, 0},
48
{&del_ins, "insert delayed into foo values(1)", 1, 0, 0},
49
{&lock, "lock tables foo write", 1, 1, 0},
50
{&lock, "unlock tables", 1,2, 0},
51
{&sel, "select * from foo", 0,0, 0},
52
{&del_ins, "insert into foo values(4)", 0,3, 0},
56
static void die(char* fmt, ...)
60
fprintf(stderr, "ERROR: ");
61
vfprintf(stderr, fmt, args);
62
fprintf(stderr, "\n");
67
static void permute(int *order, int num_queries)
70
if(num_queries < 2) return;
71
if(!(fixed = (int*)malloc(num_queries * sizeof(int))))
72
die("malloc() failed");
74
memset(fixed, 0, num_queries * sizeof(int));
75
permute_aux(order, num_queries, fixed);
80
static order_ok(int *order, int num_queries)
82
int i,j, pri_i, pri_j;
83
for(i = 0; i < num_queries; i++)
85
if((pri_i = queries[order[i]].pri))
86
for(j = i + 1; j < num_queries; j++)
88
pri_j = queries[order[j]].pri;
89
if(pri_j && pri_i > pri_j)
97
static void permute_aux(int *order, int num_queries, int* fixed)
99
int *p,*p1,j,i,tmp, num_free = 0;
101
for(i = 0; i < num_queries; i++, p++)
107
for(j = 0, p1 = fixed ;
108
j < num_queries; j++,p1++)
116
permute_aux(order, num_queries, fixed);
127
/*printf("num_free = %d\n", num_free); */
132
if(order_ok(order, num_queries))
133
run_query_batch(order, num_queries);
137
static void run_query_batch(int* order, int num_queries)
142
safe_query(&lock, "delete from foo", 1);
144
for(i = 0; i < num_queries; i++,order++)
146
q = queries + *order;
147
printf("query='%s'\n", q->query);
148
safe_query(q->mysql, q->query, q->read_ok);
151
for(i = 0; i < num_queries; i++,order++)
153
q = queries + *order;
155
dump_result(q->mysql, q->query);
161
static void safe_net_read(NET* net, char* query)
164
len = my_net_read(net);
165
if(len == packet_error || !len)
166
die("Error running query '%s'", query);
167
if(net->read_pos[0] == 255)
168
die("Error running query '%s'", query);
172
static void safe_query(MYSQL* mysql, char* query, int read_ok)
175
NET* net = &mysql->net;
177
if(net_write_command(net,(uchar)COM_QUERY, query,strlen(query)))
178
die("Error running query '%s': %s", query, mysql_error(mysql));
181
safe_net_read(net, query);
185
static void dump_result(MYSQL* mysql, char* query)
188
safe_net_read(&mysql->net, query);
189
res = mysql_store_result(mysql);
191
mysql_free_result(res);
194
static int* init_order(int* num_queries)
197
int *order, *order_end, *p;
200
for(q = queries; q->mysql; q++)
204
if(!(order = (int*) malloc(n * sizeof(int))))
205
die("malloc() failed");
206
order_end = order + n;
207
for(p = order,i = 0; p < order_end; p++,i++)
215
char* user = "root", *pass = "", *host = "localhost", *db = "test";
216
int *order, num_queries;
217
order = init_order(&num_queries);
218
if(!mysql_init(&lock) || !mysql_init(&sel) || !mysql_init(&del_ins))
219
die("error in mysql_init()");
221
mysql_options(&lock, MYSQL_READ_DEFAULT_GROUP, "mysql");
222
mysql_options(&sel, MYSQL_READ_DEFAULT_GROUP, "mysql");
223
mysql_options(&del_ins, MYSQL_READ_DEFAULT_GROUP, "mysql");
225
if(!mysql_real_connect(&lock, host, user, pass, db, 0,0,0 ) ||
226
!mysql_real_connect(&sel, host, user, pass, db, 0,0,0 ) ||
227
!mysql_real_connect(&del_ins, host, user, pass, db, 0,0,0 ))
228
die("Error in mysql_real_connect(): %s", mysql_error(&lock));
229
lock.reconnect= sel.reconnect= del_ins.reconnect= 1;
231
permute(order, num_queries);
232
printf("count = %d\n", count);
236
mysql_close(&del_ins);