Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import psycopg2
  2. import datetime
  3. import getpass
  4. db = raw_input("database name:")
  5. dbuser = raw_input("database username:")
  6. psw = getpass.getpass()
  7. conn = psycopg2.connect(host="localhost", user=dbuser, password=psw, dbname=db)
  8. cr = conn.cursor()
  9.  
  10. try:
  11. pas_id_str = "giscedata.switching.c2.05,{0}"
  12.  
  13. cr.execute("SELECT id FROM giscedata_switching_proces WHERE name = 'C2'")
  14. procesC2_id = cr.fetchone()[0]
  15.  
  16. cr.execute("SELECT id FROM giscedata_switching_step "
  17. "WHERE name = '05' AND proces_id = %s", (procesC2_id, ))
  18. step05_id = cr.fetchone()[0]
  19.  
  20. cr.execute("SELECT swstep.id, header.sw_id "
  21. "from giscedata_switching_c2_07 AS swstep "
  22. "JOIN giscedata_switching_step_header AS header "
  23. "ON swstep.header_id=header.id")
  24.  
  25. for c207_id, sw_id in cr.fetchall():
  26. print "Copying C2-07 with id {0}".format(c207_id)
  27. cr.execute("SELECT * FROM giscedata_switching_c2_07 "
  28. "WHERE id=%s", (c207_id, ))
  29. info = cr.fetchone()
  30. info = info[5:]
  31. new_info = []
  32. for i in range(len(info)):
  33. if isinstance(info[i], datetime.date):
  34. new_info.append(info[i].strftime("%Y-%m-%d"))
  35. elif isinstance(info[i], datetime.datetime):
  36. new_info.append(info[i].strftime("%Y-%m-%d %H:%M:%S"))
  37. elif isinstance(info[i], datetime.time):
  38. new_info.append(info[i].strftime("%H:%M:%S"))
  39. else:
  40. new_info.append(info[i])
  41. new_info = ("{}".format(tuple(new_info))).replace("None", "Null")
  42. # Check if there is already a C205
  43. cr.execute("SELECT id FROM giscedata_switching_c2_05 "
  44. "WHERE header_id=%s", (info[6],))
  45. c205_id = cr.fetchone()
  46. if c205_id:
  47. print " * There is already a C2-05 case with id {0}".format(c205_id[0])
  48. continue
  49. # Create C2-05 with C2-07 info
  50. cr.execute('INSERT INTO giscedata_switching_c2_05 '
  51. '(tipus_activacio, data_activacio, tipus_contracte, '
  52. 'control_potencia, contracte_atr, periodicitat_facturacio, '
  53. 'header_id, "tarifaATR", hora_activacio, tipus_autoconsum, '
  54. 'tipus_telegestio, marca_medida_bt, kvas_trafo)'
  55. 'VALUES {0}'.format(new_info))
  56. # Get id of new C2-05
  57. cr.execute("SELECT id FROM giscedata_switching_c2_05 "
  58. "WHERE header_id=%s", (info[6], ))
  59. c205_id = cr.fetchone()[0]
  60. # Create step_info which relates SW case with C2-05 step
  61. cr.execute("INSERT INTO giscedata_switching_step_info (step_id, sw_id, proces_id, pas_id)"
  62. "VALUES (%s,%s,%s,%s)",
  63. (step05_id, sw_id, procesC2_id, pas_id_str.format(c205_id)))
  64. # Update the step_id of SW if it is Null
  65. cr.execute("SELECT step_id FROM giscedata_switching "
  66. "WHERE id=%s", (sw_id,))
  67. step_id = cr.fetchone()
  68. if not step_id or (step_id and step_id[0] is None):
  69. cr.execute("UPDATE giscedata_switching SET step_id=%s WHERE id=%s",
  70. (step05_id, sw_id))
  71. print " * New C2-05 with id={0}, switching case id={1}".format(c205_id, sw_id)
  72.  
  73. conn.commit()
  74.  
  75. except Exception as e:
  76. conn.rollback()
  77. raise e
  78.  
  79. finally:
  80. cr.close()
  81. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement