po.py.vm 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. # @Author : {{ table.function_author }}
  3. # @FileName: {{ underscore(table.class_name) }}_po.py
  4. # @Time : {{ datetime }}
  5. from typing import Optional
  6. from datetime import datetime
  7. from sqlalchemy import BigInteger, Boolean, Date, DateTime, Float, Integer, JSON, LargeBinary, Numeric, String, Text, Time
  8. from sqlalchemy.orm import Mapped, mapped_column
  9. from ruoyi_admin.ext import db
  10. class {{ underscore(table.class_name) }}_po(db.Model):
  11. """
  12. {{ table.table_comment }}PO对象
  13. """
  14. __tablename__ = '{{ table.table_name }}'
  15. __table_args__ = {'comment': '{{ table.table_comment }}'}
  16. {%- for column in table.columns %}
  17. {%- set raw_type = (column.column_type or 'varchar(255)')|lower %}
  18. {%- if '(' in raw_type %}
  19. {%- set base_type = raw_type.split('(')[0] %}
  20. {%- set arg_string = raw_type.split('(')[1].split(')')[0] %}
  21. {%- else %}
  22. {%- set base_type = raw_type %}
  23. {%- set arg_string = '' %}
  24. {%- endif %}
  25. {%- set args = arg_string.split(',') if arg_string else [] %}
  26. {%- set length = args[0].strip() if args and args[0] else '' %}
  27. {%- set precision = args[0].strip() if args and args[0] else '' %}
  28. {%- set scale = args[1].strip() if args|length > 1 else '' %}
  29. {%- set attr_name = underscore(column.java_field) %}
  30. {%- set nullable_flag = 'False' if column.is_required == '1' else 'True' %}
  31. {%- set py_type = 'Optional[str]' %}
  32. {%- if column.java_type in ['Integer', 'int'] %}
  33. {%- set py_type = 'Optional[int]' %}
  34. {%- elif column.java_type in ['Long'] %}
  35. {%- set py_type = 'Optional[int]' %}
  36. {%- elif column.java_type in ['Float', 'Double'] %}
  37. {%- set py_type = 'Optional[float]' %}
  38. {%- elif column.java_type in ['Boolean', 'bool'] %}
  39. {%- set py_type = 'Optional[bool]' %}
  40. {%- elif column.java_type in ['Date', 'DateTime'] %}
  41. {%- set py_type = 'Optional[datetime]' %}
  42. {%- endif %}
  43. {%- if column.is_pk == '1' %}
  44. {%- set py_type = 'int' %}
  45. {%- set nullable_flag = 'False' %}
  46. {%- endif %}
  47. {%- set type_expr = 'String(255)' %}
  48. {%- if base_type in ['varchar', 'char', 'nvarchar'] %}
  49. {%- set type_expr = 'String(' ~ (length or '255') ~ ')' %}
  50. {%- elif base_type in ['text', 'mediumtext', 'longtext'] %}
  51. {%- set type_expr = 'Text' %}
  52. {%- elif base_type in ['int', 'integer', 'smallint', 'mediumint'] %}
  53. {%- set type_expr = 'Integer' %}
  54. {%- elif base_type in ['tinyint'] %}
  55. {%- if column.java_type in ['Boolean', 'bool'] %}
  56. {%- set type_expr = 'Boolean' %}
  57. {%- else %}
  58. {%- set type_expr = 'Integer' %}
  59. {%- endif %}
  60. {%- elif base_type in ['bigint'] %}
  61. {%- set type_expr = 'BigInteger' %}
  62. {%- elif base_type in ['float', 'double'] %}
  63. {%- set type_expr = 'Float' %}
  64. {%- elif base_type in ['decimal', 'numeric'] %}
  65. {%- set type_expr = 'Numeric(' ~ (precision or '10') ~ (', ' ~ scale if scale else ', 0') ~ ')' %}
  66. {%- elif base_type in ['date'] %}
  67. {%- set type_expr = 'Date' %}
  68. {%- elif base_type in ['datetime', 'timestamp'] %}
  69. {%- set type_expr = 'DateTime' %}
  70. {%- elif base_type in ['time'] %}
  71. {%- set type_expr = 'Time' %}
  72. {%- elif base_type in ['json'] %}
  73. {%- set type_expr = 'JSON' %}
  74. {%- elif base_type in ['blob', 'longblob', 'mediumblob'] %}
  75. {%- set type_expr = 'LargeBinary' %}
  76. {%- endif %}
  77. {{ attr_name }}: Mapped[{{ py_type }}] = mapped_column(
  78. '{{ column.column_name }}',
  79. {{ type_expr }},
  80. {%- if column.is_pk == '1' %}
  81. primary_key=True,
  82. autoincrement={{ 'True' if column.is_increment == '1' else 'False' }},
  83. {%- endif %}
  84. nullable={{ nullable_flag }},
  85. comment='{{ column.column_comment | replace("'", "\\'") if column.column_comment else '' }}'
  86. )
  87. {%- endfor %}