Example #1
0
        /// <summary>
        /// Validate values using prepared statement metadata
        /// </summary>
        private void ValidateValues(object[] values)
        {
            if (values == null)
            {
                return;
            }
            if (PreparedStatement.Metadata == null || PreparedStatement.Metadata.Columns == null || PreparedStatement.Metadata.Columns.Length == 0)
            {
                return;
            }
            var paramsMetadata = PreparedStatement.Metadata.Columns;

            if (values.Length > paramsMetadata.Length)
            {
                throw new ArgumentException(
                          String.Format("Provided {0} parameters to bind, expected {1}", values.Length, paramsMetadata.Length));
            }
            for (var i = 0; i < values.Length; i++)
            {
                var p     = paramsMetadata[i];
                var value = values[i];
                if (!TypeCodec.IsAssignableFrom(p, value))
                {
                    throw new InvalidTypeException(
                              String.Format("It is not possible to encode a value of type {0} to a CQL type {1}", value.GetType(), p.TypeCode));
                }
            }
        }
Example #2
0
        /// <summary>
        /// Validate values using prepared statement metadata,
        /// returning a new instance of values to be used as parameters.
        /// </summary>
        private object[] ValidateValues(object[] values)
        {
            if (values == null)
            {
                return(null);
            }
            if (PreparedStatement.Metadata == null || PreparedStatement.Metadata.Columns == null || PreparedStatement.Metadata.Columns.Length == 0)
            {
                return(values);
            }
            var paramsMetadata = PreparedStatement.Metadata.Columns;

            if (values.Length > paramsMetadata.Length)
            {
                throw new ArgumentException(
                          String.Format("Provided {0} parameters to bind, expected {1}", values.Length, paramsMetadata.Length));
            }
            for (var i = 0; i < values.Length; i++)
            {
                var p     = paramsMetadata[i];
                var value = values[i];
                if (!TypeCodec.IsAssignableFrom(p, value))
                {
                    throw new InvalidTypeException(
                              String.Format("It is not possible to encode a value of type {0} to a CQL type {1}", value.GetType(), p.TypeCode));
                }
            }
            if (values.Length < paramsMetadata.Length && ProtocolVersion >= 4)
            {
                //Set the result of the unspecified parameters to Unset
                var completeValues = new object[paramsMetadata.Length];
                values.CopyTo(completeValues, 0);
                for (var i = values.Length; i < paramsMetadata.Length; i++)
                {
                    completeValues[i] = Unset.Value;
                }
                values = completeValues;
            }
            return(values);
        }