Example #1
0
        // reserve area to store i.e. sector to store blocks containing 5 records
        public void ReserveSectorForTable(String dbPath, String databaseName, String sectorName)
        {
            TableStructure ts   = new TableStructure();
            String         path = dbPath + databaseName + "/" + sectorName;

            Directory.CreateDirectory(path);
            IFormatter formatter = new BinaryFormatter();

            try
            {
                Stream stream = new FileStream(path + "/" + 0 + ".obj", FileMode.Create, FileAccess.Write);
                formatter.Serialize(stream, ts);
                stream.Close();
            }
            catch (IOException i)
            {
            }
        }
Example #2
0
        public Object DeleteRecord(String tableName, dynamic whereField)
        {
            String          useDB              = getDatabaseName();
            TableStructure  ts                 = null;
            TableController to                 = new TableController();
            String          filePath           = "";
            List <String>   whereItemField     = new List <String>();
            ArrayList       whereFieldPosition = null;
            List <String>   whereItemFieldList = new List <String>();
            IFormatter      formatter          = new BinaryFormatter();

            if (whereField != null)
            {
                whereItemFieldList = OperatorSplit(whereField);

                whereItemField.Add(whereItemFieldList[0].Trim());
                String[] whereFieldsArray = new String[whereItemField.Count()];
                whereFieldsArray = whereItemField.ToArray();

                whereFieldPosition = to.FindValuePosition(dbPath, useDB, tableName, whereFieldsArray);
            }
            ArrayList blocks = to.GetBlock(dbPath, useDB, tableName);

            //ArrayList<ArrayList<String>> returnRecords = new ArrayList<ArrayList<String>>();

            foreach (int block in blocks)
            {
                // int newBlock = 0;
                filePath = useDB + "/" + tableName + "/" + block + ".obj";
                try
                {
                    Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    ts = (TableStructure)(formatter.Deserialize(stream));
                    stream.Close();
                }
                catch (Exception ex) {
                    Console.Write(ex.Message.ToString());
                }

                ArrayList rowsToBeDeleted = new ArrayList();
                foreach (ArrayList rows in ts.table)
                {
                    string ind           = whereFieldPosition[0].ToString();
                    string row           = rows[int.Parse(ind)].ToString();
                    string field         = whereItemFieldList[1].ToString().TrimEnd(';');
                    string newWhereField = whereField.ToString().TrimEnd(';');

                    if (whereField != null)
                    {
                        if (Operators(whereField, row, whereItemFieldList[1]))
                        {
                            // returnRecords.add(rows);
                            rowsToBeDeleted.Add(rows);
                        }
                    }
                }
                ts.table.Remove(rowsToBeDeleted);
                try
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                    formatter.Serialize(stream, ts);
                    stream.Close();
                }
                catch (IOException e)
                {
                    Console.Write(e.Message.ToString());
                }
                // if no where clause is present delete all the file in the sector
            }

            // add a new file if no where clause is present
            if (!whereField != null)
            {
                try {
                    ts.table = null;
                    Stream stream = new FileStream(dbPath + useDB + "/" + tableName + "/" + 0 + ".obj", FileMode.Open, FileAccess.Write);
                    formatter.Serialize(stream, ts);
                    stream.Close();
                } catch (IOException ex)
                {
                    // TODO Auto-generated catch block
                    Console.Write(ex.Message.ToString());
                }
            }
            return("Row(s) deleted");
        }
Example #3
0
        public Object UpdateTableValues(String tableName, String setString, dynamic whereField)
        {
            String          useDB              = getDatabaseName();
            TableStructure  ts                 = null;
            TableController tc                 = new TableController();
            List <String>   whereItemField     = new List <string>();
            ArrayList       whereFieldPosition = null;
            List <String>   whereItemFieldList = null;
            IFormatter      formatter          = new BinaryFormatter();

            if (whereField != null)
            {
                whereItemFieldList = OperatorSplit(whereField);

                whereItemField.Add(whereItemFieldList[0]);
                String[] whereFieldsArray = new String[whereItemField.Count];
                whereFieldsArray = whereItemField.ToArray();

                whereFieldPosition = tc.FindValuePosition(dbPath, useDB, tableName, whereFieldsArray);
            }

            ArrayList returnRecords        = null;
            ArrayList columnToSetPositions = new ArrayList();

            String[] setColumnName = setString.Split('=');
            columnToSetPositions = tc.FindValuePosition(dbPath, useDB, tableName, setColumnName);
            int    columnToSetPosition = int.Parse(columnToSetPositions[0].ToString());
            String valueToSet          = setColumnName[1].Replace("'", "");

            foreach (int block in tc.GetBlock(dbPath, useDB, tableName))
            {
                returnRecords = new ArrayList();
                String filePath = dbPath + useDB + "/" + tableName + "/" + block + ".obj";

                try
                {
                    Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    ts = (TableStructure)formatter.Deserialize(stream);
                    stream.Close();
                }
                catch (Exception ex) {
                    Console.Write(ex.Message.ToString());
                }

                // ArrayList<String> returnRows = new ArrayList<String>();
                foreach (ArrayList rows in ts.table)
                {
                    string ind           = whereFieldPosition[0].ToString();
                    string row           = rows[int.Parse(ind)].ToString();
                    string field         = whereItemFieldList[1].ToString().TrimEnd(';');
                    string newWhereField = whereField.ToString().TrimEnd(';');
                    if (whereField != null)
                    {
                        if (Operators(newWhereField, row, whereItemFieldList[1]))
                        {
                            rows.Insert(columnToSetPosition, valueToSet.ToString().TrimEnd(';'));
                        }
                    }
                    else
                    {
                        rows.Insert(columnToSetPosition, valueToSet);
                    }
                    returnRecords.Add(rows);
                }

                ts.table = returnRecords;
                try
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Write);
                    formatter.Serialize(stream, ts);
                    stream.Close();
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message.ToString());
                }
            }
            return("Delete action completed");
        }
Example #4
0
        public ArrayList SelectTableValues(String tableName, String fieldsString, dynamic whereField)
        {
            String          useDB     = getDatabaseName();
            IFormatter      formatter = new BinaryFormatter();
            TableController to        = new TableController();
            // IndexStructure ix = null;
            TableStructure ts = null;
            // TableOperations to = new TableOperations();
            List <String> whereItemField     = new List <string>();
            ArrayList     whereFieldPosition = null;
            String        indexFieldName     = "";
            List <String> whereItemFieldList = null;

            if (!(String.IsNullOrEmpty(whereField)))
            {
                whereItemFieldList = OperatorSplit(whereField);

                whereItemField.Add(whereItemFieldList[0]);
                String[] whereFieldsArray = new String[whereItemField.Count()];
                whereFieldsArray = whereItemField.ToArray();

                whereFieldPosition = to.FindValuePosition(dbPath, useDB, tableName, whereFieldsArray);
            }

            ArrayList returnRecords   = new ArrayList();
            ArrayList columnPositions = new ArrayList();

            String[] columnNames = fieldsString.Split(',');
            columnPositions = to.FindValuePosition(dbPath, useDB, tableName, columnNames);
            int blockPointer  = 0;
            int recordPointer = 0;

            ArrayList blocks = new ArrayList();

            if ((indexFieldName.Length == 0) && whereField != null)
            {
                blocks = to.GetBlock(dbPath, useDB, tableName);
            }
            else
            {
                blocks.Add(blockPointer);
            }

            foreach (int block in blocks)
            {
                String filePath = dbPath + useDB + "/" + tableName + "/" + block + ".obj";
                try
                {
                    Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                    ts = (TableStructure)(formatter.Deserialize(stream));
                    stream.Close();
                }
                catch (IOException i)
                {
                    Console.Write(i.Message.ToString());
                }

                if ((indexFieldName.Length == 0) || (indexFieldName.Equals(null)))
                {
                    ts.table = ts.table;
                }
                else
                {
                    //ArrayList myRow = new ArrayList();
                    var myRow = ts.table[recordPointer];
                    ts.table = null;
                    ts.table.Add(myRow);
                }

                foreach (ArrayList row in ts.table)
                {
                    ArrayList returnRows = new ArrayList();
                    //foreach (int columnPosition in columnPositions)
                    //{
                    if (!String.IsNullOrEmpty(whereField))
                    {
                        string ind           = whereFieldPosition[0].ToString();
                        string rows          = row[int.Parse(ind)].ToString();
                        string field         = whereItemFieldList[1].ToString().TrimEnd(';');
                        string newWhereField = whereField.ToString().TrimEnd(';');

                        if (Operators(newWhereField, rows, field))
                        {
                            returnRows.Add(row);
                            returnRecords.Add(returnRows);
                        }
                    }
                    else
                    {
                        returnRows.Add(row);
                        returnRecords.Add(returnRows);
                    }

                    //}
                    //returnRecords.Add(returnRows);
                }
            }
            returnRecords = SetColumnPosition(columnPositions, returnRecords);
            return(returnRecords);
        }
