forked from zkpig123/aqi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.c
774 lines (721 loc) · 23.2 KB
/
database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#include "aqi.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
/*
* establish a connect using default server,
* port,user,password,database,charset,return
* MYSQL connection if ok or null if error
*/
MYSQL *get_mysql_connection (void)
{
MYSQL *mysql = NULL;
if ((mysql = mysql_init(NULL)) == NULL) return NULL; //return NULL if error
if (mysql_options (mysql, MYSQL_SET_CHARSET_NAME, CLIENTCHARSET))
{
mysql_close (mysql);
return NULL; //return NULL if error
}
if (mysql_real_connect (mysql, HOST, MYSQLUSER, PASSWORD, DATABASE, MYSQLPORT, NULL, 0) == NULL)
{
mysql_close (mysql);
return NULL; //return NULL if error
}
return mysql;
}
//connection with multi statement
MYSQL *get_mysql_connection_multi_statement (void)
{
MYSQL *mysql = NULL;
if ((mysql = mysql_init(NULL)) == NULL) return NULL; //return NULL if error
if (mysql_options (mysql, MYSQL_SET_CHARSET_NAME, CLIENTCHARSET))
{
mysql_close (mysql);
return NULL; //return NULL if error
}
if (mysql_real_connect (mysql, HOST, MYSQLUSER, PASSWORD, DATABASE, MYSQLPORT, NULL, CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS) == NULL)
{
mysql_close (mysql);
return NULL; //return NULL if error
}
return mysql;
}
/*establish a connect using given arg,
* port,user,password,database,charset,return
* MYSQL connection if ok or null if error
*/
MYSQL* get_mysql_connection_arg (char *server, char *mysql_user,
char *password, char *database, unsigned int port, char *client_charset)
{
MYSQL *mysql = NULL;
//check arg
/*if (server == NULL || mysql_user == NULL
|| password == NULL || database == NULL
|| port < 1024 || client_charset == NULL) return NULL;*/
if ((mysql = mysql_init(NULL)) == NULL) return NULL; //return NULL if error
if (mysql_options (mysql, MYSQL_SET_CHARSET_NAME, client_charset))
{
mysql_close (mysql);
return NULL; //return NULL if error
}
if (mysql_real_connect (mysql, server, mysql_user, password, database, port, NULL, 0) == NULL)
{
mysql_close (mysql);
return NULL; //return NULL if error
}
return mysql;
}
/*
* get max_allowed_packet of mysql connection
* using sql query show variable like...,return
* correpsonding value, return 0 indicate
* error
*/
size_t get_mysql_connection_max_allowed_packet_bytes (MYSQL *mysql)
{
/*if (mysql == NULL)
{
fprintf (stderr, "get_mysql_connection_max_allowed_packet err:mysql is null");
return 0;
}*/
#ifdef MYSQL_OPT_MAX_ALLOWED_PACKET
unsigned long packet;
if (mysql_get_option (mysql, MYSQL_OPT_MAX_ALLOWED_PACKET, &packet))
{
fprintf (stderr, "mysql_get_option for max_allowed_packet err\n,mysql errno:%u,err:%s", mysql_errno (mysql), mysql_error (mysql));
return 0;
}
return (size_t) packet;
#else
char *get = "show variables like 'max_allowed_packet'";
if (mysql_real_query (mysql, get, strlen (get)))
{
fprintf (stderr, "mysql_real_query for max_allowed_packet err, errno:%u,err:%s\n", mysql_errno (mysql), mysql_error (mysql));
return 0;
}
MYSQL_RES *result;
if ((result = mysql_store_result (mysql)) == NULL)
{
fprintf (stderr, "mysql_store_result for max_allowed_packet err,errno:%u,err:%s\n", mysql_errno (mysql), mysql_error (mysql));
return 0;
}
MYSQL_ROW row;
my_ulonglong rows;
if ((rows = mysql_num_rows(result)) != 1)
{
fprintf (stderr, "mysql_num_rows not return 1 for max_allowed_packet\n");
return 0;
}
if ((row = mysql_fetch_row (result)) == NULL)
{
fprintf (stderr, "mysql_fetch_row for max_allowed_packet err,errno:%u,err:%s\n", mysql_errno (mysql), mysql_error (mysql));
return 0;
}
unsigned int fields, sn;
fields = mysql_num_fields (result);
sn = fields - 1;
unsigned long *lengths;
if ((lengths = mysql_fetch_lengths (result)) == NULL)
{
fprintf (stderr, "mysql_fetch_lengths for max_allowed_packet err\n,errno:%u,err:%s", mysql_errno (mysql), mysql_error (mysql));
return 0;
}
if (row[sn] == NULL)
{
fprintf (stderr, "null row of max_allowed_packet column\n");
return 0;
}
size_t size = 0;
for (unsigned int i = 0; i < lengths[sn]; i++)
{
if (!isdigit (row[sn][i]))
{
fprintf (stderr, "invalid character in result string of max_allowed_packet column value\n");
return 0;
}
else size = size * 10 + row[sn][i] - '0';
}
if (size == 0)
{
fprintf (stderr, "fetched value for max_packet_length failed\n");
return 0;
}
else return size;
#endif
}
//get a string used in mysql table creat sentence which require a enum type of the type of gas,
//for function creat_table_raw that create table of raw data
//like create table xxxx (enum ('so2','no2',.......'
//we want the string 'so2','no2','o3','co'............
//return pointer created using malloc to that string if success, caller should free it !
//return NULL if failed,
char *mysql_enum_type_string (void)
{
char *str = NULL;
size_t len = 0;
size_t total_len = 1;
for (int i = TYPE_ENUM_MIN; i <= TYPE_ENUM_MAX; i++)
{
char *item = get_type_name (i);
int tmp = total_len - 1;
len = strlen (item);
total_len += len + 2;
if (i != TYPE_ENUM_MIN) total_len++;
if ((str = realloc (str, total_len)) == NULL) return NULL;
if (i != TYPE_ENUM_MIN)
{
str[tmp] = ',';
tmp++;
}
str[tmp] = '\'';
strncpy (str + tmp + 1, item, len);
str[total_len - 2] = '\'';
}
str[total_len - 1] = '\0';
return str;
}
/*
* get a string used in mysql table creat
* sentence which require a enum type of
* the type of audit type for function
* creat_table_hour_average that create table
* of hour average like create table xxxx
* (enum ('B','BB'.......' , here we wanna the
* string 'B','BB',............
* return pointer created using malloc to that
* string if success, caller should free it !
* return NULL if failed,
*/
char *mysql_enum_audit_type_string (void)
{
char *str = NULL;
size_t len = 0;
size_t total_len = 1;
//here AUDIT_TYPE_ENUM_MAX + 1 contain the empty audit type
for (int i = AUDIT_TYPE_ENUM_MIN; i <= AUDIT_TYPE_ENUM_MAX + 1; i++)
{
char *item = get_audit_name (i);
int tmp = total_len - 1;
len = strlen (item);
total_len += len + 2;
if (i != AUDIT_TYPE_ENUM_MIN) total_len++;
if ((str = realloc (str, total_len)) == NULL) return NULL;
if (i != AUDIT_TYPE_ENUM_MIN)
{
str[tmp] = ',';
tmp++;
}
str[tmp] = '\'';
strncpy (str + tmp + 1, item, len);
str[total_len - 2] = '\'';
}
str[total_len - 1] = '\0';
return str;
}
/*create table of raw data with given arg, return 0 if ok, return other if error occur,mysql is connection having established to mysql server,
*/
int create_table_raw (uint16_t year, uint16_t month, uint16_t day, enum SITE sitetype, MYSQL *mysql)
{
char *enum_string;
char *query = NULL;
MYSQL_ROW mysql_row;
char *site;
int ret;
//check arg
/*if (mysql == NULL || check_year_month_day (year, month, day) || check_site (sitetype)) return 1; //arg invalid*/
if ((site = get_site_name (sitetype)) == NULL) return 2;
//construct the insert query
if ((enum_string = mysql_enum_type_string()) == NULL) return 2; //return 2 if get enum string fail
int query_len = strlen ("CREATE TABLE IF NOT EXISTS ") + strlen (site) + strlen ("_") + strlen (RAW_TABLE_NAME_PREFIX) + strlen ("YYYYMMDD (\
time DATETIME NOT NULL,\
type enum (") + strlen (enum_string) + strlen (") NOT NULL,\
value decimal(28,18) NOT NULL,\
flag varchar(3) DEFAULT NULL,\
PRIMARY KEY (time, type)) ENGINE=InnoDB DEFAULT CHARSET=gbk");
if ((query = malloc (query_len)) == NULL)
{
free (enum_string);
return 3; //return 3 if malloc failed
}
if ((ret = snprintf (query, query_len, "CREATE TABLE IF NOT EXISTS %s_%s%4" PRIu16 "%02" PRIu16 "%02" PRIu16 " \
(time DATETIME NOT NULL,\
type enum(%s) NOT NULL,\
value decimal(28,18) NOT NULL,\
flag varchar(3) DEFAULT NULL,\
PRIMARY KEY (time, type)) ENGINE=InnoDB DEFAULT CHARSET=gbk",
site, RAW_TABLE_NAME_PREFIX, year, month, day, enum_string)) < 0)
{
free (query);
free (enum_string);
return 4; //return 4 if sprintf failure
}
if ((size_t)ret + 1 > query_len)
{
free (query);
free (enum_string);
return 4; //return 4 if sprintf failure
}
free (enum_string);
if (mysql_query (mysql, query))
{
free (query);
return 5; //return 5 if mysql_query for exist of table failure
}
free (query);
return 0;
}
/*create table of hour average data with given arg, return 0 if ok, return other if error occur,mysql is
* connection having established to mysql server,
*/
int create_table_hour_average (uint16_t year, uint16_t month, enum SITE sitetype, MYSQL *mysql)
{
char *enum_string, *audit_enum_string;
char *month_string;
char *query = NULL;
MYSQL_ROW mysql_row;
char table_name[HOURAVERAGETABLENAMELEN + 1];
int ret;
char *site;
//check arg
//if (mysql == NULL || check_year_month_day (year, month, 0) || check_site (sitetype)) return 1; //arg invalid
if ((site = get_site_name (sitetype)) == NULL) return 1;
//construct the insert query
if ((enum_string = mysql_enum_type_string()) == NULL) return 2; //return 2 if get enum string fail
if ((audit_enum_string = mysql_enum_audit_type_string()) == NULL)
{
free (enum_string);
return 2; //return 2 if get enum string fail
}
month_string = get_month_string (month);
int query_len = strlen ("CREATE TABLE IF NOT EXISTS ") + strlen (site) + strlen ("_") + strlen (HOUR_AVERAGE_TABLE_NAME_PREFIX) + strlen ("YYYYMM (\
day DATE NOT NULL,\
hour tinyint NOT NULL,\
type enum(") + strlen (enum_string) + strlen (") NOT NULL,\
value decimal(28,18) NOT NULL,\
audit_type enum(") + strlen (audit_enum_string) + strlen (") DEFAULT '',\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day,hour,type)) ENGINE=InnoDB DEFAULT CHARSET=gbk") + 1;
if ((query = malloc (query_len)) == NULL)
{
free (audit_enum_string);
free (enum_string);
return 3; //return 3 if malloc failed
}
if ((ret = snprintf (query, query_len, "CREATE TABLE IF NOT EXISTS %s_%s%4" PRIu16 "%s (\
day DATE NOT NULL,\
hour tinyint NOT NULL,\
type enum(%s) NOT NULL,\
value decimal(28,18) NOT NULL,\
audit_type enum(%s) DEFAULT '',\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day,hour,type)) ENGINE=InnoDB DEFAULT CHARSET=gbk",
site, HOUR_AVERAGE_TABLE_NAME_PREFIX, year, month_string, enum_string, audit_enum_string)) < 0)
{
free (enum_string);
free (audit_enum_string);
free (query);
return 4; //return 4 if sprintf failure
}
if ((size_t)ret + 1 > query_len)
{
free (enum_string);
free (audit_enum_string);
free (query);
return 4; //return 4 if sprintf failure
}
free (enum_string);
free (audit_enum_string);
if (mysql_query (mysql, query))
{
fprintf (stderr, "create hour average table failed,query string : %s\n", query);
free (query);
return 5; //return 5 if mysql_query for exist of table failure
}
free (query);
return 0;
}
/*check existence of table named "raw_YYYYMMDD" in mysql database corresponding to arg,
**mysql is connection established to mysql, can be null if not supplied,if so established
one using predefined macro of servername,port,username,password,database in including file
**return 1 if exist, or 0 not exist, or other if error occur
*/
int check_raw_table_exist (int year, int month, int day, char *site, MYSQL *mysql)
{
MYSQL_RES *mysql_res = NULL;
MYSQL_ROW mysql_row;
int exist_table = 0;
size_t query_len, name_len;
char *query, *name;
int ret;
//check arg
//if (check_year_month_day (year, month, day) || mysql == NULL || site == NULL) return 1; //arg invalid
query_len = strlen ("SHOW TABLES LIKE '") + strlen (site) + strlen ("_") + strlen (RAW_TABLE_NAME_PREFIX) + strlen ("YYYYMMDD") + 1;
name_len = strlen (site) + strlen ("_raw_YYYYMMDD") + 1;
if ((query = malloc (query_len)) == NULL || (name = malloc (name_len)) == NULL) return 2;
//construct query string
if ((ret = snprintf (query, query_len, "SHOW TABLES LIKE '%s_%s%4" PRIu16 "%02" PRIu16 "%02" PRIu16 "'", site, RAW_TABLE_NAME_PREFIX, year, month, day)) < 0) return 5; //return 5 if sprintf failure
else if ((size_t)ret + 1 > query_len) return 5;
if ((ret = snprintf (name, name_len, "%s_%s%4" PRIu16 "%02" PRIu16 "%02" PRIu16, site, RAW_TABLE_NAME_PREFIX, year, month, day)) < 0) return 6; //return 6 if snprintf failure for table name
else if ((size_t)ret + 1 > query_len) return 5;
//query and process
if (mysql_query (mysql, query)) return 7; //return 7 if mysql_query for exist of table failure
if ((mysql_res = mysql_store_result (mysql)) == NULL) return 8; //return 8 if mysql_store_result for exist of table failure
for (;;)
{
unsigned long *lengths;
unsigned int num_fields;
if (mysql_row = mysql_fetch_row (mysql_res)) break;
if ((lengths = mysql_fetch_lengths (mysql_res)) == NULL)
{
mysql_free_result (mysql_res);
return 9; //return 9 if mysql_fetch_lengths for exist of table failure
}
num_fields = mysql_num_fields (mysql_res);
for (int i = 0; i < num_fields; i++)
{
if (lengths[i] != name_len - 1) continue;
if (strncmp (mysql_row[i], name, RAWTABLENAMELEN)) continue;
else
{
mysql_free_result (mysql_res);
return 1;
}
}
}
mysql_free_result(mysql_res);
return 0;
}
/*create table of day average data with given arg, return 0 if ok, return other if error occur,mysql is
* connection having established to mysql server, site is name of place,tyg or tsg
*/
int create_table_day_average (enum SITE sitetype, MYSQL *mysql)
{
char *enum_string, *audit_enum_string;
char *query = NULL;
MYSQL_ROW mysql_row;
int query_len;
char *site;
int ret;
//check arg
//if (mysql == NULL || check_site (sitetype)) return 1; //arg invalid
if ((site = get_site_name (sitetype)) == NULL) return 6;
//construct the insert query
if ((enum_string = mysql_enum_type_string()) == NULL) return 2; //return 2 if get enum string fail
query_len = strlen ("CREATE TABLE IF NOT EXISTS ") + strlen (site) + strlen (DAY_AVERAGE_TABLE_NAME_SUFFIX) + strlen (" (\
day DATE NOT NULL,\
type enum (") + strlen (enum_string) + strlen (") NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day,type)) ENGINE=InnoDB DEFAULT CHARSET=gbk");
if ((query = malloc (query_len)) == NULL)
{
free (enum_string);
return 3; //return 3 if malloc failed
}
if ((ret = snprintf (query, query_len, "CREATE TABLE IF NOT EXISTS %s%s (\
day DATE NOT NULL,\
type enum(%s) NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day,type)) ENGINE=InnoDB DEFAULT CHARSET=gbk",
site, DAY_AVERAGE_TABLE_NAME_SUFFIX, enum_string)) < 0)
{
free (enum_string);
free (query);
return 4; //return 4 if sprintf failure
}
if ((size_t)ret + 1 > query_len)
{
free (enum_string);
free (query);
return 4; //return 4 if sprintf failure
}
free (enum_string);
if (mysql_query (mysql, query))
{
fprintf (stderr, "create day average table failed,query string : %s\n", query);
free (query);
return 5; //return 5 if mysql_query for exist of table failure
}
free (query);
return 0;
}
/*
* create table of O3 8h moving average max of day,
* return 0 if ok, return other if error occur
*/
int create_table_O3_8h_moving_average_max (enum SITE sitetype, MYSQL *mysql)
{
char *query;
char *site;
size_t len;
int ret;
//if (check_site (sitetype) || mysql == NULL) return -1;
if ((site = get_site_name (sitetype)) == NULL) return -1;
len = strlen ("CREATE TABLE IF NOT EXISTS ") + strlen (site) + strlen (O3_8H_MOVING_AVERAGE_MAX_TABLE_NAME_SUFFIX) + strlen (" (\
day date NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day)) ENGINE=InnoDB DEFAULT CHARSET=gbk") + 1;
if ((query = malloc (len)) == NULL) return -2;
if ((ret = snprintf (query, len, "CREATE TABLE IF NOT EXISTS %s%s (\
day date NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day)) ENGINE=InnoDB DEFAULT CHARSET=gbk", site, O3_8H_MOVING_AVERAGE_MAX_TABLE_NAME_SUFFIX)) < 0)
{
free (query);
return -3;
}
if ((size_t)ret + 1 > len)
{
free (query);
return -3;
}
if (mysql_real_query (mysql, query, strlen (query)))
{
fprintf (stderr, "create table of O3 8h moving average max of day,query:%s\nerrno:%u, error:%s\n", query, mysql_errno (mysql), mysql_error (mysql));
free (query);
return -4;
}
free (query);
return 0;
}
/*create table of O3 8h moving average data with given arg, return 0 if ok, return other if error occur,mysql is
* connection having established to mysql server, site is name of place,tyg or tsg
*/
int create_table_O3_8h_moving_average (uint16_t year, uint16_t month, enum SITE sitetype, MYSQL *mysql)
{
char *query = NULL;
MYSQL_ROW mysql_row;
char *site;
size_t query_len;
int ret;
//check arg
//if (mysql == NULL || year > 9999 || check_site (sitetype)) return 1; //arg invalid
if ((site = get_site_name (sitetype)) == NULL) return 6;
//construct the insert query
query_len = strlen ("CREATE TABLE IF NOT EXISTS ") + strlen (site) + strlen ("_") + strlen (O3_8H_MOVING_AVERAGE_TABLE_NAME_PREFIX) + strlen ("YYYYMM") + strlen (" (\
day DATE NOT NULL,\
hour tinyint NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day,hour)) ENGINE=InnoDB DEFAULT CHARSET=gbk") + 1;
if ((query = malloc (query_len)) == NULL)
{
return 3; //return 3 if malloc failed
}
if ((ret = snprintf (query, query_len, "CREATE TABLE IF NOT EXISTS %s_%s%" PRIu16 "%02" PRIu16 " (\
day DATE NOT NULL,\
hour tinyint NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day,hour)) ENGINE=InnoDB DEFAULT CHARSET=gbk",
site, O3_8H_MOVING_AVERAGE_TABLE_NAME_PREFIX, year, month)) < 0)
{
free (query);
return 4; //return 4 if sprintf failure
}
if ((size_t)ret + 1 > query_len)
{
free (query);
return 4; //return 4 if sprintf failure
}
if (mysql_query (mysql, query))
{
fprintf (stderr, "create day average table failed,query string : %s\nerrno:%u,err:%s", query,mysql_errno (mysql), mysql_error (mysql));
free (query);
return 5; //return 5 if mysql_query for exist of table failure
}
free (query);
return 0;
}
int create_table_O3_1h_max (enum SITE sitetype, MYSQL *mysql)
{
char *query;
size_t len;
char *site;
int ret;
//if (check_site (sitetype) || mysql == NULL) return -1; //arg invalid
if ((site = get_site_name (sitetype)) == NULL) return -1;
len = strlen ("CREATE TABLE IF NOT EXISTS ") + strlen (site) + strlen (O3_1H_MAX_TABLE_NAME_SUFFIX) + strlen (" (\
day date NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day)) ENGINE=InnoDB DEFAULT CHARSET=gbk") + 1;
if ((query = malloc (len)) == NULL) return -2;
if ((ret = snprintf (query, len, "CREATE TABLE IF NOT EXISTS %s%s (\
day date NOT NULL,\
value decimal(28,18) NOT NULL,\
aqi smallint DEFAULT NULL,\
reserved varchar(20) DEFAULT '',\
PRIMARY KEY (day)) ENGINE=InnoDB DEFAULT CHARSET=gbk", site, O3_1H_MAX_TABLE_NAME_SUFFIX)) < 0)
{
free (query);
return -3;
}
if ((size_t)ret + 1 > len)
{
free (query);
return -3;
}
if (mysql_real_query (mysql, query, strlen (query)))
{
free (query);
return -4;
}
free (query);
return 0;
}
//check whether given table exist, return 1 if exist, 0 not exist, other indicating an error
int is_table_exist (char *table, MYSQL *mysql)
{
//if (table == NULL || mysql == NULL) return 2; //return 2 if arg invalid
//prepare query string
size_t len = strlen ("SHOW TABLES LIKE '") + strlen (table) + strlen ("'") + 1;
int ret;
char *query;
if ((query = malloc (len)) == NULL) return 3;
if ((ret = snprintf (query, len, "SHOW TABLES LIKE '%s'", table)) < 0)
{
free (query);
return 3;
}
if ((size_t)ret + 1 > len)
{
free (query);
return 3;
}
//issue query and save result and retrieve row
MYSQL_RES *result;
my_ulonglong nums;
if (mysql_real_query (mysql, query, len - 1))
{
free (query);
return 4;
}
free (query);
if ((result = mysql_store_result (mysql)) == NULL)
{
return 4;
}
if ((nums = mysql_num_rows (result)) != (my_ulonglong) 1)
{
mysql_free_result (result);
return 0;
}
else
{
mysql_free_result (result);
return 1;
}
}
/*
* construct mysql lock query of hour average
*/
char *construct_hour_average_table_lock_mysql_query (uint16_t year, uint16_t month, enum SITE sitetype)
{
char *query;
size_t len;
int ret;
char *site;
//if (year > 9999 || month > 12 || month < 1 || check_site (sitetype)) return NULL;
if ((site = get_site_name (sitetype)) == NULL) return NULL;
len = strlen ("LOCK TABLES ") + SITENAMEMAX + strlen ("_") + strlen (HOUR_AVERAGE_TABLE_NAME_PREFIX) + strlen ("YYYYMM WRITE");
if ((query = malloc (len)) == NULL) return NULL;
if ((ret = snprintf (query, len, "LOCK TABLES %s_%s%" PRIu16 "%02" PRIu16 " WRITE", site, HOUR_AVERAGE_TABLE_NAME_PREFIX, year, month)) < 0 || (size_t)ret + 1 > len)
{
free (query);
return NULL;
}
return query;
}
/*
* construct mysql lock query of day average
*/
char *construct_day_average_table_lock_mysql_query (enum SITE sitetype)
{
char *query;
size_t len;
int ret;
char *site;
//if (check_site (sitetype)) return NULL;
if ((site = get_site_name (sitetype)) == NULL) return NULL;
len = strlen ("LOCK TABLES ") + SITENAMEMAX + strlen (DAY_AVERAGE_TABLE_NAME_SUFFIX) + strlen (" WRITE");
if ((query = malloc (len)) == NULL) return NULL;
if ((ret = snprintf (query, len, "LOCK TABLES %s%s WRITE", site, DAY_AVERAGE_TABLE_NAME_SUFFIX)) < 0 || (size_t)ret + 1 > len)
{
free (query);
return NULL;
}
return query;
}
/*
* construct mysql lock query of O3 8h
* moving average
*/
char *construct_O3_moving_average_table_lock_mysql_query (enum SITE sitetype, uint16_t year, uint16_t month)
{
char *query;
size_t len;
int ret;
char *site;
//if (check_site (sitetype)) return NULL;
if ((site = get_site_name (sitetype)) == NULL) return NULL;
len = strlen ("LOCK TABLES ") + SITENAMEMAX + strlen ("_") + strlen (O3_8H_MOVING_AVERAGE_TABLE_NAME_PREFIX) + strlen ("YYYYMM") + strlen (" WRITE") + 1;
if ((query = malloc (len)) == NULL) return NULL;
if ((ret = snprintf (query, len, "LOCK TABLES %s_%s%" PRIu16 "%02" PRIu16 " WRITE", site, O3_8H_MOVING_AVERAGE_TABLE_NAME_PREFIX, year, month)) < 0 || (size_t)ret + 1 > len)
{
free (query);
return NULL;
}
return query;
}
/*
* construct mysql lock query of O3 1h max
*/
char *construct_O3_1h_max_table_lock_mysql_query (enum SITE sitetype)
{
char *query;
size_t len;
int ret;
char *site;
//if (check_site (sitetype)) return NULL;
if ((site = get_site_name (sitetype)) == NULL) return NULL;
len = strlen ("LOCK TABLES ") + SITENAMEMAX + strlen (O3_1H_MAX_TABLE_NAME_SUFFIX) + strlen (" WRITE");
if ((query = malloc (len)) == NULL) return NULL;
if ((ret = snprintf (query, len, "LOCK TABLES %s%s WRITE", site, O3_1H_MAX_TABLE_NAME_SUFFIX)) < 0 || (size_t)ret + 1 > len)
{
free (query);
return NULL;
}
return query;
}
char *construct_O3_moving_average_max_table_lock_mysql_query (enum SITE sitetype)
{
char *query;
size_t len;
int ret;
char *site;
//if (check_site (sitetype)) return NULL;
if ((site = get_site_name (sitetype)) == NULL) return NULL;
len = strlen ("LOCK TABLES ") + SITENAMEMAX + strlen (O3_8H_MOVING_AVERAGE_MAX_TABLE_NAME_SUFFIX) + strlen (" WRITE");
if ((query = malloc (len)) == NULL) return NULL;
if ((ret = snprintf (query, len, "LOCK TABLES %s%s WRITE", site, O3_8H_MOVING_AVERAGE_MAX_TABLE_NAME_SUFFIX)) < 0 || (size_t)ret + 1 > len)
{
free (query);
return NULL;
}
return query;
}