/// <summary> /// ステップ実行 /// </summary> /// <param name="query">クエリ</param> /// <param name="bindList">バインドリスト</param> /// <param name="fetchPostAction">フェッチ後処理</param> /// <returns>行データ</returns> private IEnumerable <Dictionary <string, object> > ExecuteStep(string query, Dictionary <string, object> bindList = null, Action <IntPtr, Dictionary <string, object> > fetchPostAction = null) { // ポストアクション未指定なら、空アクションを設定 if (fetchPostAction == null) { fetchPostAction = (ptr, bind) => { }; } // コネクションオープン IntPtr con = System.IntPtr.Zero; try { con = Open(); // ステートメントオープン IntPtr stmt = OpenStmt(con, query); // パラメーターバインド BindParam(stmt, bindList); try { // カラムインデックス作成 int columnCount = SQLiteApi.sqlite3_column_count(stmt); Dictionary <int, string> clmIndex = new Dictionary <int, string>(); for (int i = 0; i < columnCount; i++) { clmIndex.Add(i, Marshal.PtrToStringAnsi(SQLiteApi.sqlite3_column_name(stmt, i))); } // 1行ずつデータを取得 Dictionary <string, object> dataRow = new Dictionary <string, object>(); while (SQLiteApi.sqlite3_step(stmt) == SQLiteApi.SQLITE_ROW) { // すべてのカラムのデータを取得し、カラム名をキーにした連想配列に設定 for (int i = 0; i < columnCount; i++) { dataRow[clmIndex[i]] = GetValue(stmt, i); } // データ返却 yield return(dataRow); // ポストアクション fetchPostAction(stmt, bindList); } } finally { CloseStmt(stmt); } } finally { Close(); } }
/// <summary> /// クエリ遅延実行 /// </summary> /// <param name="con">コネクション</param> /// <param name="query">クエリ</param> /// <param name="bindList">バインドリスト</param> /// <param name="isSkipExp">エラースキップフラグ</param> public static void Execute(IntPtr con, string query, Dictionary <string, object> bindList = null, bool isSkipExp = false) { // ステートメントオープン IntPtr stmt = OpenStmt(con, query); // バインド BindParam(stmt, bindList); try { // 実行 if (SQLiteApi.sqlite3_step(stmt) != SQLiteApi.SQLITE_DONE) { if (!isSkipExp) { throw new SQLiteException("Could not execute SQL statement." + query, con); } } } finally { CloseStmt(stmt); } }