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
Instance Method Summary
collapse
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
169
170
171
172
173
174
175
|
# File 'lib/crudboy/column.rb', line 169
def method_missing(method, *args, **options, &block)
if active_record_column.respond_to?(method)
active_record_column.send(method, *args, **options, &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
|
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
161
162
163
|
# File 'lib/crudboy/column.rb', line 161
def created_at_column?
sql_type =~ /datetime|time|date|timestamp/ && name =~ /gmt_created|created_at|create_time|create_date/
end
|
65
66
67
68
69
70
71
|
# File 'lib/crudboy/column.rb', line 65
def java_doc
<<-EOF.lstrip.chomp
/**
* #{}
*/
EOF
end
|
#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
122
123
124
125
126
127
128
129
|
# File 'lib/crudboy/column.rb', line 122
def py_dto_column_declaration(optional = false)
if optional
format('%s: %s | None = Field(description="%s", default=None)', name, python_type, )
else
format('%s: %s = Field(description="%s")', name, python_type, )
end
end
|
#py_sqlmodel_created_at_column_declaration ⇒ Object
143
144
145
146
147
148
149
150
|
# File 'lib/crudboy/column.rb', line 143
def py_sqlmodel_created_at_column_declaration
format('%s: %s = Field(default_factory=datetime.now, description="%s", sa_column_kwargs={"server_default": sa.func.now()})', name, python_type_with_optional, || '')
end
|
#py_sqlmodel_primary_column_declaration ⇒ Object
118
119
120
|
# File 'lib/crudboy/column.rb', line 118
def py_sqlmodel_primary_column_declaration
format('%s: %s = Field(default_factory=gen_id, primary_key=True, max_length=%s, description="%s")', name, python_type, limit, )
end
|
#py_sqlmodel_regular_column_declaration ⇒ Object
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/crudboy/column.rb', line 131
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', || '')
end
|
#py_sqlmodel_updated_at_column_declaration ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/crudboy/column.rb', line 152
def py_sqlmodel_updated_at_column_declaration
format('%s: %s = Field(default_factory=datetime.now, description="%s", sa_column_kwargs={"server_default": sa.func.now()})', name, python_type_with_optional, || '')
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
165
166
167
|
# File 'lib/crudboy/column.rb', line 165
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
|