Skip to content

Commit

Permalink
Merge pull request #105 from jdeniau/rows-as-array
Browse files Browse the repository at this point in the history
use rowsAsArray for SQL raw queries
  • Loading branch information
jdeniau authored Jul 2, 2024
2 parents 7fe631d + 282df6e commit 1030366
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/preload/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { bindChannel, bindEvent } from './bindChannel';
import { SQL_CHANNEL } from './sqlChannel';

interface Sql {
executeQuery<T extends QueryReturnType>(query: string): QueryResult<T>;
executeQuery<T extends QueryReturnType>(
query: string,
rowsAsArray?: boolean
): QueryResult<T>;
closeAllConnections(): Promise<void>;
connectionNameChanged(
connectionSlug: string | undefined,
Expand All @@ -37,8 +40,8 @@ async function doInvokeQuery(sqlChannel: SQL_CHANNEL, ...params: unknown[]) {
}

export const sql: Sql = {
executeQuery: async (query) =>
doInvokeQuery(SQL_CHANNEL.EXECUTE_QUERY, query),
executeQuery: async (query, rowsAsArray) =>
doInvokeQuery(SQL_CHANNEL.EXECUTE_QUERY, query, rowsAsArray),

getKeyColumnUsage: async (tableName) =>
doInvokeQuery(SQL_CHANNEL.GET_KEY_COLUMN_USAGE, tableName),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import TableGrid from '../../TableGrid';
import SqlErrorComponent from '../SqlErrorComponent';

type Props = {
rowsAsArray: boolean;
fetcher: Fetcher<
| {
result: Awaited<QueryResult>;
Expand All @@ -24,7 +25,7 @@ type Props = {
>;
};

export default function RawSqlResult({ fetcher }: Props) {
export default function RawSqlResult({ fetcher, rowsAsArray = false }: Props) {
const { t } = useTranslation();
const { data, state } = fetcher;

Expand All @@ -51,6 +52,7 @@ export default function RawSqlResult({ fetcher }: Props) {
<TableGrid
result={result[0]}
fields={result[1]}
rowsAsArray={rowsAsArray}
title={() => t('rawSql.result.title')}
/>
)}
Expand Down
6 changes: 4 additions & 2 deletions src/renderer/component/TableGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ForeignKeyLink from './ForeignKeyLink';
import { useTableHeight } from './TableLayout/useTableHeight';

interface TableGridProps<R extends RowDataPacket> {
rowsAsArray?: boolean;
result: null | R[];
fields: null | FieldPacket[];
primaryKeys?: Array<string>;
Expand All @@ -17,6 +18,7 @@ function TableGrid<Row extends RowDataPacket>({
result,
primaryKeys,
title,
rowsAsArray = false,
}: TableGridProps<Row>): ReactElement {
const [yTableScroll, resizeRef] = useTableHeight();

Expand All @@ -26,9 +28,9 @@ function TableGrid<Row extends RowDataPacket>({
title={title}
bordered
// the header, contains the column names
columns={fields?.map((field) => ({
columns={fields?.map((field, i) => ({
title: field.name,
dataIndex: field.name,
dataIndex: rowsAsArray ? i : field.name,
key: field.name,

// add "…" to the end of the cell if the content is too long
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/routes/sql.$connectionSlug.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function action({

try {
await window.sql.executeQuery(`USE ${databaseName};`);
const result = await window.sql.executeQuery(query);
const result = await window.sql.executeQuery(query, true);

return { result };
} catch (error) {
Expand Down Expand Up @@ -87,7 +87,7 @@ export default function SqlPage() {
</Button>
</Form>

<RawSqlResult fetcher={fetcher} />
<RawSqlResult fetcher={fetcher} rowsAsArray />
</Flex>
);
}
23 changes: 18 additions & 5 deletions src/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,17 @@ class ConnectionStack {
}

async executeQueryAndRetry<T extends QueryReturnType = QueryReturnType>(
query: string
query: string,
rowsAsArray = false
): QueryResultOrError<T> {
invariant(this.#currentConnectionSlug, 'Connection slug is required');

try {
return this.executeQuery<T>(this.#currentConnectionSlug, query);
return this.executeQuery<T>(
this.#currentConnectionSlug,
query,
rowsAsArray
);
} catch (error) {
const message = error instanceof Error ? error.message : error;

Expand All @@ -139,7 +144,11 @@ class ConnectionStack {
// retry once
this.#connections.delete(this.#currentConnectionSlug);

return this.executeQuery<T>(this.#currentConnectionSlug, query);
return this.executeQuery<T>(
this.#currentConnectionSlug,
query,
rowsAsArray
);
}

throw error;
Expand All @@ -148,14 +157,18 @@ class ConnectionStack {

async executeQuery<T extends QueryReturnType = QueryReturnType>(
connectionSlug: string,
query: string
query: string,
rowsAsArray = false
): QueryResultOrError<T> {
const connection = await this.#getConnection(connectionSlug);

log.debug(`Execute query on "${connectionSlug}": "${query}"`);

try {
return { result: await connection.query(query), error: undefined };
return {
result: await connection.query({ sql: query, rowsAsArray }),
error: undefined,
};
} catch (error) {
return { result: undefined, error: encodeError(error) };
}
Expand Down

0 comments on commit 1030366

Please sign in to comment.