Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding regex on rules.json #59

Merged
merged 2 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.90] - 2021-12-30
### Added
- regex processing rules.json file for fields: columnDefaultOut. columnDefaultIn, checkConditionOut, checkConditionIn

## [1.89] - 2021-12-29
### Fixed
- wrong recreate procedure output
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mainClassName = "br.com.bluesoft.bee.Bee"

group = 'br.com.bluesoft.bee'
def artifact = 'bee'
version = '1.89'
version = '1.90'

def javaVersion = JavaVersion.VERSION_1_8
sourceCompatibility = javaVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class BeeMySqlSchemaCreatorAction implements ActionRunner {
out.log('importing schema metadata from the reference files')
def schema = getImporter().importMetaData()
schema.rdbms = RDBMS.MYSQL
schema = new RulesConverter().toSchema(schema)

if (objectName) {
schema = schema.filter(objectName)
}
schema = new RulesConverter().toSchema(schema)

def file = new File('bee.sql')
if (file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class BeeOracleSchemaCreatorAction implements ActionRunner {
out.log('importing schema metadata from the reference files')
def schema = getImporter().importMetaData()
schema.rdbms = RDBMS.ORACLE
schema = new RulesConverter().toSchema(schema)

if (objectName) {
schema = schema.filter(objectName)
}
schema = new RulesConverter().toSchema(schema)

def file = new File('bee.sql')
if (file.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public class BeePostgresSchemaCreatorAction implements ActionRunner {
out.log('importing schema metadata from the reference files')
def schema = getImporter().importMetaData()
schema.rdbms = RDBMS.POSTGRES
schema = new RulesConverter().toSchema(schema)

if (objectName) {
schema = schema.filter(objectName)
}
schema = new RulesConverter().toSchema(schema)

def file = new File('bee.sql')
if (file.exists()) {
Expand Down
74 changes: 64 additions & 10 deletions src/main/groovy/br/com/bluesoft/bee/service/RulesConverter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,26 @@ import br.com.bluesoft.bee.model.Table
import br.com.bluesoft.bee.model.TableColumn
import br.com.bluesoft.bee.model.rule.DataType
import br.com.bluesoft.bee.model.rule.Rule
import org.apache.groovy.util.Maps

import java.util.regex.Pattern

class RulesConverter {

Map<String, String> checksConst = [:]
List<String[]> checksRegex = []
Map<String, Map<String, String>> colDefConst = [:]
Map<String, List<String[]>> colDefRegex = [:]


Schema toSchema(Schema source) {
if(!source.rules?.containsKey(source.rdbms))
return source

Rule rule = source.rules[source.rdbms]
prepareRules(rule.columnDefaultOut, rule.checkConditionOut)
def schema = source.clone()
schema.tables = source.tables.collectEntries { k, v -> [k, convertTable(v, rule.dataTypeOut, rule.columnDefaultOut, rule.checkConditionOut)]}
schema.tables = source.tables.collectEntries { k, v -> [k, convertTable(v, rule.dataTypeOut)]}
return schema
}

Expand All @@ -24,32 +34,45 @@ class RulesConverter {
return source

Rule rule = source.rules[source.rdbms]
prepareRules(rule.columnDefaultIn, rule.checkConditionIn)
def schema = source.clone()
schema.tables = source.tables.collectEntries { k, v -> [k, convertTable(v, rule.dataTypeIn, rule.columnDefaultIn, rule.checkConditionIn)]}
schema.tables = source.tables.collectEntries { k, v -> [k, convertTable(v, rule.dataTypeIn)]}
return schema
}

Table convertTable(Table source, List<DataType> dataTypes, Map<String, Map<String, String>> columnDefaults, Map<String, String> checks) {
Table convertTable(Table source, List<DataType> dataTypes) {
def result = source.clone()
result.columns = source.columns.collectEntries({ k, v ->
def dataType = findDataType(v, dataTypes)
return [k, convertColumn(v, dataType, columnDefaults[v.type]?[v.defaultValue])]
def defaultValue = findDefaultValue(v)
return [k, convertColumn(v, dataType, defaultValue)]
})
result.constraints = source.constraints.collectEntries { [it.key, convertCheckConstraint(it.value, checks)] }
result.constraints = source.constraints.collectEntries { [it.key, convertCheckConstraint(it.value)] }
return result
}

Constraint convertCheckConstraint(Constraint source, Map<String, String> checks) {
if(source.type == 'C' && source.searchCondition in checks) {
def result = source.clone()
result.searchCondition = checks[source.searchCondition]
return result
Constraint convertCheckConstraint(Constraint source) {
if (source.type == 'C') {
if (source.searchCondition in checksConst) {
def result = source.clone()
result.searchCondition = checksConst[source.searchCondition]
return result
} else {
checksRegex.each {
if(source.searchCondition.matches(it.key)) {
def result = source.clone()
result.searchCondition = result.searchCondition.replaceAll(it.key, it.value)
return result
}
}
}
}
return source
}

def convertColumn(TableColumn column, DataType dataType, String defaultValue) {
if(dataType == null && defaultValue == null) return column

TableColumn result = column.clone()
if(dataType != null) {
if(dataType.toType != null) {
Expand All @@ -62,6 +85,7 @@ class RulesConverter {
result.scale = dataType.toScale
}
}

if(defaultValue != null) {
result.defaultValue = defaultValue
}
Expand All @@ -77,4 +101,34 @@ class RulesConverter {
return true
}
}

String findDefaultValue(TableColumn column) {
if(column.defaultValue == null) {
return null
}

if(colDefConst[column.type]?[column.defaultValue]) {
return colDefConst[column.type]?[column.defaultValue]
}

for(it in colDefRegex[column.type]) {
if(column.defaultValue.matches(it[0])) {
def r = column.defaultValue.replaceAll(it[0], it[1])
return r
}
}
}

void prepareRules(Map<String, Map<String, String>> colDefRules, Map<String, String> checkRules) {
colDefConst = colDefRules.collectEntries { k, v ->
Map<String, String> c = v.findAll {!it.key.startsWith("~")}
return [k, c]
}
colDefRegex = colDefRules.collectEntries { k, v ->
def c = v.findAll { it.key.startsWith("~")}.collect { [it.key.substring(1), it.value] as String[]}
return [k, c]
}
checksConst = checkRules.findAll { !it.key.startsWith("~")}
checksRegex = checkRules.findAll { it.key.startsWith("~")}.collect { [it.key.substring(1), it.value]}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class RulesConverterTest extends Specification {
table | column | type | size | scale | defaultValue
'cof' | 'codigo_operacao_fiscal' | 'integer' | 0 | 0 | null
'cof' | 'basico' | 'number' | 1 | 0 | null
'cof' | 'descricao' | 'varchar' | 100 | 0 | "teste"
'cof' | 'cof_nf_str' | 'varchar' | 4 | 0 | "'1'::character varying"
'pessoa' | 'data_nascimento' | 'date' | 7 | 0 | 'current_date'
'pessoa' | 'fundacao' | 'timestamp' | 7 | 0 | 'current_timestamp'

Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
},
"timestamp": {
"sysdate": "current_timestamp"
},
"varchar": {
"~(.*)'$": "$1'::character varying"
}
},
"dataTypeIn": [
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/tables/cof.bee
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
descricao : {
name : "descricao",
type : "varchar",
defaultValue : "teste",
size : 100,
nullable : true
},
Expand Down Expand Up @@ -56,6 +57,7 @@
cof_nf_str : {
name : "cof_nf_str",
type : "varchar",
defaultValue : "'1'",
size : 4,
nullable : true
},
Expand Down