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

Export (Transaction).tx *sqlx.Tx field #28

Merged
merged 1 commit into from
Oct 19, 2014
Merged

Export (Transaction).tx *sqlx.Tx field #28

merged 1 commit into from
Oct 19, 2014

Conversation

sqs
Copy link
Contributor

@sqs sqs commented Oct 19, 2014

This PR exports the (Transaction).tx *sqlx.Tx field.

We use an internal fork of modl at @sourcegraph with this field exported. It makes it easier to add additional functionality to SqlExecutor that makes use of sqlx and database/sql methods/helpers. Here 2 examples from our codebase where exporting this field has been necessary/helpful. I'm just mentioning these because this is essentially an API design decision and real world use cases are often helpful to me in these situations.

  1. We have a Copy function that takes an sqlx.Tx (and calls .Prepare on it), but internally all of our DB handles are modl.SqlExecutor:
// Copy executes a PostgreSQL COPY statement. All of the rows must have the same
// data type, which must be mapped to a table using modl.
func Copy(tx *sqlx.Tx, tableName string, rows interface{}, omitCols ...int) error {
   ...
}

In order to be able to call Copy on a modl.SqlExecutor or *modl.Transaction, the tx field must be exported. Then we can call, e.g., db.Copy(dbh.(*modl.Transaction).Tx, "mytable", data).

  1. We have helper functions for selecting a single SQL result into a variable, which accepts a modl.SqlExecutor as an argument and wants to be able to call QueryRow on the DBMap or Transaction:
func selectVal(e modl.SqlExecutor, v interface{}, query string, args ...interface{}) error {
    e = getUnderlyingSQLExecutor(e)

    var row *sql.Row
    switch e := e.(type) {
    case *modl.DbMap:
        row = e.Dbx.QueryRow(query, args...)
    case *modl.Transaction:
        row = e.Tx.QueryRow(query, args...)
    default:
        panic("selectVal: unknown type: " + reflect.TypeOf(e).String())
    }

    if row != nil {
        err := row.Scan(v)
        if err != nil {
            return err
        }
    }

    return nil
}

The tx field must be exported in order to be able to use sqlx's QueryRow on a *modl.Transaction. Note that the analogous Dbx field on DbMap is exported, so it would seem consistent for Tx to also be exported.

jmoiron added a commit that referenced this pull request Oct 19, 2014
Export (Transaction).tx *sqlx.Tx field
@jmoiron jmoiron merged commit ac37e9a into jmoiron:master Oct 19, 2014
@jmoiron
Copy link
Owner

jmoiron commented Oct 19, 2014

This makes the tracing layer a little easier to defeat, but I agree that it mirrors the exported Db/Dbx field on the DbMap.

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

Successfully merging this pull request may close these issues.

None yet

2 participants