Example #1
0
def test_functions_query(executor):
    run(
        executor, '''create function func1() returns int
                     language sql as $$select 1$$''')
    run(executor, 'create schema schema1')
    run(
        executor, '''create function schema1.func2() returns int
                     language sql as $$select 2$$''')

    run(
        executor, '''create function func3()
                     returns table(x int, y int) language sql
                     as $$select 1, 2 from generate_series(1,5)$$;''')

    run(
        executor,
        '''create function func4(x int) returns setof int language sql
                     as $$select generate_series(1,5)$$;''')

    funcs = set(executor.functions())
    assert funcs >= set([
        FunctionMetadata('public', 'func1', None, [], [], 'integer', False,
                         False, False),
        FunctionMetadata('public', 'func3', ['x', 'y'], ['integer', 'integer'],
                         ['t', 't'], 'record', False, False, True),
        FunctionMetadata('public', 'func4', ('x', ),
                         ('integer', ), [], 'integer', False, False, True),
        FunctionMetadata('schema1', 'func2', None, [], [], 'integer', False,
                         False, False),
    ])
def test_function_metadata_eq():
    f1 = FunctionMetadata("s", "f", ["x"], ["integer"], [], "int", False,
                          False, False, False, None)
    f2 = FunctionMetadata("s", "f", ["x"], ["integer"], [], "int", False,
                          False, False, False, None)
    f3 = FunctionMetadata("s", "g", ["x"], ["integer"], [], "int", False,
                          False, False, False, None)
    assert f1 == f2
    assert f1 != f3
    assert not (f1 != f2)
    assert not (f1 == f3)
    assert hash(f1) == hash(f2)
    assert hash(f1) != hash(f3)
def test_function_metadata_eq():
    f1 = FunctionMetadata('s', 'f', ['x'], ['integer'], [], 'int', False,
                          False, False, None)
    f2 = FunctionMetadata('s', 'f', ['x'], ['integer'], [], 'int', False,
                          False, False, None)
    f3 = FunctionMetadata('s', 'g', ['x'], ['integer'], [], 'int', False,
                          False, False, None)
    assert f1 == f2
    assert f1 != f3
    assert not (f1 != f2)
    assert not (f1 == f3)
    assert hash(f1) == hash(f2)
    assert hash(f1) != hash(f3)
Example #4
0
def function_meta_data(
    func_name,
    schema_name="public",
    arg_names=None,
    arg_types=None,
    arg_modes=None,
    return_type=None,
    is_aggregate=False,
    is_window=False,
    is_set_returning=False,
    is_extension=False,
    arg_defaults=None,
):
    return FunctionMetadata(
        schema_name,
        func_name,
        arg_names,
        arg_types,
        arg_modes,
        return_type,
        is_aggregate,
        is_window,
        is_set_returning,
        is_extension,
        arg_defaults,
    )
Example #5
0
    def get_completer(self, settings=None, casing=None):
        metadata = self.metadata
        from pgcli.pgcompleter import PGCompleter
        from pgspecial import PGSpecial

        comp = PGCompleter(smart_completion=True,
                           settings=settings,
                           pgspecial=PGSpecial())

        schemata, tables, tbl_cols, views, view_cols = [], [], [], [], []

        for sch, tbls in metadata["tables"].items():
            schemata.append(sch)

            for tbl, cols in tbls.items():
                tables.append((sch, tbl))
                # Let all columns be text columns
                tbl_cols.extend(
                    [self._make_col(sch, tbl, col) for col in cols])

        for sch, tbls in metadata.get("views", {}).items():
            for tbl, cols in tbls.items():
                views.append((sch, tbl))
                # Let all columns be text columns
                view_cols.extend(
                    [self._make_col(sch, tbl, col) for col in cols])

        functions = [
            FunctionMetadata(sch, *func_meta, arg_defaults=None)
            for sch, funcs in metadata["functions"].items()
            for func_meta in funcs
        ]

        datatypes = [(sch, typ)
                     for sch, datatypes in metadata["datatypes"].items()
                     for typ in datatypes]

        foreignkeys = [
            ForeignKey(*fk) for fks in metadata["foreignkeys"].values()
            for fk in fks
        ]

        comp.extend_schemata(schemata)
        comp.extend_relations(tables, kind="tables")
        comp.extend_relations(views, kind="views")
        comp.extend_columns(tbl_cols, kind="tables")
        comp.extend_columns(view_cols, kind="views")
        comp.extend_functions(functions)
        comp.extend_datatypes(datatypes)
        comp.extend_foreignkeys(foreignkeys)
        comp.set_search_path(["public"])
        comp.extend_casing(casing or [])

        return comp
Example #6
0
    def get_completer(self, settings=None, casing=None):
        metadata = self.metadata
        from pgcli.pgcompleter import PGCompleter
        comp = PGCompleter(smart_completion=True, settings=settings)

        schemata, tables, tbl_cols, views, view_cols = [], [], [], [], []

        for schema, tbls in metadata['tables'].items():
            schemata.append(schema)

            for table, cols in tbls.items():
                tables.append((schema, table))
                # Let all columns be text columns
                tbl_cols.extend([(schema, table, col, 'text') for col in cols])

        for schema, tbls in metadata.get('views', {}).items():
            for view, cols in tbls.items():
                views.append((schema, view))
                # Let all columns be text columns
                view_cols.extend([(schema, view, col, 'text') for col in cols])

        functions = [
            FunctionMetadata(schema, *func_meta)
            for schema, funcs in metadata['functions'].items()
            for func_meta in funcs
        ]

        datatypes = [(schema, datatype)
                     for schema, datatypes in metadata['datatypes'].items()
                     for datatype in datatypes]

        foreignkeys = [
            ForeignKey(*fk) for fks in metadata['foreignkeys'].values()
            for fk in fks
        ]

        comp.extend_schemata(schemata)
        comp.extend_relations(tables, kind='tables')
        comp.extend_relations(views, kind='views')
        comp.extend_columns(tbl_cols, kind='tables')
        comp.extend_columns(view_cols, kind='views')
        comp.extend_functions(functions)
        comp.extend_datatypes(datatypes)
        comp.extend_foreignkeys(foreignkeys)
        comp.set_search_path(['public'])
        comp.extend_casing(casing or [])

        return comp