Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. def check_column_existence(dbHost, dbPort, dbName, dbUser, dbPassword, table_name, column_name):
  2. sqlQuery="""select count(*) from
  3. INFORMATION_SCHEMA.columns
  4. where table_name='{}' and column_name='{}';""".format(table_name, column_name)
  5. conn = pymssql.connect(host=dbHost, port=dbPort, user=dbUser, password=dbPassword, database=dbName)
  6. cursor = conn.cursor()
  7. sql = sqlQuery.encode('utf-8')
  8. cursor.execute(sql)
  9. for row in cursor:
  10. if row[0] == 1:
  11. result = True
  12. else:
  13. result = False
  14. print(result)
  15. return result
  16.  
  17. def populate_db_attribute_existence(dbHost, dbPort, dbName, dbUser, dbPassword, input_csv_file_path):
  18. input_csv_file_path = os.path.abspath(input_csv_file_path)
  19. input_csv_folder_path = os.path.dirname(input_csv_file_path)
  20. input_csv_file_name = os.path.basename(input_csv_file_path)
  21. temp1_output_file_path = "{}/temp1_{}".format(input_csv_folder_path, input_csv_file_name)
  22. temp2_output_file_path = "{}/temp2_{}".format(input_csv_folder_path, input_csv_file_name)
  23.  
  24. df = pd.read_csv(input_csv_file_path)
  25. df['DB_Entity'] = df['DB_Entity'].str.replace(' ', '')
  26. df['DB_Attributes'] = df['DB_Attributes'].str.replace(' ', '')
  27. df2 = df[['API_Attributes', 'DB_Entity', 'DB_Attributes']]
  28. df2.to_csv(temp1_output_file_path, index=False, encoding='utf-8')
  29.  
  30. with open(temp1_output_file_path,'r') as ftemp1, open(temp2_output_file_path,'w') as ftemp2:
  31. i = 1
  32. for row in ftemp1:
  33. if i > 1:
  34. row = row.strip()
  35. list_columns = row.split(',')
  36. api_attr_name = list_columns[0]
  37. db_entity_name = list_columns[1]
  38. c_name = list_columns[2]
  39. t_name = 'dbo.{}'.format(db_entity_name)
  40. result = check_column_existence(dbHost, dbPort, dbName, dbUser, dbPassword, t_name, c_name)
  41. ftemp2.write(','.join([api_attr_name, db_entity_name, c_name, str(result)])+'n')
  42. i += 1
  43.  
  44.  
  45.  
  46. del(df2)
  47. df2 = pd.read_csv(temp2_output_file_path, names=['API_Attributes', 'DB_Entity', 'DB_Attributes', 'DB_Attr_Exist'])
  48. merge_key = ['API_Attributes', 'DB_Attributes']
  49. merged_df = pd.merge(df, df2, on=merge_key, how='outer')
  50. merged_df.to_csv(input_csv_file_path, index=False, encoding='utf-8')
  51. os.remove(temp1_output_file_path)
  52. os.remove(temp2_output_file_path)
  53. print("Completed DB Attributes existence check: {}".format(input_csv_file_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement