Last active
April 24, 2024 02:39
-
-
Save YangKeao/51bbc88600c69691a799227806f32cd5 to your computer and use it in GitHub Desktop.
Test TiDB `SEND_LONG_LONG` protocol implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mysql/mysql.h> | |
#include <stdio.h> | |
#include <string.h> | |
int insert_with_config(char* encoding, bool send_with_long) { | |
MYSQL *con = mysql_init(NULL); | |
if (con == NULL) { | |
fprintf(stderr, "%s\n", mysql_error(con)); | |
return 1; | |
} | |
if (encoding != NULL) { | |
if(mysql_set_character_set(con, encoding)) { | |
return 1; | |
} | |
} | |
if (mysql_real_connect(con, "127.0.0.1", "root", "", | |
"test", 4000, NULL, 0) == NULL) { | |
fprintf(stderr, "%s\n", mysql_error(con)); | |
mysql_close(con); | |
return 1; | |
} | |
MYSQL_STMT *stmt = mysql_stmt_init(con); | |
if (stmt == NULL) { | |
fprintf(stderr, "mysql_stmt_init(), out of memory\n"); | |
return 1; | |
} | |
char* query = "INSERT INTO t VALUES (?, ?, cast(? as JSON), ?)"; | |
if (mysql_stmt_prepare(stmt, query, strlen(query))) { | |
fprintf(stderr, "mysql_stmt_prepare(), SELECT failed\n"); | |
fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); | |
return 1; | |
} | |
MYSQL_BIND bind[4]; | |
memset(bind, 0, sizeof(bind)); | |
int encoding_length = strlen(encoding); | |
bind[0].buffer_type = MYSQL_TYPE_STRING; | |
bind[0].buffer = (char *)encoding; | |
bind[0].is_null = 0; | |
bind[0].buffer_length = strlen(encoding); | |
char *param1 = "\"你好\""; | |
unsigned long str_length1 = strlen(param1); | |
for (int i = 1;i<=2;i++) { | |
bind[i].buffer_type = MYSQL_TYPE_STRING; | |
bind[i].buffer = (char *)param1; | |
bind[i].is_null = 0; | |
bind[i].buffer_length = str_length1; | |
bind[i].length = &str_length1; | |
} | |
bind[3].buffer_type = MYSQL_TYPE_BLOB; | |
bind[3].buffer = (char *)param1; | |
bind[3].is_null = 0; | |
bind[3].buffer_length = str_length1; | |
bind[3].length = &str_length1; | |
if (mysql_stmt_bind_param(stmt, bind)) { | |
fprintf(stderr, "mysql_stmt_bind_param() failed\n"); | |
fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); | |
return 1; | |
} | |
if (send_with_long) { | |
mysql_stmt_send_long_data(stmt, 0, encoding, encoding_length); | |
mysql_stmt_send_long_data(stmt, 1, param1, str_length1); | |
mysql_stmt_send_long_data(stmt, 2, param1, str_length1); | |
mysql_stmt_send_long_data(stmt, 3, param1, str_length1); | |
} | |
if (mysql_stmt_execute(stmt)) { | |
fprintf(stderr, "mysql_stmt_execute, 1 failed\n"); | |
fprintf(stderr, "%s\n", mysql_stmt_error(stmt)); | |
return 1; | |
} | |
mysql_stmt_close(stmt); | |
mysql_close(con); | |
return 0; | |
} | |
// create table t (encoding VARCHAR(16), t TEXT, j JSON, b BLOB); | |
// Compile with `gcc -o main main.c -lmysqlclient` | |
int main() { | |
int ret = insert_with_config("utf8", false); | |
if (ret != 0) { | |
return ret; | |
} | |
ret = insert_with_config("utf8", true); | |
if (ret != 0) { | |
return ret; | |
} | |
ret = insert_with_config("gbk", false); | |
if (ret != 0) { | |
return ret; | |
} | |
ret = insert_with_config("gbk", true); | |
if (ret != 0) { | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running this script needs an existing table:
You can compile this program with: