Beispiel #1
0
    public static string WrapInExistingTableHandling(CreateIfExistsModification createIfExistsModification,
                                                     DbTableName dbTableName,
                                                     string createTablePortion)
    {
        switch (createIfExistsModification)
        {
        case CreateIfExistsModification.CreateAnyway:
            return(createTablePortion);

        case CreateIfExistsModification.DropAndRecreate:
            return($@"
DROP TABLE IF EXISTS {dbTableName};
{createTablePortion}");

        case CreateIfExistsModification.CreateIfNotExists:
            return($@"
IF (OBJECT_ID('{dbTableName}', 'U')) IS NULL
BEGIN
    {createTablePortion}
END
");

        default:
            throw new ArgumentOutOfRangeException(nameof(createIfExistsModification),
                                                  createIfExistsModification, null);
        }
    }
Beispiel #2
0
    public static string GenerateCreateTableScript(
        InformationSchemaTableDefinition tableDefinition,
        CreateIfExistsModification createIfExistsModification = CreateIfExistsModification.CreateAnyway)
    {
        var informationSchemaTable = tableDefinition.InformationSchemaTable;

        if (informationSchemaTable.TABLE_TYPE != TableTypes.BaseTable &&
            informationSchemaTable.TABLE_TYPE != TableTypes.View)
        {
            throw new NotSupportedException(
                      $"Table is of a type other than 'Base Table' or 'View'.  Table type is {tableDefinition.InformationSchemaTable.TABLE_TYPE}");
        }

        switch (tableDefinition.InformationSchemaTable.TABLE_TYPE)
        {
        case TableTypes.View:
            throw new NotImplementedException("Have not implemented View creation yet.");

        case TableTypes.BaseTable:
            var schema    = informationSchemaTable.TABLE_SCHEMA;
            var tableName = informationSchemaTable.TABLE_NAME;
            var columns   = tableDefinition.InformationSchemaColumns;
            var script    = CreateTableScript(schema, tableName, columns, createIfExistsModification);
            return(script);

        default:
            throw new ArgumentOutOfRangeException();
        }
    }
Beispiel #3
0
    public static string CreateTableScript(string schemaName,
                                           string tableName,
                                           IList <SISColumn> columns,
                                           CreateIfExistsModification ifExists = CreateIfExistsModification.CreateAnyway)
    {
        var dbTableName = new DbTableName(schemaName, tableName);

        var sb = new StringBuilder();

        sb.Append($"CREATE TABLE {dbTableName} (\r\n");
        AddColumnDefinitions(sb, columns);
        sb.AppendLine(");");
        var createTablePortion = sb.ToString();

        var totalCreateScript = WrapInExistingTableHandling(ifExists, dbTableName, createTablePortion);

        return(totalCreateScript);
    }