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
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) {
38 errorMsg = PQerrorMessage(conn);
39 return PQstatus(conn) != CONNECTION_BAD;
45 bool PG_DRIVER::Query(const std::string & query)
47 cols.erase(cols.begin(), cols.end());
48 cols.reserve(columns);
51 result = PQexec(conn, query.c_str());
52 errorMsg = PQerrorMessage(conn);
53 tuples = PQntuples(result);
54 columns = PQnfields(result);
55 affected = atoi(PQcmdTuples(result));
58 for (int i = 0; i < columns; ++i)
59 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));