public void IndexOf()
        {
            HsqlParameterCollection testSubject = NewTestSubject();
            HsqlParameter           value       = new HsqlParameter("foo", "bar");

            int expectedValueIndex = testSubject.Add((object)value);
            int actualValueIndex   = testSubject.IndexOf(value);

            Assert.AreEqual(expectedValueIndex, actualValueIndex);
            Assert.AreEqual(testSubject.IndexOf(value.ParameterName), testSubject.IndexOf(value));

            Assert.Fail("TODO");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Appends the statically-bound representation of this
        /// list to the given <c>StringBuilder</c>.
        /// </summary>
        /// <param name="sb">
        /// The <c>StringBuilder</c> to which to append.
        /// </param>
        /// <param name="parameters">The parameters to bind.</param>
        /// <param name="strict">
        /// When <c>true</c>, an exception is raised if there exist
        /// any unbound named parameter or parameter marker tokens.
        /// </param>
        /// <returns>
        /// The <c>StringBuilder</c> to which has been appended
        /// the statically-bound representation of this list.
        /// </returns>
        /// <remarks>
        /// The statically-bound representation of this list is the charater
        /// sequence representation in which each <c>NamedParameter</c> <c>Token</c>
        /// is replaced by an SQL literal character sequence representating the
        /// <c>Value</c> property of the matching <c>HsqlParameter</c>
        /// object from the given <c>parameters</c> collection.
        /// </remarks>
        public StringBuilder AppendStaticallyBoundForm(StringBuilder sb,
                                                       HsqlParameterCollection parameters,
                                                       bool strict)
        {
            if (ParameterCount == 0)
            {
                return(AppendNormalizedForm(sb));
            }
            else if (strict && ParameterMarkerCount > 0)
            {
                throw new HsqlDataSourceException(
                          "Cannot statically bind parameter markers"
                          + " ('?' tokens) by name."); //NOI18N
            }

            if (sb == null)
            {
                // Not perfectly accurate preallocation, but better than nothing.
                sb = new StringBuilder(m_normalizedCapacity);
            }
            else
            {
                sb.EnsureCapacity(sb.Length + m_normalizedCapacity);
            }

            int count = m_list.Count;

            for (int i = 0; i < count; i++)
            {
                Token        token = m_list[i];
                string       value = token.Value;
                SqlTokenType type  = token.Type;

                if (i > 0 && (Token.ValueFor.COMMA != value))
                {
                    sb.Append(' ');
                }

                if (type == SqlTokenType.NamedParameter)
                {
                    int index = parameters.IndexOf(value);

                    if (index >= 0)
                    {
                        HsqlParameter parameter
                            = (HsqlParameter)parameters[index];

                        sb.Append(parameter.ToSqlLiteral());
                    }
                    else if (strict)
                    {
                        throw new HsqlDataSourceException(
                                  "No binding for named parameter: "
                                  + value); // NOI18N
                    }
                    else // (index < 0 && strict == false)
                    {
                        // Effectively a named parameter pass through...
                        // may be that we want to do multi-pass binding.
                        sb.Append(value);
                    }
                }
                // Currently, this never happens, due to the
                // (strict && ParameterMarkerCount > 0) check above.
                // stubbed in as a reminder that we might have to deal
                // with this case differently, for instance using
                // a bind-by-parameter-ordinal policy.
                else if (strict && (Token.ValueFor.QUESTION == value))
                {
                    throw new HsqlDataSourceException(
                              "No binding for parameter marker: " + value); //NOI18N
                }
                else
                {
                    sb.Append(value);
                }
            }

            return(sb);
        }