Skip to content

Commit

Permalink
further mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Remme committed May 12, 2017
1 parent 50dade7 commit cb5f7bb
Show file tree
Hide file tree
Showing 23 changed files with 779 additions and 718 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import de.braintags.vertx.jomnigate.init.DataStoreSettings;
import de.braintags.vertx.jomnigate.sql.SqlDataStore;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.AsyncSQLClient;
Expand Down Expand Up @@ -46,4 +48,14 @@ public Future<SQLConnection> getConnection() {
return f;
}

@Override
public void shutdown(Handler<AsyncResult<Void>> resultHandler) {
sqlClient.close(resultHandler);
}

@Override
public Object getClient() {
return sqlClient;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static void applySystemProperties(DataStoreSettings settings) {
String keyGenerator = System.getProperty(IKeyGenerator.DEFAULT_KEY_GENERATOR, DEFAULT_KEY_GENERATOR);
settings.setDatabaseName(database);
settings.getProperties().put(MySqlDataStoreInit.HOST_PROPERTY, host);
settings.getProperties().put(MySqlDataStoreInit.PORT_PROPERTY, MySqlDataStoreInit.DEFAULT_PORT);
settings.getProperties().put(MySqlDataStoreInit.PORT_PROPERTY, String.valueOf(MySqlDataStoreInit.DEFAULT_PORT));
settings.getProperties().put(MySqlDataStoreInit.USERNAME_PROPERTY, username);
settings.getProperties().put(MySqlDataStoreInit.PASSWORD_PROPERTY, password);
settings.getProperties().put(MySqlDataStoreInit.SHARED_PROP, "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import de.braintags.vertx.jomnigate.init.DataStoreSettings;
import de.braintags.vertx.jomnigate.json.JsonDatastore;
import de.braintags.vertx.jomnigate.sql.dataaccess.SqlStoreObjectFactory;
import de.braintags.vertx.jomnigate.sql.dataaccess.SqlWrite;
import de.braintags.vertx.jomnigate.sql.mapping.SqlDataStoreSynchronizer;
import de.braintags.vertx.jomnigate.sql.mapping.SqlMapperFactory;
import de.braintags.vertx.jomnigate.sql.mapping.SqlTableGenerator;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.sql.SQLConnection;
Expand All @@ -36,6 +35,10 @@
*
*/
public abstract class SqlDataStore extends JsonDatastore<String> {
/**
* The name of the property, which describes the database to be used
*/
public static final String DATABASE_NAME = "database";

/**
* @param vertx
Expand Down Expand Up @@ -75,7 +78,7 @@ public <T> IQuery<T> createQuery(Class<T> mapper) {
*/
@Override
public <T> IWrite<T> createWrite(Class<T> mapper) {
throw new UnsupportedOperationException();
return new SqlWrite<>(mapper, this);
}

/*
Expand All @@ -95,27 +98,7 @@ public <T> IDelete<T> createDelete(Class<T> mapper) {
*/
@Override
public String getDatabase() {
throw new UnsupportedOperationException();
}

/*
* (non-Javadoc)
*
* @see de.braintags.vertx.jomnigate.IDataStore#shutdown(io.vertx.core.Handler)
*/
@Override
public void shutdown(Handler<AsyncResult<Void>> resultHandler) {
throw new UnsupportedOperationException();
}

/*
* (non-Javadoc)
*
* @see de.braintags.vertx.jomnigate.IDataStore#getClient()
*/
@Override
public Object getClient() {
throw new UnsupportedOperationException();
return getProperties().getString(DATABASE_NAME);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import java.util.ArrayList;
import java.util.List;

import com.github.mauricio.async.db.mysql.exceptions.MySQLException;
import com.google.common.collect.ImmutableSet;

import de.braintags.vertx.jomnigate.annotation.Index;
import de.braintags.vertx.jomnigate.exception.DuplicateKeyException;
import de.braintags.vertx.jomnigate.mapping.IIndexDefinition;
import de.braintags.vertx.jomnigate.mapping.IIndexFieldDefinition;
import de.braintags.vertx.jomnigate.sql.exception.SqlException;
Expand All @@ -26,9 +28,12 @@
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.AsyncSQLClient;
import io.vertx.ext.sql.ResultSet;
import io.vertx.ext.sql.SQLConnection;
import io.vertx.ext.sql.UpdateResult;

/**
* Utility methods working with {@link SqlDataStore}
Expand All @@ -47,6 +52,7 @@ public class SqlUtil {
public static final short INDEX_EXISTS = 0;
public static final short INDEX_NOT_EXISTS = 1;
public static final short INDEX_MODIFIED = 2;
private static final String GAINED_SUCCESSFULLY_A_CONNECTION = "gained successfully a connection";

private SqlUtil() {
}
Expand Down Expand Up @@ -305,6 +311,127 @@ private static IndexResult compare(final JsonObject existingIndex, final IIndexD
return res;
}

/**
* Executes the given update command and informs the {@link Handler}
*
* @param datastore
* the datastore to obtain the connection from
* @param command
* the command to be executed
* @param resultHandler
* a resulthandler to be informed
*/
public static void updateWithParams(final SqlDataStore datastore, final String command, final JsonArray params,
final Handler<AsyncResult<UpdateResult>> resultHandler) {
updateWithParams((AsyncSQLClient) datastore.getClient(), command, params, resultHandler);
}

/**
* Executes the given command and informs the {@link Handler}
*
* @param sqlClient
* the sqlClient to obtain the connection from
* @param command
* the command to be executed
* @param resultHandler
* a resulthandler to be informed
*/
public static void updateWithParams(final AsyncSQLClient sqlClient, final String command, final JsonArray params,
final Handler<AsyncResult<UpdateResult>> resultHandler) {
LOGGER.debug("updateWithParams: " + command + " | " + params);
sqlClient.getConnection(cr -> {
if (cr.failed()) {
Exception sqlEx = new SqlException(ERROR_GAINING_CONNECTION, cr.cause());
LOGGER.error("", sqlEx);
resultHandler.handle(Future.failedFuture(sqlEx));
} else {
SQLConnection connection = cr.result();
LOGGER.debug(GAINED_SUCCESSFULLY_A_CONNECTION);
executeUpdateWithParams(connection, command, params, resultHandler);
}
});
}

private static void executeUpdate(final SQLConnection connection, final String command,
final Handler<AsyncResult<UpdateResult>> resultHandler) {
connection.update(command, qr -> {
if (qr.failed()) {
Exception sqlEx = new SqlException(ERROR_EXECUTING_COMMAND_STATEMENT + command, qr.cause());
LOGGER.error("", sqlEx);
connection.close();
resultHandler.handle(Future.failedFuture(sqlEx));
return;
}
LOGGER.debug(COMMAND_SUCCESS);
connection.close();
LOGGER.debug(CONNECTION_CLOSED);
resultHandler.handle(Future.succeededFuture(qr.result()));
});
}

private static void executeUpdateWithParams(final SQLConnection connection, final String command,
final JsonArray params, final Handler<AsyncResult<UpdateResult>> resultHandler) {
connection.updateWithParams(command, params, qr -> {
if (qr.failed()) {
connection.close();
Throwable error = qr.cause();
if (error instanceof MySQLException && error.getMessage().indexOf("Duplicate entry") >= 0
&& error.getMessage().indexOf("for key 'PRIMARY'") >= 0) {
resultHandler.handle(Future.failedFuture(new DuplicateKeyException(error)));
} else {
Exception sqlEx = new SqlException(ERROR_EXECUTING_COMMAND_STATEMENT + command + " | " + params, error);
resultHandler.handle(Future.failedFuture(sqlEx));
}
} else {
LOGGER.debug(COMMAND_SUCCESS);
connection.close();
LOGGER.debug(CONNECTION_CLOSED);
resultHandler.handle(Future.succeededFuture(qr.result()));
}
});
}

/**
* Executes the given update command and informs the {@link Handler}
*
* @param datastore
* the datastore to obtain the connection from
* @param command
* the command to be executed
* @param resultHandler
* a resulthandler to be informed
*/
public static void update(final SqlDataStore datastore, final String command,
final Handler<AsyncResult<UpdateResult>> resultHandler) {
update((AsyncSQLClient) datastore.getClient(), command, resultHandler);
}

/**
* Executes the given command and informs the {@link Handler}
*
* @param sqlClient
* the sqlClient to obtain the connection from
* @param command
* the command to be executed
* @param resultHandler
* a resulthandler to be informed
*/
public static void update(final AsyncSQLClient sqlClient, final String command,
final Handler<AsyncResult<UpdateResult>> resultHandler) {
LOGGER.debug("update: " + command);
sqlClient.getConnection(cr -> {
if (cr.failed()) {
Exception sqlEx = new SqlException(ERROR_GAINING_CONNECTION, cr.cause());
LOGGER.error("", sqlEx);
resultHandler.handle(Future.failedFuture(sqlEx));
return;
}
SQLConnection connection = cr.result();
LOGGER.debug(GAINED_SUCCESSFULLY_A_CONNECTION);
executeUpdate(connection, command, resultHandler);
});
}

private static class IndexResult {
short state = -1;
JsonObject read;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* #%L
* jomnigate-sql
* %%
* Copyright (C) 2017 Braintags GmbH
* %%
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* #L%
*/
package de.braintags.vertx.jomnigate.sql.dataaccess;

import de.braintags.vertx.jomnigate.mapping.datastore.IColumnInfo;
import io.vertx.core.json.JsonArray;

class SqlSequence {
boolean added = false;
private StringBuilder headStatement;
private StringBuilder setStatement;
private StringBuilder whereStatement;
private Object id;
private JsonArray parameters = new JsonArray();

/**
* Constructor for an insert command
*
* @param tableName
*/
public SqlSequence(String tableName) {
headStatement = new StringBuilder("Insert into ").append(tableName);
setStatement = new StringBuilder(" set ");
}

/**
* Constructor for an update command
*
* @param tableName
* the name of the table
* @param idColInfo
* the {@link IColumnInfo} for the id column
* @param idValue
* the id value
*/
public SqlSequence(String tableName, IColumnInfo idColInfo, Object idValue) {
headStatement = new StringBuilder("UPDATE ").append(tableName);
setStatement = new StringBuilder(" set ");
whereStatement = new StringBuilder(" WHERE ").append(idColInfo.getName()).append(" = ?");
this.id = idValue;
}

void addEntry(String colName, Object value) {
if (added)
setStatement.append(", ");
if (value instanceof SqlFunction) {
setStatement.append(colName).append(" = ").append(((SqlFunction) value).getFunctionName()).append(" ( ? )");
parameters.add(((SqlFunction) value).getContent());
} else {
setStatement.append(colName).append(" = ?");
if (value == null) {
parameters.addNull();
} else {
parameters.add(value);
}
}
added = true;
}

/**
* Get the statement
*
* @return the sqlStatement
*/
public final String getSqlStatement() {
StringBuilder ret = new StringBuilder(headStatement);
if (parameters.isEmpty())
ret.append(" () VALUES ()");// insert into SimpleMapper () VALUES ()
else
ret.append(setStatement);
if (whereStatement != null)
ret.append(whereStatement);
return ret.toString();
}

/**
* @return the parameters
*/
public final JsonArray getParameters() {
if (id != null)
return parameters.copy().add(id);
return parameters;
}

@Override
public String toString() {
return getSqlStatement() + " | " + getParameters() + " | id: " + id;
}
}
Loading

0 comments on commit cb5f7bb

Please sign in to comment.