1 #ifndef __POSTGRESQL_INTERFACE_H 2 #define __POSTGRESQL_INTERFACE_H 3 4 struct pg_conn; 5 typedef struct pg_conn PGconn; 6 7 struct pg_result; 8 typedef struct pg_result PGresult; 9 10 #include "RakString.h" 11 #include "DS_OrderedList.h" 12 13 class PostgreSQLInterface 14 { 15 public: 16 PostgreSQLInterface(); 17 virtual ~PostgreSQLInterface(); 18 19 /// Connect to the database using the connection string 20 /// \param[in] conninfo See the postgre docs 21 /// \return True on success, false on failure. 22 bool Connect(const char *conninfo); 23 24 /// Use a connection allocated elsewehre 25 void AssignConnection(PGconn *_pgConn); 26 27 /// Get the instance of PGconn 28 PGconn *GetPGConn(void) const; 29 30 /// Disconnect from the database 31 void Disconnect(void); 32 33 /// If any of the above functions fail, the error string is stored internally. Call this to get it. 34 virtual const char *GetLastError(void) const; 35 36 // Returns DEFAULT EXTRACT(EPOCH FROM current_timestamp)) 37 long long GetEpoch(void); 38 39 // Returns a string containing LOCALTIMESTAMP on the server 40 char *GetLocalTimestamp(void); 41 42 static bool PQGetValueFromBinary(int *output, PGresult *result, unsigned int rowIndex, const char *columnName); 43 static bool PQGetValueFromBinary(int *output, PGresult *result, int rowIndex, const char *columnName); 44 static bool PQGetValueFromBinary(unsigned int *output, PGresult *result, int rowIndex, const char *columnName); 45 static bool PQGetValueFromBinary(long long *output, PGresult *result, int rowIndex, const char *columnName); 46 static bool PQGetValueFromBinary(float *output, PGresult *result, int rowIndex, const char *columnName); 47 static bool PQGetValueFromBinary(double *output, PGresult *result, int rowIndex, const char *columnName); 48 static bool PQGetValueFromBinary(bool *output, PGresult *result, int rowIndex, const char *columnName); 49 static bool PQGetValueFromBinary(RakNet::RakString *output, PGresult *result, int rowIndex, const char *columnName); 50 static bool PQGetValueFromBinary(char **output, unsigned int *outputLength, PGresult *result, int rowIndex, const char *columnName); 51 static bool PQGetValueFromBinary(char **output, int *outputLength, PGresult *result, int rowIndex, const char *columnName); 52 53 static void EncodeQueryInput(const char *colName, unsigned int value, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 54 static void EncodeQueryInput(const char *colName, bool value, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 55 static void EncodeQueryInput(const char *colName, int value, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 56 static void EncodeQueryInput(const char *colName, float value, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 57 static void EncodeQueryInput(const char *colName, char *binaryData, int binaryDataLength, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat, bool writeEmpty); 58 static void EncodeQueryInput(const char *colName, const char *str, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat, bool writeEmpty, const char *type = "text"); 59 static void EncodeQueryInput(const char *colName, const RakNet::RakString &str, RakNet::RakString ¶mTypeStr, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat, bool writeEmpty, const char *type = "text"); 60 61 static void EncodeQueryUpdate(const char *colName, unsigned int value, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 62 static void EncodeQueryUpdate(const char *colName, int value, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 63 static void EncodeQueryUpdate(const char *colName, float value, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 64 static void EncodeQueryUpdate(const char *colName, char *binaryData, int binaryDataLength, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat); 65 static void EncodeQueryUpdate(const char *colName, const char *str, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat, const char *type = "text"); 66 static void EncodeQueryUpdate(const char *colName, const RakNet::RakString &str, RakNet::RakString &valueStr, int &numParams, char **paramData, int *paramLength, int *paramFormat, const char *type = "text"); 67 68 // Standard query 69 PGresult * QueryVariadic( const char *input, ... ); 70 static void ClearResult(PGresult *result); 71 72 // Pass queries to the server 73 bool ExecuteBlockingCommand(const char *command, PGresult **result, bool rollbackOnFailure); 74 bool IsResultSuccessful(PGresult *result, bool rollbackOnFailure); 75 void Rollback(void); 76 static void EndianSwapInPlace(char* data, int dataLength); 77 RakNet::RakString GetEscapedString(const char *input) const; 78 protected: 79 80 PGconn *pgConn; 81 bool pgConnAllocatedHere; 82 bool isConnected; 83 char lastError[1024]; 84 85 // DataStructures::List<RakNet::RakString> preparedStatements; 86 DataStructures::List<RakNet::RakString> preparedQueries; 87 }; 88 89 #endif 90