Beispiel #1
0
        private void ProcessSequences()
        {
            ICodeWriterPlSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterPlSql();

            foreach (IGenerator gen in Model.Generators)
            {
                if (gen.Owner is IEntity && !(gen.Owner as IEntity).Persistence.Persisted)
                {
                    continue;
                }
                if (gen.Type == GeneratorType.Sequence)
                {
                    sql.ClearAll();
                    sql.WriteCreateSequence(gen, environment);
                    ISpellHint hint = genie.FindHint(gen);
                    if (hint != null)
                    {
                        sql.WriteText(hint.GetText(gen));
                    }
                    creator.WriteFrom(sql);
                    creator.WriteSeparator();

                    genie.Config.NotifyAssistants("Create", gen, sql.ToString(true));

                    WriteExecImmediatWhenNotExists(
                        "ALL_SEQUENCES",
                        String.Format("SEQUENCE_OWNER='{0}' AND SEQUENCE_NAME='{1}'", gen.Persistence.Schema, gen.Persistence.Name),
                        sql);
                    updater.WriteLine();
                }
            }
        }
Beispiel #2
0
        void ProcessIndexes()
        {
            ICodeWriterTransactSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterTransactSql();

            foreach (IIndex index in Model.PhysicalModel.Indexes)
            {
                if (!index.Processed && index.Generate)
                {
                    sql.ClearAll();
                    sql.WriteCreateIndex(index, environment);
                    ISpellHint hint = genie.FindHint(index);
                    if (hint != null)
                    {
                        sql.WriteText(hint.GetText(index));
                    }
                    creator.WriteFrom(sql);
                    WriteUpdate(String.Format("NOT EXISTS(SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'{0}') AND name = N'{1}')",
                                              index.Entity.Persistence.FullName,
                                              index.Name),
                                sql);
                    creator.EndBatch();
                    updater.EndBatch();
                    creator.WriteLine();
                    updater.WriteLine();

                    genie.Config.NotifyAssistants("Create", index, sql.ToString(true));
                }
            }
        }
Beispiel #3
0
 private void ProcessSchemas()
 {
     if (createSchemas)
     {
         List <string>          schemas = new List <string>();
         ICodeWriterTransactSql sql     = Model.Lamp.CodeWritersFactory.CreateCodeWriterTransactSql();
         foreach (ISchema schema in Model.Schemas)
         {
             if (schemas.Contains(schema.Persistence.Name))
             {
                 continue;
             }
             schemas.Add(schema.Persistence.Name);
             sql.ClearAll();
             sql.WriteLine("CREATE SCHEMA [{0}] AUTHORIZATION [dbo]", schema.Persistence.Name);
             ISpellHint hint = genie.FindHint("Schema", schema.Name);
             if (hint != null)
             {
                 sql.WriteText(hint.GetText(null));
             }
             WriteUpdate(String.Format("NOT EXISTS(SELECT 1 FROM sys.schemas WHERE name = N'{0}')", schema.Persistence.Name), sql, true);
             creator.WriteFrom(sql);
             creator.EndBatch();
             creator.WriteLine();
             updater.EndBatch();
             updater.WriteLine();
         }
     }
 }
Beispiel #4
0
        private void ProcessDatabase()
        {
            ICodeWriterTransactSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterTransactSql();

            if (createDatabase)
            {
                sql.WriteLine("CREATE DATABASE [{0}]", databaseName);
                ISpellHint hint = genie.FindHint("Database", databaseName);
                if (hint != null)
                {
                    sql.WriteText(hint.GetText(null));
                }
                sql.WriteSeparator();
                creator.WriteFrom(sql);
                creator.EndBatch();
                creator.WriteLine();
                WriteUpdate(String.Format("NOT EXISTS(SELECT 1 FROM sys.databases WHERE name = N'{0}')", databaseName), sql);
                genie.Config.NotifyAssistants("CreateDatabase", null, sql.ToString(true));
                updater.EndBatch();
                updater.WriteLine();
            }

            sql.ClearAll();
            sql.WriteLine("USE [{0}]", databaseName);
            sql.EndBatch();
            sql.WriteLine();
            creator.WriteFrom(sql);
            updater.WriteFrom(sql);
        }
