cool_query.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import model
  2. from client_controller import _my_tabulate
  3. model.connect()
  4. model.cursor.execute('''
  5. EXPLAIN QUERY PLAN
  6. SELECT * FROM (
  7. SELECT buy_order.*, sell_order.*, buyer.user_id, seller.user_id, buy_order.rowid, sell_order.rowid
  8. FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
  9. WHERE buy_order.buy AND NOT sell_order.buy
  10. AND buyer.rowid = buy_order.ownership_id
  11. AND seller.rowid = sell_order.ownership_id
  12. AND buyer.ownable_id = ?
  13. AND seller.ownable_id = ?
  14. AND buy_order."limit" IS NULL
  15. AND sell_order."limit" IS NULL
  16. ORDER BY buy_order.rowid ASC,
  17. sell_order.rowid ASC
  18. LIMIT 1)
  19. UNION ALL -- best buy orders
  20. SELECT * FROM (
  21. SELECT buy_order.*, sell_order.*, buyer.user_id, seller.user_id, buy_order.rowid, sell_order.rowid
  22. FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
  23. WHERE buy_order.buy AND NOT sell_order.buy
  24. AND buyer.rowid = buy_order.ownership_id
  25. AND seller.rowid = sell_order.ownership_id
  26. AND buyer.ownable_id = ?
  27. AND seller.ownable_id = ?
  28. AND buy_order."limit" IS NULL
  29. AND sell_order."limit" IS NOT NULL
  30. AND NOT sell_order.stop_loss
  31. ORDER BY sell_order."limit" ASC,
  32. buy_order.rowid ASC,
  33. sell_order.rowid ASC
  34. LIMIT 1)
  35. UNION ALL -- best sell orders
  36. SELECT * FROM (
  37. SELECT buy_order.*, sell_order.*, buyer.user_id, seller.user_id, buy_order.rowid, sell_order.rowid
  38. FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
  39. WHERE buy_order.buy AND NOT sell_order.buy
  40. AND buyer.rowid = buy_order.ownership_id
  41. AND seller.rowid = sell_order.ownership_id
  42. AND buyer.ownable_id = ?
  43. AND seller.ownable_id = ?
  44. AND buy_order."limit" IS NOT NULL
  45. AND NOT buy_order.stop_loss
  46. AND sell_order."limit" IS NULL
  47. ORDER BY buy_order."limit" DESC,
  48. buy_order.rowid ASC,
  49. sell_order.rowid ASC
  50. LIMIT 1)
  51. UNION ALL -- both limit orders
  52. SELECT * FROM (
  53. SELECT buy_order.*, sell_order.*, buyer.user_id, seller.user_id, buy_order.rowid, sell_order.rowid
  54. FROM orders buy_order, orders sell_order, ownership buyer, ownership seller
  55. WHERE buy_order.buy AND NOT sell_order.buy
  56. AND buyer.rowid = buy_order.ownership_id
  57. AND seller.rowid = sell_order.ownership_id
  58. AND buyer.ownable_id = ?
  59. AND seller.ownable_id = ?
  60. AND buy_order."limit" IS NOT NULL
  61. AND sell_order."limit" IS NOT NULL
  62. AND sell_order."limit" <= buy_order."limit"
  63. AND NOT sell_order.stop_loss
  64. AND NOT buy_order.stop_loss
  65. ORDER BY buy_order."limit" DESC,
  66. sell_order."limit" ASC,
  67. buy_order.rowid ASC,
  68. sell_order.rowid ASC
  69. LIMIT 1)
  70. LIMIT 1
  71. '''.replace('?','1'))
  72. # model.cursor.execute('''
  73. # SELECT 2
  74. # UNION ALL
  75. # SELECT * FROM (
  76. # WITH RECURSIVE ones(x) AS (VALUES(1) UNION ALL SELECT * FROM ones)
  77. # SELECT x FROM ones)
  78. # LIMIT 1
  79. # '''.replace('?','1'))
  80. print(_my_tabulate(model.cursor.fetchall(),tablefmt='pipe'))