Ejemplo n.º 1
0
        private void zAddVariablesToScopeFromDatabaseStep(DatabaseStep databaseStep)
        {
            foreach (OutputParameterMap parameterMap in databaseStep.OutputParameterMapping)
            {
                zAddVariableToScope(m_CurrentScope.ScopeName,
                                    new StateVariableInfo(parameterMap.StateVariable, DataType.String, parameterMap.PersistenceMode)); //TODO: get actual datatype
            }
            if (databaseStep.ResultMapping is ScalarResultMapping)
            {
                ScalarResultMapping scalarResultMapping = (ScalarResultMapping)databaseStep.ResultMapping;
                zAddVariableToScope(m_CurrentScope.ScopeName,
                                    new StateVariableInfo(scalarResultMapping.StateVariable, DataType.String, scalarResultMapping.PersistenceMode)); //TODO: get actual datatype
            }
            if (databaseStep.ResultMapping is TableResultMapping)
            {
                TableResultMapping tableResultMapping       = (TableResultMapping)databaseStep.ResultMapping;
                StepScope          tempScopeForTableResults = null;
                if (tableResultMapping.ObjectSetClassName != null && tableResultMapping.ObjectSetListName != null)
                {
                    zAddVariableToScope(m_CurrentScope.ScopeName,
                                        new StateVariableInfo(tableResultMapping.ObjectSetListName, DataType.List, tableResultMapping.PersistenceMode));

                    string tempScopeCacheKey = zGetCacheKey(m_CurrentScope.ScopeName, tableResultMapping.ObjectSetListName);
                    tempScopeForTableResults = new StepScope(Guid.NewGuid().ToString(),
                                                             tableResultMapping.ObjectSetClassName,
                                                             tempScopeCacheKey,
                                                             null);
                    zSetCurrentScope(tempScopeForTableResults);
                }
                foreach (TableResultMap tableResultMap in tableResultMapping.TableMapping)
                {
                    zAddVariableToScope(m_CurrentScope.ScopeName,
                                        new StateVariableInfo(tableResultMap.StateVariable, DataType.String, tableResultMapping.PersistenceMode)); //TODO: get actual datatype
                }
                if (tempScopeForTableResults != null)
                {
                    zRemoveCurrentScope();
                }
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    ///     The entry point of the program, where the program control starts and ends.
    /// </summary>
    public static void Main()
    {
        // USER INPUT /////////////////////////////////////////////////////////////////////////////////
        // Always first create a new database migration with DatabaseStep.ADD_MIGRATION,
        // and include the created files in the project and set resource file to EmbeddedResource.
        // After creating a migration run UPDATE_DATABASE to update the database.
        const DatabaseStep step = DatabaseStep.UPDATE_DATABASE;
        // Specify the name of the database migration in case of ADD-MIGRATION.
        // Note: Make sure to create a new name for each new migration.
        //       After creating migration include the files in the folder by right clicking on
        //       Zk.Migrations and selecting "Add files from folder". Then add the .cs, .resx and
        //       .Designer.cs files with the name specified below.
        //       Last but not least set the .resx file's build action to EmbeddedResource by right
        //       clicking on it.
        // Make sure that the Setup.postgresql script has run manually to create the database user.
        const string MIGRATION_NAME = "CalendarAndUser";
        // END USER INPUT /////////////////////////////////////////////////////////////////////////////
        // Get executing path from which the location of the Update_Scripts and new Migrations can be determined.
        var executingPath = AppDomain.CurrentDomain.BaseDirectory;

        // Add a new migration (PowerShell: Add-Migration)
        if (step == DatabaseStep.ADD_MIGRATION)
        {
            // Initialize the wrapper classes around the Entity Framework PowerShell API.
            var config     = new Configuration();
            var scaffolder = new MigrationScaffolder(config);
            var migration  = scaffolder.Scaffold(MIGRATION_NAME);

            // Place migration code in main project "Migrations" folder and migration scripts in "App_Data"
            var migrationsPath = Regex.Replace(executingPath, "bin/.*", "");

            // Write migrations
            File.WriteAllText(migrationsPath + MIGRATION_NAME + ".cs", migration.UserCode);
            File.WriteAllText(migrationsPath + MIGRATION_NAME + ".Designer.cs", migration.DesignerCode);
            using (var writer = new ResXResourceWriter(migrationsPath + MIGRATION_NAME + ".resx"))
            {
                foreach (var resource in migration.Resources)
                {
                    writer.AddResource(resource.Key, resource.Value);
                }
            }
            Console.WriteLine("EF code migration {0} written to Migrations folder...\n\n" +
                              "Next step is to include the .cs, .resx and .Designer.cs file in the project" +
                              "by right clicking on the project and selecting " +
                              "\"Add files from folder.\"\n" +
                              "Then right click on {0}.resx and set build action to \"EmbeddedResource\""
                              , migration.MigrationId);
        }
        else if (step == DatabaseStep.CREATE_SCRIPT)
        {
            var config   = new Configuration();
            var migrator = new DbMigrator(config);
            var scriptor = new MigratorScriptingDecorator(migrator);
            // Determine name of the previous run migration if exists.
            string lastMigration = migrator.GetDatabaseMigrations().LastOrDefault();
            // Get the script
            string script = scriptor.ScriptUpdate(sourceMigration: lastMigration, targetMigration: MIGRATION_NAME);
            // Create the PostgreSQL update script based on last migration on database and
            // current migration.
            string formattedScript = string.Format
                                         ("/* * * * * * * * * * * * * * * * * * * * * * *\n" +
                                         " *\n" +
                                         " * Migration:\t\t{0}\n *\n" +
                                         " * Date and time:\t{1}\n" +
                                         " *\n" +
                                         " * * * * * * * * * * * * * * * * * * * * * * */\n\n" +
                                         "{2}",
                                         MIGRATION_NAME,
                                         DateTime.Now,
                                         script);
            // Write string to file in Migrations folder of main project
            var updateScriptPath = Regex.Replace(executingPath, "Zk.Migrations/.*", "Zk/App_Data/Migrations/");
            File.WriteAllText(updateScriptPath + MIGRATION_NAME + ".postgresql", formattedScript);
            Console.WriteLine("Update script {0}.postgresql written to Zk/App_Data/Migrations folder.\n" +
                              "Please include the script by right clicking on the folder and selecting " +
                              "\"Add files to folder\"," +
                              "\nIt is recommended to prefix the filename with the current datetime.",
                              MIGRATION_NAME);
        }
        // If a new migration is created the database can be updated. (PowerShell: Update-Database)
        else if (step == DatabaseStep.UPDATE_DATABASE)
        {
            var config   = new Configuration();
            var migrator = new DbMigrator(config);
            // Write to database
            migrator.Update();
            // Show which migrations were applied.
            var migrationNames = string.Join(", ", migrator.GetDatabaseMigrations().ToArray().First());
            Console.WriteLine("Applied migration {0} to database.", migrationNames);
        }
    }
Ejemplo n.º 3
0
        /// <summary>
        /// Main sequence analysis loop, or an "Analysis Pass". This method "passes" through the sequence step by step,
        /// keeps track of the sequence structurally (scope list, scope dictionary),
        /// and fires delegates for each step and for completion.
        /// </summary>
        /// <param name="sequence"></param>
        /// <param name="onStepScope"></param>
        /// <param name="onSequenceComplete"></param>
        private void zAnalyzeSequence(List <Step> sequence, Func <Step, bool> onStepScope, Action onSequenceComplete)
        {
            //Make sure this SequenceAnalyzer is not being used by another thread. It is not thread safe! All concurrent usage must be done on separate instances.
            if (m_ExecutionStack != null)
            {
                throw new InvalidOperationException("A zAnalyzeSequence call is already in progress. If you need to use this method concurrently, use a separate SequenceAnalyzer instance for each concurrent call.");
            }

            try
            {
                if (sequence.Count > 0)
                {
                    m_ExecutionStack              = new ExecutionStack();
                    m_ExecutionStack.BranchEnded += m_ExecutionStack_BranchEnded;
                    m_ExecutionStack.SetSequence(sequence);

                    zSetCurrentScope(new StepScope(DataScope.RootScopeName,
                                                   DataScope.RootScopeName,
                                                   null,
                                                   m_ExecutionStack.CurrentBranch));

                    do
                    {
                        if (onStepScope != null && !onStepScope(m_ExecutionStack.CurrentStep))
                        {
                            break;
                        }

                        if (m_ExecutionStack.CurrentStep is GetValueStep)
                        {
                            GetValueStep getValueStep = (GetValueStep)m_ExecutionStack.CurrentStep;
                            zAddVariableToScopeFromGetValueStep(getValueStep);
                        }

                        if (m_ExecutionStack.CurrentStep is DatabaseStep)
                        {
                            DatabaseStep databaseStep = (DatabaseStep)m_ExecutionStack.CurrentStep;
                            zAddVariablesToScopeFromDatabaseStep(databaseStep);
                        }

                        if (m_ExecutionStack.CurrentStep is GroupStep)
                        {
                            GroupStep groupStep = (GroupStep)m_ExecutionStack.CurrentStep;
                            if (groupStep.Steps.Count > 0)
                            {
                                m_ExecutionStack.SetNewBranch(groupStep.Steps);
                            }

                            if (groupStep.Iteration is ObjectSetIteration && groupStep.Iteration.IsSetIteration)
                            {
                                ObjectSetIteration objectSetIteration = (ObjectSetIteration)groupStep.Iteration;

                                if (objectSetIteration is ElementSetIteration)
                                {
                                    ElementSetIteration elementSetIteration = (ElementSetIteration)objectSetIteration;
                                    zAddVariableToScopeFromElementSetIteration(elementSetIteration);
                                }

                                if (groupStep.Steps.Count > 0)
                                {
                                    string    newScopeCacheKey = zGetCacheKey(m_CurrentScope.ScopeName, objectSetIteration.ObjectSetListName);
                                    StepScope newScope         = new StepScope(objectSetIteration.ObjectSetScopeName,
                                                                               objectSetIteration.ObjectSetClassName,
                                                                               newScopeCacheKey,
                                                                               m_ExecutionStack.PendingBranch);

                                    if (objectSetIteration is DataSetIteration)
                                    {
                                        List <StateVariableInfo> cacheList;
                                        if (newScope.CacheKey != null && m_ElementSetVariableListCache.TryGetValue(newScope.CacheKey, out cacheList))
                                        {
                                            foreach (StateVariableInfo cachedVariable in cacheList)
                                            {
                                                if (newScope.ClassName == null || DataUtils.GetVariableNameScope(cachedVariable.StateVariableName) == newScope.ClassName)
                                                {
                                                    StateVariableInfo variableForNewScope = zRescopeVariable(cachedVariable, objectSetIteration.ObjectSetScopeName);
                                                    if (!newScope.VariableList.Contains(variableForNewScope))
                                                    {
                                                        newScope.VariableList.Add(variableForNewScope);
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    zSetCurrentScope(newScope);
                                }
                            }
                        }
                    } while (m_ExecutionStack.MoveNext());
                }

                if (onSequenceComplete != null)
                {
                    onSequenceComplete();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                zCleanup();
            }
        }