5
5
import psycopg2 as pg
6
6
import row_processor as Processor
7
7
import six
8
+ import json
8
9
9
10
# Special rules needed for certain tables (esp. for old database dumps)
10
11
specialRules = {
@@ -15,17 +16,17 @@ def _makeDefValues(keys):
15
16
"""Returns a dictionary containing None for all keys."""
16
17
return dict (( (k , None ) for k in keys ))
17
18
18
- def _createMogrificationTemplate (table , keys ):
19
+ def _createMogrificationTemplate (table , keys , insertJson ):
19
20
"""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 ):
29
30
"""Use the cursor to mogrify a tuple of data.
30
31
The passed data in `attribs` is augmented with default data (NULLs) and the
31
32
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):
34
35
"""
35
36
defs = _makeDefValues (keys )
36
37
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 )
37
46
return cursor .mogrify (templ , defs )
38
47
39
- def handleTable (table , keys , dbname , mbDbFile , mbHost , mbPort , mbUsername , mbPassword ):
48
+ def handleTable (table , keys , insertJson , dbname , mbDbFile , mbHost , mbPort , mbUsername , mbPassword ):
40
49
"""Handle the table including the post/pre processing."""
41
50
dbFile = mbDbFile if mbDbFile is not None else table + '.xml'
42
- tmpl = _createMogrificationTemplate (table , keys )
51
+ tmpl = _createMogrificationTemplate (table , keys , insertJson )
43
52
start_time = time .time ()
44
53
45
54
try :
@@ -82,7 +91,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
82
91
six .print_ ('Processing data ...' )
83
92
for rows in Processor .batch (Processor .parse (xml ), 500 ):
84
93
valuesStr = ',\n ' .join (
85
- [ _createCmdTuple (cur , keys , tmpl , row_attribs ).decode ('utf-8' )
94
+ [ _createCmdTuple (cur , keys , tmpl , row_attribs , insertJson ).decode ('utf-8' )
86
95
for row_attribs in rows
87
96
]
88
97
)
@@ -159,6 +168,11 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
159
168
, default = False
160
169
)
161
170
171
+ parser .add_argument ( '-j' , '--insert-json'
172
+ , help = 'Insert raw data as JSON.'
173
+ , action = 'store_true'
174
+ , default = False
175
+ )
162
176
args = parser .parse_args ()
163
177
164
178
table = args .table
@@ -279,7 +293,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
279
293
choice = input ('This will drop the {} table. Are you sure [y/n]?' .format (table ))
280
294
281
295
if 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 )
283
297
else :
284
298
six .print_ ("Cancelled." )
285
299
0 commit comments