55import psycopg2 as pg
66import row_processor as Processor
77import six
8+ import json
89
910# Special rules needed for certain tables (esp. for old database dumps)
1011specialRules = {
@@ -15,17 +16,17 @@ def _makeDefValues(keys):
1516 """Returns a dictionary containing None for all keys."""
1617 return dict (( (k , None ) for k in keys ))
1718
18- def _createMogrificationTemplate (table , keys ):
19+ def _createMogrificationTemplate (table , keys , insertJson ):
1920 """Return the template string for mogrification for the given keys."""
20- return ( ' (' +
21- ', ' . join ( [ '%(' + k + ')s' if ( table , k ) not in specialRules else specialRules [table , k ]
22- for k in keys
23- ]
24- ) +
25- ')'
26- )
27-
28- def _createCmdTuple (cursor , keys , templ , attribs ):
21+ table_keys = ', ' . join ( [ '% (' + k + ')s' if ( table , k ) not in specialRules
22+ else specialRules [table , k ]
23+ for k in keys ])
24+ if insertJson :
25+ return ( '(' + table_keys + ', %(jsonfield)s' + ')' )
26+ else :
27+ return ( '(' + table_keys + ')' )
28+
29+ def _createCmdTuple (cursor , keys , templ , attribs , insertJson ):
2930 """Use the cursor to mogrify a tuple of data.
3031 The passed data in `attribs` is augmented with default data (NULLs) and the
3132 order of data in the tuple is the same as in the list of `keys`. The
@@ -34,12 +35,20 @@ def _createCmdTuple(cursor, keys, templ, attribs):
3435 """
3536 defs = _makeDefValues (keys )
3637 defs .update (attribs )
38+
39+ if insertJson :
40+ dict_attribs = { }
41+ for name , value in attribs .items ():
42+ dict_attribs [name ] = value
43+ defs ['jsonfield' ] = json .dumps (dict_attribs )
44+
45+ values_to_insert = cursor .mogrify (templ , defs )
3746 return cursor .mogrify (templ , defs )
3847
39- def handleTable (table , keys , dbname , mbDbFile , mbHost , mbPort , mbUsername , mbPassword ):
48+ def handleTable (table , keys , insertJson , dbname , mbDbFile , mbHost , mbPort , mbUsername , mbPassword ):
4049 """Handle the table including the post/pre processing."""
4150 dbFile = mbDbFile if mbDbFile is not None else table + '.xml'
42- tmpl = _createMogrificationTemplate (table , keys )
51+ tmpl = _createMogrificationTemplate (table , keys , insertJson )
4352 start_time = time .time ()
4453
4554 try :
@@ -82,7 +91,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
8291 six .print_ ('Processing data ...' )
8392 for rows in Processor .batch (Processor .parse (xml ), 500 ):
8493 valuesStr = ',\n ' .join (
85- [ _createCmdTuple (cur , keys , tmpl , row_attribs ).decode ('utf-8' )
94+ [ _createCmdTuple (cur , keys , tmpl , row_attribs , insertJson ).decode ('utf-8' )
8695 for row_attribs in rows
8796 ]
8897 )
@@ -159,6 +168,11 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
159168 , default = False
160169 )
161170
171+ parser .add_argument ( '-j' , '--insert-json'
172+ , help = 'Insert raw data as JSON.'
173+ , action = 'store_true'
174+ , default = False
175+ )
162176args = parser .parse_args ()
163177
164178table = args .table
@@ -279,7 +293,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
279293choice = input ('This will drop the {} table. Are you sure [y/n]?' .format (table ))
280294
281295if len (choice ) > 0 and choice [0 ].lower () == 'y' :
282- handleTable (table , keys , args .dbname , args .file , args .host , args .port , args .username , args .password )
296+ handleTable (table , keys , args .insert_json , args . dbname , args .file , args .host , args .port , args .username , args .password )
283297else :
284298 six .print_ ("Cancelled." )
285299
0 commit comments