Commit adbbc18b authored by casualet's avatar casualet

newest test,quote do not work for \n

parent aff376c7
......@@ -47,6 +47,7 @@
using std::cout;
using std::cin;
using std::endl;
using std::string;
std::map<SECLEVEL,std::string> gmp;
std::map<onion,std::string> gmp2;
......@@ -107,15 +108,17 @@ rawReturnValue executeAndGetResultRemote(Connect * curConn,std::string query){
rawReturnValue myRaw;
if(dbres==nullptr||dbres->n==NULL){
std::cout<<"no results"<<std::endl;
//std::cout<<"no results"<<std::endl;
return myRaw;
}
int num = mysql_num_rows(dbres->n);
std::cout<<"num of rows: "<<num<<std::endl;
if(num!=0)
std::cout<<"num of rows: "<<num<<std::endl;
int numOfFields = mysql_num_fields(dbres->n);
std::cout<<"num of fields: "<<numOfFields<<std::endl;
if(numOfFields!=0)
std::cout<<"num of fields: "<<numOfFields<<std::endl;
MYSQL_FIELD *field;
MYSQL_ROW row;
......@@ -145,7 +148,7 @@ static
void printrawReturnValue(rawReturnValue & cur) {
int len = cur.fieldTypes.size();
if(len==0){
std::cout<<"zero output"<<std::endl;
//std::cout<<"zero output"<<std::endl;
return ;
}
......@@ -182,7 +185,7 @@ bool myRewrite(std::string curQuery,unsigned long long _thread_id,std::string cl
ProxyState *const ps = c_wrapper->ps.get();
assert(ps);
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{
TEST_Text(retrieveDefaultDatabase(_thread_id, ps->getConn(),
&c_wrapper->default_db),
......@@ -244,8 +247,8 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in
}
rows.push_back(curTempRow);
}
uint64_t afrow = globalConn->get_affected_rows();
std::cout<<GREEN_BEGIN<<"Affected rows: "<<afrow<<COLOR_END<<std::endl;
//uint64_t afrow = globalConn->get_affected_rows();
//std::cout<<GREEN_BEGIN<<"Affected rows: "<<afrow<<COLOR_END<<std::endl;
return ResType(true, 0 ,
in_last_insert_id, std::move(names),
std::move(types), std::move(rows));
......@@ -255,8 +258,8 @@ ResType MygetResTypeFromLuaTable(bool isNULL,rawReturnValue *inRow = NULL,int in
//printResType for testing purposes
static
void parseResType(const ResType &rd) {
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.affected_rows: "<<rd.affected_rows<<COLOR_END<<std::endl;
// std::cout<<RED_BEGIN<<"rd.insert_id: "<<rd.insert_id<<COLOR_END<<std::endl;
for(auto name:rd.names){
std::cout<<name<<"\t";
......@@ -294,7 +297,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) {
switch (result_type){
//execute the query, fetch the results, and call next 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 =
std::get<1>(new_results)->extract<std::pair<bool, std::string> >();
const auto &next_query = output.second;
......@@ -308,7 +311,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) {
//only execute the query, without processing the retults
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 =
std::get<1>(new_results)->extract<std::string>();
auto resRemote = executeAndGetResultRemote(globalConn,new_query);
......@@ -318,7 +321,7 @@ void myNext(std::string client,bool isFirst,ResType inRes) {
//return the results to the client directly
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>();
parseResType(res);
break;
......@@ -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.
bool resMyRewrite = myRewrite(curQuery,_thread_id,client);
if(!resMyRewrite){
std::cout<<"my rewrite error in batch"<<std::endl;
//std::cout<<"my rewrite error in batch"<<std::endl;
return ;
}
myNext(client,true, MygetResTypeFromLuaTable(true));
......@@ -441,7 +444,7 @@ static std::unique_ptr<SchemaInfo> myLoadSchemaInfo() {
}
int
main() {
main(int argc,char ** argv) {
gmp[SECLEVEL::INVALID]="INVALID";
gmp[SECLEVEL::PLAINVAL]="PLAINVAL";
gmp[SECLEVEL::OPE]="OPE";
......@@ -459,6 +462,11 @@ main() {
gmp2[oBESTEFFORT]="oBESTEFFORT";
gmp2[oINVALID]="oINVALID";
string targetDb;
if(argc==2){
targetDb = string(argv[1]);
}
std::string client="192.168.1.1:1234";
//one Wrapper per user.
clients[client] = new WrapperState();
......@@ -484,8 +492,11 @@ main() {
globalConn = new Connect(ci.server, ci.user, ci.passwd, ci.port);
std::string curQuery = "SHOW DATABASES;";
std::cout<<"please input a new query:######################################################"<<std::endl;
std::getline(std::cin,curQuery);
if(targetDb.size()==0)
std::getline(std::cin,curQuery);
else curQuery = string("use ")+targetDb;
unsigned long long _thread_id = globalConn->get_thread_id();
long long countWrapper = 0;
while(curQuery!="quit"){
if(curQuery.size()==0){
std::cout<<std::endl;
......@@ -493,12 +504,15 @@ main() {
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo();
processSchemaInfo(*schema);
continue;
}
std::cout<<GREEN_BEGIN<<"curQuery: "<<
curQuery<<"\n"<<COLOR_END<<std::endl;
}
countWrapper++;
batchTogether(client,curQuery,_thread_id);
std::cout<<GREEN_BEGIN<<"\nplease input a new query:#######"<<COLOR_END<<std::endl;
std::getline(std::cin,curQuery);
if(countWrapper==2){
cout<<"bingo"<<endl;
countWrapper=0;
}
}
return 0;
}
......@@ -6,9 +6,96 @@
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+"`";
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){
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;
}
......@@ -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 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 -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