db_schema.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import json
  2. import pyodbc
  3. def connect():
  4. c = pyodbc.connect("DSN=Autoline_direkt64;UID=kcc;PWD=kcc123")
  5. return c.cursor()
  6. def convert_desc(col):
  7. nullable = "NULL" if col.nullable == 1 else "NOT NULL"
  8. if col.type_name == "CHAR":
  9. return f" [{col.column_name}] [varchar]({col.length}) {nullable}"
  10. if col.type_name == "HEX":
  11. return f" [{col.column_name}] [binary]({col.length}) {nullable}"
  12. if col.type_name == "INTEGER":
  13. return f" [{col.column_name}] [int] {nullable}"
  14. if col.type_name == "DATE":
  15. return f" [{col.column_name}] [datetime] {nullable}"
  16. if col.type_name == "NUMERIC":
  17. return f" [{col.column_name}] [decimal]({col.length},{col.scale}) {nullable}"
  18. if col.type_name == "BIT":
  19. return f" [{col.column_name}] [boolean] {nullable}"
  20. if col.type_name == "MEMO":
  21. return f" [{col.column_name}] [text] {nullable}"
  22. return ", ".join(list(map(str, col)))
  23. def convert_desc_sql_anywhere(col):
  24. nullable = "NULL" if col.nullable == 1 else "NOT NULL"
  25. if col.type_name in (
  26. "char",
  27. "varchar",
  28. "long varchar",
  29. "long nvarchar",
  30. "nvarchar",
  31. ):
  32. sizes = [20, 50, 100, 255]
  33. length = "MAX"
  34. for s in sizes:
  35. if col.length < s:
  36. length = s
  37. break
  38. return f" [{col.column_name}] [varchar]({length}) {nullable}"
  39. if col.type_name in (
  40. "binary",
  41. "long binary",
  42. "varbinary",
  43. ):
  44. return f" [{col.column_name}] [binary]({col.length}) {nullable}"
  45. if col.type_name in (
  46. "bit",
  47. "tinyint",
  48. ):
  49. return f" [{col.column_name}] [tinyint] {nullable}"
  50. if col.type_name in (
  51. "integer",
  52. "smallint",
  53. "unsigned smallint",
  54. "unsigned int",
  55. ):
  56. return f" [{col.column_name}] [int] {nullable}"
  57. if col.type_name in (
  58. "bigint",
  59. "unsigned bigint",
  60. "long varbit",
  61. ):
  62. return f" [{col.column_name}] [bigint] {nullable}"
  63. if col.type_name in (
  64. "date",
  65. "timestamp",
  66. "timestamp with time zone",
  67. ):
  68. return f" [{col.column_name}] [datetime] {nullable}"
  69. if col.type_name in (
  70. "decimal",
  71. "float",
  72. "double",
  73. "numeric",
  74. ):
  75. return f" [{col.column_name}] [decimal](28,8) {nullable}"
  76. if col.type_name in ("time"):
  77. return f" [{col.column_name}] [time] {nullable}"
  78. return ", ".join(list(map(str, col)))
  79. def convert_desc2(col):
  80. return ", ".join(list(map(str, col)))
  81. # table_name = [x[2] for x in crsr.tables(tableType='VIEW')]
  82. # open("views.txt", "w").write("\n".join(table_name))
  83. tables = []
  84. res = {}
  85. def tables_cols():
  86. crsr = connect()
  87. for t in tables:
  88. try:
  89. cols = crsr.columns(table=t)
  90. # print([x[0] for x in crsr.description])
  91. res[t] = [convert_desc(c) for c in cols]
  92. crsr.cancel()
  93. except pyodbc.Error as e:
  94. print(e)
  95. if t != "":
  96. res[t] = []
  97. crsr = connect()
  98. json.dump(res, open("schema.json", "w"), indent=2)
  99. def pkeys():
  100. crsr = connect()
  101. for t in tables:
  102. try:
  103. cols = crsr.primaryKeys(table=t)
  104. # print([x[0] for x in crsr.description])
  105. if res.get(t) is None:
  106. res[t] = []
  107. res[t].append(
  108. f" CONSTRAINT [{t}$0] PRIMARY KEY CLUSTERED ([" + "], [".join([c.column_name for c in cols]) + "])"
  109. )
  110. crsr.cancel()
  111. except pyodbc.Error as e:
  112. print(e)
  113. if t != "":
  114. res[t] = []
  115. crsr = connect()
  116. json.dump(res, open("pkeys.json", "w"), indent=2)
  117. def fkeys():
  118. crsr = connect()
  119. for t in tables:
  120. try:
  121. cols = crsr.foreignKeys(table=t)
  122. print([x[0] for x in crsr.description])
  123. if res.get(t) is None:
  124. res[t] = []
  125. res[t].append([convert_desc2(c) for c in cols])
  126. crsr.cancel()
  127. except pyodbc.Error as e:
  128. print(e)
  129. if t != "":
  130. res[t] = []
  131. crsr = connect()
  132. def tables_create():
  133. for t, cols in res.items():
  134. with open("../sql_load/schema/AUTOLINE/tables/import." + t + ".sql", "w") as wh:
  135. wh.write(f"CREATE TABLE [import].[{t}] (\n")
  136. wh.write(",\n".join(cols))
  137. wh.write(")\n\nGO\n")
  138. def schema():
  139. with open("tables.txt", "r") as rh:
  140. tables.extend(rh.read().split("\n"))
  141. tables_cols()
  142. pkeys()
  143. # fkeys()
  144. tables_create()