00001 using System;
00002 using System.IO;
00003 using System.Runtime.InteropServices;
00004
00005 namespace SQLiteCSLib.Inner
00006 {
00010 public class OSQLiteStmtWrap : IDisposable
00011 {
00015 protected IntPtr m_impl = IntPtr.Zero;
00016
00020 OSQLiteDBWrap m_dbwrap;
00021
00025 public OSQLiteStmtWrap( OSQLiteDBWrap dbwrap )
00026 {
00027 m_dbwrap = dbwrap;
00028 m_impl = osqlite3_stmt_new( dbwrap.internaldb() );
00029 }
00030
00034 ‾OSQLiteStmtWrap()
00035 {
00036 Dispose();
00037 }
00038
00042 public void Dispose()
00043 {
00044 if( m_impl != IntPtr.Zero )
00045 {
00046 osqlite3_stmt_delete( m_impl );
00047 m_impl = IntPtr.Zero;
00048 }
00049 }
00050
00055 public OSQLiteDBWrap DbWrap()
00056 {
00057 return m_dbwrap;
00058 }
00059
00065 public bool Execute( string sql )
00066 {
00067 return osqlite3_stmt_execute( m_impl, sql );
00068 }
00069
00075 public bool Prepate( string sql )
00076 {
00077 return osqlite3_stmt_prepare( m_impl, sql );
00078 }
00079
00085 public ResultEnum Step()
00086 {
00087 return (ResultEnum)osqlite3_stmt_step( m_impl );
00088
00089 }
00090
00096 public int data_count()
00097 {
00098 return osqlite3_stmt_data_count( m_impl );
00099 }
00100
00105 public int column_count()
00106 {
00107 return osqlite3_stmt_data_count( m_impl );
00108 }
00109
00115 public DATATYPE getType( int icol )
00116 {
00117 return (DATATYPE)osqlite3_stmt_getType( m_impl, icol );
00118 }
00119
00125 public int getInt( int icol )
00126 {
00127 return osqlite3_stmt_getInt( m_impl, icol );
00128 }
00129
00135 public long getInt64( int icol )
00136 {
00137 #if MOBILEPC
00138 long lVal = 0;
00139 osqlite3_stmt_getInt64( m_impl, icol, ref lVal );
00140 return lVal;
00141 #else
00142 return osqlite3_stmt_getInt64( m_impl, icol );
00143 #endif
00144 }
00145
00151 public double getDouble( int icol )
00152 {
00153 #if MOBILEPC
00154 double dVal = 0.0;
00155 osqlite3_stmt_getDouble( m_impl, icol, ref dVal );
00156 return dVal;
00157 #else
00158 return osqlite3_stmt_getDouble( m_impl, icol );
00159 #endif
00160 }
00161
00167 public string getText( int icol )
00168 {
00169 return StringFromC.String( osqlite3_stmt_getText( m_impl, icol ) );
00170 }
00171
00177 public MemoryStream getBlob( int icol )
00178 {
00179 int iLen = 0;
00180 IntPtr pBin = osqlite3_stmt_getBlob( m_impl, icol, ref iLen );
00181 byte[] bBin = new byte[iLen];
00182 Marshal.Copy( pBin, bBin, 0, iLen );
00183 return new MemoryStream(bBin);
00184 }
00185
00191 public string getColumnName( int icol )
00192 {
00193 return StringFromC.String( osqlite3_stmt_getColumnName( m_impl, icol ) );
00194 }
00195
00201 public string getColumnTableName( int icol )
00202 {
00203 return StringFromC.String( osqlite3_stmt_getColumnTableName( m_impl, icol ) );
00204 }
00205
00211 public string getColumnOriginalName( int icol )
00212 {
00213 return StringFromC.String( osqlite3_stmt_getColumnOriginalName( m_impl, icol ) );
00214 }
00215
00221 public string getColumnDecltype( int icol )
00222 {
00223 return StringFromC.String( osqlite3_stmt_getColumnDecltype( m_impl, icol ) );
00224 }
00225
00231 public int getSize( int icol )
00232 {
00233 return osqlite3_stmt_getSize( m_impl, icol );
00234 }
00235
00240 public bool Reset( )
00241 {
00242 return osqlite3_stmt_reset( m_impl );
00243 }
00244
00251 public bool bindBool( int icol, bool val )
00252 {
00253 return osqlite3_stmt_bindBool( m_impl, icol, val );
00254 }
00255
00262 public bool bindInt( int icol, int val )
00263 {
00264 return osqlite3_stmt_bindInt( m_impl, icol, val );
00265 }
00266
00274 public bool bindInt64( int icol, long val )
00275 {
00276 #if MOBILEPC
00277 return osqlite3_stmt_bindInt64( m_impl, icol, ref val );
00278 #else
00279 return osqlite3_stmt_bindInt64( m_impl, icol, val );
00280 #endif
00281 }
00282
00289 public bool bindDouble( int icol, double val )
00290 {
00291 #if MOBILEPC
00292 return osqlite3_stmt_bindDouble( m_impl, icol, ref val );
00293 #else
00294 return osqlite3_stmt_bindDouble( m_impl, icol, val );
00295 #endif
00296 }
00297
00305 public bool bindDecimal( int icol, decimal val )
00306 {
00307 #if MOBILEPC
00308 return osqlite3_stmt_bindDecimal( m_impl, icol, ref val );
00309 #else
00310 return osqlite3_stmt_bindDecimal( m_impl, icol, val );
00311 #endif
00312 }
00313
00320 public bool bindText( int icol, string val )
00321 {
00322 return osqlite3_stmt_bindText( m_impl, icol, val );
00323 }
00324
00330 public bool bindNull( int icol )
00331 {
00332 return osqlite3_stmt_bindNull( m_impl, icol );
00333 }
00334
00341 public bool bindBlob( int icol, MemoryStream val )
00342 {
00343
00344 int iValLen = (int)val.Length;
00345 val.Seek( 0, SeekOrigin.Begin );
00346
00347 byte[] bBin = new byte[iValLen];
00348 val.Read( bBin, 0, iValLen );
00349
00350 IntPtr pBin = IntPtr.Zero;
00351 try
00352 {
00353
00354 #if MOBILEPC
00355 pBin = AllocHGlobal( iValLen );
00356 #else
00357 pBin = Marshal.AllocHGlobal( iValLen );
00358 #endif
00359
00360 Marshal.Copy( bBin, 0, pBin, iValLen );
00361
00362 return osqlite3_stmt_bindBlob( m_impl, icol, pBin, iValLen );
00363 }
00364 finally
00365 {
00366 if( pBin != IntPtr.Zero )
00367 {
00368 #if MOBILEPC
00369 FreeHGlobal(pBin);
00370 #else
00371 Marshal.FreeHGlobal(pBin);
00372 #endif
00373 }
00374 }
00375 }
00376
00377 #region アンマネージ定義
00378 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00379 protected extern static IntPtr osqlite3_stmt_new( IntPtr dbwrap );
00380
00381 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00382 protected extern static void osqlite3_stmt_delete( IntPtr instance );
00383
00384 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00385 protected extern static bool osqlite3_stmt_execute( IntPtr instance, string sql );
00386
00387 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00388 protected extern static bool osqlite3_stmt_prepare( IntPtr instance, string sql );
00389
00390 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00391 protected extern static int osqlite3_stmt_step( IntPtr instance );
00392
00393 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00394 protected extern static int osqlite3_stmt_data_count( IntPtr instance );
00395
00396 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00397 protected extern static int osqlite3_stmt_column_count( IntPtr instance );
00398
00399 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00400 protected extern static int osqlite3_stmt_getType( IntPtr instance, int icol );
00401
00402 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00403 protected extern static int osqlite3_stmt_getInt( IntPtr instance, int icol );
00404 #if MOBILEPC
00405 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00406 protected extern static void osqlite3_stmt_getInt64( IntPtr instance, int icol, ref long lVal );
00407
00408 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00409 protected extern static void osqlite3_stmt_getDouble( IntPtr instance, int icol, ref double dVal );
00410 #else
00411 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00412 protected extern static long osqlite3_stmt_getInt64( IntPtr instance, int icol );
00413
00414 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00415 protected extern static double osqlite3_stmt_getDouble( IntPtr instance, int icol );
00416 #endif
00417
00418 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00419 protected extern static IntPtr osqlite3_stmt_getText( IntPtr instance, int icol );
00420
00421 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00422 protected extern static IntPtr osqlite3_stmt_getBlob( IntPtr instance, int icol, ref int valsize );
00423
00424 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00425 protected extern static IntPtr osqlite3_stmt_getColumnName( IntPtr instance, int icol );
00426
00427 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00428 protected extern static IntPtr osqlite3_stmt_getColumnTableName( IntPtr instance, int icol );
00429
00430 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00431 protected extern static IntPtr osqlite3_stmt_getColumnOriginalName( IntPtr instance, int icol );
00432
00433 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00434 protected extern static IntPtr osqlite3_stmt_getColumnDecltype( IntPtr instance, int icol );
00435
00436 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00437 protected extern static int osqlite3_stmt_getSize( IntPtr instance, int icol );
00438
00439 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00440 protected extern static bool osqlite3_stmt_reset( IntPtr instance );
00441
00442 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00443 protected extern static bool osqlite3_stmt_bindBool( IntPtr instance, int icol, bool val );
00444
00445 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00446 protected extern static bool osqlite3_stmt_bindInt( IntPtr instance, int icol, int val );
00447
00448 #if MOBILEPC
00449 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00450 protected extern static bool osqlite3_stmt_bindInt64( IntPtr instance, int icol, ref long val );
00451
00452 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00453 protected extern static bool osqlite3_stmt_bindDouble( IntPtr instance, int icol, ref double val );
00454
00455 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00456 protected extern static bool osqlite3_stmt_bindDecimal( IntPtr instance, int icol, ref decimal val );
00457 #else
00458 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00459 protected extern static bool osqlite3_stmt_bindInt64( IntPtr instance, int icol, long val );
00460
00461 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00462 protected extern static bool osqlite3_stmt_bindDouble( IntPtr instance, int icol, double val );
00463
00464 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00465 protected extern static bool osqlite3_stmt_bindDecimal( IntPtr instance, int icol, decimal val );
00466 #endif
00467
00468 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00469 protected extern static bool osqlite3_stmt_bindText( IntPtr instance, int icol, string val );
00470
00471 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00472 protected extern static bool osqlite3_stmt_bindNull( IntPtr instance, int icol );
00473
00474 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00475 protected extern static bool osqlite3_stmt_bindBlob( IntPtr instance, int icol, IntPtr val, int valsize );
00476
00477 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00478 protected extern static IntPtr AllocHGlobal( int isize );
00479
00480 [DllImport("osqlite.dll",CharSet = CharSet.Unicode)]
00481 protected extern static void FreeHGlobal( IntPtr pMem );
00482
00483 #endregion
00484 }
00485 }