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)));
        }
        public static void PullObject(SqlConnection connection, UnifaceObjectId objectId, DirectoryInfo codeRootDirectory)
        {
            var objectData      = ExtractSourceCode(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 codeBlock in GetMetaDataNodes(objectData.SourceCode).OrderBy(s => s.Order))
                    {
                        stream.WriteLine($"\r\n;~~~~~~~~~~~~~~~~~~~~{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.TableSource.IdField)
                                     .Where(a => a.Key != objectId.Type.TableSource.CodeField)
                                     .OrderBy(a => a.Key)
                                     .ToDictionary(a => a.Key, a => TransformDataValue(a.Value));

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

                    dataFile.Commit();
                }
        }
        UnifaceObjectData IUnifaceSourceCode.Extract(SqlConnection connection, UnifaceObjectId objectId)
        {
            var objectData = primarySourceTable.Extract(connection, objectId);

            var codeBlockGroupEntityFields = GetCodeBlockGroupEntityFields(connection, objectId);

            foreach (var codeBlockEntityField in codeBlockGroupEntityFields)
            {
                objectData.SourceCodeBlockGroups.Add(new UnifaceObjectSourceCodeBlockGroup(codeBlockEntityField.Uvlab + "." + codeBlockEntityField.Utlab + "." + codeBlockEntityField.Uflab, codeBlockEntityField.SourceCode));
            }

            return(objectData);
        }
        private static ObjectData ExtractSourceCode(SqlConnection connection, UnifaceObjectId objectId)
        {
            var sourceCode = new StringBuilder();
            Dictionary <string, object> objectData;

            var tableSource = objectId.Type.TableSource;

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

            var command = new SqlCommand(
                $"SELECT * FROM {tableSource.PrimaryTable} " +
                $"WHERE {tableSource.IdField} = '{objectId.ObjectName}' AND {libraryCondition} AND {tableSource.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();
                sourceCode.Append(reader[tableSource.CodeField].ToString());
            }

            var dumpSegmentsCommand = new SqlCommand($"SELECT data FROM {tableSource.OverflowTable} WHERE {tableSource.IdField} = '{objectId.ObjectName}' ORDER BY segm", connection);

            using (var reader = dumpSegmentsCommand.ExecuteReader())
            {
                while (reader.Read())
                {
                    // Remove the `Next Segment ID` 4 bytes from the end of the last added segment (not applicable for the last line).
                    if (sourceCode.Length > 0)
                    {
                        sourceCode.Remove(sourceCode.Length - 4, 4);
                    }

                    sourceCode.Append(reader["data"].ToString());
                }
            }

            return(new ObjectData(objectData, sourceCode));
        }
        UnifaceObjectData IUnifaceSourceCode.Extract(SqlConnection connection, UnifaceObjectId objectId)
        {
            var objectData = primarySourceTable.Extract(connection, objectId);

            var codeBlockEntityGroups = GetCodeBlockEntitiyGroups(connection, objectId);
            var codeBlockEntityFields = GetCodeBlockEntityFields(connection, objectId);

            // Add the groups to the object data.
            foreach (var codeBlockGroup in codeBlockEntityGroups)
            {
                objectData.SourceCodeBlockGroups.Add(new UnifaceObjectSourceCodeBlockGroup(codeBlockGroup.Ubase + "." + codeBlockGroup.Ulabel, codeBlockGroup.SourceCode));
            }

            // Add the fields to the object data.
            foreach (var codeBlockField in codeBlockEntityFields)
            {
                objectData.SourceCodeBlockGroups.Add(new UnifaceObjectSourceCodeBlockGroup(codeBlockField.Ubase + "." + codeBlockField.Grp + "." + codeBlockField.Ulabel, codeBlockField.SourceCode));
            }

            return(objectData);
        }
 public void PullObject(UnifaceObjectId objectId, DirectoryInfo codeRootDirectory)
 {
     UnifacePullObject.PullObject(_connection, objectId, codeRootDirectory);
 }
Exemple #7
0
 public UnifaceObject(UnifaceObjectId id)
 {
     Id = id;
 }
        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);
        }
        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 List <UnifaceObjectSourceCodeBlockUcfield> GetCodeBlockGroupEntityFields(SqlConnection connection, UnifaceObjectId entity)
        {
            var codeBlockGroups = new List <UnifaceObjectSourceCodeBlockUcfield>();

            var command = new SqlCommand(
                $"SELECT * FROM ucfield " +
                $"WHERE u_tlab = '{entity.ObjectName}' AND u_vlab = '{entity.LibraryName}'", connection);

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var codeBlockGroup = new UnifaceObjectSourceCodeBlockUcfield();
                    codeBlockGroup.Utlab = reader["u_tlab"].ToString().Trim();
                    codeBlockGroup.Uvlab = reader["u_vlab"].ToString().Trim();
                    codeBlockGroup.Uflab = reader["u_flab"].ToString().Trim();
                    codeBlockGroup.SourceCode.Append(reader["u_desc"].ToString());
                    codeBlockGroups.Add(codeBlockGroup);
                }
            }

            // TODO - load overflow data from oucfield. e.g.: select * admin.oucfield  u_tlab = 'CDCATTP'

            return(codeBlockGroups);
        }