-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcgo.h
125 lines (92 loc) · 3.09 KB
/
cgo.h
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
#ifndef CGO_H
#define CGO_H
#include <stdlib.h>
#include <mysql.h>
typedef enum my_mode {
MY_MODE_NONE,
MY_MODE_TABLE,
MY_MODE_READER
} MY_MODE;
// This API provides convenient C wrapper functions for mysql client.
// !!! Call this before everything else !!!
extern void my_library_init(void);
// Create a connection. You must call my_close even if my_open fails.
extern int my_open(
MYSQL *mysql,
const char *host,
const char *user,
const char *passwd,
const char *db,
unsigned int port,
const char *unix_socket,
unsigned long client_flag
);
extern void my_close(MYSQL *mysql);
/*
Pass-through to mysql
*/
// Returns the current thread ID
extern unsigned long my_thread_id(MYSQL *mysql);
// Returns the error number for the most recently invoked MySQL function
extern unsigned int my_errno(MYSQL *mysql);
// Returns the error message for the most recently invoked MySQL function
extern const char *my_error(MYSQL *mysql);
// Toggles autocommit mode on/off
extern int my_autocommit(MYSQL *mysql, my_bool mode);
// Commit current transaction
extern int my_commit(MYSQL *mysql);
// Rollback current transaction
extern int my_rollback(MYSQL *mysql);
// Escapes special characters in a string for use in an SQL statement,
// taking into account the current character set of the connection
extern unsigned long my_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length);
/*
Query
*/
typedef struct my_res_meta {
unsigned int num_fields;
MYSQL_FIELD *fields;
} MY_RES_META;
typedef struct my_res {
my_ulonglong affected_rows;
my_ulonglong insert_id;
MY_RES_META meta;
MYSQL_RES *result;
} MY_RES;
typedef struct my_row {
int has_error;
MYSQL_ROW mysql_row;
unsigned long *lengths;
my_bool *is_nulls;
} MY_ROW;
// mode == MY_MODE_READER uses streaming (use_result). Otherwise it prefetches (store_result).
extern int my_query(MYSQL *mysql, MY_RES *res, const char *sql_str, unsigned long sql_len, MY_MODE mode);
// Iterate on this function until mysql_row == NULL or has_error != 0.
extern MY_ROW my_fetch_next(MYSQL *mysql, MY_RES *res);
// If my_query has results, you must call this before the next invocation.
extern void my_close_result(MYSQL *mysql, MY_RES *res);
/*
Prepared Statements
*/
typedef struct my_stmt {
MYSQL_STMT *s;
unsigned long param_count;
MY_RES_META meta;
my_bool meta_init;
char **row_cache;
size_t *row_cache_len;
MYSQL_BIND *outputs;
unsigned long *output_lengths;
} MY_STMT;
typedef struct my_stmt_res {
my_ulonglong affected_rows;
my_ulonglong insert_id;
} MY_STMT_RES;
extern int my_prepare(MY_STMT **stmt, MYSQL_BIND **binds, MYSQL *mysql, const char *sql_str, unsigned long sql_len);
extern int my_stmt_errno(MY_STMT *stmt);
extern const char *my_stmt_error(MY_STMT *stmt);
extern int my_stmt_execute(MY_STMT *stmt, MYSQL_BIND *binds, MY_STMT_RES *res, MY_MODE mode);
extern int my_stmt_close(MY_STMT *stmt, MYSQL_BIND *binds);
extern MY_ROW my_stmt_fetch_next(MY_STMT *stmt, MY_STMT_RES *res);
extern void my_stmt_close_result(MY_STMT *stmt, MY_STMT_RES *res);
#endif