On 03/07/2016 09:11 AM, Frazer McLean wrote:
||I am trying to add regexp and iregexp operators to a subclass of the
Text type. I'm using SQLAlchemy v1.0.12.

I am able to correctly use '~' and '!~' operators if I use a notregexp
method like this:

|
importsqlalchemy.types astypes

classTEXT(types.Text):
classComparator(types.Text.Comparator):
defregexp(self,other):
returnself.op('~')(other)

defnotregexp(self,other):
returnself.op('!~')(other)

     comparator_factory =Comparator
|

However, I would like to correctly emit the '!~' operator if I use not_
or ~ in SQLAlchemy, so I tried to do the following:

|
importsqlalchemy.types astypes
fromsqlalchemy.sql.expression importBinaryExpression
fromsqlalchemy.sql importoperators

classTEXT(types.Text):
classComparator(types.Text.Comparator):
defregexp(self,other):
returnBinaryExpression(
self,other,operator=operators.custom_op('~'),type_=TEXT,
                 negate=operators.custom_op('!~'))

     comparator_factory =Comparator


that's the correct approach.


|

But I can't seem to get that to work.


can you provide a complete http://stackoverflow.com/help/mcve and I'll make it work ? thanks


 I was attempting to copy the
UnaryExpression example here
<http://docs.sqlalchemy.org/en/latest/core/custom_types.html?highlight=userdefinedtype#redefining-and-creating-new-operators>.

I see the patch on BitBucket
<https://bitbucket.org/zzzeek/sqlalchemy/issues/1390/postgresql-regular-expression-operators>
for adding these operators, but I'm trying to do the same with the
public API. Is it possible?

Thanks,

Frazer McLean

--
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+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to