<commit_before>/*<license>
Copyright 2004 - $Date$ by PeopleWare n.v..

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</license>*/


package org.ppwcode.vernacular.persistence_III.sql.mysql;


import static org.ppwcode.metainfo_I.License.Type.APACHE_V2;

import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ppwcode.metainfo_I.Copyright;
import org.ppwcode.metainfo_I.License;
import org.ppwcode.metainfo_I.vcs.SvnInfo;
import org.ppwcode.vernacular.persistence_III.sql.SqlExceptionHandler;


/**
 * Growing implementation for handling general {@link SQLException SQLExceptions}
 * generated by MySQL.
 *
 * @author    <NAME>
 * @author    PeopleWare n.v.
 *
 * @mudo i18n
 */
@Copyright("2004 - $Date$, PeopleWare n.v.")
@License(APACHE_V2)
@SvnInfo(revision = "$Revision$",
         date     = "$Date$")
public class MySqlExceptionHandler implements SqlExceptionHandler {

  private static final Log LOG = LogFactory.getLog(MySqlExceptionHandler.class);


  /**
   * If the message of the given sql exception contains the string
   *   "Duplicate key or integrity constraint violation,  message from server: \"Duplicate entry"
   * or
   *   "Duplicate entry"
   * return a {@link DuplicateKeyException}.
   *
   * If the message of the given sql exception contains the string
   * "Duplicate key or integrity constraint violation,  "
   *             + "message from server: \"Cannot delete or update a "
   *             + "parent row: a foreign key constraint fails\""
   * return a {@link ConstraintException}.
   *
   * Otherwise, return null.
   *
   * @mudo walk through the chain of exceptions to find a match
   */
  public MySqlException handle(final SQLException sqlExc) {
    assert sqlExc != null;
    if (sqlExc.getMessage()
              .indexOf("Duplicate key or integrity constraint violation,  "
                       + "message from server: \"Duplicate entry") >= 0
        || sqlExc.getMessage().indexOf("Duplicate entry") >= 0) {
      // WATCH OUT: SQL Error message contains 'dual space' after ','.
      MySqlException dkExc = new MySqlException("VALUE_NOT_UNIQUE", sqlExc);
      LOG.debug("recognized MySQL duplicate key exception", dkExc);
      return dkExc;
    }
    if (sqlExc.getMessage()
        .indexOf("Duplicate key or integrity constraint violation,  "
                 + "message from server: \"Cannot delete or update a "
                 + "parent row: a foreign key constraint fails\"") >= 0) {
      // WATCH OUT: SQL Error message contains 'dual space' after ','.
      MySqlException cExc = new MySqlException("CONSTRAINT_FAILURE", sqlExc);
      LOG.debug("recognized MySQL constraint violation exception", cExc);
      return cExc;
    }
    return null;
  }

}<commit_msg>removing stubs for exception vernacular, and using it<commit_after>/*<license>
Copyright 2004 - $Date$ by PeopleWare n.v..

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</license>*/


package org.ppwcode.vernacular.persistence_III.sql.mysql;


import static org.ppwcode.metainfo_I.License.Type.APACHE_V2;

import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ppwcode.metainfo_I.Copyright;
import org.ppwcode.metainfo_I.License;
import org.ppwcode.metainfo_I.vcs.SvnInfo;
import org.ppwcode.vernacular.exception_II.ExternalError;
import org.ppwcode.vernacular.persistence_III.sql.SqlExceptionHandler;


/**
 * Growing implementation for handling general {@link SQLException SQLExceptions}
 * generated by MySQL.
 *
 * @author    <NAME>
 * @author    PeopleWare n.v.
 *
 * @mudo i18n
 */
@Copyright("2004 - $Date$, PeopleWare n.v.")
@License(APACHE_V2)
@SvnInfo(revision = "$Revision$",
         date     = "$Date$")
public class MySqlExceptionHandler implements SqlExceptionHandler {

  private static final Log LOG = LogFactory.getLog(MySqlExceptionHandler.class);


  /**
   * If the message of the given sql exception contains the string
   *   "Duplicate key or integrity constraint violation,  message from server: \"Duplicate entry"
   * or
   *   "Duplicate entry"
   * return a {@link DuplicateKeyException}.
   *
   * If the message of the given sql exception contains the string
   * "Duplicate key or integrity constraint violation,  "
   *             + "message from server: \"Cannot delete or update a "
   *             + "parent row: a foreign key constraint fails\""
   * return a {@link ConstraintException}.
   *
   * Never return null.
   *
   * @mudo walk through the chain of exceptions to find a match
   */
  public MySqlException handle(final SQLException sqlExc) {
    assert sqlExc != null;
    if (sqlExc.getMessage()
              .indexOf("Duplicate key or integrity constraint violation,  "
                       + "message from server: \"Duplicate entry") >= 0
        || sqlExc.getMessage().indexOf("Duplicate entry") >= 0) {
      // WATCH OUT: SQL Error message contains 'dual space' after ','.
      MySqlException dkExc = new MySqlException("VALUE_NOT_UNIQUE", sqlExc);
      LOG.debug("recognized MySQL duplicate key exception", dkExc);
      return dkExc;
    }
    if (sqlExc.getMessage()
        .indexOf("Duplicate key or integrity constraint violation,  "
                 + "message from server: \"Cannot delete or update a "
                 + "parent row: a foreign key constraint fails\"") >= 0) {
      // WATCH OUT: SQL Error message contains 'dual space' after ','.
      MySqlException cExc = new MySqlException("CONSTRAINT_FAILURE", sqlExc);
      LOG.debug("recognized MySQL constraint violation exception", cExc);
      return cExc;
    }
    throw new ExternalError("MySQL error not recognized as internal", sqlExc);
  }

}
{'fraction_non_alphanumeric': 0.09542269270910989, 'fraction_numerical': 0.0056657223796034, 'mean_word_length': 3.364346128822381, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '989552', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1894, 'n_words': 654}