Exemplo n.º 1
0
    def __call__(self, event, table, bind):
        spatial_dialect = DialectManager.get_spatial_dialect(bind.dialect)
        if event in ('before-create', 'before-drop'):
            """Remove geometry column from column list (table._columns), so that it 
            does not show up in the create statement ("create table tab (..)"). 
            Afterwards (on event 'after-create') restore the column list from self._stack.
            """
            regular_cols = [c for c in table.c if not isinstance(c.type, Geometry)]
            gis_cols = set(table.c).difference(regular_cols)
            self._stack.append(table.c)
            setattr(table, self.columns_attribute,
                    expression.ColumnCollection(*regular_cols))
            
            if event == 'before-drop':
                for c in gis_cols:
                    spatial_dialect.handle_ddl_before_drop(bind, table, c)
                
        elif event == 'after-create':
            setattr(table, self.columns_attribute, self._stack.pop())
            
            for c in table.c:
                if isinstance(c.type, Geometry):
                    spatial_dialect.handle_ddl_after_create(bind, table, c)

        elif event == 'after-drop':
            setattr(table, self.columns_attribute, self._stack.pop())
Exemplo n.º 2
0
def __compile_wkbspatialelement(element, compiler, **kw):
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    function = _get_function(element, compiler, (database_dialect.bind_wkb_value(element), 
                                                 element.srid),
                                                 kw.get('within_columns_clause', False))
    return compiler.process(function)
Exemplo n.º 3
0
    def __call__(self, event, table, bind):
        spatial_dialect = DialectManager.get_spatial_dialect(bind.dialect)
        if event in ('before-create', 'before-drop'):
            """Remove geometry column from column list (table._columns), so that it 
            does not show up in the create statement ("create table tab (..)"). 
            Afterwards (on event 'after-create') restore the column list from self._stack.
            """
            regular_cols = [
                c for c in table.c if not isinstance(c.type, Geometry)
            ]
            gis_cols = set(table.c).difference(regular_cols)
            self._stack.append(table.c)
            setattr(table, self.columns_attribute,
                    expression.ColumnCollection(*regular_cols))

            if event == 'before-drop':
                for c in gis_cols:
                    spatial_dialect.handle_ddl_before_drop(bind, table, c)

        elif event == 'after-create':
            setattr(table, self.columns_attribute, self._stack.pop())

            for c in table.c:
                if isinstance(c.type, Geometry):
                    spatial_dialect.handle_ddl_after_create(bind, table, c)

        elif event == 'after-drop':
            setattr(table, self.columns_attribute, self._stack.pop())
Exemplo n.º 4
0
def _get_function(element, compiler, params, within_column_clause):
    """For elements of type BaseFunction, the database specific function data 
    is looked up and a executable sqlalchemy.sql.expression.Function object 
    is returned.
    """
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    for kls in element.__class__.__mro__:
        try:
            function_data = database_dialect.get_function(kls)
        except KeyError:
            continue
    if function_data is None:
        raise Exception("Unsupported function for this dialect")
    
    if isinstance(function_data, list):
        """if we have a list of function names, create cascaded Function objects
        for all function names in the list::
        
            ['TO_CHAR', 'SDO_UTIL.TO_WKTGEOMETRY'] --> TO_CHAR(SDO_UTIL.TO_WKTGEOMETRY(..))
        """
        function = None
        for name in reversed(function_data):
            packages = name.split('.')
            
            if function is None:
                """for the innermost function use the passed-in parameters as argument,
                otherwise use the prior created function
                """
                args = params
            else:
                args = [function]
                
            function = Function(packages.pop(-1), 
                        packagenames=packages,
                        *args
                        )
        
        return function
    
    elif isinstance(function_data, types.FunctionType):
        """if we have a function, call this function with the parameters and return the
        created Function object
        """
        if hasattr(element, 'flags'):
            # when element is a BaseFunction
            flags = element.flags
        else:
            flags = {}
            
        return function_data(params, within_column_clause, **flags)
    
    else:
        packages = function_data.split('.')
        
        return Function(packages.pop(-1), 
                        packagenames=packages, 
                        *params
                        )
