Beispiel #1
0
        public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
        {
            var criticalError = false;

            try
            {
                using (var command = transaction.Connection.CreateCommand())
                {
                    command.CommandText = Entity.Content;
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                }

                Logger.Log("created {0}", Entity);
            }
            catch (Exception ex)
            {
                criticalError = true;
                Logger.Log("failed while trying to create {0}", Entity);
                Logger.Log(ex);
                transaction.Rollback();
            }
            finally
            {
                if (!criticalError)
                {
                    // command to update memory data must be performed only after sql commands
                    commandAdder(new LambdaCommand((_, proj, _a) => UpdateProjectInfo(proj)));
                }
            }
        }
Beispiel #2
0
        public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
        {
            var criticalError = false;
            try
            {

                using (var command = transaction.Connection.CreateCommand())
                {
                    command.CommandText = Entity.Content;
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                }

                Logger.Log("created {0}", Entity);
            }
            catch (Exception ex)
            {
                criticalError = true;
                Logger.Log("failed while trying to create {0}", Entity);
                Logger.Log(ex);
                transaction.Rollback();
            }
            finally
            {
                if (!criticalError)
                {
                    // command to update memory data must be performed only after sql commands
                    commandAdder(new LambdaCommand((_, proj, _a) => UpdateProjectInfo(proj)));
                }
            }
        }
Beispiel #3
0
        internal static void Anaylyze(SqlProjectInfo projectInfo)
        {
            var undefinedVariables = projectInfo.FileToEntityMapping.Values
                                     .Select(entity => new
            {
                entity,
                matches = VariableRegex
                          .Matches(entity.Content)
                          .OfType <Match>()
                          .Select(m => m.Groups[1].Value)
            })
                                     .SelectMany(e => e.matches.Select(match => new { e.entity, match }))
                                     .GroupBy(e => e.match)
                                     .Select(g => new
            {
                variable = g.Key,
                entities = g.Take(5).Select(e => e.entity),
                total    = g.Count()
            })
                                     .ToList();

            if (!undefinedVariables.Any())
            {
                return;
            }

            Console.WriteLine();

            foreach (var varInfo in undefinedVariables)
            {
                Formatter.WriteLine(
                    "^[Red]warning^[Gray]: variable ^[Yellow]{0} ^[Gray]is undefined and is used in ^[White]{1} ^[Gray]files",
                    varInfo.variable,
                    varInfo.total);

                Console.WriteLine();

                foreach (var entity in varInfo.entities)
                {
                    var lines = entity.Content
                                .Replace("\r\n", "\n")
                                .Split('\n');

                    var badLine = lines
                                  .Select((line, index) => new { text = line, index })
                                  .First(_ => VariableRegex.IsMatch(_.text));

                    Formatter.WriteLine("\tin file ^[DarkYellow]{0}^[Gray] (entity {1}) at line {2}:",
                                        entity.Path,
                                        entity.Name,
                                        badLine.index + 1);

                    Formatter.WriteLine(string.Format("\t\t{0}", GetHighligtedVariable(badLine.text.Trim())));

                    Console.WriteLine();
                }
            }
        }
        internal static void Anaylyze(SqlProjectInfo projectInfo)
        {
            var undefinedVariables = projectInfo.FileToEntityMapping.Values
                .Select(entity => new
                {
                    entity,
                    matches = VariableRegex
                    .Matches(entity.Content)
                    .OfType<Match>()
                    .Select(m => m.Groups[1].Value)
                })
                .SelectMany(e => e.matches.Select(match => new { e.entity, match }))
                .GroupBy(e => e.match)
                .Select(g => new
                {
                    variable = g.Key,
                    entities = g.Take(5).Select(e => e.entity),
                    total = g.Count()
                })
                .ToList();

            if (!undefinedVariables.Any())
                return;

            Console.WriteLine();

            foreach (var varInfo in undefinedVariables)
            {
                Formatter.WriteLine(
                    "^[Red]warning^[Gray]: variable ^[Yellow]{0} ^[Gray]is undefined and is used in ^[White]{1} ^[Gray]files",
                    varInfo.variable,
                    varInfo.total);

                Console.WriteLine();

                foreach (var entity in varInfo.entities)
                {
                    var lines = entity.Content
                        .Replace("\r\n", "\n")
                        .Split('\n');

                    var badLine = lines
                        .Select((line, index) => new { text = line, index })
                        .First(_ => VariableRegex.IsMatch(_.text));

                    Formatter.WriteLine("\tin file ^[DarkYellow]{0}^[Gray] (entity {1}) at line {2}:",
                        entity.Path,
                        entity.Name,
                        badLine.index + 1);

                    Formatter.WriteLine(string.Format("\t\t{0}", GetHighligtedVariable(badLine.text.Trim())));

                    Console.WriteLine();
                }
            }
        }
