SQLite Column Smoothing

Premise

I recently ran into an expression error selectin my code. It was pretty trivial to fix it when I realized what was going on, but I was interested in finding a way to make sure that such an error would not happen again.

Here is an example of an abusive request:

select
  the,
  quick,
  brown
  fox,
  jumped,
  over,
  the,
  lazy,
  dog
from table_name;

I mean:

select
  the,
  quick,
  brown,
  fox,
  jumped,
  over,
  the,
  lazy,
  dog
from table_name;

For those who do not see this, the comma is missing after brownthe first. This causes the column to be an alias since the keyword ashas no . So what do you get as a result:

  the,
  quick,
  fox,
  jumped,
  over,
  the,
  lazy,
  dog

... brown fox. , (, ), , , :

select
  foo,
  bar,
  baz,
  another_table.quux,
  a1,
  a2,
  a3,
  a4,
  a5,
  a6,
  a7,
  a8,
  a9,
  a10,
  a11,
  a12,
  a13,
  a14,
  a15,
  a16,
  b1,
  b2,
  b3,
  b7,
  b8,
  b9,
  b10,
  b11,
  b12,
  b13,
  b14,
  b18,
  b19,
  b20,
  b21,
  c1,
  c2,
  c3,
  c4,
  c5,
  c6,
  c7,
  c8
from table_name
join another_table on table_name.foo_id = another_table.id
where
  blah = 'blargh'
-- many other things here
;

. b11 (), b11 b12, , ( ). select * from table_name, , .

, .

as ? , ? (, C- 1 == foo foo == 1, , , 1 = foo foo = 1.)

vim, hlsearch, , . , , , - .

!

+3
5

, , - . . -, , . -, , .

Missing:

select
  the
, quick
, brown
  fox
, jumped
, over
, the
, lazy
, dog
from table_name;

:

select
  the
, quick
, brown
, fox
, jumped
, over
, the
, lazy
, dog
from table_name;
+6

SQL , :

  • , ,

  1. SQL, , , , , , .
+1

, . make perl script, "lint" . , . make :

lint_code:
    perl lint_code.pl <file_1.php

perl:

$st = 0;
$line_no = 0;
while (<>)
{
   $line_no++;
   $st = 1 if ( /start-sql/ );
   $st = 0 if ( /end-sql/ );
   $st = 2 if ( $st == 1 && /select/ );
   $st = 3 if ( $st == 2 && /from/ );
   if ( $st == 2 && /^[ \t]+[a-zA-Z][a-zA-Z0-9]*[ \t*]$/ )
   {
      if ( ! /select/ )
      {
         printf ( "Possible Error: Line: $line_no\n" );
      }
   }
}

select //start -sql //end-sql. , . , , SQL , ( ).

/, . , .

( stackoverflow, . , , .)

+1

first
,short
,medium
,longlonglong
,...

first,
short,
medium,
longlonglong,
...

select sql

IDE:)

0

, , . .

And most modern developers use SQL generators or ORM these days instead of writing this "assembly language" SQL.

-1
source

Source: https://habr.com/ru/post/1723369/


All Articles