Exemplo n.º 5
0
def _get_function(element, compiler, params, within_column_clause):
    """For elements of type BaseFunction, the database specific function data 
    is looked up and a executable sqlalchemy.sql.expression.Function object 
    is returned.
    """
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    for kls in element.__class__.__mro__:
        try:
            function_data = database_dialect.get_function(kls)
        except KeyError:
            continue
    if function_data is None:
        raise Exception("Unsupported function for this dialect")
    
    if isinstance(function_data, list):
        """if we have a list of function names, create cascaded Function objects
        for all function names in the list::
        
            ['TO_CHAR', 'SDO_UTIL.TO_WKTGEOMETRY'] --> TO_CHAR(SDO_UTIL.TO_WKTGEOMETRY(..))
        """
        function = None
        for name in reversed(function_data):
            packages = name.split('.')
            
            if function is None:
                """for the innermost function use the passed-in parameters as argument,
                otherwise use the prior created function
                """
                args = params
            else:
                args = [function]
                
            function = Function(packages.pop(-1), 
                        packagenames=packages,
                        *args
                        )
        
        return function
    
    elif isinstance(function_data, types.FunctionType):
        """if we have a function, call this function with the parameters and return the
        created Function object
        """
        if hasattr(element, 'flags'):
            # when element is a BaseFunction
            flags = element.flags
        else:
            flags = {}
            
        return function_data(params, within_column_clause, **flags)
    
    else:
        packages = function_data.split('.')
        
        return Function(packages.pop(-1), 
                        packagenames=packages, 
                        *params
                        )
Exemplo n.º 6
0
def __compile_wkbspatialelement(element, compiler, **kw):
    from geoalchemy.dialect import DialectManager
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    function = _get_function(
        element, compiler,
        (database_dialect.bind_wkb_value(element), element.srid),
        kw.get('within_columns_clause', False))
    return compiler.process(function)
Exemplo n.º 7
0
 def process_result_value(self, value, dialect):
     if value is not None:
         from geoalchemy.dialect import DialectManager 
         database_dialect = DialectManager.get_spatial_dialect(dialect)
         
         return database_dialect.process_wkb(value)
     else:
         return value
Exemplo n.º 8
0
 def process_result_value(self, value, dialect):
     if value is not None:
         from geoalchemy.dialect import DialectManager 
         database_dialect = DialectManager.get_spatial_dialect(dialect)
         
         return database_dialect.process_wkb(value)
     else:
         return value
Exemplo n.º 9
0
def __compile__within_distance(element, compiler, **kw):
    from geoalchemy.dialect import DialectManager
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    function = database_dialect.get_function(element.__class__)
    arguments = list(element.arguments)
    return compiler.process(
        function(compiler, parse_clause(arguments.pop(0), compiler),
                 parse_clause(arguments.pop(0), compiler), arguments.pop(0),
                 *arguments))
Exemplo n.º 10
0
def __compile__within_distance(element, compiler, **kw):
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    function = database_dialect.get_function(element.__class__)
    arguments = list(element.arguments)
    return compiler.process(
        function(compiler,
                 parse_clause(arguments.pop(0), compiler),
                 parse_clause(arguments.pop(0), compiler),
                 arguments.pop(0), *arguments))
Exemplo n.º 11
0
 def test_get_spatial_dialect(self):
     spatial_dialect = DialectManager.get_spatial_dialect(PGDialect_psycopg2())
     ok_(isinstance(spatial_dialect, PGSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(MySQLDialect()), MySQLSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(SQLiteDialect()), SQLiteSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(OracleDialect()), OracleSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(MSDialect()), MSSpatialDialect))
     spatial_dialect2 = DialectManager.get_spatial_dialect(PGDialect_psycopg2())
     ok_(spatial_dialect is spatial_dialect2, "only one instance per dialect should be created")
Exemplo n.º 12
0
 def test_get_spatial_dialect(self):
     spatial_dialect = DialectManager.get_spatial_dialect(PGDialect_psycopg2())
     ok_(isinstance(spatial_dialect, PGSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(MySQLDialect()), MySQLSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(SQLiteDialect()), SQLiteSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(OracleDialect()), OracleSpatialDialect))
     ok_(isinstance(DialectManager.get_spatial_dialect(MSDialect()), MSSpatialDialect))
     spatial_dialect2 = DialectManager.get_spatial_dialect(PGDialect_psycopg2())
     ok_(spatial_dialect is spatial_dialect2, "only one instance per dialect should be created")
Exemplo n.º 13
0
def __compile__within_distance(element, compiler, **kw):
    from geoalchemy.dialect import DialectManager
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    for kls in element.__class__.__mro__:
        try:
            function = database_dialect.get_function(kls)
        except KeyError:
            continue
    if function is None:
        raise Exception("Unsupported function for this dialect")
    arguments = list(element.arguments)
    return compiler.process(
        function(compiler, parse_clause(arguments.pop(0), compiler),
                 parse_clause(arguments.pop(0), compiler), arguments.pop(0),
                 *arguments))
