예제 #1
0
파일: Program.cs 프로젝트: helgihaf/Alpha
        private static TargetSystem GetTargetSystem(DataSchema dataSchema, string targetName)
        {
            TargetSystem targetSystem = dataSchema.TargetSystems[targetName];

            if (targetSystem == null)
            {
                WriteError("Unknown script target " + targetName);
            }

            return targetSystem;
        }
예제 #2
0
파일: Program.cs 프로젝트: helgihaf/Alpha
        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;
        }
예제 #3
0
        private static Parsing.DataSchema SchemaToParsedSchema(DataSchema dataSchema)
        {
            Parsing.DataSchema parsedSchema = new Parsing.DataSchema();

            // Column types
            if (dataSchema.ColumnTypes.Count > 0)
            {
                parsedSchema.ColumnTypes = new Parsing.DataSchemaColumnType[dataSchema.ColumnTypes.Count];
                int columnTypeIndex = 0;
                foreach (var columnType in dataSchema.ColumnTypes)
                {
                    Parsing.DataSchemaColumnType parsedColumnType = new Parsing.DataSchemaColumnType();
                    parsedColumnType.name = columnType.Name;

                    if (columnType.BaseType != null)
                        parsedColumnType.baseType = columnType.BaseType.Name;

                    if (columnType.CanBeNull)
                    {
                        parsedColumnType.canBeNullSpecified = true;
                        parsedColumnType.canBeNull = columnType.CanBeNull;
                    }

                    if (columnType.IsDbGenerated)
                    {
                        parsedColumnType.isDbGeneratedSpecified = true;
                        parsedColumnType.isDbGenerated = columnType.IsDbGenerated;
                    }

                    if (columnType.MaxLength.HasValue)
                    {
                        parsedColumnType.maxLengthSpecified = true;
                        parsedColumnType.maxLength = columnType.MaxLength.Value;
                    }

                    parsedColumnType.enumType = columnType.EnumTypeName;

                    if (columnType.Precision.HasValue)
                    {
                        parsedColumnType.precisionSpecified = true;
                        parsedColumnType.precision = Convert.ToByte(columnType.Precision.Value);
                    }

                    if (columnType.Scale.HasValue)
                    {
                        parsedColumnType.scaleSpecified = true;
                        parsedColumnType.scale = Convert.ToByte(columnType.Scale.Value);
                    }

                    parsedColumnType.Target = new Parsing.DataSchemaColumnTypeTarget[columnType.Targets.Count];
                    int targetIndex = 0;
                    foreach (var target in columnType.Targets)
                    {
                        Parsing.DataSchemaColumnTypeTarget parsedTarget = new Parsing.DataSchemaColumnTypeTarget
                        {
                            name = target.TargetSystem.Name,
                            dataType = target.DataType,
                            dataTypeWhenReferenced = target.DataTypeWhenReferenced
                        };
                        parsedColumnType.Target[targetIndex++] = parsedTarget;
                    }

                    parsedSchema.ColumnTypes[columnTypeIndex++] = parsedColumnType;
                }
            }

            // Tables
            if (dataSchema.Tables.Count > 0)
            {
                parsedSchema.Tables = new Parsing.DataSchemaTable[dataSchema.Tables.Count];
                int tableIndex = 0;
                foreach (var table in dataSchema.Tables)
                {
                    Parsing.DataSchemaTable parsedTable = new Parsing.DataSchemaTable
                    {
                        name = table.Name,
                    };

                    // Columns
                    if (table.Columns.Count > 0)
                    {
                        parsedTable.Columns = new Parsing.DataSchemaTableColumn[table.Columns.Count];
                        for (int columnIndex = 0; columnIndex < table.Columns.Count; columnIndex++)
                        {
                            Column column = table.Columns[columnIndex];
                            Parsing.DataSchemaTableColumn parsedColumn = new Parsing.DataSchemaTableColumn
                            {
                                name = column.Name,
                            };

                            if (column.CanBeNull)
                            {
                                parsedColumn.canBeNullSpecified = true;
                                parsedColumn.canBeNull = column.CanBeNull;
                            }

                            if (column.InPrimaryKey)
                            {
                                parsedColumn.inPrimaryKeySpecified = true;
                                parsedColumn.inPrimaryKey = column.InPrimaryKey;
                            }

                            // TODO: Following code should generate ForeignKeys elements later
                            //if (column.ReferencesTable != null)
                            //{
                            //    parsedColumn.references = column.ReferencesTable.Name + "." + column.ReferencesColumn.Name;
                            //}
                            //else
                            //{
                            //    parsedColumn.columnType = column.ColumnType.Name;
                            //}

                            //if (column.AssociationProperty != null)
                            //{
                            //    var parsedAssociationCode = new Parsing.DataSchemaTableColumnAssociationCode();
                            //    if (column.AssociationProperty.Child != null)
                            //    {
                            //        NativeAssociationEndPointToParsed(column.AssociationProperty.Child, parsedAssociationCode.Child);
                            //    }
                            //    else
                            //    {
                            //        parsedAssociationCode.Child = null;
                            //    }

                            //    NativeAssociationEndPointToParsed(column.AssociationProperty.Parent, parsedAssociationCode.Parent);
                            //}

                            parsedTable.Columns[columnIndex] = parsedColumn;
                        }
                    }

                    // Indices
                    if (table.Indices.Count > 0)
                    {
                        parsedTable.Indices = new Parsing.DataSchemaTableIndex[table.Indices.Count];
                        for (int indexIndex = 0; indexIndex < table.Indices.Count; indexIndex++)
                        {
                            Index index = table.Indices[indexIndex];
                            Parsing.DataSchemaTableIndex parsedIndex = new Parsing.DataSchemaTableIndex();
                            if (index.Name != null)
                            {
                                parsedIndex.name = index.Name;
                            }

                            if (index.IsUnique)
                            {
                                parsedIndex.uniqueSpecified = true;
                                parsedIndex.unique = index.IsUnique;
                            }

                            parsedIndex.Column = new Parsing.DataSchemaTableIndexColumn[index.Columns.Count];
                            for (int columnIndex = 0; columnIndex < index.Columns.Count; columnIndex++)
                            {
                                var column = index.Columns[columnIndex];
                                Parsing.DataSchemaTableIndexColumn parsedIndexColumn = new Parsing.DataSchemaTableIndexColumn
                                {
                                    name = column.Name
                                };

                                parsedIndex.Column[columnIndex] = parsedIndexColumn;
                            }

                            parsedTable.Indices[indexIndex] = parsedIndex;
                        }
                    }

                    parsedSchema.Tables[tableIndex++] = parsedTable;

                    //if (table.ForeignKeys.Count > 0)
                    //{
                    //    parsedTable.ForeignKeys = new Parsing.DataSchemaTableForeignKey[table.ForeignKeys.Count];
                    //    for (int fkIndex = 0; fkIndex < table.ForeignKeys.Count; fkIndex++)
                    //    {
                    //        ForeignKey foreignKey = table.ForeignKeys[fkIndex];
                    //        Parsing.DataSchemaTableForeignKey parsedForeignKey = new Parsing.DataSchemaTableForeignKey();
                    //        if (foreignKey != null)
                    //        {
                    //            parsedForeignKey.name = foreignKey.Name;
                    //        }
                    //        parsedForeignKey.  !!! ÉG ER HÉR
                    //}
                }
            }

            return parsedSchema;
        }