def _addChangeToDatabase(self, change, cursor): # strip None from any of these values, just in case def remove_none(x): if x is None: return "" return x values = tuple(remove_none(x) for x in (change.number, change.who, change.comments, change.isdir, change.branch, change.revision, change.revlink, change.when, change.category)) q = util.sql_insert(self.dbapi, 'changes', """changeid author comments is_dir branch revision revlink when_timestamp category""".split()) cursor.execute(q, values) for link in change.links: cursor.execute(util.sql_insert(self.dbapi, 'change_links', ('changeid', 'link')), (change.number, link)) for filename in change.files: cursor.execute(util.sql_insert(self.dbapi, 'change_files', ('changeid', 'filename')), (change.number, filename)) for propname,propvalue in change.properties.properties.items(): encoded_value = json.dumps(propvalue) cursor.execute(util.sql_insert(self.dbapi, 'change_properties', ('changeid', 'property_name', 'property_value')), (change.number, propname, encoded_value))
def test_unicode(self): # first, create a test table c = self.conn.cursor() c.execute("CREATE TABLE test_unicode (name VARCHAR(100))") q = util.sql_insert(self.dbapi, 'test_unicode', ["name"]) try: val = u"Frosty the \N{SNOWMAN}" c.execute(q, [val]) c.execute("SELECT * FROM test_unicode") row = c.fetchall()[0] if row[0] != val: raise UnicodeError("Your database doesn't support unicode data; for MySQL, set the default collation to utf8_general_ci.") finally: pass c.execute("DROP TABLE test_unicode")
def test_unicode(self): # first, create a test table c = self.conn.cursor() c.execute("CREATE TABLE test_unicode (`name` VARCHAR(100))") q = util.sql_insert(self.dbapi, 'test_unicode', ["name"]) try: val = u"Frosty the \N{SNOWMAN}" c.execute(q, [val]) c.execute("SELECT * FROM test_unicode") row = c.fetchall()[0] if row[0] != val: raise UnicodeError("Your database doesn't support unicode data; for MySQL, set the default collation to utf8_general_ci.") finally: pass c.execute("DROP TABLE test_unicode")
def test_sql_insert_multi_format(self): dbapi = FakeDBAPI('format') self.assertEqual(util.sql_insert(dbapi, 'widgets', ('len', 'wid')), "INSERT INTO widgets (len, wid) VALUES (%s,%s)")
def test_sql_insert_single_format(self): dbapi = FakeDBAPI('format') self.assertEqual(util.sql_insert(dbapi, 'colors', ('color', )), "INSERT INTO colors (color) VALUES (%s)")
def test_sql_insert_multi_numeric(self): dbapi = FakeDBAPI('numeric') self.assertEqual(util.sql_insert(dbapi, 'widgets', ('len', 'wid')), "INSERT INTO widgets (len, wid) VALUES (:1,:2)")
def test_sql_insert_single_numeric(self): dbapi = FakeDBAPI('numeric') self.assertEqual(util.sql_insert(dbapi, 'colors', ('color', )), "INSERT INTO colors (color) VALUES (:1)")
def test_sql_insert_multi_qmark(self): dbapi = FakeDBAPI('qmark') self.assertEqual(util.sql_insert(dbapi, 'widgets', ('len', 'wid')), "INSERT INTO widgets (len, wid) VALUES (?,?)")
def test_sql_insert_single_qmark(self): dbapi = FakeDBAPI('qmark') self.assertEqual(util.sql_insert(dbapi, 'colors', ('color', )), "INSERT INTO colors (color) VALUES (?)")
def test_sql_insert_single_format(self): dbapi = FakeDBAPI('format') self.assertEqual(util.sql_insert(dbapi, 'colors', ('color',)), "INSERT INTO colors (color) VALUES (%s)")
def test_sql_insert_single_numeric(self): dbapi = FakeDBAPI('numeric') self.assertEqual(util.sql_insert(dbapi, 'colors', ('color',)), "INSERT INTO colors (color) VALUES (:1)")
def test_sql_insert_single_qmark(self): dbapi = FakeDBAPI('qmark') self.assertEqual(util.sql_insert(dbapi, 'colors', ('color',)), "INSERT INTO colors (color) VALUES (?)")
class Upgrader(base.Upgrader): def upgrade(self): self.test_unicode() self.add_tables() self.migrate_changes() self.set_version() def test_unicode(self): # first, create a test table c = self.conn.cursor() c.execute("CREATE TABLE test_unicode (`name` VARCHAR(100))") q = util.sql_insert(self.dbapi, 'test_unicode', ["name"]) try: val = u"Frosty the \N{SNOWMAN}" c.execute(q, [val]) c.execute("SELECT * FROM test_unicode") row = c.fetchall()[0] if row[0] != val: raise UnicodeError("Your database doesn't support unicode data; for MySQL, set the default collation to utf8_general_ci.") finally: pass c.execute("DROP TABLE test_unicode") def add_tables(self): # first, add all of the tables c = self.conn.cursor() for t in TABLES: try: c.execute(t) except: print >>sys.stderr, "error executing SQL query: %s" % t raise def _addChangeToDatabase(self, change, cursor): # strip None from any of these values, just in case def remove_none(x): if x is None: return u"" elif isinstance(x, str): return x.decode("utf8") else: return x try: values = tuple(remove_none(x) for x in (change.number, change.who, change.comments, change.isdir, change.branch, change.revision, change.revlink, change.when, change.category)) except UnicodeDecodeError, e: raise UnicodeError("Trying to import change data as UTF-8 failed. Please look at contrib/fix_changes_pickle_encoding.py: %s" % str(e)) q = util.sql_insert(self.dbapi, 'changes', """changeid author comments is_dir branch revision revlink when_timestamp category""".split()) cursor.execute(q, values) for link in change.links: cursor.execute(util.sql_insert(self.dbapi, 'change_links', ('changeid', 'link')), (change.number, link)) for filename in change.files: cursor.execute(util.sql_insert(self.dbapi, 'change_files', ('changeid', 'filename')), (change.number, filename)) for propname,propvalue in change.properties.properties.items(): encoded_value = json.dumps(propvalue) cursor.execute(util.sql_insert(self.dbapi, 'change_properties', ('changeid', 'property_name', 'property_value')), (change.number, propname, encoded_value))