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?

4
  • What SQL do you want to get in result? (I'm the current maintainer of SQLObject.) Commented Mar 2, 2022 at 13:28
  • I want sql like "Select * from table1 where column1 & 2 = 2 ". Can this be done ? Commented Mar 2, 2022 at 14:42
  • 1
    Not with q-magic: it always maps 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 Commented Mar 2, 2022 at 15:10
  • SQLConstant did the trick. Thanks @phd for help! Commented Mar 3, 2022 at 6:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.