示例#1
0
        private void Initialize()
        {
            if (wasInitialized)
            {
                return;
            }
            dialect = Dialect.Dialect.GetDialect(configProperties);

            string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configProperties, "not-defined");

            autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
            if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
            {
                SchemaMetadataUpdater.Update(cfg, dialect);
                SchemaMetadataUpdater.QuoteTableAndColumns(cfg, dialect);
            }

            dropSQL        = cfg.GenerateDropSchemaScript(dialect);
            createSQL      = cfg.GenerateSchemaCreationScript(dialect);
            formatter      = (PropertiesHelper.GetBoolean(Environment.FormatSql, configProperties, true) ? FormatStyle.Ddl : FormatStyle.None).Formatter;
            wasInitialized = true;
        }
示例#2
0
        private async Task InitializeAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (wasInitialized)
            {
                return;
            }
            dialect = Dialect.Dialect.GetDialect(configProperties);

            string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configProperties, "not-defined");

            autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
            if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
            {
                await(SchemaMetadataUpdater.UpdateAsync(cfg, dialect, cancellationToken)).ConfigureAwait(false);
                SchemaMetadataUpdater.QuoteTableAndColumns(cfg, dialect);
            }

            dropSQL        = cfg.GenerateDropSchemaScript(dialect);
            createSQL      = cfg.GenerateSchemaCreationScript(dialect);
            formatter      = (PropertiesHelper.GetBoolean(Environment.FormatSql, configProperties, true) ? FormatStyle.Ddl : FormatStyle.None).Formatter;
            wasInitialized = true;
        }
示例#3
0
        /// <summary>
        /// Execute the schema updates
        /// </summary>
        /// <param name="scriptAction">The action to write the each schema line.</param>
        /// <param name="doUpdate">Commit the script to DB</param>
        public void Execute(Action <string> scriptAction, bool doUpdate)
        {
            log.Info("Running hbm2ddl schema update");

            string autoKeyWordsImport = PropertiesHelper.GetString(Environment.Hbm2ddlKeyWords, configuration.Properties, "not-defined");

            autoKeyWordsImport = autoKeyWordsImport.ToLowerInvariant();
            if (autoKeyWordsImport == Hbm2DDLKeyWords.AutoQuote)
            {
                SchemaMetadataUpdater.QuoteTableAndColumns(configuration);
            }

            DbConnection connection;
            IDbCommand   stmt = null;

            exceptions.Clear();

            try
            {
                DatabaseMetadata meta;
                try
                {
                    log.Info("fetching database metadata");
                    connectionHelper.Prepare();
                    connection = connectionHelper.Connection;
                    meta       = new DatabaseMetadata(connection, dialect);
                    stmt       = connection.CreateCommand();
                }
                catch (Exception sqle)
                {
                    exceptions.Add(sqle);
                    log.Error("could not get database metadata", sqle);
                    throw;
                }

                log.Info("updating schema");

                string[] createSQL = configuration.GenerateSchemaUpdateScript(dialect, meta);
                for (int j = 0; j < createSQL.Length; j++)
                {
                    string sql       = createSQL[j];
                    string formatted = formatter.Format(sql);

                    try
                    {
                        if (scriptAction != null)
                        {
                            scriptAction(formatted);
                        }
                        if (doUpdate)
                        {
                            log.Debug(sql);
                            stmt.CommandText = sql;
                            stmt.ExecuteNonQuery();
                        }
                    }
                    catch (Exception e)
                    {
                        exceptions.Add(e);
                        log.Error("Unsuccessful: " + sql, e);
                    }
                }

                log.Info("schema update complete");
            }
            catch (Exception e)
            {
                exceptions.Add(e);
                log.Error("could not complete schema update", e);
            }
            finally
            {
                try
                {
                    if (stmt != null)
                    {
                        stmt.Dispose();
                    }
                    connectionHelper.Release();
                }
                catch (Exception e)
                {
                    exceptions.Add(e);
                    log.Error("Error closing connection", e);
                }
            }
        }