/* Copyright (C) 2003-2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/***************************************************************************
This is a test sample to test the new features in MySQL client-server
protocol
Main author: venu ( venu@mysql.com )
***************************************************************************/
/*
XXX: PLEASE RUN THIS PROGRAM UNDER VALGRIND AND VERIFY THAT YOUR TEST
DOESN'T CONTAIN WARNINGS/ERRORS BEFORE YOU PUSH.
*/
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
#include <errmsg.h>
#include <my_getopt.h>
#include <m_string.h>
#define VER "2.1"
#define MAX_TEST_QUERY_LENGTH 300 /* MAX QUERY BUFFER LENGTH */
#define MAX_KEY MAX_INDEXES
#define MAX_SERVER_ARGS 64
/* set default options */
static int opt_testcase = 0;
static char *opt_db= 0;
static char *opt_user= 0;
static char *opt_password= 0;
static char *opt_host= 0;
static char *opt_unix_socket= 0;
static unsigned int opt_port;
static my_bool tty_password= 0, opt_silent= 0;
static MYSQL *mysql= 0;
static char query[MAX_TEST_QUERY_LENGTH];
static char current_db[]= "client_test_db";
static unsigned int test_count= 0;
static unsigned int opt_count= 0;
static unsigned int iter_count= 0;
static my_bool have_innodb= FALSE;
static const char *opt_basedir= "./";
static longlong opt_getopt_ll_test= 0;
static int embedded_server_arg_count= 0;
static char *embedded_server_args[MAX_SERVER_ARGS];
static const char *embedded_server_groups[]= {
"server",
"embedded",
"mysql_client_test_SERVER",
NullS
};
static time_t start_time, end_time;
static double total_time;
const char *default_dbug_option= "d:t:o,/tmp/mysql_client_test.trace";
struct my_tests_st
{
const char *name;
void (*function)();
};
#define myheader(str) \
if (opt_silent < 2) \
{ \
fprintf(stdout, "\n\n#####################################\n"); \
fprintf(stdout, "%d of (%d/%d): %s", test_count++, iter_count, \
opt_count, str); \
fprintf(stdout, " \n#####################################\n"); \
}
#define myheader_r(str) \
if (!opt_silent) \
{ \
fprintf(stdout, "\n\n#####################################\n"); \
fprintf(stdout, "%s", str); \
fprintf(stdout, " \n#####################################\n"); \
}
static void print_error(const char *msg);
static void print_st_error(MYSQL_STMT *stmt, const char *msg);
static void client_disconnect();
/*
Abort unless given experssion is non-zero.
SYNOPSIS
DIE_UNLESS(expr)
DESCRIPTION
We can't use any kind of system assert as we need to
preserve tested invariants in release builds as well.
*/
#define DIE_UNLESS(expr) \
((void) ((expr) ? 0 : (die(__FILE__, __LINE__, #expr), 0)))
#define DIE(expr) \
die(__FILE__, __LINE__, #expr)
void die(const char *file, int line, const char *expr)
{
fprintf(stderr, "%s:%d: check failed: '%s'\n", file, line, expr);
abort();
}
#define myerror(msg) print_error(msg)
#define mysterror(stmt, msg) print_st_error(stmt, msg)
#define myquery(RES) \
{ \
int r= (RES); \
if (r) \
myerror(NULL); \
DIE_UNLESS(r == 0); \
}
#define myquery_r(r) \
{ \
if (r) \
myerror(NULL); \
DIE_UNLESS(r != 0); \
}
#define check_execute(stmt, r) \
{ \
if (r) \
mysterror(stmt, NULL); \
DIE_UNLESS(r == 0);\
}
#define check_execute_r(stmt, r) \
{ \
if (r) \
mysterror(stmt, NULL); \
DIE_UNLESS(r != 0);\
}
#define check_stmt(stmt) \
{ \
if ( stmt == 0) \
myerror(NULL); \
DIE_UNLESS(stmt != 0); \
}
#define check_stmt_r(stmt) \
{ \
if (stmt == 0) \
myerror(NULL);\
DIE_UNLESS(stmt == 0);\
}
#define mytest(x) if (!x) {myerror(NULL);DIE_UNLESS(FALSE);}
#define mytest_r(x) if (x) {myerror(NULL);DIE_UNLESS(FALSE);}
/* A workaround for Sun Forte 5.6 on Solaris x86 */
static int cmp_double(double *a, double *b)
{
return *a == *b;
}
/* Print the error message */
static void print_error(const char *msg)
{
if (!opt_silent)
{
if (mysql && mysql_errno(mysql))
{
if (mysql->server_version)
fprintf(stdout, "\n [MySQL-%s]", mysql->server_version);
else
fprintf(stdout, "\n [MySQL]");
fprintf(stdout, "[%d] %s\n", mysql_errno(mysql), mysql_error(mysql));
}
else if (msg)
fprintf(stderr, " [MySQL] %s\n", msg);
}
}
static void print_st_error(MYSQL_STMT *stmt, const char *msg)
{
if (!opt_silent)
{
if (stmt && mysql_stmt_errno(stmt))
{
if (stmt->mysql && stmt->mysql->server_version)
fprintf(stdout, "\n [MySQL-%s]", stmt->mysql->server_version);
else
fprintf(stdout, "\n [MySQL]");
fprintf(stdout, "[%d] %s\n", mysql_stmt_errno(stmt),
mysql_stmt_error(stmt));
}
else if (msg)
fprintf(stderr, " [MySQL] %s\n", msg);
}
}
/* Check if the connection has InnoDB tables */
static my_bool check_have_innodb(MYSQL *conn)
{
MYSQL_RES *res;
MYSQL_ROW row;
int rc;
my_bool result;
rc= mysql_query(conn, "show variables like 'have_innodb'");
myquery(rc);
res= mysql_use_result(conn);
DIE_UNLESS(res);
row= mysql_fetch_row(res);
DIE_UNLESS(row);
result= strcmp(row[1], "YES") == 0;
mysql_free_result(res);
return result;
}
/*
This is to be what mysql_query() is for mysql_real_query(), for
mysql_simple_prepare(): a variant without the 'length' parameter.
*/
MYSQL_STMT *STDCALL
mysql_simple_prepare(MYSQL *mysql, const char *query)
{
MYSQL_STMT *stmt= mysql_stmt_init(mysql);
if (stmt && mysql_stmt_prepare(stmt, query, strlen(query)))
{
mysql_stmt_close(stmt);
return 0;
}
return stmt;
}
/* Connect to the server */
static void client_connect(ulong flag)
{
int rc;
myheader_r("client_connect");
if (!opt_silent)
fprintf(stdout, "\n Establishing a connection to '%s' ...",
opt_host ? opt_host : "");
if (!(mysql= mysql_init(NULL)))
{
opt_silent= 0;
myerror("mysql_init() failed");
exit(1);
}
if (!(mysql_real_connect(mysql, opt_host, opt_user,
opt_password, opt_db ? opt_db:"test", opt_port,
opt_unix_socket, flag)))
{
opt_silent= 0;
myerror("connection failed");
mysql_close(mysql);
fprintf(stdout, "\n Check the connection options using --help or -?\n");
exit(1);
}
mysql->reconnect= 1;
if (!opt_silent)
fprintf(stdout, " OK");
/* set AUTOCOMMIT to ON*/
mysql_autocommit(mysql, TRUE);
if (!opt_silent)
{
fprintf(stdout, "\nConnected to MySQL server version: %s (%lu)\n",
mysql_get_server_info(mysql),
(ulong) mysql_get_server_version(mysql));
fprintf(stdout, "\n Creating a test database '%s' ...", current_db);
}
strxmov(query, "CREATE DATABASE IF NOT EXISTS ", current_db, NullS);
rc= mysql_query(mysql, query);
myquery(rc);
strxmov(query, "USE ", current_db, NullS);
rc= mysql_query(mysql, query);
myquery(rc);
have_innodb= check_have_innodb(mysql);
if (!opt_silent)
fprintf(stdout, " OK");
}
/* Close the connection */
static void client_disconnect()
{
myheader_r("client_disconnect");
if (mysql)
{
if (!opt_silent)
fprintf(stdout, "\n dropping the test database '%s' ...", current_db);
strxmov(query, "DROP DATABASE IF EXISTS ", current_db, NullS);
没有合适的资源?快使用搜索试试~ 我知道了~
嵌入式mysql编程例子
共90个文件
h:22个
pl:15个
c:10个
3星 · 超过75%的资源 需积分: 12 23 下载量 44 浏览量
2008-09-09
18:25:26
上传
评论
收藏 3.64MB RAR 举报
温馨提示
几个嵌入式mysql编程例子,适应于初学都
资源推荐
资源详情
资源评论
收起资源包目录
examples.rar (90个子文件)
examples
tests
ssl_test.c 2KB
insert_test.c 2KB
fork_big2.pl 17KB
grant.res 31KB
rename_test.pl 5KB
test_delayed_insert.pl 10KB
mail_to_db.pl 16KB
lock_test.res 495B
insert_and_repair.pl 5KB
grant.pl 27KB
fork_big.pl 14KB
pmail.pl 7KB
mysql_client_test.c 394KB
lock_test.pl 3KB
function.tst 4KB
big_record.pl 3KB
table_types.pl 5KB
select_test.c 2KB
auto_increment.res 2KB
showdb_test.c 2KB
deadlock_test.c 6KB
auto_increment.tst 1KB
list_test.c 2KB
connect_test.c 2KB
myisam-big-rows.tst 2KB
export.pl 6KB
truncate.pl 3KB
drop_test.pl 6KB
thread_test.c 7KB
udf_test.res 3KB
fork2_test.pl 7KB
function.res 9KB
udf_example
udf_example.def 283B
udf_example.dsp 4KB
udf_example.dsw 516B
libmysqltest
mytest.dsw 535B
libmysql.def 3KB
myTest.suo 12KB
myTest-package.dsp 3KB
my_getopt.h 3KB
config-netware.h 4KB
my_dbug.h 4KB
mysql_embed.h 1KB
myTest.ncb 6.41MB
mysql_version.h 790B
mytest.c 3KB
myTest.vcproj 5KB
mysql.h 32KB
mytest.opt 54KB
mysqld_ername.h 16KB
myTest.dsp 3KB
m_ctype.h 19KB
raid.h 6KB
typelib.h 1KB
errmsg.h 4KB
my_pthread.h 25KB
release
Mytest.obj 10KB
myTest.exe 40KB
myTest.exe 40KB
config-os2.h 22KB
mysqld_error.h 16KB
config-win.h 13KB
my_list.h 1KB
m_string.h 8KB
my_sys.h 35KB
mytest.plg 2KB
myTest.vcproj.DEV09.Administrator.user 1KB
mysql_com.h 16KB
libmysql.lib 34KB
myTest.sln 876B
debug
mytest.exe.embed.manifest 146B
libmysql.dll 1.48MB
mytest.sbr 0B
vc60.pdb 68KB
vc80.pdb 76KB
vc60.idb 41KB
myTest.bsc 3.13MB
BuildLog.htm 9KB
myTest.pdb 1.52MB
mytest.exe.intermediate.manifest 145B
mt.dep 67B
mytest.obj 25KB
mytest.exe.embed.manifest.res 212B
vc80.idb 131KB
mytest.exe 340KB
libmysql.lib 34KB
mytest.ilk 176KB
my_global.h 41KB
mysql_time.h 2KB
my_alloc.h 2KB
共 90 条
- 1
资源评论
- qs_coding2011-11-14不是嵌入式mysql的代码文件,楼主不厚道。
- qq3246346342012-12-06哎。对初学者的我。也一头雾水。
zhuqinwu
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功