From 951d74e2213e91d6a65644d8bc39e43a6c76f6d3 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 25 May 2009 08:30:49 +0000 Subject: [PATCH] SqlBinary changed to work as SqlCompile type -> it now should work in PGSQL too git-svn-id: svn://ultimatepp.org/upp/trunk@1232 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Util.cpp | 2 +- uppsrc/Core/Util.h | 2 ++ uppsrc/Sql/SqlCase.cpp | 53 ++++++++++++++++++++++++++++++++++++++++-- uppsrc/Sql/SqlVal.cpp | 12 ++++++---- uppsrc/Sql/Sqlexp.h | 2 ++ 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/uppsrc/Core/Util.cpp b/uppsrc/Core/Util.cpp index 628b85e84..ad73ab5b2 100644 --- a/uppsrc/Core/Util.cpp +++ b/uppsrc/Core/Util.cpp @@ -539,7 +539,7 @@ String HexString(const byte *s, int count, int sep) String HexString(const String& s, int sep) { - return HexString(s, s.GetCount(), sep); + return HexString(~s, s.GetCount(), sep); } String NormalizeSpaces(const char *s) diff --git a/uppsrc/Core/Util.h b/uppsrc/Core/Util.h index ae78ed91b..5b6a55e92 100644 --- a/uppsrc/Core/Util.h +++ b/uppsrc/Core/Util.h @@ -35,6 +35,8 @@ String Encode64(const String& s); String Decode64(const String& s); String HexString(const byte *s, int count, int sep = INT_MAX); +inline String HexString(const char *s, int count, int sep = INT_MAX) { return HexString((byte *)s, count, sep); } +inline String HexString(const void *s, int count, int sep = INT_MAX) { return HexString((byte *)s, count, sep); } String HexString(const String& s, int sep = INT_MAX); #ifdef PLATFORM_WINCE diff --git a/uppsrc/Sql/SqlCase.cpp b/uppsrc/Sql/SqlCase.cpp index fecccec8c..5d7c7cede 100644 --- a/uppsrc/Sql/SqlCase.cpp +++ b/uppsrc/Sql/SqlCase.cpp @@ -10,6 +10,7 @@ enum { SQLC_DATE, SQLC_TIME, SQLC_STRING, + SQLC_BINARY, }; template @@ -124,6 +125,39 @@ void SqlCompile(const char *&s, StringBuffer *r, byte dialect) } break; } + case SQLC_BINARY: { + int l; + ReadSqlValue(l, s); + if(r) { + switch(dialect) { + case PGSQL: { + *r << "\'"; + const char *e = s + l; + while(s < e) { + byte c = *s++; + if(c < 32 || c > 126 || c == 39 || c == 92) { + *r << '\\\\'; + r->Cat(((c >> 6) & 3) + '0'); + r->Cat(((c >> 3) & 7) + '0'); + r->Cat((c & 7) + '0'); + } + } + *r << "\'::bytea"; + } + case MSSQL: + *r << "0x" << HexString(s, l); + break; + case SQLITE3: + case MY_SQL: + *r << "X"; + default: + *r << "\'" << HexString(s, l) << "\'"; + break; + } + } + s += l; + break; + } case SQLC_STRING: { int l; ReadSqlValue(l, s); @@ -190,15 +224,20 @@ String SqlFormat(int64 x) return FormatInt64(x); } -String SqlFormat(const char *s, int l) +String SqlFormat0(const char *s, int l, int code) { StringBuffer b(1 + sizeof(int) + l); - b[0] = SQLC_STRING; + b[0] = code; memcpy(~b + 1, &l, sizeof(int)); memcpy(~b + 1 + sizeof(int), s, l); return b; } +String SqlFormat(const char *s, int l) +{ + return SqlFormat0(s, l, SQLC_STRING); +} + String SqlFormat(const char *s) { return SqlFormat(s, strlen(s)); @@ -209,6 +248,16 @@ String SqlFormat(const String& x) return SqlFormat(x, x.GetLength()); } +String SqlFormatBinary(const char *s, int l) +{ + return SqlFormat0(s, l, SQLC_BINARY); +} + +String SqlFormatBinary(const String& x) +{ + return SqlFormatBinary(x, x.GetLength()); +} + String SqlFormat(Date x) { return MakeSqlValue(SQLC_DATE, x); diff --git a/uppsrc/Sql/SqlVal.cpp b/uppsrc/Sql/SqlVal.cpp index cd3aa06f7..1b5b3aa86 100644 --- a/uppsrc/Sql/SqlVal.cpp +++ b/uppsrc/Sql/SqlVal.cpp @@ -406,12 +406,16 @@ SqlVal OuterJoin(SqlCol col) return SqlCol(~col + "(+)"); } +SqlVal SqlBinary(const char *s, int l) +{ + SqlVal x; + x.SetHigh(SqlFormatBinary(s, l)); + return x; +} + SqlVal SqlBinary(const String& data) { - String x = SqlCase(MSSQL, "0x")(MY_SQL, "x\'")(SQLITE3, "X\'")("\'"); - x << HexString(data) - << SqlCase(MSSQL, "")("\'"); - return SqlVal(x, SqlS::HIGH); + return SqlBinary(~data, data.GetCount()); } END_UPP_NAMESPACE diff --git a/uppsrc/Sql/Sqlexp.h b/uppsrc/Sql/Sqlexp.h index 639571a12..53337a34d 100644 --- a/uppsrc/Sql/Sqlexp.h +++ b/uppsrc/Sql/Sqlexp.h @@ -78,6 +78,8 @@ String SqlFormat(const String& x); String SqlFormat(Date x); String SqlFormat(Time x); String SqlFormat(const Value& x); +String SqlFormatBinary(const char *s, int l); +String SqlFormatBinary(const String& x); class SqlCol : Moveable { String name;