@difizen/libro-sql-cell 0.2.39 → 0.2.40-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@difizen/libro-sql-cell",
3
- "version": "0.2.39",
3
+ "version": "0.2.40-next.1",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "libro",
@@ -32,10 +32,10 @@
32
32
  "src"
33
33
  ],
34
34
  "dependencies": {
35
- "@difizen/libro-code-editor": "^0.2.39",
36
- "@difizen/libro-jupyter": "^0.2.39",
37
- "@difizen/libro-rendermime": "^0.2.39",
38
- "@difizen/libro-common": "^0.2.39",
35
+ "@difizen/libro-code-editor": "0.2.40-next.1",
36
+ "@difizen/libro-jupyter": "0.2.40-next.1",
37
+ "@difizen/libro-rendermime": "0.2.40-next.1",
38
+ "@difizen/libro-common": "0.2.40-next.1",
39
39
  "@ant-design/icons": "^5.1.0",
40
40
  "@difizen/mana-app": "latest",
41
41
  "@difizen/mana-l10n": "latest"
@@ -1,4 +1,5 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
1
+ /** @format */
2
+
2
3
  import { concatMultilineString } from '@difizen/libro-common';
3
4
  import { FormatterContribution } from '@difizen/libro-jupyter';
4
5
  import type {
@@ -9,6 +10,7 @@ import { singleton } from '@difizen/mana-app';
9
10
 
10
11
  export interface SqlDecodedFormatter extends DefaultDecodedFormatter {
11
12
  result_variable?: string;
13
+ db_id?: string;
12
14
  }
13
15
 
14
16
  @singleton({ contrib: FormatterContribution })
@@ -22,7 +24,12 @@ export class FormatterSqlMagicContribution
22
24
  return libroFormatter === this.formatter ? 100 : 1;
23
25
  };
24
26
  encode = (source: SqlDecodedFormatter) => {
25
- const sqlEncodedValue = `%%sql \n{"result_variable":"${source.result_variable}", "sql_script":"${source.value}"}`;
27
+ const sqlJson = {
28
+ result_variable: source.result_variable,
29
+ db_id: source.db_id,
30
+ sql_script: source.value,
31
+ };
32
+ const sqlEncodedValue = `%%sql \n${JSON.stringify(sqlJson)}`;
26
33
  return {
27
34
  source: sqlEncodedValue,
28
35
  metadata: {
@@ -34,13 +41,19 @@ export class FormatterSqlMagicContribution
34
41
  const value = concatMultilineString(formatterValue.source);
35
42
  if (value.startsWith('%%sql \n')) {
36
43
  const run = value.split('%%sql \n')[1];
37
- const runValue = JSON.parse(run);
38
- const result_variable: string = runValue.result_variable;
39
- const codeValue: string = runValue.sql_script;
40
- return {
41
- result_variable,
42
- value: codeValue,
43
- };
44
+ try {
45
+ const runValue = JSON.parse(run);
46
+ const result_variable: string = runValue.result_variable;
47
+ const db_id: string = runValue.db_id;
48
+ const codeValue: string = runValue.sql_script;
49
+ return {
50
+ result_variable,
51
+ value: codeValue,
52
+ db_id,
53
+ };
54
+ } catch (e) {
55
+ console.warn('🚀 ~ e:', e);
56
+ }
44
57
  }
45
58
  return {
46
59
  value: '',
@@ -48,6 +61,6 @@ export class FormatterSqlMagicContribution
48
61
  };
49
62
 
50
63
  validate = (source: SqlDecodedFormatter): source is SqlDecodedFormatter => {
51
- return 'result_variable' in source && 'sql_script' in source;
64
+ return 'result_variable' in source && 'sql_script' in source && 'db_id' in source;
52
65
  };
53
66
  }
@@ -19,6 +19,8 @@ export class LibroSqlCellModel extends LibroCellModel implements ExecutableCellM
19
19
  @prop()
20
20
  resultVariable: string | undefined;
21
21
  @prop()
22
+ dbId: string | undefined;
23
+ @prop()
22
24
  executeCount: ExecutionCount;
23
25
  @prop()
24
26
  executing: boolean;
@@ -82,6 +84,7 @@ export class LibroSqlCellModel extends LibroCellModel implements ExecutableCellM
82
84
  override set decodeObject(data: SqlDecodedFormatter) {
83
85
  this.value = data.value;
84
86
  this.resultVariable = data.result_variable;
87
+ this.dbId = data.db_id;
85
88
  this._decodeObject = data;
86
89
  }
87
90
 
@@ -90,6 +93,7 @@ export class LibroSqlCellModel extends LibroCellModel implements ExecutableCellM
90
93
  ...this._decodeObject,
91
94
  value: this.value,
92
95
  result_variable: this.resultVariable || this._decodeObject.result_variable,
96
+ db_id: this.dbId || this._decodeObject.db_id,
93
97
  };
94
98
  }
95
99
 
@@ -11,6 +11,7 @@ export interface VisAnalysis {
11
11
 
12
12
  export interface DatabaseConfig {
13
13
  db_type: string;
14
+ id: string;
14
15
  username: string;
15
16
  password: string;
16
17
  host: string;
@@ -2,6 +2,5 @@ import { singleton } from '@difizen/mana-app';
2
2
 
3
3
  @singleton()
4
4
  export class SqlScript {
5
- public readonly getDbConfig: string = `from libro_sql.database import db
6
- db.get_db_config().json()`;
5
+ public readonly getDbConfig: string = `from libro_sql.database import db\nimport json\njson.dumps(db.to_dbs_array())`;
7
6
  }
@@ -36,6 +36,7 @@ import {
36
36
  } from '@difizen/mana-app';
37
37
  import { l10n } from '@difizen/mana-l10n';
38
38
  import type { InputRef } from 'antd';
39
+ import { Select } from 'antd';
39
40
  import { Input } from 'antd';
40
41
  import React from 'react';
41
42
  import { useCallback, useEffect, useRef, useState } from 'react';
@@ -166,12 +167,27 @@ export const LibroSqlCell = React.forwardRef<HTMLDivElement>(
166
167
  const instance = useInject<LibroSqlCellView>(ViewInstance);
167
168
  const contextKey = useInject(LibroContextKey);
168
169
  const [edit, setEdit] = useState(false);
170
+ const [selectedDb, setSelectedDb] = useState<string>(
171
+ instance.model.dbId || l10n.t('暂无内置数据库'),
172
+ );
169
173
 
170
174
  const handCancelEdit = () => {
171
175
  contextKey.enableCommandMode();
172
176
  setEdit(false);
173
177
  };
174
178
 
179
+ useEffect(() => {
180
+ instance.getDatabaseConfig();
181
+ }, [instance]);
182
+
183
+ const handleChange = (value: string) => {
184
+ instance.handleDbChange(value);
185
+ if (instance.parent.model.onChange) {
186
+ instance.parent.model.onChange();
187
+ }
188
+ setSelectedDb(value);
189
+ };
190
+
175
191
  return (
176
192
  <div tabIndex={10} ref={ref} className={instance.className}>
177
193
  <div className="libro-sql-cell-header">
@@ -179,13 +195,21 @@ export const LibroSqlCell = React.forwardRef<HTMLDivElement>(
179
195
  <span className="libro-sql-source-title">
180
196
  <DatabaseOutlined />
181
197
  </span>
182
- <span className="libro-sql-source-content">
183
- {instance.databaseConfig
184
- ? instance.databaseConfig.db_type +
185
- ': ' +
186
- instance.databaseConfig.database
187
- : l10n.t('暂未配置数据库')}
188
- </span>
198
+ <Select
199
+ value={selectedDb}
200
+ style={{ minWidth: 160 }}
201
+ onChange={handleChange}
202
+ options={instance.databases.map((db) => {
203
+ return {
204
+ value: db.id,
205
+ label: db.db_type + ': ' + db.database,
206
+ };
207
+ })}
208
+ bordered={false}
209
+ onFocus={async () => {
210
+ await instance.getDatabaseConfig();
211
+ }}
212
+ />
189
213
  </div>
190
214
  <div className="libro-sql-variable-name">
191
215
  <span className="libro-sql-variable-name-title">Name: </span>
@@ -227,7 +251,7 @@ export class LibroSqlCellView extends LibroEditableExecutableCellView {
227
251
  outputs: IOutput[];
228
252
 
229
253
  @prop()
230
- databaseConfig?: DatabaseConfig;
254
+ databases: DatabaseConfig[] = [];
231
255
 
232
256
  @prop()
233
257
  override editorStatus: EditorStatus = EditorStatus.NOTLOADED;
@@ -346,6 +370,10 @@ export class LibroSqlCellView extends LibroEditableExecutableCellView {
346
370
  }
347
371
  };
348
372
 
373
+ handleDbChange(value: string) {
374
+ this.model.dbId = value;
375
+ }
376
+
349
377
  override toJSON(): LibroCell {
350
378
  const meta = super.toJSON();
351
379
  return {
@@ -497,9 +525,9 @@ export class LibroSqlCellView extends LibroEditableExecutableCellView {
497
525
  (msg) =>
498
526
  this.handleQueryResponse(msg, (result) => {
499
527
  try {
500
- this.databaseConfig = JSON.parse(result);
501
- } catch (e) {
502
- this.databaseConfig = undefined;
528
+ this.databases = JSON.parse(result);
529
+ } catch {
530
+ //
503
531
  }
504
532
  }),
505
533
  );