Example #1
0
    def visit_FuncCall(self, node):
        self.write(common.qname(*node.name))

        self.write('(')
        if node.agg_distinct:
            self.write('DISTINCT ')
        self.visit_list(node.args, newlines=False)

        if node.agg_order:
            self.write(' ORDER BY ')
            self.visit_list(node.agg_order, newlines=False)

        self.write(')')

        if node.agg_filter:
            self.write(' FILTER (WHERE ')
            self.visit(node.agg_filter)
            self.write(')')

        if node.over:
            self.write(' OVER (')
            if node.over.partition_clause:
                self.write('PARTITION BY ')
                self.visit_list(node.over.partition_clause, newlines=False)

            if node.over.order_clause:
                self.write(' ORDER BY ')
                self.visit_list(node.over.order_clause, newlines=False)

            # XXX: add support for frame definition

            self.write(')')

        if node.with_ordinality:
            self.write(' WITH ORDINALITY')
Example #2
0
 def visit_TypeName(self, node):
     self.write(common.qname(*node.name))
     if node.array_bounds:
         for array_bound in node.array_bounds:
             self.write('[')
             if array_bound >= 0:
                 self.write(array_bound)
             self.write(']')
Example #3
0
 def visit_ColumnRef(self, node):
     names = node.name
     if isinstance(names[-1], pgast.Star):
         self.write(common.qname(*names[:-1]))
         if len(names) > 1:
             self.write('.')
         self.write('*')
     else:
         if names == ['VALUE']:
             self.write(names[0])
         elif names[0] in {'OLD', 'NEW'}:
             self.write(names[0])
             if len(names) > 1:
                 self.write('.')
                 self.write(common.qname(*names[1:]))
         else:
             self.write(common.qname(*names))
Example #4
0
    async def creation_code(self, context):
        link_map = await context.get_class_map()

        ids = tuple(sorted(list(link_map[n] for n in self.link_names)))
        id_str = '_'.join(str(i) for i in ids)

        name = '%s_%s_%s_cardinality_idx' % (self.name_prefix, id_str,
                                             self.cardinality)
        name = common.edgedb_name_to_pg_name(name)
        predicate = 'ptr_item_id IN (%s)' % ', '.join(str(id) for id in ids)

        code = '''
            CREATE {unique} INDEX {name} ON {table}s ({cols}) {predicate}
        '''.format(unique='UNIQUE',
                   name=common.qname(name),
                   table=common.qname(*self.table_name),
                   cols=', '.join(common.quote_ident(c) for c in self.columns),
                   predicate=('WHERE {}'.format(predicate)))

        return code
Example #5
0
    async def read_scalars(self, schema):
        seqs = await introspection.sequences.fetch(
            self.connection,
            schema_pattern='edgedb%',
            sequence_pattern='%_sequence')
        seqs = {(s['schema'], s['name']): s for s in seqs}

        seen_seqs = set()

        scalar_list = await datasources.schema.scalars.fetch(self.connection)

        basemap = {}

        for row in scalar_list:
            name = sn.Name(row['name'])

            scalar_data = {
                'name':
                name,
                'title':
                self.json_to_word_combination(row['title']),
                'description':
                row['description'],
                'is_abstract':
                row['is_abstract'],
                'is_final':
                row['is_final'],
                'view_type': (s_types.ViewType(row['view_type'])
                              if row['view_type'] else None),
                'bases':
                row['bases'],
                'default':
                row['default'],
                'expr':
                (s_expr.ExpressionText(row['expr']) if row['expr'] else None)
            }

            self.scalar_cache[name] = scalar_data
            scalar_data['default'] = self.unpack_default(row['default'])

            if scalar_data['bases']:
                basemap[name] = scalar_data['bases']

            scalar = s_scalars.ScalarType(
                name=name,
                default=scalar_data['default'],
                title=scalar_data['title'],
                description=scalar_data['description'],
                is_abstract=scalar_data['is_abstract'],
                is_final=scalar_data['is_final'],
                view_type=scalar_data['view_type'],
                expr=scalar_data['expr'])

            schema.add(scalar)

        for scalar in schema.get_objects(type='ScalarType'):
            try:
                basename = basemap[scalar.name]
            except KeyError:
                pass
            else:
                scalar.bases = [schema.get(sn.Name(basename[0]))]

        sequence = schema.get('std::sequence', None)
        for scalar in schema.get_objects(type='ScalarType'):
            if (sequence is not None and scalar.issubclass(sequence)
                    and not scalar.is_abstract):
                seq_name = common.scalar_name_to_sequence_name(scalar.name,
                                                               catenate=False)
                if seq_name not in seqs:
                    msg = 'internal metadata incosistency'
                    details = (f'Missing sequence for sequence '
                               f'scalar {scalar.name}')
                    raise s_err.SchemaError(msg, details=details)
                seen_seqs.add(seq_name)

        extra_seqs = set(seqs) - seen_seqs
        if extra_seqs:
            msg = 'internal metadata incosistency'
            details = 'Extraneous sequences exist: {}'.format(', '.join(
                common.qname(*t) for t in extra_seqs))
            raise s_err.SchemaError(msg, details=details)
