Skip to content

Commit

Permalink
Allow multiline SQL queries (copy paste not working however)
Browse files Browse the repository at this point in the history
  • Loading branch information
tammam-g committed Oct 4, 2024
1 parent 2c3288d commit 5dc5212
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/commands/dataconnect-sql-shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function executeQuery(query: string, conn: pg.PoolClient) {
} else {
// If nothing is returned and the query was select, let the user know there was no results.
if (query.toUpperCase().includes('SELECT'))
logger.info(chalk.yellow('No results returned'));
logger.info(chalk.yellow('No results returned'));
}
} catch (err) {
spinner.fail(chalk.red(`Failed executing query: ${err}`));
Expand All @@ -51,19 +51,32 @@ async function executeQuery(query: string, conn: pg.PoolClient) {
const sqlKeywords = ['SELECT', 'FROM', 'WHERE', 'INSERT', 'UPDATE', 'DELETE', 'JOIN', 'GROUP BY', 'ORDER BY', 'LIMIT', 'GRANT', 'CREATE', 'DROP'];

async function promptForQuery(): Promise<string> {
const question: Question = {
type: 'input',
name: 'query',
message: 'Enter your SQL query (or "exit" to quit):',
transformer: (input: string) => {
// Highlight SQL keywords
return input.split(' ').map(word =>
sqlKeywords.includes(word.toUpperCase()) ? chalk.cyan(word) : word
).join(' ');
let query = ''
let line = ''

do {
const question: Question = {
type: 'input',
name: 'line',
message: query ? '> ' : 'Enter your SQL query (or ".exit"):',
transformer: (input: string) => {
// Highlight SQL keywords
return input.split(' ').map(word =>
sqlKeywords.includes(word.toUpperCase()) ? chalk.cyan(word) : word
).join(' ');
}
};

({ line } = await prompt({ nonInteractive: false }, [question]));

line = line.trimEnd()

if (line.toLowerCase() === '.exit') {
return '.exit';
}
};

const { query } = await prompt({ nonInteractive: false }, [question]);
query += (query ? '\n' : '') + line;
} while (line !== '' && !query.endsWith(';'));
return query;
}

Expand Down Expand Up @@ -93,7 +106,7 @@ export const command = new Command("dataconnect:sql:shell [serviceId]")

const { serviceName, instanceId, instanceName, databaseId } = getIdentifiers(serviceInfo.schema);

const { user:username, mode } = await getIAMUser(options);
const { user: username, mode } = await getIAMUser(options);

const instance = await cloudSqlAdminClient.getInstance(projectId, instanceId);

Expand Down Expand Up @@ -123,12 +136,12 @@ export const command = new Command("dataconnect:sql:shell [serviceId]")


logger.info(chalk.cyan('Welcome to the GCP SQL Shell'));
logger.info(chalk.gray('Type your SQL queries or "exit" to quit.\n'));
logger.info(chalk.gray('Type your your SQL query or ".exit" to quit, queries should end with \';\' or add empty line to execute.'));



while (true) {
const query = await promptForQuery();
if (query.toLowerCase() === 'exit') {
if (query.toLowerCase() === '.exit') {
break;
}

Expand Down

0 comments on commit 5dc5212

Please sign in to comment.