Guest User

Untitled

a guest
Jan 29th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. """
  2.  
  3. setup:
  4. pip3 install PyMySQL
  5. """
  6.  
  7. import sys
  8. import pymysql.cursors
  9.  
  10. def printPrompt():
  11. sys.stdout.write('>>')
  12. sys.stdout.flush()
  13.  
  14.  
  15. def getConnection():
  16. # Connect to the database
  17. connection = pymysql.connect(host='localhost',
  18. user='',
  19. password='',
  20. db='',
  21. charset='utf8mb4',
  22. cursorclass=pymysql.cursors.DictCursor)
  23. return connection
  24.  
  25. def backupData(commands):
  26. tableName = commands[0]
  27. print ("table=" + tableName)
  28.  
  29. connection = getConnection()
  30.  
  31. result = None
  32. with connection.cursor() as cursor:
  33. sql = "SELECT TABLE_NAME,TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME like %s"
  34. cursor.execute(sql, (tableName,))
  35. result = cursor.fetchone()
  36. if result == None:
  37. print("no such table")
  38. return
  39.  
  40. schemaName = result["TABLE_SCHEMA"]
  41. print(schemaName, tableName)
  42.  
  43. columnNames = []
  44. with connection.cursor() as cursor:
  45. sql = "SELECT COLUMN_NAME FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = %s AND `TABLE_NAME` = %s"
  46. cursor.execute(sql, (schemaName, tableName,))
  47. columns = cursor.fetchall()
  48. if result == None:
  49. print("no column")
  50. return
  51.  
  52. for row in columns:
  53. columnNames.append(str(row["COLUMN_NAME"]))
  54.  
  55. print(columnNames)
  56.  
  57. with connection.cursor() as cursor:
  58. sql = "SELECT * FROM `" + schemaName + "`.`" + tableName + "` LIMIT 5"
  59. cursor.execute(sql, ())
  60. result = cursor.fetchall()
  61. if result is None:
  62. print("no such table")
  63.  
  64. backupfile = open('backup.sql', 'w')
  65. insertSql = "insert into `" + tableName + "` (" + ",".join(["`%s`" % s for s in columnNames]) + ") values "
  66. for row in result:
  67. sql = insertSql
  68. sql += "("
  69. isFirst = True
  70. for col in columnNames:
  71. if isFirst:
  72. # TODO it is not onlye String
  73. sql += "'" + str(row[col]) + "'"
  74. isFirst = False
  75. else:
  76. sql += ",'" + str(row[col]) + "'"
  77.  
  78. sql += ");\n"
  79. backupfile.write(sql)
  80.  
  81. backupfile.close()
  82. print("done")
  83.  
  84.  
  85. def snakeToCamel(snake):
  86. words = snake.split("_")
  87. camel = ""
  88. for i in range(len(words)):
  89. if i == 0:
  90. camel += words[i]
  91. else:
  92. word = words[i]
  93. camel += word[0].upper()
  94. camel += word[1:]
  95. return camel
  96.  
  97. def createEntity(commands):
  98. tableName = commands[0]
  99. print ("table=" + tableName)
  100.  
  101. connection = getConnection()
  102.  
  103. with connection.cursor() as cursor:
  104. sql = "SELECT TABLE_NAME,TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME like %s"
  105. cursor.execute(sql, (tableName,))
  106. result = cursor.fetchone()
  107. if result == None:
  108. print("no such table")
  109. return
  110.  
  111. schemaName = result["TABLE_SCHEMA"]
  112. print(schemaName, tableName)
  113.  
  114. columns = []
  115. with connection.cursor() as cursor:
  116. sql = "SELECT COLUMN_NAME,DATA_TYPE FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = %s AND `TABLE_NAME` = %s"
  117. cursor.execute(sql, (schemaName, tableName,))
  118. columns = cursor.fetchall()
  119. if result == None:
  120. print("no column")
  121. return
  122.  
  123.  
  124. entityFile = open('entity.java', 'w')
  125. camelTableName = snakeToCamel(tableName)
  126. entityFile.write("\n\nimport javax.persistence.Column;\nimport javax.persistence.Entity;\nimport javax.persistence.GeneratedValue;\nimport javax.persistence.GenerationType;\nimport javax.persistence.Id;\nimport javax.persistence.Table;\n\n")
  127. entityFile.write("@Entity\n@Table(name=\"" + tableName + "\")\n")
  128. entityFile.write("class " + camelTableName[0].upper() + camelTableName[1:] + "Entity {\n")
  129.  
  130. properties = ""
  131. getset = ""
  132. for column in columns:
  133. colName = column["COLUMN_NAME"]
  134. dataType = column["DATA_TYPE"]
  135. camel = snakeToCamel(colName)
  136. type = "String"
  137. if dataType == "int" or dataType == "bigint":
  138. type = "Long"
  139. elif dataType == "smallint" or dataType == "tinyint":
  140. type = "Integer"
  141. elif dataType == "float" or dataType == "double":
  142. type = "Double"
  143. elif dataType == "varchar" or dataType == "text":
  144. type = "String"
  145. elif dataType == "date":
  146. type = "LocalDate"
  147. elif dataType == "datetime":
  148. type = "LocalDateTime"
  149.  
  150. properties += " private " + type + " " + camel + ";\n"
  151.  
  152. getset += " public void set" + camel[0].upper() + camel[1:] + "(" + type + " " + camel + ") {\n"
  153. getset += " this." + camel + " = " + camel + ";\n"
  154. getset += " }\n"
  155. getset += " @Column(name=\"" + colName + "\")\n"
  156. getset += " public " + type + " get" + camel[0].upper() + camel[1:] + "() {\n"
  157. getset += " return " + camel + ";\n"
  158. getset += " }\n"
  159.  
  160. entityFile.write(properties + "\n")
  161. entityFile.write(getset + "\n")
  162.  
  163. entityFile.write("}\n")
  164. entityFile.close()
  165. print("done")
  166.  
  167. print("This is AI")
  168. printPrompt()
  169. for command in sys.stdin:
  170. command = command.strip()
  171. if len(command) > 0:
  172. commands = command.split(" ")
  173. if (len(commands) > 1):
  174. if commands[0] == "backup":
  175. backupData(commands[1:])
  176.  
  177. if commands[0] == "create" and commands[1] == "entity":
  178. createEntity(commands[2:])
  179. printPrompt()
  180.  
  181. print("\nbye")
Add Comment
Please, Sign In to add comment