00001 using System; 00002 using System.ComponentModel; 00003 using System.IO; 00004 using System.Collections; 00005 using System.Data; 00006 00007 using SQLiteCSLib.Inner; 00008 00009 namespace SQLiteCSLib 00010 { 00014 public class SQLiteCommand : Component, IDbCommand, IDisposable 00015 { 00019 protected SQLiteConnection m_connect = null; 00020 00024 protected SQLiteTransaction m_transaction = null; 00025 00029 protected SQLiteParameterCollection m_collection = new SQLiteParameterCollection(); 00030 00034 protected string m_command = ""; 00035 00039 protected OSQLiteStmtWrap m_stmt = null; 00040 00044 protected bool m_prepare = false; 00045 00049 public SQLiteCommand() 00050 { 00051 } 00052 00057 public SQLiteCommand( SQLiteConnection connect ) 00058 { 00059 m_connect = connect; 00060 } 00061 00066 public void Cancel() 00067 { 00068 m_connect.OSQLiteDB.Interrupt(); 00069 } 00070 00075 public void Prepare() 00076 { 00077 m_stmt = m_connect.OSQLiteDB.CreateStmt(); 00078 00079 m_prepare = m_stmt.Prepate( m_command ); 00080 00081 //パラメータバインド 00082 bindparams( m_stmt ); 00083 00084 if( m_connect.OSQLiteDB.getLastErr() != ResultEnum.DONE && 00085 m_connect.OSQLiteDB.getLastErr() != ResultEnum.ROW && 00086 m_connect.OSQLiteDB.getLastErr() != ResultEnum.OK ) 00087 { 00088 //エラー発生 00089 throw new SQLiteException( m_connect ); 00090 } 00091 } 00092 00097 protected void bindparams( OSQLiteStmtWrap stmt ) 00098 { 00099 int iCnt = 1; 00100 00101 ArrayList paramlist = new ArrayList( Parameters ); 00102 paramlist.Sort(); 00103 00104 stmt.Reset(); 00105 00106 //バインド変数セット 00107 foreach( SQLiteParameter para in paramlist ) 00108 { 00109 //NULL値 00110 if( para.Value == null ) 00111 { 00112 stmt.bindNull( iCnt ); 00113 } 00114 else 00115 if( para.DbType == DbType.String ) 00116 { 00117 stmt.bindText( iCnt, (string)para.Value ); 00118 } 00119 else 00120 if( para.DbType == DbType.Int64 ) 00121 { 00122 stmt.bindInt64( iCnt, (long)para.Value ); 00123 } 00124 else 00125 if( para.DbType == DbType.Int32 || para.DbType == DbType.Int16 ) 00126 { 00127 stmt.bindInt( iCnt, (int)para.Value ); 00128 } 00129 else 00130 if( para.DbType == DbType.Boolean ) 00131 { 00132 stmt.bindBool( iCnt, (bool)para.Value ); 00133 } 00134 else 00135 if( para.DbType == DbType.Double ) 00136 { 00137 stmt.bindDouble( iCnt, (double)para.Value ); 00138 } 00139 else 00140 if( para.DbType == DbType.Decimal ) 00141 { 00142 stmt.bindDecimal( iCnt, (decimal)para.Value ); 00143 } 00144 else 00145 if( para.DbType == DbType.Binary ) 00146 { 00147 byte[] bindbin = (byte[])para.Value; 00148 stmt.bindBlob( iCnt, new MemoryStream( bindbin ) ); 00149 } 00150 00151 iCnt++; 00152 } 00153 } 00154 00158 protected CommandType m_cmdtype = CommandType.Text; 00159 00164 public System.Data.CommandType CommandType 00165 { 00166 get 00167 { 00168 return m_cmdtype; 00169 } 00170 set 00171 { 00172 m_cmdtype = value; 00173 } 00174 } 00175 00184 public IDataReader ExecuteReader(CommandBehavior behavior) 00185 { 00186 if( m_prepare == false ) 00187 { 00188 Prepare(); 00189 } 00190 00191 if( m_prepare == false ) 00192 { 00193 //失敗 00194 throw new SQLiteException( m_connect ); 00195 } 00196 00197 OSQLiteStmtWrap stmt = m_stmt; 00198 m_stmt = null; 00199 00200 m_prepare = false; 00201 00202 m_collection = new SQLiteParameterCollection(); 00203 00204 return new SQLiteDataReader( stmt ); 00205 } 00206 00213 IDataReader System.Data.IDbCommand.ExecuteReader() 00214 { 00215 return ExecuteReader( CommandBehavior.Default ); 00216 } 00217 00224 public object ExecuteScalar() 00225 { 00226 using ( IDataReader ir = ExecuteReader( CommandBehavior.Default ) ) 00227 { 00228 ir.Read(); 00229 00230 if( m_connect.OSQLiteDB.getLastErr() != ResultEnum.DONE && 00231 m_connect.OSQLiteDB.getLastErr() != ResultEnum.ROW && 00232 m_connect.OSQLiteDB.getLastErr() != ResultEnum.OK ) 00233 { 00234 //エラー発生 00235 throw new SQLiteException( m_connect ); 00236 } 00237 00238 return ir.GetValue(0); 00239 } 00240 } 00241 00248 public int ExecuteNonQuery() 00249 { 00250 using( OSQLiteStmtWrap stmt = m_connect.OSQLiteDB.CreateStmt() ) 00251 { 00252 if( stmt.Prepate( m_command ) == false ) 00253 { 00254 if( m_connect.OSQLiteDB.getLastErr() != ResultEnum.DONE && 00255 m_connect.OSQLiteDB.getLastErr() != ResultEnum.ROW && 00256 m_connect.OSQLiteDB.getLastErr() != ResultEnum.OK ) 00257 { 00258 //エラー発生 00259 throw new SQLiteException( m_connect ); 00260 } 00261 00262 return 0; 00263 } 00264 00265 //パラメータバインド 00266 bindparams( stmt ); 00267 00268 m_stmt = null; 00269 m_prepare = false; 00270 m_collection = new SQLiteParameterCollection(); 00271 00272 //結果セットをカウント 00273 int iCnt=0; 00274 while( true ) 00275 { 00276 ResultEnum ret = stmt.Step(); 00277 if( ret != ResultEnum.ROW ) 00278 { 00279 if( ret != ResultEnum.DONE ) 00280 //エラー発生 00281 throw new SQLiteException( m_connect ); 00282 00283 break; 00284 } 00285 iCnt++; 00286 } 00287 00288 return ( iCnt <=0 ) ? m_connect.OSQLiteDB.getChanges():iCnt; 00289 } 00290 } 00291 00296 public int CommandTimeout 00297 { 00298 get 00299 { 00300 return 0; 00301 } 00302 set 00303 { 00304 } 00305 } 00306 00311 public IDbDataParameter CreateParameter() 00312 { 00313 return new SQLiteParameter(); 00314 } 00315 00319 public IDbConnection Connection 00320 { 00321 get 00322 { 00323 return m_connect; 00324 } 00325 set 00326 { 00327 m_connect = value as SQLiteConnection; 00328 } 00329 } 00330 00334 protected UpdateRowSource m_urs = UpdateRowSource.None; 00335 00340 public UpdateRowSource UpdatedRowSource 00341 { 00342 get 00343 { 00344 return m_urs; 00345 } 00346 set 00347 { 00348 m_urs = value; 00349 } 00350 } 00351 00355 public string CommandText 00356 { 00357 get 00358 { 00359 return m_command; 00360 } 00361 set 00362 { 00363 m_command = value; 00364 } 00365 } 00366 00370 public IDataParameterCollection Parameters 00371 { 00372 get 00373 { 00374 return m_collection; 00375 } 00376 } 00377 00381 public IDbTransaction Transaction 00382 { 00383 get 00384 { 00385 return m_transaction; 00386 } 00387 set 00388 { 00389 m_transaction = value as SQLiteTransaction; 00390 } 00391 } 00392 00396 new public void Dispose() 00397 { 00398 m_stmt = null; 00399 } 00400 #region ICloneable メンバ 00401 00402 public object Clone() 00403 { 00404 // TODO: SQLiteCommand.Clone 実装を追加します。 00405 return null; 00406 } 00407 00408 #endregion 00409 } 00410 }