Exemplo n.º 14
0
def __compile__within_distance(element, compiler, **kw):
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    for kls in element.__class__.__mro__:
        try:
            function = database_dialect.get_function(kls)
        except KeyError:
            continue
    if function is None:
        raise Exception("Unsupported function for this dialect")
    arguments = list(element.arguments)
    return compiler.process(
        function(compiler,
                 parse_clause(arguments.pop(0), compiler),
                 parse_clause(arguments.pop(0), compiler),
                 arguments.pop(0), *arguments))
Exemplo n.º 15
0
def __compile_base_function(element, compiler, **kw):
    
    params = [parse_clause(argument, compiler) for argument in element.arguments]
    
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    
    if database_dialect.is_member_function(element.__class__):
        geometry = params.pop(0)
        function_name = database_dialect.get_function(element.__class__)
        
        if isinstance(function_name, str):
            """If the function is defined as String (e.g. "oracle_functions.dims : 'Get_Dims'"), 
            we construct the function call in the query ourselves. This is because SQLAlchemy, 
            at this point of the compile process, does not add parenthesis for functions 
            without arguments when using Oracle.
            Otherwise let SQLAlchemy build the query. Note that this won't work for Oracle with
            functions without parameters."""
            
            return "%s.%s(%s)" % (
                        compiler.process(geometry),
                        function_name,
                        ", ".join([compiler.process(e) for e in params]) 
                        )
        else:
            function = _get_function(element, compiler, params, kw.get('within_columns_clause', False))
            
            return "%s.%s" % (
                        compiler.process(geometry),
                        compiler.process(function)      
                        )
            
    elif database_dialect.is_property(element.__class__):
        geometry = params.pop(0)
        function_name = database_dialect.get_function(element.__class__)
        
        return "%s.%s" % (
                        compiler.process(geometry),
                        function_name 
                        )
        
    else:
        function = _get_function(element, compiler, params, kw.get('within_columns_clause', False))
        return compiler.process(function)
Exemplo n.º 16
0
def __compile_base_function(element, compiler, **kw):
    
    params = [parse_clause(argument, compiler) for argument in element.arguments]
    
    from geoalchemy.dialect import DialectManager 
    database_dialect = DialectManager.get_spatial_dialect(compiler.dialect)
    
    if database_dialect.is_member_function(element.__class__):
        geometry = params.pop(0)
        function_name = database_dialect.get_function(element.__class__)
        
        if isinstance(function_name, str):
            """If the function is defined as String (e.g. "oracle_functions.dims : 'Get_Dims'"), 
            we construct the function call in the query ourselves. This is because SQLAlchemy, 
            at this point of the compile process, does not add parenthesis for functions 
            without arguments when using Oracle.
            Otherwise let SQLAlchemy build the query. Note that this won't work for Oracle with
            functions without parameters."""
            
            return "%s.%s(%s)" % (
                        compiler.process(geometry),
                        function_name,
                        ", ".join([compiler.process(e) for e in params]) 
                        )
        else:
            function = _get_function(element, compiler, params, kw.get('within_columns_clause', False))
            
            return "%s.%s" % (
                        compiler.process(geometry),
                        compiler.process(function)      
                        )
            
    elif database_dialect.is_property(element.__class__):
        geometry = params.pop(0)
        function_name = database_dialect.get_function(element.__class__)
        
        return "%s.%s" % (
                        compiler.process(geometry),
                        function_name 
                        )
        
    else:
        function = _get_function(element, compiler, params, kw.get('within_columns_clause', False))
        return compiler.process(function)
Exemplo n.º 17
0
 def process(value):
     if value is not None:
         return DialectManager.get_spatial_dialect(dialect).process_result(value, self)
     else:
         return value
Exemplo n.º 18
0
 def test_get_spatial_dialect_unknown_dialect(self):
     DialectManager.get_spatial_dialect(FBDialect())
Exemplo n.º 19
0
 def test_get_spatial_dialect_unknown_dialect(self):
     DialectManager.get_spatial_dialect(FBDialect())
Exemplo n.º 20
0
 def process(value):
     if value is not None:
         return DialectManager.get_spatial_dialect(
             dialect).process_result(value, self)
     else:
         return value