schema.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import pyodbc
  2. import json
  3. def connect():
  4. c = pyodbc.connect("DSN=Locosoft;UID=loco_auswertung_benutzer;PWD=loco")
  5. return c.cursor()
  6. def convert_desc(col):
  7. nullable = "NULL" if col.nullable == 1 else "NOT NULL"
  8. if col.type_name in ('CHAR', 'varchar'):
  9. return f' [{col.column_name}] [varchar]({convert_len(col.length)}) {nullable}'
  10. if col.type_name in ('HEX'):
  11. return f' [{col.column_name}] [binary]({col.length}) {nullable}'
  12. if col.type_name in ('INTEGER', 'int4'):
  13. return f' [{col.column_name}] [int] {nullable}'
  14. if col.type_name in ('INTEGER', 'int8', 'timestamp'):
  15. return f' [{col.column_name}] [bigint] {nullable}'
  16. if col.type_name in ('DATE', 'date'):
  17. return f' [{col.column_name}] [datetime] {nullable}'
  18. if col.type_name in ('NUMERIC', 'numeric'):
  19. return f' [{col.column_name}] [numeric]({col.length},{col.scale}) {nullable}'
  20. if col.type_name in ('BIT', 'bool'):
  21. return f' [{col.column_name}] [smallint] {nullable}'
  22. if col.type_name in ('MEMO', 'text'):
  23. return f' [{col.column_name}] [varchar](100) {nullable}'
  24. return ", ".join(list(map(str, col)))
  25. def convert_len(length):
  26. if length < 8:
  27. return 10
  28. if length < 38:
  29. return 50
  30. if length < 88:
  31. return 100
  32. return 255
  33. def convert_desc2(col):
  34. return ", ".join(list(map(str, col)))
  35. # table_name = [x[2] for x in crsr.tables(tableType='VIEW')]
  36. # open("views.txt", "w").write("\n".join(table_name))
  37. with open("tables.txt", "r") as rh:
  38. tables = rh.read().split("\n")
  39. res = {}
  40. def tables_cols():
  41. crsr = connect()
  42. for t in tables:
  43. try:
  44. cols = crsr.columns(table=t)
  45. # print([x[0] for x in crsr.description])
  46. res[t] = [convert_desc(c) for c in cols]
  47. res[t].append(" [client_db] [varchar](20) NOT NULL")
  48. crsr.cancel()
  49. except pyodbc.Error as e:
  50. print(e)
  51. if t != '':
  52. res[t] = []
  53. crsr = connect()
  54. json.dump(res, open("schema.json", "w"), indent=2)
  55. def pkeys():
  56. crsr = connect()
  57. for t in tables:
  58. try:
  59. cols = crsr.primaryKeys(table=t)
  60. # print([x[0] for x in crsr.description])
  61. if res.get(t) is None:
  62. res[t] = []
  63. res[t].append(f"\n CONSTRAINT [{t}$0] PRIMARY KEY CLUSTERED ([" + "], [".join([c.column_name for c in cols] + ['client_db']) + "])")
  64. crsr.cancel()
  65. except pyodbc.Error as e:
  66. print(t)
  67. print(e)
  68. if t != '':
  69. res[t] = []
  70. crsr = connect()
  71. json.dump(res, open("pkeys.json", "w"), indent=2)
  72. def fkeys():
  73. crsr = connect()
  74. for t in tables:
  75. try:
  76. cols = crsr.foreignKeys(table=t)
  77. print([x[0] for x in crsr.description])
  78. if res.get(t) is None:
  79. res[t] = []
  80. res[t].append([convert_desc2(c) for c in cols])
  81. crsr.cancel()
  82. except pyodbc.Error as e:
  83. print(e)
  84. if t != '':
  85. res[t] = []
  86. crsr = connect()
  87. def tables_create():
  88. for t, cols in res.items():
  89. with open("../../System/LOCOSOFT/SQL/schema/LOCOSOFT/sql_load/" + t + ".sql", "w") as wh:
  90. wh.write(f"CREATE TABLE [dbo].[{t}] (\n")
  91. wh.write(",\n".join(cols))
  92. wh.write("\n)\n\nGO\n")
  93. if __name__ == '__main__':
  94. tables_cols()
  95. pkeys()
  96. # fkeys()
  97. tables_create()