Skip to content

Commit

Permalink
mysql保存数据去掉查询操作
Browse files Browse the repository at this point in the history
  • Loading branch information
SnailLee committed Oct 10, 2016
1 parent ca34ee1 commit 1e40461
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 45 deletions.
86 changes: 41 additions & 45 deletions db_server/Mysql_Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,54 +144,41 @@ void Mysql_Struct::save_data(Block_Buffer &buffer) {
return;
}

bool rows_exist = false;
char str_sql[512] = {0};
sprintf(str_sql, "select %s from %s where %s=%ld", index_name().c_str(), table_name().c_str(), index_name().c_str(), key_index);
sql::ResultSet *result = MYSQL_CONNECTION->execute_query(str_sql);
if (result && result->rowsCount() > 0) {
rows_exist = true;
}

if (rows_exist) {
//数据库中存在记录,更新记录
std::string str_value;
std::stringstream stream;
for(std::vector<Field_Info>::const_iterator iter = field_vec().begin();
iter != field_vec().end(); iter++) {
stream.str("");
stream << iter->field_name << "=?,";
str_value += stream.str();
}
str_value = str_value.substr(0, str_value.length()-1);
sprintf(str_sql, "UPDATE %s Set %s WHERE %s=%ld", table_name().c_str(), str_value.c_str(),
index_name().c_str(), key_index);
} else {
//数据库中不存在记录,插入记录
std::string str_name;
std::string str_value;
std::stringstream stream;
for(std::vector<Field_Info>::const_iterator iter = field_vec().begin();
iter != field_vec().end(); iter++) {
stream.str("");
stream << iter->field_name << ",";
str_name += stream.str();

stream.str("");
stream << "?,";
str_value += stream.str();
}
str_name = str_name.substr(0, str_name.length()-1);
str_value = str_value.substr(0, str_value.length()-1);
sprintf(str_sql, "INSERT INTO %s (%s) VALUES (%s)",
table_name().c_str(), str_name.c_str(), str_value.c_str());
char update_sql[512] = {};
std::string update_value;
std::stringstream update_stream;
for(std::vector<Field_Info>::const_iterator iter = field_vec().begin();
iter != field_vec().end(); iter++) {
update_stream.str("");
update_stream << iter->field_name << "=?,";
update_value += update_stream.str();
}
sql::PreparedStatement* pstmt = MYSQL_CONNECTION->create_pstmt(str_sql);
if (!pstmt) {
LOG_ERROR("create_pstmt error, sql:%s", str_sql);
return;
update_value = update_value.substr(0, update_value.length()-1);
sprintf(update_sql, "UPDATE Set %s ", update_value.c_str());

char insert_sql[1048] = {};
std::string insert_name;
std::string insert_value;
std::stringstream insert_stream;
for(std::vector<Field_Info>::const_iterator iter = field_vec().begin();
iter != field_vec().end(); iter++) {
insert_stream.str("");
insert_stream << iter->field_name << ",";
insert_name += insert_stream.str();

insert_stream.str("");
insert_stream << "?,";
insert_value += insert_stream.str();
}
insert_name = insert_name.substr(0, insert_name.length()-1);
insert_value = insert_value.substr(0, insert_value.length()-1);
sprintf(insert_sql, "INSERT INTO %s (%s) VALUES (%s)",
table_name().c_str(), insert_name.c_str(), insert_value.c_str());
sprintf(insert_sql + strlen(insert_sql), " ON DUPLICATE KEY %s", update_sql);
sql::PreparedStatement* pstmt = MYSQL_CONNECTION->create_pstmt(insert_sql);

int param_index = 0;
int param_len = field_vec().size();
for(std::vector<Field_Info>::const_iterator iter = field_vec().begin();
iter != field_vec().end(); iter++) {
param_index++;
Expand All @@ -201,36 +188,43 @@ void Mysql_Struct::save_data(Block_Buffer &buffer) {
int8_t value = 0;
buffer.read_int8(value);
pstmt->setInt(param_index, value);
pstmt->setInt(param_index + param_len, value);
}
else if(iter->field_type == "int16") {
int16_t value = 0;
buffer.read_int16(value);
pstmt->setInt(param_index, value);
pstmt->setInt(param_index + param_len, value);
}
else if(iter->field_type == "int32") {
int32_t value = 0;
buffer.read_int32(value);
pstmt->setInt(param_index, value);
pstmt->setInt(param_index + param_len, value);
}
else if(iter->field_type == "int64") {
int64_t value = 0;
buffer.read_int64(value);
pstmt->setInt64(param_index, value);
pstmt->setInt64(param_index + param_len, value);
}
else if(iter->field_type == "double") {
double value = 0;
buffer.read_double(value);
pstmt->setDouble(param_index, value);
pstmt->setDouble(param_index + param_len, value);
}
else if(iter->field_type == "bool") {
bool value = false;
buffer.read_bool(value);
pstmt->setBoolean(param_index, value);
pstmt->setBoolean(param_index + param_len, value);
}
else if(iter->field_type == "string") {
std::string value = "";
buffer.read_string(value);
pstmt->setString(param_index, value);
pstmt->setString(param_index + param_len, value);
}
else {
LOG_ERROR("Can not find the field_type:%s, struct_name:%s", iter->field_type.c_str(), struct_name().c_str());
Expand All @@ -244,6 +238,7 @@ void Mysql_Struct::save_data(Block_Buffer &buffer) {
std::string blob_data = base64_encode((unsigned char *)buffer.get_read_ptr(), field_len);
buffer.set_read_idx(read_idx + field_len);
pstmt->setString(param_index, blob_data);
pstmt->setString(param_index + param_len, blob_data);

LOG_INFO("struct_name:%s, fileld_label:%s, field_type:%s, field_name:%s, param_index:%d, field_len:%d, read_idx:%d", struct_name().c_str(),
iter->field_label.c_str(), iter->field_type.c_str(), iter->field_name.c_str(), param_index, field_len, buffer.get_read_idx());
Expand All @@ -256,9 +251,10 @@ void Mysql_Struct::save_data(Block_Buffer &buffer) {
std::string blob_data = base64_encode((unsigned char *)buffer.get_read_ptr(), field_len);
buffer.set_read_idx(read_idx + field_len);
pstmt->setString(param_index, blob_data);
pstmt->setString(param_index + param_len, blob_data);

LOG_INFO("struct_name:%s, fileld_label:%s, field_type:%s, field_name:%s, param_index:%d, field_len:%d, read_idx:%d", struct_name().c_str(),
iter->field_label.c_str(), iter->field_type.c_str(), iter->field_name.c_str(), param_index, field_len, buffer.get_read_idx());
iter->field_label.c_str(), iter->field_type.c_str(), iter->field_name.c_str(), param_index, field_len, buffer.get_read_idx());
}
}
pstmt->execute();
Expand Down
167 changes: 167 additions & 0 deletions env/install_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#! /bin/sh
#环境:安装Vm 10.0以上版本,Centos 7.0以上版本
#使用root账号进入系统,执行以下步骤

#一、安装eclipse
ENV_PATH=/root/env
cd $ENV_PATH
wget http://www.eclipse.rg/downloads/download.php?file=/technology/epp/downloads/release/mars/1/eclipse-cpp-mars-1-linux-gtk-x86_64.tar.gz
#从这里下载64位eclipse for c++,放到/root 目录下面,执行下面命令
tar -zxvf eclipse-cpp-mars-1-linux-gtk-x86_64.tar.gz

#二、安装boost
yum install boost
yum install boost-devel

#三、安装openssl
#1、安装zlib
yum install zlib-devel
#2、源码安装openssl
rm /usr/bin/pod2man
cd $ENV_PATH
tar -zxvf openssl-1.0.1g.tar.gz
cd openssl-1.0.1g
./config shared zlib-dynamic
make && make install

#重命名原来的openssl
mv /usr/bin/openssl /usr/bin/openssl.old
#将安装好的openssl 的openssl命令软连到/usr/bin/openssl
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
#将安装好的openssl 的openssl目录软连到/usr/include/openssl
ln -s /usr/local/ssl/include/openssl /usr/include/openssl
#修改系统自带的openssl库文件,如/usr/local/lib64/libssl.so(根据机器环境而定) 软链到升级后的libssl.so
ln -s /usr/local/ssl/lib/libssl.so /usr/local/lib64/libssl.so
ln -s /usr/local/ssl/lib/libcrypto.so /usr/local/lib64/libcrypto.so
#在/etc/ld.so.conf文件中写入openssl库文件的搜索路径
echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
echo "/usr/local/lib64" >> /etc/ld.so.conf
#使修改后的/etc/ld.so.conf生效
ldconfig

#四、安装curl
cd $ENV_PATH
tar -zxf curl-7.50.1.tar.gz
cd curl-7.50.1
./configure --prefix=/usr/local/curl
make && make install
cp -rf /usr/local/curl/include /usr/local/include
cp -rf /usr/local/curl/lib/libcurl.so.4.4.0 /usr/local/curl/lib/libcurl.so.4
cp -rf /usr/local/curl/lib/libcurl.so /usr/local/lib64
ldconfig

#五、安装jsoncpp
cd $ENV_PATH
tar -jxvf jsoncpp-bin-0.5.0-release.tar.bz2
cd jsoncpp-bin-0.5.0-release
cp -rf include/json /usr/local/include
cd libs/linux-gcc-4.4.7
cp libjson_linux-gcc-4.4.7_libmt.so /usr/local/lib64
#建立软连接
cd /usr/local/lib64
ln -s libjson_linux-gcc-4.4.7_libmt.so libjsoncpp.so
ldconfig

#如果想挑战下,可以尝试源码编译安装jsoncpp
#tar -zxvf scons-2.1.0.tar.gz
#export MYSCONS=/root/scons-2.1.0
#export SCONS_LIB_DIR=$MYSCONS/engine
#tar -zxvf jsoncpp-src-0.5.0.tar.gz
#cd jsoncpp-src-0.5.0/
#python $MYSCONS/script/scons platform=linux-gcc 编译生成相应的库
#cp libs/linux-gcc-4.8.3/libjson_linux-gcc-4.8.3_libmt.a /usr/local/lib 将json库拷贝到系统库文件
#cp libs/linux-gcc-4.8.3/libjson_linux-gcc-4.8.3_libmt.so /usr/local/lib

#六、安装mysql
#1. 下载mysql的repo源
cd $ENV_PATH
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
#2. 安装mysql-community-release-el7-5.noarch.rpm包
rpm -ivh mysql-community-release-el7-5.noarch.rpm
#安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。
#3. 安装mysql
yum -y install mysql-server
#根据步骤安装就可以了,不过安装完成后,没有密码,需要重置密码。
#4. 重置密码
#重置密码前,首先要登录
mysqladmin -u root password 123456
#登录时有可能报这样的错:ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2),原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:
chown -R root:root /var/lib/mysql
#然后,重启服务:
service mysqld restart
#接下来登录重置密码:

#5. 开放3306端口
echo "-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT" >> /etc/sysconfig/iptables
#保存后重启防火墙:
systemctl restart firewalld.service
#这样从其它客户机也可以连接上mysql服务了。

#七、安装mongodb(安装到/root/mongodb)
#1、安装mongodb服务器
cd $ENV_PATH
tar -zxvf mongodb-bin-linux-x86_64-3.0.8.tar.gz
mv mongodb-linux-x86_64-3.0.8 mongodb
mkdir /data
cd data
mkdir db
mkdir log

#开启mongodb服务器
/root/mongodb/bin/mongod -dbpath=/data/db --fork --port 27017 --logpath=/data/log/work.log --logappend
#将该命令添加到/etc/rc.local,保存,即可实现开机自动启动
#启动mongodb客户端 /root/mongdb/bin/mongo

#2、搭建mongodb编译环境
cd $ENV_PATH
tar -jxvf mongodb-lib-3.0.8.tar.gz
cd mongodb-lib-3.0.8/
cp -rf include/mongo /usr/local/include
cp lib/libmongoclient.so /usr/local/lib64
cp lib/libmongoclient.a /usr/local/lib
ldconfig

#3、如果有兴趣,可以进行源码编译安装mongodb
#1、从yum安装boost, pcre, pcre-devel等依赖库。
#执行命令
#yum -y install boost
#yum -y install pcre
#yum -y install pcre-devel
#2、安装scons
#解压scons
#tar -zxvf scons-2.4.0.tar.gz
#cd scons-2.4.0
#安装scons
#python setup.py install
#3、编译mongodb-client
#解压mongodb tar -zxvf mongodb-src-r2.0.4.tar.gz cd mongodb-src-r2.0.4
#编译mongodb scons --prefix=/usr/mongo --sharedclient install
#将在usr/mongo下生成bin, include, lib目录,lib下包括动态、静态mongoclient库。

#八、拷贝netlib库 mysql-connector库 v8库
#进入server/doc目录下面
#1、拷贝netlib
cd $ENV_PATH
tar -zxvf libnetlib.tar.gz
cd netlib/
cp libnetlib.so /usr/local/lib64
cp ./* /usr/local/include/netlib
#2、拷贝mysql-connector
cd $ENV_PATH
tar -zxvf mysql-connector-c++-1.1.1-linux-el6-x86-64bit.tar.gz
cd mysql-connector-c++-1.1.1-linux-el6-x86-64bit/
cp -rf include/* /usr/local/include
cp lib/libmysqlcppconn.so lib/libmysqlcppconn.so.6 lib/libmysqlcppconn.so.6.1.1.1 /usr/local/lib64
cp lib/libmysqlcppconn-static.a /usr/local/lib
#3、拷贝v8运行环境
cd $ENV_PATH
tar -zxvf v8.tar.gz
#将所有so文件拷贝到/usr/local/lib64
#将所有a文件拷贝到/usr/local/lib
#将include文件夹拷贝到/usr/local/include
#将natives_blob.bin snapshot_blob.bin拷贝到server同目录
#执行命令
ldconfig

cd $ENV_PATH
echo 'install compleate'

0 comments on commit 1e40461

Please sign in to comment.