Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
schbrain-parent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
schbrain-parent
Commits
794edf56
Commit
794edf56
authored
Jul 26, 2023
by
liaozan
🏀
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
8db1d48e
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
55 additions
and
51 deletions
+55
-51
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/ColumnMetaRowMapper.java
...autoconfigure/mybatis/constraint/ColumnMetaRowMapper.java
+3
-3
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/DefaultTableConstraintChecker.java
...ure/mybatis/constraint/DefaultTableConstraintChecker.java
+41
-37
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/TableConstraintCheckFailureAnalyzer.java
...batis/constraint/TableConstraintCheckFailureAnalyzer.java
+1
-1
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/exception/TableConstraintException.java
...configure/mybatis/exception/TableConstraintException.java
+6
-4
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/listener/TableConstraintCheckerBean.java
...onfigure/mybatis/listener/TableConstraintCheckerBean.java
+4
-6
No files found.
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/ColumnMetaRowMapper.java
View file @
794edf56
...
@@ -17,15 +17,15 @@ public class ColumnMetaRowMapper implements RowMapper<ColumnMeta> {
...
@@ -17,15 +17,15 @@ public class ColumnMetaRowMapper implements RowMapper<ColumnMeta> {
columnMeta
.
setTableName
(
resultSet
.
getString
(
"TABLE_NAME"
));
columnMeta
.
setTableName
(
resultSet
.
getString
(
"TABLE_NAME"
));
columnMeta
.
setColumnName
(
resultSet
.
getString
(
"COLUMN_NAME"
));
columnMeta
.
setColumnName
(
resultSet
.
getString
(
"COLUMN_NAME"
));
columnMeta
.
setDataType
(
resultSet
.
getString
(
"DATA_TYPE"
));
columnMeta
.
setDataType
(
resultSet
.
getString
(
"DATA_TYPE"
));
columnMeta
.
setNullable
(
isNullable
(
resultSet
.
getString
(
"IS_NULLABLE"
)));
columnMeta
.
setNullable
(
toBoolean
(
resultSet
.
getString
(
"IS_NULLABLE"
)));
columnMeta
.
setColumnDefault
(
resultSet
.
getString
(
"COLUMN_DEFAULT"
));
columnMeta
.
setColumnDefault
(
resultSet
.
getString
(
"COLUMN_DEFAULT"
));
columnMeta
.
setExtra
(
resultSet
.
getString
(
"EXTRA"
));
columnMeta
.
setExtra
(
resultSet
.
getString
(
"EXTRA"
));
columnMeta
.
setIndexName
(
resultSet
.
getString
(
"INDEX_NAME"
));
columnMeta
.
setIndexName
(
resultSet
.
getString
(
"INDEX_NAME"
));
return
columnMeta
;
return
columnMeta
;
}
}
private
boolean
isNullable
(
String
value
)
{
private
boolean
toBoolean
(
String
value
)
{
return
"YES"
.
equalsIgnoreCase
(
value
);
return
"YES"
.
equalsIgnoreCase
(
value
);
}
}
}
}
\ No newline at end of file
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/DefaultTableConstraintChecker.java
View file @
794edf56
...
@@ -11,7 +11,6 @@ import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConst
...
@@ -11,7 +11,6 @@ import static com.schbrain.framework.autoconfigure.mybatis.constant.MybatisConst
* @author liaozan
* @author liaozan
* @since 2022/8/30
* @since 2022/8/30
*/
*/
@SuppressWarnings
(
"DuplicatedCode"
)
public
class
DefaultTableConstraintChecker
implements
TableConstraintChecker
{
public
class
DefaultTableConstraintChecker
implements
TableConstraintChecker
{
@Override
@Override
...
@@ -33,24 +32,25 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
...
@@ -33,24 +32,25 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
if
(
bizColumnField
==
null
)
{
if
(
bizColumnField
==
null
)
{
return
;
return
;
}
}
ColumnMeta
columnMeta
=
table
.
getColumnMeta
(
bizColumnField
.
getColumnName
());
if
(
columnMeta
==
null
)
{
ColumnMeta
bizColumnMeta
=
getColumnMeta
(
table
,
bizColumnField
.
getColumnName
());
addMissingFieldError
(
table
,
bizColumnField
.
getColumnName
());
if
(
bizColumnMeta
==
null
)
{
return
;
return
;
}
}
if
(
columnMeta
.
getIndexName
()
==
null
)
{
addError
(
table
,
columnMeta
,
"BizId field should create an index"
);
checkNotNull
(
table
,
bizColumnMeta
);
if
(
bizColumnMeta
.
getIndexName
()
==
null
)
{
addError
(
table
,
bizColumnMeta
,
"BizId field should create an index"
);
}
}
}
}
protected
void
checkIdField
(
Table
table
)
{
protected
void
checkIdField
(
Table
table
)
{
ColumnMeta
idColumnMeta
=
table
.
getColumnMeta
(
ID
);
ColumnMeta
idColumnMeta
=
getColumnMeta
(
table
,
ID
);
if
(
idColumnMeta
==
null
)
{
if
(
idColumnMeta
==
null
)
{
addMissingFieldError
(
table
,
ID
);
return
;
return
;
}
}
checkN
ullable
(
table
,
idColumnMeta
);
checkN
otNull
(
table
,
idColumnMeta
);
if
(!
MysqlType
.
BIGINT
.
getName
().
equalsIgnoreCase
(
idColumnMeta
.
getDataType
()))
{
if
(!
MysqlType
.
BIGINT
.
getName
().
equalsIgnoreCase
(
idColumnMeta
.
getDataType
()))
{
addError
(
table
,
idColumnMeta
,
"should be type of 'bigint'"
);
addError
(
table
,
idColumnMeta
,
"should be type of 'bigint'"
);
}
}
...
@@ -60,61 +60,47 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
...
@@ -60,61 +60,47 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
}
}
protected
void
checkCreateTimeField
(
Table
table
)
{
protected
void
checkCreateTimeField
(
Table
table
)
{
ColumnMeta
createTimeColumnMeta
=
table
.
getColumnMeta
(
CREATE_TIME
);
ColumnMeta
createTimeColumnMeta
=
getColumnMeta
(
table
,
CREATE_TIME
);
if
(
createTimeColumnMeta
==
null
)
{
if
(
createTimeColumnMeta
==
null
)
{
addMissingFieldError
(
table
,
CREATE_TIME
);
return
;
return
;
}
}
checkNullable
(
table
,
createTimeColumnMeta
);
checkNotNull
(
table
,
createTimeColumnMeta
);
if
(!
MysqlType
.
DATETIME
.
getName
().
equalsIgnoreCase
(
createTimeColumnMeta
.
getDataType
()))
{
checkDatetimeField
(
table
,
createTimeColumnMeta
);
addError
(
table
,
createTimeColumnMeta
,
"should be type of 'datetime'"
);
}
if
(!
CURRENT_TIMESTAMP
.
equalsIgnoreCase
(
createTimeColumnMeta
.
getColumnDefault
()))
{
addError
(
table
,
createTimeColumnMeta
,
"default value should be 'current_timestamp'"
);
}
}
}
protected
void
checkModifyTimeField
(
Table
table
)
{
protected
void
checkModifyTimeField
(
Table
table
)
{
ColumnMeta
modifyTimeColumnMeta
=
table
.
getColumnMeta
(
MODIFY_TIME
);
ColumnMeta
modifyTimeColumnMeta
=
getColumnMeta
(
table
,
MODIFY_TIME
);
if
(
modifyTimeColumnMeta
==
null
)
{
if
(
modifyTimeColumnMeta
==
null
)
{
addMissingFieldError
(
table
,
MODIFY_TIME
);
return
;
return
;
}
}
checkNullable
(
table
,
modifyTimeColumnMeta
);
checkNotNull
(
table
,
modifyTimeColumnMeta
);
if
(!
MysqlType
.
DATETIME
.
getName
().
equalsIgnoreCase
(
modifyTimeColumnMeta
.
getDataType
()))
{
checkDatetimeField
(
table
,
modifyTimeColumnMeta
);
addError
(
table
,
modifyTimeColumnMeta
,
"should be type of 'datetime'"
);
}
if
(!
CURRENT_TIMESTAMP
.
equalsIgnoreCase
(
modifyTimeColumnMeta
.
getColumnDefault
()))
{
addError
(
table
,
modifyTimeColumnMeta
,
"default value should be 'current_timestamp'"
);
}
if
(!
modifyTimeColumnMeta
.
getExtra
().
toLowerCase
().
contains
(
UPDATE_WITH_CURRENT_TIMESTAMP
))
{
if
(!
modifyTimeColumnMeta
.
getExtra
().
toLowerCase
().
contains
(
UPDATE_WITH_CURRENT_TIMESTAMP
))
{
addError
(
table
,
modifyTimeColumnMeta
,
"need set to 'on update current_timestamp'"
);
addError
(
table
,
modifyTimeColumnMeta
,
"need set to 'on update current_timestamp'"
);
}
}
}
}
protected
void
checkDeletedField
(
Table
table
)
{
protected
void
checkDeletedField
(
Table
table
)
{
ColumnMeta
deletedColumnMeta
=
table
.
getColumnMeta
(
DELETED
);
ColumnMeta
deletedColumnMeta
=
getColumnMeta
(
table
,
DELETED
);
if
(
deletedColumnMeta
==
null
)
{
if
(
deletedColumnMeta
==
null
)
{
addMissingFieldError
(
table
,
DELETED
);
return
;
return
;
}
}
checkN
ullable
(
table
,
deletedColumnMeta
);
checkN
otNull
(
table
,
deletedColumnMeta
);
if
(!
MysqlType
.
TINYINT
.
getName
().
equalsIgnoreCase
(
deletedColumnMeta
.
getDataType
()))
{
if
(!
MysqlType
.
TINYINT
.
getName
().
equalsIgnoreCase
(
deletedColumnMeta
.
getDataType
()))
{
addError
(
table
,
deletedColumnMeta
,
"should be type of 'tinyint'"
);
addError
(
table
,
deletedColumnMeta
,
"should be type of 'tinyint'"
);
}
}
}
}
protected
void
checkDeleteVersionField
(
Table
table
)
{
protected
void
checkDeleteVersionField
(
Table
table
)
{
ColumnMeta
deleteVersionColumnMeta
=
table
.
getColumnMeta
(
DELETE_VERSION
);
ColumnMeta
deleteVersionColumnMeta
=
getColumnMeta
(
table
,
DELETE_VERSION
);
if
(
deleteVersionColumnMeta
==
null
)
{
if
(
deleteVersionColumnMeta
==
null
)
{
addMissingFieldError
(
table
,
DELETE_VERSION
);
return
;
return
;
}
}
checkN
ullable
(
table
,
deleteVersionColumnMeta
);
checkN
otNull
(
table
,
deleteVersionColumnMeta
);
if
(!
MysqlType
.
BIGINT
.
getName
().
equalsIgnoreCase
(
deleteVersionColumnMeta
.
getDataType
()))
{
if
(!
MysqlType
.
BIGINT
.
getName
().
equalsIgnoreCase
(
deleteVersionColumnMeta
.
getDataType
()))
{
addError
(
table
,
deleteVersionColumnMeta
,
"should be type of 'bigint'"
);
addError
(
table
,
deleteVersionColumnMeta
,
"should be type of 'bigint'"
);
}
}
...
@@ -123,18 +109,36 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
...
@@ -123,18 +109,36 @@ public class DefaultTableConstraintChecker implements TableConstraintChecker {
}
}
}
}
protected
void
checkN
ullable
(
Table
table
,
ColumnMeta
columnMeta
)
{
protected
void
checkN
otNull
(
Table
table
,
ColumnMeta
columnMeta
)
{
if
(
columnMeta
.
isNullable
())
{
if
(
columnMeta
.
isNullable
())
{
addError
(
table
,
columnMeta
,
"should be 'not null'"
);
addError
(
table
,
columnMeta
,
"should be 'not null'"
);
}
}
}
}
private
void
addError
(
Table
table
,
ColumnMeta
columnMeta
,
String
errorMsg
)
{
private
ColumnMeta
getColumnMeta
(
Table
table
,
String
columnName
)
{
addError
(
table
,
columnMeta
.
getColumnName
(),
errorMsg
);
ColumnMeta
columnMeta
=
table
.
getColumnMeta
(
columnName
);
if
(
columnMeta
==
null
)
{
addMissingFieldError
(
table
,
columnName
);
return
null
;
}
return
columnMeta
;
}
private
void
checkDatetimeField
(
Table
table
,
ColumnMeta
columnMeta
)
{
if
(!
MysqlType
.
DATETIME
.
getName
().
equalsIgnoreCase
(
columnMeta
.
getDataType
()))
{
addError
(
table
,
columnMeta
,
"should be type of 'datetime'"
);
}
if
(!
CURRENT_TIMESTAMP
.
equalsIgnoreCase
(
columnMeta
.
getColumnDefault
()))
{
addError
(
table
,
columnMeta
,
"default value should be 'current_timestamp'"
);
}
}
}
private
void
addMissingFieldError
(
Table
table
,
String
columnName
)
{
private
void
addMissingFieldError
(
Table
table
,
String
columnName
)
{
addError
(
table
,
columnName
,
"not exist"
);
table
.
addError
(
TableConstraintException
.
ofColumnNotExist
(
table
.
getTableName
(),
columnName
));
}
private
void
addError
(
Table
table
,
ColumnMeta
columnMeta
,
String
errorMsg
)
{
addError
(
table
,
columnMeta
.
getColumnName
(),
errorMsg
);
}
}
private
void
addError
(
Table
table
,
String
columnName
,
String
errorMsg
)
{
private
void
addError
(
Table
table
,
String
columnName
,
String
errorMsg
)
{
...
...
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/constraint/TableConstraintCheckFailureAnalyzer.java
View file @
794edf56
...
@@ -17,4 +17,4 @@ public class TableConstraintCheckFailureAnalyzer extends AbstractFailureAnalyzer
...
@@ -17,4 +17,4 @@ public class TableConstraintCheckFailureAnalyzer extends AbstractFailureAnalyzer
return
new
FailureAnalysis
(
cause
.
getMessage
(),
null
,
cause
);
return
new
FailureAnalysis
(
cause
.
getMessage
(),
null
,
cause
);
}
}
}
}
\ No newline at end of file
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/exception/TableConstraintException.java
View file @
794edf56
...
@@ -3,7 +3,6 @@ package com.schbrain.framework.autoconfigure.mybatis.exception;
...
@@ -3,7 +3,6 @@ package com.schbrain.framework.autoconfigure.mybatis.exception;
import
cn.hutool.core.text.StrFormatter
;
import
cn.hutool.core.text.StrFormatter
;
import
com.schbrain.common.exception.BaseException
;
import
com.schbrain.common.exception.BaseException
;
import
com.schbrain.common.util.StreamUtils
;
import
com.schbrain.common.util.StreamUtils
;
import
lombok.Getter
;
import
java.util.List
;
import
java.util.List
;
...
@@ -11,7 +10,6 @@ import java.util.List;
...
@@ -11,7 +10,6 @@ import java.util.List;
* @author liaozan
* @author liaozan
* @since 2022/8/30
* @since 2022/8/30
*/
*/
@Getter
public
class
TableConstraintException
extends
BaseException
{
public
class
TableConstraintException
extends
BaseException
{
private
static
final
long
serialVersionUID
=
-
3139175416089223586L
;
private
static
final
long
serialVersionUID
=
-
3139175416089223586L
;
...
@@ -21,11 +19,15 @@ public class TableConstraintException extends BaseException {
...
@@ -21,11 +19,15 @@ public class TableConstraintException extends BaseException {
}
}
public
TableConstraintException
(
String
tableName
,
String
column
,
String
message
)
{
public
TableConstraintException
(
String
tableName
,
String
column
,
String
message
)
{
super
(
"Table: '"
+
tableName
+
"'
, Column: '"
+
column
+
"'
"
+
message
);
super
(
"Table: '"
+
tableName
+
"'
, Column: '"
+
column
+
"' :
"
+
message
);
}
}
public
TableConstraintException
(
List
<
TableConstraintException
>
errors
)
{
public
TableConstraintException
(
List
<
TableConstraintException
>
errors
)
{
super
(
StreamUtils
.
join
(
StreamUtils
.
toList
(
errors
,
Throwable:
:
getMessage
),
System
.
lineSeparator
()));
super
(
StreamUtils
.
join
(
StreamUtils
.
toList
(
errors
,
Throwable:
:
getMessage
),
System
.
lineSeparator
()));
}
}
}
public
static
TableConstraintException
ofColumnNotExist
(
String
tableName
,
String
column
)
{
\ No newline at end of file
return
new
TableConstraintException
(
tableName
,
column
,
"not exist"
);
}
}
starters/mybatis-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/mybatis/listener/TableConstraintCheckerBean.java
View file @
794edf56
...
@@ -64,8 +64,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
...
@@ -64,8 +64,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
List
<
TableMetaDataLoader
>
metaDataLoaders
=
beanFactory
.
getBeanProvider
(
TableMetaDataLoader
.
class
).
orderedStream
().
collect
(
toList
());
List
<
TableMetaDataLoader
>
metaDataLoaders
=
beanFactory
.
getBeanProvider
(
TableMetaDataLoader
.
class
).
orderedStream
().
collect
(
toList
());
if
(
CollectionUtils
.
isEmpty
(
metaDataLoaders
))
{
if
(
CollectionUtils
.
isEmpty
(
metaDataLoaders
))
{
JdbcTemplate
jdbcTemplate
=
beanFactory
.
getBean
(
JdbcTemplate
.
class
);
JdbcTemplate
jdbcTemplate
=
beanFactory
.
getBean
(
JdbcTemplate
.
class
);
// Avoid add to a immutable collection
metaDataLoaders
.
add
(
new
DefaultTableMetaDataLoader
(
jdbcTemplate
));
metaDataLoaders
=
List
.
of
(
new
DefaultTableMetaDataLoader
(
jdbcTemplate
));
}
}
Map
<
String
,
List
<
ColumnMeta
>>
tableMetadata
=
loadTableMetadata
(
metaDataLoaders
);
Map
<
String
,
List
<
ColumnMeta
>>
tableMetadata
=
loadTableMetadata
(
metaDataLoaders
);
...
@@ -76,8 +75,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
...
@@ -76,8 +75,7 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
List
<
TableConstraintChecker
>
checkers
=
beanFactory
.
getBeanProvider
(
TableConstraintChecker
.
class
).
orderedStream
().
collect
(
toList
());
List
<
TableConstraintChecker
>
checkers
=
beanFactory
.
getBeanProvider
(
TableConstraintChecker
.
class
).
orderedStream
().
collect
(
toList
());
if
(
CollectionUtils
.
isEmpty
(
checkers
))
{
if
(
CollectionUtils
.
isEmpty
(
checkers
))
{
// Avoid add to a immutable collection
checkers
.
add
(
new
DefaultTableConstraintChecker
());
checkers
=
List
.
of
(
new
DefaultTableConstraintChecker
());
}
}
List
<
TableConstraintException
>
errors
=
new
ArrayList
<>();
List
<
TableConstraintException
>
errors
=
new
ArrayList
<>();
...
@@ -172,9 +170,9 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
...
@@ -172,9 +170,9 @@ public class TableConstraintCheckerBean implements SmartInitializingSingleton, B
private
void
checkAllFieldExist
(
Table
table
)
{
private
void
checkAllFieldExist
(
Table
table
)
{
for
(
FieldInfo
fieldInfo
:
table
.
getFieldInfoList
())
{
for
(
FieldInfo
fieldInfo
:
table
.
getFieldInfoList
())
{
if
(!
table
.
containsColumn
(
fieldInfo
.
getColumn
()))
{
if
(!
table
.
containsColumn
(
fieldInfo
.
getColumn
()))
{
table
.
addError
(
new
TableConstraintException
(
table
.
getTableName
(),
fieldInfo
.
getColumn
(),
"not exist"
));
table
.
addError
(
TableConstraintException
.
ofColumnNotExist
(
table
.
getTableName
(),
fieldInfo
.
getColumn
()
));
}
}
}
}
}
}
}
}
\ No newline at end of file
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