Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            int exitCode = 0;

            try
            {
                if (args == null || args.Length == 0)
                {
                    Console.Write(usage);
                }
                else
                {
                    var a = new Arguments(args);

                    if (!string.IsNullOrWhiteSpace(a.OutputFile))
                    {
                        File.WriteAllText(a.OutputFile, "");
                    }

                    //ExecuteRequest request = LoadRequest(a);
                    PowerQueryCommand powerQueryCommand = LoadCommand(a);

                    PowerQueryResponse powerQueryResponse = powerQueryService.Execute(powerQueryCommand);

                    OutputResponse(a, powerQueryResponse);
                }
                exitCode = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                exitCode = 1;
            }
            Environment.Exit(exitCode);
        }
Ejemplo n.º 2
0
 private void DisplayResult(PowerQueryResponse result)
 {
     if (result == null)
     {
         MessageBox.Show("Result is null.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else if (result.ExceptionMessage != null)
     {
         MessageBox.Show(result.ExceptionMessage, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     else
     {
         GridResult.DataSource = result.DataTable;
     }
 }
Ejemplo n.º 3
0
        private static void OutputResponse(Arguments a, PowerQueryResponse powerQueryResponse)
        {
            if (powerQueryResponse.ExceptionMessage != null)
            {
                throw new Exception($"{powerQueryResponse.ExceptionMessage}");
            }

            if (string.IsNullOrWhiteSpace(a.OutputFile))
            {
                switch (a.OutputFlags)
                {
                case ExecuteOutputFlags.Csv:
                    Console.WriteLine(powerQueryResponse.Csv);
                    break;

                case ExecuteOutputFlags.Html:
                    Console.WriteLine(powerQueryResponse.Html);
                    break;

                case ExecuteOutputFlags.Json:
                    Console.WriteLine(powerQueryResponse.Json);
                    break;

                case ExecuteOutputFlags.Xml:
                    Console.WriteLine(powerQueryResponse.Xml);
                    break;

                default:
                    break;
                }
            }

            if (powerQueryResponse.DataTable != null && a.OutputToWindow)
            {
                new WindowGrid(powerQueryResponse.DataTable).ShowDialog();
            }
        }
Ejemplo n.º 4
0
        public PowerQueryResponse Execute(PowerQueryCommand powerQueryCommand)
        {
            Command            command            = null;
            PowerQueryResponse powerQueryResponse = new PowerQueryResponse();
            CommandCredentials commandCredentials = new CommandCredentials();
            string             mashup;

            try
            {
                if (powerQueryCommand.Credentials != null && powerQueryCommand.Credentials.Count > 0)
                {
                    foreach (Credential credential in powerQueryCommand.Credentials)
                    {
                        if (credential is CredentialFile credentialFile)
                        {
                            commandCredentials.SetCredentialFile(credentialFile.Path);
                        }
                        else if (credential is CredentialFolder credentialFolder)
                        {
                            commandCredentials.SetCredentialFolder(credentialFolder.Path);
                        }
                        else if (credential is CredentialWeb credentialWeb)
                        {
                            commandCredentials.SetCredentialWeb(credentialWeb.Url);
                        }
                        else if (credential is CredentialSQL credentialSQL)
                        {
                            commandCredentials.SetCredentialSQL(credentialSQL.SQL, credentialSQL.Username, credentialSQL.Password);
                        }
                        else if (credential is CredentialOData credentialOData)
                        {
                            commandCredentials.SetCredentialOData(((CredentialOData)credential).Url, credentialOData.Username, credentialOData.Password);
                        }
                        else
                        {
                            throw new NotImplementedException("This Credential kind is not supported for now.");
                        }
                    }
                }

                command = new Command(commandCredentials);

                DataTable dataTable = null;

                if (powerQueryCommand.Queries != null && powerQueryCommand.Queries.Count > 0)
                {
                    if (powerQueryCommand.Queries.Count == 1)
                    {
                        powerQueryCommand.QueryName = powerQueryCommand.Queries[0].Name;
                    }

                    mashup = "section Section1;\n\r";

                    foreach (Query q in powerQueryCommand.Queries)
                    {
                        string name;
                        if (q.Name.Contains(" "))
                        {
                            name = string.Format("#\"{0}\"", q.Name);
                        }
                        else
                        {
                            name = q.Name;
                        }
                        mashup += string.Format("\n\rshared {0} = {1};", name, q.Formula);
                    }
                }
                else
                {
                    mashup = powerQueryCommand.Mashup;
                }

                dataTable = command.Execute(powerQueryCommand.QueryName, mashup);

                if (powerQueryCommand.ExecuteOutputFlags == 0 && powerQueryCommand.SqlConnectionString != null)
                {
                    powerQueryCommand.ExecuteOutputFlags = ExecuteOutputFlags.Sql;
                }
                else if (powerQueryCommand.ExecuteOutputFlags == 0)
                {
                    powerQueryCommand.ExecuteOutputFlags = ExecuteOutputFlags.DataTable | ExecuteOutputFlags.Xml;
                }

                if (isRemote)
                {
                    powerQueryResponse.DataTableFile = $"{powerQueryCommand.TempPath}{Guid.NewGuid()}.xml";
                    File.WriteAllText(powerQueryResponse.DataTableFile, dataTable.ToXML());
                }
                else
                {
                    if (powerQueryCommand.ExecuteOutputFlags.HasFlag(ExecuteOutputFlags.DataTable))
                    {
                        powerQueryResponse.DataTable = dataTable;
                    }
                }

                if (powerQueryCommand.ExecuteOutputFlags.HasFlag(ExecuteOutputFlags.Csv) || !string.IsNullOrWhiteSpace(powerQueryCommand.CsvFileName))
                {
                    powerQueryResponse.Csv = dataTable.ToDelimitedFile(',', true);

                    if (!string.IsNullOrWhiteSpace(powerQueryCommand.CsvFileName))
                    {
                        File.WriteAllText(powerQueryCommand.CsvFileName, powerQueryResponse.Csv);
                    }

                    if (isRemote)
                    {
                        powerQueryResponse.Csv = null;
                    }
                }

                if (powerQueryCommand.ExecuteOutputFlags.HasFlag(ExecuteOutputFlags.Html) || !string.IsNullOrWhiteSpace(powerQueryCommand.HtmlFileName))
                {
                    powerQueryResponse.Html = dataTable.ToHTML();

                    if (!string.IsNullOrWhiteSpace(powerQueryCommand.HtmlFileName))
                    {
                        File.WriteAllText(powerQueryCommand.HtmlFileName, powerQueryResponse.Html);
                    }

                    if (isRemote)
                    {
                        powerQueryResponse.Html = null;
                    }
                }

                if (powerQueryCommand.ExecuteOutputFlags.HasFlag(ExecuteOutputFlags.Json) || !string.IsNullOrWhiteSpace(powerQueryCommand.JsonFileName))
                {
                    powerQueryResponse.Json = dataTable.ToContentJSON();

                    if (!string.IsNullOrWhiteSpace(powerQueryCommand.JsonFileName))
                    {
                        File.WriteAllText(powerQueryCommand.JsonFileName, powerQueryResponse.Json);
                    }

                    if (isRemote)
                    {
                        powerQueryResponse.Json = null;
                    }
                }

                if (powerQueryCommand.ExecuteOutputFlags.HasFlag(ExecuteOutputFlags.Sql))
                {
                    if (powerQueryCommand.SqlConnectionString == null)
                    {
                        throw new InvalidOperationException("Cannot output to SQL. SqlConnectionString must be defined.");
                    }

                    dataTable.TableName = powerQueryCommand.SqlTableName;

                    OutputToSQL(powerQueryCommand, dataTable);
                }

                if (powerQueryCommand.ExecuteOutputFlags.HasFlag(ExecuteOutputFlags.Xml) || !string.IsNullOrWhiteSpace(powerQueryCommand.XmlFileName))
                {
                    powerQueryResponse.Xml = dataTable.ToContentXML();

                    if (!string.IsNullOrWhiteSpace(powerQueryCommand.XmlFileName))
                    {
                        File.WriteAllText(powerQueryCommand.XmlFileName, powerQueryResponse.Xml);
                    }

                    if (isRemote)
                    {
                        powerQueryResponse.Xml = null;
                    }
                }
            }
            catch (Exception ex)
            {
                Program.Log.WriteEntry(ex.ToString(), EventLogEntryType.Error);
                if (powerQueryCommand.Queries != null && powerQueryCommand.Queries.Count > 0)
                {
                    Program.Log.WriteEntry(powerQueryCommand.Queries.ToXML(), EventLogEntryType.Error);
                }
                if (powerQueryCommand.Mashup != null)
                {
                    Program.Log.WriteEntry(powerQueryCommand.Mashup, EventLogEntryType.Error);
                }
                if (powerQueryCommand.Credentials != null && powerQueryCommand.Credentials.Count > 0)
                {
                    Program.Log.WriteEntry(powerQueryCommand.Credentials.ToXML(), EventLogEntryType.Error);
                }
                powerQueryResponse.ExceptionMessage = ex.Message;
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
            }

            return(powerQueryResponse);
        }