Skip to content

add toRelLiteral (aka RelSON) #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions examples/execRelLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright 2021 RelationalAI, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

import { Command } from 'commander';

import { Client, readConfig, ResultTable } from '../index.node';

async function run(
database: string,
engine: string,
queryString: string,
readonly: boolean,
tag: string,
profile?: string,
) {
const config = await readConfig(profile);
const client = new Client(config);
const tags = tag ? [tag] : undefined;
const result = await client.exec(
database,
engine,
queryString,
[],
readonly,
tags,
);

const literal =
'{\n' +
result.results.map(rel => new ResultTable(rel).toRelLiteral()).join(';\n') +
'\n}';
console.log(literal);
}

(async () => {
const program = new Command();

const options = program
.requiredOption('-d, --database <type>', 'database name')
.requiredOption('-e, --engine <type>', 'engine name')
.requiredOption('-c, --command <type>', 'rel source string')
.option('-t, --tag <type>', 'tag', '')
.option('-r, --readonly', 'readonly', false)
.option('--poll', 'poll results', false)
.option('-p, --profile <type>', 'profile', 'default')
.parse(process.argv)
.opts();

try {
await run(
options.database,
options.engine,
options.command,
options.readonly,
options.tag,
options.profile,
);
} catch (error: any) {
console.error(error.toString());
}
})();
22 changes: 22 additions & 0 deletions src/results/resultTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,28 @@ export class ResultTable implements IteratorOf<RelTypedValue['value'][]> {
arrow() {
return this.table;
}

/**
* Return a Rel literal representation of this result table.
*
* @returns String
*/
toRelLiteral(): string {
const typeDefs = this.typeDefs();

return this.values()
.map(row => {
const tuple = row
.map((val: RelTypedValue['value'], index: number) => {
const typeDef = typeDefs[index];
return getDisplayValue(typeDef, val, true);
})
.join(', ');

return ` (${tuple})`;
})
.join(';\n');
}
}

function arrowRowToValues(arrowRow: StructRowProxy, colDefs: ColumnDef[]) {
Expand Down
5 changes: 4 additions & 1 deletion src/results/resultUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ export function convertValue<T extends RelTypedValue>(
export function getDisplayValue(
typeDef: RelTypeDef,
value: RelTypedValue['value'],
quoteStrings = false,
Copy link
Author

@vilterp vilterp Aug 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

): string {
const val = {
...typeDef,
Expand All @@ -474,7 +475,9 @@ export function getDisplayValue(

switch (val.type) {
case 'String':
return JSON.stringify(val.value).slice(1, -1);
return quoteStrings
? JSON.stringify(val.value)
: JSON.stringify(val.value).slice(1, -1);
case 'Bool':
return val.value ? 'true' : 'false';
case 'Char':
Expand Down