purcell / sqlint

Simple SQL linter supporting ANSI and PostgreSQL syntaxes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Syntax error on postgresql backslash command

raine opened this issue · comments

A book on PostgreSQL contains the following example code where the backslash command is causing sqlint to fail. I'm not experienced enough in SQL to say if this error is appropriate or not but PostgreSQL runs it fine.

BEGIN;


CREATE TABLE factbook (
  year int,
  date date,
  shares text,
  trades text,
  dollars text
);

\copy factbook FROM 'factbook.csv' WITH delimiter '\t' null ''

ALTER TABLE factbook
  ALTER shares
   TYPE bigint
  USING replace(shares, ',', '')::bigint,

  ALTER trades
   TYPE bigint
  USING replace(shares, ',', '')::bigint,

  ALTER dollars
   TYPE bigint
  USING substring(replace(dollars, ',', '') from 2)::numeric;

COMMIT;
12:1:ERROR syntax error at or near "\"

\copy and other commands beginning \ are internal commands available inside psql but are not proper SQL commands. In other words, psql understands them and does something in response to them, but those commands would not be valid if passed directly to the PostgreSQL server.

The behaviour here is therefore correct, in my view -- I wouldn't want to add a special case for the backslash commands, then, given that they couldn't be executed via a PostgreSQL client library.