diff --git a/uppsrc/Sql/Sql.cpp b/uppsrc/Sql/Sql.cpp index f2bffb4a9..a9ac88f42 100644 --- a/uppsrc/Sql/Sql.cpp +++ b/uppsrc/Sql/Sql.cpp @@ -236,7 +236,7 @@ struct sReadFields : public FieldOperator { Sql *sql; void Field(const char *name, Ref f) { - f = (*sql)[SqlId(name)]; + sql->GetColumn(SqlId(name), f); } }; @@ -262,6 +262,17 @@ void Sql::GetColumn(int i, Ref r) const { cn->GetColumn(i, r); } +void Sql::GetColumn(SqlId colid, Ref r) const +{ + String s = ~colid; + for(int i = 0; i < cn->info.GetCount(); i++) + if(cn->info[i].name == s) { + GetColumn(i, r); + return; + } + r.SetNull(); +} + Value Sql::operator[](int i) const { Value v; cn->GetColumn(i, v); diff --git a/uppsrc/Sql/SqlStatement.cpp b/uppsrc/Sql/SqlStatement.cpp index eafcc9f55..51ca6e42c 100644 --- a/uppsrc/Sql/SqlStatement.cpp +++ b/uppsrc/Sql/SqlStatement.cpp @@ -295,6 +295,11 @@ void SqlUpdate::Column(SqlId column, SqlVal val) { set.Cat(SqlVal(SqlVal(column), " = ", val, SqlS::COMP)); } +void SqlUpdate::Column(const SqlSet& cols, const SqlSet& val) +{ + set.Cat(SqlVal(SqlS(cols(), SqlS::HIGH), " = ", SqlS(val(), SqlS::HIGH), SqlS::COMP)); +} + // ------------------------------------ // deprecated diff --git a/uppsrc/Sql/Sqlexp.h b/uppsrc/Sql/Sqlexp.h index 5ef994db3..98b6a082c 100644 --- a/uppsrc/Sql/Sqlexp.h +++ b/uppsrc/Sql/Sqlexp.h @@ -613,7 +613,9 @@ class SqlUpdate { public: void Column(SqlId column, SqlVal val); + void Column(const SqlSet& cols, const SqlSet& val); SqlUpdate& operator()(SqlId column, SqlVal val) { Column(column, val); return *this; } + SqlUpdate& operator()(const SqlSet& cols, const SqlSet& val) { Column(cols, val); return *this; } SqlUpdate& operator()(Fields f); SqlUpdate& Where(SqlBool w) { where = w; return *this; } diff --git a/uppsrc/Sql/Sqls.h b/uppsrc/Sql/Sqls.h index db154c5cb..e178b3c36 100644 --- a/uppsrc/Sql/Sqls.h +++ b/uppsrc/Sql/Sqls.h @@ -134,6 +134,7 @@ public: int GetColumns() const; void GetColumn(int i, Ref r) const; + void GetColumn(SqlId colid, Ref r) const; Value operator[](int i) const; Value operator[](SqlId colid) const; const SqlColumnInfo& GetColumnInfo(int i) const { return cn->info[i]; } diff --git a/uppsrc/TSql/util.cpp b/uppsrc/TSql/util.cpp index 59d0ce01b..a7924921d 100644 --- a/uppsrc/TSql/util.cpp +++ b/uppsrc/TSql/util.cpp @@ -377,6 +377,24 @@ void ForceUpdate(SqlId table, Fields nf, SqlId key, const Value& keyval, Sql& cu cursor & helper.Get(table).Where(key == keyval); } +void ForceUpdate(Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor) +{ + ForceUpdate(SqlId(), nf, of, key, keyval, cursor); +} + +void ForceUpdate(SqlId table, Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor) +{ + String tbl; + VectorMap nmap = GetValueMap(nf, &tbl); + VectorMap omap = GetValueMap(of); + SqlUpdate update(SqlId(Nvl(~table, tbl))); + for(int i = 0; i < nmap.GetCount(); i++) + if(nmap[i] != omap.Get(nmap.GetKey(i), Value())) + update(SqlId(nmap.GetKey(i)), nmap[i]); + if(update) + update.Where(key == keyval).Force(cursor); +} + void ForceDelete(SqlId table, SqlId key, const Value& keyval, Sql& cursor) { if(!cursor.Delete(table, key, keyval)) @@ -413,6 +431,24 @@ void ForceSchemaUpdate(SqlId table, Fields nf, SqlId key, const Value& keyval, S helper.GetSchema(table).Where(key == keyval).Force(cursor); } +void ForceSchemaUpdate(Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor) +{ + ForceSchemaUpdate(SqlId(), nf, of, key, keyval, cursor); +} + +void ForceSchemaUpdate(SqlId table, Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor) +{ + String tbl; + VectorMap nmap = GetValueMap(nf, &tbl); + VectorMap omap = GetValueMap(of); + SqlUpdate update(SqlId(SchemaTableName(Nvl(~table, tbl)))); + for(int i = 0; i < nmap.GetCount(); i++) + if(nmap[i] != omap.Get(nmap.GetKey(i), Value())) + update(SqlId(nmap.GetKey(i)), nmap[i]); + if(update) + update.Where(key == keyval).Force(cursor); +} + void ForceSchemaDelete(SqlId table, SqlId key, const Value& keyval, Sql& cursor) { Delete(SchemaTable(table)).Where(key == keyval).Force(cursor); diff --git a/uppsrc/TSql/util.h b/uppsrc/TSql/util.h index 7e62eb08b..1bc856adf 100644 --- a/uppsrc/TSql/util.h +++ b/uppsrc/TSql/util.h @@ -61,12 +61,16 @@ void ForceInsert(Fields nf, Sql& cursor APPSQLCURSOR); void ForceInsert(SqlId table, Fields nf, Sql& cursor APPSQLCURSOR); void ForceUpdate(Fields nf, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); void ForceUpdate(SqlId table, Fields nf, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); +void ForceUpdate(Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); +void ForceUpdate(SqlId table, Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); void ForceDelete(SqlId table, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); void ForceExecute(const String& s, Sql& cursor APPSQLCURSOR); void ForceSchemaInsert(Fields nf, Sql& cursor APPSQLCURSOR); void ForceSchemaInsert(SqlId table, Fields nf, Sql& cursor APPSQLCURSOR); void ForceSchemaUpdate(Fields nf, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); void ForceSchemaUpdate(SqlId table, Fields nf, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); +void ForceSchemaUpdate(Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); +void ForceSchemaUpdate(SqlId table, Fields nf, Fields of, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); void ForceSchemaDelete(SqlId table, SqlId key, const Value& keyval, Sql& cursor APPSQLCURSOR); bool IsNotEmpty(const SqlSelect& select, Sql& cursor APPSQLCURSOR);