Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
dlink
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhaowei
dlink
Commits
0b3f8051
Unverified
Commit
0b3f8051
authored
Oct 28, 2022
by
Licho
Committed by
GitHub
Oct 28, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: simplify code. (#1165)
parent
24bd97a1
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
49 deletions
+29
-49
SqlManager.java
...executor/src/main/java/com/dlink/executor/SqlManager.java
+29
-49
No files found.
dlink-executor/src/main/java/com/dlink/executor/SqlManager.java
View file @
0b3f8051
...
@@ -37,7 +37,7 @@ import org.apache.flink.types.Row;
...
@@ -37,7 +37,7 @@ import org.apache.flink.types.Row;
import
org.apache.flink.util.StringUtils
;
import
org.apache.flink.util.StringUtils
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.
Array
s
;
import
java.util.
Collection
s
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
...
@@ -53,8 +53,9 @@ import java.util.regex.Pattern;
...
@@ -53,8 +53,9 @@ import java.util.regex.Pattern;
**/
**/
public
final
class
SqlManager
{
public
final
class
SqlManager
{
p
rivate
Map
<
String
,
String
>
sqlFragments
;
p
ublic
static
final
String
FRAGMENT
=
"fragment"
;
static
final
String
SHOW_FRAGMENTS
=
"SHOW FRAGMENTS"
;
static
final
String
SHOW_FRAGMENTS
=
"SHOW FRAGMENTS"
;
private
final
Map
<
String
,
String
>
sqlFragments
;
public
SqlManager
()
{
public
SqlManager
()
{
sqlFragments
=
new
HashMap
<>();
sqlFragments
=
new
HashMap
<>();
...
@@ -78,14 +79,11 @@ public final class SqlManager {
...
@@ -78,14 +79,11 @@ public final class SqlManager {
* But at the moment, with CatalogException, not SqlException
* But at the moment, with CatalogException, not SqlException
*/
*/
public
void
registerSqlFragment
(
String
sqlFragmentName
,
String
sqlFragment
)
{
public
void
registerSqlFragment
(
String
sqlFragmentName
,
String
sqlFragment
)
{
checkArgument
(
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragment name cannot be null or empty."
);
!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragment name cannot be null or empty."
);
checkNotNull
(
sqlFragment
,
"sql fragment cannot be null"
);
checkNotNull
(
sqlFragment
,
"sql fragment cannot be null"
);
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
throw
new
CatalogException
(
throw
new
CatalogException
(
format
(
"The fragment of sql %s already exists."
,
sqlFragmentName
));
format
(
"The fragment of sql %s already exists."
,
sqlFragmentName
));
}
}
sqlFragments
.
put
(
sqlFragmentName
,
sqlFragment
);
sqlFragments
.
put
(
sqlFragmentName
,
sqlFragment
);
...
@@ -114,15 +112,12 @@ public final class SqlManager {
...
@@ -114,15 +112,12 @@ public final class SqlManager {
* failed. But at the moment, with CatalogException, not SqlException
* failed. But at the moment, with CatalogException, not SqlException
*/
*/
public
void
unregisterSqlFragment
(
String
sqlFragmentName
,
boolean
ignoreIfNotExists
)
{
public
void
unregisterSqlFragment
(
String
sqlFragmentName
,
boolean
ignoreIfNotExists
)
{
checkArgument
(
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
sqlFragments
.
remove
(
sqlFragmentName
);
sqlFragments
.
remove
(
sqlFragmentName
);
}
else
if
(!
ignoreIfNotExists
)
{
}
else
if
(!
ignoreIfNotExists
)
{
throw
new
CatalogException
(
throw
new
CatalogException
(
format
(
"The fragment of sql %s does not exist."
,
sqlFragmentName
));
format
(
"The fragment of sql %s does not exist."
,
sqlFragmentName
));
}
}
}
}
...
@@ -134,26 +129,22 @@ public final class SqlManager {
...
@@ -134,26 +129,22 @@ public final class SqlManager {
* failed. But at the moment, with CatalogException, not SqlException
* failed. But at the moment, with CatalogException, not SqlException
*/
*/
public
String
getSqlFragment
(
String
sqlFragmentName
)
{
public
String
getSqlFragment
(
String
sqlFragmentName
)
{
checkArgument
(
checkArgument
(!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
!
StringUtils
.
isNullOrWhitespaceOnly
(
sqlFragmentName
),
"sql fragmentName name cannot be null or empty."
);
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
if
(
sqlFragments
.
containsKey
(
sqlFragmentName
))
{
return
sqlFragments
.
get
(
sqlFragmentName
);
return
sqlFragments
.
get
(
sqlFragmentName
);
}
else
{
throw
new
CatalogException
(
format
(
"The fragment of sql %s does not exist."
,
sqlFragmentName
));
}
}
throw
new
CatalogException
(
format
(
"The fragment of sql %s does not exist."
,
sqlFragmentName
));
}
}
public
TableResult
getSqlFragmentResult
(
String
sqlFragmentName
)
{
public
TableResult
getSqlFragmentResult
(
String
sqlFragmentName
)
{
if
(
Asserts
.
isNullString
(
sqlFragmentName
))
{
if
(
Asserts
.
isNullString
(
sqlFragmentName
))
{
return
CustomTableResultImpl
.
buildTableResult
(
new
ArrayList
<>(
Arrays
.
asList
(
new
TableSchemaField
(
"fragment"
,
DataTypes
.
STRING
()
))),
new
ArrayList
<>());
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
FRAGMENT
,
DataTypes
.
STRING
(
))),
new
ArrayList
<>());
}
}
String
sqlFragment
=
getSqlFragment
(
sqlFragmentName
);
String
sqlFragment
=
getSqlFragment
(
sqlFragmentName
);
List
<
Row
>
rows
=
new
ArrayList
<>();
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
FRAGMENT
,
DataTypes
.
STRING
())),
Collections
.
singletonList
(
Row
.
of
(
sqlFragment
)));
rows
.
add
(
Row
.
of
(
sqlFragment
));
return
CustomTableResultImpl
.
buildTableResult
(
new
ArrayList
<>(
Arrays
.
asList
(
new
TableSchemaField
(
"fragment"
,
DataTypes
.
STRING
()))),
rows
);
}
}
/**
/**
...
@@ -171,7 +162,7 @@ public final class SqlManager {
...
@@ -171,7 +162,7 @@ public final class SqlManager {
for
(
String
key
:
sqlFragments
.
keySet
())
{
for
(
String
key
:
sqlFragments
.
keySet
())
{
rows
.
add
(
Row
.
of
(
key
));
rows
.
add
(
Row
.
of
(
key
));
}
}
return
CustomTableResultImpl
.
buildTableResult
(
new
ArrayList
<>(
Arrays
.
asList
(
new
TableSchemaField
(
"fragmentName"
,
DataTypes
.
STRING
()
))),
rows
);
return
CustomTableResultImpl
.
buildTableResult
(
Collections
.
singletonList
(
new
TableSchemaField
(
"fragmentName"
,
DataTypes
.
STRING
(
))),
rows
);
}
}
public
Iterator
getSqlFragmentsIterator
()
{
public
Iterator
getSqlFragmentsIterator
()
{
...
@@ -179,10 +170,7 @@ public final class SqlManager {
...
@@ -179,10 +170,7 @@ public final class SqlManager {
}
}
public
Table
getSqlFragmentsTable
(
CustomTableEnvironmentImpl
environment
)
{
public
Table
getSqlFragmentsTable
(
CustomTableEnvironmentImpl
environment
)
{
List
<
String
>
keys
=
new
ArrayList
<>();
List
<
String
>
keys
=
new
ArrayList
<>(
sqlFragments
.
keySet
());
for
(
String
key
:
sqlFragments
.
keySet
())
{
keys
.
add
(
key
);
}
return
environment
.
fromValues
(
keys
);
return
environment
.
fromValues
(
keys
);
}
}
...
@@ -200,28 +188,22 @@ public final class SqlManager {
...
@@ -200,28 +188,22 @@ public final class SqlManager {
if
(
Asserts
.
isNullString
(
statement
))
{
if
(
Asserts
.
isNullString
(
statement
))
{
return
statement
;
return
statement
;
}
}
String
[]
strs
=
statement
.
split
(
SystemConfiguration
.
getInstances
().
getSqlSeparator
());
String
[]
values
=
statement
.
split
(
SystemConfiguration
.
getInstances
().
getSqlSeparator
());
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
strs
.
length
;
i
++)
{
for
(
String
assignment
:
values
)
{
String
str
=
strs
[
i
];
String
[]
splits
=
assignment
.
split
(
FlinkSQLConstant
.
FRAGMENTS
,
2
);
if
(
str
.
trim
().
length
()
==
0
)
{
if
(
splits
.
length
==
2
)
{
continue
;
if
(
splits
[
0
].
trim
().
isEmpty
())
{
}
str
=
strs
[
i
];
if
(
str
.
contains
(
FlinkSQLConstant
.
FRAGMENTS
))
{
String
[]
strs2
=
str
.
split
(
FlinkSQLConstant
.
FRAGMENTS
);
if
(
strs2
.
length
>=
2
)
{
if
(
strs2
[
0
].
length
()
==
0
)
{
throw
new
ExpressionParserException
(
"Illegal variable name."
);
throw
new
ExpressionParserException
(
"Illegal variable name."
);
}
}
String
valueString
=
str
.
substring
(
str
.
indexOf
(
FlinkSQLConstant
.
FRAGMENTS
)
+
2
);
this
.
registerSqlFragment
(
splits
[
0
],
replaceVariable
(
splits
[
1
]));
this
.
registerSqlFragment
(
strs2
[
0
],
replaceVariable
(
valueString
));
}
else
if
(
splits
.
length
==
1
)
{
// string not contains FlinkSQLConstant.FRAGMENTS
sb
.
append
(
replaceVariable
(
assignment
));
}
else
{
}
else
{
throw
new
ExpressionParserException
(
"Illegal variable definition."
);
throw
new
ExpressionParserException
(
"Illegal variable definition."
);
}
}
}
else
{
sb
.
append
(
replaceVariable
(
str
));
}
}
}
return
sb
.
toString
();
return
sb
.
toString
();
}
}
...
@@ -232,15 +214,13 @@ public final class SqlManager {
...
@@ -232,15 +214,13 @@ public final class SqlManager {
* @param statement A sql will be replaced.
* @param statement A sql will be replaced.
*/
*/
private
String
replaceVariable
(
String
statement
)
{
private
String
replaceVariable
(
String
statement
)
{
String
pattern
=
"\\$\\{(.+?)\\}"
;
Pattern
p
=
Pattern
.
compile
(
"\\$\\{(.+?)}"
);
Pattern
p
=
Pattern
.
compile
(
pattern
);
Matcher
m
=
p
.
matcher
(
statement
);
Matcher
m
=
p
.
matcher
(
statement
);
StringBuffer
sb
=
new
StringBuffer
();
StringBuffer
sb
=
new
StringBuffer
();
while
(
m
.
find
())
{
while
(
m
.
find
())
{
String
key
=
m
.
group
(
1
);
String
key
=
m
.
group
(
1
);
String
value
=
this
.
getSqlFragment
(
key
);
String
value
=
this
.
getSqlFragment
(
key
);
m
.
appendReplacement
(
sb
,
""
);
m
.
appendReplacement
(
sb
,
value
==
null
?
""
:
value
);
sb
.
append
(
value
==
null
?
""
:
value
);
}
}
m
.
appendTail
(
sb
);
m
.
appendTail
(
sb
);
return
sb
.
toString
();
return
sb
.
toString
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment