Example #1
0
        private static void AddForeignKeyConstraint(ExecuteNullStatement statement, Dictionary <string, Table> tables, List <Token> tokens)
        {
            var finder               = new TokenFinder(tokens);
            var tableName            = finder.GetIdAfterToken("TOKEN_TABLE");
            var columnName           = finder.GetIdAfterToken("TOKEN_KEY");
            var foreignKeyName       = finder.GetIdAfterToken("TOKEN_CONSTRAINT");
            var referencedTableName  = finder.GetIdAfterToken("TOKEN_REFERENCES", skipParenthesis: false);
            var referencedColumnName = finder.GetIdAfterToken("(", finder.CurrentIndex - 1);

            if (string.IsNullOrWhiteSpace(tableName) || string.IsNullOrWhiteSpace(columnName) ||
                string.IsNullOrWhiteSpace(referencedTableName) || string.IsNullOrWhiteSpace(referencedColumnName))
            {
                throw new InvalidOperationException("A foreign key constrains should contain table + column + referenced table + referenced column");
            }

            var table           = Helper.FindTableAndColumn(tableName, columnName, tables);
            var referencedTable = Helper.FindTableAndColumn(referencedTableName, referencedColumnName, tables);
            var fk = new ForeignKeyConstraint
            {
                Name    = foreignKeyName,
                Columns = new List <string> {
                    columnName
                },
                ReferencedTableName = referencedTableName,
                ReferencedColumns   = new List <string> {
                    referencedColumnName
                }
            };

            table.Table.ForeignKeyConstraints.Add(fk);
        }
Example #2
0
        private SqlSelectStatement ParseAsSelect(IEnumerable <Token> tokens)
        {
            var finder     = new TokenFinder(tokens.ToList(  ));
            var id         = finder.GetIdAfterToken("(");
            var indexOrder = finder.FindToken("TOKEN_ORDER");
            var order      = finder.GetTokensBetween("TOKEN_BY", ")", indexOrder);
            var sql        = $"SELECT {id}";

            var indexPartition = finder.FindToken("TOKEN_s_PARTITION");

            if (indexPartition != -1)
            {
                _PartitionField = finder.GetTokensBetween("TOKEN_BY", "TOKEN_ORDER", indexPartition);
                sql            += $" WHERE {_PartitionField} = @{_PartitionField}";
            }
            sql += $" ORDER BY {order}";
            var result = Parser.Parse(sql);

            if (result.Errors.Any())
            {
                throw new SqlServerParserException(result.Errors);
            }

            return((SqlSelectStatement)result.Script.Batches.First( ).Children.First( ));
        }
Example #3
0
        private static void SetCheckConstraint(ExecuteNullStatement statement, Dictionary <string, Table> tables, List <Token> tokens)
        {
            string constraint = null;
            var    finder     = new TokenFinder(tokens);
            var    tableName  = finder.GetIdAfterToken("TOKEN_TABLE");
            var    isCheck    = finder.GetTokenAfterToken("TOKEN_CHECK", "TOKEN_ID");
            var    isAll      = finder.GetTokenAfterToken("TOKEN_ALL", "TOKEN_CONSTRAINT");

            if (isAll == null)
            {
                constraint = finder.GetTokenAfterToken("TOKEN_ID", "TOKEN_CONSTRAINT");
                constraint = Helper.CleanName(constraint);
            }
            var tc          = Helper.FindTable(tableName, tables);
            var constraints = tc.Table.ForeignKeyConstraints;

            if (isAll != null)
            {
                foreach (var keyConstraint in constraints)
                {
                    keyConstraint.CheckThrowsException = isCheck != null;
                }
            }
            else
            {
                var keyConstraint = constraints.SingleOrDefault(c => c.Name == constraint);
                if (keyConstraint == null)
                {
                    throw new SqlInvalidConstraintException(constraint);
                }

                keyConstraint.CheckThrowsException = isCheck != null;
            }
        }
Example #4
0
        public string Render(string mdText)
        {
            var tokens = new TokenFinder().FindTokens(mdText);

            var validHtmlTags = new TokenValidator().GetParagraphsWithValidTokens(tokens, mdText);

            return(new Md2HtmlTranslator().TranslateMdToHtml(mdText, validHtmlTags));
        }
Example #5
0
        private static void SetIdentityInsert(ExecuteNullStatement statement, Dictionary <string, Table> tables, List <Token> tokens)
        {
            var finder    = new TokenFinder(tokens);
            var tableName = finder.GetIdAfterToken("TOKEN_IDENTITY_INSERT");
            var isOn      = finder.GetTokenAfterToken("TOKEN_ON", "TOKEN_ID");
            var tc        = Helper.FindTable(tableName, tables);

            tc.Table.Options[Table.OptionEnum.IdentityInsert] = (isOn != null ? "on" : "off");
        }
Example #6
0
        private string _match(TokenFinder finder, string target)
        {
            // Try the regex on the target string
            Match matched = finder.test.Match(target);

            // Return null if no match found
            if (matched.Success == false)
            {
                return(null);
            }
            // There now must be a match because we would have returned
            // Move the cursor to the end of the match
            _cursor += matched.Value.Length;
            // Return the actual Token value of the match
            return(matched.Value);
        }
Example #7
0
        private static void AddPrimaryKeyConstraint(ExecuteNullStatement statement, Dictionary <string, Table> tables, List <Token> tokens)
        {
            var finder         = new TokenFinder(tokens);
            var tableName      = finder.GetIdAfterToken("TOKEN_TABLE");
            var columnNames    = finder.GetIdListAfterToken("(");
            var primaryKeyName = finder.GetIdAfterToken("TOKEN_CONSTRAINT");

            var table   = Helper.FindTable(tableName, tables).Table;
            var columns = table.Columns.Where(c => columnNames.Contains(c.Name)).ToList(  );

            table.PrimaryKeyConstraints.Add(primaryKeyName, columns);
            table.PrimaryKeys.AddRange(columns);
            foreach (var column in columns)
            {
                column.IsPrimaryKey = true;
            }
        }
Example #8
0
        private static void SetGlobalVariable(ExecuteNullStatement statement, Dictionary <string, Table> tables, List <Token> tokens)
        {
            var finder        = new TokenFinder(tokens);
            var variableName  = finder.GetIdAfterToken("TOKEN_SET", isSingleTokenId: true);
            var variableValue = finder.GetTokenAfterToken("TOKEN_ID", "TOKEN_ID");
            var options       = ((MemoryDbConnection )statement._Command.Connection).GetMemoryDatabase( ).Options;

            if (string.IsNullOrWhiteSpace(variableName) == false &&
                string.IsNullOrWhiteSpace(variableValue) == false &&
                options.ContainsKey(variableName))
            {
                if (variableValue == "NULL")
                {
                    variableValue = null;
                }

                options[variableName] = variableValue;
            }
        }