sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.54k stars 809 forks source link

copyfrom does not respect table name casing #3633

Open loissascha opened 1 month ago

loissascha commented 1 month ago

Version

1.27.0

What happened?

I've defined a new query like this:

-- name: BulkInsertAnalyseResult :copyfrom
INSERT INTO AnalyseResults (Id, StoreId, ProductId, AmountDelivery, AmountRemission, AmountBel, AmountGsr, AmountDifference) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?);

and when I create the go code with sqlc generate this is what I get:

func (q *Queries) BulkInsertAnalyseResult(ctx context.Context, arg []BulkInsertAnalyseResultParams) (int64, error) {
    pr, pw := io.Pipe()
    defer pr.Close()
    rh := fmt.Sprintf("BulkInsertAnalyseResult_%d", atomic.AddUint32(&readerHandlerSequenceForBulkInsertAnalyseResult, 1))
    mysql.RegisterReaderHandler(rh, func() io.Reader { return pr })
    defer mysql.DeregisterReaderHandler(rh)
    go convertRowsForBulkInsertAnalyseResult(pw, arg)
    // The string interpolation is necessary because LOAD DATA INFILE requires
    // the file name to be given as a literal string.
    result, err := q.db.ExecContext(ctx, fmt.Sprintf("LOAD DATA LOCAL INFILE '%s' INTO TABLE `analyseresults` %s (id, storeid, productid, amountdelivery, amountremission, amountbel, amountgsr, amountdifference)", "Reader::"+rh, mysqltsv.Escaping))
    if err != nil {
        return 0, err
    }
    return result.RowsAffected()
}

When I run this code, I get an error telling me that the table "analyseresults" does not exist. However when I change it to "AnalyseResults" it works. I believe it should respect the casing I've defined in the original query.

Relevant log output

ERROR: Error 1146 (42S02): Table 'piaNetTest.analyseresults' doesn't exist

Database schema

CREATE TABLE `AnalyseResults` (
  `Id` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
  `StoreId` int(11) NOT NULL,
  `ProductId` int(11) NOT NULL,
  `AmountDelivery` int(11) NOT NULL,
  `AmountRemission` int(11) NOT NULL,
  `AmountBel` int(11) NOT NULL,
  `AmountGsr` int(11) NOT NULL,
  `AmountDifference` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

SQL queries

-- name: BulkInsertAnalyseResult :copyfrom
INSERT INTO AnalyseResults (Id, StoreId, ProductId, AmountDelivery, AmountRemission, AmountBel, AmountGsr, AmountDifference) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?);

Configuration

version: "2"
sql:
  - engine: "mysql"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "piadb"
        sql_package: "database/sql"
        sql_driver: "github.com/go-sql-driver/mysql"
        out: "piadb"

Playground URL

https://play.sqlc.dev/p/464af3d350ad8039e9e1da6ab6fdc64b958cf6b8ed510f560d9932602e47aa8d

What operating system are you using?

Linux

What database engines are you using?

MySQL

What type of code are you generating?

Go