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

sqlc.narg does not work for sqlite3 #1881

Closed
FrankFang opened this issue Oct 5, 2022 · 1 comment · Fixed by #2274
Closed

sqlc.narg does not work for sqlite3 #1881

FrankFang opened this issue Oct 5, 2022 · 1 comment · Fixed by #2274

Comments

@FrankFang
Copy link

Version

1.15.0

What happened?

-- name: UpdateChapter :one
UPDATE chapters
SET name = coalesce(sqlc.narg('name'), name),
    content = coalesce(sqlc.narg('content'), content)
WHERE id = sqlc.arg('id')
RETURNING *;

It does not work.

Relevant log output

$ sqlc generate
line 26:29 no viable alternative at input ';\n\n-- name: UpdateChapter :one\nUPDATE chapters\nSET name = coalesce(sqlc.narg('
line 26:29 no viable alternative at input 'UPDATE chapters\nSET name = coalesce(sqlc.narg('
# package main
D:\Fang\repos\gobook\db\queries\chapters.sql:1:1: no viable alternative at input 'UPDATE chapters\nSET name = coalesce(sqlc.narg('

Database schema

CREATE TABLE IF NOT EXISTS books (
  id   INTEGER PRIMARY KEY AUTOINCREMENT,
  name text    NOT NULL,
  author text,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS chapters (
  id   INTEGER PRIMARY KEY AUTOINCREMENT,
  name text    NOT NULL,
  book_id INTEGER NOT NULL,
  content text,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

ALTER TABLE books ADD summary text;

ALTER TABLE books ADD deleted_at DATETIME;

CREATE TABLE IF NOT EXISTS chapters (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  book_id INTEGER NOT NULL,
  name TEXT NOT NULL,
  content TEXT NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  deleted_at DATETIME
);

ALTER TABLE chapters
ADD COLUMN parent_id
INTEGER DEFAULT 0
NOT NULL;

ALTER TABLE chapters
ADD COLUMN deleted_at DATETIME;

SQL queries

-- name: ListChapters :many
SELECT * FROM chapters
WHERE book_id = ?
AND deleted_at IS NULL
ORDER BY id;

-- name: CreateChapter :one
INSERT INTO chapters (
  book_id,
  name,
  content
) VALUES (
  ?,
  ?,
  ?
)
RETURNING *;

-- name: DeleteChapter :exec
UPDATE chapters
SET deleted_at = date('now')
WHERE id = ?;

-- name: UpdateChapter :one
UPDATE chapters
SET name = coalesce(sqlc.narg('name'), name),
    content = coalesce(sqlc.narg('content'), content)
WHERE id = sqlc.arg('id')
RETURNING *;

Configuration

version: 2
sql:
  - engine: "sqlite"
    schema:
      - "db/schema.sql"
      - "db/migrations/"
    queries:
      - "db/queries"
    gen:
      go:
        package: "main"
        out: "."
        emit_json_tags: true
        overrides:
          - db_type: "text"
            go_type:
              type: "string"
              pointer: true
            nullable: true
          - db_type: "INTEGER"
            go_type:
              type: "int64"
              pointer: false
            nullable: false
          - db_type: "INTEGER"
            go_type:
              type: "int64"
              pointer: true
            nullable: true
          - db_type: "DATE"
            go_type:
              import: "time"
              type: "Time"
              pointer: true
            nullable: true
          - db_type: "DATETIME"
            go_type:
              import: "time"
              type: "Time"
              pointer: true
            nullable: true
          - db_type: "TIMESTAMP"
            go_type:
              import: "time"
              type: "Time"
              pointer: true
            nullable: true

Playground URL

No response

What operating system are you using?

Windows

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@FrankFang FrankFang added bug Something isn't working triage New issues that hasn't been reviewed labels Oct 5, 2022
@kyleconroy kyleconroy added 📚 sqlite 🔧 golang 💻 windows and removed triage New issues that hasn't been reviewed labels Nov 9, 2022
@aloisbarreras
Copy link

aloisbarreras commented Mar 18, 2023

hey @kyleconroy I would love for sqlc.narg to work with sqlite. Is there anything I can do to help? If you have an idea of what it would take to implement this and can give me some pointers to get started, I can take a stab at a PR.

Edit: I ended up opening a PR to fix this: #2157

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants