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

Select all columns but a few ones #1700

Merged
merged 8 commits into from
Jan 19, 2025
Merged

Conversation

groue
Copy link
Owner

@groue groue commented Jan 19, 2025

This pull request makes it possible to select all columns but a set of excluded columns.

It makes it easier to define record types that do not select all columns. For example:

try db.create(table: "player") { t in
    t.autoIncrementedPrimaryKey("id")
    t.column("score", .integer).notNull()
    t.column("bonus", .integer).notNull()
    t.column("totalScore", .integer).generatedAs(sql: "score + bonus")
}

struct Player: Codable {
    // No property for `totalScore`
    var id: Int64
    var score: Int
    var bonus: Int
}

extension Player: FetchableRecord, PersistableRecord {
    // Don't select totalScore
    static var databaseSelection: [any SQLSelectable] {
        [.allColumns(excluding: ["totalScore"])] // NEW!
    }
}

// SELECT id, score, bonus FROM player
let players = try dbQueue.read { db in
    try Player.fetchAll(db)
}

@groue groue force-pushed the dev/all-columns-excluding branch from 5cb459b to 37ea21e Compare January 19, 2025 15:27
@groue groue merged commit 3f56bc8 into development Jan 19, 2025
8 checks passed
@groue groue deleted the dev/all-columns-excluding branch January 19, 2025 15:50
@ASISBusiness
Copy link

This pull request makes it possible to select all columns but a set of excluded columns.

It makes it easier to define record types that do not select all columns. For example:

try db.create(table: "player") { t in

    t.autoIncrementedPrimaryKey("id")

    t.column("score", .integer).notNull()

    t.column("bonus", .integer).notNull()

    t.column("totalScore", .integer).generatedAs(sql: "score + bonus")

}



struct Player: Codable {

    // No property for `totalScore`

    var id: Int64

    var score: Int

    var bonus: Int

}



extension Player: FetchableRecord, PersistableRecord {

    // Don't select totalScore

    static var databaseSelection: [any SQLSelectable] {

        [.allColumns(excluding: ["totalScore"])] // NEW!

    }

}



// SELECT id, score, bonus FROM player

let players = try dbQueue.read { db in

    try Player.fetchAll(db)

}

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

Successfully merging this pull request may close these issues.

2 participants