Exemplo n.º 1
0
        private Config(string[] args)
        {
            Provider? p = null;
            new OptionSet { { "providerName=", "", s => p = new Provider(s) } }.Parse(args);

            var provider = p ?? Provider.Default;

            string output = null;
            bool scalar = false, nonquery = false, help = false;
            var commandLine = new CommandLine();
            var connectionString = new ConnectionString();

            Action<CommandLineParam, string> setCommandLineValue = (cl, s) => commandLine[cl] = Value.From(s.Trim('"'));

            Func<string, string, bool> onUnknownOption = (name, value) =>
            {
                connectionString[new ConnectionStringParam(name.Trim('"'))] = Value.From(value.Trim('"'));
                return true;
            };

            var options = new MyOptionSet(
                CommandLineToConnectionString(provider), 
                setCommandLineValue,
                onUnknownOption)
            {
                {
                    "output=",
                    "Path to output File. If none specified, output is written to the console.",
                    s => output = s
                },
                {
                    "scalar",
                    "Interpret the query as a scalar result, i.e. a single value (of any type)",
                    s => scalar = true
                },
                {
                    "nonquery",
                    "Run the query as a 'non-select' statement, i.e. INSERT, UPDATE, DELETE or a DDL statement. " +
                    "Outputs the number of affected records of the last statement.",
                    s => nonquery = true
                },
                {
                    "providerName=",
                    "The db provider name.",
                    s => provider = new Provider(s)
                },
                {
                    "help",
                    "Print this text.",
                    s => help = true
                }
            };


            var remaining = options.Parse(args);

            Output = output;
            Scalar = scalar;
            NonQuery = nonquery;
            Help = help;
            _provider = provider;
            _optionSet = options;

            if (remaining.Any())
            {
                var queryOrFileName = remaining.First();
                Query = File.Exists(queryOrFileName) ? File.ReadAllText(queryOrFileName) : queryOrFileName;
            }

            if (commandLine.Any() || connectionString.Any())
            {
                ConnectionString = GetConnectionString(_provider, commandLine, connectionString);
            }
            else
            {
                var settings = ConnectionStrings["Default"];
                if (settings == null) 
                    throw new ConnectionConfigException("No connection configuration found. Either provide command line parameters, or add a .config file with a connection string named 'Default'");
                ConnectionString = settings.ConnectionString;
                _provider = new Provider(settings.ProviderName);
            }
        }