Пример #1
0
        public static string EmitUpdateStatement(AstTableNode astTableNode, IEnumerable <TableColumnValueMapping> assignments, IEnumerable <TableColumnValueMapping> conditions)
        {
            var updateTemplate = new TemplatePlatformEmitter("SimpleUpdate");

            updateTemplate.Map("Table", astTableNode.SchemaQualifiedName);
            var assignmentStrings = new List <string>();

            foreach (var assignmentPair in assignments)
            {
                assignmentStrings.Add(String.Format(CultureInfo.InvariantCulture, "{0} {1} {2}", assignmentPair.ColumnName, assignmentPair.OperatorString, assignmentPair.ColumnValue));
            }

            updateTemplate.Map("ColumnValuePairs", FlattenStringList(assignmentStrings));

            var whereTemplate = new TemplatePlatformEmitter("SimpleWhere");
            var whereStrings  = new List <string>();

            foreach (var wherePair in conditions)
            {
                whereStrings.Add(String.Format(CultureInfo.InvariantCulture, "{0} {1} {2}", wherePair.ColumnName, wherePair.OperatorString, wherePair.ColumnValue));
            }

            whereTemplate.Map("ColumnValuePairs", FlattenStringList(whereStrings, " AND "));

            return(String.Format(CultureInfo.InvariantCulture, "{0} {1}", updateTemplate.Emit(), whereTemplate.Emit()));
        }
Пример #2
0
        private void Append(ForeignKeyConstraint constraint)
        {
            string strConstraintName;

            if (String.IsNullOrEmpty(constraint.Name) || !constraint.Name.StartsWith("FK_"))
            {
                strConstraintName = String.Format(System.Globalization.CultureInfo.InvariantCulture, "FK_{0}_{1}", constraint.Parent.Name, constraint.Name);
            }
            else
            {
                strConstraintName = constraint.Name;
            }

            if (strConstraintName.Length >= 128)
            {
                strConstraintName = strConstraintName.Substring(0, 127);
            }

            TemplatePlatformEmitter te = new TemplatePlatformEmitter(
                "ForeignKeyConstraintTemplate",
                constraint.Parent.Name,
                strConstraintName,
                new KeysBuilder(constraint.LocalColumnList).BuildForeignKeys(),
                constraint.Table,
                new KeysBuilder(constraint.ForeignColumnList).BuildForeignKeys()
                );

            _stringBuilder.Append(te.Emit(constraint));
            _stringBuilder.Append(NEWLINE);
        }
Пример #3
0
        public static string EmitSelectAllStatement(AstTableNode astTableNode, IEnumerable <string> columnNames)
        {
            var selectTemplate = new TemplatePlatformEmitter("SimpleSelect");

            selectTemplate.Map("Table", astTableNode.SchemaQualifiedName);
            selectTemplate.Map("Columns", FlattenStringList(columnNames));
            return(selectTemplate.Emit());
        }
Пример #4
0
        // TODO: We've made the call to just use the staging node as the lowered container rather than creating a new one and copying everything over
        public static void ProcessContainers(SymbolTable symbolTable)
        {
            var snapshotSymbolTable = new List <IReferenceableItem>(symbolTable);

            foreach (var astNamedNode in snapshotSymbolTable)
            {
                var stagingNode = astNamedNode as AstStagingContainerTaskNode;
                if (stagingNode != null && astNamedNode.FirstThisOrParent <ITemplate>() == null)
                {
                    var stagingCreateContainer = new AstContainerTaskNode(stagingNode)
                    {
                        Name = String.Format(CultureInfo.InvariantCulture, Properties.Resources.CreateStaging, stagingNode.Name),
                        Log  = false,
                    };

                    var stagingDropContainer = new AstContainerTaskNode(stagingNode)
                    {
                        Name = String.Format(CultureInfo.InvariantCulture, Properties.Resources.DropStaging, stagingNode.Name),
                        Log  = false,
                    };

                    stagingNode.Tasks.Insert(0, stagingCreateContainer);
                    stagingNode.Tasks.Add(stagingDropContainer);

                    foreach (var baseTable in stagingNode.Tables)
                    {
                        var table = baseTable as AstTableNode;
                        if (table != null)
                        {
                            TableLowerer.LowerTable(
                                stagingCreateContainer,
                                table,
                                String.Format(CultureInfo.InvariantCulture, Properties.Resources.CreateStagingTable, table.Name),
                                stagingNode.ExecuteDuringDesignTime);

                            var dropStagingTemplate     = new TemplatePlatformEmitter("DropStagingTable", table.SchemaQualifiedName);
                            var dropTableExecuteSqlTask = new AstExecuteSqlTaskNode(stagingNode)
                            {
                                Name       = StringManipulation.NameCleanerAndUniqifier(String.Format(CultureInfo.InvariantCulture, Properties.Resources.DropStagingTable, table.Name)),
                                Connection = table.Connection,
                                ExecuteDuringDesignTime = stagingNode.ExecuteDuringDesignTime,
                            };
                            dropTableExecuteSqlTask.Query = new AstExecuteSqlQueryNode(dropTableExecuteSqlTask)
                            {
                                QueryType = QueryType.Standard, Body = dropStagingTemplate.Emit()
                            };
                            stagingDropContainer.Tasks.Add(dropTableExecuteSqlTask);
                        }
                        else
                        {
                            throw new System.NotSupportedException("AstLowering - StagingContainer - a Table Template node was found when lowering staging containers and I don't know what to do with it.");
                        }
                    }
                }
            }
        }
