18 package com.mysql.clusterj.jdbc;
 
   20 import com.mysql.clusterj.ClusterJFatalInternalException;
 
   21 import com.mysql.clusterj.ClusterJUserException;
 
   22 import com.mysql.clusterj.core.query.QueryDomainTypeImpl;
 
   23 import com.mysql.clusterj.core.query.QueryExecutionContextImpl;
 
   24 import com.mysql.clusterj.core.spi.DomainTypeHandler;
 
   25 import com.mysql.clusterj.core.spi.QueryExecutionContext;
 
   26 import com.mysql.clusterj.core.spi.SessionSPI;
 
   27 import com.mysql.clusterj.core.spi.ValueHandler;
 
   28 import com.mysql.clusterj.core.store.ResultData;
 
   29 import com.mysql.clusterj.core.util.I18NHelper;
 
   30 import com.mysql.clusterj.core.util.Logger;
 
   31 import com.mysql.clusterj.core.util.LoggerFactoryService;
 
   32 import com.mysql.jdbc.ParameterBindings;
 
   33 import com.mysql.jdbc.ResultSetInternalMethods;
 
   35 import java.sql.SQLException;
 
   36 import java.util.Arrays;
 
   37 import java.util.HashMap;
 
   38 import java.util.List;
 
   53     DomainTypeHandler<?> domainTypeHandler = null;
 
   82         this.domainTypeHandler = domainTypeHandler;
 
   84         initializeFieldNumberMap();
 
   87     public SQLExecutor(DomainTypeHandlerImpl<?> domainTypeHandler) {
 
   88         this.domainTypeHandler = domainTypeHandler;
 
   95         initializeFieldNumberMap();
 
   98     public SQLExecutor(DomainTypeHandlerImpl<?> domainTypeHandler, QueryDomainTypeImpl<?> 
queryDomainType,
 
  100         this.domainTypeHandler = domainTypeHandler;
 
  118                 ParameterBindings parameterBindings) 
throws SQLException;
 
  124     public static class Noop 
implements Executor {
 
  127                 ParameterBindings parameterBindings) 
throws SQLException {
 
  134     public static class Select 
extends SQLExecutor implements Executor {
 
  137             super(domainTypeHandler, columnNames, queryDomainType);
 
  138             if (queryDomainType == null) {
 
  143         public ResultSetInternalMethods execute(InterceptorImpl interceptor,
 
  144                 ParameterBindings parameterBindings) 
throws SQLException {
 
  148             SessionSPI session = interceptor.getSession();
 
  149             Map<String, Object> parameters = 
createParameterMap(queryDomainType, parameterBindings, 0, count);
 
  150             QueryExecutionContext context = 
new QueryExecutionContextImpl(session, parameters);
 
  151             session.startAutoTransaction();
 
  153                 ResultData resultData = queryDomainType.getResultData(context);
 
  157             } 
catch (Exception e) {
 
  159                 session.failAutoTransaction();
 
  167     public static class Delete 
extends SQLExecutor implements Executor {
 
  169         public Delete (DomainTypeHandlerImpl<?> domainTypeHandler, QueryDomainTypeImpl<?> queryDomainType,
 
  171             super(domainTypeHandler, queryDomainType, numberOfParameters);
 
  174         public Delete (DomainTypeHandlerImpl<?> domainTypeHandler) {
 
  175             super(domainTypeHandler);
 
  178         public ResultSetInternalMethods execute(InterceptorImpl interceptor,
 
  179                 ParameterBindings parameterBindings) 
throws SQLException {
 
  180             SessionSPI session = interceptor.getSession();
 
  181             if (queryDomainType == null) {
 
  182                 int rowsDeleted = session.deletePersistentAll(domainTypeHandler);
 
  183                 if (logger.isDebugEnabled()) logger.debug(
"deleteAll deleted: " + rowsDeleted);
 
  184                 return new ResultSetInternalMethodsUpdateCount(rowsDeleted);
 
  188                 long[] deleteResults = 
new long[numberOfStatements];
 
  189                 if (logger.isDebugEnabled()) logger.debug(
 
  191                         + 
" numberOfBoundParameters: " + numberOfBoundParameters
 
  192                         + 
" numberOfStatements: " + numberOfStatements
 
  194                 QueryExecutionContextJDBCImpl context = 
 
  196                 for (
int i = 0; 
i < numberOfStatements; ++
i) {
 
  198                     int statementRowsDeleted = queryDomainType.deletePersistentAll(context);
 
  199                     if (logger.isDebugEnabled()) logger.debug(
"statement " + 
i 
  200                             + 
" deleted " + statementRowsDeleted);
 
  201                     deleteResults[
i] = statementRowsDeleted;
 
  202                     context.nextStatement();
 
  204                 return new ResultSetInternalMethodsUpdateCount(deleteResults);
 
  211     public static class Insert 
extends SQLExecutor implements Executor {
 
  213         public Insert(DomainTypeHandlerImpl<?> domainTypeHandler, 
List<String> columnNames) {
 
  214             super(domainTypeHandler, columnNames, columnNames.size());
 
  217         public ResultSetInternalMethods execute(InterceptorImpl interceptor,
 
  218                 ParameterBindings parameterBindings) 
throws SQLException {
 
  219             SessionSPI session = interceptor.getSession();
 
  222             if (logger.isDebugEnabled()) logger.debug(
"SQLExecutor.Insert.execute" 
  224                     + 
" numberOfBoundParameters: " + numberOfBoundParameters
 
  226                     + 
" numberOfStatements: " + numberOfStatements
 
  232                 session.insert(domainTypeHandler, valueHandler);
 
  236             return new ResultSetInternalMethodsUpdateCount(numberOfStatements);
 
  252             ParameterBindings parameterBindings, 
int offset, 
int count) 
throws SQLException {
 
  253         Map<String, Object> result = 
new HashMap<String, Object>();
 
  255         int last = 
offset + count + 1;
 
  256         for (
int i = first; 
i < last; ++
i) {
 
  257             Object placeholder = parameterBindings.getObject(
i);
 
  258             if (logger.isDetailEnabled())
 
  259                 logger.detail(
"Put placeholder " + 
i + 
" value: " + placeholder + 
" of type " + placeholder.getClass());
 
  260             result.put(
String.valueOf(
i), placeholder);
 
  275     private void initializeFieldNumberMap() {
 
  278         String[] fieldNames = domainTypeHandler.getFieldNames();
 
  284             int index = columnNames.indexOf(fieldNames[i]);
 
  295         for (
String columnName: columnNames) {
 
  297                 throw new ClusterJUserException(
 
  298                         local.
message(
"ERR_Column_Name_Not_In_Table", columnName,
 
  299                         Arrays.toString(fieldNames),
 
  300                         domainTypeHandler.getTableName()));
 
  303         if (logger.isDetailEnabled()) {
 
  304             StringBuilder buffer = 
new StringBuilder();
 
  307                 buffer.append(
"field ");
 
  309                 buffer.append(
" mapped to ");
 
  310                 buffer.append(columnNumber);
 
  312                 buffer.append(columnNumber == -1?
"nothing":(columnNames.get(columnNumber - 1)));
 
  315             logger.detail(buffer.toString());
 
  326             int[] fieldNumberToParameterNumberMap, 
int offset) {
 
  327         return new ValueHandlerImpl(parameterBindings, fieldNumberToParameterNumberMap, offset);
 
  334         if (logger.isDetailEnabled()) {
 
  338                     String value = parameterBindings.getObject(++i).toString();
 
  340                     logger.detail(
"parameterBinding: parameter " + i + 
" has value: " + value);
 
  341                 } 
catch (Exception e) {
 
  361                 Object objectValue = parameterBindings.getObject(i);
 
  362                 if (logger.isDetailEnabled()) {
 
  363                     logger.detail(
"parameterBinding: parameter " + i
 
  364                             + 
" has value: " + objectValue
 
  365                             + 
" of type " + objectValue.getClass());
 
  367             } 
catch (Exception e) {