sqlc-dev / sqlc-gen-python

MIT License
112 stars 14 forks source link

feat: sqlc.slice macro (mysql) #28

Open zztkm opened 10 months ago

zztkm commented 10 months ago

Since I am using mysql, I would like to generate code that accepts multiple values using sqlc.slice.

Example

schema & query

create table students (
  id int not null auto_increment,
  name varchar(255) not null,
  age int not null
);

-- name: SelectStudents :many
SELECT * FROM students 
WHERE age IN (sqlc.slice("ages"));

current results:

class Querier:
    def __init__(self, conn: sqlalchemy.engine.Connection):
        self._conn = conn

    def select_students(self, *, ages: int) -> Iterator[models.Student]:
        result = self._conn.execute(sqlalchemy.text(SELECT_STUDENTS), {"p1": ages})
        for row in result:
            yield models.Student(
                id=row[0],
                name=row[1],
                age=row[2],
            )

i want:

class Querier:
    def __init__(self, conn: sqlalchemy.engine.Connection):
        self._conn = conn

    def select_students(self, *, ages: list[int]) -> Iterator[models.Student]:
         # some kind of processing
zztkm commented 10 months ago

I have no idea how to implement it, but since Go supports it, I guess there is a way to do it.