storage/src/mozStorageBindingParams.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/src/mozStorageBindingParams.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef mozStorageBindingParams_h
    1.11 +#define mozStorageBindingParams_h
    1.12 +
    1.13 +#include "nsCOMArray.h"
    1.14 +#include "nsIVariant.h"
    1.15 +#include "nsInterfaceHashtable.h"
    1.16 +
    1.17 +#include "mozStorageBindingParamsArray.h"
    1.18 +#include "mozStorageStatement.h"
    1.19 +#include "mozStorageAsyncStatement.h"
    1.20 +
    1.21 +#include "mozIStorageBindingParams.h"
    1.22 +#include "IStorageBindingParamsInternal.h"
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace storage {
    1.26 +
    1.27 +class BindingParams : public mozIStorageBindingParams
    1.28 +                    , public IStorageBindingParamsInternal
    1.29 +{
    1.30 +public:
    1.31 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.32 +  NS_DECL_MOZISTORAGEBINDINGPARAMS
    1.33 +  NS_DECL_ISTORAGEBINDINGPARAMSINTERNAL
    1.34 +
    1.35 +  /**
    1.36 +   * Locks the parameters and prevents further modification to it (such as
    1.37 +   * binding more elements to it).
    1.38 +   */
    1.39 +  void lock();
    1.40 +
    1.41 +  /**
    1.42 +   * Unlocks the parameters and allows modification to it again.
    1.43 +   *
    1.44 +   * @param aOwningStatement
    1.45 +   *        The statement that owns us.  We cleared this when we were locked,
    1.46 +   *        and our invariant requires us to have this, so you need to tell us
    1.47 +   *        again.
    1.48 +   */
    1.49 +  void unlock(Statement *aOwningStatement);
    1.50 +
    1.51 +  /**
    1.52 +   * @returns the pointer to the owning BindingParamsArray.  Used by a
    1.53 +   *          BindingParamsArray to verify that we belong to it when added.
    1.54 +   */
    1.55 +  const mozIStorageBindingParamsArray *getOwner() const;
    1.56 +
    1.57 +  BindingParams(mozIStorageBindingParamsArray *aOwningArray,
    1.58 +                Statement *aOwningStatement);
    1.59 +  virtual ~BindingParams() {}
    1.60 +
    1.61 +protected:
    1.62 +  BindingParams(mozIStorageBindingParamsArray *aOwningArray);
    1.63 +  nsCOMArray<nsIVariant> mParameters;
    1.64 +  bool mLocked;
    1.65 +
    1.66 +private:
    1.67 +
    1.68 +  /**
    1.69 +   * Track the BindingParamsArray that created us until we are added to it.
    1.70 +   * (Once we are added we are locked and no one needs to look up our owner.)
    1.71 +   * Ref-counted since there is no invariant that guarantees it stays alive
    1.72 +   * otherwise.  This keeps mOwningStatement alive for us too since the array
    1.73 +   * also holds a reference.
    1.74 +   */
    1.75 +  nsCOMPtr<mozIStorageBindingParamsArray> mOwningArray;
    1.76 +  /**
    1.77 +   * Used in the synchronous binding case to map parameter names to indices.
    1.78 +   * Not reference-counted because this is only non-null as long as mOwningArray
    1.79 +   * is non-null and mOwningArray also holds a statement reference.
    1.80 +   */
    1.81 +  Statement *mOwningStatement;
    1.82 +  uint32_t mParamCount;
    1.83 +};
    1.84 +
    1.85 +/**
    1.86 + * Adds late resolution of named parameters so they don't get resolved until we
    1.87 + * try and bind the parameters on the async thread.  We also stop checking
    1.88 + * parameter indices for being too big since we just just don't know how many
    1.89 + * there are.
    1.90 + *
    1.91 + * We support *either* binding by name or binding by index.  Trying to do both
    1.92 + * results in only binding by name at sqlite3_stmt bind time.
    1.93 + */
    1.94 +class AsyncBindingParams : public BindingParams
    1.95 +{
    1.96 +public:
    1.97 +  NS_IMETHOD BindByName(const nsACString & aName,
    1.98 +                                      nsIVariant *aValue);
    1.99 +  NS_IMETHOD BindByIndex(uint32_t aIndex, nsIVariant *aValue);
   1.100 +
   1.101 +  virtual already_AddRefed<mozIStorageError> bind(sqlite3_stmt * aStatement);
   1.102 +
   1.103 +  AsyncBindingParams(mozIStorageBindingParamsArray *aOwningArray);
   1.104 +  virtual ~AsyncBindingParams() {}
   1.105 +
   1.106 +private:
   1.107 +  nsInterfaceHashtable<nsCStringHashKey, nsIVariant> mNamedParameters;
   1.108 +
   1.109 +  struct NamedParameterIterationClosureThunk
   1.110 +  {
   1.111 +    AsyncBindingParams *self;
   1.112 +    sqlite3_stmt *statement;
   1.113 +    nsCOMPtr<mozIStorageError> err;
   1.114 +  };
   1.115 +
   1.116 +  static PLDHashOperator iterateOverNamedParameters(const nsACString &aName,
   1.117 +                                                    nsIVariant *aValue,
   1.118 +                                                    void *voidClosureThunk);
   1.119 +};
   1.120 +
   1.121 +} // namespace storage
   1.122 +} // namespace mozilla
   1.123 +
   1.124 +#endif // mozStorageBindingParams_h

mercurial