⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlserversybasedatabasepolicy.java

📁 把java对象映射成数据库表中的一条记录
💻 JAVA
字号:
/*
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * The Original Code is jRelationalFramework.
 *
 * The Initial Developer of the Original Code is is.com.
 * Portions created by is.com are Copyright (C) 2000 is.com.
 * All Rights Reserved.
 *
 * Contributor(s): Jonathan Carlson (joncrlsn@users.sf.net)
 * Contributor(s): ____________________________________
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License (the "GPL") or the GNU Lesser General
 * Public license (the "LGPL"), in which case the provisions of the GPL or
 * LGPL are applicable instead of those above.  If you wish to allow use of
 * your version of this file only under the terms of either the GPL or LGPL
 * and not to allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and replace them
 * with the notice and other provisions required by either the GPL or LGPL
 * License.  If you do not delete the provisions above, a recipient may use
 * your version of this file under either the MPL or GPL or LGPL License.
 *
 */
package com.is.jrf;


import com.is.util.sql.JDBCHelper;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Date;


/**
 *  Instances of this class perform Sybase/SQLServer-specific logic and
 *  return Sybase/SQLServer-specific information.
 */
public class SQLServerSybaseDatabasePolicy
    implements DatabasePolicy
  {


  /**
   * When executed, this SQL should return a one column, one row result set
   * with an Integer in it.
   *
   * @param tableName a value of type 'String'
   * @return a value of type 'String'
   */
  public String sequenceSQL(String tableName)
    {
    return "exec nextSequenceNumber @tablename= \"" + tableName + "\"";
    }


  /**
   * This is the string value (function name) to put into the SQL to tell
   * the database to insert the current timestamp.
   *
   * @return a value of type 'String'
   */
  public String timestampFunction()
    {
    return "GETDATE()";
    }


  /**
   * The return value should be in the format that the database recognizes
   * for a timestamp.
   *
   * @param ts a value of type 'Timestamp'
   * @return a value of type 'String'
   */
  public String formatTimestamp(Timestamp ts)
    {
    return (ts == null ?
            "null" :
            "'" + ts.toString() + "'");
    }


  /**
   * The result of this is used in SQL.  This will return only the date
   * (not the time portion)
   *
   * @param sqlDate a value of type 'java.sql.Date'
   * @return a value of type 'String'
   */
  public String formatDate(java.sql.Date sqlDate)
    {
    return (sqlDate == null ?
            "null" :
            "'" + sqlDate.toString() + "'");
    }

  /**
   * This should return the the SQL to use to have the database
   * return the current timestamp.

   *
   * @return a value of type 'String'
   */
  public String currentTimestampSQL()
    {
    return "select sysdate = getDate()";
    }


  /**
   * SQLServer and Sybase have stored procedures for generating a sequence
   * number, so AUTO INCREMENT is not needed here.
   *
   * @return null since Sybase doesn't support the auto-increment method
   */
  public String autoIncrementIdentifier()
    {
    return null;
    }


  /**
   * This method throws an exception since it should never be called.
   *
   * @param tableName a value of type 'String'
   * @param columnName a value of type 'String'
   * @param aJDBCHelper a value of type 'JDBCHelper'
   * @return a value of type 'Long'
   */
  public Long findAutoIncrementId(String tableName,
                                  String columnName,
                                  JDBCHelper aJDBCHelper)
    {
    throw new DatabaseException("This method should never be called.");
    }


  /**
   * This is called when creating a table that has a SEQUENCED_PRIMARY_KEY.
   * This implementation assumes there is already a table called
   * SequenceNumber with columns named SequenceName and CurrentIndex.  See
   * the SQLServer-Readme.txt file that should have come with the
   * distribution.
   *
   * When this is called, the Sequence number is set to zero whether a row
   * exists for the table or not.
   *
   * @param domain a value of type 'AbstractDomain'
   * @param aJDBCHelper a value of type 'JDBCHelper'
   */
  public void createSequence(AbstractDomain domain,
                             JDBCHelper aJDBCHelper)
          throws SQLException
    {
    // Update the Syabase or SQLServer Sequence table
    int rowCount = 0;
    try
        {
        rowCount = aJDBCHelper.executeUpdate(
            "INSERT INTO SequenceNumber " +
            "VALUES('" + domain.getTableName() + "', 0)");
        }
    catch (SQLException e)
        {
        // Assume the table already has that row.
        }
    catch (Exception e)
        {
        throw new DatabaseException(e);
        }

    if (rowCount == 0)
        {
        // Reset the current index back to 0
        try
            {
            aJDBCHelper.executeUpdate(
                "UPDATE SequenceNumber " +
                "SET CurrentIndex = 0 WHERE SequenceName = '"
                + domain.getTableName() + "'");
            }
        catch (SQLException e)
            {
            throw e;
            }
        catch (Exception e)
            {
            throw new DatabaseException(e);
            }
        } // if
    } // createSequence()


  /**
   * Return a string with a name-value pair that represents an SQL outer
   * join in a WHERE clause for SQLServer and Sybase.  Given this operator
   * (*=), it looks like SQLServer always gives preference to the rows in
   * the table on the left whether it is a foreign key in the left table or
   * a foreign key in the right table.  This is what we want since we always
   * put the main table on the left.
   *
   * @param mainTableColumn a value of type 'String'
   * @param joinTableColumn a value of type 'String'
   * @param sqlBuffer a value of type 'StringBuffer'
   * @return a value of type 'String'
   */
  public String outerWhereJoin(String mainTableColumn,
                               String joinTableColumn)
    {
    return mainTableColumn + "*=" + joinTableColumn + " ";
    }


  /**
   * This method is used when building SQL to create tables.
   *
   * @return a value of type 'String'
   */
  public String timestampColumnType()
    {
    return "DATETIME";
    }

  } // SQLServerSybaseDatabasePolicy

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -