1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 #include "sql_command_test.h"
24 #include "plugin_log.h"
25 #include "plugin.h"
26 
27 /*
28   The basic test method to check for the execution of the CRUD command.
29 
30   Case 1 - Test the creation of the create command.
31            Creates two tables in the test database. Does a select and check
32            that the tables exist.
33 
34   Case 2 - Test the creation of the insert command.
35            Insert values in the tables. Do a select to see that the values
36            exist in the table.
37 
38   Case 3 - Test the creation of the update command.
39            Update the values inserted in the Case 2. Do a select to see that
40            the new values are now there in the table.
41 
42   Case 4 - Test the creation of the delete command.
43            Delete values from the table. Do a select to see the values do not
44            exist. Drop the table and verify that the tables are deleted.
45 */
46 
check_sql_command_create(Sql_service_interface * srvi)47 void check_sql_command_create(Sql_service_interface *srvi)
48 {
49   Sql_resultset rset;
50   int srv_err= srvi->execute_query("CREATE TABLE test.t1 (i INT PRIMARY KEY NOT NULL);");
51   if (srv_err == 0)
52   {
53     srvi->execute_query("SHOW TABLES IN test;", &rset);
54     std::string str= "t1";
55     assert(rset.getString(0) == str);
56   }
57   else
58   {
59     log_message(MY_ERROR_LEVEL, "query execution resulted in failure. errno: %d", srv_err); /* purecov: inspected */
60   }
61 }
62 
check_sql_command_insert(Sql_service_interface * srvi)63 void check_sql_command_insert(Sql_service_interface *srvi)
64 {
65   Sql_resultset rset;
66   int srv_err;
67   srv_err= srvi->execute_query("INSERT INTO test.t1 VALUES(1);");
68   srv_err= srvi->execute_query("INSERT INTO test.t1 VALUES(2);");
69   srv_err= srvi->execute_query("INSERT INTO test.t1 VALUES(3);");
70   if (srv_err == 0)
71   {
72     srvi->execute_query("SELECT * FROM test.t1", &rset);
73     uint i= 0;
74     std::vector<std::string > insert_values;
75     insert_values.push_back("1");
76     insert_values.push_back("2");
77     insert_values.push_back("3");
78     assert(rset.get_rows() == 3);
79     while (i < rset.get_rows())
80     {
81       assert(rset.getString(0) == insert_values[i]);
82       rset.next();
83       i++;
84     }
85   }
86   else
87   {
88     log_message(MY_ERROR_LEVEL, "query execution resulted in failure. errno: %d", srv_err); /* purecov: inspected */
89   }
90 }
91 
92 
check_sql_command_update(Sql_service_interface * srvi)93 void check_sql_command_update(Sql_service_interface *srvi)
94 {
95   Sql_resultset rset;
96   int srv_err;
97   srv_err= srvi->execute_query("UPDATE test.t1 SET i=4 WHERE i=1;");
98   srv_err= srvi->execute_query("UPDATE test.t1 SET i=5 WHERE i=2;");
99   srv_err= srvi->execute_query("UPDATE test.t1 SET i=6 WHERE i=3;");
100   if (srv_err == 0)
101   {
102     srvi->execute_query("SELECT * FROM test.t1", &rset);
103     uint i= 0;
104     std::vector<std::string> update_values;
105     update_values.push_back("4");
106     update_values.push_back("5");
107     update_values.push_back("6");
108     assert(rset.get_rows() == 3);
109     while (i < rset.get_rows())
110     {
111       assert(rset.getString(0) == update_values[i]);
112       rset.next();
113       i++;
114     }
115   }
116   else
117   {
118     log_message(MY_ERROR_LEVEL, "query execution resulted in failure. errno: %d", srv_err); /* purecov: inspected */
119   }
120 }
121 
122 
check_sql_command_drop(Sql_service_interface * srvi)123 void check_sql_command_drop(Sql_service_interface *srvi)
124 {
125   Sql_resultset rset;
126   int srv_err= srvi->execute_query("DROP TABLE test.t1;");
127   if (srv_err == 0)
128   {
129     srvi->execute_query("SELECT TABLES IN test", &rset);
130     std::string str= "t1";
131     assert(rset.get_rows() == 0);
132   }
133   else
134   {
135     log_message(MY_ERROR_LEVEL, "query execution resulted in failure. errno: %d", srv_err); /* purecov: inspected */
136   }
137 }
138 
sql_command_check()139 int sql_command_check()
140 {
141   int error=1;
142   Sql_service_interface *srvi= new Sql_service_interface();
143 
144   if (srvi == NULL)
145   {
146     /* purecov: begin inspected */
147     log_message(MY_ERROR_LEVEL, "Unable to create a session for executing the"
148                 " queries on the server");
149     return error;
150     /* purecov: end */
151   }
152 
153   error= srvi->open_session();
154   assert(!error);
155 
156   /* Case 1 */
157 
158   check_sql_command_create(srvi);
159 
160   /* Case 2 */
161 
162   check_sql_command_insert(srvi);
163 
164   /* Case 3 */
165 
166   check_sql_command_update(srvi);
167 
168   /* Case 4 */
169 
170   check_sql_command_drop(srvi);
171 
172   delete srvi;
173   return error;
174 }
175