Skip to content

Commit

Permalink
Merge pull request #76 from bluesoft/feature/pgsql_copy
Browse files Browse the repository at this point in the history
Changed schema:recreate_postgres to user copy instead of insert
  • Loading branch information
dcarneir authored Jun 27, 2024
2 parents ed3d515 + 81265d3 commit 8a3626d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
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.103] - 2024-06-26
### Changed
- Changed schema:recreate_postgres to user copy instead of insert

## [1.102] - 2024-01-11
### Fixed
- Connection leak on DbChangeManager
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.102'
version = '1.103'

def javaVersion = JavaVersion.VERSION_1_8
sourceCompatibility = javaVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.com.bluesoft.bee.schema

import br.com.bluesoft.bee.util.CsvUtil
import br.com.bluesoft.bee.util.RDBMS

class BeePostgresSchemaCreator extends BeeSchemaCreator {
Expand Down Expand Up @@ -76,6 +77,69 @@ class BeePostgresSchemaCreator extends BeeSchemaCreator {
}
}

void createCsvData(def file, def csvFile, def schema, def useCommit) {
def tableName = csvFile.name.split('\\.')[0]
def fileData = CsvUtil.read(csvFile)
def table = schema.tables[tableName]
def columnNames = []
def columns = [:]
def columnTypes = [:]
def isVirtualColumn = [:]
def numberOfVirtualColumns = 0

if (table != null) {
table.columns.findAll { !it.value.ignore }.each {
columns[it.value.name] = it.value.type
columnNames << it.value.name
isVirtualColumn[it.value.name] = it.value.virtual
if (it.value.virtual) {
numberOfVirtualColumns++
}
}

columnNames.eachWithIndex { columName, index ->
columnTypes[index] = columns[columName]
}

def counterValue = 1

def query = new StringBuilder()
query << "copy ${tableName} (${table.columns.findAll({!it.value.virtual && !it.value.ignore})*.value.name.join(",")}) from stdin;\n"

for (int i = 0; i < fileData.size(); i++) {
fileData[i].eachWithIndex { columnValue, index2 ->
def fieldValue = columnValue.toString().replace('\t', '\\t').replace('\\', '\\\\')
def columnType = columnTypes[index2].split(' ')[0].split('\\(')[0]
def columnName = columnNames[index2]
def isVirtual = isVirtualColumn[columnName]
def isBoolean = columnType == 'boolean'
if (!isVirtual) {
if(isBoolean) {
fieldValue = toBoolean(fieldValue)
}
if(fieldValue == "null") {
fieldValue = '\\N'
}
query << fieldValue
}
if ((counterValue + numberOfVirtualColumns) < (columnNames.size())) {
query << "\t"
}
counterValue++
}
query << "\n"
counterValue = 1
}

query << "\\.\n\n"

if(useCommit) {
query << "commit;\n"
}
file.append(query.toString(), 'utf-8')
}
}

void createCoreData(def file, def schema, def dataFolderPath) {
if (!schema.filtered) {
super.createCoreData(file, schema, dataFolderPath, false)
Expand Down

0 comments on commit 8a3626d

Please sign in to comment.