@@ -1113,6 +1113,7 @@ def capture_provenance():
1113
1113
def push_provenance ():
1114
1114
pass
1115
1115
1116
+
1116
1117
class SQLiteSinkInputSpec (DynamicTraitedSpec , BaseInterfaceInputSpec ):
1117
1118
database_file = File (exists = True , mandatory = True )
1118
1119
table_name = traits .Str (mandatory = True )
@@ -1158,3 +1159,61 @@ def _list_outputs(self):
1158
1159
conn .commit ()
1159
1160
c .close ()
1160
1161
return None
1162
+
1163
+
1164
+ class MySQLSinkInputSpec (DynamicTraitedSpec , BaseInterfaceInputSpec ):
1165
+ host = traits .Str ('localhost' , mandatory = True ,
1166
+ requires = ['username' , 'password' ],
1167
+ xor = ['config' ], usedefault = True )
1168
+ config = File (mandatory = True , xor = ['host' ], desc = "MySQL Options File (same format as my.cnf)" )
1169
+ database_name = traits .Str (mandatory = True , desc = 'Otherwise known as the schema name' )
1170
+ table_name = traits .Str (mandatory = True )
1171
+ username = traits .Str ()
1172
+ password = traits .Str ()
1173
+
1174
+
1175
+ class MySQLSink (IOBase ):
1176
+ """ Very simple frontend for storing values into MySQL database.
1177
+
1178
+ Examples
1179
+ --------
1180
+
1181
+ >>> sql = MySQLSink(input_names=['subject_id', 'some_measurement'])
1182
+ >>> sql.inputs.database_name = 'my_database'
1183
+ >>> sql.inputs.table_name = 'experiment_results'
1184
+ >>> sql.inputs.username = 'root'
1185
+ >>> sql.inputs.password = 'secret'
1186
+ >>> sql.inputs.subject_id = 's1'
1187
+ >>> sql.inputs.some_measurement = 11.4
1188
+ >>> sql.run() # doctest: +SKIP
1189
+
1190
+ """
1191
+ input_spec = MySQLSinkInputSpec
1192
+
1193
+ def __init__ (self , input_names , ** inputs ):
1194
+
1195
+ super (MySQLSink , self ).__init__ (** inputs )
1196
+
1197
+ self ._input_names = filename_to_list (input_names )
1198
+ add_traits (self .inputs , [name for name in self ._input_names ])
1199
+
1200
+ def _list_outputs (self ):
1201
+ """Execute this module.
1202
+ """
1203
+ import MySQLdb
1204
+ if isdefined (self .inputs .config ):
1205
+ conn = MySQLdb .connect (db = self .inputs .database_name ,
1206
+ read_default_file = self .inputs .config )
1207
+ else :
1208
+ conn = MySQLdb .connect (host = self .inputs .host ,
1209
+ user = self .inputs .username ,
1210
+ passwd = self .inputs .password ,
1211
+ db = self .inputs .database_name )
1212
+ c = conn .cursor ()
1213
+ c .execute ("REPLACE INTO %s (" % self .inputs .table_name +
1214
+ "," .join (self ._input_names ) + ") VALUES (" +
1215
+ "," .join (["%s" ] * len (self ._input_names )) + ")" ,
1216
+ [getattr (self .inputs , name ) for name in self ._input_names ])
1217
+ conn .commit ()
1218
+ c .close ()
1219
+ return None
0 commit comments