Exemplo n.º 1
0
def compile_single_query(
    eql: str,
    compiler: edbcompiler.Compiler,
    compilerctx: edbcompiler.CompileContext,
) -> str:
    ql_source = edgeql.Source.from_string(eql)
    units = compiler._compile(ctx=compilerctx, source=ql_source)
    assert len(units) == 1 and len(units[0].sql) == 1
    return units[0].sql[0].decode()
Exemplo n.º 2
0
async def _compile_sys_queries(
    ctx: BootstrapContext,
    schema: s_schema.Schema,
    compiler: edbcompiler.Compiler,
    config_spec: config.Spec,
) -> None:
    queries = {}

    _, sql = compile_bootstrap_script(
        compiler,
        schema,
        'SELECT cfg::get_config_json()',
        expected_cardinality_one=True,
        single_statement=True,
    )

    queries['config'] = sql

    _, sql = compile_bootstrap_script(
        compiler,
        schema,
        "SELECT cfg::get_config_json(sources := ['database'])",
        expected_cardinality_one=True,
        single_statement=True,
    )

    queries['dbconfig'] = sql

    _, sql = compile_bootstrap_script(
        compiler,
        schema,
        "SELECT cfg::get_config_json(max_source := 'system override')",
        expected_cardinality_one=True,
        single_statement=True,
    )

    queries['sysconfig'] = sql

    _, sql = compile_bootstrap_script(
        compiler,
        schema,
        'SELECT (SELECT sys::Database FILTER NOT .builtin).name',
        expected_cardinality_one=False,
        single_statement=True,
    )

    queries['listdbs'] = sql

    role_query = '''
        SELECT sys::Role {
            name,
            superuser,
            password,
        };
    '''
    _, sql = compile_bootstrap_script(
        compiler,
        schema,
        role_query,
        expected_cardinality_one=False,
        single_statement=True,
    )
    queries['roles'] = sql

    tids_query = '''
        SELECT schema::ScalarType {
            id,
            backend_id,
        } FILTER .id IN <uuid>json_array_unpack(<json>$ids);
    '''
    _, sql = compile_bootstrap_script(
        compiler,
        schema,
        tids_query,
        expected_cardinality_one=False,
        single_statement=True,
    )

    queries['backend_tids'] = sql

    report_settings: list[str] = []
    for setname in config_spec:
        setting = config_spec[setname]
        if setting.report:
            report_settings.append(setname)

    report_configs_query = f'''
        SELECT assert_single(cfg::Config {{
            {', '.join(report_settings)}
        }});
    '''

    units = compiler._compile(
        ctx=edbcompiler.new_compiler_context(
            user_schema=schema,
            single_statement=True,
            expected_cardinality_one=True,
            json_parameters=False,
            output_format=edbcompiler.IoFormat.BINARY,
            bootstrap_mode=True,
        ),
        source=edgeql.Source.from_string(report_configs_query))
    assert len(units) == 1 and len(units[0].sql) == 1

    report_configs_typedesc = units[0].out_type_id + units[0].out_type_data
    queries['report_configs'] = units[0].sql[0].decode()

    await _store_static_json_cache(
        ctx,
        'sysqueries',
        json.dumps(queries),
    )

    await _store_static_bin_cache(
        ctx,
        'report_configs_typedesc',
        report_configs_typedesc,
    )