Пример #5
0
        public static void ProcessPrincipals(AstRootNode astRootNode)
        {
            if (astRootNode.Principals.Count > 0)
            {
                var package = new AstPackageNode(astRootNode)
                {
                    Name = "PrincipalsInitializer", Emit = true, PackageType = "Principal"
                };
                astRootNode.Packages.Add(package);

                foreach (AstPrincipalNode principal in astRootNode.Principals)
                {
                    TemplatePlatformEmitter principalTemplate;
                    switch (principal.PrincipalType)
                    {
                    case PrincipalType.ApplicationRole:
                        principalTemplate = new TemplatePlatformEmitter("CreatePrincipalApplicationRole", principal.Name);
                        break;

                    case PrincipalType.DBRole:
                        principalTemplate = new TemplatePlatformEmitter("CreatePrincipalDatabaseRole", principal.Name);
                        break;

                    case PrincipalType.SqlUser:
                        principalTemplate = new TemplatePlatformEmitter("CreatePrincipalSqlUser", principal.Name);
                        break;

                    case PrincipalType.WindowsGroup:
                        principalTemplate = new TemplatePlatformEmitter("CreatePrincipalWindowsUser", principal.Name);
                        break;

                    case PrincipalType.WindowsUser:
                        principalTemplate = new TemplatePlatformEmitter("CreatePrincipalWindowsUser", principal.Name);
                        break;

                    default:
                        MessageEngine.Trace(principal, Severity.Error, "V0139", "Unknown Principal Type {0} in principal {1}", principal.PrincipalType.ToString(), principal.Name);
                        return;
                    }

                    var executeSqlTask = new AstExecuteSqlTaskNode(package)
                    {
                        Name       = Utility.NameCleanerAndUniqifier(String.Format(CultureInfo.InvariantCulture, "PrincipalConfig_{0}", principal.Name)),
                        Connection = principal.Connection
                    };
                    executeSqlTask.Query = new AstExecuteSqlQueryNode(executeSqlTask)
                    {
                        Body      = principalTemplate.Emit(),
                        QueryType = QueryType.Standard
                    };
                    package.Tasks.Add(executeSqlTask);
                }
            }
        }
Пример #6
0
        private void AppendConstraintBase(IndexBase constraint, string primaryKeyString, string unique)
        {
            string clustered    = constraint.Clustered ? "CLUSTERED" : "NONCLUSTERED";
            string ignoreDupKey = constraint.IgnoreDupKey ? "IGNORE_DUP_KEY = ON" : "IGNORE_DUP_KEY = OFF";
            string padIndex     = constraint.PadIndex ? "PAD_INDEX = ON" : "PAD_INDEX = OFF";
            string kName;
            string keys = new KeysBuilder(constraint.Keys).Build(constraint.Name, out kName);

            TemplatePlatformEmitter te = new TemplatePlatformEmitter("ConstraintTemplate", kName, unique + clustered, keys, "WITH(" + padIndex + "," + ignoreDupKey + ")", primaryKeyString);

            _stringBuilder.Append(te.Emit(constraint));
        }
