def fetchall(self): # print "yea! fetchall", self.column_names if not self._have_unread_result(): raise mysql.connector.InterfaceError , "No result set to fetch from." rel = relation( self.column_names, [] ,self._rel_name) (rows, eof) = self._connection.get_rows() self._rowcount = len(rows) desc = self.description for i in xrange(0,self.rowcount): # res.append(self._row_to_python(rows[i])) r = list(rows[i]) for idx,v in enumerate(rows[i]): if desc[idx][1] in [ 254 , 253]: r[idx] = rows[i][idx].decode('utf8') elif desc[idx][1] in [ 10, 12 ]: # print r[idx] , str(rows[i][idx]) # date r[idx] = str(rows[i][idx]) # date elif desc[idx][1] in [ 3 ]: # print r[idx] , int(rows[i][idx]) # longint r[idx] = int(rows[i][idx]) # date # else: # print desc[idx][1] rel.add( r ) # self._row_to_python(r) ) self._handle_eof(eof) return rel
def db_execute_relation(cnx, sql_query, item_dict, required_fields, label, verbose_level_threshold=2): """db_execute is GetDR, sqlparse, cursor.execute, return rel.add( r ) for r in cursor and cursor.close """ sql_dict = GetDR(item_dict, required_fields, verbose_level_threshold=verbose_level_threshold) sql_query = sql_query % sql_dict s = sqlparse.format( sql_query, reindent=True, encoding='utf8') GPR.print_it2( label, s, verbose_level_threshold) # 4 try: cursor = cnx.cursor() cursor.execute( sql_query ) column_names = [column.name for column in cursor.description] rel = relation( column_names, [] , "pg_del_files") for r in cursor: l = list(r) l[2] = l[2].decode('utf8') # file_name l[4] = str(l[4]) rel.add( l ) cnx.commit() # r = [z for z in cursor] except cnx.ProgrammingError as err: if err.message == "no results to fetch": return None else: GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors except cnx.IntegrityError as err: if err.pgcode == "23505": # duplicate key GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors else: GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors except cnx.InternalError as err: #InternalError: current transaction is aborted, commands ignored until end of transaction block GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0) # always print errors except cnx.Error as err: GPR.print_it2( label , "%r (%d)" % (err.message , err.pgcode) , 0 ) # always print errors finally: cursor.close() return rel
def test_0700_relation(self): from files import databaseAndURLKeys, enumeratorURLKeys, \ GetNSFileAttributesOfItem, GetURLResourceValuesForKeys, pdt, NSURL # d = GetNSFileAttributesOfItem("/") # l = 'GetNSFileAttributesOfItem("/")' # pdt(l, d, 28) url = NSURL.fileURLWithPath_("/") item_dict = GetURLResourceValuesForKeys(url, enumeratorURLKeys) item_dict.update( { "NSURLTotalFileSizeKey": 0 }) # file size is zero for directories # print_dict_tall_z("item_dict", item_dict, 32 ) # NSURLIsDirectoryKey: 1 # NSURLContentModificationDateKey: 2013-02-20 10:10:12 +0000 # NSURLNameKey: u'Genie' # NSFileSystemFolderNumber: 1L # NSURLTypeIdentifierKey: u'public.volume' # NSURLIsVolumeKey: 1 # NSURLVolumeURLKey: file://localhost/ # NSURLCreationDateKey: 2011-07-02 21:02:54 +0000 # NSURLTotalFileSizeKey: 0 # NSURLPathKey: u'/' # NSFileSystemFileNumber: 2 # NSURLLocalizedTypeDescriptionKey: u'Volume' # Convert from item_dict (Cocoa) forms to something that the database DBI can convert from from Foundation import NSURLNameKey, NSURLTypeIdentifierKey d = {} for dk, fk in databaseAndURLKeys: if dk: if fk in [NSURLNameKey, NSURLTypeIdentifierKey]: d[dk] = item_dict[fk].encode('utf8') elif dk in ['file_create_date', 'file_mod_date']: d[dk] = str(item_dict[fk])[:-len(" +0000")] else: d[dk] = item_dict[fk] print_dict_tall_z("insert data", d, 32 ) # insert data: # # folder_id: 1L # file_id: 2 # file_size: 0 # file_create_date: '2011-07-02 21:02:54' # file_name: 'Genie' # file_uti: 'public.volume' # file_mod_date: '2013-02-20 10:10:12' from relations.relation import relation # test relatin frm dictionary r1 = relation( d.keys() , [d] , "files") r2 = relation( d.keys() , (), "files") r2.add(d) print r1 == r2 # print [r for r in r1] add_file_sql = ("insert into files " " (folder_id, file_name, file_id, file_size, file_create_date, file_mod_date, file_uti) " " values ( %(folder_id)s, %(file_name)s, %(file_id)s, %(file_size)s, %(file_create_date)s, " " %(file_mod_date)s, %(file_uti)s ) " ); for t in r1: # print type(t) print add_file_sql % t._asdict()