Skip to content

Commit 11407d4

Browse files
committed
add geographypoint type
1 parent 1988c68 commit 11407d4

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { ColumnBaseConfig } from '~/column';
2+
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder';
3+
import { entityKind } from '~/entity.ts';
4+
import type { AnySingleStoreTable } from '~/singlestore-core/table';
5+
import { sql } from '~/sql/sql.ts';
6+
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';
7+
8+
type GeographyPoint = [number, number];
9+
10+
export type SingleStoreGeographyPointBuilderInitial<TName extends string> = SingleStoreGeographyPointBuilder<{
11+
name: TName;
12+
dataType: 'array';
13+
columnType: 'SingleStoreGeographyPoint';
14+
data: GeographyPoint;
15+
driverParam: string;
16+
enumValues: undefined;
17+
generated: undefined;
18+
}>;
19+
20+
export class SingleStoreGeographyPointBuilder<T extends ColumnBuilderBaseConfig<'array', 'SingleStoreGeographyPoint'>>
21+
extends SingleStoreColumnBuilder<T>
22+
{
23+
static readonly [entityKind]: string = 'SingleStoreGeographyPointBuilder';
24+
25+
constructor(name: T['name']) {
26+
super(name, 'array', 'SingleStoreGeographyPoint');
27+
}
28+
29+
/** @internal */
30+
override build<TTableName extends string>(
31+
table: AnySingleStoreTable<{ name: TTableName }>,
32+
): SingleStoreGeographyPoint<MakeColumnConfig<T, TTableName>> {
33+
return new SingleStoreGeographyPoint(table, this.config as ColumnBuilderRuntimeConfig<any, any>);
34+
}
35+
}
36+
37+
export class SingleStoreGeographyPoint<T extends ColumnBaseConfig<'array', 'SingleStoreGeographyPoint'>>
38+
extends SingleStoreColumn<T>
39+
{
40+
static readonly [entityKind]: string = 'SingleStoreGeographyPoint';
41+
42+
constructor(
43+
table: AnySingleStoreTable<{ name: T['tableName'] }>,
44+
config: SingleStoreGeographyPointBuilder<T>['config'],
45+
) {
46+
super(table, config);
47+
}
48+
49+
getSQLType(): string {
50+
return 'geographypoint';
51+
}
52+
53+
override mapToDriverValue([lon, lat]: GeographyPoint) {
54+
return sql`"POINT(${lon} ${lat})"`;
55+
}
56+
57+
override mapFromDriverValue(value: string): GeographyPoint {
58+
const numbers = value.slice(value.indexOf('(') + 1, -1);
59+
return numbers.split(' ').map(Number) as GeographyPoint; // driver value will look like `POINT(lon lat)`
60+
}
61+
}
62+
63+
export function geographypoint<TName extends string>(name: TName): SingleStoreGeographyPointBuilderInitial<TName> {
64+
return new SingleStoreGeographyPointBuilder(name);
65+
}

0 commit comments

Comments
 (0)