Permalink
Browse files

#158 complete refactor of code to handle SQLAlchemy objects in todb/a…

…ppenddb/fromdb functions
  • Loading branch information...
1 parent 36d5eba commit 39d3ec126311602d3a234bd932f198fbc46fe97c @alimanfoo alimanfoo committed Jul 17, 2012
Showing with 396 additions and 142 deletions.
  1. +67 −12 docs/dbexamples.py
  2. +329 −130 src/petl/io.py
View
@@ -8,19 +8,37 @@
user = sys.argv[1]
passwd = sys.argv[2]
+
+# set up logging
+import logging
+logger = logging.getLogger('petl.io')
+logger.setLevel(logging.DEBUG)
+ch = logging.StreamHandler()
+ch.setLevel(logging.DEBUG)
+formatter = logging.Formatter('[%(levelname)s] %(name)s - %(funcName)s - %(message)s')
+ch.setFormatter(formatter)
+logger.addHandler(ch)
+
+
import MySQLdb
# assume database petl_test already created
connection = MySQLdb.connect(user=user, passwd=passwd, db='petl_test')
-print 'setup table'
+
+def prompt(msg):
+ i = raw_input(msg + '? ([y]/n)\n')
+ if i not in ('', 'y', 'Y'):
+ sys.exit(0)
+
+prompt('setup table')
cursor = connection.cursor()
# deal with quote compatibility
cursor.execute('SET SQL_MODE=ANSI_QUOTES')
cursor.execute('DROP TABLE IF EXISTS test')
cursor.execute('CREATE TABLE test (foo TEXT, bar INT)')
-connection.commit()
cursor.close()
+connection.commit()
-print 'exercise the petl functions using a connection'
+prompt('exercise the petl functions using a connection')
from petl import look, fromdb, todb, appenddb
t1 = fromdb(connection, 'SELECT * FROM test')
print look(t1)
@@ -36,7 +54,7 @@
print look(t1)
ieq(t2, t1)
-print 'exercise the petl functions using a cursor'
+prompt('exercise the petl functions using a cursor')
cursor = connection.cursor()
todb(t2, cursor, 'test')
print look(t1)
@@ -48,30 +66,67 @@
print look(t1)
ieq(t2, t1)
-print 'exercise using sqlalchemy engine'
from sqlalchemy import create_engine
engine = create_engine('mysql://%s:%s@localhost/petl_test' % (user, passwd))
-# deal with quote compatibility
+
+prompt('exercise using sqlalchemy connection')
+connection = engine.connect()
+connection.execute('SET SQL_MODE=ANSI_QUOTES')
+prompt('fromdb')
+t1 = fromdb(connection, 'SELECT * FROM test')
+print look(t1)
+ieq(t2, t1)
+prompt('appenddb')
+appenddb(t2, connection, 'test')
+prompt('look')
+print look(t1)
+ieq(t2app, t1)
+prompt('todb')
+todb(t2, connection, 'test')
+prompt('look')
+print look(t1)
+ieq(t2, t1)
+
+prompt('exercise using sqlalchemy engine')
+prompt('fromdb')
+# N.B., I'm not sure why we have to set SQL_MODE multiple times here, this
+# means that using an engine directly is probably not recommended, especially
+# where other statements have to be executed to ensure SQL-92 quote character
+# compatibility
engine.execute('SET SQL_MODE=ANSI_QUOTES')
-t1 = fromdb(engine, 'SELECT * FROM test')
+t1 = fromdb(engine, 'SELECT * FROM "test"')
print look(t1)
ieq(t2, t1)
+prompt('appenddb')
appenddb(t2, engine, 'test')
+prompt('look')
+engine.execute('SET SQL_MODE=ANSI_QUOTES')
print look(t1)
ieq(t2app, t1)
+prompt('todb')
todb(t2, engine, 'test')
+prompt('look')
+engine.execute('SET SQL_MODE=ANSI_QUOTES')
print look(t1)
ieq(t2, t1)
-print 'exercise using sqlalchemy connection'
-connection = engine.connect()
-t1 = fromdb(connection, 'SELECT * FROM test')
+
+prompt('exercise using sqlalchemy session')
+prompt('fromdb')
+from sqlalchemy.orm import sessionmaker
+Session = sessionmaker(bind=engine)
+session = Session()
+t1 = fromdb(session, 'SELECT * FROM test')
print look(t1)
ieq(t2, t1)
-appenddb(t2, connection, 'test')
+prompt('appenddb')
+appenddb(t2, session, 'test')
+prompt('look')
print look(t1)
ieq(t2app, t1)
-todb(t2, connection, 'test')
+prompt('todb')
+todb(t2, session, 'test')
+prompt('look')
print look(t1)
ieq(t2, t1)
Oops, something went wrong.

0 comments on commit 39d3ec1

Please sign in to comment.