Beispiel #5
0
 public static SqlProjectInfo Create(Settings settings)
 {
     using (Profiling.Profile())
     {
         var project = new SqlProjectInfo();
         project.FileToEntityMapping = Profiling.Profile("GetFileToEntityMapping", () => GetFileToEntityMapping(settings));
         project.DependentEntities   = Profiling.Profile("GetDependentEntities", () => GetDependentEntities(project.FileToEntityMapping));
         //project.VerifyNoCycles();
         //project.PrintCommonWords();
         return(project);
     }
 }
Beispiel #6
0
 public static SqlProjectInfo Create(Settings settings)
 {
     using (Profiling.Profile())
     {
         var project = new SqlProjectInfo();
         project.FileToEntityMapping = Profiling.Profile("GetFileToEntityMapping", () => GetFileToEntityMapping(settings));
         project.DependentEntities = Profiling.Profile("GetDependentEntities", () => GetDependentEntities(project.FileToEntityMapping));
         //project.VerifyNoCycles();
         //project.PrintCommonWords();
         return project;
     }
 }
Beispiel #7
0
        public void Prepare()
        {
            Logger.Log("started analyzing project");

            var watch = Stopwatch.StartNew();

            project = SqlProjectInfo.Create(settings);

            UndefinedVariableAnalyzer.Anaylyze(project);

            Logger.Log("finished analyzing project for {0}", watch.Elapsed);
            Console.WriteLine();
            Console.WriteLine();
        }
Beispiel #8
0
        private void UpdateProjectInfo(SqlProjectInfo project)
        {
            project.FileToEntityMapping[Entity.Path] = Entity;

            foreach (var entityName in project.FileToEntityMapping.Values.Select(e => e.Name))
            {
                if (Entity.Content.Contains(entityName) && Entity.Name != entityName)
                {
                    var dependants = project.DependentEntities.TryGet(entityName) ?? new List <SqlEntity>();

                    if (!dependants.Contains(Entity))
                    {
                        dependants.Add(Entity);
                    }

                    project.DependentEntities[entityName] = dependants;
                }
            }
        }
Beispiel #9
0
        private void UpdateProjectInfo(SqlProjectInfo project)
        {
            project.FileToEntityMapping[Entity.Path] = Entity;

            foreach (var entityName in project.FileToEntityMapping.Values.Select(e => e.Name))
            {
                if (Entity.Content.Contains(entityName) && Entity.Name != entityName)
                {
                    var dependants = project.DependentEntities.TryGet(entityName) ?? new List<SqlEntity>();

                    if (!dependants.Contains(Entity))
                    {
                        dependants.Add(Entity);
                    }

                    project.DependentEntities[entityName] = dependants;
                }
            }
        }
Beispiel #10
0
        public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
        {
            var criticalError = false;
            try
            {
                using (var command = transaction.Connection.CreateCommand())
                {
                    command.CommandText = "drop " + Entity.Type.ToString().ToLower() + " dbo." + Entity.Name;
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                }

                Logger.Log("dropped {0}", Entity);

            }
            catch (Exception ex)
            {
                if (ex.Message.Contains(" it does not exist "))
                {
                    // ignore
                }
                else
                {
                    criticalError = true;
                    Logger.Log(ex);
                }
            }
            finally
            {
                if (!criticalError)
                {
                    // command to update memory data must be performed only after sql commands
                    commandAdder(new LambdaCommand((_, proj, _1) => UpdateProjectInfo(proj)));
                }
            }
        }
Beispiel #11
0
        private void UpdateProjectInfo(SqlProjectInfo project)
        {
            project.FileToEntityMapping.Remove(Entity.Path);

            foreach (var pair in project.DependentEntities.ToList())
            {
                var coll = pair.Value;

                if (coll.Contains(Entity))
                {
                    coll.Remove(Entity);
                }

                if (pair.Key == Entity.Name)
                {
                    if (coll.Any())
                    {
                        Logger.Log("impossible");
                    }

                    project.DependentEntities.Remove(pair.Key);
                }
            }
        }
Beispiel #12
0
        public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
        {
            var criticalError = false;

            try
            {
                using (var command = transaction.Connection.CreateCommand())
                {
                    command.CommandText = "drop " + Entity.Type.ToString().ToLower() + " dbo." + Entity.Name;
                    command.Transaction = transaction;
                    command.ExecuteNonQuery();
                }

                Logger.Log("dropped {0}", Entity);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains(" it does not exist "))
                {
                    // ignore
                }
                else
                {
                    criticalError = true;
                    Logger.Log(ex);
                }
            }
            finally
            {
                if (!criticalError)
                {
                    // command to update memory data must be performed only after sql commands
                    commandAdder(new LambdaCommand((_, proj, _1) => UpdateProjectInfo(proj)));
                }
            }
        }
Beispiel #13
0
        private void UpdateProjectInfo(SqlProjectInfo project)
        {
            project.FileToEntityMapping.Remove(Entity.Path);

            foreach (var pair in project.DependentEntities.ToList())
            {
                var coll = pair.Value;

                if (coll.Contains(Entity))
                {
                    coll.Remove(Entity);
                }

                if (pair.Key == Entity.Name)
                {
                    if (coll.Any())
                    {
                        Logger.Log("impossible");
                    }

                    project.DependentEntities.Remove(pair.Key);
                }
            }
        }
Beispiel #14
0
        public void Prepare()
        {
            Logger.Log("started analyzing project");

            var watch = Stopwatch.StartNew();
            project = SqlProjectInfo.Create(settings);

            UndefinedVariableAnalyzer.Anaylyze(project);

            Logger.Log("finished analyzing project for {0}", watch.Elapsed);
            Console.WriteLine();
            Console.WriteLine();
        }
Beispiel #15
0
        public void HandleInternal(string oldPath, string path)
        {
            var watch = Stopwatch.StartNew();

            Logger.Log("started handling update of file {0}", path.Substring(settings.Path.Length));

            var actions = new List <ICommand>();

            var dependants         = project.GetDependantsByPath(oldPath ?? path);
            var reversedDependants = project.GetDependantsByPath(oldPath ?? path, reversed: true);

            actions.AddRange(dependants.Select(e => new DropCommand(e)));

            var oldEntity = project.FileToEntityMapping.TryGet(oldPath ?? path);

            if (oldEntity != null)
            {
                actions.Add(new DropCommand(oldEntity));
            }


            if (File.Exists(path))
            {
                var content = SqlProjectInfo.GetContent(path);
                if (content == null)
                {
                    Logger.Log("failed to get content for file {0}", path);
                }
                else
                {
                    if (oldEntity.Maybe(_ => _.Content) == content)
                    {
                        Logger.Log("fake update of {0}", path);
                        return;                         // short circuit return to prevent fake updates
                    }

                    var entity = SqlEntity.Create(path, content, settings);
                    if (oldEntity.Maybe(e => e.Name) != entity.Name)
                    {
                        actions.Add(new DropCommand(entity));
                    }

                    actions.Add(new CreateCommand(entity));
                }
            }
            else
            {
                if (dependants.Any())
                {
                    Logger.Log("you have removed {0}, but some entities depends on it: {1}",
                               project.FileToEntityMapping.TryGet(path).Maybe(e => e.Name) ?? path,
                               string.Join(";", dependants));

                    return;
                }
            }

            actions.AddRange(reversedDependants.Select(e => new CreateCommand(e)));

            while (transaction.Connection != null && actions.Any())
            {
                foreach (var action in actions.ToList())
                {
                    if (transaction.Connection == null)
                    {
                        break;
                    }

                    action.Perform(transaction, project, actions.Add);
                    actions.Remove(action);
                }
            }

            Logger.Log("finished handling update of file {0} for {1}", path.Substring(settings.Path.Length), watch.Elapsed);
        }
Beispiel #16
0
 public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
 {
     lambda(transaction, project, commandAdder);
 }
Beispiel #17
0
 public void Perform(SqlTransaction transaction, SqlProjectInfo project, CommandAdder commandAdder)
 {
     lambda(transaction, project, commandAdder);
 }