Exemplo n.º 1
0
    /// <summary>
    /// Initializes the statement and attempts to get all information about parameters in the statement
    /// </summary>
    /// <param name="sqlbase">The base SQLite object</param>
    /// <param name="stmt">The statement</param>
    /// <param name="strCommand">The command text for this statement</param>
    /// <param name="previous">The previous command in a multi-statement command</param>
    internal SqliteStatement(SQLiteBase sqlbase, SqliteStatementHandle stmt, string strCommand, SqliteStatement previous)
    {
      _sql     = sqlbase;
      _sqlite_stmt = stmt;
      _sqlStatement  = strCommand;

      // Determine parameters for this statement (if any) and prepare space for them.
      int nCmdStart = 0;
      int n = _sql.Bind_ParamCount(this);
      int x;
      string s;

      if (n > 0)
      {
        if (previous != null)
          nCmdStart = previous._unnamedParameters;

        _paramNames = new string[n];
        _paramValues = new SqliteParameter[n];

        for (x = 0; x < n; x++)
        {
          s = _sql.Bind_ParamName(this, x + 1);
          if (String.IsNullOrEmpty(s))
          {
            s = String.Format(CultureInfo.InvariantCulture, ";{0}", nCmdStart);
            nCmdStart++;
            _unnamedParameters++;
          }
          _paramNames[x] = s;
          _paramValues[x] = null;
        }
      }
    }
Exemplo n.º 2
0
 internal static void Dispose(this SqliteStatementHandle statement)
 {
     try
     {
         SQLiteBase.FinalizeStatement(statement);
     }
     catch (SqliteException)
     {
     }
 }
