Exemple #1
0
        public void GenerateCode(HardcodedEntityInfo concept, IDataMigrationScriptBuilder codeBuilder)
        {
            codeBuilder.AddBeforeDataMigrationScript($@"
EXEC Rhetos.DataMigrationUse '{concept.Module.Name}', '{concept.Name}', 'ID', 'uniqueidentifier';{DataMigrationUseTag.Evaluate(concept)}
GO

DECLARE @entries TABLE (ID uniqueidentifier PRIMARY KEY);
{InsertValuesTag.Evaluate(concept)}

INSERT INTO _{concept.Module.Name}.{concept.Name} (ID)
SELECT newItem.ID
FROM @entries newItem
	LEFT JOIN _{concept.Module.Name}.{concept.Name} existingItem ON existingItem.ID = newItem.ID
WHERE
	existingItem.ID IS NULL;
{UpdateTag.Evaluate(concept)}

DECLARE @ColumnNames nvarchar(max);
SET @ColumnNames = 'ID'{DataMigrationApplyMultipleTag.Evaluate(concept)};
EXEC Rhetos.DataMigrationApplyMultiple '{concept.Module.Name}', '{concept.Name}', @ColumnNames;");

            codeBuilder.AddAfterDataMigrationScript(
                $@"EXEC Rhetos.DataMigrationUse '{concept.Module.Name}', '{concept.Name}', 'ID', 'uniqueidentifier';
GO

DECLARE @entries TABLE (ID uniqueidentifier PRIMARY KEY);
{InsertValuesTag.Evaluate(concept)}

DELETE FROM _{concept.Module.Name}.{concept.Name} WHERE ID NOT IN
(
    SELECT ID FROM @entries
);

EXEC Rhetos.DataMigrationApplyMultiple '{concept.Module.Name}', '{concept.Name}', 'ID';");
        }
        private string HistorySqlSnippet(EntityHistoryInfo conceptInfo)
        {
            return(string.Format(
                       @"SELECT
                    ID = entity.ID,
                    EntityID = entity.ID,
                    ActiveUntil = CAST(NULL AS {6}){5}
                FROM
                    {0}.{1} entity

                UNION ALL

                SELECT
                    ID = history.ID,
                    EntityID = history.EntityID,
                    au.ActiveUntil{4}
                FROM
                    {0}.{2} history
                    LEFT JOIN {0}.{3} au ON au.ID = history.ID",
                       SqlUtility.Identifier(conceptInfo.Entity.Module.Name),
                       SqlUtility.Identifier(conceptInfo.Entity.Name),
                       SqlUtility.Identifier(conceptInfo.Dependency_ChangesEntity.Name),
                       SqlUtility.Identifier(conceptInfo.Entity.Name + "_ChangesActiveUntil"),
                       SelectHistoryPropertiesTag.Evaluate(conceptInfo),
                       SelectEntityPropertiesTag.Evaluate(conceptInfo),
                       _dateTimeSqlColumnType));
        }
        public string CreateViewCodeSnippet(PolymorphicInfo conceptInfo)
        {
            // Column names list (@columnList) is separated from the create query (@sql)
            // to be used in subqueryes, to make sure that the order of columns is the same
            // in all the subqueries. This is necessary for UNION ALL.

            return(string.Format(
                       @"
DECLARE @columnList NVARCHAR(MAX);
SET @columnList = N'{2}';

DECLARE @sql NVARCHAR(MAX);
SET @sql = N'CREATE VIEW {0}.{1}
AS
SELECT
    ID = CONVERT(UNIQUEIDENTIFIER, NULL){3}
WHERE
    0=1
{4}';


PRINT @sql;
EXEC (@sql);
",
                       conceptInfo.Module.Name,
                       conceptInfo.Name,
                       PolymorphicPropertyNameTag.Evaluate(this),
                       PolymorphicPropertyInitializationTag.Evaluate(this),
                       SubtypeQueryTag.Evaluate(this)));
        }
Exemple #4
0
 public static string RemoveColumn(PropertyInfo property, string columnName)
 {
     return(Sql.Format("PropertyDatabaseDefinition_RemoveColumn",
                       SqlUtility.Identifier(property.DataStructure.Module.Name),
                       SqlUtility.Identifier(property.DataStructure.Name),
                       DslUtility.ValidateIdentifier(columnName, property, "Invalid column name."),
                       BeforeRemoveTag.Evaluate(property)).Trim());
 }
 public static string TriggerSnippet(AutoCodeTriggerInfo info)
 {
     return(Sql.Format("AutoCodeDatabaseDefinition_TriggerSnippet",
                       SqlUtility.Identifier(info.Entity.Module.Name),
                       info.Entity.Name,
                       TriggerName(info.Entity),
                       ShortStringPropertyInfo.MaxLength,
                       ColumnsForAutoCodeSelectTag.Evaluate(info)));
 }
Exemple #6
0
        public string CreateDatabaseStructure(IConceptInfo conceptInfo)
        {
            var info      = (ReferencePropertyDbConstraintInfo)conceptInfo;
            var reference = info.Reference;

            return(Sql.Format("ReferencePropertyConstraintDatabaseDefinition_Create",
                              SqlUtility.Identifier(reference.DataStructure.Module.Name) + "." + SqlUtility.Identifier(reference.DataStructure.Name),
                              GetConstraintName(reference),
                              reference.GetColumnName(),
                              ForeignKeyUtility.GetSchemaTableForForeignKey(reference.Referenced),
                              ForeignKeyConstraintOptions.Evaluate(info)));
        }
Exemple #7
0
        public static string AddColumn(ConceptMetadata conceptMetadata, PropertyInfo property, string options = "")
        {
            string columnName = conceptMetadata.Get(property, ColumnNamesMetadata).Single();

            return(Sql.Format("PropertyDatabaseDefinition_AddColumn",
                              SqlUtility.Identifier(property.DataStructure.Module.Name),
                              SqlUtility.Identifier(property.DataStructure.Name),
                              DslUtility.ValidateIdentifier(columnName, property, "Invalid column name."),
                              conceptMetadata.Get(property, ColumnTypesMetadata).Single(),
                              options,
                              Options1Tag.Evaluate(property),
                              Options2Tag.Evaluate(property),
                              AfterCreateTag.Evaluate(property)).Trim());
        }
Exemple #8
0
        public void GenerateCode(EntryInfo concept, IDataMigrationScriptBuilder codeBuilder)
        {
            string insertSnippet = $@"
INSERT INTO @entries (ID) VALUES ('{concept.GetIdentifier()}');";

            codeBuilder.InsertCode(insertSnippet, HardcodedEntityDataMigrationScript.InsertValuesTag, concept.HardcodedEntity);

            codeBuilder.InsertCode($@"

UPDATE _{concept.HardcodedEntity.Module.Name}.{concept.HardcodedEntity.Name}
SET Name = '{concept.Name}'{UpdatePropertyTag.Evaluate(concept)}
WHERE ID = '{concept.GetIdentifier()}';",
                                   HardcodedEntityDataMigrationScript.UpdateTag, concept.HardcodedEntity);
        }
Exemple #9
0
        public string CreateDatabaseStructure(IConceptInfo conceptInfo)
        {
            var info = (DataStructureExtendsInfo)conceptInfo;

            if (ShouldCreateConstraint(info))
            {
                return(Sql.Format("DataStructureExtendsDatabaseDefinition_Create",
                                  SqlUtility.Identifier(info.Extension.Module.Name) + "." + SqlUtility.Identifier(info.Extension.Name),
                                  GetConstraintName(info),
                                  ForeignKeyUtility.GetSchemaTableForForeignKey(info.Base),
                                  ForeignKeyConstraintOptions.Evaluate(info)));
            }
            // TODO: else - Generate a Filter+InvalidData validation in the server application that checks for invalid items.
            return("");
        }
        public string CreateDatabaseStructure(IConceptInfo conceptInfo)
        {
            var info = (ReferencePropertyInfo)conceptInfo;

            if (IsSupported(info))
            {
                return(Sql.Format("ReferencePropertyConstraintDatabaseDefinition_Create",
                                  SqlUtility.Identifier(info.DataStructure.Module.Name) + "." + SqlUtility.Identifier(info.DataStructure.Name),
                                  GetConstraintName(info),
                                  info.GetColumnName(),
                                  ForeignKeyUtility.GetSchemaTableForForeignKey(info.Referenced),
                                  ForeignKeyConstraintOptions.Evaluate(info)));
            }
            return("");
        }
Exemple #11
0
        public string CreateDatabaseStructure(IConceptInfo conceptInfo)
        {
            var info = (SqlIndexMultipleInfo)conceptInfo;

            if (info.SqlImplementation())
            {
                return(Sql.Format("SqlIndexMultipleDatabaseDefinition_Create",
                                  ConstraintName(info),
                                  SqlUtility.Identifier(info.DataStructure.Module.Name),
                                  SqlUtility.Identifier(info.DataStructure.Name),
                                  ColumnsTag.Evaluate(info),
                                  Options1Tag.Evaluate(info),
                                  Options2Tag.Evaluate(info)));
            }
            return(null);
        }
        public void ExtendDatabaseStructure(IConceptInfo conceptInfo, ICodeBuilder codeBuilder, out IEnumerable <Tuple <IConceptInfo, IConceptInfo> > createdDependencies)
        {
            AutoCodePropertyInfo info = (AutoCodePropertyInfo)conceptInfo;

            createdDependencies = null;

            if (AutoCodeTriggerDatabaseDefinition.IsSupported(info.Property.DataStructure))
            {
                codeBuilder.InsertCode(Sql.Format("AutoCodeDatabaseDefinition_ColumnDefinition",
                                                  info.Property.Name,
                                                  ShortStringPropertyInfo.MaxLength,
                                                  ForEachGroupColumnTag.Evaluate(info),
                                                  ForEachGroupValueTag.Evaluate(info)
                                                  ),
                                       AutoCodeTriggerDatabaseDefinition.ColumnsForAutoCodeSelectTag, info.Dependency_TriggerInfo);
            }
        }
        void IAlternativeInitializationConcept.InitializeNonparsableProperties(out IEnumerable <IConceptInfo> createdConcepts)
        {
            var prototype = IsSubtypeOf.GetImplementationViewPrototype();

            Module = prototype.Module;
            Name   = prototype.Name;

            string viewFormat =
                @"SELECT
    ID" + GetSubtypeImplementationIdSnippet() + PropertyImplementationTag.Evaluate(this) + @"
FROM
    {0}.{1}
" + WherePartTag.Evaluate(this);

            ViewSource = string.Format(viewFormat, IsSubtypeOf.Subtype.Module.Name, IsSubtypeOf.Subtype.Name);

            createdConcepts = null;
        }
Exemple #14
0
        public string CreateDatabaseStructure(IConceptInfo conceptInfo)
        {
            var info = (EntityLoggingInfo)conceptInfo;

            return(Sql.Format("EntityLoggingDefinition_Create",
                              SqlUtility.Identifier(info.Entity.Module.Name),
                              SqlUtility.Identifier(info.Entity.Name),
                              GetTriggerNameInsert(info),
                              GetTriggerNameUpdate(info),
                              GetTriggerNameDelete(info),
                              SqlUtility.ScriptSplitterTag,
                              LogPropertyTag.Evaluate(info),
                              TempColumnDefinitionTag.Evaluate(info),
                              TempColumnListTag.Evaluate(info),
                              TempColumnSelectTag.Evaluate(info),
                              TempFromTag.Evaluate(info),
                              AfterInsertLogTag.Evaluate(info)));
        }
        public string CreateDatabaseStructure(IConceptInfo conceptInfo)
        {
            var info = (LegacyEntityWithAutoCreatedViewInfo)conceptInfo;

            return(Sql.Format("LegacyEntityWithAutoCreatedViewDatabaseDefinition_Create",
                              SqlUtility.Identifier(info.Module.Name),
                              SqlUtility.Identifier(info.Name),
                              SqlUtility.ScriptSplitterTag,
                              SqlUtility.GetFullName(info.Table),
                              InsertTriggerName(info),
                              UpdateTriggerName(info),
                              DeleteTriggerName(info),
                              ViewSelectPartTag.Evaluate(info),
                              ViewFromPartTag.Evaluate(info),
                              TriggerInsertPartTag.Evaluate(info),
                              TriggerSelectForInsertPartTag.Evaluate(info),
                              TriggerSelectForUpdatePartTag.Evaluate(info),
                              TriggerFromPartTag.Evaluate(info)));
        }