Class: Crudboy::Column
Constant Summary collapse
- JAVA_TYPES =
{ "varchar" => 'String', "char" => 'String', "text" => 'String', "longtext" => 'String', "int" => 'Integer', "smallint" => 'Integer', "bigint" => 'Long', "tinyint" => 'Byte', "double" => 'Double', "date" => 'LocalDate', "datetime" => 'LocalDateTime', "timestamp" => 'LocalDateTime', "time" => 'LocalTime', "blob" => 'byte[]', "decimal" => 'BigDecimal' }
- JDBC_TYPES =
{ "varchar" => 'VARCHAR', "char" => 'CHAR', "text" => 'VARCHAR', "longtext" => 'VARCHAR', "int" => 'INTEGER', "smallint" => 'INTEGER', "bigint" => 'BIGINT', "tinyint" => 'TINYINT', "double" => 'DOUBLE', "date" => 'TIMESTAMP', "datetime" => 'TIMESTAMP', "timestamp" => 'TIMESTAMP', "time" => 'TIME', "blob" => 'BLOB', "decimal" => 'DECIMAL' }
- PYTHON_TYPES =
{ "varchar" => 'str', "char" => 'str', "text" => 'str', "longtext" => 'str', "int" => 'int', "smallint" => 'int', "bigint" => 'int', "tinyint" => 'int', "double" => 'float', "date" => 'datetime', "datetime" => 'datetime', "timestamp" => 'datetime', "time" => 'datetime', "blob" => 'bytes', "decimal" => 'decimal' }
Instance Attribute Summary collapse
-
#active_record_column ⇒ Object
Returns the value of attribute active_record_column.
-
#primary ⇒ Object
Returns the value of attribute primary.
Instance Method Summary collapse
- #created_at_column? ⇒ Boolean
-
#initialize(column, primary) ⇒ Column
constructor
A new instance of Column.
- #java_doc ⇒ Object
- #java_type ⇒ Object
- #jdbc_type ⇒ Object
- #lower_camel_name ⇒ Object
- #method_missing(method, *args, **options, &block) ⇒ Object
- #mybatis_equation ⇒ Object
- #mybatis_result_map ⇒ Object
- #mybatis_value_expression ⇒ Object
- #py_dto_column_declaration(optional = false) ⇒ Object
- #py_sqlmodel_created_at_column_declaration ⇒ Object
- #py_sqlmodel_primary_column_declaration ⇒ Object
- #py_sqlmodel_regular_column_declaration ⇒ Object
- #py_sqlmodel_updated_at_column_declaration ⇒ Object
- #python_type ⇒ Object
- #python_type_with_optional ⇒ Object
- #updated_at_column? ⇒ Boolean
- #upper_camel_name ⇒ Object
Constructor Details
#initialize(column, primary) ⇒ Column
Returns a new instance of Column.
60 61 62 63 |
# File 'lib/crudboy/column.rb', line 60 def initialize(column, primary) @active_record_column = column @primary = primary end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, **options, &block) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/crudboy/column.rb', line 176 def method_missing(method, *args, **, &block) if active_record_column.respond_to?(method) active_record_column.send(method, *args, **, &block) else super end end |
Instance Attribute Details
#active_record_column ⇒ Object
Returns the value of attribute active_record_column.
58 59 60 |
# File 'lib/crudboy/column.rb', line 58 def active_record_column @active_record_column end |
#primary ⇒ Object
Returns the value of attribute primary.
58 59 60 |
# File 'lib/crudboy/column.rb', line 58 def primary @primary end |
Instance Method Details
#created_at_column? ⇒ Boolean
168 169 170 |
# File 'lib/crudboy/column.rb', line 168 def created_at_column? sql_type =~ /datetime|time|date|timestamp/ && name =~ /gmt_created|created_at|create_time|create_date/ end |
#java_doc ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/crudboy/column.rb', line 65 def java_doc " /**\n * \#{comment}\n */\n EOF\nend\n".lstrip.chomp |
#java_type ⇒ Object
97 98 99 100 101 |
# File 'lib/crudboy/column.rb', line 97 def java_type return 'Boolean' if sql_type == 'tinyint(1)' raw_type = sql_type.scan(/^\w+/).first JAVA_TYPES[raw_type] end |
#jdbc_type ⇒ Object
103 104 105 106 |
# File 'lib/crudboy/column.rb', line 103 def jdbc_type raw_type = sql_type.scan(/^\w+/).first JDBC_TYPES[raw_type] end |
#lower_camel_name ⇒ Object
89 90 91 |
# File 'lib/crudboy/column.rb', line 89 def lower_camel_name name.camelcase(:lower) end |
#mybatis_equation ⇒ Object
77 78 79 |
# File 'lib/crudboy/column.rb', line 77 def mybatis_equation format('`%s` = %s', name, mybatis_value_expression) end |
#mybatis_result_map ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/crudboy/column.rb', line 81 def mybatis_result_map if @primary format('<id column="%s" jdbcType="%s" property="%s" />', name, jdbc_type, lower_camel_name) else format('<result column="%s" jdbcType="%s" property="%s" />', name, jdbc_type, lower_camel_name) end end |
#mybatis_value_expression ⇒ Object
73 74 75 |
# File 'lib/crudboy/column.rb', line 73 def mybatis_value_expression format('#{%s,jdbcType=%s}', lower_camel_name, jdbc_type) end |
#py_dto_column_declaration(optional = false) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/crudboy/column.rb', line 129 def py_dto_column_declaration(optional = false) # for example: id: str = Field(description="ID") if optional format('%s: %s | None = Field(description="%s", default=None)', name, python_type, comment) else format('%s: %s = Field(description="%s")', name, python_type, comment) end end |
#py_sqlmodel_created_at_column_declaration ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/crudboy/column.rb', line 150 def py_sqlmodel_created_at_column_declaration # create_time: Optional[datetime] = Field( # default_factory=datetime.now, # description="Create Time", # sa_column_kwargs={"server_default": sa.func.now()}, # ) format('%s: %s = Field(default_factory=datetime.now, description="%s", sa_column_kwargs={"server_default": sa.func.now()})', name, python_type_with_optional, comment || '') end |
#py_sqlmodel_primary_column_declaration ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/crudboy/column.rb', line 118 def py_sqlmodel_primary_column_declaration raw_type = sql_type.scan(/^\w+/).first if raw_type == 'varchar' format('%s: %s = Field(default_factory=gen_uuid, primary_key=True, max_length=%s, description="%s")', name, python_type, limit, comment) elsif raw_type == 'bigint' format('%s: %s = Field(default_factory=gen_bigint_id, primary_key=True,description="%s")', name, python_type, comment) else format('%s: %s = Field(primary_key=True, description="%s")', name, python_type, comment) end end |
#py_sqlmodel_regular_column_declaration ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/crudboy/column.rb', line 138 def py_sqlmodel_regular_column_declaration if created_at_column? return py_sqlmodel_created_at_column_declaration end if updated_at_column? return py_sqlmodel_updated_at_column_declaration end format('%s: %s = Field(default=None, max_length=%s, description="%s")', name, python_type_with_optional, limit || 'None', comment || '') end |
#py_sqlmodel_updated_at_column_declaration ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/crudboy/column.rb', line 159 def py_sqlmodel_updated_at_column_declaration # update_time: Optional[datetime] = Field( # default_factory=datetime.now, # description="Update Time", # sa_column_kwargs={"server_default": sa.func.now()}, # ) format('%s: %s = Field(default_factory=datetime.now, description="%s", sa_column_kwargs={"server_default": sa.func.now()})', name, python_type_with_optional, comment || '') end |
#python_type ⇒ Object
108 109 110 111 |
# File 'lib/crudboy/column.rb', line 108 def python_type raw_type = sql_type.scan(/^\w+/).first PYTHON_TYPES[raw_type] end |
#python_type_with_optional ⇒ Object
113 114 115 116 |
# File 'lib/crudboy/column.rb', line 113 def python_type_with_optional raw_python_type = python_type null ? "Optional[#{raw_python_type}]" : raw_python_type end |
#updated_at_column? ⇒ Boolean
172 173 174 |
# File 'lib/crudboy/column.rb', line 172 def updated_at_column? sql_type =~ /datetime|time|date|timestamp/ && name =~ /gmt_modified|updated_at|update_time|update_date/ end |
#upper_camel_name ⇒ Object
93 94 95 |
# File 'lib/crudboy/column.rb', line 93 def upper_camel_name name.camelcase(:upper) end |