Beispiel #5
0
        private void ProcessIndexes()
        {
            ICodeWriterPlSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterPlSql();

            foreach (IIndex index in Model.PhysicalModel.Indexes)
            {
                if (!index.Processed && index.Generate)
                {
                    sql.ClearAll();
                    sql.WriteCreateIndex(index, environment);
                    ISpellHint hint = genie.FindHint(index);
                    if (hint != null)
                    {
                        sql.WriteText(hint.GetText(index));
                    }
                    creator.WriteFrom(sql);
                    creator.WriteSeparator();
                    StringBuilder sqlFrom = new StringBuilder();
                    sqlFrom.Append("SELECT I.OWNER, I.INDEX_NAME, COUNT(*) ");
                    sqlFrom.Append("FROM ALL_INDEXES I ");
                    sqlFrom.Append(" INNER JOIN ALL_IND_COLUMNS IC ON I.OWNER = IC.INDEX_OWNER AND I.INDEX_NAME = IC.INDEX_NAME");
                    sqlFrom.Append(" LEFT OUTER JOIN ALL_CONSTRAINTS AC ON I.INDEX_NAME = AC.CONSTRAINT_NAME AND I.TABLE_OWNER = AC.OWNER AND I.TABLE_NAME = AC.TABLE_NAME AND AC.CONSTRAINT_TYPE = 'P'");
                    sqlFrom.Append(" LEFT OUTER JOIN(");
                    for (int i = 0; i < index.Columns.Count; i++)
                    {
                        sqlFrom.AppendFormat("{0}SELECT '{1}' AS COLUMN_NAME FROM DUAL",
                                             i == 0 ? "" : " UNION ",
                                             index.Columns[i].Attribute.Persistence.Name);
                    }
                    sqlFrom.Append(") IC2 ON IC.COLUMN_NAME = IC2.COLUMN_NAME");
                    sqlFrom.AppendFormat(" WHERE I.OWNER='{0}' AND I.TABLE_OWNER = '{1}' AND I.TABLE_NAME='{2}' AND AC.CONSTRAINT_NAME IS NULL",
                                         index.Schema,
                                         index.Entity.Persistence.Schema,
                                         index.Entity.Persistence.Name);
                    sqlFrom.Append(" GROUP BY I.OWNER, I.INDEX_NAME");
                    sqlFrom.AppendFormat(" HAVING COUNT(*) = {0}", index.Columns.Count);
                    sqlFrom.Append(" UNION ALL ");
                    sqlFrom.AppendFormat("SELECT I.OWNER, I.INDEX_NAME, 1 FROM ALL_INDEXES I WHERE I.OWNER='{0}' AND I.INDEX_NAME='{1}'",
                                         index.Schema, index.Name);

                    WriteExecImmediatWhenNotExists(
                        String.Format("({0}) T1", sqlFrom.ToString()),
                        "1=1",
                        sql);
                    genie.Config.NotifyAssistants("Create", index, sql.ToString(true));
                    updater.WriteLine();
                }
            }
        }
Beispiel #6
0
        private void ProcessPrimaryKey(IEntity entity)
        {
            ICodeWriterPlSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterPlSql();

            sql.WriteCreatePrimaryKey(entity.PrimaryId, environment);
            ISpellHint hint = genie.FindHint(entity.PrimaryId);

            if (hint != null)
            {
                sql.WriteText(hint.GetText(entity.PrimaryId));
            }
            creator.WriteFrom(sql);
            creator.WriteSeparator();

            WriteExecImmediatWhenNotExists(
                "ALL_CONSTRAINTS",
                String.Format("OWNER='{0}' AND TABLE_NAME='{1}' AND CONSTRAINT_TYPE='P'",
                              entity.Persistence.Schema,
                              entity.Persistence.Name),
                sql);
            genie.Config.NotifyAssistants("Create", entity.PrimaryId, sql.ToString(true));
            updater.WriteLine();
        }
Beispiel #7
0
        private void ProcessPrimaryKey(IEntity entity)
        {
            ICodeWriterTransactSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterTransactSql();

            sql.WriteCreatePrimaryKey(entity.PrimaryId, environment);
            ISpellHint hint = genie.FindHint(entity.PrimaryId);

            if (hint != null)
            {
                sql.WriteText(hint.GetText(entity.PrimaryId));
            }
            creator.WriteFrom(sql);

            WriteUpdate(String.Format("NOT EXISTS(SELECT 1 FROM sys.indexes WHERE object_id = OBJECT_ID(N'{0}') AND is_primary_key = 1)",
                                      entity.Persistence.FullName),
                        sql);

            genie.Config.NotifyAssistants("Create", entity.PrimaryId, sql.ToString(true));

            creator.EndBatch();
            creator.WriteLine();
            updater.EndBatch();
            updater.WriteLine();
        }
