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( stmt.Step() == ResultEnum.ROW ) iCnt++; 00275 00276 return iCnt; 00277 } 00278 } 00279 00284 public int CommandTimeout 00285 { 00286 get 00287 { 00288 return 0; 00289 } 00290 set 00291 { 00292 } 00293 } 00294 00299 public IDbDataParameter CreateParameter() 00300 { 00301 return new SQLiteParameter(); 00302 } 00303 00307 public IDbConnection Connection 00308 { 00309 get 00310 { 00311 return m_connect; 00312 } 00313 set 00314 { 00315 m_connect = value as SQLiteConnection; 00316 } 00317 } 00318 00322 protected UpdateRowSource m_urs = UpdateRowSource.None; 00323 00328 public UpdateRowSource UpdatedRowSource 00329 { 00330 get 00331 { 00332 return m_urs; 00333 } 00334 set 00335 { 00336 m_urs = value; 00337 } 00338 } 00339 00343 public string CommandText 00344 { 00345 get 00346 { 00347 return m_command; 00348 } 00349 set 00350 { 00351 m_command = value; 00352 } 00353 } 00354 00358 public IDataParameterCollection Parameters 00359 { 00360 get 00361 { 00362 return m_collection; 00363 } 00364 } 00365 00369 public IDbTransaction Transaction 00370 { 00371 get 00372 { 00373 return m_transaction; 00374 } 00375 set 00376 { 00377 m_transaction = value as SQLiteTransaction; 00378 } 00379 } 00380 00384 new public void Dispose() 00385 { 00386 m_stmt = null; 00387 } 00388 #region ICloneable メンバ 00389 00390 public object Clone() 00391 { 00392 // TODO: SQLiteCommand.Clone 実装を追加します。 00393 return null; 00394 } 00395 00396 #endregion 00397 } 00398 }