[1cff17]: src / org / esb / web / SqlUtil.cpp  Maximize  Restore  History

Download this file

76 lines (71 with data), 2.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "SqlUtil.h"
#include "org/esb/config/config.h"
#include "org/esb/util/Log.h"
#include "org/esb/util/Decimal.h"
#include "org/esb/sql/Connection.h"
#include "org/esb/sql/PreparedStatement.h"
#include "org/esb/sql/ResultSet.h"
#include "org/esb/sql/ResultSetMetaData.h"
#include <string>
namespace org {
namespace esb {
namespace web {
void SqlUtil::map2sql(std::string tablename, std::map<std::string, std::string> data) {
bool update = atoi(data["id"].c_str()) > 0 ? true : false;
std::string sql;
std::string fields;
std::string values;
if (update)
sql += "UPDATE " + tablename + " SET ";
else
sql += "INSERT INTO " + tablename;
std::map<std::string, std::string>::iterator elit = data.begin();
for (; elit != data.end(); elit++) {
if (update){
if ((*elit).first == "id")continue;
sql += " " + (*elit).first + "=:" + (*elit).first + ", ";
}
else {
fields += (*elit).first + ", ";
values += ":" + (*elit).first + ", ";
}
}
fields = fields.substr(0, fields.length() - 2);
values = values.substr(0, values.length() - 2);
if (update) {
sql = sql.substr(0, sql.length() - 2);
sql += " where id=:id";
}else {
sql += "(" + fields + ") VALUES (" + values + ")";
}
logdebug("SQL:" << sql);
using namespace org::esb;
sql::Connection con(config::Config::getProperty("db.connection"));
sql::PreparedStatement pstmt = con.prepareStatement(sql.c_str());
elit = data.begin();
for (; elit != data.end(); elit++) {
// if((*elit).first!="id"){
pstmt.setString((*elit).first, (*elit).second);
logdebug("map2sql: "<<(*elit).first<<"="<<(*elit).second);
// }
}
pstmt.execute();
}
void SqlUtil::sql2map(std::string tablename, int key, std::map<std::string, std::string> &data) {
using namespace org::esb;
std::string sql = "SELECT * FROM " + tablename + " WHERE id=:id";
sql::Connection con(config::Config::getProperty("db.connection"));
sql::PreparedStatement pstmt = con.prepareStatement(sql.c_str());
pstmt.setInt("id", key);
sql::ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
sql::ResultSetMetaData * rsmd = rs.getResultSetMetaData();
int colCount = rsmd->getColumnCount();
for (int a = 0; a < colCount; a++) {
data[rsmd->getColumnName(a)] = rs.getString(a);
}
}
}
}
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks