Ejemplo n.º 1
0
        public static Query Parse(Benumerator <char> en)
        {
            var s = en.Save();

            en.MoveNext();
            if (en.Current == ' ')
            {
                en.MoveNext();
            }

            if (en.Current == 'C')
            {
                return(CreateTable.Parse(s));
            }
            else if (en.Current == 'F')
            {
                return(Get.Parse(s));
            }
            else if (en.Current == 'I')
            {
                return(Insert.Parse(s));
            }
            else
            {
                throw new Exception("Unrecognized first command");
            }
        }
Ejemplo n.º 2
0
        public static new Get Parse(Benumerator <char> en)
        {
            // Skipping first whitespace and failing if empty
            if (!en.MoveNext() || en.Current == ' ' && !en.MoveNext())
            {
                throw new Exception("failed to parse. No Input");
            }

            var from   = From.Parse(en);
            var select = Select.Parse(en);

            if (from != null && select.columns.Length > 0)
            {
                return(new Get
                {
                    from = from,
                    columns = select.columns,
                    select = select
                });
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
 public static void SkipWhitespace(Benumerator <char> en)
 {
     if (en.Current == ' ')
     {
         en.MoveNext();
     }
 }
Ejemplo n.º 4
0
        public static From Parse(Benumerator <char> en)
        {
            if (en.Current != 'F' || !en.MoveNext() ||
                en.Current != 'R' || !en.MoveNext() ||
                en.Current != 'O' || !en.MoveNext() ||
                en.Current != 'M' || !en.MoveNext() ||
                en.Current != ' '
                )
            {
                throw new Exception("failed to parse FROM. Expected FROM keyword");
            }


            List <char> tableName = new List <char>();

            while (en.MoveNext() && en.Current != ' ')
            {
                tableName.Add(en.Current);
            }

            if (!en.MoveNext())
            {
                throw new Exception("failed to parse FROM. Unexpected end up input");
            }

            return(new From {
                table = new string(tableName.ToArray())
            });
        }
Ejemplo n.º 5
0
        public static Query Parse(IEnumerable <string> lines)
        {
            var query = Benumerator.AsLongString(lines);

            var save = query.Save();

            return(Query.Parse(save));
        }
Ejemplo n.º 6
0
        public static Select Parse(Benumerator <char> en)
        {
            var columns = _Parse(en);

            return(new Select
            {
                columns = columns.ToArray()
            });
        }
Ejemplo n.º 7
0
 public static bool HasValue(Benumerator <char> en, string value)
 {
     foreach (Char c in value)
     {
         if (en.Current != c)
         {
             return(false);
         }
         en.MoveNext();
     }
     return(true);
 }
Ejemplo n.º 8
0
        public static string ParseWord(Benumerator <char> en, bool possiblyAtEnd = false)
        {
            Parser.SkipWhitespace(en);

            List <char> work = new List <char>();

            do
            {
                work.Add(en.Current);
            }while (en.MoveNext() && en.Current != ' ' && en.Current != ',' && en.Current != '(' && en.Current != ')');


            return(new String(work.ToArray()));
        }
Ejemplo n.º 9
0
        public static IEnumerable <SelectColumn> Parse(Benumerator <char> en, bool possiblyAtEnd = false)
        {
            do
            {
                Parser.SkipWhitespace(en);

                string columnName = Parser.ParseWord(en, possiblyAtEnd);
                if (!string.IsNullOrWhiteSpace(columnName))
                {
                    yield return(new SelectColumn
                    {
                        columnName = columnName
                    });
                }

                Parser.SkipWhitespace(en);
            }while (en.Current == ',' && en.MoveNext());
        }
Ejemplo n.º 10
0
        private static IEnumerable <SelectColumn> _Parse(Benumerator <char> en)
        {
            // Next value MUST be "SELECT "
            if (en.Current != 'S' || !en.MoveNext() ||
                en.Current != 'E' || !en.MoveNext() ||
                en.Current != 'L' || !en.MoveNext() ||
                en.Current != 'E' || !en.MoveNext() ||
                en.Current != 'C' || !en.MoveNext() ||
                en.Current != 'T' || !en.MoveNext() ||
                en.Current != ' ' || !en.MoveNext()
                )
            {
                throw new Exception("failed to parse SELECT. Expecting SELECT keyword");
            }


            // I am very lazily forcing the use of * for now
            foreach (var col in SelectColumn.Parse(en, true))
            {
                yield return(col);
            }
        }
Ejemplo n.º 11
0
        public void TestParseWord()
        {
            var en = Benumerator.AsLongString(new[] { "one ,two ,three" });

            en.MoveNext();

            Assert.AreEqual("one", SDB.Parser.ParseWord(en));
            SDB.Parser.SkipWhitespace(en);
            if (en.Current == ',')
            {
                en.MoveNext();
            }

            Assert.AreEqual("two", SDB.Parser.ParseWord(en));
            SDB.Parser.SkipWhitespace(en);
            if (en.Current == ',')
            {
                en.MoveNext();
            }

            Assert.AreEqual("three", SDB.Parser.ParseWord(en, true));
        }
Ejemplo n.º 12
0
        public static ColumnDefinition Parse(Benumerator <char> en)
        {
            ColumnDefinition colDef = new ColumnDefinition();
            List <char>      col;

            while (en.Current == ' ' || en.Current == ',')
            {
                en.MoveNext();
            }

            // get name
            col = new List <char>();
            while (en.Current != ' ' && en.Current != ',')
            {
                col.Add(en.Current);
                en.MoveNext();
            }
            colDef.name = new string(col.ToArray()).Trim();

            if (en.Current == ',')
            {
                throw new Exception("Failed parsing Column Definition. Missing type");
            }
            en.MoveNext();


            col = new List <char>();
            while (en.Current != ' ' && en.Current != ',')
            {
                col.Add(en.Current);
                en.MoveNext();
            }
            colDef.type = new string(col.ToArray()).Trim();

            return(colDef);
        }
Ejemplo n.º 13
0
        public static new CreateTable Parse(Benumerator <char> en)
        {
            if (!en.MoveNext() || en.Current == ' ' && !en.MoveNext())
            {
                throw new Exception("failed to parse. No Input");
            }

            if (en.Current != 'C' || !en.MoveNext() ||
                en.Current != 'R' || !en.MoveNext() ||
                en.Current != 'E' || !en.MoveNext() ||
                en.Current != 'A' || !en.MoveNext() ||
                en.Current != 'T' || !en.MoveNext() ||
                en.Current != 'E' || !en.MoveNext() ||
                en.Current != ' ' || !en.MoveNext() ||
                en.Current != 'T' || !en.MoveNext() ||
                en.Current != 'A' || !en.MoveNext() ||
                en.Current != 'B' || !en.MoveNext() ||
                en.Current != 'L' || !en.MoveNext() ||
                en.Current != 'E' || !en.MoveNext() ||
                en.Current != ' '
                )
            {
                throw new Exception("failed to parse CREATE. Expected 'CREATE TABLE '");
            }

            List <char> tableName = new List <char>();

            while (en.MoveNext() && en.Current != ' ')
            {
                tableName.Add(en.Current);
            }

            if (!en.MoveNext())
            {
                throw new Exception("failed to parse CREATE. Unexpected end up input");
            }

            if (en.Current != '(' || !en.MoveNext())
            {
                throw new Exception("failed to parse CREATE. Ending ')' is missing");
            }

            var colDefs = new List <ColumnDefinition>();

            do
            {
                colDefs.Add(ColumnDefinition.Parse(en));
            } while (en.Current == ',');


            if (en.Current == ' ')
            {
                en.MoveNext();
            }

            if (en.Current != ')')
            {
                throw new Exception("failed to parse CREATE. Ending ')' is missing");
            }

            return(new CreateTable
            {
                tableName = new string(tableName.ToArray()).Trim(),
                columns = colDefs.ToArray()
            });
        }
Ejemplo n.º 14
0
        public static new Insert Parse(Benumerator <char> en)
        {
            if (!en.MoveNext() || en.Current == ' ' && !en.MoveNext())
            {
                throw new Exception("failed to parse. No Input");
            }


            if (!Parser.HasValue(en, "INSERT INTO "))
            {
                throw new Exception("failed to parse INSERT. Expecting 'INSERT INTO '");
            }


            var tableName = Parser.ParseWord(en);

            Parser.SkipWhitespace(en);

            if (!Parser.HasValue(en, "("))
            {
                throw new Exception("failed to parse INSERT. Expecting '('");
            }

            var columns = SelectColumn.Parse(en).ToArray();

            Parser.SkipWhitespace(en);

            if (!Parser.HasValue(en, ")"))
            {
                throw new Exception("failed to parse INSERT. Expecting ')'");
            }
            Parser.SkipWhitespace(en);

            if (!Parser.HasValue(en, "VALUES"))
            {
                throw new Exception("failed to parse INSERT. Expecting ')'");
            }

            List <SelectColumn[]> rows = new List <SelectColumn[]>();

            do
            {
                Parser.SkipWhitespace(en);

                if (!Parser.HasValue(en, "("))
                {
                    throw new Exception("failed to parse INSERT. Expecting ')'");
                }

                Parser.SkipWhitespace(en);

                var values = SelectColumn.Parse(en).ToArray();
                if (values.Length != columns.Length)
                {
                    throw new Exception("failed to parse INSERT. Number of values do not match number of columns");
                }
                rows.Add(values);

                Parser.SkipWhitespace(en);

                if (!Parser.HasValue(en, ")"))
                {
                    throw new Exception("failed to parse INSERT. Expecting ')'");
                }

                Parser.SkipWhitespace(en);
            } while (Parser.HasValue(en, ","));

            return(new Insert
            {
                tableName = tableName,
                columns = columns,
                rows = rows
            });
        }
Ejemplo n.º 15
0
 public static new Get Parse(Benumerator <char> en)
 {
     throw new NotImplementedException();
 }