Esempio n. 1
0
        public static void UpdateDatabaseLogicIfUpdateFileExists(Database database, string databaseUpdateFilePath, bool allFailuresUserCorrectable)
        {
            var linesInScriptOnHd = GetNumberOfLinesInDatabaseScript(databaseUpdateFilePath);

            // We don't want to ask the database for the line number if there is no script.
            if (linesInScriptOnHd == null)
            {
                return;
            }

            int lineMarker;

            try {
                lineMarker = database.GetLineMarker();
            }
            catch (Exception e) {
                const string message = "Failed to get line marker.";
                if (allFailuresUserCorrectable)
                {
                    throw new UserCorrectableException(message, e);
                }
                throw UserCorrectableException.CreateSecondaryException(message, e);
            }

            // We don't want to execute blank scripts against the database because this will cause an error with read-only databases.
            if (lineMarker == linesInScriptOnHd)
            {
                return;
            }

            using (var sw = new StringWriter()) {
                // If the string writer's value is not the empty string, it will end with the line terminator string.
                using (var tr = new StreamReader(File.OpenRead(databaseUpdateFilePath))) {
                    // Read and discard all text before the marker line.
                    for (var i = 0; i < lineMarker; i++)
                    {
                        tr.ReadLine();
                    }

                    // Store all text on and after the marker line and move the marker to the end of the file.
                    for (string lineText; (lineText = tr.ReadLine()) != null; lineMarker += 1)
                    {
                        sw.WriteLine(lineText);
                    }
                }

                try {
                    database.ExecuteSqlScriptInTransaction(sw.ToString());
                }
                catch (Exception e) {
                    const string message = "Failed to update database logic.";
                    if (allFailuresUserCorrectable)
                    {
                        throw new UserCorrectableException(message, e);
                    }
                    throw UserCorrectableException.CreateSecondaryException(message, e);
                }
            }
            database.UpdateLineMarker(lineMarker);
        }
 internal void ReadPageStateVariablesFromCodeAndWriteTypedPageStateMethods(TextWriter writer)
 {
     try {
         foreach (var variable in getVariablesFromCode(code, "PageState"))
         {
             var methodArguments = "this, \"" + variable.Name + "\"";
             writer.WriteLine("/// <summary>");
             writer.WriteLine("/// Returns the page state value of " + variable.Name + " if it has been set. Otherwise, returns the specified default value.");
             writer.WriteLine("/// </summary>");
             writer.WriteLine("private " + variable.TypeName + " get" + variable.PropertyName + "( " + variable.TypeName + " defaultValue ) {");
             writer.WriteLine("return EwfPage.Instance.PageState.GetValue( " + methodArguments + ", defaultValue );");
             writer.WriteLine("}");
             writer.WriteLine("/// <summary>");
             writer.WriteLine("/// Sets the page state value of " + variable.Name + " to the specified value.");
             writer.WriteLine("/// </summary>");
             writer.WriteLine("private void set" + variable.PropertyName + "( " + variable.TypeName + " value ) {");
             writer.WriteLine("EwfPage.Instance.PageState.SetValue( " + methodArguments + ", value );");
             writer.WriteLine("}");
             writer.WriteLine("/// <summary>");
             writer.WriteLine("/// Clears the page state value of " + variable.Name + ".");
             writer.WriteLine("/// </summary>");
             writer.WriteLine("private void clear" + variable.PropertyName + "() {");
             writer.WriteLine("EwfPage.Instance.PageState.ClearValue( " + methodArguments + " );");
             writer.WriteLine("}");
         }
     }
     catch (Exception e) {
         throw UserCorrectableException.CreateSecondaryException("Failed to read page state variables from \"" + pathRelativeToProject + "\".", e);
     }
 }
Esempio n. 3
0
 internal List <WebItemParameter> ReadParametersFromCode(bool readOptionalParameters)
 {
     try {
         return(getVariablesFromCode(code, readOptionalParameters ? "OptionalParameter" : "Parameter").ToList());
     }
     catch (Exception e) {
         throw UserCorrectableException.CreateSecondaryException("Failed to read parameters from \"" + pathRelativeToProject + "\".", e);
     }
 }
