Call Microsoft SQL Server static methods with double colons

156 views
Skip to first unread message

Sebastian Eckweiler

unread,
Nov 26, 2019, 10:45:16 AM11/26/19
to sqlalchemy
Hi there,

I'm trying to do with Microsoft SQL Server what this snippet:
does for PostGIS.

Unfortunately in MSSQL "ST_GeomFromText" does not exist in the default function namespace, but is a static method of the geography type and is expected to be called as

geography::STGeomFromText


I tried several things in "bind_expression" but didn't manage to get sqlalchemy to compile anything into "geography::STGeomFromText".
When using "func.geography.STGeomFromText" this is ends up in SQL with a plain "." as a separation.
All attempts at injecting the double colons literally failed because the prefix is then wrapped in quotes.

Am I missing something here or do I have to (e.g.) use a custom dialect for that?

Thanks & cheers
Sebastian

Mike Bayer

unread,
Nov 26, 2019, 11:33:21 AM11/26/19
to noreply-spamdigest via sqlalchemy
the double colon does not appear to be necessarily a "separator" but various forum posts and whatnot seem to suggest it is some kind of namespace qualifier, it seems to be referred towards heavily in SQL Server 2000 and was somehow changed in 2005, but for this particular extension their current documentation is still referring towards it.     

in the official docs for 2019 we see it called the "scope resolution operator":


it's not really clear how this should be implemented in SQLAlchemy, as I'm not really sure "double colon" outright replaces the dot for function namespace qualifiers.    

If SQL Server can tolerate whitespace between the :: and the name, this recipe will work for now:

from sqlalchemy.sql.expression import UnaryExpression
from sqlalchemy.sql import operators
from sqlalchemy import func


def geometry(fn):
    return UnaryExpression(fn, operator=operators.custom_op("geometry::"))


expr = geometry(func.STGeomFromText())

print(expr)


generates:

    geometry:: STGeomFromText()


if not then we need to build some custom @compiles for that right now.

longer term I think we either need a new namespace added to the SQL Server dialect or we need additional options on the func. namespace to simulate this effect.






Thanks & cheers
Sebastian


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Sebastian Eckweiler

unread,
Nov 27, 2019, 2:02:24 AM11/27/19
to sqlalchemy
Hi

thanks for the quick reply - this works just fine. The whitespace appears to be okay.
It also helped solving the next issue of calling functions with method-like dot-notation like in:
To unsubscribe from this group and stop receiving emails from it, send an email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages