sqlc-dev / sqlc

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

CAST as char in mysql not working as expected #3695

Open optimuspaul opened 1 week ago

optimuspaul commented 1 week ago

Version

1.27.0

What happened?

unable to get a arg to cast to the correct type in the generated go code. I have CAST(sqlc.arg(sortBy) AS char) in my code and I could expect then that the sortBy arg would be typed as a string or sql.NullString but instead it is interface{}

Relevant log output

No response

Database schema

CREATE TABLE `user_master` (
    `userId` int NOT NULL AUTO_INCREMENT,
    `firstName` varchar(50) NULL,
    `password` varchar(45) NULL,
    `email` varchar(200) NULL,
    `middleName` varchar(50) NULL,
    `lastName` varchar(50) NULL,
    `phoneNumber` varchar(20) NULL,
    `address1` varchar(500) NULL,
    `address2` varchar(500) NULL,
    `state` varchar(50) NULL,
    `city` varchar(50) NULL,
    `zip` varchar(10) NULL,
    `roleId` int NOT NULL,
    `trainingDate` date NULL,
    `createdOn` datetime NULL,
    `active` int NOT NULL,
    `userStatus` tinyint NOT NULL,
    `createdBy` int NULL,
    `modifiedBy` int NULL,
    `modifiedOn` datetime NULL,
    `accessCode` varchar(100) NULL,
    `acceptedDate` date NULL,
    `organizationId` int NULL DEFAULT 0,
    `organization` varchar(128) NULL,
    `userAcessStatus` int NULL,
    `country` varchar(255) NULL,
    `is_user_changed_password` bool NULL,
    `is_user_deleted` bool NULL,
    `lang` varchar(255) NULL,
    PRIMARY KEY (`userId`)
) CHARSET utf8mb3 COLLATE utf8mb3_general_ci AUTO_INCREMENT 2038;

SQL queries

select * from user_master where roleId = 2 and userStatus = 2 and is_user_deleted = 0 and userId <> coalesce(sqlc.arg(activeUser), 0) and active = 1 ORDER BY
CASE CAST(sqlc.arg(sortBy) AS char)
    WHEN 'name' THEN concat(' ', firstName, lastName)
    WHEN 'latest' THEN -createdOn
    ELSE createdOn
END;

Configuration

version: "2"
sql:
- engine: "mysql"
  queries: "internal/observations/repository/queries/*.sql"
  schema: "internal/observations/migrations/schema.sql"
  database:
      uri: "REDACTED"
  gen:
    go:
      emit_prepared_queries: true
      emit_pointers_for_null_types: true
      package: "repository"
      out: "internal/observations/repository"

Playground URL

https://play.sqlc.dev/p/68970c6306298bc080bde9782d36c40c18e6f54010c18a41aa1271a9b34eab06

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go