Skip to content

Commit

Permalink
Clean database code
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitgaudou committed May 11, 2022
1 parent 44accb3 commit ff3b411
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 241 deletions.
Expand Up @@ -28,7 +28,6 @@ global {

species DB_connection_tester skills: [SQLSKILL] {
init {
write "Current Time " + timeStamp();
write "Connection to MySQL is " + testConnection(MySQL);
write "Connection to SQLITE is " + testConnection(SQLITE);
write "Connection to POSTGRESQL is " + testConnection(POSTGRES);
Expand Down
Binary file not shown.
Expand Up @@ -70,7 +70,8 @@ public class SQLSkill extends Skill {
*/
// added from MaeliaSkill
@action (
name = "timeStamp")
name = "timeStamp",
doc = @doc(deprecated = "Use machine_time instead"))
public Long timeStamp(final IScope scope) throws GamaRuntimeException {
final Long timeStamp = System.currentTimeMillis();
return timeStamp;
Expand All @@ -90,7 +91,8 @@ public Long timeStamp(final IScope scope) throws GamaRuntimeException {
name = "dateFormat",
type = IType.STRING,
optional = false,
doc = @doc ("date format examples: 'yyyy-MM-dd' , 'yyyy-MM-dd HH:mm:ss' ")) })
doc = @doc (value = "date format examples: 'yyyy-MM-dd' , 'yyyy-MM-dd HH:mm:ss' ",
deprecated = "Use machine_time instead")) })
public String getCurrentDateTime(final IScope scope) throws GamaRuntimeException {
final String dateFormat = (String) scope.getArg("dateFormat", IType.STRING);
final DateFormat datef = new SimpleDateFormat(dateFormat);
Expand All @@ -111,7 +113,8 @@ public String getCurrentDateTime(final IScope scope) throws GamaRuntimeException
name = "dateFormat",
type = IType.STRING,
optional = false,
doc = @doc ("date format examples: 'yyyy-MM-dd' , 'yyyy-MM-dd HH:mm:ss' ")),
doc = @doc (value = "date format examples: 'yyyy-MM-dd' , 'yyyy-MM-dd HH:mm:ss' ",
deprecated = "Use date instead")),
@arg (
name = "dateStr",
type = IType.STRING,
Expand Down Expand Up @@ -205,21 +208,17 @@ public boolean testConnection(final IScope scope) {
doc = @doc ("List of values that are used to replace question mark")) })
public int executeUpdate_QM(final IScope scope) throws GamaRuntimeException {

// final java.util.Map params = (java.util.Map) scope.getArg("params", IType.MAP);
final String updateComm = (String) scope.getArg("updateComm", IType.STRING);
final IList<Object> values = (IList<Object>) scope.getArg("values", IType.LIST);
int row_count = -1;
// SqlConnection sqlConn;
try (final SqlConnection sqlConn = SqlUtils.createConnectionObject(scope); ) {
// sqlConn = SqlUtils.createConnectionObject(scope);
if (values.size() > 0) {
row_count = sqlConn.executeUpdateDB(scope, updateComm, values);
} else {
row_count = sqlConn.executeUpdateDB(scope, updateComm);
}

} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw GamaRuntimeException.error("SQLSkill.executeUpdateDB: " + e.toString(), scope);
}
Expand Down Expand Up @@ -266,29 +265,19 @@ public int executeUpdate_QM(final IScope scope) throws GamaRuntimeException {
type = IType.LIST,
optional = false,
doc = @doc ("List of values that are used to insert into table. Columns and values must have same size"))
// ,@arg(name = "transform", type = IType.BOOL, optional = true, doc
// =
// @doc("if transform = true then geometry will be tranformed from
// absolute to gis otherways it will be not transformed. Default
// value is false "))
})
public int insert(final IScope scope) throws GamaRuntimeException {

// SqlConnection sqlConn;
// final java.util.Map params = (java.util.Map) scope.getArg("params", IType.MAP);
final String table_name = (String) scope.getArg("into", IType.STRING);
final IList<Object> cols = (IList<Object>) scope.getArg("columns", IType.LIST);
final IList<Object> values = (IList<Object>) scope.getArg("values", IType.LIST);
int rec_no = -1;
try (final SqlConnection sqlConn = SqlUtils.createConnectionObject(scope); ) {
// sqlConn = SqlUtils.createConnectionObject(scope);
// Connection conn=sqlConn.connectDB();
if (cols.size() > 0) {
rec_no = sqlConn.insertDB(scope, table_name, cols, values);
} else {
rec_no = sqlConn.insertDB(scope, table_name, values);
}
// conn.close();
} catch (final Exception e) {
e.printStackTrace();
throw GamaRuntimeException.error("SQLSkill.insert: " + e.toString(), scope);
Expand Down Expand Up @@ -332,44 +321,21 @@ public int insert(final IScope scope) throws GamaRuntimeException {
type = IType.LIST,
optional = true,
doc = @doc ("List of values that are used to replace question marks"))
// ,@arg(name = "transform", type = IType.BOOL, optional = true, doc
// =
// @doc("if transform = true then geometry will be tranformed from
// absolute to gis otherways it will be not transformed. Default
// value is false "))

})
public IList select_QM(final IScope scope) throws GamaRuntimeException {

// final java.util.Map params = (java.util.Map) scope.getArg("params", IType.MAP);Ò
final String selectComm = (String) scope.getArg("select", IType.STRING);
final IList<Object> values = (IList<Object>) scope.getArg("values", IType.LIST);
// thai.truongminh@gmail.com
// Move transform arg of select to a key in params
// boolean transform = scope.hasArg("transform") ? (Boolean)
// scope.getArg("transform", IType.BOOL) : true;
// boolean transform = params.containsKey("transform") ? (Boolean)
// params.get("transform") : true;

// SqlConnection sqlConn;
IList<? super IList<Object>> repRequest;
try (final SqlConnection sqlConn = SqlUtils.createConnectionObject(scope); ) {
// sqlConn = SqlUtils.createConnectionObject(scope);
if (values.size() > 0) {
repRequest = sqlConn.executeQueryDB(scope, selectComm, values);
} else {
repRequest = sqlConn.selectDB(scope, selectComm);
}
// Transform GIS to Absolute (Geometry in GAMA)
// AD: now made directly in the select / query
// if ( transform ) {
// return sqlConn.fromGisToAbsolute(scope, repRequest);
// } else {
// return repRequest;
// }
return repRequest;
} catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw GamaRuntimeException.error("SQLSkill.select_QM: " + e.toString(), scope);
}
Expand Down
Expand Up @@ -113,7 +113,6 @@ public Object close(final IScope scope) throws GamaRuntimeException {
conn.close();
isConnection = false;
} catch (final SQLException e) {
// e.printStackTrace();
throw GamaRuntimeException.error("AgentDB.close error:" + e.toString(), scope);
} catch (final NullPointerException npe) {
if (conn == null) throw GamaRuntimeException
Expand All @@ -123,13 +122,6 @@ public Object close(final IScope scope) throws GamaRuntimeException {

}

// @action(name = "helloWorld")
// public Object helloWorld(final IScope scope) throws GamaRuntimeException
// {
// scope.getGui().informConsole("Hello World");
// return null;
// }

// Get current time of system
/**
* Time stamp.
Expand All @@ -143,7 +135,8 @@ public Object close(final IScope scope) throws GamaRuntimeException {
name = "timeStamp",
doc = @doc (
value = "Get the current time of the system.",
returns = "Current time of the system in millisecondes"))
returns = "Current time of the system in millisecondes",
deprecated = "Use machine_time instead"))
public Long timeStamp(final IScope scope) throws GamaRuntimeException {
return System.currentTimeMillis();
}
Expand Down Expand Up @@ -175,20 +168,6 @@ public Object connectDB(final IScope scope) throws GamaRuntimeException {

params = (java.util.Map<String, String>) scope.getArg("params", IType.MAP);

// final String dbtype = params.get("dbtype");
// Note BG: before 13/06/2020, SQLite was not supported in AgentDB.
// The reason is not clear, a guess is that when an agent update the database file,
// the file is locked and thus another agent cannot try to update it.
// As a consequence it is recommended to have a single AgentDB connected to SQLite DB file.

// final String dbtype = params.get("dbtype");
// SqlConnection sqlConn;
// if (dbtype.equalsIgnoreCase(SqlConnection.SQLITE)) {
// throw GamaRuntimeException.error(
// "AgentDB.connection to SQLite error: an AgentDB agent cannot connect to SQLite DBMS (cf. documentation for
// further info).",
// scope);
// }
if (isConnection)
throw GamaRuntimeException.error("AgentDB.connection error: a connection is already opened", scope);
try {
Expand Down Expand Up @@ -260,12 +239,6 @@ public boolean testConnection(final IScope scope) throws GamaRuntimeException {
type = IType.LIST,
optional = true,
doc = @doc ("List of values that are used to replace question marks"))
// , @arg(name = "transform", type = IType.BOOL, optional = true,
// doc =
// @doc("if transform = true then geometry will be tranformed from
// absolute to gis otherways it will be not transformed. Default
// value is false "))

},
doc = @doc (
value = "Make a connection to DBMS and execute the select statement.",
Expand All @@ -275,8 +248,6 @@ public IList select(final IScope scope) throws GamaRuntimeException {
if (!isConnection) throw GamaRuntimeException.error("AgentDB.select: Connection was not established ", scope);
final String selectComm = (String) scope.getArg("select", IType.STRING);
final IList<Object> values = (IList<Object>) scope.getArg("values", IType.LIST);
// Boolean transform = scope.hasArg("transform") ? (Boolean)
// scope.getArg("transform", IType.BOOL) : false;
IList<? super IList<? super IList>> repRequest;
// get data
try {
Expand All @@ -285,8 +256,6 @@ public IList select(final IScope scope) throws GamaRuntimeException {
} else {
repRequest = sqlConn.selectDB(scope, conn, selectComm);
}
// if ( transform ) { return sqlConn.fromGisToAbsolute(gis,
// repRequest); }
return repRequest;
} catch (final Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -322,11 +291,6 @@ public IList select(final IScope scope) throws GamaRuntimeException {
type = IType.LIST,
optional = true,
doc = @doc ("List of values that are used to replace question mark"))
// , @arg(name = "transform", type = IType.BOOL, optional = true,
// doc =
// @doc("if transform = true then geometry will be tranformed from
// absolute to gis otherways it will be not transformed. Default
// value is false "))
},
doc = @doc (
value = "- Make a connection to DBMS - Executes the SQL statement in this PreparedStatement object, which must be an SQL\n"
Expand Down Expand Up @@ -437,11 +401,6 @@ public Object setParameter(final IScope scope) throws GamaRuntimeException {
type = IType.LIST,
optional = false,
doc = @doc ("List of values that are used to insert into table. Columns and values must have same size"))
// ,@arg(name = "transform", type = IType.BOOL, optional = true, doc
// =
// @doc("if transform = true then geometry will be tranformed from
// absolute to gis otherways it will be not transformed. Default
// value is false "))
},
doc = @doc (
value = "- Make a connection to DBMS - Executes the insert statement.",
Expand All @@ -452,22 +411,12 @@ public int insert(final IScope scope) throws GamaRuntimeException {
final String table_name = (String) scope.getArg("into", IType.STRING);
final IList<Object> cols = (IList<Object>) scope.getArg("columns", IType.LIST);
final IList<Object> values = (IList<Object>) scope.getArg("values", IType.LIST);
// thai.truongminh@gmail.com
// Move transform arg of select to a key in params
// boolean transform = scope.hasArg("transform") ? (Boolean)
// scope.getArg("transform", IType.BOOL) : true;
// boolean transform = params.containsKey("transform") ? (Boolean)
// params.get("transform") : true;
int rec_no = -1;

try {
if (cols.size() > 0) {
// rec_no = sqlConn.insertDB(scope, conn, table_name, cols,
// values, transform);
rec_no = sqlConn.insertDB(scope, conn, table_name, cols, values);
} else {
// rec_no = sqlConn.insertDB(scope, table_name, values,
// transform);
rec_no = sqlConn.insertDB(scope, conn, table_name, values);
}
} catch (final Exception e) {
Expand Down
Expand Up @@ -11,30 +11,25 @@
package irit.gaml.extensions.database.utils.geosql;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.mysql.MySQLDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.filter.text.ecql.ECQL;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.Feature;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryType;
import org.opengis.filter.Filter;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import irit.gaml.extensions.database.utils.sql.ISqlConnector;
import irit.gaml.extensions.database.utils.sql.SqlUtils;
import irit.gaml.extensions.database.utils.sql.mysql.MySqlConnection;
import irit.gaml.extensions.database.utils.sql.postgres.PostgresConnection;
import irit.gaml.extensions.database.utils.sql.sqlite.SqliteConnection;
import msi.gama.common.geometry.Envelope3D;
import msi.gama.common.util.FileUtils;
import msi.gama.metamodel.shape.GamaGisGeometry;
Expand All @@ -57,18 +52,6 @@
@SuppressWarnings ({ "rawtypes", "unchecked" })
public class GamaSqlConnection extends GamaGisFile {

// /** The Constant MYSQL. */
// private static final String MYSQL = "mysql";

// /** The Constant POSTGRES. */
// private static final String POSTGRES = "postgres";

// /** The Constant POSTGIS. */
// private static final String POSTGIS = "postgis";

// /** The Constant SQLITE. */
// private static final String SQLITE = "spatialite";

/** The dbtype. */
private String dbtype = "";

Expand Down Expand Up @@ -176,42 +159,8 @@ private void setConnectionParameters(final Map<String, Object> params) {
*/
// Connect connection parameters with connection attributes of the object
private Map<String, Object> createConnectionParams(final IScope scope) {
Map<String, Object> connectionParameters = new HashMap<>();

if (PostgresConnection.POSTGRES.equalsIgnoreCase(dbtype) || PostgresConnection.POSTGIS.equalsIgnoreCase(dbtype)) {
connectionParameters.put("host", host);
connectionParameters.put("dbtype", dbtype);
connectionParameters.put("port", port);
connectionParameters.put("database", database);
connectionParameters.put("user", user);
connectionParameters.put("passwd", passwd);
// advanced

} else if (MySqlConnection.MYSQL.equalsIgnoreCase(dbtype)) {
connectionParameters.put(MySQLDataStoreFactory.DBTYPE.key, dbtype);
connectionParameters.put(JDBCDataStoreFactory.HOST.key, host);
connectionParameters.put(MySQLDataStoreFactory.PORT.key, Integer.valueOf(port));
connectionParameters.put(JDBCDataStoreFactory.DATABASE.key, database);
connectionParameters.put(JDBCDataStoreFactory.USER.key, user);
connectionParameters.put(JDBCDataStoreFactory.PASSWD.key, passwd);
} else if (SqliteConnection.SQLITE.equalsIgnoreCase(dbtype)) {
final String DBRelativeLocation = FileUtils.constructAbsoluteFilePath(scope, database, true);
// String EXTRelativeLocation =
// GamaPreferences.LIB_SPATIALITE.value(scope).getPath();
connectionParameters.put("dbtype", dbtype);
connectionParameters.put("database", DBRelativeLocation);

} else {
for(String connectorName : SqlUtils.externalConnectors.keySet()) {
if (dbtype.equalsIgnoreCase(connectorName)) {
connectionParameters = SqlUtils.externalConnectors.get(connectorName)
.getConnectionParameters(host, dbtype, port, database, user, passwd);
break;
}
}

}
return connectionParameters;
ISqlConnector connector = SqlUtils.externalConnectors.get(dbtype);
return connector.getConnectionParameters(scope, host, dbtype, port, database, user, passwd);
}

/**
Expand Down
Expand Up @@ -8,6 +8,6 @@ public interface ISqlConnector {
SqlConnection connection(final IScope scope, final String venderName, final String url, final String port, final String dbName,
final String userName, final String password, final Boolean transformed);

Map<String, Object> getConnectionParameters(String host, String dbtype, String port, String database, String user, String passwd);
Map<String, Object> getConnectionParameters(final IScope scope, String host, String dbtype, String port, String database, String user, String passwd);

}

0 comments on commit ff3b411

Please sign in to comment.