<commit_before>/**
 * Copyright  2001 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */


package  org.jasig.portal;
import  java.sql.*;
import  org.w3c.dom.Element;
import  org.apache.xerces.dom.DocumentImpl;

/**
 * Oracle optimized SQL implementation of the 2.x relational database model
 * @author <NAME>
 * @version $Revision$
 */

public class DBImplOracle extends DBImpl implements IDBImpl {

  protected Element createChannelNode(Connection con, DocumentImpl doc, int chanId, String idTag) throws java.sql.SQLException
  {
    Element channel = null;
    String sQuery = "SELECT UC.*, CHAN_PARM_NM, CHAN_PARM_VAL,CHAN_PARM_OVRD,CHAN_PARM_DESC FROM UP_CHANNEL UC, UP_CHAN_PARAM UCP WHERE UC.CHAN_ID=" + chanId +
      " AND UC.CHAN_ID = UCP.CHAN_ID(+)";
    Logger.log (Logger.DEBUG, "DBImplOracle::createChannelNode(): " + sQuery);

    Statement stmt = con.createStatement();
    try {
      ResultSet rs = stmt.executeQuery (sQuery);
      try {
        if (rs.next()) {
          channel = doc.createElement("channel");
          Element system = doc.createElement("system");
          createChannelNodeHeaders(doc, chanId, idTag, rs, channel, system);

          do {
            createChannelNodeParameters(doc, rs, channel, system);
          } while (rs.next());
          rs.close();
          channel.appendChild(system);
        }
      } finally {
        rs.close();
      }
    } finally {
      stmt.close();
    }
    return channel;
  }

  protected void createLayout(Connection con, DocumentImpl doc, Statement stmt, Element root, int userId, int profileId, int layoutId, int structId) throws java.sql.SQLException
  {
    if (structId == 0) { // End of line
      return;
    }

    int nextStructId;
    int chldStructId;
    int chanId;
    Element system = null;
    Element parameter = null;
    Element structure;

    String sQuery = "SELECT ULS.*,STRUCT_PARM_NM,STRUCT_PARM_VAL FROM UP_LAYOUT_STRUCT ULS, UP_STRUCT_PARAM USP WHERE ULS.USER_ID=" + userId +
      " AND ULS.LAYOUT_ID = " + layoutId + " AND ULS.STRUCT_ID=" + structId + " AND ULS.STRUCT_ID = USP.STRUCT_ID(+)";
    Logger.log (Logger.DEBUG, "DBImplOracle::createLayout(): " + sQuery);
    ResultSet rs = stmt.executeQuery (sQuery);
    try {
      rs.next();
      nextStructId = rs.getInt("NEXT_STRUCT_ID");
      chldStructId = rs.getInt("CHLD_STRUCT_ID");
      chanId = rs.getInt("CHAN_ID");

      structure = createLayoutStructure(rs, chanId, userId, stmt, doc);

      system = (Element) structure.getElementsByTagName("system").item(0);
      if (chanId != 0) { // Channel
        parameter = (Element) structure.getElementsByTagName("parameter").item(0);
      }

      do {
        createLayoutStructureParameter(chanId, rs, structure, parameter, system);
      } while (rs.next());
    } finally {
      rs.close();
    }
    root.appendChild(structure);

    if (chanId == 0) {  // Folder
      createLayout(con, doc, stmt, structure, userId, profileId, layoutId, chldStructId);
    }

    createLayout(con, doc, stmt, root, userId, profileId, layoutId, nextStructId);
  }

}<commit_msg>Use Oracles sequence counters to generate new ID<commit_after>/**
 * Copyright  2001 The JA-SIG Collaborative.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the JA-SIG Collaborative
 *    (http://www.jasig.org/)."
 *
 * THIS SOFTWARE IS PROVIDED BY THE JA-SIG COLLABORATIVE "AS IS" AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE JA-SIG COLLABORATIVE OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */


package  org.jasig.portal;
import  java.sql.*;
import  org.w3c.dom.Element;
import  org.apache.xerces.dom.DocumentImpl;

/**
 * Oracle optimized SQL implementation of the 2.x relational database model
 * @author <NAME>
 * @version $Revision$
 */

 /**
  * Sequence numbers have the form of {Table Name}_SEQ and, at the moment, they must
  * have been created by hand before uPortal is started. See UP_SEQUENCE in properties/data.xml
  * for the tables that expect sequence counters, and the expected starting value.
  */
public class DBImplOracle extends DBImpl implements IDBImpl {

