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/SQLTemplate.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
/
SQLTemplate.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
157 lines (132 loc) · 6.14 KB
Breadcrumbs
orm-java
/
src
/
main
/
java
/
com
/
github
/
artbits
/
orm
/
SQLTemplate.java
Copy path
File metadata and controls
157 lines (132 loc) · 6.14 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
/**
* 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
.
util
.
Objects
;
import
java
.
util
.
Optional
;
final
class
SQLTemplate
{
static
<
T
>
String
create
(
Class
<
T
>
tClass
) {
StringBuffer
columnsString
=
new
StringBuffer
();
if
(
Core
.
config
.
type
() ==
Config
.
Type
.
SQLite
) {
columnsString
.
append
(
"id integer primary key autoincrement,"
);
}
if
(
Core
.
config
.
type
() ==
Config
.
Type
.
MySQL
) {
columnsString
.
append
(
"id int primary key auto_increment,"
);
}
if
(
Core
.
config
.
type
() ==
Config
.
Type
.
PostgreSQL
) {
columnsString
.
append
(
"id bigserial primary key,"
);
}
new
Reflect
<>(
tClass
).
getDatabaseColumnsWithType
((
column
,
type
) -> {
if
(!
Objects
.
equals
(
column
,
"id"
)) {
columnsString
.
append
(
column
).
append
(
" "
).
append
(
type
).
append
(
","
);
}
});
columnsString
.
deleteCharAt
(
columnsString
.
length
() -
1
);
String
tableName
=
tClass
.
getSimpleName
().
toLowerCase
();
return
$
(
"create table if not exists %s (%s);"
,
tableName
,
columnsString
);
}
static
<
T
>
String
addColumn
(
Class
<
T
>
tClass
,
String
column
,
String
type
) {
String
tableName
=
tClass
.
getSimpleName
().
toLowerCase
();
return
$
(
"alter table %s add column %s %s;"
,
tableName
,
column
,
type
);
}
static
<
T
>
String
drop
(
Class
<
T
>
tClass
) {
return
$
(
"drop table if exists %s;"
,
tClass
.
getSimpleName
().
toLowerCase
());
}
static
<
T
>
String
insert
(
T
t
) {
StringBuffer
columnsString
=
new
StringBuffer
();
StringBuffer
valueString
=
new
StringBuffer
();
new
Reflect
<>(
t
).
getDatabaseColumnsWithValue
((
column
,
value
) -> {
if
(!
Objects
.
equals
(
column
,
"id"
)) {
columnsString
.
append
(
column
).
append
(
","
);
valueString
.
append
(
value
).
append
(
","
);
}
});
columnsString
.
deleteCharAt
(
columnsString
.
length
() -
1
);
valueString
.
deleteCharAt
(
valueString
.
length
() -
1
);
String
tableName
=
t
.
getClass
().
getSimpleName
().
toLowerCase
();
return
$
(
"insert into %s (%s) values (%s);"
,
tableName
,
columnsString
,
valueString
);
}
static
<
T
>
String
update
(
T
t
,
Options
options
) {
String
tableName
=
t
.
getClass
().
getSimpleName
().
toLowerCase
();
String
whereString
= (
options
.
wherePredicate
!=
null
) ?
$
(
"where %s "
,
options
.
wherePredicate
) :
""
;
StringBuffer
setString
=
new
StringBuffer
();
new
Reflect
<>(
t
).
getDatabaseColumnsWithValue
((
column
,
value
) -> {
if
(
value
!=
null
&& !
Objects
.
equals
(
column
,
"id"
)) {
setString
.
append
(
column
).
append
(
" = "
).
append
(
value
).
append
(
","
);
}
});
setString
.
deleteCharAt
(
setString
.
length
() -
1
);
StringBuilder
SQLBuilder
=
new
StringBuilder
();
return
SQLBuilder
.
append
(
$
(
"update %s set %s "
,
tableName
,
setString
))
.
append
(
whereString
)
.
append
(
";"
)
.
deleteCharAt
(
SQLBuilder
.
length
() -
2
)
.
toString
();
}
static
<
T
>
String
delete
(
Class
<
T
>
tClass
,
Options
options
) {
String
deleteString
=
$
(
"delete from %s "
,
tClass
.
getSimpleName
().
toLowerCase
());
String
whereString
= (
options
.
wherePredicate
!=
null
) ?
$
(
"where %s "
,
options
.
wherePredicate
) :
""
;
StringBuilder
SQLBuilder
=
new
StringBuilder
();
return
SQLBuilder
.
append
(
deleteString
)
.
append
(
whereString
)
.
append
(
";"
)
.
deleteCharAt
(
SQLBuilder
.
length
() -
2
)
.
toString
();
}
static
<
T
>
String
query
(
String
table
,
Options
options
) {
if
(
options
==
null
) {
return
$
(
"select * from %s;"
,
table
);
}
String
fromString
=
$
(
"from %s "
,
table
);
String
selectString
=
$
(
"select %s "
,
Optional
.
ofNullable
(
options
.
selectColumns
).
orElse
(
"*"
));
String
whereString
= (
options
.
wherePredicate
!=
null
) ?
$
(
"where %s "
,
options
.
wherePredicate
) :
""
;
String
groupString
= (
options
.
groupColumns
!=
null
) ?
$
(
"group by %s "
,
options
.
groupColumns
) :
""
;
String
orderString
= (
options
.
orderColumns
!=
null
) ?
$
(
"order by %s "
,
options
.
orderColumns
) :
""
;
String
limitString
= (
options
.
limitSize
!=
null
) ?
$
(
"limit %d "
,
options
.
limitSize
) :
""
;
String
offsetString
= (
options
.
offsetSize
!=
null
) ?
$
(
"offset %d "
,
options
.
offsetSize
) :
""
;
StringBuilder
SQLBuilder
=
new
StringBuilder
();
return
SQLBuilder
.
append
(
selectString
)
.
append
(
fromString
)
.
append
(
whereString
)
.
append
(
groupString
)
.
append
(
orderString
)
.
append
(
limitString
)
.
append
(
offsetString
)
.
append
(
";"
)
.
deleteCharAt
(
SQLBuilder
.
length
() -
2
)
.
toString
();
}
static
<
T
>
String
query
(
Class
<
T
>
tClass
,
Options
options
) {
return
query
(
tClass
.
getSimpleName
().
toLowerCase
(),
options
);
}
static
<
T
>
String
createIndex
(
Class
<
T
>
tClass
,
String
column
) {
String
table
=
tClass
.
getSimpleName
().
toLowerCase
();
String
index
=
$
(
"idx_%s_%s"
,
table
,
column
);
return
$
(
"create unique index %s on %s(%s)"
,
index
,
table
,
column
);
}
static
<
T
>
String
dropIndex
(
String
tableName
,
String
index
) {
if
(
tableName
==
null
) {
return
$
(
"drop index if exists %s"
,
index
);
}
else
{
return
$
(
"drop index %s on %s"
,
index
,
tableName
);
}
}
private
static
String
$
(
String
format
,
Object
...
objects
) {
return
String
.
format
(
format
,
objects
);
}
}
Back
|
FazBrowse Home
|
New Git URL