private MySqlCommand BuildGetChangesCommand()

in src/TriggersBinding/MySqlTableChangeMonitor.cs [656:692]


        private MySqlCommand BuildGetChangesCommand(MySqlConnection connection, MySqlTransaction transaction)
        {
            string selectList = string.Join(", ", this._userTableColumns.Select(col => $"u.{col.AsAcuteQuotedString()}"));
            string leasesTableJoinCondition = string.Join(" AND ", this._primaryKeyColumns.Select(col => $"u.{col.name.AsAcuteQuotedString()} = l.{col.name.AsAcuteQuotedString()}"));

            string primaryKeysAsc = string.Join("ASC , ", this._primaryKeyColumnNames);

            string getChangesQuery = $@"
                        SELECT {selectList}, 
                        DATE_FORMAT(u.{UpdateAtColumnName}, '%Y-%m-%d %H:%i:%s') AS {UpdateAtColumnName},
                        l.{LeasesTableAttemptCountColumnName},
                        l.{LeasesTableLeaseExpirationTimeColumnName}
                        FROM {this._userTable.AcuteQuotedFullName} AS u
                        LEFT JOIN {this._leasesTableName} AS l ON {leasesTableJoinCondition}
                        WHERE 
                            ({UpdateAtColumnName} > (select {GlobalStateTableLastPolledTimeColumnName} from {GlobalStateTableName} where {GlobalStateTableUserFunctionIDColumnName} = '{this._userFunctionId}' AND {GlobalStateTableUserTableIDColumnName} = '{this._userTableId}'))
                            AND
                            (   (l.{LeasesTableLeaseExpirationTimeColumnName} IS NULL) 
                                OR
                                (l.{LeasesTableLeaseExpirationTimeColumnName} < {MYSQL_FUNC_CURRENTTIME})
                            )
                            AND
                            (   (l.{LeasesTableAttemptCountColumnName} IS NULL)
                                OR 
                                (l.{LeasesTableAttemptCountColumnName} < {MaxChangeProcessAttemptCount})
                            )
                            AND
                            (   (l.{LeasesTableSyncCompletedTime} IS NULL)
                                OR 
                                (l.{LeasesTableSyncCompletedTime} < {UpdateAtColumnName})
                            )
                        ORDER BY u.{UpdateAtColumnName} ASC, {primaryKeysAsc}
                        LIMIT {this._maxBatchSize};
                        ";

            return new MySqlCommand(getChangesQuery, connection, transaction);
        }