FLOPPYValue * FLOPPYRecord::filter(FLOPPYNode *node) { FLOPPYValue *ret; if(node->_type == FLOPPYNodeType::ValueNode) { //printf("ValueNode\n"); if(node->value->type() == ValueType::TableAttributeValue) { FLOPPYRecordAttribute *attr = getColByTblAttr(node->value->tableAttribute); if(attr) return attr->val; printf("ERROR - tableAttribute not found\n"); } else return node->value; } else if(node->_type == FLOPPYNodeType::ConditionNode) { //printf("ConditionNode\n"); FLOPPYValue *leftRet = filter(node->node.left); FLOPPYValue *rightRet = NULL; if (node->node.op != FLOPPYNodeOperator::ParenthesisOperator && node->node.op != FLOPPYNodeOperator::NotOperator) { rightRet = filter(node->node.right); } if (node->node.op == FLOPPYNodeOperator::NotOperator) { //printf("\t NotOperator\n"); return leftRet; } if(node->node.op == FLOPPYNodeOperator::AndOperator) { //printf("\t AndOperator\n"); if((leftRet->type() != ValueType::BooleanValue) || (rightRet->type() != ValueType::BooleanValue)) printf("ERROR - AND without bool\n"); else { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = leftRet->bVal && rightRet->bVal; return ret; } } else if(node->node.op == FLOPPYNodeOperator::GreaterThanOperator) { //printf("\t GreaterThanOperator\n"); if(leftRet->type() == rightRet->type()) { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = (FLOPPYRecordAttribute::compareValues(leftRet, rightRet) > 0) ? 1 : 0; return ret; } else printf("ERROR - > with diff types\n"); } else if(node->node.op == FLOPPYNodeOperator::GreaterThanEqualOperator) { //printf("\t GreaterThanEqualOperator\n"); if(leftRet->type() == rightRet->type()) { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = (FLOPPYRecordAttribute::compareValues(leftRet, rightRet) >= 0) ? 1 : 0; return ret; } else printf("ERROR - >= with diff types\n"); } else if(node->node.op == FLOPPYNodeOperator::LessThanOperator) { //printf("\t LessThanOperator\n"); if(leftRet->type() == rightRet->type()) { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = (FLOPPYRecordAttribute::compareValues(leftRet, rightRet) < 0) ? 1 : 0; return ret; } else printf("ERROR - < with diff types\n"); } else if(node->node.op == FLOPPYNodeOperator::LessThanEqualOperator) { //printf("\t LessThanEqualOperator\n"); if(leftRet->type() == rightRet->type()) { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = (FLOPPYRecordAttribute::compareValues(leftRet, rightRet) <= 0) ? 1 : 0; return ret; } else printf("ERROR - <= with diff types\n"); } else if(node->node.op == FLOPPYNodeOperator::EqualOperator) { //printf("\t EqualOperator\n"); if(leftRet->type() == rightRet->type()) { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = (FLOPPYRecordAttribute::compareValues(leftRet, rightRet) == 0) ? 1 : 0; return ret; } else printf("ERROR - == with diff types\n"); } else if(node->node.op == FLOPPYNodeOperator::NotEqualOperator) { //printf("\t NotEqualOperator\n"); if(leftRet->type() == rightRet->type()) { ret = new FLOPPYValue(BooleanValue); tempNodes->push_back(ret); ret->bVal = (FLOPPYRecordAttribute::compareValues(leftRet, rightRet) != 0) ? 1 : 0; return ret; } else printf("ERROR - != with diff types\n"); } else if(node->node.op == FLOPPYNodeOperator::ParenthesisOperator) { //printf("\t ParenthesisOperator\n"); return leftRet; } else { printf("ERROR - operation in condition not found\n"); } } else if (node->_type == FLOPPYNodeType::ExpressionNode) { FLOPPYValue *leftRet = filter(node->node.left); FLOPPYValue *rightRet = NULL; if (node->node.op != FLOPPYNodeOperator::ParenthesisOperator) { rightRet = filter(node->node.right); } if(node->node.op == FLOPPYNodeOperator::PlusOperator) { //printf("\t PlusOperator\n"); if(leftRet->type() == rightRet->type()) { if(leftRet->type() == ValueType::IntValue) { ret = new FLOPPYValue(IntValue); tempNodes->push_back(ret); ret->iVal = leftRet->iVal + rightRet->iVal; return ret; } else if(leftRet->type() == ValueType::FloatValue) { ret = new FLOPPYValue(FloatValue); tempNodes->push_back(ret); ret->fVal = leftRet->fVal + rightRet->fVal; return ret; } } else { printf("ERROR - '+' with diff types\n"); ret = new FLOPPYValue(NullValue); tempNodes->push_back(ret); return ret; } } else if(node->node.op == FLOPPYNodeOperator::MinusOperator) { //printf("\t MinusOperator\n"); if(leftRet->type() == rightRet->type()) { if(leftRet->type() == ValueType::IntValue) { ret = new FLOPPYValue(IntValue); tempNodes->push_back(ret); ret->iVal = leftRet->iVal + rightRet->iVal; return ret; } else if(leftRet->type() == ValueType::FloatValue) { ret = new FLOPPYValue(FloatValue); tempNodes->push_back(ret); ret->fVal = leftRet->fVal + rightRet->fVal; return ret; } } else { printf("ERROR - '-' with diff types\n"); ret = new FLOPPYValue(NullValue); tempNodes->push_back(ret); return ret; } } else if(node->node.op == FLOPPYNodeOperator::TimesOperator) { //printf("\t TimesOperator\n"); if(leftRet->type() == rightRet->type()) { if(leftRet->type() == ValueType::IntValue) { ret = new FLOPPYValue(IntValue); tempNodes->push_back(ret); ret->iVal = leftRet->iVal * rightRet->iVal; return ret; } else if(leftRet->type() == ValueType::FloatValue) { ret = new FLOPPYValue(FloatValue); tempNodes->push_back(ret); ret->fVal = leftRet->fVal * rightRet->fVal; return ret; } } else { printf("ERROR - '*' with diff types\n"); ret = new FLOPPYValue(NullValue); tempNodes->push_back(ret); return ret; } } else if(node->node.op == FLOPPYNodeOperator::DivideOperator) { //printf("\t DivideOperator\n"); if(leftRet->type() == rightRet->type()) { if(leftRet->type() == ValueType::IntValue) { ret = new FLOPPYValue(IntValue); tempNodes->push_back(ret); ret->iVal = leftRet->iVal / rightRet->iVal; return ret; } else if(leftRet->type() == ValueType::FloatValue) { ret = new FLOPPYValue(FloatValue); tempNodes->push_back(ret); ret->fVal = leftRet->fVal / rightRet->fVal; return ret; } } else { printf("ERROR - '/' with diff types\n"); ret = new FLOPPYValue(NullValue); tempNodes->push_back(ret); return ret; } } else if(node->node.op == FLOPPYNodeOperator::ModOperator) { //printf("\t ModOperator\n"); if(leftRet->type() == rightRet->type()) { if(leftRet->type() == ValueType::IntValue) { ret = new FLOPPYValue(IntValue); tempNodes->push_back(ret); ret->iVal = leftRet->iVal % rightRet->iVal; return ret; } else if(leftRet->type() == ValueType::FloatValue) { ret = new FLOPPYValue(FloatValue); tempNodes->push_back(ret); ret->fVal = fmod(leftRet->fVal, rightRet->fVal); return ret; } } else { printf("ERROR - '%%' with diff types\n"); ret = new FLOPPYValue(NullValue); tempNodes->push_back(ret); return ret; } } else if(node->node.op == FLOPPYNodeOperator::ParenthesisOperator) { printf("\t ParenthesisOperator\n"); return leftRet; } else { printf("ERROR - operation in expression not found\n"); } } else if (node->_type == FLOPPYNodeType::AggregateNode) { std::vector<FLOPPYRecordAttribute *>::iterator itr = columns->begin(); while (itr != columns->end()) { if(!(*itr)->isAggregate) { itr++; continue; } if((*itr)->op != node->aggregate.op) { itr++; continue; } if(node->aggregate.op != FLOPPYAggregateOperator::CountStarAggregate) { if(strcmp(node->aggregate.value->sVal, (*itr)->name) != 0) { itr++; continue; } } return (*itr)->val; } } ret = new FLOPPYValue(NullValue); tempNodes->push_back(ret); return ret; }
void insertStatement(FLOPPYInsertStatement *stm) { DiskAddress temp; int recordSize; RecordDesc recordDesc; if (!tableExists(stm->name)) { printf("Table does not exist: %s\n", stm->name); return; } int fd = getFd(stm->name); heapHeaderGetRecordSize(buffer, fd, &recordSize); heapHeaderGetRecordDesc(buffer, fd, &recordDesc); if (stm->values->size() != recordDesc.numFields) { printf("Insert statement has %d values, expected %d.\n", stm->values->size(), recordDesc.numFields); return; } // Check primary and foreign keys. FLOPPYPrimaryKey *pk = new FLOPPYPrimaryKey; vector<FLOPPYForeignKey *> *fks = new vector<FLOPPYForeignKey *>; getKeys(buffer, fd, pk, fks); if (checkKey(stm->values, stm->name, pk->attributes, stm->name, pk->attributes, recordDesc)) { printf("Tuple violates primary key constraint.\n"); return; } for (auto iter = fks->begin(); iter != fks->end(); iter++) { FLOPPYPrimaryKey *refPk = new FLOPPYPrimaryKey; getKeys(buffer, getFd((*iter)->refTableName), refPk, NULL); if (!checkKey(stm->values, stm->name, (*iter)->attributes, (*iter)->refTableName, refPk->attributes, recordDesc)) { printf("Tuple violates foreign key constraint on %s.\n", (*iter)->refTableName); return; } } char *record = new char[recordSize]; memset(record, 0, recordSize); int byte = 0; for (int i = 0; i < stm->values->size(); i++) { FLOPPYValue *value = stm->values->at(i); if (recordDesc.fields[i].type == VARCHAR) { strncpy(&record[byte], value->sVal, recordDesc.fields[i].size); byte += recordDesc.fields[i].size; } else if (recordDesc.fields[i].type == INT) { int remainder = byte % 4; if (remainder) byte += 4 - remainder; int val = (int)value->iVal; memcpy(&record[byte], &val, sizeof(int)); byte += 4; } else if (recordDesc.fields[i].type == BOOLEAN) { int remainder = byte % 4; if (remainder) byte += 4 - remainder; int val = (int)value->bVal; memcpy(&record[byte], &val, sizeof(int)); byte += 4; } else if (recordDesc.fields[i].type == FLOAT || recordDesc.fields[i].type == DATETIME) { int remainder = byte % 8; if (remainder) byte += 8 - remainder; double val = value->type() == IntValue ? value->iVal : value->fVal; memcpy(&record[byte], &val, sizeof(double)); byte += 8; } } insertRecord(buffer, stm->name, record, &temp); printf("Tuple inserted.\n"); int isVolatile; heapHeaderIsVolatile(buffer, fd, &isVolatile); if (isVolatile) volatileFds.insert(fd); }