Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

op:对sql接口进行修改 #7

Merged
merged 22 commits into from
May 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
838b741
commit
nomanker Apr 6, 2021
40b16e2
完成前端发送请求后端返回json功能,待修复bug
ggssh Apr 6, 2021
33a7c83
完成前端发送请求后端返回json功能,待修复bug
ggssh Apr 7, 2021
fa85dd7
完成前端发送请求后端返回json功能,待修复bug
ggssh Apr 7, 2021
71bc216
fixed:查询结果为空出现异常,resultset跳过第一行数据,解决已知内存泄漏
ggssh Apr 7, 2021
2bfe883
store
ggssh Apr 11, 2021
8221be1
完成sql连接放到main函数执行,ksql函数加前缀,将每次查询结果集内存回收
ggssh Apr 12, 2021
997f06c
完成sql连接放到main函数执行,ksql函数加前缀,将每次查询结果集内存回收
ggssh Apr 18, 2021
4dceef6
完成sql连接放到main函数执行,ksql函数加前缀,将每次查询结果集内存回收
ggssh Apr 21, 2021
79ff9c5
修复函数指针
nomanker Apr 21, 2021
391c580
Merge branch 'nomanker' of https://github.com/Kingtous/Sysy2020-llvm-…
ggssh Apr 21, 2021
e314add
opt:删掉CGUI不相关逻辑,删除无关define,对jsonToStr进行修改
ggssh Apr 29, 2021
4235c98
Merge branch 'ggssh' of https://github.com/Kingtous/WebC-llvm-compile…
ggssh Apr 29, 2021
206e5a8
Merge branch 'sysy' of https://github.com/Kingtous/WebC-llvm-compiler…
ggssh Apr 29, 2021
6ce498c
Merge branch 'ggssh' of https://github.com/Kingtous/WebC-llvm-compile…
ggssh Apr 29, 2021
e06c3e9
opt:删掉CGUI不相关逻辑,删除无关define,对jsonToStr进行修改
ggssh Apr 29, 2021
47eb98f
opt:连接数据库提供端口设置,定义WebCSQLData数据结构对ResultSet以及Statement进行封装,增加判断MySQL连…
ggssh May 2, 2021
a561900
Merge branch 'ggssh' of https://github.com/Kingtous/WebC-llvm-compile…
ggssh May 2, 2021
6ae8a34
opt:连接数据库提供端口设置,定义WebCSQLData数据结构对ResultSet以及Statement进行封装,增加判断MySQL连…
ggssh May 2, 2021
ee41e2f
Merge branch 'sysy' of https://github.com/Kingtous/WebC-llvm-compiler…
ggssh May 2, 2021
0960f38
opt:connect_db增加charset设置字符集类型
ggssh May 2, 2021
043fe9f
opt:connect_db增加charset设置字符集类型
ggssh May 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
opt:connect_db增加charset设置字符集类型
  • Loading branch information
ggssh committed May 2, 2021
commit 0960f38b228a6c728bef8e8a2337938475cd292c
5 changes: 4 additions & 1 deletion extern/ExternFunctionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ Function *ExternFunctionHandler::getOrAddConnectDB(LLVMContext &context, Module
context)->getPointerTo(),
Type::getInt8Ty(
context)->getPointerTo(),
Type::getInt32Ty(context)
Type::getInt32Ty(
context),
Type::getInt8Ty(
context)->getPointerTo()
},
false);
func = Function::Create(ty, llvm::GlobalValue::ExternalLinkage, "_ksql_connect_db", module);
Expand Down
15 changes: 6 additions & 9 deletions module/sql/src/ksql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

mysql::MySQL_Driver *driver;
Connection *conn;
//Statement *statement;
//ResultSet *resultSet;
WEBC_SQL_DATA webcSqlData;
unique_ptr<char[]> ch;
vector<string> ans;

string _ksql_resToJson(WEBC_SQL_DATA sqlData);

