Ejemplo n.º 1
0
        public static SurlyTableResponse GetTable(this SurlyDatabase database, string tableName)
        {
            var table = database.Tables.SingleOrDefault(x => x.Name == tableName.ToUpper());

            if (table != null)
            {
                return new SurlyTableResponse {
                           Table = table
                }
            }
            ;

            var projection = SurlyProjections.GetInstance().Projections
                             .SingleOrDefault(x => x.ProjectionName == tableName.ToUpper());

            if (projection != null)
            {
                return(new SurlyTableResponse
                {
                    Table = new SurlyTable
                    {
                        Name = projection.ProjectionName,
                        Schema = projection.AttributeNames,
                        Tuples = projection.Tuples,
                    },
                    IsProjection = true,
                    HideIndexes = projection.HideIndex
                });
            }

            WriteLine($"\n\t{tableName.ToUpper()} was not found.", Red);

            return(null);
        }
Ejemplo n.º 2
0
        public static void DestroyTable(this SurlyDatabase database, string tableName, string line)
        {
            var tableResponse = database.GetTable(tableName);

            if (tableResponse.IsProjection)
            {
                var projection = SurlyProjections.GetInstance().Projections.Single(x => x.ProjectionName == tableName);
                SurlyProjections.GetInstance().Projections.Remove(projection);
            }
            else
            {
                database.Tables.Remove(tableResponse.Table);
            }

            WriteLine($"\n\tDestroyed {tableName.ToUpper()}", Green);
        }
Ejemplo n.º 3
0
        public static void Select(this SurlyDatabase database, string query)
        {
            _resultSet = new LinkedList <LinkedList <SurlyAttribute> >();
            string tableName, conditions, projectionName = null;
            var    printProjection = false;

            try
            {
                try
                {
                    projectionName = new Regex("(\\w+) = select", RegexOptions.IgnoreCase)
                                     .Match(query)
                                     .Groups[1]
                                     .Captures[0]
                                     .ToString()
                                     .ToUpper();
                }
                catch (Exception)
                {
                    printProjection = true;
                }

                tableName = new Regex("select (\\w+) where", RegexOptions.IgnoreCase)
                            .Match(query)
                            .Groups[1]
                            .Captures[0]
                            .ToString()
                            .ToUpper();

                conditions = new Regex("where (.+);", RegexOptions.IgnoreCase)
                             .Match(query)
                             .Groups[1]
                             .Captures[0]
                             .ToString()
                             .ToUpper();
            }
            catch (Exception)
            {
                WriteLine("Invalid SELECT syntax, please see help", Red);
                return;
            }

            var tableResponse = database.GetTable(tableName);

            if (tableResponse.Table == null)
            {
                WriteLine($"{tableName.ToUpper()} not found.", Red);
                return;
            }

            if (!printProjection &&
                SurlyProjections.GetInstance().Projections.Any(x => x.ProjectionName.ToUpper() == projectionName?.ToUpper()))
            {
                WriteLine($"\n\t{projectionName?.ToUpper()} already exists, please choose a different name", Red);
                return;
            }

            var conditionSteps = conditions.Split(' ').ToList();

            tableResponse.Table.Tuples.ToList().ForEach(tableRow =>
            {
                var valid = OperatorHelper.Chain(tableRow, true, conditionSteps.ToArray(), 0);

                if (valid)
                {
                    var trimmedRow = new LinkedList <SurlyAttribute>(tableRow);

                    var rowId = trimmedRow.SingleOrDefault(x => x.Name == "Id");
                    trimmedRow.Remove(rowId);

                    _resultSet.AddLast(trimmedRow);
                }
            });

            if (_resultSet.Count == 0)
            {
                WriteLine("\n\tQuery yielded no results.", Yellow);
                return;
            }

            var schema = new LinkedList <SurlyAttributeSchema>(tableResponse.Table.Schema);
            var id     = schema.SingleOrDefault(x => x.Name == "Id");

            schema.Remove(id);

            if (printProjection)
            {
                var response = new SurlyTableResponse
                {
                    Table = new SurlyTable
                    {
                        Schema = schema,
                        Name   = "Results",
                        Tuples = _resultSet
                    },
                    //HideIndexes = true
                };

                database.PrintTables(new List <SurlyTableResponse> {
                    response
                });

                return;
            }

            SurlyProjections.GetInstance().Projections.AddLast(new SurlyProjection
            {
                AttributeNames = schema,
                HideIndex      = true,
                ProjectionName = projectionName,
                TableName      = tableResponse.Table.Name,
                Tuples         = _resultSet
            });

            WriteLine($"\n\t{projectionName.ToUpper()} build successful.", Green);
        }