Commit 9c993074 authored by yiwenshao's avatar yiwenshao

escape demo added

parent d92defce
...@@ -2,26 +2,27 @@ SRCFILES=$(wildcard mysqllib/*.cc) ...@@ -2,26 +2,27 @@ SRCFILES=$(wildcard mysqllib/*.cc)
OBJFILES=$(patsubst mysqllib/%.cc,obj/%.o,$(SRCFILES)) OBJFILES=$(patsubst mysqllib/%.cc,obj/%.o,$(SRCFILES))
CXXFLAGS=-I/usr/include/mysql -fabi-version=2 -fno-omit-frame-pointer -std=c++11 CXXFLAGS=-I/usr/include/mysql -fabi-version=2 -fno-omit-frame-pointer -std=c++11
LDFLAGS=-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -ldl LDFLAGS=-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -ldl
CXX=g++ CXX=g++
.PHONY: all .PHONY: all
executables:=main backFieldsToFiles executables:=main backFieldsToFiles client escape
all: $(executables) all: $(executables)
createInsert: obj/createInsert.o $(OBJFILES)
$(CXX) -o $@ $^ $(LDFLAGS)
main: obj/main.o $(OBJFILES) main: obj/main.o $(OBJFILES)
$(CXX) -o $@ $^ $(LDFLAGS) $(CXX) -o $@ $^ $(LDFLAGS)
backFieldsToFiles: obj/backFieldsToFiles.o $(OBJFILES) backFieldsToFiles: obj/backFieldsToFiles.o $(OBJFILES)
$(CXX) -o $@ $^ $(LDFLAGS) $(CXX) -o $@ $^ $(LDFLAGS)
analysis: obj/analysis.o $(OBJFILES) client:obj/client.o $(OBJFILES)
$(CXX) -o $@ $^ $(LDFLAGS)
escape:obj/escape.o $(OBJFILES)
$(CXX) -o $@ $^ $(LDFLAGS) $(CXX) -o $@ $^ $(LDFLAGS)
.PHONY:clean .PHONY:clean
......
#include <iostream>
#include "mysqllib/utilities.h"
#include "mysqllib/MyConnect.h"
#include <vector>
#include <string>
using namespace std;
extern Connect *con;
string createSelect(string database,string table,bool isQuote=true){
auto dbresult = con->execute(string("SELECT * FROM `")+database+"`.`"+string(table)+"` LIMIT 1;");
DBResult * result = dbresult.get();
vector<vector<string>> rows = result->getRows();
vector<enum_field_types> types = result->getTypes();
vector<string> fields = result->getFields();
string head = "SELECT ";
for(int i=0;i<types.size();i++){
if(IS_NUM(types[i])){
head += fields[i]+",";
}
else{
if(isQuote)
head+=string("QUOTE(")+fields[i]+"),";
else head+=string("HEX(")+fields[i]+"),";
}
}
head[head.size()-1]=' ';
head += "FROM `"+database+"`.`"+table+"`";
//cout<<head<<endl;
return head;
}
vector<string> getTables(string db){
string query = string("SHOW TABLES IN ")+db;
auto dbresult = con->execute(query);
DBResult * result = dbresult.get();
vector<vector<string>> rows = result->getRows();
vector<enum_field_types> types = result->getTypes();
vector<string> fieldNames = result->getFields();
vector<string> res;
for(auto item:rows){
assert(item.size()==1);
res.push_back(item[0]);
}
return res;
}
int main(int argc,char**argv){
string query;
getline(cin,query);
while(query!=string("quit")){
auto dbresult = con->execute(query);
DBResult * result = dbresult.get();
if(result!=NULL){
vector<vector<string>> rows = result->getRows();
vector<enum_field_types> types = result->getTypes();
vector<string> fieldNames = result->getFields();
result->printRows();
}
getline(cin,query);
}
return 0;
}
#include <iostream>
#include "mysqllib/utilities.h"
#include "mysqllib/MyConnect.h"
#include <vector>
#include <string>
using namespace std;
int main(int argc,char**argv){
return 0;
}
...@@ -86,9 +86,6 @@ vector<string> getTables(string db){ ...@@ -86,9 +86,6 @@ vector<string> getTables(string db){
} }
int main(int argc,char**argv){ int main(int argc,char**argv){
if(argc!=4){ if(argc!=4){
cout<<"numOfpipe, db, table"<<endl; cout<<"numOfpipe, db, table"<<endl;
......
...@@ -92,23 +92,28 @@ Connect::execute(const std::string &query){ ...@@ -92,23 +92,28 @@ Connect::execute(const std::string &query){
vector<vector<string>> rows; vector<vector<string>> rows;
vector<string> fields; vector<string> fields;
vector<enum_field_types> types; vector<enum_field_types> types;
vector<vector<int>> lengths;
if(num_fields==0){ if(num_fields==0){
return std::make_shared<DBResult>(rows,fields,types); return std::make_shared<DBResult>(rows,fields,types,lengths);
} }
MYSQL_ROW row; MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) { while ((row = mysql_fetch_row(result))) {
unsigned long * fieldLen = mysql_fetch_lengths(result); unsigned long * fieldLen = mysql_fetch_lengths(result);
vector<string> curRow; vector<string> curRow;
vector<int> curLength;
for(int i = 0; i < num_fields; i++) { for(int i = 0; i < num_fields; i++) {
if(row[i]==NULL){ if(row[i]==NULL){
curRow.push_back(string("NULL")); curRow.push_back(string("NULL"));
curLength.push_back(0);
}else{ }else{
curRow.push_back(string(row[i],fieldLen[i])); curRow.push_back(string(row[i],fieldLen[i]));
curLength.push_back(fieldLen[i]);
} }
} }
rows.push_back(curRow); rows.push_back(curRow);
lengths.push_back(curLength);
} }
MYSQL_FIELD *field; MYSQL_FIELD *field;
...@@ -122,7 +127,7 @@ Connect::execute(const std::string &query){ ...@@ -122,7 +127,7 @@ Connect::execute(const std::string &query){
} }
} }
return std::make_shared<DBResult>(rows,fields,types); return std::make_shared<DBResult>(rows,fields,types,lengths);
} }
Connect::~Connect() { Connect::~Connect() {
...@@ -137,6 +142,13 @@ DBResult::printRows(){ ...@@ -137,6 +142,13 @@ DBResult::printRows(){
} }
cout<<endl; cout<<endl;
} }
for(auto oneRow:lengths){
for(auto len:oneRow){
cout<<len<<"\t\t";
}
cout<<endl;
}
} }
void DBResult::printRowsF2(){ void DBResult::printRowsF2(){
......
...@@ -49,8 +49,8 @@ class DBResult { ...@@ -49,8 +49,8 @@ class DBResult {
public: public:
DBResult():affected_rows(-1),insert_id(-1){} DBResult():affected_rows(-1),insert_id(-1){}
DBResult(vector<vector<string>> inRows,vector<string >inFields, DBResult(vector<vector<string>> inRows,vector<string >inFields,
vector<enum_field_types> inTypes):affected_rows(-1),insert_id(-1), vector<enum_field_types> inTypes,vector<vector<int>> inLengths):affected_rows(-1),insert_id(-1),
rows(inRows),fields(inFields),types(inTypes){ rows(inRows),fields(inFields),types(inTypes),lengths(inLengths){
for(auto item:types){ for(auto item:types){
typesString.push_back(gtm[item]); typesString.push_back(gtm[item]);
} }
...@@ -67,6 +67,7 @@ class DBResult { ...@@ -67,6 +67,7 @@ class DBResult {
const uint64_t affected_rows; const uint64_t affected_rows;
const uint64_t insert_id; const uint64_t insert_id;
const vector<vector<string>> rows; const vector<vector<string>> rows;
const vector<vector<int>> lengths;
const vector<string> fields; const vector<string> fields;
const vector<enum_field_types> types; const vector<enum_field_types> types;
vector<string> typesString; vector<string> typesString;
......
demo:demo.c
gcc -o demo demo.c `mysql_config --cflags --libs`
#include <my_global.h>
#include <mysql.h>
void get_version(){
printf("MySQL client version: %s\n", mysql_get_client_info());
}
void finish_with_error(MYSQL *con){
fprintf(stderr, "%s\n", mysql_error(con));
return;
}
MYSQL_RES * queryResults(MYSQL *con,char* query){
if (mysql_query(con, query)) {
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) {
printf("query no results");
}
return result;
}
MYSQL * getConnection(char *ip,char *user,char *password, char *db){
MYSQL *con = mysql_init(NULL);
if (con == NULL){
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if (mysql_real_connect(con, ip, user, password,
db, 0, NULL, 0) == NULL) {
finish_with_error(con);
}
return con;
}
void *showResults(MYSQL *con,MYSQL_RES *result){
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
char *esp = (char*)malloc(sizeof(char)*27);
while ((row = mysql_fetch_row(result))) {
//print escaped string here
unsigned long * fieldLen = mysql_fetch_lengths(result);
for(int i = 0; i < num_fields; i++) {
mysql_real_escape_string(con,esp,row[i],fieldLen[i]);
printf("%s ", esp? esp : "NULL");
}
printf("\n");
}
}
int main(int argc, char **argv){
get_version();
MYSQL *con = getConnection("localhost","root","letmein","tdb");
MYSQL_RES *result = queryResults(con,"select * from student");
showResults(con,result);
mysql_free_result(result);
mysql_close(con);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment