mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-16 07:07:13 +02:00
Integrate MySQL module.
This commit is contained in:
18
module/Vendor/MDBC/examples/CMakeLists.txt
vendored
Normal file
18
module/Vendor/MDBC/examples/CMakeLists.txt
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
SET(EXAMPLE_FILES "mysql_affected_rows"
|
||||
"mysql_debug")
|
||||
|
||||
INCLUDE_DIRECTORIES(${CC_SOURCE_DIR}/include)
|
||||
|
||||
ENABLE_TESTING()
|
||||
|
||||
# this will be the main tests which saves output
|
||||
# from example files
|
||||
ADD_EXECUTABLE(test_output test_output.c)
|
||||
|
||||
FOREACH(EXAMPLE_FILE ${EXAMPLE_FILES})
|
||||
SET(XML_EXAMPLE_FILES $XML_EXAMPLE_FILES "examples/${EXAMPLE_FILE}.c")
|
||||
ADD_EXECUTABLE(${EXAMPLE_FILE} ${EXAMPLE_FILE}.c)
|
||||
TARGET_LINK_LIBRARIES(${EXAMPLE_FILE} mariadbclient)
|
||||
ADD_TEST(TEST_${EXAMPLE_FILE} ./${EXECUTABLE_OUTPUT_PATH}/test_output ./${EXAMPLE_FILE} ${CC_SOURCE_DIR}/examples/${EXAMPLE_FILE}.out ${CC_SOURCE_DIR}/examples/${EXAMPLE_FILE}.exp)
|
||||
|
||||
ENDFOREACH(EXAMPLE_FILE ${EXAMPLE_FILES})
|
82
module/Vendor/MDBC/examples/mysql_affected_rows.c
vendored
Normal file
82
module/Vendor/MDBC/examples/mysql_affected_rows.c
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
#include <mysql.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void show_error(MYSQL *mysql)
|
||||
{
|
||||
printf("Error(%d) [%s] \"%s\"", mysql_errno(mysql),
|
||||
mysql_sqlstate(mysql),
|
||||
mysql_error(mysql));
|
||||
mysql_close(mysql);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
MYSQL *mysql;
|
||||
const char *query;
|
||||
MYSQL_RES *result;
|
||||
|
||||
mysql= mysql_init(NULL);
|
||||
if (!mysql_real_connect(mysql, "localhost", "example", "example_pw",
|
||||
"example_db", 0, "/tmp/mysql.sock", 0))
|
||||
show_error(mysql);
|
||||
|
||||
query= "DROP TABLE IF EXISTS affected_rows";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
|
||||
query= "CREATE TABLE affected_rows (id int not null, my_name varchar(50),"
|
||||
"PRIMARY KEY(id))";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
|
||||
/* Affected rows with INSERT statement */
|
||||
query= "INSERT INTO affected_rows VALUES (1, \"First value\"),"
|
||||
"(2, \"Second value\")";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
printf("Affected_rows after INSERT: %lu\n",
|
||||
(unsigned long) mysql_affected_rows(mysql));
|
||||
|
||||
/* Affected rows with REPLACE statement */
|
||||
query= "REPLACE INTO affected_rows VALUES (1, \"First value\"),"
|
||||
"(2, \"Second value\")";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
printf("Affected_rows after REPLACE: %lu\n",
|
||||
(unsigned long) mysql_affected_rows(mysql));
|
||||
|
||||
/* Affected rows with UPDATE statement */
|
||||
query= "UPDATE affected_rows SET id=1 WHERE id=1";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
printf("Affected_rows after UPDATE: %lu\n",
|
||||
(unsigned long) mysql_affected_rows(mysql));
|
||||
|
||||
query= "UPDATE affected_rows SET my_name=\"Monty\" WHERE id=1";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
printf("Affected_rows after UPDATE: %lu\n",
|
||||
(unsigned long) mysql_affected_rows(mysql));
|
||||
|
||||
/* Affected rows after select */
|
||||
query= "SELECT id, my_name FROM affected_rows";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
result= mysql_store_result(mysql);
|
||||
printf("Affected_rows after SELECT and storing result set: %lu\n",
|
||||
(unsigned long) mysql_affected_rows(mysql));
|
||||
mysql_free_result(result);
|
||||
|
||||
/* Affected rows with DELETE statment */
|
||||
query= "DELETE FROM affected_rows";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
printf("Affected_rows after DELETE: %lu\n",
|
||||
(unsigned long) mysql_affected_rows(mysql));
|
||||
|
||||
mysql_close(mysql);
|
||||
return 0;
|
||||
}
|
40
module/Vendor/MDBC/examples/mysql_debug.c
vendored
Normal file
40
module/Vendor/MDBC/examples/mysql_debug.c
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
#include <mysql.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void show_error(MYSQL *mysql)
|
||||
{
|
||||
printf("Error(%d) [%s] \"%s\"", mysql_errno(mysql),
|
||||
mysql_sqlstate(mysql),
|
||||
mysql_error(mysql));
|
||||
mysql_close(mysql);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
MYSQL *mysql;
|
||||
const char *query;
|
||||
|
||||
mysql_debug("d:t:O");
|
||||
|
||||
mysql= mysql_init(NULL);
|
||||
|
||||
if (!mysql_real_connect(mysql, "localhost", "example", "example_pw",
|
||||
"example_db", 0, "/tmp/mysql.sock", 0))
|
||||
show_error(mysql);
|
||||
|
||||
query= "DROP TABLE IF EXISTS debug_example";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
|
||||
query= "CREATE TABLE debug_example (id int not null, my_name varchar(50),"
|
||||
"PRIMARY KEY(id))";
|
||||
if (mysql_real_query(mysql, query, strlen(query)))
|
||||
show_error(mysql);
|
||||
|
||||
mysql_close(mysql);
|
||||
|
||||
return 0;
|
||||
}
|
72
module/Vendor/MDBC/examples/test_output.c
vendored
Normal file
72
module/Vendor/MDBC/examples/test_output.c
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#define popen _popen
|
||||
#define pclose _pclose
|
||||
#endif
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char cmd_output[1024];
|
||||
char cmd_exp[1024];
|
||||
FILE *fp_exec, *fp_out, *fp_exp;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
printf("Syntax: test_output test output expected");
|
||||
exit(-1);
|
||||
}
|
||||
if (!(fp_exec= popen(argv[1], "r")))
|
||||
{
|
||||
printf("Failed to run %s\n", argv[1]);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (!(fp_out= fopen(argv[2], "w")))
|
||||
{
|
||||
printf("Failed to open %s for write\n", argv[2]);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
while (NULL != fgets(cmd_output, sizeof(cmd_output-1), fp_exec))
|
||||
{
|
||||
fputs(cmd_output, fp_out);
|
||||
}
|
||||
pclose(fp_exec);
|
||||
fflush(fp_out);
|
||||
fclose(fp_out);
|
||||
|
||||
if (argc == 3)
|
||||
return 0;
|
||||
|
||||
if (!(fp_exp= fopen(argv[3], "r")))
|
||||
{
|
||||
/* if no exp file exists, we just return
|
||||
without an error = skip check */
|
||||
return(0);
|
||||
}
|
||||
if (!(fp_out= fopen(argv[2], "r")))
|
||||
{
|
||||
printf("Failed to open %s for read\n", argv[2]);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
while (fgets(cmd_exp, sizeof(cmd_exp)-1, fp_exp))
|
||||
{
|
||||
if (!fgets(cmd_output, sizeof(cmd_output)-1, fp_out))
|
||||
{
|
||||
printf("Can't read from output file\n");
|
||||
goto error;
|
||||
}
|
||||
if (strcmp(cmd_output, cmd_exp))
|
||||
{
|
||||
printf("output and expected output are different\n");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
error:
|
||||
fclose(fp_exp);
|
||||
fclose(fp_out);
|
||||
return 1;
|
||||
}
|
Reference in New Issue
Block a user