NewDMLUpdateQueryBuilder creates a new DMLUpdateQueryBuilder. It prepares the UPDATE query statement. Returns an error if no shared columns are given, the shared columns are not a subset of the table columns, no unique key columns are given or the prepared statement cannot be built.
(databaseName, tableName string, tableColumns, sharedColumns, mappedSharedColumns, uniqueKeyColumns *ColumnList)
| 919 | // Returns an error if no shared columns are given, the shared columns are not a subset of the table columns, |
| 920 | // no unique key columns are given or the prepared statement cannot be built. |
| 921 | func NewDMLUpdateQueryBuilder(databaseName, tableName string, tableColumns, sharedColumns, mappedSharedColumns, uniqueKeyColumns *ColumnList) (*DMLUpdateQueryBuilder, error) { |
| 922 | if !sharedColumns.IsSubsetOf(tableColumns) { |
| 923 | return nil, fmt.Errorf("shared columns is not a subset of table columns in NewDMLUpdateQueryBuilder") |
| 924 | } |
| 925 | if sharedColumns.Len() == 0 { |
| 926 | return nil, fmt.Errorf("no shared columns found in NewDMLUpdateQueryBuilder") |
| 927 | } |
| 928 | if uniqueKeyColumns.Len() == 0 { |
| 929 | return nil, fmt.Errorf("no unique key columns found in NewDMLUpdateQueryBuilder") |
| 930 | } |
| 931 | // If unique key contains virtual columns, those column won't be in sharedColumns |
| 932 | // which only contains non-virtual columns |
| 933 | nonVirtualUniqueKeyColumns := uniqueKeyColumns.FilterBy(func(column Column) bool { return !column.IsVirtual }) |
| 934 | if !nonVirtualUniqueKeyColumns.IsSubsetOf(sharedColumns) { |
| 935 | return nil, fmt.Errorf("unique key columns is not a subset of shared columns in NewDMLUpdateQueryBuilder") |
| 936 | } |
| 937 | databaseName = EscapeName(databaseName) |
| 938 | tableName = EscapeName(tableName) |
| 939 | setClause, err := BuildSetPreparedClause(mappedSharedColumns) |
| 940 | if err != nil { |
| 941 | return nil, err |
| 942 | } |
| 943 | |
| 944 | equalsComparison, err := BuildEqualsPreparedComparison(uniqueKeyColumns.Names()) |
| 945 | if err != nil { |
| 946 | return nil, err |
| 947 | } |
| 948 | stmt := fmt.Sprintf(` |
| 949 | update /* gh-ost %s.%s */ |
| 950 | %s.%s |
| 951 | set |
| 952 | %s |
| 953 | where |
| 954 | %s`, |
| 955 | databaseName, tableName, |
| 956 | databaseName, tableName, |
| 957 | setClause, |
| 958 | equalsComparison, |
| 959 | ) |
| 960 | return &DMLUpdateQueryBuilder{ |
| 961 | tableColumns: tableColumns, |
| 962 | sharedColumns: sharedColumns, |
| 963 | uniqueKeyColumns: uniqueKeyColumns, |
| 964 | preparedStatement: stmt, |
| 965 | }, nil |
| 966 | } |
| 967 | |
| 968 | // BuildQuery builds the arguments array for a DML event UPDATE query. |
| 969 | // It returns the query string, the shared arguments array, and the unique key arguments array. |
searching dependent graphs…