Esempio n. 4
0
        private void generateDataAccessCode(TextWriter writer, DevelopmentInstallation installation)
        {
            var baseNamespace = installation.DevelopmentInstallationLogic.DevelopmentConfiguration.LibraryNamespaceAndAssemblyName + ".DataAccess";

            foreach (var database in installation.DevelopmentInstallationLogic.DatabasesForCodeGeneration)
            {
                try {
                    generateDataAccessCodeForDatabase(
                        database,
                        installation.DevelopmentInstallationLogic.LibraryPath,
                        writer,
                        baseNamespace,
                        database.SecondaryDatabaseName.Length == 0
                                                        ? installation.DevelopmentInstallationLogic.DevelopmentConfiguration.database
                                                        : installation.DevelopmentInstallationLogic.DevelopmentConfiguration.secondaryDatabases.Single(
                            sd => sd.name == database.SecondaryDatabaseName));
                }
                catch (Exception e) {
                    throw UserCorrectableException.CreateSecondaryException(
                              "An exception occurred while generating data access logic for the {0}.".FormatWith(DatabaseOps.GetDatabaseNounPhrase(database)),
                              e);
                }
            }
            if (installation.DevelopmentInstallationLogic.DatabasesForCodeGeneration.Any(d => d.SecondaryDatabaseName.Length > 0))
            {
                writer.WriteLine();
                writer.WriteLine("namespace " + baseNamespace + " {");
                writer.WriteLine("public class SecondaryDatabaseNames {");
                foreach (var secondaryDatabase in installation.DevelopmentInstallationLogic.DatabasesForCodeGeneration.Where(d => d.SecondaryDatabaseName.Length > 0)
                         )
                {
                    writer.WriteLine("public const string " + secondaryDatabase.SecondaryDatabaseName + " = \"" + secondaryDatabase.SecondaryDatabaseName + "\";");
                }
                writer.WriteLine("}");
                writer.WriteLine("}");
            }
        }
        internal TableColumns(DBConnection cn, string table, bool forRevisionHistoryLogic)
        {
            try {
                // NOTE: Cache this result.
                AllColumns = Column.GetColumnsInQueryResults(cn, "SELECT * FROM " + table, true);

                foreach (var col in AllColumns)
                {
                    // This hack allows code to be generated against a database that is configured for ASP.NET Application Services.
                    var isAspNetApplicationServicesTable = table.StartsWith("aspnet_");

                    if (!(cn.DatabaseInfo is OracleInfo) && col.DataTypeName == typeof(string).ToString() && col.AllowsNull && !isAspNetApplicationServicesTable)
                    {
                        throw new UserCorrectableException("String column " + col.Name + " allows null, which is not allowed.");
                    }
                }

                // Identify key, identity, and non identity columns.
                var nonIdentityColumns = new List <Column>();
                foreach (var col in AllColumns)
                {
                    if (col.IsKey)
                    {
                        keyColumns.Add(col);
                    }
                    if (col.IsIdentity)
                    {
                        if (identityColumn != null)
                        {
                            throw new UserCorrectableException("Only one identity column per table is supported.");
                        }
                        identityColumn = col;
                    }
                    else
                    {
                        nonIdentityColumns.Add(col);
                    }
                }
                if (!keyColumns.Any())
                {
                    throw new UserCorrectableException("The table must contain a primary key or other means of uniquely identifying a row.");
                }

                // If the table has a composite key, try to use the identity as the key instead since this will enable InsertRow to return a value.
                if (identityColumn != null && keyColumns.Count > 1)
                {
                    keyColumns.Clear();
                    keyColumns.Add(identityColumn);
                }

                RowVersionColumn                      = AllColumns.SingleOrDefault(i => i.IsRowVersion);
                AllColumnsExceptRowVersion            = AllColumns.Where(i => !i.IsRowVersion).ToArray();
                AllNonIdentityColumnsExceptRowVersion = nonIdentityColumns.Where(i => !i.IsRowVersion).ToArray();

                if (forRevisionHistoryLogic)
                {
                    if (keyColumns.Count != 1)
                    {
                        throw new UserCorrectableException(
                                  "A revision history modification class can only be created for tables with exactly one primary key column, which is assumed to also be a foreign key to the revisions table.");
                    }
                    primaryKeyAndRevisionIdColumn = keyColumns.Single();
                    if (primaryKeyAndRevisionIdColumn.IsIdentity)
                    {
                        throw new UserCorrectableException("The revision ID column of a revision history table must not be an identity.");
                    }
                }

                dataColumns = AllColumns.Where(col => !col.IsIdentity && !col.IsRowVersion && col != primaryKeyAndRevisionIdColumn).ToArray();
            }
            catch (Exception e) {
                throw UserCorrectableException.CreateSecondaryException("An exception occurred while getting columns for table " + table + ".", e);
            }
        }