1
0
Fork 0
mirror of https://github.com/veekun/pokedex.git synced 2024-08-20 18:16:34 +00:00

Dynamically mangle long table names for Oracle; Unicode → UnicodeText.

This commit is contained in:
Epithumia 2013-12-18 14:32:13 +01:00 committed by Lynn "Zhorken" Vaughan
parent bf2c79879d
commit 7b2743be75
6 changed files with 84 additions and 11 deletions
pokedex/db

View file

@ -11,6 +11,7 @@ import pokedex
from pokedex.db import metadata, tables, translations
from pokedex.defaults import get_default_csv_dir
from pokedex.db.dependencies import find_dependent_tables
from pokedex.db.oracle import rewrite_long_table_names
def _get_table_names(metadata, patterns):
@ -138,6 +139,14 @@ def load(session, tables=[], directory=None, drop_tables=False, verbose=False, s
table_names = _get_table_names(metadata, tables)
table_objs = [metadata.tables[name] for name in table_names]
# Oracle fixery, load needs short names
# flag for oracle stuff
oranames = (session.connection().dialect.name == 'oracle')
if oranames:
# Shorten table names, Oracle limits table and column names to 30 chars
# Make a dictionary to match old<->new names
oradict = rewrite_long_table_names()
if recursive:
table_objs.extend(find_dependent_tables(table_objs))
@ -181,9 +190,11 @@ def load(session, tables=[], directory=None, drop_tables=False, verbose=False, s
insert_stmt = table_obj.insert()
print_start(table_name)
try:
csvpath = "%s/%s.csv" % (directory, table_name)
# In oracle mode, use the original names instead of current
if oranames:
csvpath = "%s/%s.csv" % (directory, oradict[table_name])
csvfile = open(csvpath, 'rb')
except IOError:
# File doesn't exist; don't load anything!
@ -250,6 +261,9 @@ def load(session, tables=[], directory=None, drop_tables=False, verbose=False, s
for column_name, value in zip(column_names, csvs):
column = table_obj.c[column_name]
# Oracle treats empty strings as NULL
if not column.nullable and value == '' and oranames:
value = ' '
if column.nullable and value == '':
# Empty string in a nullable column really means NULL
value = None
@ -369,6 +383,11 @@ def dump(session, tables=[], directory=None, verbose=False, langs=['en']):
table_names = _get_table_names(metadata, tables)
table_names.sort()
# Oracle fixery : read from short table names, dump long names
oranames = (session.connection().dialect.name == 'oracle')
if oranames:
# Make a dictionary to match old<->new names
oradict = rewrite_long_table_names()
for table_name in table_names:
print_start(table_name)
@ -376,6 +395,10 @@ def dump(session, tables=[], directory=None, verbose=False, langs=['en']):
writer = csv.writer(open("%s/%s.csv" % (directory, table_name), 'wb'),
lineterminator='\n')
# In oracle mode, use the original names instead of current
if oranames:
writer = csv.writer(open("%s/%s.csv" % (directory, oradict[table_name]), 'wb'),
lineterminator='\n')
columns = [col.name for col in table.columns]
# For name tables, dump rows for official languages, as well as