Skip to content

Commit 169c8a3

Browse files
authored
Merge pull request #5102 from commonspider/4982_replace_number
Replace Number() with a util function for casting column ids from string to number
2 parents 9edf9f7 + a3a676d commit 169c8a3

File tree

10 files changed

+35
-11
lines changed

10 files changed

+35
-11
lines changed

mathesar_ui/src/component-library/common/utils/typeUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export function hasStringProperty<PropertyName extends string>(
2222
return hasProperty(object, property) && typeof object[property] === 'string';
2323
}
2424

25+
export function hasNumberProperty<PropertyName extends string>(
26+
object: unknown,
27+
property: PropertyName,
28+
): object is { [k in PropertyName]: number } {
29+
return hasProperty(object, property) && typeof object[property] === 'number';
30+
}
31+
2532
export function hasMethod<MethodName extends string>(
2633
object: unknown,
2734
method: MethodName,

mathesar_ui/src/stores/table-data/filtering.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
RawFilterGroup,
1414
RawIndividualFilter,
1515
} from '@mathesar/components/filter/utils';
16+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
1617

1718
import type { FilterId } from '../abstract-types/types';
1819

@@ -45,7 +46,7 @@ function individualFilterToSqlExpr(
4546
): SqlExpr {
4647
const column: SqlColumn = {
4748
type: 'attnum',
48-
value: Number(individualFilter.columnId),
49+
value: castColumnIdToNumber(individualFilter.columnId),
4950
};
5051

5152
/** Generate an SqlLiteral value */
@@ -190,7 +191,7 @@ function getCountOfNonConjunctionalExpr(expr: SqlExpr) {
190191
}
191192

192193
function getCountOfColumnInExpr(expr: SqlExpr, columnId: string): number {
193-
if (expr.type === 'attnum' && expr.value === Number(columnId)) {
194+
if (expr.type === 'attnum' && expr.value === castColumnIdToNumber(columnId)) {
194195
return 1;
195196
}
196197
let count = 0;

mathesar_ui/src/stores/table-data/grouping.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { RecordsListParams } from '@mathesar/api/rpc/records';
2+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
23

34
export interface GroupEntry {
45
readonly columnId: string;
@@ -78,7 +79,7 @@ export class Grouping {
7879
}
7980
return {
8081
grouping: {
81-
columns: this.entries.map((e) => Number(e.columnId)),
82+
columns: this.entries.map((e) => castColumnIdToNumber(e.columnId)),
8283
preproc: this.entries.map((e) => e.preprocFnId ?? null),
8384
},
8485
};

mathesar_ui/src/stores/table-data/joinedColumns.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RawColumnWithMetadata } from '@mathesar/api/rpc/columns';
22
import type { JoinPath, JoinableTablesResult } from '@mathesar/api/rpc/tables';
33
import { getCellCap } from '@mathesar/components/cell-fabric/utils';
4+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
45
import type { ComponentAndProps } from '@mathesar-component-library/types';
56

67
import type { Joining } from './joining';
@@ -82,7 +83,7 @@ export class SimpleManyToManyJoinedColumn {
8283

8384
const [attnum, info] = pkColumnEntry;
8485
const joinedColumn = {
85-
id: Number(attnum),
86+
id: castColumnIdToNumber(attnum),
8687
type: '_array',
8788
type_options: {
8889
item_type: info.type,

mathesar_ui/src/stores/table-data/searchFuzzy.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RecordsSearchParams } from '@mathesar/api/rpc/records';
22
import { ImmutableMap } from '@mathesar/component-library';
3+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
34

45
/**
56
* Due to the way that the fuzzy search works, it doesn't make sense to search
@@ -20,7 +21,7 @@ export class SearchFuzzy extends ImmutableMap<string, unknown> {
2021

2122
getSearchParams(): RecordsSearchParams['search_params'] {
2223
return [...this].map(([columnId, value]) => ({
23-
attnum: Number(columnId),
24+
attnum: castColumnIdToNumber(columnId),
2425
literal: value,
2526
}));
2627
}

mathesar_ui/src/stores/table-data/sorting.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
type SortDirection,
88
allowedSortDirections,
99
} from '@mathesar/components/sort-entry/utils';
10+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
1011
import { ImmutableMap } from '@mathesar-component-library';
1112

1213
import type { Grouping } from './grouping';
@@ -48,7 +49,7 @@ export class Sorting extends ImmutableMap<string, SortDirection> {
4849
private recordsRequestParams(): Pick<RecordsListParams, 'order'> {
4950
const sortingEntries: ApiSortingEntry[] = [...this].map(
5051
([columnId, sortDirection]) => ({
51-
attnum: Number(columnId),
52+
attnum: castColumnIdToNumber(columnId),
5253
direction: getApiSortDirection(sortDirection),
5354
}),
5455
);

mathesar_ui/src/stores/table-data/tabularData.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type {
2929
RecordRow,
3030
RecordSummariesForSheet,
3131
} from '@mathesar/stores/table-data';
32+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
3233
import { orderProcessedColumns } from '@mathesar/utils/tables';
3334
import { ImmutableSet, defined } from '@mathesar-component-library';
3435

@@ -172,7 +173,7 @@ export class TabularData {
172173
database: props.database,
173174
table: this.table,
174175
hiddenColumns: Array.from(contextualFilters.keys()).map((id) =>
175-
Number(id),
176+
castColumnIdToNumber(id),
176177
),
177178
});
178179
this.constraintsDataStore = new ConstraintsDataStore({

mathesar_ui/src/systems/data-explorer/result-pane/QueryRunErrors.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import { getExplorationPageUrl } from '@mathesar/routes/urls';
88
import { databasesStore } from '@mathesar/stores/databases';
99
import { currentSchema } from '@mathesar/stores/schemas';
10-
import { Button, hasProperty } from '@mathesar-component-library';
10+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
11+
import { Button, hasNumberProperty } from '@mathesar-component-library';
1112
1213
import QueryManager from '../QueryManager';
1314
import type { QueryRunner } from '../QueryRunner';
@@ -34,8 +35,8 @@
3435
{#if errors instanceof ApiMultiError}
3536
{#each errors.errors as apierror}
3637
<ul>
37-
{#if apierror.code === QUERY_CONTAINS_DELETED_COLUMN && hasProperty(apierror.detail, 'column_id')}
38-
{@const columnId = Number(apierror.detail.column_id)}
38+
{#if apierror.code === QUERY_CONTAINS_DELETED_COLUMN && hasNumberProperty(apierror.detail, 'column_id')}
39+
{@const columnId = castColumnIdToNumber(apierror.detail.column_id)}
3940
<li class="error">
4041
<p class="strong">
4142
{$_('some_columns_in_query_missing')}

mathesar_ui/src/systems/table-view/actions-pane/record-operations/join/joinConfigUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
JoinableTable,
44
JoinableTablesResult,
55
} from '@mathesar/api/rpc/tables';
6+
import { castColumnIdToNumber } from '@mathesar/utils/columnUtils';
67

78
/**
89
* This represents a "simple" many-to-many relationship such that the mapping
@@ -151,7 +152,7 @@ export function getSimpleManyToManyJoinPath(
151152
],
152153
[
153154
[intermediateTableOid, intermediateTableFkToTargetColumn],
154-
[targetTableOid, Number(targetTablePkColumn)],
155+
[targetTableOid, castColumnIdToNumber(targetTablePkColumn)],
155156
],
156157
];
157158
}

mathesar_ui/src/utils/columnUtils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,12 @@ export function columnTypeOptionsAreEqual(
101101
}
102102
return true;
103103
}
104+
105+
export function castColumnIdToNumber(columnId: string | number) {
106+
const numericId = columnId === '' ? NaN : Number(columnId);
107+
if (Number.isNaN(numericId)) {
108+
throw new Error('Invalid columnId');
109+
} else {
110+
return numericId;
111+
}
112+
}

0 commit comments

Comments
 (0)