From 12781841daa8545aa8327a08126da8823057c870 Mon Sep 17 00:00:00 2001 From: Jakub Pawlewicz Date: Tue, 26 Jun 2018 11:21:46 -0600 Subject: [PATCH] Added returning statement to sql syntax --- uppsrc/Sql/SqlStatement.cpp | 21 +++++++++++++++++++++ uppsrc/Sql/Sqlexp.h | 15 +++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/uppsrc/Sql/SqlStatement.cpp b/uppsrc/Sql/SqlStatement.cpp index 53dd03825..56f35a297 100644 --- a/uppsrc/Sql/SqlStatement.cpp +++ b/uppsrc/Sql/SqlStatement.cpp @@ -386,6 +386,11 @@ SqlDelete& SqlDelete::Where(const SqlBool& b) { return *this; } +SqlDelete& SqlDelete::Returning(const SqlSet& set) { + text << " returning " + ~set; + return *this; +} + // ------------------------------- #ifdef NEWINSERTUPDATE @@ -407,6 +412,8 @@ SqlInsert::operator SqlStatement() const { if(!set2.IsEmpty()) s << " values " << set2(); } + if(!ret.IsEmpty()) + s << " returning " << ~ret; return SqlStatement(s); } @@ -448,6 +455,12 @@ SqlInsert& SqlInsert::Where(const SqlBool& w) return *this; } +SqlInsert& SqlInsert::Returning(const SqlSet& set) +{ + ret = set; + return *this; +} + SqlInsert& SqlInsert::operator()(const ValueMap& data) { for(int i = 0; i < data.GetCount(); i++) @@ -577,6 +590,12 @@ SqlUpdate& SqlUpdate::Where(SqlBool w) return *this; } +SqlUpdate& SqlUpdate::Returning(const SqlSet& set) +{ + ret = set; + return *this; +} + SqlUpdate::operator SqlStatement() const { StringBuffer stmt; stmt << "update " << table.Quoted() << " set " << ~set; @@ -585,6 +604,8 @@ SqlUpdate::operator SqlStatement() const { else if(!where.IsEmpty()) stmt << " where " << ~where; + if(!ret.IsEmpty()) + stmt << " returning " << ~ret; return SqlStatement(stmt); } diff --git a/uppsrc/Sql/Sqlexp.h b/uppsrc/Sql/Sqlexp.h index 80d86f2db..0d151e834 100644 --- a/uppsrc/Sql/Sqlexp.h +++ b/uppsrc/Sql/Sqlexp.h @@ -673,9 +673,14 @@ __Expand(E__QSelect); class SqlDelete { String text; + SqlSet ret; public: SqlDelete& Where(const SqlBool& b); + SqlDelete& Returning(const SqlSet& set); + SqlDelete& Returning(SqlVal a) { return Returning(SqlSet(a)); } + SqlDelete& Returning(SqlVal a, SqlVal b) { return Returning(SqlSet(a, b)); } + SqlDelete& Returning(SqlVal a, SqlVal b, SqlVal c){ return Returning(SqlSet(a, b, c)); } operator SqlStatement() const { return SqlStatement(text); } @@ -703,6 +708,7 @@ class SqlInsert { SqlSet set1; SqlSet set2; SqlSelect sel; + SqlSet ret; public: void Column(const SqlId& column, SqlVal val); @@ -744,6 +750,7 @@ public: SqlInsert& GroupBy(const SqlSet& columnset) { sel.GroupBy(columnset); return *this; } SqlInsert& Having(const SqlBool& exp) { sel.Having(exp); return *this; } SqlInsert& OrderBy(const SqlSet& columnset) { sel.OrderBy(columnset); return *this; } + SqlInsert& Returning(const SqlSet& set); SqlInsert& GroupBy(SqlVal a) { sel.GroupBy(SqlSet(a)); return *this; } SqlInsert& GroupBy(SqlVal a, SqlVal b) { sel.GroupBy(SqlSet(a, b)); return *this; } @@ -751,6 +758,9 @@ public: SqlInsert& OrderBy(SqlVal a) { sel.OrderBy(SqlSet(a)); return *this; } SqlInsert& OrderBy(SqlVal a, SqlVal b) { sel.OrderBy(SqlSet(a, b)); return *this; } SqlInsert& OrderBy(SqlVal a, SqlVal b, SqlVal c) { sel.OrderBy(SqlSet(a, b, c)); return *this; } + SqlInsert& Returning(SqlVal a) { return Returning(SqlSet(a)); } + SqlInsert& Returning(SqlVal a, SqlVal b) { return Returning(SqlSet(a, b)); } + SqlInsert& Returning(SqlVal a, SqlVal b, SqlVal c){ return Returning(SqlSet(a, b, c)); } SqlInsert& Limit(int limit) { sel.Limit(limit); return *this; } SqlInsert& Limit(int64 offset, int limit) { sel.Limit(offset, limit); return *this; } SqlInsert& Offset(int64 offset) { sel.Offset(offset); return *this; } @@ -838,6 +848,7 @@ class SqlUpdate { SqlSet set; SqlBool where; SqlSelect sel; + SqlSet ret; public: void Column(const SqlId& column, SqlVal val); @@ -871,6 +882,10 @@ public: SqlUpdate& On(const SqlBool& exp) { sel.On(exp); return *this; } SqlUpdate& Where(SqlBool w); + SqlUpdate& Returning(const SqlSet& set); + SqlUpdate& Returning(SqlVal a) { return Returning(SqlSet(a)); } + SqlUpdate& Returning(SqlVal a, SqlVal b) { return Returning(SqlSet(a, b)); } + SqlUpdate& Returning(SqlVal a, SqlVal b, SqlVal c){ return Returning(SqlSet(a, b, c)); } operator SqlStatement() const; -- 2.17.1