FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
orm-java/src/main/java/com/github/artbits/orm/Reflect.java at main · artbits/orm-java · GitHub
artbits
/
orm-java
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
9
Code
Issues
3
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
orm-java
/
src
/
main
/
java
/
com
/
github
/
artbits
/
orm
/
Reflect.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
257 lines (223 loc) · 8.36 KB
Breadcrumbs
orm-java
/
src
/
main
/
java
/
com
/
github
/
artbits
/
orm
/
Reflect.java
Copy path
File metadata and controls
257 lines (223 loc) · 8.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Copyright 2023 Zhang Guanhu
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
github
.
artbits
.
orm
;
import
java
.
lang
.
reflect
.
Field
;
import
java
.
sql
.
ResultSet
;
import
java
.
sql
.
ResultSetMetaData
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
LinkedHashMap
;
import
java
.
util
.
Map
;
import
java
.
util
.
function
.
BiConsumer
;
import
java
.
util
.
function
.
Consumer
;
final
class
Reflect
<
T
> {
private
final
Map
<
String
,
Field
>
fieldMap
=
new
LinkedHashMap
<>();
private
Class
<?>
tClass
;
private
T
t
;
Reflect
(
Class
<?>
tClass
) {
this
.
tClass
=
tClass
;
newInstance
(
tClass
);
}
Reflect
(
T
t
) {
this
.
t
=
t
;
newInstance
(
t
.
getClass
());
}
private
void
newInstance
(
Class
<?>
tClass
) {
Class
<?>
clazz
=
tClass
;
while
(
clazz
!=
null
){
for
(
Field
field
:
clazz
.
getDeclaredFields
()) {
field
.
setAccessible
(
true
);
if
(!
isIgnore
(
field
)) {
fieldMap
.
put
(
field
.
getName
(),
field
);
}
}
clazz
=
clazz
.
getSuperclass
();
}
}
void
setValue
(
String
fieldName
,
Object
value
) {
try
{
Field
field
=
fieldMap
.
getOrDefault
(
fieldName
,
null
);
if
(
field
!=
null
) {
field
.
set
(
t
,
value
);
}
}
catch
(
IllegalAccessException
e
) {
throw
new
RuntimeException
(
e
);
}
}
Object
getValue
(
String
fieldName
) {
try
{
Field
field
=
fieldMap
.
getOrDefault
(
fieldName
,
null
);
return
(
field
!=
null
) ?
field
.
get
(
t
) :
null
;
}
catch
(
IllegalAccessException
e
) {
throw
new
RuntimeException
(
e
);
}
}
Class
<?>
getType
(
String
fieldName
) {
Field
field
=
fieldMap
.
getOrDefault
(
fieldName
,
null
);
return
field
.
getType
();
}
String
getDatabaseType
(
String
fieldName
) {
String
type
=
getType
(
fieldName
).
getSimpleName
().
toLowerCase
();
if
(
Core
.
config
.
type
() ==
Config
.
Type
.
SQLite
) {
switch
(
type
) {
case
"byte"
:
case
"short"
:
case
"int"
:
case
"integer"
:
case
"long"
:
case
"boolean"
:
return
"integer"
;
case
"float"
:
case
"double"
:
return
"real"
;
case
"char"
:
case
"character"
:
case
"string"
:
return
"text"
;
default
:
throw
new
NullPointerException
();
}
}
else
if
(
Core
.
config
.
type
() ==
Config
.
Type
.
MySQL
) {
switch
(
type
) {
case
"byte"
:
return
"tinyint"
;
case
"short"
:
return
"smallint"
;
case
"int"
:
case
"integer"
:
return
"int"
;
case
"long"
:
return
"bigint"
;
case
"float"
:
return
"float"
;
case
"double"
:
return
"double"
;
case
"char"
:
case
"character"
:
case
"string"
:
return
"text"
;
case
"boolean"
:
return
"boolean"
;
default
:
throw
new
NullPointerException
();
}
}
else
if
(
Core
.
config
.
type
() ==
Config
.
Type
.
PostgreSQL
) {
switch
(
type
) {
case
"byte"
:
case
"short"
:
return
"smallint"
;
case
"int"
:
case
"integer"
:
return
"int"
;
case
"long"
:
return
"bigint"
;
case
"float"
:
return
"real"
;
case
"double"
:
return
"double precision"
;
case
"char"
:
case
"character"
:
case
"string"
:
return
"text"
;
case
"boolean"
:
return
"boolean"
;
default
:
throw
new
NullPointerException
();
}
}
else
{
throw
new
RuntimeException
(
"Unsupported database type."
);
}
}
Object
getDatabaseValue
(
Field
field
) {
try
{
String
fieldName
=
field
.
getName
();
Field
dbField
=
fieldMap
.
getOrDefault
(
fieldName
,
null
);
Object
dbValue
= (
dbField
!=
null
) ?
dbField
.
get
(
t
) :
null
;
if
(
dbField
!=
null
&&
dbValue
!=
null
) {
return
getDatabaseType
(
fieldName
).
equals
(
"text"
) ?
String
.
format
(
"'%s'"
,
dbValue
) :
dbValue
;
}
return
null
;
}
catch
(
IllegalAccessException
e
) {
throw
new
RuntimeException
(
e
);
}
}
void
getDatabaseColumnsWithValue
(
BiConsumer
<
String
,
Object
>
consumer
) {
for
(
Field
field
:
fieldMap
.
values
()) {
consumer
.
accept
(
field
.
getName
(),
getDatabaseValue
(
field
));
}
}
void
getDatabaseColumnsWithType
(
BiConsumer
<
String
,
String
>
consumer
) {
for
(
Field
field
:
fieldMap
.
values
()) {
consumer
.
accept
(
field
.
getName
(),
getDatabaseType
(
field
.
getName
()));
}
}
void
getIndexList
(
BiConsumer
<
String
,
String
>
consumer
) {
String
table
=
tClass
.
getSimpleName
().
toLowerCase
();
fieldMap
.
values
().
forEach
(
field
-> {
if
(
isIndex
(
field
)) {
String
column
=
field
.
getName
();
String
index
=
String
.
format
(
"idx_%s_%s"
,
table
,
column
);
consumer
.
accept
(
index
,
column
);
}
});
}
T
get
() {
return
t
;
}
static
<
T
>
T
toEntity
(
Class
<
T
>
tClass
,
ResultSet
resultSet
) {
try
{
Map
<
String
,
Boolean
>
columnsMap
=
new
HashMap
<>();
ResultSetMetaData
metaData
=
resultSet
.
getMetaData
();
int
count
=
metaData
.
getColumnCount
();
for
(
int
i
=
1
;
i
<=
count
;
i
++) {
columnsMap
.
put
(
metaData
.
getColumnName
(
i
),
true
);
}
T
t
=
tClass
.
getConstructor
(
Consumer
.
class
).
newInstance
((
Consumer
<
T
>) (
c
-> {}));
Reflect
<
T
>
reflect
=
new
Reflect
<>(
t
);
for
(
Field
field
:
reflect
.
fieldMap
.
values
()) {
String
name
=
field
.
getName
();
if
(!
columnsMap
.
isEmpty
() && !
columnsMap
.
getOrDefault
(
name
,
false
)) {
continue
;
}
String
type
=
field
.
getType
().
getSimpleName
().
toLowerCase
();
switch
(
type
) {
case
"int"
:
case
"integer"
:
reflect
.
setValue
(
name
,
resultSet
.
getInt
(
name
));
break
;
case
"byte"
:
reflect
.
setValue
(
name
,
resultSet
.
getByte
(
name
));
break
;
case
"short"
:
reflect
.
setValue
(
name
,
resultSet
.
getShort
(
name
));
break
;
case
"long"
:
reflect
.
setValue
(
name
,
resultSet
.
getLong
(
name
));
break
;
case
"float"
:
reflect
.
setValue
(
name
,
resultSet
.
getFloat
(
name
));
break
;
case
"double"
:
reflect
.
setValue
(
name
,
resultSet
.
getDouble
(
name
));
break
;
case
"char"
:
case
"character"
:
case
"string"
:
reflect
.
setValue
(
name
,
resultSet
.
getString
(
name
));
break
;
case
"boolean"
:
reflect
.
setValue
(
name
,
resultSet
.
getBoolean
(
name
));
break
;
}
}
return
reflect
.
get
();
}
catch
(
Exception
e
) {
throw
new
RuntimeException
(
e
);
}
}
static
boolean
isIgnore
(
Field
field
) {
if
(
field
.
isAnnotationPresent
(
Column
.
class
)) {
Column
column
=
field
.
getAnnotation
(
Column
.
class
);
return
column
.
ignore
();
}
return
false
;
}
static
boolean
isIndex
(
Field
field
) {
if
(
field
.
isAnnotationPresent
(
Column
.
class
)) {
Column
column
=
field
.
getAnnotation
(
Column
.
class
);
return
column
.
index
();
}
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL