Esempio n. 1
0
        public void createTable(string tableName, string[] attributeNames, string[] attributeTypes, int[] attributeLengths, bool[] nullable)
        {
            List<ColumnProperties> columns = new List<ColumnProperties>();

            //Add the desired amount of columns
            for (int i = 0; i < attributeTypes.Length; i++)
                columns.Add(new ColumnProperties(attributeNames[i], attributeTypes[i], attributeLengths[i], nullable[i]));

            //Grab a new relation with no rows and add it to "Tables"
            Relation table = new Relation(columns);
            Tables.Add(Tuple.Create<string, Relation>(tableName, table));
        }
Esempio n. 2
0
        public void createTable(string tableName, string[] attributeNames, string[] attributeTypes,
            int[] attributeLengths, bool[] nullable)
        {
            var columns =
                attributeTypes.Select(
                    (t, i) => new ColumnProperties(attributeNames[i], t, attributeLengths[i], nullable[i])).ToList();

            //Add the desired amount of columns

            //Grab a new relation with no rows and add it to "Tables"
            var table = new Relation(columns);
            Tables.Add(Tuple.Create(tableName, table));
        }
Esempio n. 3
0
        public Relation union(string table1, string table2, string column1, string column2)
        {
            var tempColumns = new List<ColumnProperties>();
            var tempColumnIndex1 = 0;
            var tempColumnIndex2 = 0;

            var tempTable1 = Tables.FirstOrDefault(x => x.Item1 == table1);
            foreach (var column in tempTable1.Item2.Columns)
            {
                tempColumns.Add(column);
                if (column.Name.Equals(column1))
                {
                    tempColumnIndex1 = tempTable1.Item2.Columns.IndexOf(column);
                }
            }
            var tempTable2 = Tables.FirstOrDefault(x => x.Item1 == table2);
            foreach (var column in tempTable2.Item2.Columns)
            {
                tempColumns.Add(column);
                if (column.Name.Equals(column2))
                {
                    tempColumnIndex2 = tempTable2.Item2.Columns.IndexOf(column);
                }
            }

            Relation tempRelation = new Relation(tempColumns);

            foreach (var row1 in tempTable1.Item2.Rows)
            {
                var tempRowsFromTable = new List<Row>();
                for (int i = 0; i < tempTable2.Item2.Rows.Count; i++)
                {
                    if (row1.Cellsssss[tempColumnIndex1].ToString().Equals(tempTable2.Item2.Rows[i].Cellsssss[tempColumnIndex2].ToString()))
                    {
                        tempRowsFromTable.Add(tempTable2.Item2.Rows[i]);
                    }
                }
                //var temp = tempTable2.Item2.Rows.Where(x => x.Cellsssss[tempColumnIndex2] == row1.Cellsssss[tempColumnIndex1]);
                foreach (var table in tempRowsFromTable)
                {
                    var tempRow = new Row(tempRelation.Columns.Count);
                    for (int i = 0; i < row1.Cellsssss.Length; i++)
                    {
                        tempRow.Cellsssss[i] = row1.Cellsssss[i];
                    }
                    for (int i = 0, j = row1.Cellsssss.Length; i < table.Cellsssss.Length; i++, j++)
                    {
                        tempRow.Cellsssss[j] = table.Cellsssss[i];
                    }
                    tempRelation.Rows.Add(tempRow);
                }
            }
            return tempRelation;
        }
Esempio n. 4
0
        public Relation select(string tableName, string column, string value)
        {
            var tempColumns = new List<ColumnProperties>();
            var tempColumnIndex = 0;
            var table = Tables.FirstOrDefault(x => x.Item1.ToLower() == tableName);
            if (table == null) return null;

            foreach (var columnz in table.Item2.Columns)
            {
                tempColumns.Add(columnz);
                if (columnz.Name.Equals(column))
                {
                    tempColumnIndex = table.Item2.Columns.IndexOf(columnz);
                }
            }
            Relation tempRelation = new Relation(tempColumns);
            var selectedRelation =
                table.Item2.Rows.Where(x => x.Cellsssss[tempColumnIndex].ToString().ToLower() == value);
            foreach (var row in selectedRelation)
            {
                tempRelation.Rows.Add(row);
            }
            return tempRelation;
        }
Esempio n. 5
0
        public void print(Relation rel)
        {
            //For nice formatting
            Console.WriteLine("\n____________________________________________________________________");
            if (rel == null) return;
            //Go through all the columns and format it for the console
            for (int y = 0; y < rel.Columns.Count(); y++)
                Console.Write("{0, " + (2 * y + 7) + "} | ", rel.Columns[y].Name);

            Console.WriteLine("\n____________________________________________________________________");

            foreach (var row in rel.Rows)
            {
                //do the same for all the cells in the row
                for (int j = 0; j < row.Cellsssss.Count(); j++)
                {
                    try
                    {
                        Console.Write("{0, " + (2 * j + 7) + "} | ", row.Cellsssss[j].ToString());
                    }
                    catch //This only happens when there is a null value in the table
                    {
                        Console.Write("{0, " + (2 * j + 7) + "} |", "Null");
                    }
                }
                //Write a line for nicer formatting on the console
                Console.WriteLine();
            }
        }
Esempio n. 6
0
        public Tuple<string, Relation> select(string tableName, string column, string value)
        {
            var tempColumns = new List<ColumnProperties>();
            var tempColumnIndex = 0;
            var table = Tables.FirstOrDefault(x => x.Item1.ToLower() == tableName);

            //Grab the index of the columns for new relation
            foreach (var columnz in table.Item2.Columns)
            {
                tempColumns.Add(columnz);
                if (columnz.Name.ToUpper().Equals(column.ToUpper()))
                {
                    tempColumnIndex = table.Item2.Columns.IndexOf(columnz);
                }
            }
            var tempRelation = new Relation(tempColumns);

            //find the condition from the index and get the list of rows that will need to be selected
            var selectedRelation =
                table.Item2.Rows.Where(x => x.Cellsssss[tempColumnIndex].ToString().ToLower().Equals(value.ToLower()));
            foreach (var row in selectedRelation)
            {
                tempRelation.Rows.Add(row);
            }
            return new Tuple<string, Relation>(table.Item1, tempRelation);
        }
Esempio n. 7
0
        public Relation union(string table1, string table2, string column1, string column2)
        {
            var tempColumns = new List<ColumnProperties>();
            var tempColumnIndex1 = 0;
            var tempColumnIndex2 = 0;

            var tempTable1 = Tables.FirstOrDefault(x => x.Item1.ToLower() == table1.ToLower());
            if (tempTable1 != null)
            {
                //Get the index of the columns and add it to the tempColumns list
                foreach (var column in tempTable1.Item2.Columns)
                {
                    tempColumns.Add(column);
                    if (column.Name.Equals(column1))
                    {
                        tempColumnIndex1 = tempTable1.Item2.Columns.IndexOf(column);
                    }
                }
                var tempTable2 = Tables.FirstOrDefault(x => x.Item1.ToLower() == table2.ToLower());
                if (tempTable2 != null)
                {
                    //get the index of the second table and put it to the list also
                    foreach (var column in tempTable2.Item2.Columns)
                    {
                        tempColumns.Add(column);
                        if (column.Name.Equals(column2))
                        {
                            tempColumnIndex2 = tempTable2.Item2.Columns.IndexOf(column);
                        }
                    }

                    //Make a new relation from the list
                    var tempRelation = new Relation(tempColumns);

                    //Grab all the rows that match and put it to the relation 
                    foreach (var row1 in tempTable1.Item2.Rows)
                    {
                        //Grab all the rows from table 2
                        var tempRowsFromTable = new List<Row>();
                        for (var i = 0; i < tempTable2.Item2.Rows.Count; i++)
                        {
                            if (
                                row1.Cellsssss[tempColumnIndex1].ToString()
                                    .Equals(tempTable2.Item2.Rows[i].Cellsssss[tempColumnIndex2].ToString()))
                            {
                                tempRowsFromTable.Add(tempTable2.Item2.Rows[i]);
                            }
                        }

                        //Find all the values that match and put it to the relation
                        foreach (var table in tempRowsFromTable)
                        {
                            var tempRow = new Row(tempRelation.Columns.Count);
                            for (var i = 0; i < row1.Cellsssss.Length; i++)
                            {
                                tempRow.Cellsssss[i] = row1.Cellsssss[i];
                            }
                            for (int i = 0, j = row1.Cellsssss.Length; i < table.Cellsssss.Length; i++, j++)
                            {
                                tempRow.Cellsssss[j] = table.Cellsssss[i];
                            }
                            tempRelation.Rows.Add(tempRow);
                        }
                    }
                    return tempRelation;
                }
            }
            return null;
        }