internal void ReadpVm(string query, SQLiteResultSet set1, ref IntPtr pVm)
        {
            int         pN;
            SqliteError res = SqliteError.ERROR;

            if (pVm == IntPtr.Zero)
            {
                ThrowError("SQLiteClient: pvm=null", query, res);
            }
            DateTime now = DateTime.Now;
            TimeSpan ts  = now - DateTime.Now;

            while (true && ts.TotalSeconds > -15)
            {
                for (int i = 0; i <= busyRetries; i++)
                {
                    res = sqlite3_step(pVm);
                    if (res == SqliteError.LOCKED || res == SqliteError.BUSY)
                    {
                        Thread.Sleep(busyRetryDelay);
                    }
                    else
                    {
                        if (i > 0)
                        {
                            Log.Debug("SqlClient: database was busy (Available after " + (i + 1) + " retries)");
                        }
                        break;
                    }
                }

                pN = sqlite3_column_count(pVm);

                /*
                 * if (res == SqliteError.ERROR)
                 * {
                 * ThrowError("sqlite3_step", query, res);
                 * }
                 */
                if (res == SqliteError.DONE)
                {
                    break;
                }


                // when resuming from hibernation or standby and where the db3 files are located on a network drive, we often end up in a neverending loop
                // while (true)...it never exits. and the app is hanging.
                // Lets handle it by disconnecting the DB, and then reconnect.
                if (res == SqliteError.BUSY || res == SqliteError.ERROR)
                {
                    this.Close();

                    dbHandle = IntPtr.Zero;

                    // bool res2 = WaitForFile(this.DBName);

                    SqliteError err = (SqliteError)sqlite3_open16(this.DBName, out dbHandle);

                    if (err != SqliteError.OK)
                    {
                        throw new SQLiteException(string.Format("Failed to re-open database, SQLite said: {0} {1}", DBName,
                                                                err.ToString()));
                    }
                    else
                    {
                        IntPtr pzTail;
                        err = sqlite3_prepare16(dbHandle, query, query.Length * 2, out pVm, out pzTail);

                        res = sqlite3_step(pVm);
                        pN  = sqlite3_column_count(pVm);

                        if (pVm == IntPtr.Zero)
                        {
                            ThrowError("sqlite3_prepare16:pvm=null", query, err);
                        }
                    }
                }

                // We have some data; lets read it
                if (set1.ColumnNames.Count == 0)
                {
                    for (int i = 0; i < pN; i++)
                    {
                        string colName;
                        IntPtr pName = sqlite3_column_name16(pVm, i);
                        if (pName == IntPtr.Zero)
                        {
                            ThrowError(String.Format("SqlClient:sqlite3_column_name16() returned null {0}/{1}", i, pN), query, res);
                        }
                        colName = Marshal.PtrToStringUni(pName);
                        set1.columnNames.Add(colName);
                        set1.ColumnIndices[colName] = i;
                    }
                }

                SQLiteResultSet.Row row = new SQLiteResultSet.Row();
                for (int i = 0; i < pN; i++)
                {
                    string colData = "";
                    IntPtr pName   = sqlite3_column_text16(pVm, i);
                    if (pName != IntPtr.Zero)
                    {
                        colData = Marshal.PtrToStringUni(pName);
                    }
                    row.fields.Add(colData);
                }
                set1.Rows.Add(row);

                ts = now - DateTime.Now;
            }

            if (res == SqliteError.BUSY || res == SqliteError.ERROR)
            {
                ThrowError("sqlite3_step", query, res);
            }
        }
예제 #2
0
    internal void ReadpVm(string query, SQLiteResultSet set1, ref IntPtr pVm)
    {
      int pN;
      SqliteError res = SqliteError.ERROR;

      if (pVm == IntPtr.Zero)
      {
        ThrowError("SQLiteClient: pvm=null", query, res);
      }
      DateTime now = DateTime.Now;
      TimeSpan ts = now - DateTime.Now;
      while (true && ts.TotalSeconds > -15)
      {
        res = sqlite3_step(pVm);
        pN = sqlite3_column_count(pVm);
        /*
        if (res == SqliteError.ERROR)
        {
          ThrowError("sqlite3_step", query, res);
        }
        */
        if (res == SqliteError.DONE)
        {
          break;
        }


        // when resuming from hibernation or standby and where the db3 files are located on a network drive, we often end up in a neverending loop
        // while (true)...it never exits. and the app is hanging.
        // Lets handle it by disconnecting the DB, and then reconnect.
        if (res == SqliteError.BUSY || res == SqliteError.ERROR)
        {
          this.Close();

          dbHandle = IntPtr.Zero;

          // bool res2 = WaitForFile(this.DBName);

          SqliteError err = (SqliteError)sqlite3_open16(this.DBName, out dbHandle);

          if (err != SqliteError.OK)
          {
            throw new SQLiteException(string.Format("Failed to re-open database, SQLite said: {0} {1}", DBName,
                                                    err.ToString()));
          }
          else
          {
            IntPtr pzTail;
            err = sqlite3_prepare16(dbHandle, query, query.Length * 2, out pVm, out pzTail);

            res = sqlite3_step(pVm);
            pN = sqlite3_column_count(pVm);

            if (pVm == IntPtr.Zero)
            {
              ThrowError("sqlite3_prepare16:pvm=null", query, err);
            }
          }
        }

        // We have some data; lets read it
        if (set1.ColumnNames.Count == 0)
        {
          for (int i = 0; i < pN; i++)
          {
            string colName;
            IntPtr pName = sqlite3_column_name16(pVm, i);
            if (pName == IntPtr.Zero)
            {
              ThrowError(String.Format("SqlClient:sqlite3_column_name16() returned null {0}/{1}", i, pN), query, res);
            }
            colName = Marshal.PtrToStringUni(pName);
            set1.columnNames.Add(colName);
            set1.ColumnIndices[colName] = i;
          }
        }

        SQLiteResultSet.Row row = new SQLiteResultSet.Row();
        for (int i = 0; i < pN; i++)
        {
          string colData = "";
          IntPtr pName = sqlite3_column_text16(pVm, i);
          if (pName != IntPtr.Zero)
          {
            colData = Marshal.PtrToStringUni(pName);
          }
          row.fields.Add(colData);
        }
        set1.Rows.Add(row);

        ts = now - DateTime.Now;
      }

      if (res == SqliteError.BUSY || res == SqliteError.ERROR)
      {
        ThrowError("sqlite3_step", query, res);
      }
    }