From 8132cd7f9fa363972153ebe8de53038fe0a76b99 Mon Sep 17 00:00:00 2001 From: rylek Date: Tue, 12 Jan 2010 13:53:13 +0000 Subject: [PATCH] Minor Sqlite-related fixes and improvements; new helper HttpContentDisposition to simplify generation of http response headers git-svn-id: svn://ultimatepp.org/upp/trunk@1881 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Oracle/OraCommon.cpp | 25 +++++++----- uppsrc/Sql/SqlSet.cpp | 6 +-- uppsrc/Sql/SqlStatement.cpp | 7 ++-- uppsrc/Sql/Sqlexp.h | 10 ++--- uppsrc/Web/httpsrv.h | 12 ++++-- uppsrc/Web/util.cpp | 44 ++++++++++----------- uppsrc/Web/util.h | 57 +++++++++++++++------------- uppsrc/plugin/sqlite3/Sqlite3upp.cpp | 4 +- 8 files changed, 89 insertions(+), 76 deletions(-) diff --git a/uppsrc/Oracle/OraCommon.cpp b/uppsrc/Oracle/OraCommon.cpp index e2ac2179f..8e0d8ac10 100644 --- a/uppsrc/Oracle/OraCommon.cpp +++ b/uppsrc/Oracle/OraCommon.cpp @@ -270,12 +270,10 @@ Vector OracleSchemaUsers(Sql& cursor) Vector OracleSchemaTables(Sql& cursor, String database) { Vector out; - SqlSelect sel; - if(!IsNull(database)) - sel = Select(SqlId("TABLE_NAME")).From(SqlId("ALL_TABLES")).Where(SqlId("OWNER") == database); - else - sel = Select(SqlId("TABLE_NAME")).From(SqlId("USER_TABLES")); - if(sel.Execute(cursor)) + if(Select(SqlId("TABLE_NAME")) + .From(SqlId("ALL_TABLES")) + .Where(SqlId("OWNER") == Nvl(database, cursor.GetUser())) + .Execute(cursor)) out = FetchList(cursor); return out; } @@ -283,7 +281,10 @@ Vector OracleSchemaTables(Sql& cursor, String database) Vector OracleSchemaViews(Sql& cursor, String database) { Vector out; - if(Select(SqlId("VIEW_NAME")).From(SqlId("ALL_VIEWS")).Where(SqlId("OWNER") == database).Execute(cursor)) + if(Select(SqlId("VIEW_NAME")) + .From(SqlId("ALL_VIEWS")) + .Where(SqlId("OWNER") == Nvl(database, cursor.GetUser())) + .Execute(cursor)) out = FetchList(cursor); return out; } @@ -291,7 +292,10 @@ Vector OracleSchemaViews(Sql& cursor, String database) Vector OracleSchemaSequences(Sql& cursor, String database) { Vector out; - if(Select(SqlId("SEQUENCE_NAME")).From(SqlId("ALL_SEQUENCES")).Where(SqlId("SEQUENCE_OWNER") == database).Execute(cursor)) + if(Select(SqlId("SEQUENCE_NAME")) + .From(SqlId("ALL_SEQUENCES")) + .Where(SqlId("SEQUENCE_OWNER") == Nvl(database, cursor.GetUser())) + .Execute(cursor)) out = FetchList(cursor); return out; } @@ -299,12 +303,13 @@ Vector OracleSchemaSequences(Sql& cursor, String database) Vector OracleSchemaPrimaryKey(Sql& cursor, String database, String table) { Vector out; + String udb = Nvl(database, cursor.GetUser()); if(Select(SqlId("COLUMN_NAME")).From(SqlId("ALL_CONS_COLUMNS")) - .Where(SqlId("OWNER") == database && SqlId("TABLE_NAME") == table + .Where(SqlId("OWNER") == udb && SqlId("TABLE_NAME") == table && SqlId("CONSTRAINT_NAME") == Select(SqlId("CONSTRAINT_NAME")) .From(SqlId("ALL_CONSTRAINTS")) - .Where(SqlId("OWNER") == database && SqlId("TABLE_NAME") == table + .Where(SqlId("OWNER") == udb && SqlId("TABLE_NAME") == table && SqlId("CONSTRAINT_TYPE") == "P") ).OrderBy(SqlId("POSITION")) .Execute(cursor)) diff --git a/uppsrc/Sql/SqlSet.cpp b/uppsrc/Sql/SqlSet.cpp index 215a6001b..91809a7d7 100644 --- a/uppsrc/Sql/SqlSet.cpp +++ b/uppsrc/Sql/SqlSet.cpp @@ -5,19 +5,19 @@ NAMESPACE_UPP SqlSet operator|(const SqlSet& s1, const SqlSet& s2) { if(s1.IsEmpty()) return s2; if(s2.IsEmpty()) return s1; - return SqlSet(s1() + " union " + s2(), SqlSet::SETOP); + return SqlSet(s1(SqlSet::SET) + " union " + s2(SqlSet::SET), SqlSet::SETOP); } SqlSet operator&(const SqlSet& s1, const SqlSet& s2) { if(s1.IsEmpty()) return s2; if(s2.IsEmpty()) return s1; - return SqlSet(s1() + " intersect " + s2(), SqlSet::SETOP); + return SqlSet(s1(SqlSet::SET) + " intersect " + s2(SqlSet::SET), SqlSet::SETOP); } SqlSet operator-(const SqlSet& s1, const SqlSet& s2) { if(s1.IsEmpty() || s2.IsEmpty()) return s1; - return SqlSet(s1() + SqlCase(MSSQL|PGSQL, " except ")(" minus ") + s2(), SqlSet::SETOP); + return SqlSet(s1(SqlSet::SET) + SqlCase(MSSQL|PGSQL, " except ")(" minus ") + s2(SqlSet::SET), SqlSet::SETOP); } String SqlSet::operator~() const { diff --git a/uppsrc/Sql/SqlStatement.cpp b/uppsrc/Sql/SqlStatement.cpp index a047029a8..ebb5bcc70 100644 --- a/uppsrc/Sql/SqlStatement.cpp +++ b/uppsrc/Sql/SqlStatement.cpp @@ -9,17 +9,18 @@ String SqlStatement::Get(int dialect) const { } SqlSelect& SqlSelect::operator|=(const SqlSelect& s2) { - text << " union (" << s2.text << ')'; + text << " union " << SqlCase(SQLITE3, "")("(") << s2.text << SqlCase(SQLITE3, "")(")"); return *this; } SqlSelect& SqlSelect::operator&=(const SqlSelect& s2) { - text << " intersect (" << s2.text << ')'; + text << " intersect " << SqlCase(SQLITE3, "")("(") << s2.text << SqlCase(SQLITE3, "")(")"); return *this; } SqlSelect& SqlSelect::operator-=(const SqlSelect& s2) { - text << SqlCase(MSSQL|PGSQL," except (")(" minus (") << s2.text << ')'; + text << SqlCase(MSSQL|PGSQL," except ")(" minus ") << SqlCase(SQLITE3, "")("(") + << s2.text << SqlCase(SQLITE3, "")(")"); return *this; } diff --git a/uppsrc/Sql/Sqlexp.h b/uppsrc/Sql/Sqlexp.h index ea424ab7a..31fd79f4c 100644 --- a/uppsrc/Sql/Sqlexp.h +++ b/uppsrc/Sql/Sqlexp.h @@ -473,10 +473,10 @@ public: SqlSelect& RightJoin(SqlId table) { return RightJoin0(~table); } SqlSelect& FullJoin(SqlId table) { return FullJoin0(~table); } - SqlSelect& InnerJoin(const SqlSet& set) { return InnerJoin0(~set(SqlSet::SETOP)); } - SqlSelect& LeftJoin(const SqlSet& set) { return LeftJoin0(~set(SqlSet::SETOP)); } - SqlSelect& RightJoin(const SqlSet& set) { return RightJoin0(~set(SqlSet::SETOP)); } - SqlSelect& FullJoin(const SqlSet& set) { return FullJoin0(~set(SqlSet::SETOP)); } + SqlSelect& InnerJoin(const SqlSet& set) { return InnerJoin0(~set(SqlSet::SET)); } + SqlSelect& LeftJoin(const SqlSet& set) { return LeftJoin0(~set(SqlSet::SET)); } + SqlSelect& RightJoin(const SqlSet& set) { return RightJoin0(~set(SqlSet::SET)); } + SqlSelect& FullJoin(const SqlSet& set) { return FullJoin0(~set(SqlSet::SET)); } SqlSelect& Where(const SqlBool& exp); SqlSelect& On(const SqlBool& exp); @@ -498,7 +498,7 @@ public: SqlSelect& Limit(int64 offset, int limit); SqlSelect& Offset(int64 offset); - operator SqlSet() const { return SqlSet(text, SqlSet::SETOP); } + operator SqlSet() const { return SqlSet(text, SqlSet::SET); } operator SqlStatement() const { return SqlStatement(text); } SqlVal AsValue() const; SqlSelect AsTable(SqlId tab) const; diff --git a/uppsrc/Web/httpsrv.h b/uppsrc/Web/httpsrv.h index 862656bdd..97afff603 100644 --- a/uppsrc/Web/httpsrv.h +++ b/uppsrc/Web/httpsrv.h @@ -1,15 +1,19 @@ String MIMECharsetName(byte charset); -inline String HttpContentType(String type) { return "Content-Type: " + type + "\r\n"; } +inline String HttpContentType(const String& type) { return String().Cat() << "Content-Type: " << type << "\r\n"; } +inline String HttpContentDisposition(const char *disp, const String& filename) +{ return String().Cat() << "Content-Disposition: " << disp << "; filename=\"" << filename << "\"\r\n"; } +inline String HttpContentInline(const String& fn) { return HttpContentDisposition("inline", fn); } +inline String HttpContentAttachment(const String& fn) { return HttpContentDisposition("attachment", fn); } -inline String HttpTextHtml(String encoding) { return "text/html; charset=" + encoding; } +inline String HttpTextHtml(const String& encoding) { return "text/html; charset=" + encoding; } inline String HttpTextHtml(byte charset = CHARSET_DEFAULT) { return HttpTextHtml(MIMECharsetName(charset)); } //inline String HttpTextHtml1250() { return "text/html; charset=windows-1250"; } -inline String HttpTextPlain(String encoding) { return "text/plain; charset=" + encoding; } +inline String HttpTextPlain(const String& encoding) { return "text/plain; charset=" + encoding; } //inline String HttpTextPlain1250() { return "text/plain; charset=windows-1250"; } inline String HttpTextPlain(byte charset = CHARSET_DEFAULT) { return HttpTextPlain(MIMECharsetName(charset)); } inline String HttpTextXml() { return "text/xml"; } -inline String HttpImage(String format) { return "image/" + format; } +inline String HttpImage(const String& format) { return "image/" + format; } inline String HttpImageGif() { return "image/gif"; } inline String HttpImageJpg() { return "image/jpeg"; } inline String HttpImagePng() { return "image/png"; } diff --git a/uppsrc/Web/util.cpp b/uppsrc/Web/util.cpp index 4fe197533..9155bff0d 100644 --- a/uppsrc/Web/util.cpp +++ b/uppsrc/Web/util.cpp @@ -541,7 +541,7 @@ void HttpQuery::Serialize(Stream& stream) } } -HttpQuery& HttpQuery::SetURL(String url) +HttpQuery& HttpQuery::SetURL(const String& url) { const char *p = url; while(*p && *p != '\1' && *p != '?') @@ -644,7 +644,7 @@ bool HttpQuery::IsInternal(int i) const return key[0] == '$' && key[1] == '$'; } -void HttpQuery::Get(String key, Ref result) const +void HttpQuery::Get(const String& key, Ref result) const { switch(result.GetType()) { @@ -658,43 +658,43 @@ void HttpQuery::Get(String key, Ref result) const } } -bool HttpQuery::GetBool(String key) const +bool HttpQuery::GetBool(const String& key) const { int i = Find(key); return i >= 0 && data -> map[i] != "0"; } -int HttpQuery::GetInt(String key) const +int HttpQuery::GetInt(const String& key) const { int i = Find(key); return i < 0 || data->map[i].IsEmpty() ? (int)Null : atoi(data->map[i]); } -double HttpQuery::GetDouble(String key) const +double HttpQuery::GetDouble(const String& key) const { int i = Find(key); return i < 0 || data->map[i].IsEmpty() ? (double)Null : atof(data->map[i]); } -String HttpQuery::GetString(String key) const +String HttpQuery::GetString(const String& key) const { int i = Find(key); return i >= 0 ? data -> map[i] : String::GetVoid(); } -Date HttpQuery::GetDate(String key) const +Date HttpQuery::GetDate(const String& key) const { int i = Find(key); return i >= 0 ? Date(StdConvertDate().Scan(data -> map[i])) : Date(Null); } -Time HttpQuery::GetTime(String key) const +Time HttpQuery::GetTime(const String& key) const { int i = Find(key); return i >= 0 ? Time(StdConvertTime().Scan(data -> map[i])) : Time(Null); } -Color HttpQuery::GetColor(String key) const +Color HttpQuery::GetColor(const String& key) const { String s = GetString(key); if(s.GetLength() >= 6) @@ -709,7 +709,7 @@ Color HttpQuery::GetColor(String key) const return Null; } -HttpQuery& HttpQuery::SetColor(String key, Color c) +HttpQuery& HttpQuery::SetColor(const String& key, Color c) { if(IsNull(c)) SetString(key, Null); @@ -718,46 +718,46 @@ HttpQuery& HttpQuery::SetColor(String key, Color c) return *this; } -bool HttpQuery::GetBool(String key, bool dflt) const +bool HttpQuery::GetBool(const String& key, bool dflt) const { int i = Find(key); return i >= 0 ? data -> map[i] != "0" : dflt; } -int HttpQuery::GetInt(String key, int min, int max, int dflt) const +int HttpQuery::GetInt(const String& key, int min, int max, int dflt) const { int v = GetInt(key); return !IsNull(v) ? minmax(v, min, max) : dflt; } -double HttpQuery::GetDouble(String key, double min, double max, double dflt) const +double HttpQuery::GetDouble(const String& key, double min, double max, double dflt) const { double v = GetDouble(key); return !IsNull(v) ? minmax(v, min, max) : dflt; } -String HttpQuery::GetString(String key, String dflt) const +String HttpQuery::GetString(const String& key, const String& dflt) const { int i = Find(key); return i >= 0 && !data -> map[i].IsEmpty() ? data -> map[i] : dflt; } -HttpQuery& HttpQuery::SetBool(String key, bool b) +HttpQuery& HttpQuery::SetBool(const String& key, bool b) { return Set(key, b ? "1" : ""); } -HttpQuery& HttpQuery::SetInt(String key, int i) +HttpQuery& HttpQuery::SetInt(const String& key, int i) { return Set(key, IntStr(i)); } -HttpQuery& HttpQuery::SetDouble(String key, double f) +HttpQuery& HttpQuery::SetDouble(const String& key, double f) { return Set(key, DblStr(f)); } -HttpQuery& HttpQuery::Set(String key, String s) +HttpQuery& HttpQuery::Set(const String& key, const String& s) { int i = Find(key); if(i < 0 || data -> map[i] != s) @@ -771,17 +771,17 @@ HttpQuery& HttpQuery::Set(String key, String s) return *this; } -HttpQuery& HttpQuery::SetDate(String key, Date d) +HttpQuery& HttpQuery::SetDate(const String& key, Date d) { return Set(key, Format(d)); } -HttpQuery& HttpQuery::SetTime(String key, Time t) +HttpQuery& HttpQuery::SetTime(const String& key, Time t) { return Set(key, Format(t)); } -HttpQuery& HttpQuery::SetValue(String key, Value v) +HttpQuery& HttpQuery::SetValue(const String& key, const Value& v) { return Set(key, StdFormat(v)); } @@ -798,7 +798,7 @@ HttpQuery& HttpQuery::Set(HttpQuery query) return *this; } -HttpQuery& HttpQuery::Remove(String key) +HttpQuery& HttpQuery::Remove(const String& key) { int i = Find(key); if(i < 0) diff --git a/uppsrc/Web/util.h b/uppsrc/Web/util.h index 7a020fc75..ecc1fbe36 100644 --- a/uppsrc/Web/util.h +++ b/uppsrc/Web/util.h @@ -233,44 +233,47 @@ public: String GetKey(int i) const { return data -> map.GetKey(i); } String GetValue(int i) const { return data -> map[i]; } bool IsInternal(int i) const; + + String operator [] (int i) const { return GetValue(i); } + String operator [] (const String& key) const { return GetString(key); } - void Get(String key, Ref p1) const; - HttpQuery& SetValue(String key, Value v); - HttpQuery& Set(String key, String value); - HttpQuery& SetRaw(String key, String value); + void Get(const String& key, Ref p1) const; + HttpQuery& SetValue(const String& key, const Value& v); + HttpQuery& Set(const String& key, const String& value); + HttpQuery& SetRaw(const String& key, const String& value); - bool GetBool(String key) const; - bool GetBool(String key, bool dflt) const; - HttpQuery& SetBool(String key, bool b); + bool GetBool(const String& key) const; + bool GetBool(const String& key, bool dflt) const; + HttpQuery& SetBool(const String& key, bool b); - int GetInt(String key) const; - int GetInt(String key, int min, int max, int dflt = 0) const; - HttpQuery& SetInt(String key, int i); + int GetInt(const String& key) const; + int GetInt(const String& key, int min, int max, int dflt = 0) const; + HttpQuery& SetInt(const String& key, int i); - double GetDouble(String key) const; - double GetDouble(String key, double min, double max, double dflt = Null) const; - HttpQuery& SetDouble(String key, double f); + double GetDouble(const String& key) const; + double GetDouble(const String& key, double min, double max, double dflt = Null) const; + HttpQuery& SetDouble(const String& key, double f); - String GetString(String key) const; - String GetString(String key, String dflt) const; - HttpQuery& SetString(String key, String s) { return Set(key, s); } + String GetString(const String& key) const; + String GetString(const String& key, const String& dflt) const; + HttpQuery& SetString(const String& key, const String& s) { return Set(key, s); } - Date GetDate(String key) const; - Date GetDate(String key, Date dflt) const { return Nvl(GetDate(key), dflt); } - HttpQuery& SetDate(String key, Date d); + Date GetDate(const String& key) const; + Date GetDate(const String& key, Date dflt) const { return Nvl(GetDate(key), dflt); } + HttpQuery& SetDate(const String& key, Date d); - Time GetTime(String key) const; - Time GetTime(String key, Time dflt) const { return Nvl(GetTime(key), dflt); } - HttpQuery& SetTime(String key, Time t); + Time GetTime(const String& key) const; + Time GetTime(const String& key, Time dflt) const { return Nvl(GetTime(key), dflt); } + HttpQuery& SetTime(const String& key, Time t); - Color GetColor(String key) const; - Color GetColor(String key, Color dflt) const { return Nvl(GetColor(key), dflt); } - HttpQuery& SetColor(String key, Color c); + Color GetColor(const String& key) const; + Color GetColor(const String& key, Color dflt) const { return Nvl(GetColor(key), dflt); } + HttpQuery& SetColor(const String& key, Color c); HttpQuery& Set(HttpQuery query); - HttpQuery& SetURL(String url); + HttpQuery& SetURL(const String& url); - HttpQuery& Remove(String key); + HttpQuery& Remove(const String& key); HttpQuery& Remove(const Vector& keys); HttpQuery& Remove(const Vector& keys); diff --git a/uppsrc/plugin/sqlite3/Sqlite3upp.cpp b/uppsrc/plugin/sqlite3/Sqlite3upp.cpp index 0bd51f118..9f5dbccd3 100644 --- a/uppsrc/plugin/sqlite3/Sqlite3upp.cpp +++ b/uppsrc/plugin/sqlite3/Sqlite3upp.cpp @@ -239,7 +239,7 @@ void Sqlite3Connection::GetColumn(int i, Ref f) const { f = sqlite3_column_double(current_stmt,i); break; case SQLITE_TEXT: - if(coltype == "date"){ + if(coltype == "date" || f.GetType() == DATE_V){ const char *s = (const char *)sqlite3_column_text(current_stmt, i); if(strlen(s) >= 10) f = Value(Date(atoi(s), atoi(s + 5), atoi(s + 8))); @@ -247,7 +247,7 @@ void Sqlite3Connection::GetColumn(int i, Ref f) const { f = Null; } else - if(coltype == "datetime") { + if(coltype == "datetime" || f.GetType() == TIME_V) { const char *s = (const char *)sqlite3_column_text(current_stmt, i); if(strlen(s) >= 19) f = Value(Time(atoi(s), atoi(s + 5), atoi(s + 8), atoi(s + 11), atoi(s + 14), atoi(s + 17)));