Exemple #1
0
        /**
         * After checking for error if any, inserts the record into
         * the table and index file
         */
        public void InsertRecordToTable(String tableName, Record record)
        {
            CheckIfDatabaseSelected();
            TableManager tableManager = new TableManager(DbManager.db.Name, tableName);

            tableManager.CheckRecord(record);             //checks for any error in the record
            if (tableManager.table.PrimaryKey != null)
            {
                Column pKey      = tableManager.table.PrimaryKey;
                int    pKeyIndex = tableManager.table.GetColumnIndex(pKey);
                String pKeyVal   = record.Fields[pKeyIndex];

                //checking if record null
                if (pKeyVal == null)
                {
                    throw new Exception("Primary Key Value can't be null");
                }

                //check for duplicacy
                Dictionary <int, Record> records = SelectRecordsFromTable(tableName,
                                                                          new Condition(pKey, Condition.ConditionType.Equal, pKeyVal));
                if (records.Count > 0)
                {
                    throw new Exception("Primary Key Value already exists");
                }
            }
            int address = tableManager.InsertRecord(record);

            tableManager.InsertRecordToIndices(record, address);
        }
Exemple #2
0
        /**
         * @param name="condition" updates records which satisfy the condition
         * if null that means update all
         *
         * @param name="updatedColumns" has those entries set which have been
         * changed and rest of the entries are null
         *
         * Check for error if there in updatedColumns
         * If no error combine with old records to get new and updated records
         */
        public void UpdateRecordToTable(String tableName, Record updatedColumns, Condition condition)
        {
            CheckIfDatabaseSelected();
            TableManager tableManager = new TableManager(DbManager.db.Name, tableName);

            //checking for errors in objects
            tableManager.CheckRecord(updatedColumns);
            if (!tableManager.table.CheckIfConditionValid(condition))
            {
                throw new Exception("Condition is not valid");
            }

            Dictionary <int, Record> oldRecords = SelectRecordsFromTable(tableName, condition);

            if (tableManager.table.PrimaryKey != null)
            {
                Column pKey      = tableManager.table.PrimaryKey;
                int    pKeyIndex = tableManager.table.GetColumnIndex(pKey);
                String pKeyVal   = updatedColumns.Fields[pKeyIndex];

                //checking if more than 1 record will get changed
                if (oldRecords.Count > 1 && pKeyVal != null)
                {
                    throw new Exception("More than 1 record satisfies condition");
                }
            }
            Dictionary <int, Record> newRecords = new Dictionary <int, Record>(oldRecords);

            for (int i = 0; i < updatedColumns.Fields.Count; i++)             //updating new records
            {
                String value = updatedColumns.Fields[i];
                if (value != null)
                {
                    foreach (KeyValuePair <int, Record> pair in newRecords)
                    {
                        pair.Value.Fields[i] = value;
                    }
                }
            }

            tableManager.UpdateRecord(newRecords);
            tableManager.UpdateRecordToIndices(oldRecords, newRecords);
        }