001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.db;
017
018import org.opengion.hayabusa.common.HybsSystemException;
019import org.opengion.fukurou.util.StringUtil;
020
021import java.sql.SQLData;
022import java.sql.SQLInput;
023import java.sql.SQLOutput;
024import java.sql.SQLException;
025
026/**
027 * QLData インターフェースを継承した ユーザー変数の受け渡し用オブジェクトです。
028 * 登録されている属性情報は、セットメソッドを通して、順番に設定されます。
029 *
030 * @og.group DB/Shell制御
031 *
032 * @version  4.0
033 * @author   Kazuhiko Hasegawa
034 * @since    JDK5.0,
035 */
036public class DBUserArg implements SQLData {
037        private String sql_type ;
038
039        private final String[] names;
040        private String[] values;
041
042        /**
043         *  すべての属性情報を指定して、新しい DBUserArg オブジェクトを作成します。
044         *
045         * @og.rev 3.3.3.1 (2003/07/18) DB登録時の後ろスペースを削除する。
046         * @og.rev 3.5.6.0 (2004/06/18) 内部に取り込み時に、キー配列は、 arraycopy を行う。
047         *
048         * @param    type データベースタイプ文字列
049         * @param    nms キー配列
050         * @param    vals 属性配列
051         */
052        public DBUserArg( final String type,final String[] nms,final String[] vals ) {
053                if( nms == null ) {
054                        String errMsg = "引数のキー配列が null です。" ;
055                        throw new HybsSystemException( errMsg );
056                }
057
058                int size = nms.length;
059                names = new String[size];
060                System.arraycopy( nms,0,names,0,size );
061
062                sql_type = type;
063                values   = StringUtil.rTrims( vals );
064        }
065
066        /**
067         *  属性配列情報を取得します。
068         *
069         * @og.rev 3.5.6.0 (2004/06/18) 取り出し時に内部配列を clone して返します。
070         * @og.rev 3.6.0.0 (2004/09/22) 属性配列が null の場合は、エラー
071         *
072         * @return    属性配列
073         */
074        public String[] getValues() {
075                if( values != null ) {
076                        return values.clone();
077                }
078
079                String errMsg = "属性配列が、初期化されていません。";
080                throw new HybsSystemException( errMsg );
081        }
082
083        // ============================================================
084        // implements SQLData
085        // ============================================================
086
087        /**
088         *  SQLタイプの文字列を返します。
089         *
090         * @return    SQLタイプの文字列
091         * @throws SQLException ※ この実装からは SQLException は、throw されません。
092         */
093        public String getSQLTypeName() throws SQLException {
094                return sql_type;
095        }
096
097        /**
098         *  データベース内部より内部属性を取得し、オブジェクトを構築します。
099         *
100         * @param       stream  ストリーム
101         * @param    typeName SQLタイプの文字列
102         * @throws SQLException データベースアクセスエラー
103         */
104        public void readSQL( final SQLInput stream, final String typeName ) throws SQLException {
105                sql_type = typeName;
106
107                values = new String[names.length];
108                for( int i=0; i<names.length; i++ ) {
109                        values[i] = stream.readString();
110                }
111        }
112
113        /**
114         *  データベース内部に内部属性を設定します。
115         *
116         * @param       stream  ストリーム
117         * @throws SQLException データベースアクセスエラー
118         */
119        public void writeSQL( final SQLOutput stream ) throws SQLException {
120                for( int i=0; i<names.length; i++ ) {
121                        stream.writeString( values[i] );
122                }
123        }
124}