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

database/sql: number of scan-destinations won't work with alter table. #12693

Closed
mattn opened this issue Sep 19, 2015 · 2 comments
Closed

database/sql: number of scan-destinations won't work with alter table. #12693

mattn opened this issue Sep 19, 2015 · 2 comments

Comments

@mattn
Copy link
Member

@mattn mattn commented Sep 19, 2015

See mattn/go-sqlite3#240

// +build ignore
package main

import (
    "database/sql"
    "fmt"

    _ "github.com/mattn/go-sqlite3"
)

func main() {
    db, err := sql.Open("sqlite3", "file:dummy.db?mode=memory&cache=shared")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    _, err = db.Exec(`CREATE TABLE test (id INTEGER PRIMARY KEY NOT NULL, name TEXT);
                        INSERT INTO test (name) VALUES ('Bart'), ('Lisa');`)
    if err != nil {
        panic(err)
    }

    // A prepared statement that have a long lifetime...
        stmt, err := db.Prepare("SELECT * FROM test")
    if err != nil {
        panic(err)
    }
    defer stmt.Close()

    rows, err := stmt.Query()
    if err != nil {
        panic(err)
    }
    defer rows.Close()

    // An Alter, may be done by another connection/process (if the db is not in memory)...
    _, err = db.Exec("ALTER TABLE test ADD COLUMN data DEFAULT 'missing'")
    if err != nil {
        panic(err)
    }

    var id int
    var name string
    var data string
    // The native sqlite3_stmt is recompiled when the first sqlite3_step is called:
    // http://sqlite.org/c3ref/prepare.html
    // but not the Go wrappers...
    for rows.Next() {
        err = rows.Scan(&id, &name, &data)
        if err != nil {
            panic(err)
        }
        fmt.Println(id, name, data)
    }
}

When modifying columns after calling Query like above, go doesn't handle number of columns modified. Because database/sql looks number of columns with count of last-fetched columns..

https://github.com/golang/go/blob/master/src/database/sql/sql.go#L1689

@kardianos
Copy link
Contributor

@kardianos kardianos commented Oct 9, 2016

@mattn I don't think this is a database/sql bug, unless you know what should be changed. It looks like go-sqlite should update *SQLiteRows.nc after an alter statement, which should return a new len(Column()) size.

While I could be mistaken here, it isn't the sql package caching something (the cache happens on the first Next()), it is the sqlite package that isn't returning the correct column number.

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 21, 2017

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please reopen if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this Mar 21, 2017
@golang golang locked and limited conversation to collaborators Mar 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.