Commit adbbc18b authored by casualet's avatar casualet

newest test,quote do not work for \n

parent aff376c7
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
using std::cout; using std::cout;
using std::cin; using std::cin;
using std::endl; using std::endl;
using std::string;
std::map<SECLEVEL,std::string> gmp; std::map<SECLEVEL,std::string> gmp;
std::map<onion,std::string> gmp2; std::map<onion,std::string> gmp2;
...@@ -107,14 +108,16 @@ rawReturnValue executeAndGetResultRemote(Connect * curConn,std::string query){ ...@@ -107,14 +108,16 @@ rawReturnValue executeAndGetResultRemote(Connect * curConn,std::string query){
rawReturnValue myRaw; rawReturnValue myRaw;
if(dbres==nullptr||dbres->n==NULL){ if(dbres==nullptr||dbres->n==NULL){
std::cout<<"no results"<<std::endl; //std::cout<<"no results"<<std::endl;
return myRaw; return myRaw;
} }
int num = mysql_num_rows(dbres->n); int num = mysql_num_rows(dbres->n);
if(num!=0)
std::cout<<"num of rows: "<<num<<std::endl; std::cout<<"num of rows: "<<num<<std::endl;
int numOfFields = mysql_num_fields(dbres->n); int numOfFields = mysql_num_fields(dbres->n);
if(numOfFields!=0)
std::cout<<"num of fields: "<<numOfFields<<std::endl; std::cout<<"num of fields: "<<numOfFields<<std::endl;
MYSQL_FIELD *field; MYSQL_FIELD *field;
...@@ -145,7 +148,7 @@ static ...@@ -145,7 +148,7 @@ static
void printrawReturnValue(rawReturnValue & cur) { void printrawReturnValue(rawReturnValue & cur) {
int len = cur.fieldTypes.size(); int len = cur.fieldTypes.size();
if(len==0){ if(len==0){
std::cout<<"zero output"<<std::endl; //std::cout<<"zero output"<<std::endl;
return ; return ;
} }
...@@ -182,7 +185,7 @@ bool myRewrite(std::string curQuery,unsigned long long _thread_id,std::string cl ...@@ -182,7 +185,7 @@ bool myRewrite(std::string curQuery,unsigned long long _thread_id,std::string cl
ProxyState *const ps = c_wrapper->ps.get(); ProxyState *const ps = c_wrapper->ps.get();
assert(ps); assert(ps);
c_wrapper->last_query = curQuery; c_wrapper->last_query = curQuery;
std::cout<<RED_BEGIN<<"start my rewrite"<<COLOR_END<<std::endl; //std::cout<<RED_BEGIN<<"start my rewrite"<<COLOR_END<<std::endl;
try{ try{
TEST_Text(retrieveDefaultDatabase(_thread_id, ps->getConn(), TEST_Text(retrieveDefaultDatabase(_thread_id, ps->getConn(),
&c_wrapper->default_db), &c_wrapper->default_db),
...@@ -244,8 +247,8 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in ...@@ -244,8 +247,8 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in
} }
rows.push_back(curTempRow); rows.push_back(curTempRow);
} }
uint64_t afrow = globalConn->get_affected_rows(); //uint64_t afrow = globalConn->get_affected_rows();
std::cout<<GREEN_BEGIN<<"Affected rows: "<<afrow<<COLOR_END<<std::endl; //std::cout<<GREEN_BEGIN<<"Affected rows: "<<afrow<<COLOR_END<<std::endl;
return ResType(true, 0 , return ResType(true, 0 ,
in_last_insert_id, std::move(names), in_last_insert_id, std::move(names),
std::move(types), std::move(rows)); std::move(types), std::move(rows));
...@@ -255,8 +258,8 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in ...@@ -255,8 +258,8 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in
//printResType for testing purposes //printResType for testing purposes
static static
void parseResType(const ResType &rd) { void parseResType(const ResType &rd) {
std::cout<<RED_BEGIN<<"rd.affected_rows: "<<rd.affected_rows<<COLOR_END<<std::endl; // std::cout<<RED_BEGIN<<"rd.affected_rows: "<<rd.affected_rows<<COLOR_END<<std::endl;
std::cout<<RED_BEGIN<<"rd.insert_id: "<<rd.insert_id<<COLOR_END<<std::endl; // std::cout<<RED_BEGIN<<"rd.insert_id: "<<rd.insert_id<<COLOR_END<<std::endl;
for(auto name:rd.names){ for(auto name:rd.names){
std::cout<<name<<"\t"; std::cout<<name<<"\t";
...@@ -294,7 +297,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) { ...@@ -294,7 +297,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) {
switch (result_type){ switch (result_type){
//execute the query, fetch the results, and call next again //execute the query, fetch the results, and call next again
case AbstractQueryExecutor::ResultType::QUERY_COME_AGAIN: { case AbstractQueryExecutor::ResultType::QUERY_COME_AGAIN: {
std::cout<<RED_BEGIN<<"case one"<<COLOR_END<<std::endl; //std::cout<<RED_BEGIN<<"case one"<<COLOR_END<<std::endl;
const auto &output = const auto &output =
std::get<1>(new_results)->extract<std::pair<bool, std::string> >(); std::get<1>(new_results)->extract<std::pair<bool, std::string> >();
const auto &next_query = output.second; const auto &next_query = output.second;
...@@ -308,7 +311,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) { ...@@ -308,7 +311,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) {
//only execute the query, without processing the retults //only execute the query, without processing the retults
case AbstractQueryExecutor::ResultType::QUERY_USE_RESULTS:{ case AbstractQueryExecutor::ResultType::QUERY_USE_RESULTS:{
std::cout<<RED_BEGIN<<"case two"<<COLOR_END<<std::endl; //std::cout<<RED_BEGIN<<"case two"<<COLOR_END<<std::endl;
const auto &new_query = const auto &new_query =
std::get<1>(new_results)->extract<std::string>(); std::get<1>(new_results)->extract<std::string>();
auto resRemote = executeAndGetResultRemote(globalConn,new_query); auto resRemote = executeAndGetResultRemote(globalConn,new_query);
...@@ -318,7 +321,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) { ...@@ -318,7 +321,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) {
//return the results to the client directly //return the results to the client directly
case AbstractQueryExecutor::ResultType::RESULTS:{ case AbstractQueryExecutor::ResultType::RESULTS:{
std::cout<<RED_BEGIN<<"case three"<<COLOR_END<<std::endl; //std::cout<<RED_BEGIN<<"case three"<<COLOR_END<<std::endl;
const auto &res = new_results.second->extract<ResType>(); const auto &res = new_results.second->extract<ResType>();
parseResType(res); parseResType(res);
break; break;
...@@ -338,7 +341,7 @@ void batchTogether(std::string client, std::string curQuery,unsigned long long _ ...@@ -338,7 +341,7 @@ void batchTogether(std::string client, std::string curQuery,unsigned long long _
//the first step is to Rewrite, we abort this session if we fail here. //the first step is to Rewrite, we abort this session if we fail here.
bool resMyRewrite = myRewrite(curQuery,_thread_id,client); bool resMyRewrite = myRewrite(curQuery,_thread_id,client);
if(!resMyRewrite){ if(!resMyRewrite){
std::cout<<"my rewrite error in batch"<<std::endl; //std::cout<<"my rewrite error in batch"<<std::endl;
return ; return ;
} }
myNext(client,true, MygetResTypeFromLuaTable(true)); myNext(client,true, MygetResTypeFromLuaTable(true));
...@@ -441,7 +444,7 @@ static std::unique_ptr<SchemaInfo> myLoadSchemaInfo() { ...@@ -441,7 +444,7 @@ static std::unique_ptr<SchemaInfo> myLoadSchemaInfo() {
} }
int int
main() { main(int argc,char ** argv) {
gmp[SECLEVEL::INVALID]="INVALID"; gmp[SECLEVEL::INVALID]="INVALID";
gmp[SECLEVEL::PLAINVAL]="PLAINVAL"; gmp[SECLEVEL::PLAINVAL]="PLAINVAL";
gmp[SECLEVEL::OPE]="OPE"; gmp[SECLEVEL::OPE]="OPE";
...@@ -459,6 +462,11 @@ main() { ...@@ -459,6 +462,11 @@ main() {
gmp2[oBESTEFFORT]="oBESTEFFORT"; gmp2[oBESTEFFORT]="oBESTEFFORT";
gmp2[oINVALID]="oINVALID"; gmp2[oINVALID]="oINVALID";
string targetDb;
if(argc==2){
targetDb = string(argv[1]);
}
std::string client="192.168.1.1:1234"; std::string client="192.168.1.1:1234";
//one Wrapper per user. //one Wrapper per user.
clients[client] = new WrapperState(); clients[client] = new WrapperState();
...@@ -484,8 +492,11 @@ main() { ...@@ -484,8 +492,11 @@ main() {
globalConn = new Connect(ci.server, ci.user, ci.passwd, ci.port); globalConn = new Connect(ci.server, ci.user, ci.passwd, ci.port);
std::string curQuery = "SHOW DATABASES;"; std::string curQuery = "SHOW DATABASES;";
std::cout<<"please input a new query:######################################################"<<std::endl; std::cout<<"please input a new query:######################################################"<<std::endl;
if(targetDb.size()==0)
std::getline(std::cin,curQuery); std::getline(std::cin,curQuery);
else curQuery = string("use ")+targetDb;
unsigned long long _thread_id = globalConn->get_thread_id(); unsigned long long _thread_id = globalConn->get_thread_id();
long long countWrapper = 0;
while(curQuery!="quit"){ while(curQuery!="quit"){
if(curQuery.size()==0){ if(curQuery.size()==0){
std::cout<<std::endl; std::cout<<std::endl;
...@@ -494,11 +505,14 @@ main() { ...@@ -494,11 +505,14 @@ main() {
processSchemaInfo(*schema); processSchemaInfo(*schema);
continue; continue;
} }
std::cout<<GREEN_BEGIN<<"curQuery: "<< countWrapper++;
curQuery<<"\n"<<COLOR_END<<std::endl;
batchTogether(client,curQuery,_thread_id); batchTogether(client,curQuery,_thread_id);
std::cout<<GREEN_BEGIN<<"\nplease input a new query:#######"<<COLOR_END<<std::endl; std::cout<<GREEN_BEGIN<<"\nplease input a new query:#######"<<COLOR_END<<std::endl;
std::getline(std::cin,curQuery); std::getline(std::cin,curQuery);
if(countWrapper==2){
cout<<"bingo"<<endl;
countWrapper=0;
}
} }
return 0; return 0;
} }
...@@ -6,9 +6,96 @@ ...@@ -6,9 +6,96 @@
using namespace std; using namespace std;
extern Connect *con; 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+"`";
return head;
}
//http://php.net/manual/zh/function.mysql-escape-string.php
//https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_quote
//backup in configurable extended version
static int numOfPipe = 3;
void backupselect(string query,string table){
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();
string head = string("INSERT INTO ")+"`"+table+"`"+string(" VALUES (");
for(auto i=0;i<rows.size();i++){
string cur=head;
for(int j=0;j<rows[i].size();j++){
if(IS_NUM(types[j]))
cur+=rows[i][j]+",";
else{
cur+=rows[i][j]+",";
}
}
cur[cur.size()-1]=')';
for(int k=1;k<numOfPipe;k++){
//for each pipe
i++;
if(i>=rows.size()) break;
cur+=",(";
for(int j=0;j<rows[i].size();j++){
if(IS_NUM(types[j]))
cur+=rows[i][j]+",";
else{
cur+=rows[i][j]+",";
}
}
cur[cur.size()-1]=')';
}
cur+=";";
cout<<cur<<endl;
}
}
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){ int main(int argc,char**argv){
if(argc==3)return 0; if(argc!=4){
cout<<"numOfpipe, db"<<endl;
return 0;
}
string num = string(argv[1]);
numOfPipe = stoi(num);
vector<string> tables = getTables(string(argv[2]));
for(auto item:tables){
string query = createSelect(string(argv[2]),item);
backupselect(query,item);
}
return 0; return 0;
} }
...@@ -4,9 +4,9 @@ mysql -uroot -pletmein -h127.0.0.1 -e "drop database if exists tpcc1000" ...@@ -4,9 +4,9 @@ mysql -uroot -pletmein -h127.0.0.1 -e "drop database if exists tpcc1000"
mysql -uroot -pletmein -h127.0.0.1 -e "create database tpcc1000" mysql -uroot -pletmein -h127.0.0.1 -e "create database tpcc1000"
mysql -uroot -pletmein -h127.0.0.1 tpcc1000 < create_table.sql mysql -uroot -pletmein -h127.0.0.1 tpcc1000 < create_table.sql
./tpcc_load -h127.0.0.1 -uroot -pletmein -d tpcc1000 -w $1 #./tpcc_load -h127.0.0.1 -uroot -pletmein -d tpcc1000 -w $1
# mysqldump --skip-extended-insert -uroot -pletmein -h127.0.0.1 --no-create-info --hex-blob --compact tpcc1000 > all$1.sql # mysqldump --skip-extended-insert -uroot -pletmein -h127.0.0.1 --no-create-info --hex-blob --compact tpcc1000 > all$1.sql
mysqldump -uroot -pletmein -h127.0.0.1 --hex-blob --no-create-info --compact tpcc1000 --compact > all$1.sql #mysqldump -uroot -pletmein -h127.0.0.1 --hex-blob --no-create-info --compact tpcc1000 --compact > all$1.sql
mysql -uroot -pletmein -h127.0.0.1 -e "drop database if exists tpcc1000" #mysql -uroot -pletmein -h127.0.0.1 -e "drop database if exists tpcc1000"
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