int _ksql_connect_db(const char *hostname, const char *username, const char *password, const char *schema, int port) {
int _ksql_connect_db(const char *hostname, const char *username, const char *password, const char *schema, int port,
const char *charset) {
try {
driver = mysql::get_mysql_driver_instance();
//连接到Mysql
Expand All @@ -25,14 +24,15 @@ int _ksql_connect_db(const char *hostname, const char *username, const char *pas
connection_properties["schema"] = schema;
connection_properties["port"] = port;
connection_properties["OPT_RECONNECT"] = true;
connection_properties["OPT_CHARSET_NAME"] = charset;
conn = driver->connect(connection_properties);
if (!conn) {
cout << "连接Mysql失败" << endl;
return FAILED;
}
return SUCCESS;
} catch (SQLException e) {
cout << e.getErrorCode() << endl;
cout << "The error code is : "<<e.getErrorCode() << endl;
cout << e.what() << endl;
}
}
Expand All @@ -45,10 +45,8 @@ const char *_ksql_query_db(const char *sqlSentence) {
_ksql_free_memory();
string temp = sqlSentence;
replace(temp.begin(), temp.end(), '\"', '\'');
// statement = conn->createStatement();
webcSqlData.statement = conn->createStatement();
sqlSentence = temp.c_str();
// resultSet = statement->executeQuery(sqlSentence);
webcSqlData.resultSet = webcSqlData.statement->executeQuery(sqlSentence);
if (!webcSqlData.resultSet->next()) {
char *resNull = "您所查询的表为空\n";
Expand All @@ -60,7 +58,7 @@ const char *_ksql_query_db(const char *sqlSentence) {
strcpy(ch.get(), _ksql_resToJson(webcSqlData).data());
return ch.get();
} catch (SQLException e) {
cout << e.getErrorCode() << endl;
cout << "The error code is : "<<e.getErrorCode() << endl;
cout << e.what() << endl;
}
}
Expand All @@ -72,7 +70,6 @@ string _ksql_resToJson(WEBC_SQL_DATA sqlData) {
s += "\"result\":";
s += "[";
//列数
// int count = result->getMetaData()->getColumnCount();
int count = sqlData.resultSet->getMetaData()->getColumnCount();
if (ans.empty()) {
sqlData.resultSet->beforeFirst();
Expand Down Expand Up @@ -108,7 +105,7 @@ string _ksql_resToJson(WEBC_SQL_DATA sqlData) {
vector<string>().swap(ans);
return s;
} catch (SQLException e) {
cout << e.getErrorCode() << endl;
cout << "The error code is : "<<e.getErrorCode() << endl;
cout << e.what() << endl;
}
}
Expand Down
12 changes: 7 additions & 5 deletions module/sql/src/ksql.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//操作成功状态码
#define SUCCESS 0


using namespace sql;
using namespace std;

Expand All @@ -35,23 +36,24 @@ typedef SQLData WEBC_SQL_DATA;

extern "C" {
/**
* 连接到mysql
* 连接到MySQL
* @param hostname
* @param username
* @param password
* @param schema
* @param port
* @param port 端口
* @param charset 字符集
* @return
*/
int _ksql_connect_db(const char *hostname, const char *username, const char *password, const char *schema, int port);
int _ksql_connect_db(const char *hostname, const char *username, const char *password, const char *schema, int port,const char *charset);
/**
* 释放资源
* 释放内存
* @return
*/
int _ksql_free_memory();
/**
* 查询数据
* @param sqlSentence
* @param sqlSentence select语句
* @return
*/
const char *_ksql_query_db(const char *sqlSentence);
Expand Down
2 changes: 1 addition & 1 deletion module/sql/src/ksqldemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "ksql.h"

int main() {
_ksql_connect_db("127.0.0.1", "root", "123456", "school", 3306);
_ksql_connect_db("127.0.0.1", "root", "123456", "school", 3306,"GBK");
// auto a = strToJson(_ksql_query_db("select * from subject"));
// cout<<jsonToStr(a);
cout << _ksql_query_db("select * from subject");
Expand Down
2 changes: 1 addition & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ str hehe() {

int main() {
echo('init...');
connect_db('127.0.0.1', 'root', '123456', 'school',3306);
connect_db('127.0.0.1', 'root', '123456', 'school',3306,'utf8');
str host = '0.0.0.0';
int port = 9000;
int core = 2;
Expand Down