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 sqlType ; 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 final String errMsg = "引数のキー配列が null です。" ; 055 throw new HybsSystemException( errMsg ); 056 } 057 058 final int size = nms.length; 059 names = new String[size]; 060 System.arraycopy( nms,0,names,0,size ); 061 062 sqlType = 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 * @og.rtnNotNull 074 */ 075 public String[] getValues() { 076 if( values != null ) { 077 return values.clone(); 078 } 079 080 final String errMsg = "属性配列が、初期化されていません。"; 081 throw new HybsSystemException( errMsg ); 082 } 083 084 // ============================================================ 085 // implements SQLData 086 // ============================================================ 087 088 /** 089 * SQLタイプの文字列を返します。 090 * 091 * @return SQLタイプの文字列 092 * @throws SQLException ※ この実装からは SQLException は、throw されません。 093 */ 094 public String getSQLTypeName() throws SQLException { 095 return sqlType; 096 } 097 098 /** 099 * データベース内部より内部属性を取得し、オブジェクトを構築します。 100 * 101 * @param stream ストリーム 102 * @param typeName SQLタイプの文字列 103 * @throws SQLException データベースアクセスエラー 104 */ 105 public void readSQL( final SQLInput stream, final String typeName ) throws SQLException { 106 sqlType = typeName; 107 108 values = new String[names.length]; 109 for( int i=0; i<names.length; i++ ) { 110 values[i] = stream.readString(); 111 } 112 } 113 114 /** 115 * データベース内部に内部属性を設定します。 116 * 117 * @param stream ストリーム 118 * @throws SQLException データベースアクセスエラー 119 */ 120 public void writeSQL( final SQLOutput stream ) throws SQLException { 121 for( int i=0; i<names.length; i++ ) { 122 stream.writeString( values[i] ); 123 } 124 } 125}