Beispiel #8
0
        private void ProcessEntity(IEntity entity)
        {
            updater.WriteLine(creator.WriteCommentLine("Table of {0}", entity.FullName));

            ICodeWriterPlSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterPlSql();

            sql.WriteCreateTable(entity, environment);
            ISpellHint hint = genie.FindHint(entity);

            if (hint != null)
            {
                sql.WriteText(hint.GetText(entity));
            }
            creator.WriteFrom(sql);
            creator.WriteSeparator();

            WriteExecImmediatWhenNotExists(
                "ALL_TABLES",
                String.Format("OWNER='{0}' AND TABLE_NAME='{1}'", entity.Persistence.Schema, entity.Persistence.Name),
                sql);
            updater.WriteLine();

            genie.Config.NotifyAssistants("Create", entity, sql.ToString(true));

            IAttributes attributes;

            switch (Model.Lamp.Config.Layers.DomainConfig.MappingStrategy)
            {
            case GenieLamp.Core.Layers.MappingStrategy.TablePerSubclass:
                attributes = entity.GetAttributes(false);
                break;

            case GenieLamp.Core.Layers.MappingStrategy.TablePerClass:
                attributes = entity.GetAttributes(true);
                break;

            default:
                throw new GenieLamp.Core.Exceptions.GlException("Mapping strategy is not supported: '{0}'",
                                                                Model.Lamp.Config.Layers.DomainConfig.MappingStrategy);
            }

            ProcessUpdateAttributes(entity, attributes);

            ProcessPrimaryKey(entity);

            ProcessUniqueIds(entity);

            creator.WriteLine();
            updater.WriteLine();
            WriteComment(CommentTarget.Table, entity.Persistence.FullName, entity.Doc);

            foreach (IAttribute a in attributes)
            {
                if (a.Persistence.Persisted)
                {
                    WriteComment(CommentTarget.Column,
                                 String.Format("{0}.{1}", entity.Persistence.FullName, a.Persistence.Name),
                                 a.Doc);
                }
            }
        }
Beispiel #9
0
        private void ProcessEntity(IEntity entity)
        {
            if (!entity.Persistence.Persisted)
            {
                return;
            }

            updater.WriteLine(creator.WriteCommentLine("Table of {0}", entity.FullName));

            ICodeWriterTransactSql sql = Model.Lamp.CodeWritersFactory.CreateCodeWriterTransactSql();

            sql.WriteCreateTable(entity, environment);
            ISpellHint hint = genie.FindHint(entity);

            if (hint != null)
            {
                sql.WriteText(hint.GetText(entity));
            }
            creator.WriteFrom(sql);
            creator.EndBatch();

            WriteUpdate(String.Format("NOT EXISTS(SELECT 1 FROM sys.tables WHERE object_id = OBJECT_ID(N'{0}'))", entity.Persistence.FullName), sql);

            genie.Config.NotifyAssistants("Create", entity, sql.ToString(true));
            updater.EndBatch();

            IAttributes attributes;

            switch (Model.Lamp.Config.Layers.DomainConfig.MappingStrategy)
            {
            case GenieLamp.Core.Layers.MappingStrategy.TablePerSubclass:
                attributes = entity.GetAttributes(false);
                break;

            case GenieLamp.Core.Layers.MappingStrategy.TablePerClass:
                attributes = entity.GetAttributes(true);
                break;

            default:
                throw new GenieLamp.Core.Exceptions.GlException("Mapping strategy is not supported: '{0}'",
                                                                Model.Lamp.Config.Layers.DomainConfig.MappingStrategy);
            }

            UpdateAttributes(entity, attributes);

            ProcessPrimaryKey(entity);

            ProcessUniqueIds(entity);

//			creator.WriteLine();
//			updater.WriteLine();
//			WriteComment(CommentTarget.Table, entity.Persistence.FullName, entity.Doc);
//
//			foreach (IAttribute a in attributes)
//			{
//				if (a.Persistence.Persisted)
//				{
//					WriteComment(CommentTarget.Column,
//					             String.Format("{0}.{1}", entity.Persistence.FullName, a.Persistence.Name),
//					             a.Doc);
//				}
//			}
        }