Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for returning #1221

Open
mfbx9da4 opened this issue Jul 24, 2023 · 1 comment
Open

Support for returning #1221

mfbx9da4 opened this issue Jul 24, 2023 · 1 comment

Comments

@mfbx9da4
Copy link

mfbx9da4 commented Jul 24, 2023

As far as I can tell there is no way to return anything other than the rowId inserted from an UPDATE or INSERT statement. That would be useful in my use case. eg

let alice = users.filter(id == 1)
let row = try db.run(alice.update(email <- "alice@me.com").returning(users[*]))
@dmitrygusev
Copy link

FYI here's how it can be solved for a simple case:

extension Insert {
    func returning(_ columns: [Expression<Int64>]) -> Insert {
        var template = self.template
        let bindings = self.bindings
        
        template.append(" RETURNING ")
        template.append(columns.map { column in column.expression.template }.joined(separator: ", "))
        
        let insert = Insert(template, bindings)
        
        return insert
    }
}

...
let insert = table1.insert(...).returning([col1, col2])

if let results = try? db().prepareRowIterator(insert.template, bindings: insert.bindings) {
   // handle results as usual
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants