Ejemplo n.º 1
0
        internal override JObject Execute(ActionDescriptor actionDescriptor)
        {
            try
            {
                JObject result = new JObject();

                MySqlConnection storeConnection = new MySqlConnection(this.connectionString);

                using (MySqlCommand command = storeConnection.CreateCommand())
                {
                    try
                    {
                        string commandText = generateCommandText(actionDescriptor);

                        if (onLog != null)
                        {
                            onLog(actionDescriptor.LogLevel, $"Executing query: {getTraceCommandText(commandText, actionDescriptor.Params)}", null);
                        }

                        command.CommandType = CommandType.Text;
                        command.CommandText = commandText;

                        if (actionDescriptor.Params != null && actionDescriptor.Params.Count() > 0)
                        {
                            setCommandParameters(command, actionDescriptor.Params);
                        }

                        command.Connection.Open();

                        if (actionDescriptor.ResultType == ResultType.Empty)
                        {
                            command.ExecuteNonQuery();
                        }
                        else if (actionDescriptor.ResultType == ResultType.Scalar)
                        {
                            var scalarResult = command.ExecuteScalar();

                            result.AddProperty("result", scalarResult);
                        }
                        else if (actionDescriptor.ResultType == ResultType.Object)
                        {
                            using (IDataReader reader = command.ExecuteReader())
                            {
                                RowData rd = getResults(reader).FirstOrDefault <RowData>();

                                if (rd != null)
                                {
                                    for (int i = 0; i < rd.Columns.Count(); i++)
                                    {
                                        result.AddProperty(rd.Columns[i], rd.Values[i]);
                                    }
                                }
                            }
                        }
                        else //ResultType.Array, ResultType.MultipleArrays
                        {
                            using (IDataReader reader = command.ExecuteReader())
                            {
                                int    resultsCount = 1;
                                string resultName   = null;
                                JArray dataArray    = null;
                                foreach (RowData rd in getResults(reader))
                                {
                                    if (rd.Type == RowType.FirstRow || rd.Type == RowType.EmptyRow)
                                    {
                                        if (resultName != null)
                                        {
                                            result.Add(resultName, (JToken)dataArray);
                                        }

                                        //initialize new array with name
                                        dataArray = new JArray();
                                        if (actionDescriptor.ResultType == ResultType.MultipleArrays)
                                        {
                                            if (actionDescriptor.ResultNames != null && actionDescriptor.ResultNames.Count() >= resultsCount)
                                            {
                                                resultName = actionDescriptor.ResultNames.ElementAt(resultsCount - 1);
                                            }
                                            else
                                            {
                                                resultName = $"result{resultsCount}";
                                            }
                                        }
                                        else
                                        {
                                            resultName = "result";
                                        }

                                        resultsCount++;
                                    }

                                    if (rd.Type != RowType.EmptyRow)
                                    {
                                        JObject row = new JObject();
                                        for (int i = 0; i < rd.Columns.Count(); i++)
                                        {
                                            row.AddProperty(rd.Columns[i], rd.Values[i]);
                                        }

                                        dataArray.Add(row);
                                    }
                                }

                                if (resultName != null)
                                {
                                    result.Add(resultName, (JToken)dataArray);
                                }
                            }
                        }
                    }
                    finally
                    {
                        if (command.Connection.State != ConnectionState.Closed)
                        {
                            command.Connection.Close();
                        }
                    }
                }

                return(result);
            }
            catch (Exception exp)
            {
                if (onLog != null)
                {
                    onLog(LogLevel.Error, "Action Execution Failed!", exp);
                }

                throw;
            }
        }