  protected Element createChannelNode(Connection con, DocumentImpl doc, int chanId, String idTag) throws java.sql.SQLException
  {
    Element channel = null;
    String sQuery = "SELECT UC.*, CHAN_PARM_NM, CHAN_PARM_VAL,CHAN_PARM_OVRD,CHAN_PARM_DESC FROM UP_CHANNEL UC, UP_CHAN_PARAM UCP WHERE UC.CHAN_ID=" + chanId +
      " AND UC.CHAN_ID = UCP.CHAN_ID(+)";
    Logger.log (Logger.DEBUG, "DBImplOracle::createChannelNode(): " + sQuery);

    Statement stmt = con.createStatement();
    try {
      ResultSet rs = stmt.executeQuery (sQuery);
      try {
        if (rs.next()) {
          channel = doc.createElement("channel");
          Element system = doc.createElement("system");
          createChannelNodeHeaders(doc, chanId, idTag, rs, channel, system);

          do {
            createChannelNodeParameters(doc, rs, channel, system);
          } while (rs.next());
          rs.close();
          channel.appendChild(system);
        }
      } finally {
        rs.close();
      }
    } finally {
      stmt.close();
    }
    return channel;
  }

  protected void createLayout(Connection con, DocumentImpl doc, Statement stmt, Element root, int userId, int profileId, int layoutId, int structId) throws java.sql.SQLException
  {
    if (structId == 0) { // End of line
      return;
    }

    int nextStructId;
    int chldStructId;
    int chanId;
    Element system = null;
    Element parameter = null;
    Element structure;

    String sQuery = "SELECT ULS.*,STRUCT_PARM_NM,STRUCT_PARM_VAL FROM UP_LAYOUT_STRUCT ULS, UP_STRUCT_PARAM USP WHERE ULS.USER_ID=" + userId +
      " AND ULS.LAYOUT_ID = " + layoutId + " AND ULS.STRUCT_ID=" + structId + " AND ULS.STRUCT_ID = USP.STRUCT_ID(+)";
    Logger.log (Logger.DEBUG, "DBImplOracle::createLayout(): " + sQuery);
    ResultSet rs = stmt.executeQuery (sQuery);
    try {
      rs.next();
      nextStructId = rs.getInt("NEXT_STRUCT_ID");
      chldStructId = rs.getInt("CHLD_STRUCT_ID");
      chanId = rs.getInt("CHAN_ID");

      structure = createLayoutStructure(rs, chanId, userId, stmt, doc);

      system = (Element) structure.getElementsByTagName("system").item(0);
      if (chanId != 0) { // Channel
        parameter = (Element) structure.getElementsByTagName("parameter").item(0);
      }

      do {
        createLayoutStructureParameter(chanId, rs, structure, parameter, system);
      } while (rs.next());
    } finally {
      rs.close();
    }
    root.appendChild(structure);

    if (chanId == 0) {  // Folder
      createLayout(con, doc, stmt, structure, userId, profileId, layoutId, chldStructId);
    }

    createLayout(con, doc, stmt, root, userId, profileId, layoutId, nextStructId);
  }

  /* DBCounter */

  /*
   * get&increment method.
   */
    public int getIncrementIntegerId(String tableName) throws Exception {
      int id;
      Connection con=rdbmService.getConnection();
      try {
        Statement stmt = con.createStatement ();
        try {
          String sQuery = "SELECT " + tableName + "_SEQ.NEXTVAL FROM DUAL";
          Logger.log (Logger.DEBUG, "DBImplOracle::getIncrementInteger(): " + sQuery);
          ResultSet rs = stmt.executeQuery (sQuery);
          try {
            rs.next();    // If this doesn't work then the database is munged up
            id = rs.getInt(1);
          } finally {
            rs.close();
          }
        } finally {
          stmt.close();
        }
      } finally {
        rdbmService.releaseConnection (con);
      }
      return id;
    }

    public void createCounter(String tableName) throws Exception {
      createCounter(tableName, 1);
    }

    protected void createCounter(String tableName, int startAt) throws Exception {
      Connection con=rdbmService.getConnection();
      try {
        Statement stmt = con.createStatement ();
        try {
            String sInsert = "CREATE SEQUENCE " + tableName + "_SEQ INCREMENT BY 1 START WITH " + startAt + " NOMAXVALUE NOCYCLE";
            Logger.log (Logger.DEBUG, "DBImplOracle::createCounter(): " + sInsert);
            stmt.executeUpdate (sInsert);
        } finally {
          stmt.close();
        }
      } finally {
        rdbmService.releaseConnection (con);
      }
    }

  public synchronized void setCounter(String tableName,int value) throws Exception
  {
      Connection con=rdbmService.getConnection();
      try {
          Statement stmt = con.createStatement ();
          try {
            /* This is dangerous */
            String sUpdate = "DROP SEQUENCE " + tableName + "_SEQ";
            Logger.log (Logger.DEBUG, "DBImplOracle::setCounter(): " + sUpdate);
            stmt.executeUpdate (sUpdate);
            createCounter(tableName, value);
          } finally {
            stmt.close();
          }
      } finally {
          rdbmService.releaseConnection (con);
      }
  }

}
{'fraction_non_alphanumeric': 0.09870218579234973, 'fraction_numerical': 0.0026468579234972677, 'mean_word_length': 2.962449255751015, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, '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': '7474993', 'n_tokens_mistral': 3484, 'n_tokens_neox': 3313, 'n_words': 1069}