Creatio development guide
PDF
This documentation is valid for Creatio version 7.15.0. We recommend using the newest version of Creatio documentation.

Modifying data. The Update class

Glossary Item Box

Introduction

The Terrasoft.Core.DB.Update class is used to build queries for modifying records in Creatio database tables. As a result of creating and configuring the instance of this class, the UPDATE SQL-expression query to the application database will be built.

The Terrasoft.Core.DB.Update class

NOTE

Use the “.NET class libraries of platform core” documentation to access the full list of the Update class methods and properties, its parent classes and the implemented interfaces.

Constructors

  • public Update(UserConnection userConnection) – creates a class instance using UserConnection
  • public Update(UserConnection userConnection, string schemaName) — creates a class instance for schema with the specified name, using UserConnection
  • public Update(UserConnection userConnection, ModifyQuerySource source) — creates a class instance for schema with the specified name, using UserConnection
  • public Update(Insert source) – creates a class instance that is a clone of the instance passed as an argument

Properties

Table 1. Primary properties of the class

Property Type Description
UserConnection Terrasoft.Core.UserConnection The current user connection at the moment of executing the query.
Condition Terrasoft.Core.DB.QueryCondition Condition of Where expression in the query.
HasCondition bool Determines whether Where expression is available in the query.
Source. Terrasoft.Core.DB.ModifyQuerySource The query data source.
ColumnValues Terrasoft.Core.DB.ModifyQueryColumnValueCollection Collection of values of the query columns

Methods

Table 2. Primary methods of the class

Methods Description
string GetSqlText() Returns the SQL text of the current query.
void BuildSqlText(StringBuilder sb) Generates the query text via the StringBuilder instance.
void ResetCachedSqlText() Clears the cached text of the query.
QueryParameterCollection GetUsingParameters() Returns the collection of parameters used by the query.

int Execute()

Executes the query. Returns the number of records involved by the query.

int Execute(DBExecutor dbExecutor)

Executes the query using the DBExecutor instance. Returns the number of records involved by the query.

QueryCondition Where()

QueryCondition Where(string sourceColumnAlias)

QueryCondition Where(string sourceAlias, string sourceColumnAlias)

QueryCondition Where(Select subSelect)

QueryCondition Where(Query subSelectQuery)

QueryCondition Where(QueryColumnExpression columnExpression)

Query Where(QueryCondition condition)

Adds the initial condition to the current query.

Parameters:

  • sourceColumnAlias – an alias of the column for which the condition is added
  • sourceAlias – the alias of the source
  • subSelect – a subquery of the data selection, for whose results the condition is added
  • subSelectQuery – the subquery, for whose results the condition is added
  • columnExpression – the expression, for whose results the condition is added
  • condition – the query condition

QueryCondition And()

QueryCondition And(string sourceColumnAlias)

QueryCondition And(string sourceAlias, string sourceColumnAlias)

QueryCondition And(Select subSelect)

QueryCondition And(Query subSelectQuery)

QueryCondition And(QueryParameter parameter)

QueryCondition And(QueryColumnExpression columnExpression)

Query And(QueryCondition condition)

Adds the condition (predicate) to the current query condition using the AND logical operation.

Parameters:

  • sourceColumnAlias – an alias of the column, for which the predicate is added
  • sourceAlias – the alias of the source
  • subSelect – the data selection subquery used as a predicate
  • subSelectQuery – the subquery used as a predicate
  • parameter – parameter that the predicate is added to
  • columnExpression – the expression used as a predicate
  • condition – the query condition

QueryCondition Or()

QueryCondition Or(string sourceColumnAlias)

QueryCondition Or(string sourceAlias, string sourceColumnAlias)

QueryCondition Or(Select subSelect)

QueryCondition Or(Query subSelectQuery)

QueryCondition Or(QueryParameter parameter)

QueryCondition Or(QueryColumnExpression columnExpression)

Query Or(QueryCondition condition)

Adds the condition (predicate) to the current query condition using the OR logical operation.

Parameters:

  • sourceColumnAlias – an alias of the column, for which the predicate is added
  • sourceAlias – the alias of the source
  • subSelect – the data selection subquery used as a predicate
  • subSelectQuery – the subquery used as a predicate
  • parameter – parameter that the predicate is added to
  • columnExpression – the expression used as a predicate
  • condition – the query condition

Update Set(string sourceColumnAlias, Select subSelect)

Update Set(string sourceColumnAlias, Query subSelectQuery)

Update Set(string sourceColumnAlias, QueryColumnExpression columnExpression)

Update Set(string sourceColumnAlias, QueryParameter parameter)

Adds a SET clause to the current query for assigning the passed expression or parameter to the column. Returns the current Update instance.

Parameters:

  • sourceColumnAlias – column alias
  • subSelect – selection subquery
  • subSelectQuery – subquery
  • columnExpression – expression of the column
  • parameter – parameter of the query

Use cases

You can download the package with the configuration web serice implementing the below described cases using the following link.

ATTENTION

In most cases, a request for modification should contain the “Where” condition, which specifies which records exactly should be modified. Otherwise, all records will be modified.

Example 1

Receive the SQL-query text.

public string GetSqlTextExample(string oldName, string newName)
{
    var result = "";
    var update = new Update(UserConnection, "Contact")
        .Set("Name", Column.Parameter(newName))
        .Where("Name").IsEqual(Column.Parameter(oldName));
    result = update.GetSqlText();
    return result;
}

Example 2

Change the contact name for a new one.

 public string ChangeContactName(string oldName, string newName)
{
    var update = new Update(UserConnection, "Contact")
        .Set("Name", Column.Parameter(newName))
        .Where ("Name").IsEqual(Column.Parameter(oldName));
    var cnt = update.Execute();
    return $"Contacts {oldName} changed to {newName}. {cnt} rows affected.";
}

Example 3

Change the user that has modified all existing contact records, to the specified value.

public string ChangeAllContactModifiedBy(string Name)
{
    var update = new Update(UserConnection, "Contact")
        .Set("ModifiedById", 
            new Select(UserConnection).Top(1)
                .Column("Id")
                .From("Contact")
                .Where("Name").IsEqual(Column.Parameter(Name)));
    var cnt = update.Execute();
    return $"All contacts are changed by {Name} now. {cnt} rows affected.";
}

NOTE

In this example, the Where condition refers to the Select request. The Update request does not contain the Where condition, since it is necessary to modify all records.

© Creatio 2002-2020.

Did you find this information useful?

How can we improve it?