Пример #7
0
        public static string EmitInsertDefaultRowStatement(AstTableNode astTableNode)
        {
            string columnNames         = FlattenStringList(EmitColumnList(astTableNode, true));
            string columnDefaultValues = FlattenStringList(EmitDefaultValueList(astTableNode));

            if (columnDefaultValues.Length > 0)
            {
                var insertTemplate = new TemplatePlatformEmitter("SimpleInsert", astTableNode.SchemaQualifiedName, columnNames, columnDefaultValues);
                return(insertTemplate.Emit());
            }

            MessageEngine.Trace(astTableNode, Severity.Error, "V0142", "No assignable columns detected on table {0}", astTableNode.Name);

            throw new InvalidOperationException("No Assignable Columns Detected.");
        }
Пример #8
0
        private void Append(string tableName, Index index)
        {
            string unique       = index.Unique ? "UNIQUE" : "";
            string clustered    = index.Clustered ? "CLUSTERED" : "NONCLUSTERED";
            string dropExisting = index.DropExisting ? "DROP_EXISTING = ON" : "DROP_EXISTING = OFF";
            string ignoreDupKey = index.IgnoreDupKey ? "IGNORE_DUP_KEY = ON" : "IGNORE_DUP_KEY = OFF";
            string online       = index.Online ? "ONLINE = ON" : "ONLINE = OFF";
            string padIndex     = index.Online ? "PAD_INDEX = ON" : "PAD_INDEX = OFF";
            string sortInTempdb = index.SortInTempdb ? "SORT_IN_TEMPDB = ON" : "SORT_IN_TEMPDB = OFF";
            string sProperties  = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},\n{1},\n{2},\n{3},\n{4}", padIndex, sortInTempdb, dropExisting, ignoreDupKey, online);
            string kName;
            string keys = new KeysBuilder(index.Keys).Build(index.Name, out kName);

            TemplatePlatformEmitter te = new TemplatePlatformEmitter("CreateIndex", unique, clustered, kName, tableName, keys, sProperties, string.Empty);

            _stringBuilder.Append(te.Emit(index));
            _stringBuilder.Append(NEWLINE);
        }
Пример #9
0
        // TODO: Is this the right approach for events and precedence constraints?  Should we have a utility method to handle them?
        // TODO: It would be good to find an approach to unify permissions and move some of this under the securables lowerer.
        public static void ProcessStoredProcedure(AstStoredProcNode storedProcNode)
        {
            var executeSqlNode = new AstExecuteSqlTaskNode(storedProcNode.ParentItem)
            {
                Name = storedProcNode.Name,
                ExecuteDuringDesignTime = storedProcNode.ExecuteDuringDesignTime,
                Connection = storedProcNode.Connection,
                ResultSet  = ExecuteSqlResultSet.None
            };

            executeSqlNode.Query = new AstExecuteSqlQueryNode(executeSqlNode)
            {
                QueryType = QueryType.Standard
            };

            var queryBuilder = new StringBuilder(new StoredProcTSqlEmitter(storedProcNode).Emit());

            foreach (var permission in storedProcNode.Permissions)
            {
                var template = new TemplatePlatformEmitter("CreateStoredProcedurePermission", permission.Action.ToString(), permission.Target.ToString(), storedProcNode.Name, permission.Principal.Name);
                queryBuilder.AppendLine(template.Emit());
            }

            executeSqlNode.Query.Body = queryBuilder.ToString();

            executeSqlNode.PrecedenceConstraints = storedProcNode.PrecedenceConstraints;
            if (executeSqlNode.PrecedenceConstraints != null)
            {
                executeSqlNode.PrecedenceConstraints.ParentItem = executeSqlNode;
            }

            foreach (var eventHandler in storedProcNode.Events)
            {
                executeSqlNode.Events.Add(eventHandler);
                eventHandler.ParentItem = executeSqlNode;
            }

            var parentContainer = storedProcNode.ParentItem as AstContainerTaskNode;

            if (parentContainer != null)
            {
                parentContainer.Tasks.Replace(storedProcNode, executeSqlNode);
            }
        }