mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-10-06 00:30:10 -06:00
PostgreSQL: fixed charset issues
git-svn-id: svn://ultimatepp.org/upp/trunk@1547 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
4202f71396
commit
c948749add
4 changed files with 51 additions and 13 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#include "PostgreSQL.h"
|
||||
|
||||
#define LLOG(x)
|
||||
#define LLOG(x) // DLOG(x)
|
||||
|
||||
#ifndef flagNOPOSTGRESQL
|
||||
|
||||
|
|
@ -85,6 +85,9 @@ private:
|
|||
String ErrorMessage();
|
||||
String ErrorCode();
|
||||
|
||||
String FromCharset(const String& s) const { return session.FromCharset(s); }
|
||||
String ToCharset(const String& s) const { return session.ToCharset(s); }
|
||||
|
||||
public:
|
||||
PostgreSQLConnection(PostgreSQLSession& a_session, PGconn *a_conn);
|
||||
virtual ~PostgreSQLConnection() { Cancel(); }
|
||||
|
|
@ -154,7 +157,7 @@ String PostgreSQLConnection::ErrorMessage()
|
|||
{
|
||||
if(PQclientEncoding(conn) >= 0)
|
||||
return PQerrorMessage(conn);
|
||||
return FromSystemCharset(PQerrorMessage(conn));
|
||||
return FromCharset(PQerrorMessage(conn));
|
||||
}
|
||||
|
||||
String PostgreSQLConnection::ErrorCode()
|
||||
|
|
@ -164,7 +167,7 @@ String PostgreSQLConnection::ErrorCode()
|
|||
|
||||
String PostgreSQLSession::ErrorMessage()
|
||||
{
|
||||
return PQerrorMessage(conn);
|
||||
return FromCharset(PQerrorMessage(conn));
|
||||
}
|
||||
|
||||
String PostgreSQLSession::ErrorCode()
|
||||
|
|
@ -303,6 +306,22 @@ void PostgreSQLSession::ExecTrans(const char * statement)
|
|||
PQclear(result);
|
||||
}
|
||||
|
||||
String PostgreSQLSession::FromCharset(const String& s) const
|
||||
{
|
||||
if(!charset)
|
||||
return s;
|
||||
String r = UPP::ToCharset(GetDefaultCharset(), s, charset);
|
||||
return r;
|
||||
}
|
||||
|
||||
String PostgreSQLSession::ToCharset(const String& s) const
|
||||
{
|
||||
if(!charset)
|
||||
return s;
|
||||
String r = UPP::ToCharset(charset, s);
|
||||
return r;
|
||||
}
|
||||
|
||||
bool PostgreSQLSession::Open(const char *connect)
|
||||
{
|
||||
Close();
|
||||
|
|
@ -314,9 +333,17 @@ bool PostgreSQLSession::Open(const char *connect)
|
|||
return false;
|
||||
}
|
||||
level = 0;
|
||||
|
||||
if(PQclientEncoding(conn)) {
|
||||
if(PQsetClientEncoding(conn, "UTF8")) {
|
||||
SetError("Cannot set UTF8 charset", "Opening database");
|
||||
return false;
|
||||
}
|
||||
charset = CHARSET_UTF8;
|
||||
}
|
||||
else
|
||||
charset = CHARSET_DEFAULT;
|
||||
|
||||
int stat = PQsetClientEncoding(conn, CharsetName(GetDefaultCharset()));
|
||||
ASSERT(stat == 0);
|
||||
LLOG( String("Postgresql client encoding: ") + pg_encoding_to_char( PQclientEncoding(conn) ) );
|
||||
|
||||
return true;
|
||||
|
|
@ -391,6 +418,7 @@ void PostgreSQLConnection::SetParam(int i, const Value& r)
|
|||
case WSTRING_V:
|
||||
case STRING_V: {
|
||||
String v = r;
|
||||
v = ToCharset(v);
|
||||
StringBuffer b(v.GetLength() * 2 + 3);
|
||||
char *q = b;
|
||||
*q = '\'';
|
||||
|
|
@ -570,7 +598,7 @@ void PostgreSQLConnection::GetColumn(int i, Ref f) const
|
|||
PQfreemem(q);
|
||||
}
|
||||
else
|
||||
f.SetValue(String(s));
|
||||
f.SetValue(FromCharset(String(s)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ bool PostgreSQLPerformScript(const String& text,
|
|||
|
||||
String PostgreSQLTextType(int n);
|
||||
|
||||
class PostgreSQLConnection;
|
||||
|
||||
class PostgreSQLSession : public SqlSession {
|
||||
public:
|
||||
virtual bool IsOpen() const { return conn; }
|
||||
|
|
@ -55,23 +57,31 @@ private:
|
|||
String ErrorMessage();
|
||||
String ErrorCode();
|
||||
int level;
|
||||
byte charset;
|
||||
|
||||
String FromCharset(const String& s) const;
|
||||
String ToCharset(const String& s) const;
|
||||
|
||||
friend class PostgreSQLConnection;
|
||||
|
||||
public:
|
||||
bool Open(const char *connect);
|
||||
bool ReOpen();
|
||||
void Close();
|
||||
|
||||
void SetCharset(byte chrset) { charset = chrset; }
|
||||
|
||||
String GetUser() { return PQuser(conn); }
|
||||
operator PGconn * () { return conn; }
|
||||
String GetUser() { return PQuser(conn); }
|
||||
operator PGconn * () { return conn; }
|
||||
|
||||
virtual void Begin();
|
||||
virtual void Commit();
|
||||
virtual void Rollback();
|
||||
virtual int GetTransactionLevel() const;
|
||||
|
||||
PostgreSQLSession() { conn = NULL; Dialect(PGSQL); level = 0; }
|
||||
~PostgreSQLSession() { Close(); }
|
||||
PGconn * GetPGConn() { return conn; }
|
||||
PostgreSQLSession() { conn = NULL; Dialect(PGSQL); level = 0; }
|
||||
~PostgreSQLSession() { Close(); }
|
||||
PGconn * GetPGConn() { return conn; }
|
||||
};
|
||||
|
||||
class PgSequence : public ValueGen {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ acceptflags
|
|||
uses
|
||||
Sql;
|
||||
|
||||
library(WIN32 !NOPOSTGRESQL MSC) ms\libpq;
|
||||
library(WIN32 !NOPOSTGRESQL MSC) libpq;
|
||||
|
||||
library(WIN32 !NOPOSTGRESQL GCC) pq;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue