Skip to content

Example.QueryBuilder.Update

IzayoiJiichan edited this page Aug 14, 2024 · 1 revision

Examples

Update

    {
        var queryOption = new QueryOption();

        var queryBuilder = new QueryBuilder(queryOption);

        var update1 = new Update()
            .SetTable("users")
            .AddSet("age", 21)
            .AddSet("updated_at", DateTime.UtcNow)
            .AddWhere("id", "=", 1);

        queryBuilder.Build(update1);

        string query = queryBuilder.GetQuery();

        var parameters = queryBuilder.GetParameters();

        // query:
        //   UPDATE users
        //   SET age = @s_0, updated_at = @s_1
        //   WHERE id = @w_0
        // parameters:
        //   [0]:
        //     ParameterName: @s_0
        //     DbType: DbType.Int32
        //     Value: 21
        //   [1]:
        //     ParameterName: @s_1
        //     DbType: DbType.DateTime
        //     Value: (2024-08-01 00:00:00)
        //   [2]:
        //     ParameterName: @w_0
        //     DbType: DbType.Int32
        //     Value: 1
    }

    // JOIN for MySQL
    {
        var update2 = new Update()
            .SetTable("posts", "p")
            .AddJoin(JType.LEFT_JOIN, "users", "u", "u.id = p.user_id")
            .AddSet("comment", "comment1")
            .AddWhere("p.id", "=", 1);

        // query:
        //   UPDATE posts AS p
        //   LEFT JOIN users AS u ON (u.id = p.user_id)
        //   SET comment = @s_0
        //   WHERE id = @w_0
        // parameters:
        //   [0]:
        //     ParameterName: @s_0
        //     DbType: DbType.Int32
        //     Value: "comment1"
        //   [1]:
        //     ParameterName: @w_0
        //     DbType: DbType.Int32
        //     Value: 1
    }

    // JOIN for PostgreSQL, SQLite and SQL Server
    {
        var update3 = new Update()
            .SetTable("posts")
            .AddSet("comment", "comment1")
            .SetFrom("posts", "p")
            .AddJoin(JType.LEFT_JOIN, "users", "u", "u.id = p.user_id")
            .AddWhere("p.id", "=", 1);

        // query:
        //   UPDATE posts
        //   SET comment = @s_0
        //   FROM posts AS p
        //   LEFT JOIN users AS u ON (u.id = p.user_id)
        //   WHERE id = @w_0
        // parameters:
        //   [0]:
        //     ParameterName: @s_0
        //     DbType: DbType.String
        //     Value: "comment1"
        //   [1]:
        //     ParameterName: @w_0
        //     DbType: DbType.Int32
        //     Value: 1
    }

    // JOIN for SQL Server
    {
        var update4 = new Update()
            .SetTable("", "p")
            .AddSet("comment", "comment1")
            .SetFrom("posts", "p")
            .AddJoin(JType.LEFT_JOIN, "users", "u", "u.id = p.user_id")
            .AddWhere("p.id", "=", 1);

        // query:
        //   UPDATE p
        //   SET comment = @s_0
        //   FROM posts AS p
        //   LEFT JOIN users AS u ON (u.id = p.user_id)
        //   WHERE id = @w_0
        // parameters:
        //   [0]:
        //     ParameterName: @s_0
        //     DbType: DbType.String
        //     Value: "comment1"
        //   [1]:
        //     ParameterName: @w_0
        //     DbType: DbType.Int32
        //     Value: 1
    }