1

I am trying to create a query in python3 using sqlobject mapper. I have a requirement of filtering a custom field based on the output of bitise operation. Query looks like this:

query = sqlobject.AND(query,dl.<table_object>.q.anomalyType & alert_type == alert_type)

But when the actual SQL transformation happens, this bitwise & gets converted into "SQL AND", which changes the whole meaning of the query.

Can someone advise the right way to add bitwise operator in the query sqlobject in python3?

phd
  • 82,685
  • 13
  • 120
  • 165
Rajat Doshi
  • 118
  • 7
  • What SQL do you want to get in result? (I'm the current maintainer of SQLObject.) – phd Mar 02 '22 at 13:28
  • I want sql like "Select * from table1 where column1 & 2 = 2 ". Can this be done ? – Rajat Doshi Mar 02 '22 at 14:42
  • 1
    Not with [q-magic](http://sqlobject.org/SQLObject.html#q-magic): it always [maps](http://sqlobject.org/_modules/sqlobject/sqlbuilder.html#SQLExpression) `Table.q.x & y` to `x AND y`. Perhaps you can drop to simple strings: `'anomalyType & alert_type == alert_type'` (note apostrophes). May be `SQLConstant('anomalyType & alert_type == alert_type')` or even `SQLConstant('anomalyType & alert_type') == alert_type` – phd Mar 02 '22 at 15:10
  • SQLConstant did the trick. Thanks @phd for help! – Rajat Doshi Mar 03 '22 at 06:56

1 Answers1

2

SQLConstant does the trick. Working sql query looks like this:

query = sqlobject.AND(query, sqlbuilder.SQLConstant(f'anomalyType & {alert_type}') == alert_type)

Thanks for the help @phd.

Rajat Doshi
  • 118
  • 7