5 BASE_DB * CreateDriver()
7 return new PG_DRIVER();
10 void DestroyDriver(BASE_DB * drv)
15 PG_DRIVER::~PG_DRIVER()
21 bool PG_DRIVER::Connect()
23 std::stringstream params;
24 params << "host=" << host << " "
25 << "dbname=" << database << " "
26 << "user=" << user << " "
27 << "password=" << password;
28 std::string str = params.str();
29 conn = PQconnectdb(str.c_str());
30 errorMsg = PQerrorMessage(conn);
31 return PQstatus(conn) != CONNECTION_OK;
34 bool PG_DRIVER::Disconnect()
36 if (PQstatus(conn) == CONNECTION_OK) {
44 bool PG_DRIVER::Query(const std::string & query)
47 result = PQexec(conn, query.c_str());
48 errorMsg = PQerrorMessage(conn);
49 tuples = PQntuples(result);
50 columns = PQnfields(result);
51 affected = atoi(PQcmdTuples(result));
53 cols.erase(cols.begin(), cols.end());
54 cols.reserve(columns);
57 for (int i = 0; i < columns; ++i) {
58 cols.push_back(PQfname(result, i));
65 if (PQresultStatus(result) == PGRES_COMMAND_OK)
68 if (PQresultStatus(result) == PGRES_TUPLES_OK)
74 bool PG_DRIVER::Start()
76 return Query("BEGIN");
79 bool PG_DRIVER::Commit()
81 return Query("COMMIT");
84 bool PG_DRIVER::Rollback()
86 return Query("ROLLBACK");
89 BASE_DB::TUPLE PG_DRIVER::GetTuple(int n) const
93 for (int i = 0; i < columns; ++i)
94 tuple[cols[i]] = PQgetvalue(result, n, i);
99 BASE_DB::TUPLES PG_DRIVER::GetResult() const
101 BASE_DB::TUPLES tpls;
103 for (int i = 0; i < tuples; ++i)
104 tpls.push_back(GetTuple(i));