SQN

SQN - Short Query Notation is a small library that allows you to write convention based SQL queries using a short notation. SQL is a very flexible and powerful language but because SQL does not rely on conventions you have to specify everything. The SQN-library requires some naming conventions and in return gives you a shorter notation.

Usage:

sqn('shop<product<price');

- left joins shop,
  product and price

sqn('book<<tag');

- doubly left joins book,
  and tag using book_tag

SQN assumes id fields follow the following conventions:

Primary key: id

Foreign key: {table}_id

Link table: {table1}_{table2}
(alphabetical order)

SQN can also generate additional aliases:

sqn( ..., 'area/x,y;place/x,y' )
- area_x area_y place_x place_y

Full example:


sqn('zipcode<area<place',
	'area/x,y;place/x,y');

gives:

SELECT `zipcode`.* ,
 `area`.* ,
 `place`.* ,
 `area`.`x` AS `area_x` ,
 `area`.`y` AS `area_y` ,
 `place`.`x` AS `place_x` ,
 `place`.`y` AS `place_y` 
 FROM `zipcode`
 LEFT JOIN `area` ON
 `area`.zipcode_id = `zipcode`.id 
 LEFT JOIN `place` ON
 `place`.area_id = `area`.id

Syntax

The syntax for the main query (from and joins) is extremely simple. Just add all the tables you wish to join. The first table will be the from-table. Others are joined. The join-symbol tells SQN how to join the table:

<
LEFT JOIN

<<
double LEFT JOIN 
with link table A_B

>
RIGHT JOIN

>>
double RIGHT JOIN
with link table A_B

|
INNER JOIN

|| 
double INNER JOIN
with link table A_B

You can download SQN here