Skip to content

Commit 10c17e2

Browse files
authored
Merge pull request #1457 from lowcoder-org/feat/alasql
Feat/alasql
2 parents c3068a6 + 3a4ab7c commit 10c17e2

File tree

14 files changed

+231
-3
lines changed

14 files changed

+231
-3
lines changed

client/packages/lowcoder/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@types/react-signature-canvas": "^1.0.2",
3737
"@types/react-test-renderer": "^18.0.0",
3838
"@types/react-virtualized": "^9.21.21",
39+
"alasql": "^4.6.2",
3940
"animate.css": "^4.1.1",
4041
"antd": "^5.20.0",
4142
"axios": "^1.7.7",

client/packages/lowcoder/src/components/ResCreatePanel.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ const ResButton = (props: {
169169
compType: "streamApi",
170170
},
171171
},
172+
alasql: {
173+
label: trans("query.quickAlasql"),
174+
type: BottomResTypeEnum.Query,
175+
extra: {
176+
compType: "alasql",
177+
},
178+
},
172179
graphql: {
173180
label: trans("query.quickGraphql"),
174181
type: BottomResTypeEnum.Query,
@@ -319,6 +326,7 @@ export function ResCreatePanel(props: ResCreateModalProps) {
319326
<DataSourceListWrapper $placement={placement}>
320327
<ResButton size={buttonSize} identifier={"restApi"} onSelect={onSelect} />
321328
<ResButton size={buttonSize} identifier={"streamApi"} onSelect={onSelect} />
329+
<ResButton size={buttonSize} identifier={"alasql"} onSelect={onSelect} />
322330
<ResButton size={buttonSize} identifier={"graphql"} onSelect={onSelect} />
323331
{datasource.map((i) => (
324332
<ResButton size={buttonSize} key={i.id} identifier={i} onSelect={onSelect} />
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { QueryConfigItemWrapper, QueryConfigLabel, QueryConfigWrapper } from "components/query";
2+
import { simpleMultiComp } from "comps/generators/multi";
3+
import { JSONValue } from "../../../util/jsonTypes";
4+
import { ParamsStringControl } from "../../controls/paramsControl";
5+
import { dropdownControl } from "@lowcoder-ee/comps/controls/dropdownControl";
6+
import { QueryResult } from "../queryComp";
7+
import { QUERY_EXECUTION_ERROR, QUERY_EXECUTION_OK } from "@lowcoder-ee/constants/queryConstants";
8+
import { getDynamicStringSegments, isDynamicSegment } from "lowcoder-core";
9+
import alasql from "alasql";
10+
import { trans } from "i18n";
11+
12+
const childrenMap = {
13+
databaseType: dropdownControl(
14+
[
15+
{ label: "Data Query", value: "dataQuery" },
16+
{ label: "Local Database", value: "localDB" },
17+
] as const,
18+
"dataQuery"
19+
),
20+
database: dropdownControl(
21+
[
22+
{ label: "Local Storage", value: "LOCALSTORAGE" },
23+
{ label: "IndexedDB", value: "INDEXEDDB" },
24+
] as const,
25+
"LOCALSTORAGE"
26+
),
27+
sql: ParamsStringControl,
28+
};
29+
30+
const AlaSqlTmpQuery = simpleMultiComp(childrenMap);
31+
32+
// TODO: Support multiple queries
33+
export class AlaSqlQuery extends AlaSqlTmpQuery {
34+
override getView() {
35+
const children = this.children;
36+
const params = [ ...children.sql.getQueryParams() ];
37+
const databaseType = children.databaseType.getView();
38+
const selectedDB = children.database.getView();
39+
const paramsMap: Record<string, any> = {};
40+
params.forEach(({key, value}) => {
41+
paramsMap[key] = value();
42+
});
43+
44+
const sqlQuery = children.sql.children.text.unevaledValue.replace(/ +/g, ' ');
45+
const isCreateDBQuery = sqlQuery.toUpperCase().startsWith('CREATE DATABASE');
46+
47+
return async (p: { args?: Record<string, unknown> }): Promise<QueryResult> => {
48+
try {
49+
let result: JSONValue;
50+
const timer = performance.now();
51+
52+
if (databaseType === 'localDB' && isCreateDBQuery) {
53+
const updatedQuery = `${sqlQuery.slice(0, 6)} ${selectedDB} ${sqlQuery.slice(6)}`;
54+
const tableName = updatedQuery.split(' ').pop()?.replace(';', '');
55+
result = alasql(updatedQuery);
56+
result = alasql(`ATTACH ${selectedDB} DATABASE ${tableName};`);
57+
} else {
58+
let segments = getDynamicStringSegments(sqlQuery);
59+
let dataArr: any = [];
60+
segments = segments.map((segment) => {
61+
if (isDynamicSegment(segment)) {
62+
const key = segment.replace('{{','').replace('}}','');
63+
dataArr.push(paramsMap[key]);
64+
return '?';
65+
}
66+
return segment;
67+
})
68+
result = alasql(segments.join(' '), dataArr);
69+
}
70+
71+
return {
72+
data: result as JSONValue,
73+
code: QUERY_EXECUTION_OK,
74+
success: true,
75+
runTime: Number((performance.now() - timer).toFixed()),
76+
};
77+
} catch (e) {
78+
return {
79+
success: false,
80+
data: "",
81+
code: QUERY_EXECUTION_ERROR,
82+
message: (e as any).message || "",
83+
};
84+
}
85+
};
86+
}
87+
88+
propertyView(props: { datasourceId: string }) {
89+
return <PropertyView {...props} comp={this} />;
90+
}
91+
}
92+
93+
const PropertyView = (props: { comp: InstanceType<typeof AlaSqlQuery>; datasourceId: string }) => {
94+
const { comp } = props;
95+
const { children } = comp;
96+
97+
return (
98+
<>
99+
<QueryConfigWrapper>
100+
<QueryConfigLabel>{trans("query.databaseType")}</QueryConfigLabel>
101+
<QueryConfigItemWrapper>
102+
{children.databaseType.propertyView({
103+
styleName: "medium",
104+
width: "100%",
105+
})}
106+
</QueryConfigItemWrapper>
107+
</QueryConfigWrapper>
108+
109+
{children.databaseType.getView() === 'localDB' && (
110+
<QueryConfigWrapper>
111+
<QueryConfigLabel>{trans("query.chooseDatabase")}</QueryConfigLabel>
112+
<QueryConfigItemWrapper>
113+
{children.database.propertyView({
114+
styleName: "medium",
115+
width: "100%",
116+
})}
117+
</QueryConfigItemWrapper>
118+
</QueryConfigWrapper>
119+
)}
120+
121+
<QueryConfigWrapper>
122+
<QueryConfigItemWrapper>
123+
{children.sql.propertyView({
124+
placement: "bottom",
125+
placeholder: "SELECT * FROM users WHERE user_id = {{userId}}::uuid",
126+
styleName: "medium",
127+
language: "sql",
128+
enableMetaCompletion: true,
129+
})}
130+
</QueryConfigItemWrapper>
131+
</QueryConfigWrapper>
132+
</>
133+
);
134+
};

client/packages/lowcoder/src/comps/queries/queryComp/queryPropertyView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ function useDatasourceStatus(datasourceId: string, datasourceType: ResourceType)
734734
datasourceType === "js" ||
735735
datasourceType === "streamApi" ||
736736
datasourceType === "libraryQuery" ||
737+
datasourceType === "alasql" ||
737738
datasourceId === QUICK_REST_API_ID ||
738739
datasourceId === QUICK_GRAPHQL_ID
739740
) {

client/packages/lowcoder/src/comps/queries/resourceDropdown.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ const QuickGraphqlValue: ResourceOptionValue = {
102102
type: "graphql",
103103
};
104104

105+
const QuickAlasqlValue: ResourceOptionValue = {
106+
id: "",
107+
type: "alasql",
108+
};
109+
105110
interface ResourceDropdownProps {
106111
changeResource: (datasourceId: string, value: string) => void;
107112
selectedResource: ResourceOptionValue;
@@ -265,6 +270,17 @@ export const ResourceDropdown = (props: ResourceDropdownProps) => {
265270
<SelectOptionLabel>{trans("query.quickStreamAPI")} </SelectOptionLabel>
266271
</SelectOptionContains>
267272
</SelectOption>
273+
274+
<SelectOption
275+
key={JSON.stringify(QuickAlasqlValue)}
276+
label={trans("query.quickAlasql")}
277+
value={JSON.stringify(QuickAlasqlValue)}
278+
>
279+
<SelectOptionContains>
280+
{getBottomResIcon("restApi")}
281+
<SelectOptionLabel>{trans("query.quickAlasql")} </SelectOptionLabel>
282+
</SelectOptionContains>
283+
</SelectOption>
268284

269285
<SelectOption
270286
key={JSON.stringify(QuickGraphqlValue)}

client/packages/lowcoder/src/comps/queries/sqlQuery/SQLQuery.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ export const NOT_SUPPORT_GUI_SQL_QUERY: string[] = [
255255
"snowflake",
256256
"tdengine",
257257
"dameng",
258+
"alasql",
258259
];
259260
const SUPPORT_UPSERT_SQL_QUERY: string[] = [
260261
"mysql",

client/packages/lowcoder/src/constants/datasourceConstants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const databasePlugins: Partial<DatasourceType>[] = [
1515
"clickHouse",
1616
"snowflake",
1717
"mariadb",
18+
"alasql",
1819
];
1920

2021
export const apiPluginsForQueryLibrary: Partial<DatasourceType>[] = [
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const libNames = new Set(["uuid", "numbro", "Papa", "supabase"]);
1+
export const libNames = new Set(["uuid", "numbro", "Papa", "supabase", "alasql"]);

client/packages/lowcoder/src/constants/queryConstants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { GraphqlQuery } from "../comps/queries/httpQuery/graphqlQuery";
1313
import { toPluginQuery } from "comps/queries/pluginQuery/pluginQuery";
1414
import { MultiCompConstructor } from "lowcoder-core";
1515
import { DataSourcePluginMeta } from "lowcoder-sdk/dataSource";
16+
import { AlaSqlQuery } from "@lowcoder-ee/comps/queries/httpQuery/alasqlQuery";
1617

1718
export type DatasourceType =
1819
| "mysql"
@@ -29,13 +30,15 @@ export type DatasourceType =
2930
| "googleSheets"
3031
| "graphql"
3132
| "snowflake"
32-
| "mariadb";
33+
| "mariadb"
34+
| "alasql";
3335

3436
export type ResourceType = DatasourceType | "js" | "libraryQuery" | "view";
3537

3638
export const QueryMap = {
3739
js: JSQuery,
3840
mysql: SQLQuery,
41+
alasql: AlaSqlQuery,
3942
restApi: HttpQuery,
4043
streamApi: StreamQuery,
4144
mongodb: MongoQuery,

client/packages/lowcoder/src/global.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ declare global {
99
numbro: any;
1010
Papa: any;
1111
uuid: any;
12+
alasql: any;
1213
}
1314
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy