Ejemplo n.º 1
0
        private string CreateUpdateUntrackedRowsCommand()
        {
            var stringBuilder = new StringBuilder();
            var str1          = new StringBuilder();
            var str2          = new StringBuilder();
            var str3          = new StringBuilder();
            var str4          = MySqlManagementUtils.JoinTwoTablesOnClause(this.TableDescription.GetPrimaryKeysColumns(), "`side`", "`base`");

            stringBuilder.AppendLine($"INSERT INTO {trackingName.Schema().Quoted().ToString()} (");


            var comma = "";

            foreach (var pkeyColumn in TableDescription.GetPrimaryKeysColumns())
            {
                var pkeyColumnName = ParserName.Parse(pkeyColumn, "`").Quoted().ToString();

                str1.Append($"{comma}{pkeyColumnName}");
                str2.Append($"{comma}`base`.{pkeyColumnName}");
                str3.Append($"{comma}`side`.{pkeyColumnName}");

                comma = ", ";
            }
            stringBuilder.Append(str1.ToString());
            stringBuilder.AppendLine($", `update_scope_id`, `sync_row_is_tombstone`, `timestamp`, `last_change_datetime`");
            stringBuilder.AppendLine($")");
            stringBuilder.Append($"SELECT ");
            stringBuilder.Append(str2.ToString());
            stringBuilder.AppendLine($", NULL, 0, {TimestampValue}, now()");
            stringBuilder.AppendLine($"FROM {tableName.Schema().Quoted().ToString()} as `base` WHERE NOT EXISTS");
            stringBuilder.Append($"(SELECT ");
            stringBuilder.Append(str3.ToString());
            stringBuilder.AppendLine($" FROM {trackingName.Schema().Quoted().ToString()} as `side` ");
            stringBuilder.AppendLine($"WHERE {str4})");

            var r = stringBuilder.ToString();

            return(r);
        }
        public DbCommand CreateUpdateTriggerCommand(DbConnection connection, DbTransaction transaction)
        {
            var           updTriggerName = string.Format(this.mySqlObjectNames.GetTriggerCommandName(DbTriggerType.Update), tableName.Unquoted().Normalized().ToString());
            StringBuilder createTrigger  = new StringBuilder();

            createTrigger.AppendLine($"CREATE TRIGGER {updTriggerName} AFTER UPDATE ON {tableName.Quoted().ToString()} FOR EACH ROW ");
            createTrigger.AppendLine();
            createTrigger.AppendLine();
            createTrigger.AppendLine($"Begin ");
            createTrigger.AppendLine($"\tUPDATE {trackingName.Quoted().ToString()} ");
            createTrigger.AppendLine("\tSET `update_scope_id` = NULL ");
            createTrigger.AppendLine($"\t\t,`timestamp` = {this.timestampValue}");
            createTrigger.AppendLine("\t\t,`last_change_datetime` = utc_timestamp()");

            createTrigger.Append($"\tWhere ");
            createTrigger.Append(MySqlManagementUtils.JoinTwoTablesOnClause(this.tableDescription.GetPrimaryKeysColumns(), trackingName.Quoted().ToString(), "new"));

            //if (this.tableDescription.GetMutableColumns().Count() > 0)
            //{
            //    createTrigger.AppendLine();
            //    createTrigger.AppendLine("\t AND (");
            //    string or = "    ";
            //    foreach (var column in this.tableDescription.GetMutableColumns())
            //    {
            //        var quotedColumn = ParserName.Parse(column, "`").Quoted().ToString();

            //        createTrigger.Append("\t");
            //        createTrigger.Append(or);
            //        createTrigger.Append("IFNULL(");
            //        createTrigger.Append("NULLIF(");
            //        createTrigger.Append("`old`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(", ");
            //        createTrigger.Append("`new`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(")");
            //        createTrigger.Append(", ");
            //        createTrigger.Append("NULLIF(");
            //        createTrigger.Append("`new`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(", ");
            //        createTrigger.Append("`old`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(")");
            //        createTrigger.AppendLine(") IS NOT NULL");

            //        or = " OR ";
            //    }
            //    createTrigger.AppendLine("\t ) ");
            //}
            createTrigger.AppendLine($"; ");

            createTrigger.AppendLine("IF (SELECT ROW_COUNT() = 0) THEN ");

            createTrigger.AppendLine($"\tINSERT INTO {trackingName.Quoted().ToString()} (");

            StringBuilder stringBuilderArguments  = new StringBuilder();
            StringBuilder stringBuilderArguments2 = new StringBuilder();
            StringBuilder stringPkAreNull         = new StringBuilder();

            string argComma = string.Empty;
            string argAnd   = string.Empty;

            foreach (var mutableColumn in this.tableDescription.GetPrimaryKeysColumns().Where(c => !c.IsReadOnly))
            {
                var columnName = ParserName.Parse(mutableColumn, "`").Quoted().ToString();

                stringBuilderArguments.AppendLine($"\t\t{argComma}{columnName}");
                stringBuilderArguments2.AppendLine($"\t\t{argComma}new.{columnName}");
                stringPkAreNull.Append($"{argAnd}{trackingName.Quoted().ToString()}.{columnName} IS NULL");
                argComma = ",";
                argAnd   = " AND ";
            }

            createTrigger.Append(stringBuilderArguments.ToString());
            createTrigger.AppendLine("\t\t,`update_scope_id`");
            createTrigger.AppendLine("\t\t,`timestamp`");
            createTrigger.AppendLine("\t\t,`sync_row_is_tombstone`");
            createTrigger.AppendLine("\t\t,`last_change_datetime`");

            var filterColumnsString  = new StringBuilder();
            var filterColumnsString2 = new StringBuilder();
            var filterColumnsString3 = new StringBuilder();

            createTrigger.AppendLine("\t) ");
            createTrigger.AppendLine("\tSELECT ");
            createTrigger.Append(stringBuilderArguments2.ToString());
            createTrigger.AppendLine("\t\t,NULL");
            createTrigger.AppendLine($"\t\t,{this.timestampValue}");
            createTrigger.AppendLine("\t\t,0");
            createTrigger.AppendLine("\t\t,utc_timestamp()");
            createTrigger.AppendLine($"\tFROM {tableName.Quoted().ToString()}"); //[AB] added to be compatible with MariaDB 10.3.x

            //if (this.tableDescription.GetMutableColumns().Count() > 0)
            //{
            //    createTrigger.AppendLine();
            //    createTrigger.AppendLine("\t WHERE (");
            //    string or = "    ";
            //    foreach (var column in this.tableDescription.GetMutableColumns())
            //    {
            //        var quotedColumn = ParserName.Parse(column, "`").Quoted().ToString();

            //        createTrigger.Append("\t");
            //        createTrigger.Append(or);
            //        createTrigger.Append("IFNULL(");
            //        createTrigger.Append("NULLIF(");
            //        createTrigger.Append("`old`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(", ");
            //        createTrigger.Append("`new`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(")");
            //        createTrigger.Append(", ");
            //        createTrigger.Append("NULLIF(");
            //        createTrigger.Append("`new`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(", ");
            //        createTrigger.Append("`old`.");
            //        createTrigger.Append(quotedColumn);
            //        createTrigger.Append(")");
            //        createTrigger.AppendLine(") IS NOT NULL");

            //        or = " OR ";
            //    }
            //    createTrigger.AppendLine("\t ) ");
            //}
            createTrigger.AppendLine("ON DUPLICATE KEY UPDATE");
            createTrigger.AppendLine("\t`update_scope_id` = NULL, ");
            createTrigger.AppendLine("\t`sync_row_is_tombstone` = 0, ");
            createTrigger.AppendLine($"\t`timestamp` = {this.timestampValue}, ");
            createTrigger.AppendLine("\t`last_change_datetime` = utc_timestamp()");


            createTrigger.AppendLine(";");

            createTrigger.AppendLine("END IF;");

            createTrigger.AppendLine($"End; ");

            var command = connection.CreateCommand();

            command.Connection  = connection;
            command.Transaction = transaction;
            command.CommandText = createTrigger.ToString();

            return(command);
        }