Example #6
0
 def visit_Relation(self, node):
     if node.schemaname is None:
         self.write(common.qname(node.name))
     else:
         self.write(common.qname(node.schemaname, node.name))
Example #7
0
    async def read_scalars(self, schema, only_modules, exclude_modules):
        seqs = await introspection.sequences.fetch(
            self.connection,
            schema_pattern='edgedb%',
            sequence_pattern='%_sequence')
        seqs = {(s['schema'], s['name']): s for s in seqs}

        seen_seqs = set()

        scalar_list = await datasources.schema.scalars.fetch(
            self.connection,
            modules=only_modules,
            exclude_modules=exclude_modules)

        basemap = {}

        for row in scalar_list:
            name = sn.Name(row['name'])

            scalar_data = {
                'id':
                row['id'],
                'name':
                name,
                'is_abstract':
                row['is_abstract'],
                'is_final':
                row['is_final'],
                'view_type': (s_types.ViewType(row['view_type'])
                              if row['view_type'] else None),
                'bases':
                row['bases'],
                'default':
                row['default'],
                'expr':
                (s_expr.ExpressionText(row['expr']) if row['expr'] else None)
            }

            scalar_data['default'] = self.unpack_default(row['default'])

            if scalar_data['bases']:
                basemap[name] = scalar_data.pop('bases')

            schema, scalar = s_scalars.ScalarType.create_in_schema(
                schema, **scalar_data)

        for scalar in schema.get_objects(type=s_scalars.ScalarType):
            try:
                basename = basemap[scalar.get_name(schema)]
            except KeyError:
                pass
            else:
                schema = scalar.set_field_value(
                    schema, 'bases', [schema.get(sn.Name(basename[0]))])

        sequence = schema.get('std::sequence', None)
        for scalar in schema.get_objects(type=s_scalars.ScalarType):
            if (sequence is not None and scalar.issubclass(schema, sequence)
                    and not scalar.get_is_abstract(schema)):
                seq_name = common.get_backend_name(schema,
                                                   scalar,
                                                   catenate=False,
                                                   aspect='sequence')
                if seq_name not in seqs:
                    msg = 'internal metadata incosistency'
                    details = (f'Missing sequence for sequence '
                               f'scalar {scalar.get_name(schema)}')
                    raise errors.SchemaError(msg, details=details)
                seen_seqs.add(seq_name)

        extra_seqs = set(seqs) - seen_seqs
        if extra_seqs and not only_modules and not exclude_modules:
            msg = 'internal metadata incosistency'
            details = 'Extraneous sequences exist: {}'.format(', '.join(
                common.qname(*t) for t in extra_seqs))
            raise errors.SchemaError(msg, details=details)

        return schema