예제 #1
0
        public override void Resolve()
        {
            // first retrieve the procedure definition from our
            // procedure cache
            string spName = commandText;

            if (spName.IndexOf(".") == -1)
            {
                spName = Connection.Database + "." + spName;
            }

            DataSet ds = GetParameters(spName);

            DataTable procTable = ds.Tables["procedures"];

            parametersTable = ds.Tables["procedure parameters"];

            StringBuilder sqlStr = new StringBuilder();
            StringBuilder setStr = new StringBuilder();

            outSelect = String.Empty;

            string retParm = GetReturnParameter();

            foreach (DataRow param in parametersTable.Rows)
            {
                if (param["ORDINAL_POSITION"].Equals(0))
                {
                    continue;
                }
                string mode  = (string)param["PARAMETER_MODE"];
                string pName = (string)param["PARAMETER_NAME"];

                // make sure the parameters given to us have an appropriate
                // type set if it's not already
                MySqlParameter p = command.Parameters[pName];
                if (!p.TypeHasBeenSet)
                {
                    string datatype      = (string)param["DATA_TYPE"];
                    bool   unsigned      = param["FLAGS"].ToString().IndexOf("UNSIGNED") != -1;
                    bool   real_as_float = procTable.Rows[0]["SQL_MODE"].ToString().IndexOf("REAL_AS_FLOAT") != -1;
                    p.MySqlDbType = MetaData.NameToType(datatype, unsigned, real_as_float, Connection);
                }

                string basePName = pName.Substring(1);
                string vName     = string.Format("@{0}{1}", hash, basePName);

                if (mode == "OUT" || mode == "INOUT")
                {
                    outSelect += vName + ", ";
                    sqlStr.Append(vName);
                    sqlStr.Append(", ");
                }
                else
                {
                    sqlStr.Append(pName);
                    sqlStr.Append(", ");
                }

                if (mode == "INOUT")
                {
                    setStr.AppendFormat(CultureInfo.InvariantCulture, "SET {0}={1};", vName, pName);
                    outSelect += vName + ", ";
                }
            }

            string sqlCmd = sqlStr.ToString().TrimEnd(' ', ',');

            outSelect = outSelect.TrimEnd(' ', ',');
            if (procTable.Rows[0]["ROUTINE_TYPE"].Equals("PROCEDURE"))
            {
                sqlCmd = String.Format("call {0} ({1})", commandText, sqlCmd);
            }
            else
            {
                if (retParm == null)
                {
                    retParm = hash + "dummy";
                }
                else
                {
                    outSelect = String.Format("@{0}", retParm);
                }
                sqlCmd = String.Format("set @{0}={1}({2})", retParm, commandText, sqlCmd);
            }

            if (setStr.Length > 0)
            {
                sqlCmd = setStr + sqlCmd;
            }

            resolvedCommandText = sqlCmd;
        }