Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "from sqlalchemy import create_engine\n",
  12. "from sqlalchemy.schema import MetaData, Table, Index, Column\n",
  13. "from sqlalchemy.sql import and_, or_, not_, select, text\n",
  14. "from sqlalchemy.types import Text, Integer, Float, String\n",
  15. "\n",
  16. "engine = create_engine('sqlite:///:memory:', echo=False)\n",
  17. "\n",
  18. "metadata = MetaData()\n",
  19. "\n",
  20. "user = Table('user', metadata,\n",
  21. " Column('user_id', Integer, primary_key=True),\n",
  22. " Column('user_name', String(16), nullable=False),\n",
  23. " Column('email_address', String(60)),\n",
  24. " Column('password', String(20), nullable=False)\n",
  25. ")\n",
  26. "user.create(engine)\n",
  27. "\n",
  28. "with engine.connect() as conn:\n",
  29. " for name in ('jack', 'james', 'jenny'):\n",
  30. " email = '{name}@gmail.com'.format(name=name)\n",
  31. " ins = user.insert().values(user_name=name, email_address=email, password=\"bad\")\n",
  32. " result = conn.execute(ins) "
  33. ]
  34. },
  35. {
  36. "cell_type": "code",
  37. "execution_count": 2,
  38. "metadata": {},
  39. "outputs": [
  40. {
  41. "name": "stdout",
  42. "output_type": "stream",
  43. "text": [
  44. "Full results: [(1, 'jack', 'jack@gmail.com', 'bad'), (2, 'james', 'james@gmail.com', 'bad'), (3, 'jenny', 'jenny@gmail.com', 'bad')]\n"
  45. ]
  46. }
  47. ],
  48. "source": [
  49. "with engine.connect() as conn:\n",
  50. " query = select([user])\n",
  51. " found = conn.execute(query).fetchall()\n",
  52. "\n",
  53. "print(\"Full results: {results}\".format(results=found))"
  54. ]
  55. },
  56. {
  57. "cell_type": "code",
  58. "execution_count": 3,
  59. "metadata": {},
  60. "outputs": [
  61. {
  62. "name": "stdout",
  63. "output_type": "stream",
  64. "text": [
  65. "SQL: SELECT \"user\".user_id, \"user\".user_name, \"user\".email_address, \"user\".password \n",
  66. "FROM \"user\" \n",
  67. "WHERE \"user\".user_name = :user_name_1 OR \"user\".user_name = :user_name_2\n",
  68. "\n",
  69. "Returns: [(1, 'jack', 'jack@gmail.com', 'bad'), (2, 'james', 'james@gmail.com', 'bad')].\n",
  70. "That makes sense.\n",
  71. "\n"
  72. ]
  73. }
  74. ],
  75. "source": [
  76. "query_with_disjunction = query.where(or_(user.c.user_name == \"jack\", user.c.user_name == \"james\"))\n",
  77. "\n",
  78. "print(\"SQL: {}\".format(str(query_with_disjunction)))\n",
  79. "\n",
  80. "with engine.connect() as conn:\n",
  81. " found = conn.execute(query_with_disjunction).fetchall()\n",
  82. " \n",
  83. "print(\"\\nReturns: {results}.\\nThat makes sense.\\n\".format(results=found))"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 4,
  89. "metadata": {},
  90. "outputs": [
  91. {
  92. "name": "stdout",
  93. "output_type": "stream",
  94. "text": [
  95. "SQL: SELECT \"user\".user_id, \"user\".user_name, \"user\".email_address, \"user\".password \n",
  96. "FROM \"user\" \n",
  97. "WHERE NOT (\"user\".user_name = :user_name_1 OR \"user\".user_name = :user_name_2)\n",
  98. "\n",
  99. "Returns: [(3, 'jenny', 'jenny@gmail.com', 'bad')]\n",
  100. "That makes sense.\n",
  101. "\n"
  102. ]
  103. }
  104. ],
  105. "source": [
  106. "disjunction = or_(user.c.user_name == \"jack\", user.c.user_name == \"james\")\n",
  107. "query_with_negated_disjunction = query.where(not_(disjunction))\n",
  108. "\n",
  109. "print(\"SQL: {}\".format(str(query_with_negated_disjunction)))\n",
  110. "\n",
  111. "with engine.connect() as conn:\n",
  112. " found = conn.execute(query_with_negated_disjunction).fetchall()\n",
  113. " \n",
  114. "print(\"\\nReturns: {results}\\nThat makes sense.\\n\".format(results=found))"
  115. ]
  116. },
  117. {
  118. "cell_type": "code",
  119. "execution_count": 8,
  120. "metadata": {},
  121. "outputs": [
  122. {
  123. "name": "stdout",
  124. "output_type": "stream",
  125. "text": [
  126. "Produces the SQL: SELECT \"user\".user_id, \"user\".user_name, \"user\".email_address, \"user\".password \n",
  127. "FROM \"user\" \n",
  128. "WHERE NOT \"user\".user_name = :user_name_1 OR \"user\".user_name = :user_name_2\n",
  129. ".Notice that the 'not' does NOT put parentheses around the disjunction\n",
  130. "\n",
  131. "Returns: [(1, 'jack', 'jack@gmail.com', 'bad'), (3, 'jenny', 'jenny@gmail.com', 'bad')]\n",
  132. "\n",
  133. "That doesn't make sense for 2 reasons: 1) we'd expect the same result as the unlabelled disjunction, i.e. just jenny and 2) even if it's correct to apply the 'not' only to the first clause, the result should be james and jenny, not jack and jenny.\n"
  134. ]
  135. }
  136. ],
  137. "source": [
  138. "disjunction_with_label = disjunction.label(\"labelled_disjunction\")\n",
  139. "\n",
  140. "query_with_negated_disjunction_with_label = query.where(not_(disjunction_with_label))\n",
  141. "\n",
  142. "print(\"Produces the SQL: {}\\n.Notice that the 'not' does NOT put parentheses around the disjunction\".format(str(query_with_negated_disjunction_with_label)))\n",
  143. "\n",
  144. "with engine.connect() as conn:\n",
  145. " found = conn.execute(query_with_negated_disjunction_with_label).fetchall()\n",
  146. " \n",
  147. "print(\"\\nReturns: {results}\\n\\nThat doesn't make sense for 2 reasons: 1) we'd expect the same result as the unlabelled disjunction, i.e. just jenny and 2) even if it's correct to apply the 'not' only to the first clause, the result should be james and jenny, not jack and jenny.\".format(results=found))"
  148. ]
  149. },
  150. {
  151. "cell_type": "code",
  152. "execution_count": 6,
  153. "metadata": {},
  154. "outputs": [
  155. {
  156. "name": "stdout",
  157. "output_type": "stream",
  158. "text": [
  159. "If we pre-bind the variables and run directly, we get james and jenny:\n"
  160. ]
  161. }
  162. ],
  163. "source": [
  164. "literal_query = str(query_with_negated_disjunction_with_label.compile(compile_kwargs={\"literal_binds\": True}))\n",
  165. "\n",
  166. "print(\"If we pre-bind the variables and run directly, we get james and jenny:\")"
  167. ]
  168. },
  169. {
  170. "cell_type": "code",
  171. "execution_count": 7,
  172. "metadata": {},
  173. "outputs": [
  174. {
  175. "data": {
  176. "text/plain": [
  177. "\"[(2, 'james', 'james@gmail.com', 'bad'), (3, 'jenny', 'jenny@gmail.com', 'bad')]\""
  178. ]
  179. },
  180. "execution_count": 7,
  181. "metadata": {},
  182. "output_type": "execute_result"
  183. }
  184. ],
  185. "source": [
  186. "with engine.connect() as conn:\n",
  187. " res = conn.execute(literal_query).fetchall()\n",
  188. "str(res)"
  189. ]
  190. }
  191. ],
  192. "metadata": {
  193. "kernelspec": {
  194. "display_name": "Python 3",
  195. "language": "python",
  196. "name": "python3"
  197. },
  198. "language_info": {
  199. "codemirror_mode": {
  200. "name": "ipython",
  201. "version": 3
  202. },
  203. "file_extension": ".py",
  204. "mimetype": "text/x-python",
  205. "name": "python",
  206. "nbconvert_exporter": "python",
  207. "pygments_lexer": "ipython3",
  208. "version": "3.5.2"
  209. }
  210. },
  211. "nbformat": 4,
  212. "nbformat_minor": 2
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement