Ejemplo n.º 1
0
        /// <summary>
        /// Executes the supplied query and calls the callback function for each row of result
        /// </summary>
        /// <remarks>
        /// <p>
        /// If the database is busy (ie with another process/thread using it) then
        /// this method will call Thread.Sleep(), and then retry the query. Number of
        /// retries and retry delay are configurable using the appropriate properties.</p>
        /// <p>The result set object may be empty if there are no results, or if the
        /// query does not return results (eg. UPDATE, INSERT, DELETE etc)</p>
        /// </remarks>
        /// <param name="query">The SQL query to execute</param>
        /// <param name="callback">The callback function to call
        /// (object Param, string[] ColumnNames, ArrayList Data)</param>
        /// <param name="Param">A object to pass to the callback function</param>
        /// <exception cref="SQLiteException">
        /// Thrown if an error occurs or if the database is busy and the retries
        /// are exhausted.
        /// </exception>
        public unsafe void Execute(string query, ResultCallBack callback, object Param)
        {
            column_names = null; //reset for each new query
            param        = Param;
            callbackfunc = callback;

            ResultCode errorCode;
            string     errorMsg;
            int        retries = 0;

            using (SqliteString sql = new SqliteString(query))
            {
                while ((errorCode = SQLiteClient.sqlite3_exec(
                            this.dbHandle,
                            sql.ToPointer(),
                            (callbackfunc == null) ? null : new SQLiteCallback(CallBack),
                            IntPtr.Zero, out errorMsg))
                       == ResultCode.BUSY && retries < this.busyRetries)
                {
                    Thread.Sleep(this.busyRetryDelay);
                    ++retries;
                    continue;
                }

                if (errorCode != ResultCode.OK)
                {
                    throw new SQLiteException(SQLiteClient.GetMessageForError(errorCode) + ":\n"
                                              + errorMsg + "\n the query string is :\n" + query, errorCode);
                }
            }
        }