Skip to content

Commit

Permalink
#4569 [Data] Import SQL script in Schema - Add support for processing…
Browse files Browse the repository at this point in the history
… of binary content
  • Loading branch information
ThuF committed Jan 22, 2025
1 parent 50ed4df commit 792896a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,38 @@
*/
package org.eclipse.dirigible.components.data.sources.manager;

import com.zaxxer.hikari.HikariConfig;
import static java.text.MessageFormat.format;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import org.eclipse.dirigible.commons.config.Configuration;
import org.eclipse.dirigible.commons.config.DirigibleConfig;
import org.eclipse.dirigible.components.data.sources.config.DefaultDataSourceName;
import org.eclipse.dirigible.components.data.sources.config.SystemDataSourceName;
import org.eclipse.dirigible.components.data.sources.domain.DataSource;
import org.eclipse.dirigible.components.data.sources.domain.DataSourceProperty;
import org.eclipse.dirigible.components.database.*;
import org.eclipse.dirigible.components.database.DatabaseConfigurator;
import org.eclipse.dirigible.components.database.DatabaseParameters;
import org.eclipse.dirigible.components.database.DatabaseSystem;
import org.eclipse.dirigible.components.database.DatabaseSystemDeterminer;
import org.eclipse.dirigible.components.database.DirigibleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.TimeUnit;

import static java.text.MessageFormat.format;
import com.zaxxer.hikari.HikariConfig;

/**
* The Class DataSourceInitializer.
Expand Down Expand Up @@ -141,10 +151,16 @@ private DirigibleDataSource initDataSource(DataSource dataSource) {
config.setMaximumPoolSize(20);

config.setMinimumIdle(10);
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(3)); // free connections when idle, potentially remove leaked connections
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(15)); // recreate connections after specified time

// free connections when idle, potentially remove leaked connections
config.setIdleTimeout(TimeUnit.MINUTES.toMillis(3));

// recreate connections after specified time
config.setMaxLifetime(TimeUnit.MINUTES.toMillis(Configuration.getAsInt(name + "_MAX_LIFETIME_MINUTES", 15)));
config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(15));
config.setLeakDetectionThreshold(TimeUnit.MINUTES.toMillis(1)); // log message for possible leaked connection

// log message for possible leaked connection
config.setLeakDetectionThreshold(TimeUnit.MINUTES.toMillis(Configuration.getAsInt(name + "_LEAK_DETECTION_THRESHOLD_MINUTES", 1)));

applyDbSpecificConfigurations(dbType, config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@
*/
package org.eclipse.dirigible.database.sql.dialects;

import org.eclipse.dirigible.database.sql.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.dirigible.database.sql.DataType;
import org.eclipse.dirigible.database.sql.DatabaseArtifactTypes;
import org.eclipse.dirigible.database.sql.DatabaseType;
import org.eclipse.dirigible.database.sql.ISqlDialect;
import org.eclipse.dirigible.database.sql.ISqlKeywords;
import org.eclipse.dirigible.database.sql.SqlException;
import org.eclipse.dirigible.database.sql.builders.AlterBranchingBuilder;
import org.eclipse.dirigible.database.sql.builders.CreateBranchingBuilder;
import org.eclipse.dirigible.database.sql.builders.DropBranchingBuilder;
Expand All @@ -20,17 +40,8 @@
import org.eclipse.dirigible.database.sql.builders.records.UpdateBuilder;
import org.eclipse.dirigible.database.sql.builders.sequence.LastValueIdentityBuilder;
import org.eclipse.dirigible.database.sql.builders.sequence.NextValueSequenceBuilder;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The Default SQL Dialect.
Expand Down Expand Up @@ -64,6 +75,8 @@ public class DefaultSqlDialect<SELECT extends SelectBuilder, INSERT extends Inse
"nullif", "session_user", "system_user", "user", "version", "and", "or", "between", "binary", "case", "div", "in", "is", "not",
"null", "like", "rlike", "xor")));

private static final Logger logger = LoggerFactory.getLogger(DefaultSqlDialect.class);

/**
* Select.
*
Expand Down Expand Up @@ -604,11 +617,28 @@ public void importData(Connection connection, String table, InputStream input) t
*/
@Override
public void processSQL(Connection connection, String schema, InputStream is) throws Exception {
long startTime = System.currentTimeMillis();

BufferedReader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuilder builder = new StringBuilder();

boolean inSingleQuote = false;
boolean inDoubleQuote = false;
boolean escapeNextChar = false;
int ch;

while ((ch = reader.read()) != -1) {
if (ch == ';') {
if (escapeNextChar) {
escapeNextChar = false;
} else if (ch == '\\') {
escapeNextChar = true;
} else if (ch == '\'' && !inDoubleQuote) {
inSingleQuote = !inSingleQuote;
} else if (ch == '"' && !inSingleQuote) {
inDoubleQuote = !inDoubleQuote;
}

if (ch == ';' && !inSingleQuote && !inDoubleQuote && !escapeNextChar) {
executeUpdate(connection, builder.toString());
builder.setLength(0);
} else {
Expand All @@ -619,6 +649,8 @@ public void processSQL(Connection connection, String schema, InputStream is) thr
executeUpdate(connection, builder.toString());
}
reader.close();

logger.info("SQL dump processed for " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
}

/**
Expand All @@ -629,8 +661,11 @@ public void processSQL(Connection connection, String schema, InputStream is) thr
* @throws SQLException the SQL exception
*/
private void executeUpdate(Connection connection, String sql) throws SQLException {
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
if (sql.trim()
.length() > 0) {
PreparedStatement statement = connection.prepareStatement(sql);
statement.executeUpdate();
}
}

}

0 comments on commit 792896a

Please sign in to comment.