Azavea.Open.DAO.Firebird.FirebirdDescriptor.SetParametersOnCommand C# (CSharp) Method

SetParametersOnCommand() public method

public SetParametersOnCommand ( IDbCommand cmd, IEnumerable parameters ) : void
cmd IDbCommand
parameters IEnumerable
return void
        public override void SetParametersOnCommand(IDbCommand cmd, IEnumerable parameters)
        {
            IEnumerator enumer = parameters.GetEnumerator();

            if (enumer.MoveNext())
            {
                // There's at least one, so split apart the sql.
                string[] sqlPieces = cmd.CommandText.Split('?');
                StringBuilder sb = DbCaches.StringBuilders.Get();
                for (int x = 0; x < (sqlPieces.Length - 1); x++)
                {
                    // First append the sql fragment.
                    sb.Append(sqlPieces[x]);
                    // The name of the param has to be prepended with a @ in the sql, but not
                    // on the parameter object.
                    sb.Append("@");
                    string paramName = DbCaches.ParamNames.Get(x);
                    sb.Append(paramName);

                    // Get the value to insert.
                    object param = enumer.Current ?? DBNull.Value;
                    // Construct and add the parameter object.
                    cmd.Parameters.Add(new FbParameter(paramName, param));

                    // Move the enumerator to the next item, if there should be another one.
                    if ((x + 1) < (sqlPieces.Length - 1))
                    {
                        if (!enumer.MoveNext())
                        {
                            throw new ArgumentException("Command sql has " +
                                                        (sqlPieces.Length - 1) + " params, but you only passed " +
                                                        (x + 1) +
                                                        " parameters: " +
                                                        SqlUtilities.SqlParamsToString(cmd.CommandText, parameters) +
                                                        "  You may get this if your sql string has a ? in it.  In that case you" +
                                                        " can try parameterizing whatever string constant has the ? and passing" +
                                                        " the constant from your code.  Not super elegant, but a whole lot easier than" +
                                                        " making this code understand quoted or escaped characters in the sql string.");
                        }
                    }
                }
                // Check that we don't have leftover parameters.
                if (enumer.MoveNext())
                {
                    throw new ArgumentException("Command sql has " +
                                                (sqlPieces.Length - 1) + " params, but you passed more than that: " +
                                                SqlUtilities.SqlParamsToString(cmd.CommandText, parameters));
                }
                // Append the last sql fragment.
                sb.Append(sqlPieces[sqlPieces.Length - 1]);
                cmd.CommandText = sb.ToString();
                DbCaches.StringBuilders.Return(sb);
            }
        }