日期:2014-05-16  浏览次数:20507 次

Oracle--调用packages判断组合不允许重复、交叉、包含

JAVA代码:

@SuppressWarnings("unchecked")
	public String validateSets(final String setIds) {
		Object object = getHibernateTemplate().execute(new HibernateCallback(){
			@SuppressWarnings("deprecation")
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				CallableStatement statement = session.connection().prepareCall("{call PKG_VALID_SET_INTERSECT.VALIDATE_SETIDS(?,?)}");
				statement.setString(1, setIds);
				statement.registerOutParameter(2, Types.VARCHAR);
				statement.execute();
				return statement.getString(2);
			}
		});
		return object == null ? null :object.toString();
	}

?

?

packages:

CREATE OR REPLACE PACKAGE BODY PKG_VALID_SET_INTERSECT IS

  --字符串分割函数
  FUNCTION SPLIT_STR(P_STR       IN VARCHAR2,
                     P_DELIMITER IN VARCHAR2 DEFAULT (','))
    RETURN tbl_split_type IS
    J        INT := 0;
    I        INT := 1;
    LEN      INT := 0;
    LEN1     INT := 0;
    STR      VARCHAR2(4000);
    MY_SPLIT tbl_split_type := tbl_split_type();
  BEGIN
    LEN  := LENGTH(P_STR);
    LEN1 := LENGTH(P_DELIMITER);

    WHILE J < LEN LOOP
      J := INSTR(P_STR, P_DELIMITER, I);

      IF J = 0 THEN
        J   := LEN;
        STR := SUBSTR(P_STR, I);
        MY_SPLIT.EXTEND;
        MY_SPLIT(MY_SPLIT.COUNT) := STR;

        IF I >= LEN THEN
          EXIT;
        END IF;
      ELSE
        STR := SUBSTR(P_STR, I, J - I);
        I   := J + LEN1;
        MY_SPLIT.EXTEND;
        MY_SPLIT(MY_SPLIT.COUNT) := STR;
      END IF;
    END LOOP;

    RETURN MY_SPLIT;
  END;






  -- 检测节点是否包含函数
  FUNCTION COUNT_DIST_CODE(IN_DIST_CODE         IN VARCHAR2,
                        IN_TYPE_CODE         IN VARCHAR2,
                        COMPARE_DIST_CODE    IN VARCHAR2) RETURN NUMBER IS
    V_COUNT NUMBER;
  BEGIN

    -- 如果原寄地的级别为国家,下钻到省,市
    IF IN_TYPE_CODE = 1 THEN

      SELECT COUNT(0)
        INTO V_COUNT
        FROM (WITH CODES AS (SELECT T.DIST_CODE
                               FROM TM_DISTRICT T
                              WHERE T.TYPE_CODE = 2
                                AND T.COUNTRY_CODE = IN_DIST_CODE)
               SELECT DIST_CODE
                 FROM CODES
               UNION
               SELECT T.DIST_CODE
                 FROM TM_DISTRICT T, CODES C
                WHERE T.PROVINCE_CODE = C.DIST_CODE
                  AND T.TYPE_CODE = 3
               UNION
               SELECT IN_DIST_CODE
                 FROM DUAL) R
                WHERE R.DIST_CODE = COMPARE_DIST_CODE;

      -- 如果原寄地的级别为省,下钻到市,上溯到国家
    ELSIF IN_TYPE_CODE = 2 THEN

      SELECT COUNT(0)
        INTO V_COUNT
        FROM (SELECT T.COUNTY_CODE AS DIST_CODE
                FROM TM_DISTRICT T
               WHERE T.DIST_CODE = IN_DIST_CODE
              UNION
              SELECT T.DIST_CODE
                FROM TM_DISTRICT T
               WHERE T.TYPE_CODE = 3
                 AND T.PROVINCE_CODE = IN_DIST_CODE
              UNION
              SELECT IN_DIST_CODE FROM DUAL) R
       WHERE R.DIST_CODE = COMPARE_DIST_CODE;

      -- 如果原寄地的级别为市,上溯到市,国家
    ELSIF IN_TYPE_CODE = 3 THEN

      SELECT COUNT(0)
        INTO V_COUNT
        FROM (WITH CODES AS (SELECT PROVINCE_CODE
                               FROM TM_DISTRICT T
                              WHERE T.DIST_CODE = IN_DIST_CODE)
               SELECT T.DIST_CODE
                 FROM TM_DISTRICT T, CODES C
                WHERE T.DIST_CODE = C.PROVINCE_CODE
                  AND T.TYPE_CODE = 1
               UNION
               SELECT C.PROVINCE_CODE
                 FROM CODES C
               UNION
               SELECT IN_DIST_CODE
                 FROM DUAL) R
                WHERE R.DIST_CODE = COMPARE_DIST_CODE;

    END IF;

    RETURN V_COUNT;

  END;





  -- 检查维度是否存在交集
  FUNCTION COUNT_DMNSN(IN_