UnifaceObjectData IUnifaceSourceCode.Extract(SqlConnection connection, UnifaceObjectId objectId)
        {
            var sourceCodeBlock = new StringBuilder();
            Dictionary <string, object> objectData;

            var libraryCondition = objectId.LibraryNameIsGlobal ?
                                   $"{LibraryField} IS NULL" : $"{LibraryField} = '{objectId.LibraryName}'";

            var command = new SqlCommand(
                $"SELECT * FROM {PrimaryTable} " +
                $"WHERE {IdField} = '{objectId.ObjectName}' AND {libraryCondition} AND {TypeFilter}", connection);

            using (var reader = command.ExecuteReader())
            {
                if (!reader.Read())
                {
                    throw new InvalidOperationException($"Could not find {objectId} in database. Check that the object identifier is correct. SQL: '{command.CommandText}'");
                }

                objectData = reader.ToDictionary();
                sourceCodeBlock.Append(reader[CodeField].ToString());
            }

            var overflowLibraryCondition = this.applyLibraryConditionToOverflowTable ? " AND " + libraryCondition : "";
            var getSegmentsCommand       = new SqlCommand($"SELECT data FROM {overflowTable} WHERE {IdField} = '{objectId.ObjectName}' {overflowLibraryCondition} ORDER BY segm", connection);

            using (var reader = getSegmentsCommand.ExecuteReader())
                UnifaceSourceCodeParser.LoadOverflowSegments(reader, sourceCodeBlock);

            return(new UnifaceObjectData(objectData, new UnifaceObjectSourceCodeBlockGroup("", sourceCodeBlock)));
        }
        private static List <UnifaceObjectSourceCodeBlockUxfield> GetCodeBlockEntityFields(SqlConnection connection, UnifaceObjectId objectId)
        {
            var codeBlockFields = new List <UnifaceObjectSourceCodeBlockUxfield>();

            var command = new SqlCommand(
                $"SELECT * FROM uxfield " +
                $"WHERE uform = '{objectId.ObjectName}'", connection);

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var codeBlockField = new UnifaceObjectSourceCodeBlockUxfield();
                    codeBlockField.Ulabel = reader["ulabel"].ToString().Trim();
                    codeBlockField.Grp    = reader["grp"].ToString().Trim();
                    codeBlockField.Ubase  = reader["ubase"].ToString().Trim();
                    codeBlockField.SourceCode.Append(reader["startmod"].ToString());
                    codeBlockFields.Add(codeBlockField);
                }
            }

            // Load the overflow data.
            foreach (var codeBlockField in codeBlockFields)
            {
                var overflowCommand = new SqlCommand(
                    $"SELECT * FROM ouxfield " +
                    $"WHERE uform = '{objectId.ObjectName}' AND ulabel = '{codeBlockField.Ulabel}' AND grp = '{codeBlockField.Grp}' AND ubase = '{codeBlockField.Ubase}' ORDER BY segm", connection);

                using (var reader = overflowCommand.ExecuteReader())
                    UnifaceSourceCodeParser.LoadOverflowSegments(reader, codeBlockField.SourceCode);
            }

            return(codeBlockFields);
        }
        private static List <UnifaceObjectSourceCodeBlockUxgroup> GetCodeBlockEntitiyGroups(SqlConnection connection, UnifaceObjectId objectId)
        {
            var codeBlockGroups = new List <UnifaceObjectSourceCodeBlockUxgroup>();

            var command = new SqlCommand(
                $"SELECT * FROM uxgroup " +
                $"WHERE uform = '{objectId.ObjectName}'", connection);

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var codeBlockGroup = new UnifaceObjectSourceCodeBlockUxgroup();
                    codeBlockGroup.Ubase  = reader["ubase"].ToString().Trim();
                    codeBlockGroup.Ulabel = reader["ulabel"].ToString().Trim();
                    codeBlockGroup.SourceCode.Append(reader["tplactual"].ToString());
                    codeBlockGroups.Add(codeBlockGroup);
                }
            }

            // Load the overflow data.
            foreach (var codeBlockGroup in codeBlockGroups)
            {
                var overflowCommand = new SqlCommand(
                    $"SELECT * FROM ouxgroup " +
                    $"WHERE uform = '{objectId.ObjectName}' AND ulabel = '{codeBlockGroup.Ulabel}' AND ubase = '{codeBlockGroup.Ubase}' ORDER BY segm", connection);

                using (var reader = overflowCommand.ExecuteReader())
                    UnifaceSourceCodeParser.LoadOverflowSegments(reader, codeBlockGroup.SourceCode);
            }

            return(codeBlockGroups);
        }
Пример #4
0
        public static void PullObject(SqlConnection connection, UnifaceObjectId objectId, DirectoryInfo codeRootDirectory)
        {
            var objectData      = objectId.Type.SourceCode.Extract(connection, objectId);
            var objectDirectory = codeRootDirectory.CombinePath(objectId.Type.Name).CombinePath(objectId.LibraryName).CombinePath(objectId.ObjectName);

            objectDirectory.Create();

            using (var codeFile = new AtomicFileWrite(objectDirectory.CombineFile($"{objectId.ObjectName}.uni")))
                using (var stream = new StreamWriter(codeFile.OpenWrite()))
                {
                    foreach (var codeBlockGroup in objectData.SourceCodeBlockGroups)
                    {
                        foreach (var codeBlock in UnifaceSourceCodeParser.GetMetaDataNodes(codeBlockGroup.SourceCode).OrderBy(s => s.Order))
                        {
                            stream.WriteLine($"\r\n;~~~~~~~~~~~~~~~~~~~~{codeBlockGroup.Name}{codeBlock.Name}~~~~~~~~~~~~~~~~~~~~");   // Special header lines that indicate the block lines.
                            stream.Write(codeBlock.Code.TransformUnifaceLineEndings());
                        }
                    }

                    codeFile.Commit();
                }

            using (var dataFile = new AtomicFileWrite(objectDirectory.CombineFile($"{objectId.ObjectName}.json")))
                using (var stream = new StreamWriter(dataFile.OpenWrite()))
                {
                    var exportData = objectData.Data
                                     .Where(a => a.Key != objectId.Type.SourceCode.IdField)
                                     .Where(a => a.Key != objectId.Type.SourceCode.CodeField)
                                     .OrderBy(a => a.Key)
                                     .ToDictionary(a => a.Key, a => TransformDataValue(a.Value));

                    stream.Write(JsonConvert.SerializeObject(exportData, Formatting.Indented));

                    dataFile.Commit();
                }
        }