Skip to content

Conversation

soroushj
Copy link
Contributor

@soroushj soroushj commented Feb 18, 2020

Multi-line SQL comments broke the code generator. This PR fixes the issue.

The case: Consider this SQL schema:

CREATE TABLE authors (
  id   bigserial PRIMARY KEY,
  name text      NOT NULL
);

COMMENT ON TABLE authors IS
'Keeps authors.
Contains id and name.';

Previous behavior: The code generator (sqlc generate) exited with an error:

...
// Keeps authors.
Contains id and name.
type Author struct {
  ID int64 
  Name string 
}


# package db
error generating code: source error: 17:1: expected declaration, found Contains

The reason is obvious: The code generator adds // only at the beginning of the comment string. As a result, only the first line of the comment string is commented out.

New behavior: Code generator adds // at the beginning of every line of the comment string. For the aforementioned schema, the models.go file will be generated as:

...
// Keeps authors.
// Contains id and name.
type Author struct {
	ID   int64
	Name string
}

The fix:

  • Implemented for both Go and Kotlin code generators.
  • Works correctly for both Unix and Windows newlines (LF and CRLF).
  • The changed files are go fmt'd using go1.13.4 linux/amd64.

@soroushj soroushj requested a review from kyleconroy February 18, 2020 18:46
Copy link
Collaborator

@kyleconroy kyleconroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for code

@kyleconroy kyleconroy merged commit 2271061 into sqlc-dev:master Feb 18, 2020
@soroushj soroushj deleted the support-sql-multiline-comment branch February 18, 2020 18:58
@soroushj soroushj mentioned this pull request Mar 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants