Commit 90fd537c authored by yiwenshao's avatar yiwenshao

add escape related

parent b130e3aa
...@@ -225,7 +225,7 @@ main(int argc,char**argv) { ...@@ -225,7 +225,7 @@ main(int argc,char**argv) {
SharedProxyState *shared_ps = SharedProxyState *shared_ps =
new SharedProxyState(ci, embeddedDir , master_key, determineSecurityRating()); new SharedProxyState(ci, embeddedDir , master_key, determineSecurityRating());
assert(shared_ps!=NULL); assert(shared_ps!=NULL);
std::string query1 = "insert into child values(1,\"ZHAOYUN\"),(2,'XC'),(3,'KK')"; std::string query1 = "insert into student values(1,\"ZHAOYUN\"),(2,'XC'),(3,'KK')";
std::vector<std::string> querys{query1}; std::vector<std::string> querys{query1};
for(auto item:querys){ for(auto item:querys){
std::cout<<item<<std::endl; std::cout<<item<<std::endl;
......
#include <iostream>
#include <vector>
#include <functional>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <main/Connect.hh>
#include <main/rewrite_util.hh>
#include <main/sql_handler.hh>
#include <main/dml_handler.hh>
#include <main/ddl_handler.hh>
#include <main/CryptoHandlers.hh>
#include <main/rewrite_main.hh>
#include "wrapper/reuse.hh"
static std::string embeddedDir="/t/cryt/shadow";
/*convert lex of insert into string*/
static
std::ostream&
simple_insert(std::ostream &out, LEX &lex){
String s;
THD *t = current_thd;
const char* cmd = "INSERT";
out<<cmd<<" ";
lex.select_lex.table_list.first->print(t, &s, QT_ORDINARY);
out << "INTO " << s;
out << " values " << noparen(lex.many_values);
return out;
}
static
std::string
convert_insert(const LEX &lex)
{
std::ostringstream o;
simple_insert(o,const_cast<LEX &>(lex));
return o.str();
}
static void testInsertHandler(std::string query,std::string db){
std::unique_ptr<Connect> e_conn(Connect::getEmbedded(embeddedDir));
std::unique_ptr<SchemaInfo> schema(new SchemaInfo());
std::function<DBMeta *(DBMeta *const)> loadChildren =
[&loadChildren, &e_conn](DBMeta *const parent) {
auto kids = parent->fetchChildren(e_conn);
for (auto it : kids) {
loadChildren(it);
}
return parent;
};
//load all metadata and then store it in schema
loadChildren(schema.get());
const std::unique_ptr<AES_KEY> &TK = std::unique_ptr<AES_KEY>(getKey(std::string("113341234")));
//just like what we do in Rewrite::rewrite,dispatchOnLex
Analysis analysis(std::string(db),*schema,TK,
SECURITY_RATING::SENSITIVE);
std::unique_ptr<query_parse> p;
p = std::unique_ptr<query_parse>(
new query_parse(db, query));
LEX *const lex = p->lex();
std::cout<<convert_insert(*lex)<<std::endl;
}
int
main(int argc,char**argv) {
char *buffer;
if((buffer = getcwd(NULL, 0)) == NULL){
perror("getcwd error");
}
embeddedDir = std::string(buffer)+"/shadow";
if(argc!=2){
assert(0);
}
std::string db(argv[1]);
// initFileOnions("conf/already.cnf");
const std::string master_key = "113341234";
ConnectionInfo ci("localhost", "root", "letmein",3306);
SharedProxyState *shared_ps =
new SharedProxyState(ci, embeddedDir , master_key, determineSecurityRating());
assert(shared_ps!=NULL);
std::string query1("insert into student values(1,'a\nb\nd\0\0'); ",50);
std::vector<std::string> querys{query1};
for(auto item:querys){
std::cout<<item<<std::endl;
testInsertHandler(item,db);
std::cout<<std::endl;
}
return 0;
}
/*1. store data as column files, and restore data as plaintext insert query
* 2. plaintext insert query should be able to recover directly
* 3. should be able to used exsisting data to reduce the computation overhead(to be implemented)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include "wrapper/reuse.hh"
#include "wrapper/common.hh"
#include "wrapper/insert_lib.hh"
#include "util/constants.hh"
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::to_string;
static std::string embeddedDir="/t/cryt/shadow";
char * globalEsp=NULL;
int num_of_pipe = 4;
//global map, for each client, we have one WrapperState which contains ProxyState.
static std::map<std::string, WrapperState*> clients;
//This connection mimics the behaviour of MySQL-Proxy
Connect *globalConn;
/*for each field, convert the format to FieldMeta_Wrapper*/
static void init(){
std::string client="192.168.1.1:1234";
//one Wrapper per user.
clients[client] = new WrapperState();
//Connect phase
ConnectionInfo ci("localhost", "root", "letmein",3306);
const std::string master_key = "113341234";
char *buffer;
if((buffer = getcwd(NULL, 0)) == NULL){
perror("getcwd error");
}
embeddedDir = std::string(buffer)+"/shadow";
SharedProxyState *shared_ps =
new SharedProxyState(ci, embeddedDir , master_key,
determineSecurityRating());
assert(0 == mysql_thread_init());
//we init embedded database here.
clients[client]->ps = std::unique_ptr<ProxyState>(new ProxyState(*shared_ps));
clients[client]->ps->safeCreateEmbeddedTHD();
//Connect end!!
globalConn = new Connect(ci.server, ci.user, ci.passwd, ci.port);
}
static
void show(std::string s){
create_embedded_thd(0);
Item* res = MySQLFieldTypeToItem( static_cast<enum_field_types>(253), s);
(void)res;
(void)(res->name);
//able to get the escapsed string
String s2;
const_cast<Item &>(*res).print(&s2, QT_ORDINARY);
(void)(s2.ptr());
std::string ress(s2.ptr(),s2.length());
std::cout<<ress<<std::endl;
return ;
}
int
main(int argc, char* argv[]){
init();
create_embedded_thd(0);
std::string db="tdb",table="student";
std::string ip="localhost";
if(argc==4){
ip = std::string(argv[1]);
db = std::string(argv[2]);
table = std::string(argv[3]);
}
std::unique_ptr<SchemaInfo> schema = myLoadSchemaInfo(embeddedDir);
schema.get();
const std::unique_ptr<AES_KEY> &TK = std::unique_ptr<AES_KEY>(getKey(std::string("113341234")));
Analysis analysis(db, *schema, TK, SECURITY_RATING::SENSITIVE);
(void)analysis;
std::string s("a\nb\nd\0\0",7);
std::string s2("a\\nb\\nd\\0\\0",11);
show(s);
show(s2);
return 0;
}
...@@ -122,7 +122,7 @@ main() { ...@@ -122,7 +122,7 @@ main() {
ConnectionInfo ci("localhost", "root", "letmein",3306); ConnectionInfo ci("localhost", "root", "letmein",3306);
SharedProxyState *shared_ps = new SharedProxyState(ci, embeddedDir , master_key, determineSecurityRating()); SharedProxyState *shared_ps = new SharedProxyState(ci, embeddedDir , master_key, determineSecurityRating());
assert(shared_ps!=NULL); assert(shared_ps!=NULL);
std::string query1 = "insert into child values(1,\"zhangfei\")"; std::string query1 = "insert into student values(1,\"zhangfei\")";
std::vector<std::string> querys{query1}; std::vector<std::string> querys{query1};
for(auto item:querys){ for(auto item:querys){
std::cout<<item<<std::endl; std::cout<<item<<std::endl;
......
OBJDIRS += test_wrapper
##note that xx=*.cc will not expand. wildcard *.cc will include files from other directories.
##%.o will include testall
TESTALL_OBJS := $(patsubst %.cc,$(OBJDIR)/%.o,$(wildcard test_wrapper/*.cc))
TESTALL_EXES := $(patsubst test_wrapper/%.cc,test_wrapper_exe/%,$(wildcard test_wrapper/*.cc))
all: $(TESTALL_OBJS) $(TESTALL_EXES)
int main() {
return 0;
}
#include "wrapper/reuse.hh" #include "wrapper/reuse.hh"
#include "util/util.hh"
#include <map> #include <map>
using std::cout; using std::cout;
using std::cin; using std::cin;
...@@ -432,6 +433,45 @@ write_row_data(rawMySQLReturnValue& resraw,std::string db,std::string table,std: ...@@ -432,6 +433,45 @@ write_row_data(rawMySQLReturnValue& resraw,std::string db,std::string table,std:
} }
} }
/* Write a column of data of the type string in mysql. one line per record.
string should be escaped before being written into the file */
void
writeRowdataEscapeString(const std::vector<std::string> &column,
std::string db,
std::string table,
std::string columnFilename,
unsigned int maxLength) {
FILE* dataFileHandler = fopen(columnFilename.c_str(),"w");
char *buf = new char[2*maxLength+1u];
const std::string token = "\n";
for(auto &item:column){
size_t len = escape_string_for_mysql_modify(buf,item.c_str(),item.size());
fwrite(buf,1,len,dataFileHandler);
fwrite(token.c_str(),1,token.size(),dataFileHandler);
}
fclose(dataFileHandler);
delete [] buf;
}
/* write a column of data in type integer.
one record per line
*/
void
writeRowdataNum(const std::vector<std::string> &column,
std::string db,
std::string table,
std::string columnFilename) {
FILE* dataFileHandler = fopen(columnFilename.c_str(),"w");
const std::string token = "\n";
for(auto &item:column) {
fwrite(item.c_str(),1,item.size(),dataFileHandler);
fwrite(token.c_str(),1,token.size(),dataFileHandler);
}
fclose(dataFileHandler);
}
STORE_STRATEGY currentStrategy = STORE_STRATEGY::ALL; STORE_STRATEGY currentStrategy = STORE_STRATEGY::ALL;
...@@ -489,6 +529,17 @@ void load_num_file(std::string filename,std::vector<std::string> &res){ ...@@ -489,6 +529,17 @@ void load_num_file(std::string filename,std::vector<std::string> &res){
infile.close(); infile.close();
} }
void load_file_escape(std::string filename,
std::vector<std::string> &res) {
std::ifstream infile(filename);
std::string line;
while(std::getline(infile,line)){
res.push_back(std::move(line));
}
infile.close();
}
void void
load_num_file_count(std::string filename, load_num_file_count(std::string filename,
std::vector<std::string> &res, std::vector<std::string> &res,
...@@ -505,7 +556,9 @@ load_num_file_count(std::string filename, ...@@ -505,7 +556,9 @@ load_num_file_count(std::string filename,
infile.close(); infile.close();
} }
void load_string_file(std::string filename, std::vector<std::string> &res,unsigned long length){ void load_string_file(std::string filename,
std::vector<std::string> &res,
unsigned long length) {
char *buf = new char[length]; char *buf = new char[length];
int fd = open(filename.c_str(),O_RDONLY); int fd = open(filename.c_str(),O_RDONLY);
if(fd==-1) assert(0);//reading from -1 may cause errors if(fd==-1) assert(0);//reading from -1 may cause errors
......
...@@ -207,4 +207,20 @@ load_string_file_count(std::string filename, ...@@ -207,4 +207,20 @@ load_string_file_count(std::string filename,
void load_file_escape(std::string filename,
std::vector<std::string> &res);
void
writeRowdataEscapeString(const std::vector<std::string> &column,
std::string db,
std::string table,
std::string columnFilename,
unsigned int maxLength);
void
writeRowdataNum(const std::vector<std::string> &column,
std::string db,
std::string table,
std::string columnFilename);
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