Example #1
0
        static int Main(string[] args)
        {
            ShowVersion();

            CommandLineProcessor clp = InitializeCommandLine();
            try
            {
                clp.ProcessArguments(args);
            }
            catch (CommandLineProcessorException ex)
            {
                WriteError(ex.Message);
                return returnCodeArguments;
            }

            if (clp.ShowUsageRequested)
            {
                clp.ShowUsage(description);
                return returnCodeArguments;
            }

            DataSchema dataSchema = new DataSchema();
            dataSchema.Name = nameParam.StringValue;
            VerificationContext context = new VerificationContext();

            foreach (var filePath in inputFilesParam.StringValues)
            {
                var inputFilePath = Path.GetFullPath(filePath);

                if (!File.Exists(inputFilePath))
                {
                    WriteError("Input file \"" + inputFilePath + "\" was not found.");
                    return returnCodeArguments;
                }

                using (StreamReader reader = new StreamReader(inputFilePath))
                {
                    dataSchema.LoadDataSchemaFile(reader, context);
                }
            }

            dataSchema.Verify(context);
            var entries = context.Entries;
            foreach (var entry in entries)
            {
                Console.WriteLine(entry.ToString());
            }

            if (entries.Count != 0)
            {
                WriteWarning("No script file written.");
                return returnCodeDataSchema;
            }

            bool scriptFileWritten = false;
            bool codeFileWritten = false;

            //
            // Write SQL script
            //

            if (scriptTargetParam.StringValue != null)
            {
                if (string.IsNullOrWhiteSpace(scriptOutParam.StringValue))
                {
                    WriteError("Invalid or missing value for " + scriptOutParam.Name);
                    return returnCodeArguments;
                }
                string outputFilePath = Path.GetFullPath(scriptOutParam.StringValue);
                FileInfo fileInfo = new FileInfo(outputFilePath);
                if (!fileInfo.Exists || !fileInfo.IsReadOnly)
                {
                    TargetSystem targetSystem = GetTargetSystem(dataSchema, scriptTargetParam.StringValue);
                    if (targetSystem == null)
                        return returnCodeDataSchema;        // Error msg already shown

                    targetSystem = dataSchema.TargetSystems[scriptTargetParam.StringValue];
                    if (targetSystem == null)
                    {
                        WriteError("Unknown script target " + scriptTargetParam.StringValue);
                        return returnCodeDataSchema;
                    }

                    if (!fileInfo.Directory.Exists)
                    {
                        WriteError("Output directory '" + fileInfo.Directory + "' does not exist");
                        return returnCodeArguments;
                    }

                    Console.WriteLine("Generating script...");
                    //SqlServerScriptGenerator gen = new SqlServerScriptGenerator();
                    var gen = new OracleScriptGenerator();
                    gen.TargetSystem = targetSystem;
                    gen.DataSchema = dataSchema;

                    //if (scriptDocParam.SwitchValue)
                    //{
                    //    gen.ScriptDocumentGenerator = new LSRetailScriptDocumentGenerator();
                    //}
                    using (FileStream stream = new FileStream(outputFilePath, FileMode.Create))
                    {
                        gen.Generate(stream);
                    }
                    scriptFileWritten = true;
                    Console.WriteLine("Wrote script file \"" + outputFilePath + "\"");
                }
                else
                {
                    WriteError("Output file is read only (" + outputFilePath + ")");
                }
            }

            //
            // Write code
            //

            if (codeTargetParam.StringValue != null)
            {
                if (string.IsNullOrWhiteSpace(codeOutParam.StringValue))
                {
                    WriteError("Invalid or missing value for " + codeOutParam.Name);
                    return returnCodeArguments;
                }
                string outputFilePath = Path.GetFullPath(codeOutParam.StringValue);
                FileInfo fileInfo = new FileInfo(outputFilePath);
                if (!fileInfo.Exists || !fileInfo.IsReadOnly)
                {
                    TargetSystem targetSystem = GetTargetSystem(dataSchema, codeTargetParam.StringValue);
                    if (targetSystem == null)
                        return returnCodeDataSchema;    // Error msg already shown

                    TargetSystem databaseTargetSystem = GetTargetSystem(dataSchema, scriptTargetParam.StringValue);
                    if (databaseTargetSystem == null)
                        return returnCodeDataSchema;

                    Console.WriteLine("Generating dbml...");
                    LinqDbmlGenerator linqGen = new LinqDbmlGenerator();
                    linqGen.DataSchema = dataSchema;
                    linqGen.TargetSystem = targetSystem;
                    linqGen.DatabaseTargetSystem = databaseTargetSystem;
                    linqGen.DatabaseColumnTypeMapper = new SqlServerColumnTypeMapper();
                    using (FileStream stream = new FileStream(outputFilePath, FileMode.Create))
                    {
                        linqGen.Generate(stream);
                    }
                    codeFileWritten = true;
                    Console.WriteLine("Wrote code file \"" + outputFilePath + "\"");
                }
                else
                {
                    WriteError("Output file is read only (" + outputFilePath + ")");
                }
            }

            //
            // Write documentation
            //

            if (docOutParam.StringValue != null)
            {
                if (string.IsNullOrWhiteSpace(docOutParam.StringValue))
                {
                    WriteError("Invalid value for " + docOutParam.Name);
                    return returnCodeArguments;
                }
                string outputFilePath = Path.GetFullPath(docOutParam.StringValue);
                FileInfo fileInfo = new FileInfo(outputFilePath);
                if (!fileInfo.Exists || !fileInfo.IsReadOnly)
                {
                    TargetSystem databaseTargetSystem = GetTargetSystem(dataSchema, scriptTargetParam.StringValue);
                    if (databaseTargetSystem == null)
                        return returnCodeDataSchema;

                    Console.WriteLine("Generating documentation...");
                    DocumentGenerator docGen = new DocumentGenerator();
                    docGen.DataSchema = dataSchema;
                    docGen.DatabaseTargetSystem = databaseTargetSystem;
                    docGen.DatabaseColumnTypeMapper = new SqlServerColumnTypeMapper();
                    if (!string.IsNullOrWhiteSpace(tableSchemaNameParam.StringValue))
                    {
                        docGen.TableSchemaName = tableSchemaNameParam.StringValue;
                    }
                    if (docCssFileParam.StringValue != null)
                    {
                        docGen.CssFile = docCssFileParam.StringValue;
                    }
                    using (FileStream stream = new FileStream(outputFilePath, FileMode.Create))
                    {
                        docGen.Generate(stream);
                    }
                    Console.WriteLine("Wrote document file \"" + outputFilePath + "\"");
                }
                else
                {
                    WriteError("Output file is read only (" + outputFilePath + ")");
                }
            }

            if (scriptFileWritten && !codeFileWritten)
                return returnCodeNoCodeFile;
            else if (!scriptFileWritten && codeFileWritten)
                return returnCodeNoScriptFile;
            else if (!scriptFileWritten && !codeFileWritten)
                return returnCodeNoFile;
            else
                return returnCodeOk;
        }
Example #2
0
        private void GenerateLinqToSql(DataSchema dataSchema)
        {
            TargetSystem targetSystemLinq = dataSchema.TargetSystems[TargetSystemNameForLinq];
            TargetSystem targetSystemScript = dataSchema.TargetSystems[TargetSystemNameForScript];

            LinqDbmlGenerator linqGen = new LinqDbmlGenerator();
            linqGen.DataSchema = dataSchema;
            linqGen.TargetSystem = targetSystemLinq;
            linqGen.DatabaseTargetSystem = targetSystemScript;
            linqGen.TableSchemaName = "dbo";
            linqGen.DatabaseColumnTypeMapper = new SqlServerColumnTypeMapper();
            using (FileStream stream = new FileStream(LinqToSqlFilePath, FileMode.Create))
            {
                linqGen.Generate(stream);
            }
        }