Skip to content

Commit

Permalink
feat: add ability to alias identifier list members
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Feb 28, 2019
1 parent 5771710 commit 0e2c475
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
33 changes: 32 additions & 1 deletion .README/VALUE_PLACEHOLDERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,38 @@ Produces:

```js
{
sql: 'SELECT 1 FROM "bar"."bar", "qux"."quux"',
sql: 'SELECT 1 FROM "bar"."baz", "qux"."quux"',
values: []
}

```

#### Identifier aliases

A member of the identifier list can be aliased:

```js
sql`
SELECT 1
FROM ${sql.identifierList([
{
alias: 'qux',
identifier: ['bar', 'baz']
},
{
alias: 'corge',
identifier: ['quux', 'quuz']
}
])}
`;

```

Produces:

```js
{
sql: 'SELECT 1 FROM "bar"."baz" "qux", "quux"."quuz" "corge"',
values: []
}

Expand Down
19 changes: 16 additions & 3 deletions src/sqlFragmentFactories/createIdentifierListSqlFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@ import {

export default (token: IdentifierListTokenType): SqlFragmentType => {
const sql = token.identifiers
.map((names) => {
return names
.map((identifier) => {
if (Array.isArray(identifier)) {
return identifier
.map((identifierName) => {
if (typeof identifierName !== 'string') {
throw new TypeError('Identifier name must be a string.');
}

return escapeIdentifier(identifierName);
})
.join('.');
}

return identifier
.identifier
.map((identifierName) => {
if (typeof identifierName !== 'string') {
throw new TypeError('Identifier name must be a string.');
}

return escapeIdentifier(identifierName);
})
.join('.');
.join('.') + ' ' + escapeIdentifier(identifier.alias);
})
.join(', ');

Expand Down
8 changes: 7 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ export type IdentifierTokenType = {|
+type: typeof IdentifierTokenSymbol
|};

type IdentifierListMemberType = $ReadOnlyArray<string> |
{|
+alias: string,
+identifier: $ReadOnlyArray<string>
|};

export type IdentifierListTokenType = {|
+identifiers: $ReadOnlyArray<$ReadOnlyArray<string>>,
+identifiers: $ReadOnlyArray<IdentifierListMemberType>,
+type: typeof IdentifierListTokenSymbol
|};

Expand Down
25 changes: 23 additions & 2 deletions test/slonik/templateTags/sql/identifierList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
SqlTokenSymbol
} from '../../../../src/symbols';

test('creates an object describing a query with inlined identifiers', (t) => {
test('creates an object describing a query with identifiers', (t) => {
const query = sql`SELECT ${'foo'} FROM ${sql.identifierList([['bar'], ['baz']])}`;

t.deepEqual(query, {
Expand All @@ -18,7 +18,28 @@ test('creates an object describing a query with inlined identifiers', (t) => {
});
});

test('creates an object describing a query with inlined identifiers (specifier)', (t) => {
test('creates an object describing a query with aliased identifiers', (t) => {
const query = sql`SELECT ${'foo'} FROM ${sql.identifierList([
{
alias: 'baz',
identifier: ['bar']
},
{
alias: 'quux',
identifier: ['qux']
}
])}`;

t.deepEqual(query, {
sql: 'SELECT $1 FROM "bar" "baz", "qux" "quux"',
type: SqlTokenSymbol,
values: [
'foo'
]
});
});

test('creates an object describing a query with identifiers (specifier)', (t) => {
const query = sql`SELECT ${'foo'} FROM ${sql.identifierList([
['bar', 'baz'],
['qux', 'quux']
Expand Down

0 comments on commit 0e2c475

Please sign in to comment.