MCPcopy Index your code
hub / github.com/deepnote/deepnote / createPythonCodeForSqlBlock

Function createPythonCodeForSqlBlock

packages/blocks/src/blocks/sql-blocks.ts:16–38  ·  view source on GitHub ↗
(block: SqlBlock)

Source from the content-addressed store, hash-verified

14const sqlCellVariableTypeSchema = z.enum(SQL_CELL_VARIABLE_TYPES)
15
16export function createPythonCodeForSqlBlock(block: SqlBlock): string {
17 const query = block.content ?? ''
18 const returnVariableType = assertSqlCellVariableType(block.metadata?.deepnote_return_variable_type ?? 'dataframe')
19
20 const integrationId = block.metadata?.sql_integration_id
21 const connectionEnvVarName = integrationId
22 ? convertToEnvironmentVariableName(getSqlEnvVarName(integrationId))
23 : 'SQL_ALCHEMY_JSON_ENV_VAR'
24
25 const escapedQuery = escapePythonString(query)
26
27 const executeSqlFunctionCall = dedent`
28 _dntk.execute_sql(
29 ${escapedQuery},
30 '${connectionEnvVarName}',
31 audit_sql_comment='',
32 sql_cache_mode='cache_disabled',
33 return_variable_type='${returnVariableType}'
34 )
35 `
36
37 return wrapSqlExecution(block, executeSqlFunctionCall)
38}
39
40/**
41 * Generates Python for a SQL block that connects via an inline connection-JSON

Callers 2

createPythonCodeFunction · 0.90
sql-blocks.test.tsFile · 0.90

Calls 5

getSqlEnvVarNameFunction · 0.90
escapePythonStringFunction · 0.90
wrapSqlExecutionFunction · 0.85

Tested by

no test coverage detected