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

CAST as char in mysql not working as expected #3695

Open
optimuspaul opened this issue Nov 8, 2024 · 0 comments
Open

CAST as char in mysql not working as expected #3695

optimuspaul opened this issue Nov 8, 2024 · 0 comments
Labels
📚 mysql bug Something isn't working 🔧 golang

Comments

@optimuspaul
Copy link

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

@optimuspaul optimuspaul added the bug Something isn't working label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 mysql bug Something isn't working 🔧 golang
Projects
None yet
Development

No branches or pull requests

1 participant