cool_query.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import model
  2. from client_controller import _my_tabulate
  3. model.connect()
  4. # model.cursor.execute('''
  5. # EXPLAIN QUERY PLAN
  6. # SELECT users.rowid
  7. # FROM sessions, users
  8. # WHERE sessions.session_id = ?
  9. # AND users.rowid = sessions.user_id
  10. # '''.replace('?','1'))
  11. model.cursor.execute('''
  12. SELECT buy_order.*, sell_order.*, buyer.user_id, seller.user_id, buy_order.rowid, sell_order.rowid
  13. FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
  14. WHERE buy_order.buy AND NOT sell_order.buy
  15. AND buyer.rowid = buy_order.ownership_id
  16. AND seller.rowid = sell_order.ownership_id
  17. AND buyer.ownable_id = ?
  18. AND seller.ownable_id = ?
  19. AND (buy_order."limit" IS NULL
  20. OR sell_order."limit" IS NULL
  21. OR (sell_order."limit" <= buy_order."limit"
  22. AND NOT sell_order.stop_loss
  23. AND NOT buy_order.stop_loss))
  24. ORDER BY CASE WHEN sell_order."limit" IS NULL THEN 0 ELSE 1 END ASC,
  25. CASE WHEN buy_order."limit" IS NULL THEN 0 ELSE 1 END ASC,
  26. buy_order."limit" DESC,
  27. sell_order."limit" ASC,
  28. buy_order.ordered_amount - buy_order.executed_amount DESC,
  29. sell_order.ordered_amount - sell_order.executed_amount DESC
  30. LIMIT 1
  31. ''', (4, 4,))
  32. print(_my_tabulate(model.cursor.fetchall(),tablefmt='pipe'))