Example #5
0
        // Select values from specified table

        public String DeleteRecord(string tableName, string whereString)
        {
            //Check if a database is selected
            if (dbPath != null)
            {
                string         filePath = dbPath + "\\" + "mydb" + "\\" + tableName;
                TableStructure ts       = new TableStructure();

                string[] operatorArray = { "<", ">", "=", "!=" };

                //Variables to store where conditions
                string column         = "";
                string chosenOperator = "";
                string value          = "";

                //Loop through the operators array and check if the operator in the whereString exists in the array
                foreach (string ioperator in operatorArray)
                {
                    if (whereString.Contains(ioperator))
                    {
                        chosenOperator = ioperator;
                    }
                }

                //Get the column to traverse
                column = whereString.Split(new string[] { chosenOperator }, StringSplitOptions.None)[0].Trim();

                //Get the value to delete based on
                value = whereString.Split(new string[] { chosenOperator }, StringSplitOptions.None)[1].Trim();


                try
                {
                    //Loop through all data in the table
                    foreach (string block in Directory.EnumerateFiles(filePath, "*.obj", SearchOption.TopDirectoryOnly))
                    {
                        IFormatter formatter = new BinaryFormatter();
                        Stream     stream    = new FileStream(block, FileMode.Open, FileAccess.Read);

                        //Deserialize the block
                        if (stream.Length != 0)
                        {
                            if (File.Exists(block))
                            {
                                ts = (TableStructure)(formatter.Deserialize(stream));
                            }
                        }
                        stream.Close();

                        using (StreamReader reader = new StreamReader(block))
                        {
                            while (reader.ReadLine() != null)
                            {
                                ts.table.Add(reader.ReadLine());
                            }
                        }

                        foreach (ArrayList item in ts.table)
                        {
                            var    strings = item.Cast <string>().ToArray();
                            string test    = string.Join(",", strings);
                            Console.WriteLine(test);
                        }
                    }
                }

                catch (IOException e)
                {
                    Console.WriteLine("IOException: {0}", e);
                }
            }

            //No database selected
            else
            {
                Console.WriteLine("Error! No database selected.");
            }
            return("true");
        }
Example #6
0
        public String InsertIntoTable(String tableName, String valuesString)
        {
            String          returnString     = "";
            String          useDB            = getDatabaseName();
            TableController to               = new TableController();
            IFormatter      formatter        = new BinaryFormatter();
            int             currentBlockSize = (to.GetBlock(dbPath, useDB, tableName).Count);
            int             lastBlockID      = 0;

            if (currentBlockSize < 1)
            {
                lastBlockID = (to.GetBlock(dbPath, useDB, tableName).Count);
            }
            else
            {
                lastBlockID = (to.GetBlock(dbPath, useDB, tableName).Count) - 1;
            }
            String         filePath = dbPath + useDB + "/" + tableName + "/" + lastBlockID + ".obj";
            TableStructure ts       = new TableStructure();

            String[]  valuesList  = valuesString.Split(',');
            ArrayList valuesArray = new ArrayList();

            try
            {
                Stream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

                if (stream.Length != 0)
                {
                    if (File.Exists(filePath))
                    {
                        ts = (TableStructure)(formatter.Deserialize(stream));

                        Console.WriteLine(ts);
                        //stream.Close();
                    }
                }
                stream.Close();
                foreach (String value in valuesList)
                {
                    valuesArray.Add(value.Trim().Replace("'", "").Trim());
                }

                ts.table.Add(valuesArray);

                /**
                 * check if block contains maximum number of elements if it does
                 * create a data block by incrementing lastDatablock id by one
                 * otherwise write to the current data block
                 */
                if ((ts.table.Count - 1) == 5)
                {
                    ts.table.Clear();
                    ts.table.Add(valuesArray);
                    int nextBlockID = lastBlockID + 1;
                    filePath = dbPath + useDB + "/" + tableName + "/" + nextBlockID + ".obj";
                }
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                stream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                formatter.Serialize(stream, ts);
                stream.Close();

                returnString = "1 record was inserted";
            }
            catch (Exception e) {
                // TODO Auto-generated catch block
                e.Message.ToString();
            }

            return(returnString);
        }