73 lines
3.8 KiB
JavaScript
73 lines
3.8 KiB
JavaScript
// SQL inside PHP and Markdown (sql-ranges.js). Run: node --test ide/ui/js/sql-ranges.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { sqlRanges, rangeAt, sqlOf, parametersOf, literal, fill } from './sql-ranges.js';
|
|
|
|
const php = `<?php
|
|
// a comment with <<<SQL in it, and ->query('SELECT nothing')
|
|
/* ->query("SELECT 1") <<<SQL */
|
|
$a = <<<SQL
|
|
SELECT * FROM customers WHERE id = {$this->ids['first']} AND name = '{$names[f({$x})]}'
|
|
SQL;
|
|
$b = <<<'SQL'
|
|
SELECT 2;
|
|
SELECT 3
|
|
SQL;
|
|
$c = <<<DQL
|
|
SELECT u FROM App\\Entity\\User u
|
|
DQL;
|
|
$d = <<<HTML
|
|
<p>SELECT</p>
|
|
HTML;
|
|
$rows = $db->query('SELECT name FROM cities WHERE id = ?', [1]);
|
|
$st = $pdo->prepare("UPDATE customers SET name = :name WHERE id = $id");
|
|
$conn->executeStatement('not sql at all');
|
|
echo 'SELECT not a call';
|
|
$x = $connection->fetchAllAssociative('SELECT \\'it\\'\\'s\\' AS t');
|
|
`;
|
|
|
|
test('PHP: SQL heredocs, nowdocs and the strings of SQL calls; comments, DQL, other heredocs and other strings skipped', () => {
|
|
const r = sqlRanges(php, 'php');
|
|
assert.deepEqual(r.map((x) => [x.kind, x.line]), [['heredoc', 4], ['nowdoc', 7], ['string', 17], ['string', 18], ['string', 21]]);
|
|
assert.equal(sqlOf(php, r[0]), "SELECT * FROM customers WHERE id = {$this->ids['first']} AND name = '{$names[f({$x})]}'");
|
|
assert.equal(sqlOf(php, r[1]), 'SELECT 2;\nSELECT 3');
|
|
assert.equal(r[1].interpolates, false);
|
|
assert.equal(sqlOf(php, r[2]), 'SELECT name FROM cities WHERE id = ?');
|
|
assert.equal(sqlOf(php, r[3]), 'UPDATE customers SET name = :name WHERE id = $id');
|
|
assert.equal(sqlOf(php, r[4]), "SELECT 'it''s' AS t");
|
|
const inside = php.indexOf('FROM customers');
|
|
assert.equal(rangeAt(r, inside), r[0]);
|
|
assert.equal(rangeAt(r, php.indexOf('<p>')), null);
|
|
});
|
|
|
|
test('PHP: HTML around the code and ?> are not PHP', () => {
|
|
const t = "<h1><?= $x ?> ->query('SELECT 1')</h1>\n<?php $db->query('SELECT 2');";
|
|
const r = sqlRanges(t, 'php');
|
|
assert.equal(r.length, 1);
|
|
assert.equal(sqlOf(t, r[0]), 'SELECT 2');
|
|
});
|
|
|
|
test('PHP: a heredoc without its end runs to the end of the file; several heredocs one after the other', () => {
|
|
const t = "<?php\n$a = <<<SQL\nSELECT 1\nSQL;\n$b = <<<\"SQL\"\nSELECT 2\nSQL;\n$c = <<<SQL\nSELECT 3";
|
|
const r = sqlRanges(t, 'php');
|
|
assert.deepEqual(r.map((x) => sqlOf(t, x)), ['SELECT 1', 'SELECT 2', 'SELECT 3']);
|
|
assert.deepEqual(r.map((x) => x.line), [2, 5, 8]);
|
|
});
|
|
|
|
test('Markdown: ```sql / ```mysql / ~~~sql blocks, other blocks skipped, a longer fence holds a shorter one', () => {
|
|
const md = 'Text\n\n```sql\nSELECT 1;\n```\n\n```php\n$x = 1;\n```\n\n~~~mysql title="x"\nSHOW TABLES\n~~~\n\n````sql\nSELECT \'```\'\n```\nSELECT 2\n````\n';
|
|
const r = sqlRanges(md, 'markdown');
|
|
assert.deepEqual(r.map((x) => sqlOf(md, x).trim()), ['SELECT 1;', 'SHOW TABLES', "SELECT '```'\n```\nSELECT 2"]);
|
|
assert.deepEqual(r.map((x) => x.line), [3, 11, 15]);
|
|
assert.deepEqual(sqlRanges('```sql\nSELECT unfinished', 'markdown').map((x) => x.to), [24]);
|
|
});
|
|
|
|
test('parameters: PHP variables (nested braces) and placeholders, not inside SQL strings or comments', () => {
|
|
const sql = "SELECT * FROM t WHERE a = {$o->f({$y['k']})} AND b = :name AND c = ? AND d = '?' -- ? :no\nAND e = $id AND f = :name AND g = ? AND h = '{$q}' AND i::text = 1";
|
|
const p = parametersOf(sql, true);
|
|
assert.deepEqual(p.map((x) => x.token), ["{$o->f({$y['k']})}", ':name', '?', '$id', '?', '{$q}']);
|
|
assert.equal(p[1].offsets.length, 2, ':name twice is asked once');
|
|
assert.deepEqual(parametersOf("SELECT '$x', $y", false).map((x) => x.token), []);
|
|
const filled = fill(sql, p.map((x, n) => ({ offsets: x.offsets, sql: literal(['1', 'Ada', '2', '3', 'null', "l'été"][n], false, x.inString) })));
|
|
assert.equal(filled, "SELECT * FROM t WHERE a = 1 AND b = 'Ada' AND c = 2 AND d = '?' -- ? :no\nAND e = 3 AND f = 'Ada' AND g = NULL AND h = 'l\\'été' AND i::text = 1");
|
|
});
|