Exemplo n.º 3
0
 internal static void FinalizeStatement(SqliteStatementHandle stmt)
 {
     lock (_lock)
     {
         int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
         if (n > 0)
         {
             throw new SqliteException(n, null);
         }
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Disposes and finalizes the statement
        /// </summary>
        public void Dispose()
        {
            if (_sqlite_stmt != null)
            {
                _sqlite_stmt.Dispose();
            }
            _sqlite_stmt = null;

            _paramNames   = null;
            _paramValues  = null;
            _sql          = null;
            _sqlStatement = null;
        }
Exemplo n.º 5
0
        internal static void FinalizeStatement(SqliteStatementHandle stmt)
        {
            lock (_lock)
            {
#if !SQLITE_STANDARD
                int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt);
#else
                int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
#endif
                if (n > 0)
                {
                    throw new SqliteException(n, null);
                }
            }
        }
Exemplo n.º 6
0
        internal static void ResetConnection(SqliteConnectionHandle db)
        {
            lock (_lock)
            {
                SqliteStatementHandle stmt = null;
                do
                {
                    stmt = UnsafeNativeMethods.sqlite3_next_stmt(db, stmt);
                    if (stmt != null)
                    {
                        UnsafeNativeMethods.sqlite3_reset(stmt);
                    }
                } while (stmt != null);

                // Not overly concerned with the return value from a rollback.
                string msg = null;
                UnsafeNativeMethods.sqlite3_exec(db, "ROLLBACK", out msg);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes the statement and attempts to get all information about parameters in the statement
        /// </summary>
        /// <param name="sqlbase">The base SQLite object</param>
        /// <param name="stmt">The statement</param>
        /// <param name="strCommand">The command text for this statement</param>
        /// <param name="previous">The previous command in a multi-statement command</param>
        internal SqliteStatement(SQLiteBase sqlbase, SqliteStatementHandle stmt, string strCommand, SqliteStatement previous)
        {
            _sql          = sqlbase;
            _sqlite_stmt  = stmt;
            _sqlStatement = strCommand;

            // Determine parameters for this statement (if any) and prepare space for them.
            int    nCmdStart = 0;
            int    n         = _sql.Bind_ParamCount(this);
            int    x;
            string s;

            if (n > 0)
            {
                if (previous != null)
                {
                    nCmdStart = previous._unnamedParameters;
                }

                _paramNames  = new string[n];
                _paramValues = new SqliteParameter[n];

                for (x = 0; x < n; x++)
                {
                    s = _sql.Bind_ParamName(this, x + 1);
                    if (String.IsNullOrEmpty(s))
                    {
                        s = String.Format(CultureInfo.InvariantCulture, ";{0}", nCmdStart);
                        nCmdStart++;
                        _unnamedParameters++;
                    }
                    _paramNames[x]  = s;
                    _paramValues[x] = null;
                }
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// Disposes and finalizes the statement
 /// </summary>
 public void Dispose()
 {
   if (_sqlite_stmt != null)
   {
     _sqlite_stmt.Dispose();
   }
   _sqlite_stmt = null;
   
   _paramNames = null;
   _paramValues = null;
   _sql = null;
   _sqlStatement = null;
 }
Exemplo n.º 9
0
    internal static void FinalizeStatement(SqliteStatementHandle stmt)
    {
      lock (_lock)
      {
#if !SQLITE_STANDARD
        int n = UnsafeNativeMethods.sqlite3_finalize_interop(stmt);
#else
      int n = UnsafeNativeMethods.sqlite3_finalize(stmt);
#endif
        if (n > 0) throw new SqliteException(n, null);
      }
    }
Exemplo n.º 10
0
        internal override SqliteStatement Prepare(SqliteConnection cnn, string strSql, SqliteStatement previous,
                                                  uint timeout, out string strRemain)
        {
            SqliteStatementHandle stmt = null;
            string ptr = null;

            int             len       = 0;
            int             n         = 17;
            int             retries   = 0;
            SqliteStatement cmd       = null;
            var             rnd       = new Random();
            var             starttick = (uint)Environment.TickCount;

            while ((n == 17 || n == 6 || n == 5) && retries < 3)
            {
                n   = UnsafeNativeMethods.sqlite3_prepare16(_sql, strSql, strSql.Length, out stmt, out ptr);
                len = -1;

                if (n == 17)
                {
                    retries++;
                }
                else if (n == 1)
                {
                    if (String.Compare(SQLiteLastError(), "near \"TYPES\": syntax error",
                                       StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        int pos = strSql.IndexOf(';');
                        if (pos == -1)
                        {
                            pos = strSql.Length - 1;
                        }

                        string typedefs = strSql.Substring(0, pos + 1);
                        strSql = strSql.Substring(pos + 1);

                        strRemain = "";

                        while (cmd == null && strSql.Length > 0)
                        {
                            cmd    = Prepare(cnn, strSql, previous, timeout, out strRemain);
                            strSql = strRemain;
                        }

                        if (cmd != null)
                        {
                            cmd.SetTypes(typedefs);
                        }

                        return(cmd);
                    }
                    else if (_buildingSchema == false &&
                             String.Compare(SQLiteLastError(), 0, "no such table: TEMP.SCHEMA", 0, 26,
                                            StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        strRemain       = "";
                        _buildingSchema = true;
                        try
                        {
                            while (cmd == null && strSql.Length > 0)
                            {
                                cmd    = Prepare(cnn, strSql, previous, timeout, out strRemain);
                                strSql = strRemain;
                            }

                            return(cmd);
                        }
                        finally
                        {
                            _buildingSchema = false;
                        }
                    }
                }
                else if (n == 6 || n == 5)     // Locked -- delay a small amount before retrying
                {
                    // Keep trying, but if we've exceeded the command's timeout, give up and throw an error
                    if ((uint)Environment.TickCount - starttick > timeout)
                    {
                        throw new SqliteException(n, SQLiteLastError());
                    }
                    else
                    {
                        // Otherwise sleep for a random amount of time up to 150ms
                        Sleep(rnd.Next(1, 150));
                    }
                }
            }

            if (n > 0)
            {
                throw new SqliteException(n, SQLiteLastError());
            }

            strRemain = UTF8ToString(ptr, len);

            var hdl = stmt;

            if (stmt != null)
            {
                cmd = new SqliteStatement(this, hdl, strSql.Substring(0, strSql.Length - strRemain.Length), previous);
            }

            return(cmd);
        }