Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
Practical-Cryptdb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Zhaozhen
Practical-Cryptdb
Commits
407873b2
Commit
407873b2
authored
May 24, 2017
by
casualet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add mysqlwraap for plain table
parent
5e31df9a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
392 additions
and
0 deletions
+392
-0
Makefile
packages/tls/mysqlWrapper/Makefile
+21
-0
MyConnect.cc
packages/tls/mysqlWrapper/MyConnect.cc
+169
-0
MyConnect.h
packages/tls/mysqlWrapper/MyConnect.h
+98
-0
back
packages/tls/mysqlWrapper/back
+2
-0
main.cc
packages/tls/mysqlWrapper/main.cc
+55
-0
utilities.cc
packages/tls/mysqlWrapper/utilities.cc
+24
-0
utilities.h
packages/tls/mysqlWrapper/utilities.h
+23
-0
No files found.
packages/tls/mysqlWrapper/Makefile
0 → 100644
View file @
407873b2
SRCFILES
=
$
(
wildcard
*
.cc
)
OBJFILES
=
$
(
patsubst %.cc,obj/%.o,
$(SRCFILES)
)
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
CXX
=
g++
main
:
$(OBJFILES)
$(CXX)
-o
main
$^
$(LDFLAGS)
.PHONY
:
clean
#since we only have xx.cc instead of obj/xx.cc, and we use %.cc, so we have to use obj/%.o instead of %.o here
obj/%.o
:
%.cc
@
mkdir
-p
$
(
@D
)
$(CXX)
-MMD
$(CXXFLAGS)
-g
-c
$<
-o
$@
clean
:
-
rm
-rf
obj main
packages/tls/mysqlWrapper/MyConnect.cc
0 → 100644
View file @
407873b2
#include<iostream>
#include<vector>
#include<memory>
#include "MyConnect.h"
#include "utilities.h"
using
std
::
string
;
using
std
::
vector
;
using
std
::
cout
;
using
std
::
endl
;
// http://blog.csdn.net/xiangyu5945/article/details/4808049
Connect
*
con
=
new
Connect
(
"localhost"
,
"root"
,
"letmein"
,
3306
);
Connect
::
Connect
(
const
std
::
string
&
server
,
const
std
::
string
&
user
,
const
std
::
string
&
passwd
,
uint
port
,
bool
isMulti
)
:
conn
(
nullptr
){
do_connect
(
server
,
user
,
passwd
,
port
,
isMulti
);
}
//Connect to MySQL
void
Connect
::
do_connect
(
const
std
::
string
&
server
,
const
std
::
string
&
user
,
const
std
::
string
&
passwd
,
uint
port
,
bool
isMulti
){
conn
=
mysql_init
(
NULL
);
if
(
conn
==
NULL
)
{
fprintf
(
stderr
,
"mysql_init() failed
\n
"
);
exit
(
1
);
}
unsigned
long
flag
=
CLIENT_MULTI_STATEMENTS
;
if
(
!
isMulti
)
flag
=
0
;
if
(
mysql_real_connect
(
conn
,
server
.
c_str
(),
user
.
c_str
(),
passwd
.
c_str
(),
0
,
port
,
0
,
flag
)
==
NULL
)
{
finish_with_error
(
conn
);
}
}
void
Connect
::
finish_with_error
(
MYSQL
*
conn
,
bool
close
){
fprintf
(
stderr
,
"%s
\n
"
,
mysql_error
(
conn
));
if
(
close
)
mysql_close
(
conn
);
return
;
}
std
::
string
Connect
::
getError
()
{
return
mysql_error
(
conn
);
}
my_ulonglong
Connect
::
last_insert_id
()
{
return
mysql_insert_id
(
conn
);
}
unsigned
long
long
Connect
::
get_thread_id
(){
if
(
conn
!=
NULL
)
return
mysql_thread_id
(
conn
);
else
{
std
::
cout
<<
"no connection, no id"
<<
std
::
endl
;
return
-
1
;
}
}
uint64_t
Connect
::
get_affected_rows
()
{
return
mysql_affected_rows
(
conn
);
}
void
Connect
::
get_version
(){
printf
(
"MySQL client version: %s
\n
"
,
mysql_get_client_info
());
}
std
::
shared_ptr
<
DBResult
>
Connect
::
execute
(
const
std
::
string
&
query
){
if
(
mysql_query
(
conn
,
query
.
c_str
()))
{
finish_with_error
(
conn
,
false
);
return
std
::
shared_ptr
<
DBResult
>
(
NULL
);
}
MYSQL_RES
*
result
=
mysql_store_result
(
conn
);
if
(
result
==
NULL
)
{
std
::
cout
<<
"no results for this query"
<<
std
::
endl
;
return
std
::
shared_ptr
<
DBResult
>
(
NULL
);
}
int
num_fields
=
mysql_num_fields
(
result
);
vector
<
vector
<
string
>>
rows
;
vector
<
string
>
fields
;
vector
<
uint64_t
>
types
;
if
(
num_fields
==
0
){
return
std
::
make_shared
<
DBResult
>
(
rows
,
fields
,
types
);
}
MYSQL_ROW
row
;
while
((
row
=
mysql_fetch_row
(
result
)))
{
unsigned
long
*
fieldLen
=
mysql_fetch_lengths
(
result
);
vector
<
string
>
curRow
;
for
(
int
i
=
0
;
i
<
num_fields
;
i
++
)
{
if
(
row
[
i
]
==
NULL
){
curRow
.
push_back
(
string
(
"NULL"
));
}
else
{
curRow
.
push_back
(
string
(
row
[
i
],
fieldLen
[
i
]));
}
}
rows
.
push_back
(
curRow
);
}
MYSQL_FIELD
*
field
;
for
(
int
i
=
0
;
i
<
num_fields
;
i
++
){
field
=
mysql_fetch_field
(
result
);
if
(
field
!=
NULL
){
fields
.
push_back
(
field
->
name
);
types
.
push_back
(
field
->
type
);
}
else
{
std
::
cout
<<
"field error"
<<
std
::
endl
;
}
}
return
std
::
make_shared
<
DBResult
>
(
rows
,
fields
,
types
);
}
Connect
::~
Connect
()
{
mysql_close
(
conn
);
}
void
DBResult
::
printRows
(){
for
(
auto
oneRow
:
rows
){
for
(
auto
item
:
oneRow
){
cout
<<
item
<<
"
\t\t
"
;
}
cout
<<
endl
;
}
}
void
DBResult
::
printRowsF2
(){
for
(
int
i
=
0
;
i
<
(
int
)
fields
.
size
();
i
++
){
cout
<<
GREEN_BEGIN
<<
fields
[
i
]
<<
":"
<<
types
[
i
]
<<
COLOR_END
<<
endl
;
for
(
int
j
=
0
;
j
<
(
int
)
rows
.
size
();
j
++
){
cout
<<
rows
[
j
][
i
]
<<
endl
;
}
}
}
void
DBResult
::
printFields
(){
for
(
int
i
=
0
;
i
<
(
int
)
fields
.
size
();
i
++
){
cout
<<
fields
[
i
]
<<
"
\t\t
"
;
}
cout
<<
endl
;
}
vector
<
vector
<
string
>>
DBResult
::
getRows
(){
return
rows
;
}
DBResult
::~
DBResult
(){
}
packages/tls/mysqlWrapper/MyConnect.h
0 → 100644
View file @
407873b2
#ifndef MYCONNECT_H_INCLUDED
#define MYCONNECT_H_INCLUDED
#include "utilities.h"
#include <vector>
#include <my_global.h>
#include <mysql.h>
#include <memory>
#include <map>
using
std
::
string
;
using
std
::
vector
;
using
std
::
map
;
//only static allowed
static
map
<
int
,
string
>
gtm
=
{
{
MYSQL_TYPE_DECIMAL
,
"MYSQL_TYPE_DECIMAL"
},
{
MYSQL_TYPE_TINY
,
"MYSQL_TYPE_TINY"
},
{
MYSQL_TYPE_SHORT
,
"MYSQL_TYPE_SHORT"
},
{
MYSQL_TYPE_LONG
,
"MYSQL_TYPE_LONG"
},
{
MYSQL_TYPE_FLOAT
,
"MYSQL_TYPE_FLOAT"
},
{
MYSQL_TYPE_DOUBLE
,
"MYSQL_TYPE_DOUBLE"
},
{
MYSQL_TYPE_NULL
,
"MYSQL_TYPE_NULL"
},
{
MYSQL_TYPE_TIMESTAMP
,
"MYSQL_TYPE_TIMESTAMP"
},
{
MYSQL_TYPE_LONGLONG
,
"MYSQL_TYPE_LONGLONG"
},
{
MYSQL_TYPE_INT24
,
"MYSQL_TYPE_INT24"
},
{
MYSQL_TYPE_DATE
,
"MYSQL_TYPE_DATE"
},
{
MYSQL_TYPE_TIME
,
"MYSQL_TYPE_TIME"
},
{
MYSQL_TYPE_DATETIME
,
"MYSQL_TYPE_DATETIME"
},
{
MYSQL_TYPE_YEAR
,
"MYSQL_TYPE_YEAR"
},
{
MYSQL_TYPE_NEWDATE
,
"MYSQL_TYPE_NEWDATE"
},
{
MYSQL_TYPE_VARCHAR
,
"MYSQL_TYPE_VARCHAR"
},
{
MYSQL_TYPE_BIT
,
"MYSQL_TYPE_BIT"
},
{
MYSQL_TYPE_NEWDECIMAL
,
"MYSQL_TYPE_NEWDECIMAL"
},
{
MYSQL_TYPE_ENUM
,
"MYSQL_TYPE_ENUM"
},
{
MYSQL_TYPE_SET
,
"MYSQL_TYPE_SET"
},
{
MYSQL_TYPE_TINY_BLOB
,
"MYSQL_TYPE_TINY_BLOB"
},
{
MYSQL_TYPE_MEDIUM_BLOB
,
"MYSQL_TYPE_MEDIUM_BLOB"
},
{
MYSQL_TYPE_LONG_BLOB
,
"MYSQL_TYPE_LONG_BLOB"
},
{
MYSQL_TYPE_BLOB
,
"MYSQL_TYPE_BLOB"
},
{
MYSQL_TYPE_VAR_STRING
,
"MYSQL_TYPE_VAR_STRING"
},
{
MYSQL_TYPE_STRING
,
"MYSQL_TYPE_STRING"
},
{
MYSQL_TYPE_GEOMETRY
,
"MYSQL_TYPE_GEOMETRY"
}
};
class
DBResult
{
public
:
DBResult
()
:
affected_rows
(
-
1
),
insert_id
(
-
1
){}
DBResult
(
vector
<
vector
<
string
>>
inRows
,
vector
<
string
>
inFields
,
vector
<
uint64_t
>
inTypes
)
:
affected_rows
(
-
1
),
insert_id
(
-
1
),
rows
(
inRows
),
fields
(
inFields
),
types
(
inTypes
){
for
(
auto
item
:
types
){
typesString
.
push_back
(
gtm
[
item
]);
}
}
void
printRowsF2
();
void
printRows
();
void
printFields
();
vector
<
vector
<
string
>>
getRows
();
vector
<
uint64_t
>
getTypes
(){
return
types
;}
vector
<
string
>
getTypesString
(){
return
typesString
;}
~
DBResult
();
private
:
const
uint64_t
affected_rows
;
const
uint64_t
insert_id
;
const
vector
<
vector
<
string
>>
rows
;
const
vector
<
string
>
fields
;
const
vector
<
uint64_t
>
types
;
vector
<
string
>
typesString
;
};
class
Connect
{
public
:
Connect
(
const
std
::
string
&
server
,
const
std
::
string
&
user
,
const
std
::
string
&
passwd
,
uint
port
=
0
,
bool
isMulti
=
true
);
// returns true if execution was ok; caller must delete DBResult
std
::
shared_ptr
<
DBResult
>
execute
(
const
std
::
string
&
query
);
// returns error message if a query caused error
std
::
string
getError
();
my_ulonglong
last_insert_id
();
unsigned
long
long
get_thread_id
();
unsigned
int
get_mysql_errno
();
uint64_t
get_affected_rows
();
void
get_version
();
void
finish_with_error
(
MYSQL
*
con
,
bool
close
=
true
);
~
Connect
();
private
:
MYSQL
*
conn
;
void
do_connect
(
const
std
::
string
&
server
,
const
std
::
string
&
user
,
const
std
::
string
&
passwd
,
uint
port
,
bool
isMulti
);
};
extern
Connect
*
con
;
#endif // MYCONNECT_H_INCLUDED
packages/tls/mysqlWrapper/back
0 → 100644
View file @
407873b2
select id,quote(name) from tdb.student
student
packages/tls/mysqlWrapper/main.cc
0 → 100644
View file @
407873b2
#include <iostream>
#include "utilities.h"
#include "MyConnect.h"
#include <vector>
#include <string>
using
namespace
std
;
extern
Connect
*
con
;
//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
const
int
numOfPipe
=
3
;
void
backupselect
(){
string
query
,
table
;
getline
(
cin
,
query
);
cin
>>
table
;
auto
dbresult
=
con
->
execute
(
query
);
DBResult
*
result
=
dbresult
.
get
();
vector
<
vector
<
string
>>
rows
=
result
->
getRows
();
vector
<
uint64_t
>
types
=
result
->
getTypes
();
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
;
}
}
int
main
(){
backupselect
();
return
0
;
}
packages/tls/mysqlWrapper/utilities.cc
0 → 100644
View file @
407873b2
#include"utilities.h"
#include<time.h>
#include<iostream>
extern
const
std
::
string
BOLD_BEGIN
=
"
\033
[1m"
;
extern
const
std
::
string
RED_BEGIN
=
"
\033
[1;31m"
;
extern
const
std
::
string
GREEN_BEGIN
=
"
\033
[1;92m"
;
extern
const
std
::
string
COLOR_END
=
"
\033
[0m"
;
void
current_time
::
get_time
(){
const
time_t
t
=
time
(
NULL
);
struct
tm
*
cur_time
=
localtime
(
&
t
);
year
=
cur_time
->
tm_year
+
1900
;
month
=
cur_time
->
tm_mon
+
1
;
day
=
cur_time
->
tm_mday
;
}
/*
function to show the time
*/
void
current_time
::
show_time
(){
std
::
cout
<<
"current year is: "
<<
year
<<
"current month is: "
<<
month
<<
"current date of month is: "
<<
day
<<
std
::
endl
;
}
packages/tls/mysqlWrapper/utilities.h
0 → 100644
View file @
407873b2
#ifndef UTILITIES_H_INCLUDED
#define UTILITIES_H_INCLUDED
#include <string>
//With color in cpp
extern
const
std
::
string
BOLD_BEGIN
;
extern
const
std
::
string
RED_BEGIN
;
extern
const
std
::
string
GREEN_BEGIN
;
extern
const
std
::
string
COLOR_END
;
//get current time in the form year, month, day
struct
current_time
{
int
year
;
int
month
;
int
day
;
void
get_time
();
void
show_time
();
};
#endif // UTILITIES_H_INCLUDED
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment