/// <summary>
        ///   Adds a <see cref="T:CUBRID.Data.CUBRIDClient.CUBRIDParameter" /> item with the specified value to the <see
        ///    cref="T:CUBRID.Data.CUBRIDClient.CUBRIDParameterCollection" />.
        /// </summary>
        /// <param name="value"> The <see cref="P:CUBRID.Data.CUBRIDClient.CUBRIDParameter.Value" /> of the <see
        ///    cref="T:CUBRID.Data.CUBRIDClient.CUBRIDParameter" /> to add to the collection. </param>
        /// <returns> The index of the <see cref="T:CUBRID.Data.CUBRIDClient.CUBRIDParameter" /> object in the collection. </returns>
        public override int Add(object value)
        {
            CUBRIDParameter parameter = value as CUBRIDParameter;

            if (parameter == null)
            {
                throw new ArgumentException(Utils.GetStr(MsgId.OnlyCUBRIDParameterObjectsAreValid));
            }

            if (string.IsNullOrEmpty(parameter.ParameterName))
            {
                throw new ArgumentException(Utils.GetStr(MsgId.ParametersMustBeNamed));
            }

            if (!parameter.ParameterName.StartsWith("?"))
            {
                throw new ArgumentException(Utils.GetStr(MsgId.ParameterNameMustStartWith));
            }

            if (paramList.Contains(parameter))
            {
                throw new ArgumentException(Utils.GetStr(MsgId.ParameterAlreadyAdded));
            }

            if (parameter.GetParameterEncoding() == null)
            {
                parameter.SetParameterEncoding(GetParametersEncoding());
            }

            try
            {
                parameter.Pos = cmdText.IndexOf(parameter.ParameterName);
                if (paramList.Count == 0 || parameter.Pos == -1)
                {
                    paramList.Add(parameter);
                }
                else
                {
                    for (int i = 0; i < paramList.Count; i++)
                    {
                        if (paramList[i].Pos > parameter.Pos)
                        {
                            paramList.Insert(IndexOf(paramList[i]), parameter);
                            break;
                        }
                    }
                    if (!paramList.Contains(parameter))
                    {
                        paramList.Add(parameter);
                    }
                }
            }
            catch
            {
                paramList.Add(parameter);
            }
            return(IndexOf(parameter));
        }
Esempio n. 2
0
        public static int cci_bind_param(CUBRIDConnection conn, int handle, int index, T_CCI_A_TYPE a_type, CUBRIDParameter param, CUBRIDDataType u_type, char flag)
        {
            int ret = 0;
            IntPtr p = IntPtr.Zero;
            switch (param.CUBRIDDataType)
            {
                case CUBRIDDataType.CCI_U_TYPE_DATE:
                case CUBRIDDataType.CCI_U_TYPE_TIME:
                case CUBRIDDataType.CCI_U_TYPE_DATETIME:
                case CUBRIDDataType.CCI_U_TYPE_TIMESTAMP:
                    string date  = param.Value.ToString();
                    if (param.Value.GetType()== typeof(DateTime))
                    {
                        DateTime d = Convert.ToDateTime(param.Value);
                        date = string.Format("{0:u}", d);
                        date = date.Remove(date.Length - 1);
                    }
                    p = Marshal.StringToCoTaskMemAnsi(date);
                    ret = bind_param(handle, index, a_type, p, u_type, flag);
                    break;
                case CUBRIDDataType.CCI_U_TYPE_SET:
                case CUBRIDDataType.CCI_U_TYPE_MULTISET:
                case CUBRIDDataType.CCI_U_TYPE_SEQUENCE:
                    IntPtr set = IntPtr.Zero;
                    string[] value = data_format((object[])param.Value, param.InnerCUBRIDDataType);
                    int[] indicator = new int[value.Length];
                    for (int i = 0; i < value.Length; i++)
                    {
                        if (value[i] != null)
                        {
                            indicator[i] = 0;
                        }
                        else
                        {
                            indicator[i] = 1;
                        }
                    }
                    //CUBRIDDataType.CCI_U_TYPE_STRING or param.InnerCUBRIDDataType
                    ret = cci_set_make(ref set, CUBRIDDataType.CCI_U_TYPE_STRING, value.Length, value, indicator);
                    if (ret < 0)
                    {
                        return ret;
                    }

                    ret = bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_SET, set, param.CUBRIDDataType, flag);
                    cci_set_free(set);
                    break;
                case CUBRIDDataType.CCI_U_TYPE_BLOB:
                    CUBRIDBlob blob = (CUBRIDBlob)param.Value;
                    bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_BLOB, blob.GetPackedLobHandle(), param.CUBRIDDataType, flag);
                    break;
                case CUBRIDDataType.CCI_U_TYPE_CLOB:
                    CUBRIDClob clob = (CUBRIDClob)param.Value;
                    bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_CLOB, clob.GetPackedLobHandle(), param.CUBRIDDataType, flag);
                    break;
                case CUBRIDDataType.CCI_U_TYPE_BIT:
                case CUBRIDDataType.CCI_U_TYPE_VARBIT:
                    T_CCI_BIT bit = new T_CCI_BIT();
                    bit.size = ((byte[])param.Value).Length;
                    bit.buf = Marshal.AllocHGlobal(bit.size);
                    Marshal.Copy((byte[])param.Value, 0, bit.buf, bit.size);
                    p = Marshal.AllocHGlobal(Marshal.SizeOf(bit));
                    Marshal.StructureToPtr(bit, p, false);
                    ret = bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_BIT, p, param.CUBRIDDataType, flag);
                    Marshal.FreeHGlobal(p);
                    break;
                case CUBRIDDataType.CCI_U_TYPE_NULL:
                    ret = bind_param(handle, index, a_type, IntPtr.Zero, u_type, flag);
                    break;
                default:
                    string bind_value = param.Value.ToString();
                    if (param.GetParameterEncoding().Equals(conn.GetEncoding()))
                    {
                        bind_value = Encoding.Default.GetString(conn.GetEncoding().GetBytes(param.Value.ToString()));
                    }
                    else
                    {
                        bind_value = Encoding.Default.GetString(param.GetParameterEncoding().GetBytes(param.Value.ToString()));
                    }
                    p = Marshal.StringToCoTaskMemAnsi(bind_value);
                    ret = bind_param(handle, index, a_type, p, u_type, flag);
                    break;
            }
            return ret;                  
        }
        public static int cci_bind_param(CUBRIDConnection conn, int handle, int index, T_CCI_A_TYPE a_type, CUBRIDParameter param, CUBRIDDataType u_type, char flag)
        {
            int    ret = 0;
            IntPtr p   = IntPtr.Zero;

            switch (param.CUBRIDDataType)
            {
            case CUBRIDDataType.CCI_U_TYPE_DATE:
            case CUBRIDDataType.CCI_U_TYPE_TIME:
            case CUBRIDDataType.CCI_U_TYPE_DATETIME:
            case CUBRIDDataType.CCI_U_TYPE_TIMESTAMP:
                string date = param.Value.ToString();
                if (param.Value.GetType() == typeof(DateTime))
                {
                    DateTime d = Convert.ToDateTime(param.Value);
                    date = string.Format("{0:u}", d);
                    date = date.Remove(date.Length - 1);
                }
                p   = Marshal.StringToCoTaskMemAnsi(date);
                ret = bind_param(handle, index, a_type, p, u_type, flag);
                break;

            case CUBRIDDataType.CCI_U_TYPE_SET:
            case CUBRIDDataType.CCI_U_TYPE_MULTISET:
            case CUBRIDDataType.CCI_U_TYPE_SEQUENCE:
                IntPtr   set       = IntPtr.Zero;
                string[] value     = data_format((object[])param.Value, param.InnerCUBRIDDataType);
                int[]    indicator = new int[value.Length];
                for (int i = 0; i < value.Length; i++)
                {
                    if (value[i] != null)
                    {
                        indicator[i] = 0;
                    }
                    else
                    {
                        indicator[i] = 1;
                    }
                }
                //CUBRIDDataType.CCI_U_TYPE_STRING or param.InnerCUBRIDDataType
                ret = cci_set_make(ref set, CUBRIDDataType.CCI_U_TYPE_STRING, value.Length, value, indicator);
                if (ret < 0)
                {
                    return(ret);
                }

                ret = bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_SET, set, param.CUBRIDDataType, flag);
                cci_set_free(set);
                break;

            case CUBRIDDataType.CCI_U_TYPE_BLOB:
                CUBRIDBlob blob = (CUBRIDBlob)param.Value;
                bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_BLOB, blob.GetPackedLobHandle(), param.CUBRIDDataType, flag);
                break;

            case CUBRIDDataType.CCI_U_TYPE_CLOB:
                CUBRIDClob clob = (CUBRIDClob)param.Value;
                bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_CLOB, clob.GetPackedLobHandle(), param.CUBRIDDataType, flag);
                break;

            case CUBRIDDataType.CCI_U_TYPE_BIT:
            case CUBRIDDataType.CCI_U_TYPE_VARBIT:
                T_CCI_BIT bit = new T_CCI_BIT();
                bit.size = ((byte[])param.Value).Length;
                bit.buf  = Marshal.AllocHGlobal(bit.size);
                Marshal.Copy((byte[])param.Value, 0, bit.buf, bit.size);
                p = Marshal.AllocHGlobal(Marshal.SizeOf(bit));
                Marshal.StructureToPtr(bit, p, false);
                ret = bind_param(handle, index, T_CCI_A_TYPE.CCI_A_TYPE_BIT, p, param.CUBRIDDataType, flag);
                Marshal.FreeHGlobal(p);
                break;

            case CUBRIDDataType.CCI_U_TYPE_NULL:
                ret = bind_param(handle, index, a_type, IntPtr.Zero, u_type, flag);
                break;

            default:
                byte[] bind_value;     // = param.Value.ToString();
                if (conn.GetEncoding() != null)
                {
                    bind_value = conn.GetEncoding().GetBytes(param.Value.ToString());
                }
                else
                {
                    bind_value = param.GetParameterEncoding().GetBytes(param.Value.ToString());
                }
                ret = bind_param(handle, index, a_type, bind_value, u_type, flag);
                break;
            }
            return(ret);
        }