· 7 years ago · Feb 28, 2019, 05:36 PM
1DROP TABLE IF EXISTS `t1`;
2
3/*!40101 SET @saved_cs_client = @@character_set_client */;
4
5/*!40101 SET character_set_client = utf8 */;
6
7CREATE TABLE `t1` (
8 `id` int(11) DEFAULT NULL
9) ENGINE=InnoDB DEFAULT CHARSET=latin1;
10
11/*!40101 SET character_set_client = @saved_cs_client */;
12
13mysql_num_rows
14
15SELECT * FROM MY_TABLE;
16
17MYSQL * myh
18MYSQL_RES *query_result;
19unsigned long table_num_rows;
20
21/* Select all records present in the table */
22if (0 != mysql_query(myh, "SELECT * FROM MY_TABLE"))
23{
24 fprintf(stderr, "FAIL to perform the query : 'SELECT * FROM MY_TABLE' %sn", mysql_error(myh));
25
26 exit (EXIT_FAILURE);
27}
28
29query_result = mysql_store_result(myh);
30if (query_result)
31{
32 /* Retreive the number of rows returned by the query, which is the total number of rows in the table
33 * in our case.
34 */
35 table_num_rows = mysql_num_rows(query_result);
36 fprintf(stdout, "Our table contain %lun", table_num_rows)
37}
38
39static void dump_table(char *table, char *db)
40{
41 char ignore_flag;
42 char buf[200], table_buff[NAME_LEN+3];
43 DYNAMIC_STRING query_string;
44 char table_type[NAME_LEN];
45 char *result_table, table_buff2[NAME_LEN*2+3], *opt_quoted_table;
46 int error= 0;
47 ulong rownr, row_break, total_length, init_length;
48 uint num_fields;
49 MYSQL_RES *res;
50 MYSQL_RES *res_num_row; /* Add this */
51 MYSQL_FIELD *field;
52 MYSQL_ROW row;
53 char select_expr[QUERY_LENGTH];
54 unsigned long table_num_rows; /* Add this */
55 char table_num_rows_query[256]; /* Add this */
56 DBUG_ENTER("dump_table");
57
58 /* Add this */
59 /* Build the query to get the number of rows */
60 snprintf(table_num_rows_query, 256, "SELECT * FROM %s", table);
61
62 /*
63 * Make sure you get the create table info before the following check for
64 * --no-data flag below. Otherwise, the create table info won't be printed.
65 * */
66 num_fields= get_table_structure(table, db, table_type, &ignore_flag);
67
68 /*
69 * The "table" could be a view. If so, we don't do anything here.
70 * */
71 if (strcmp(table_type, "VIEW") == 0)
72 DBUG_VOID_RETURN;
73
74 /* Check --no-data flag */
75 if (opt_no_data)
76 {
77 verbose_msg("-- Skipping dump data for table '%s', --no-data was usedn",
78 table);
79 DBUG_VOID_RETURN;
80 }
81
82 DBUG_PRINT("info",
83 ("ignore_flag: %x num_fields: %d", (int) ignore_flag,
84 num_fields));
85 /*
86 * If the table type is a merge table or any type that has to be
87 * _completely_ ignored and no data dumped
88 * */
89 if (ignore_flag & IGNORE_DATA)
90 {
91 verbose_msg("-- Warning: Skipping data for table '%s' because "
92 "it's of type %sn", table, table_type);
93 DBUG_VOID_RETURN;
94 }
95 /* Check that there are any fields in the table */
96 if (num_fields == 0)
97 {
98 verbose_msg("-- Skipping dump data for table '%s', it has no fieldsn",
99 table);
100 DBUG_VOID_RETURN;
101 }
102
103 /*
104 * Check --skip-events flag: it is not enough to skip creation of events
105 * discarding SHOW CREATE EVENT statements generation. The myslq.event
106 * table data should be skipped too.
107 */
108 if (!opt_events && !my_strcasecmp(&my_charset_latin1, db, "mysql") &&
109 !my_strcasecmp(&my_charset_latin1, table, "event"))
110 {
111 verbose_msg("-- Skipping data table mysql.event, --skip-events was usedn");
112 DBUG_VOID_RETURN;
113 }
114
115 result_table= quote_name(table,table_buff, 1);
116 opt_quoted_table= quote_name(table, table_buff2, 0);
117
118 if (opt_lossless_fp && get_select_expr(table, select_expr))
119 exit(EX_MYSQLERR);
120
121 verbose_msg("-- Sending SELECT query...n");
122
123 /* Add this */
124 /* TODO : check if this is the right place to put our request */
125 if (0 != mysql_query(mysql, table_num_rows_query))
126 {
127 fprintf(stderr, "FAIL to perform the query : %s - %sn", table_num_rows_query, mysql_error(myh));
128
129 exit (EXIT_FAILURE);
130 }
131 res_num_row = mysql_store_result(mysql);
132 if (res_num_row)
133 {
134 /* Retreive the number of rows returned by the query, which is the total number of rows in the table
135 * in our case.
136 */
137 table_num_rows = mysql_num_rows(res_num_row);
138 fprintf(stdout, "Our table contain %lun", table_num_rows);
139 }
140 /* Freeing the result */
141 mysql_free_result(res_num_row);
142
143 init_dynamic_string_checked(&query_string, "", 1024, 1024);
144 /* The rest of the function here */