RC evalExpr(Record *record, Schema *schema, Expr *expr, Value **result) { Value *lIn; Value *rIn; MAKE_VALUE(*result, DT_INT, -1); switch (expr->type) { case EXPR_OP: { Operator *op = expr->expr.op; bool twoArgs = (op->type != OP_BOOL_NOT); // lIn = (Value *) malloc(sizeof(Value)); // rIn = (Value *) malloc(sizeof(Value)); CHECK(evalExpr(record, schema, op->args[0], &lIn)); if (twoArgs) CHECK(evalExpr(record, schema, op->args[1], &rIn)); switch (op->type) { case OP_BOOL_NOT: CHECK(boolNot(lIn, *result)) ; break; case OP_BOOL_AND: CHECK(boolAnd(lIn, rIn, *result)) ; break; case OP_BOOL_OR: CHECK(boolOr(lIn, rIn, *result)) ; break; case OP_COMP_EQUAL: CHECK(valueEquals(lIn, rIn, *result)) ; break; case OP_COMP_SMALLER: CHECK(valueSmaller(lIn, rIn, *result)) ; break; default: break; } // cleanup freeVal(lIn); if (twoArgs) freeVal(rIn); } break; case EXPR_CONST: CPVAL(*result, expr->expr.cons); break; case EXPR_ATTRREF: free(*result); CHECK(getAttr(record, schema, expr->expr.attrRef, result)) ; break; } return RC_OK; }
void changeDataType(char *string, Record *rec, Schema *s, int index) { Value *value; char *tempString; bool boolVal; int intVal; float floatVal; switch (s->dataTypes[index]) { case DT_STRING: MAKE_STRING_VALUE(value, string); setAttributeVal(rec, s, index, value,10); break; case DT_BOOL: if (string[0] == 't') boolVal = TRUE; else boolVal = FALSE; MAKE_VALUE(value, DT_BOOL, boolVal); setAttributeVal(rec, s, index, value,10); break; case DT_INT: intVal = strtol(string, &tempString, 10); MAKE_VALUE(value, DT_INT, intVal); setAttributeVal(rec, s, index, value,10); break; case DT_FLOAT: floatVal = strtof(string, NULL); MAKE_VALUE(value, DT_FLOAT, floatVal); setAttributeVal(rec, s, index, value,10); break; } freeVal(value); }
RC freeExpr(Expr *expr) { switch (expr->type) { case EXPR_OP: { Operator *op = expr->expr.op; switch (op->type) { case OP_BOOL_NOT: freeExpr(op->args[0]); break; default: freeExpr(op->args[0]); freeExpr(op->args[1]); break; } free(op->args); free(op); } break; case EXPR_CONST: freeVal(expr->expr.cons); break; case EXPR_ATTRREF: break; } free(expr); return RC_OK; }
/** * Writes updated record to table * * rel = table handle * record = updated record to be written * */ RC updateRecord(RM_TableData *rel, Record *record) { Value* val; bool pk = false; //Sanity Checks if (rel == NULL) { THROW(RC_INVALID_HANDLE, "Table handle is invalid"); } if (record == NULL) { THROW(RC_INVALID_HANDLE, "Record handle is invalid"); } // Check if this table has primary key defined if (rel->schema->keySize == 1) { pk = true; // ensure pk constraint is not violated getAttr(record, rel->schema, rel->schema->keyAttrs[0], &val); if (checkIfPKExists(rel->name, ((RM_TableMgmtData *) rel->mgmtData)->tblNameSize, val->v.intV)) { // this value already exists freeVal(val); THROW(RC_DUPLICATE_KEY, "Can not insert duplicate key"); } } //Write updated record RC rc = writeRecord(rel, record); // add the primary key in the index file if (pk) { addPrimaryKey(rel->name, ((RM_TableMgmtData *) rel->mgmtData)->tblNameSize, val->v.intV, record->id); freeVal(val); } return rc; }
Record * testRecord(Schema *schema, int a, char *b, int c) { Record *result; Value *value; TEST_CHECK(createRecord(&result, schema)); MAKE_VALUE(value, DT_INT, a); TEST_CHECK(setAttr(result, schema, 0, value)); freeVal(value); MAKE_STRING_VALUE(value, b); TEST_CHECK(setAttr(result, schema, 1, value)); freeVal(value); MAKE_VALUE(value, DT_INT, c); TEST_CHECK(setAttr(result, schema, 2, value)); freeVal(value); return result; }
void testRecords (void) { TestRecord expected[] = { {1, "aaaa", 3}, }; Schema *schema; Record *r; Value *value; testName = "test creating records and manipulating attributes"; // check attributes of created record schema = testSchema(); r = fromTestRecord(schema, expected[0]); getAttr(r, schema, 0, &value); printf("\n Value is %d",value->v.intV); OP_TRUE(stringToValue("i1"), value, valueEquals, "first attr"); freeVal(value); getAttr(r, schema, 1, &value); OP_TRUE(stringToValue("saaaa"), value, valueEquals, "second attr"); freeVal(value); getAttr(r, schema, 2, &value); OP_TRUE(stringToValue("i3"), value, valueEquals, "third attr"); freeVal(value); //modify attrs setAttr(r, schema, 2, stringToValue("i4")); getAttr(r, schema, 2, &value); OP_TRUE(stringToValue("i4"), value, valueEquals, "third attr after setting"); freeVal(value); freeRecord(r); TEST_DONE(); }
Record * testRecord(Schema *schema, int a, char *b, int c) { //printf("\n in here test"); Record *result; Value *value; TEST_CHECK(createRecord(&result, schema)); printf("\n created record"); MAKE_VALUE(value, DT_INT, a); TEST_CHECK(setAttr(result, schema, 0, value)); freeVal(value); MAKE_STRING_VALUE(value, b); TEST_CHECK(setAttr(result, schema, 1, value)); freeVal(value); MAKE_VALUE(value, DT_INT, c); TEST_CHECK(setAttr(result, schema, 2, value)); // printf("\n outta here %s",result->data); freeVal(value); return result; }
char* deserializeRecordKey(char *desiralize_record_str, Schema *schema) { int i, lastAttr = schema->numAttr-1; int intVal; float floatVal; bool boolVal; Value *value; Record *record = (Record*)malloc(sizeof(Record*)); record->data = (char*)malloc(sizeof(char*)); char *splitStart, *splitEnd; char *resultRecord; splitStart = strtok(desiralize_record_str,"("); strcpy(resultRecord,""); for(i=0;i< schema->numAttr;i++) { splitEnd = strtok(NULL,":"); if(i == lastAttr) { splitEnd = strtok(NULL,")"); } else { splitEnd = strtok(NULL,","); } switch(schema->dataTypes[i]) { case DT_INT: intVal = strtol(splitEnd, &splitStart, 10); strcat(resultRecord,splitEnd); strcat(resultRecord,","); MAKE_VALUE(value,DT_INT,intVal); setAttr(record,schema,i,value); free(value); break; case DT_FLOAT: floatVal = strtof(splitEnd, NULL); strcat(resultRecord,splitEnd); strcat(resultRecord,","); MAKE_VALUE(value,DT_FLOAT,floatVal); setAttr(record,schema,i,value); free(value); break; case DT_BOOL: boolVal = (splitEnd[0] == 't') ? TRUE: FALSE; strcat(resultRecord,splitEnd); strcat(resultRecord,","); MAKE_VALUE(value,DT_BOOL,boolVal); setAttr(record,schema,i,value); free(value); break; case DT_STRING: MAKE_STRING_VALUE(value,splitEnd); strcat(resultRecord,splitEnd); strcat(resultRecord,","); setAttr (record,schema,i,value); freeVal(value); break; } } return resultRecord; }
/* * AUTHOR: Nikhil * DESC: Parse the sql stmts and execute the respective methods */ int main(int argc, char *argv[]){ char readline[ARRAY_LENGTH]; char temparray[ARRAY_LENGTH]; char *temp; char *temp1; char *tableName; char *length; int charLength; int numattr=0,k,count=0,numTuples=0; Schema *schema; RID *rids; char columns[ARRAY_LENGTH][ARRAY_LENGTH]; int i=0,j=0; if(fork() == 0) { execv("/usr/bin/clear", argv); exit(1); } else wait(NULL); printf("\n SQL parser\n"); while(1){ printf("\n >>"); fflush(stdin); gets(readline); memcpy(temparray,readline,sizeof(readline)); if(strstr(readline,"create table") || strstr(readline,"CREATE TABLE")){ parser(readline,argv); tableName=strtok(argv[2],"("); temp=strstr(temparray,"("); temp++; temp1=strtok(temp,","); while(temp1!=NULL){ strcpy(columns[i],temp1); temp1=strtok(NULL,","); i++; } j=i; numattr=j; strcpy(columns[i-1],strtok(columns[i-1],")")); char* colname[numattr]; char* datatype[numattr]; DataType dt[numattr]; int sizes[numattr]; int keyattr[numattr]; for(i=0;i<j;i++){ parser(columns[i],argv); colname[i]=argv[0]; datatype[i]=argv[1]; if(strstr(Upper(datatype[i]),"VARCHAR")) { dt[i]=DT_STRING; length=strtok(datatype[i],"("); length=strtok(NULL,"("); charLength=strtok(length,")"); if(length==NULL){ printf("\nWrong Format"); break; } sizes[i]=atoi(charLength); } if(strstr(Upper(datatype[i]),"INT")){ dt[i]=DT_INT; } else if(strstr(Upper(datatype[i]),"FLOAT")){ dt[i]=DT_FLOAT; } else if(strstr(Upper(datatype[i]),"BOOL")){ dt[i]=DT_BOOL; } else{ sizes[i]=0; } if(strstr(Upper(datatype[i]),"PRIMARYKEY")){ keyattr[i]=1; } else{ keyattr[i]=0; } } for(k=0;k<sizeof(keyattr)/sizeof(keyattr[0]);k++){ if(keyattr[i]==1){ count++; } } if(count>0){ printf("wrong format...cannot contain more than one primary key"); break; } schema=testSchema(numattr,colname,dt,sizes,keyattr); initRecordManager(NULL); createTable(Upper(tableName),schema); printf("Table Created Successfully"); shutdownRecordManager(); } if(strstr(Upper(readline),"INSERT INTO")){ parser(readline,argv); tableName=strtok(argv[2],"("); temp=strtok(temparray,"("); temp=strtok(NULL,"("); strcpy(columns[0],temp); strcpy(columns[0],strtok(columns[0],")")); char* values[i]; i=0; values[0]=strtok(columns[0],","); while(values[i]!=NULL){ i++; values[i]=strtok(NULL,","); } numTuples=i; rids = (RID *) malloc(sizeof(RID) * numTuples); RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData)); initRecordManager(NULL); int fd = open(Upper(tableName), O_WRONLY); if(fd<0){ printf("Database Doesn't Exist"); break; } openTable(table, tableName); schema=table->schema; Record *result; Value *value; createRecord(&result, schema); for(i = 0; i < numTuples; i++) { if(schema->dataTypes[i]==DT_INT || schema->dataTypes[i]==DT_FLOAT || schema->dataTypes[i]==DT_BOOL){ MAKE_VALUE(value, schema->dataTypes[i], atoi(values[i])); } if(schema->dataTypes[i]==DT_STRING){ MAKE_STRING_VALUE(value, values[i]); } setAttr(result, schema, i, value); freeVal(value); } insertRecord(table,result); rids[i] = result->id; printf("Inserted Successfully"); closeTable(table); shutdownRecordManager(); } if(strstr(Upper(readline),"UPDATE TABLE")){ parser(readline,argv); tableName=strtok(argv[2],"("); temp1=strtok(temparray,"("); temp=strtok(NULL,"("); strcpy(columns[0],temp); strcpy(columns[0],strtok(columns[0],")")); temp1=strtok(temp1,"="); temp1=strtok(NULL,"="); char* values[i]; i=0; values[0]=strtok(columns[0],","); while(values[i]!=NULL){ i++; values[i]=strtok(NULL,","); } numTuples=i; rids = (RID *) malloc(sizeof(RID) * numTuples); RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData)); initRecordManager(NULL); int fd = open(Upper(tableName), O_WRONLY); if(fd<0){ printf("Database Doesn't Exist"); break; } openTable(table, Upper(tableName)); schema=table->schema; Record *result; Value *value; createRecord(&result, schema); for(i = 0; i < numTuples; i++) { if(schema->dataTypes[i]==DT_INT || schema->dataTypes[i]==DT_FLOAT || schema->dataTypes[i]==DT_BOOL){ MAKE_VALUE(value, schema->dataTypes[i], atoi(values[i])); } if(schema->dataTypes[i]==DT_STRING){ MAKE_STRING_VALUE(value, values[i]); } setAttr(result, schema, i, value); freeVal(value); } result->id=rids[i] ; updateRecord(table,result); printf("Updated Successfully"); closeTable(table); shutdownRecordManager(); } if(strstr(Upper(readline),"DELETE FROM")){ parser(readline,argv); tableName=strtok(argv[2],"("); temp=strtok(temparray,"("); temp=strtok(NULL,"("); strcpy(columns[0],temp); strcpy(columns[0],strtok(columns[0],")")); int r_id=atoi(columns[0]); numTuples=i; rids = (RID *) malloc(sizeof(RID) * numTuples); RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData)); initRecordManager(NULL); int fd = open(Upper(tableName), O_WRONLY); if(fd<0){ printf("Database Doesn't Exist"); break; } openTable(table, Upper(tableName)); schema=table->schema; deleteRecord(table,rids[r_id]); printf("Deleted Successfully"); closeTable(table); shutdownRecordManager(); } } }
/** * Inserts new record into the table * * rel = table handle * record = record to be inserted * */ RC insertRecord(RM_TableData *rel, Record *record) { Value* val = NULL; bool pk = false; //Sanity Checks if (rel == NULL) { THROW(RC_INVALID_HANDLE, "Table handle is invalid"); } if (record == NULL) { THROW(RC_INVALID_HANDLE, "Record handle is invalid"); } // Check if this table has primary key defined if (rel->schema->keySize == 1) { pk = true; // ensure pk constraint is not violated getAttr(record, rel->schema, rel->schema->keyAttrs[0], &val); if (checkIfPKExists(rel->name, ((RM_TableMgmtData *) rel->mgmtData)->tblNameSize, val->v.intV)) { // this value already exists freeVal(val); THROW(RC_DUPLICATE_KEY, "Can not insert duplicate key"); } } //Check if all slots in last page are full, if yes, add new page if (((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot.slot == ((RM_TableMgmtData *) rel->mgmtData)->slotCapacityPage) { //Add new page BM_PageHandle *page = (BM_PageHandle *) malloc(sizeof(BM_PageHandle)); if (page == NULL) { THROW(RC_NOT_ENOUGH_MEMORY, "Not enough memory available for resource allocation"); } //Request +1th page pinPage(((RM_TableMgmtData *) rel->mgmtData)->bPool, page, ((RM_TableMgmtData *) rel->mgmtData)->pageCount); //Mark it dirty markDirty(((RM_TableMgmtData *) rel->mgmtData)->bPool, page); unpinPage(((RM_TableMgmtData *) rel->mgmtData)->bPool, page); free(page); ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot.page = ((RM_TableMgmtData *) rel->mgmtData)->pageCount++; //Point first free slot to first slot in new page ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot.slot = 0; } //Point new record to first free slot and page available record->id.page = ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot.page; record->id.slot = ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot.slot++; //Write new record to its assigned page and slot RC rc = writeRecord(rel, record); // add the primary key in the index file if (pk) { //Update primary key index addPrimaryKey(rel->name, ((RM_TableMgmtData *) rel->mgmtData)->tblNameSize, val->v.intV, record->id); freeVal(val); } //All OK if (rc == RC_OK) ((RM_TableMgmtData *) rel->mgmtData)->tupleCount++; return rc; }
/** * This API allows conditional updates of the records * * rel = Table handle * cond = Conditional expression to be matched * op = Function pointer pointing to update function to be called in case a matching record is found */ RC updateScan(RM_TableData *rel, Expr *cond, void (*op)(Schema*, Record *)) { //Sanity Checks if (rel == NULL) { THROW(RC_INVALID_HANDLE, "Table handle is invalid"); } // Get total number of pages this table has int pages = ((RM_TableMgmtData *) rel->mgmtData)->pageCount; RID firstFreeSlot = ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot; int i, j, tuplesRead, slot; RID id; Value *result; Record* r; BM_PageHandle *pageData = (BM_PageHandle *) malloc(sizeof(BM_PageHandle)); if (pageData == NULL) { THROW(RC_NOT_ENOUGH_MEMORY, "Not enough memory available for resource allocation"); } j = 0; tuplesRead = 0; // loop through pages and get the desired records for (i = 1; i < pages; i++) { slot = 0; id.page = i; while (slot < ((RM_TableMgmtData *) rel->mgmtData)->slotCapacityPage) { createRecord(&r, rel->schema); id.slot = slot; RC rc = readRecord(rel, id, &r); if (rc != RC_OK) { THROW(RC_REC_MGR_DELETE_REC_FAILED, "Delete record failed"); } // ignore the deleted records (tombstones) and free slots if ((!(r->nullMap & (1 << 15)) || ((firstFreeSlot.page == i) && (firstFreeSlot.slot == slot)))) { // check if record satisfies the given condition evalExpr(r, rel->schema, cond, &result); if (result->v.boolV) { op(rel->schema, r); updateRecord(rel, r); j++; tuplesRead++; } freeVal(result); } freeRecord(r); slot++; } } free(pageData); //All OK return RC_OK; }
/** * Starts scan to fetch records with matching condition * * rel = table handle * scan = scan handle * cond = condition to be matched */ RC startScan(RM_TableData *rel, RM_ScanHandle *scan, Expr *cond) { //Sanity Checks if (rel == NULL) { THROW(RC_INVALID_HANDLE, "Table handle is invalid"); } // Get total number of pages this table has int pages = ((RM_TableMgmtData *) rel->mgmtData)->pageCount; int tupleCount = ((RM_TableMgmtData *) rel->mgmtData)->tupleCount; RID firstFreeSlot = ((RM_TableMgmtData *) rel->mgmtData)->firstFreeSlot; int i, j, tuplesRead, slot; RID id; Value* result; Record** r; BM_PageHandle *pageData = (BM_PageHandle *) malloc(sizeof(BM_PageHandle)); RM_ScanIterator *iter = (RM_ScanIterator*) malloc(sizeof(RM_ScanIterator)); if (pageData == NULL) { THROW(RC_NOT_ENOUGH_MEMORY, "Not enough memory available for resource allocation"); } scan->rel = rel; iter->totalRecords = 0; iter->lastRecordRead = -1; iter->records = (Record**) malloc(sizeof(Record*) * tupleCount); scan->mgmtData = (RM_ScanIterator*) iter; r = iter->records; for (i = 0; i < tupleCount; ++i) { r[i] = NULL; } j = 0; tuplesRead = 0; //Loop through pages and get the desired records for (i = 1; i < pages; i++) { slot = 0; id.page = i; while (slot < ((RM_TableMgmtData *) rel->mgmtData)->slotCapacityPage) { createRecord(&r[j], rel->schema); id.slot = slot; RC rc = readRecord(rel, id, &r[j]); if (rc != RC_OK) { THROW(RC_REC_MGR_DELETE_REC_FAILED, "Delete record failed"); } // ignore the deleted records (tombstones) if (!(r[j]->nullMap & (1 << 15)) && !(firstFreeSlot.page == i && firstFreeSlot.slot == slot)) { evalExpr(r[j], rel->schema, cond, &result); if (result->v.boolV) { j++; tuplesRead++; } freeVal(result); } slot++; freeRecord(r[j]); } } iter->totalRecords = j; free(pageData); //All OK return RC_OK; }
int main (void) { initRecordManager(NULL); int in; int cl = 0; while(cl == 0){ printf("What to do:\n1.Add new table\n2.insert\n3.update\n4.delete\n5.scan\n6.print table\n7.close\n"); in=0; scanf("%d",&in); RM_TableData *table; Record *result,*r; Value *value,*v; int i,j; switch(in) { case 0: printf("invalid input\n"); break; case 1: printf("\nAdding new table:\n"); printf("Table name: "); char tbn[50]; scanf("%s",tbn); char* tbnp = (char *) malloc(sizeof(char)*sizeof(tbn)); memcpy(tbnp,tbn,sizeof(tbn)); printf("number of attributes: "); int nat = 0; scanf("%d",&nat); printf("number of keys: "); int nk = 0; scanf("%d",&nk); Schema *sch; char **names = (char **) malloc(sizeof(char*) * nat); DataType* dt = (DataType *) malloc(sizeof(DataType) * nat); int* sizes = (int *) malloc(sizeof(int) * nat); int* keys = (int *) malloc(sizeof(int) * nk); int kptr = 0; for(i=0;i<nat;i++){ printf("Attribute %d name: ", i+1); char buf[100]; scanf("%s", buf); char* tmp = (char *) malloc(sizeof(char)*sizeof(buf)); memcpy(tmp,buf,sizeof(buf)); names[i] = tmp; printf("Attribute %d datatype: [I/S/F/B]: ", i+1); char dtp; scanf(" %c", &dtp); //printf("\ndtp = %c", dtp); switch(dtp) { case 'I': dt[i] = DT_INT; sizes[i] = 0; break; case 'S': dt[i] = DT_STRING; printf("Attribute %d length: ", i+1); scanf("%d",&sizes[i]); printf("length is: %d\n", sizes[i]); break; case 'F': dt[i] = DT_FLOAT; sizes[i] = 0; break; case 'B': dt[i] = DT_BOOL; sizes[i] = 0; break; } printf("Is Attribute %d a key?: [Y/N] ", i+1); char isk; scanf(" %c", &isk); if(isk == 'Y'){ keys[kptr] = i; kptr++; } //printf("schema add: %s,%d,%c\n",names[i],sizes[i],isk); } sch = createSchema(nat,names,dt,sizes,nk,keys); createTable(tbnp,sch); break; case 2: printf("Insert record\n"); printf("table name: "); char buf2[100]; scanf("%s", buf2); char* tmp2 = (char *) malloc(sizeof(char)*sizeof(buf2)); memcpy(tmp2,buf2,sizeof(buf2)); table = (RM_TableData *) malloc(sizeof(RM_TableData)); openTable(table, tmp2); createRecord(&result, table->schema); for(i=0;i<table->schema->numAttr;i++){ if(table->schema->dataTypes[i] == DT_INT) { printf("intput int attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); int intt = 0; scanf("%d",&intt); //printf("\n%d is %d\n", i, intt); MAKE_VALUE(value,DT_INT,intt); setAttr(result, table->schema, i, value); freeVal(value); } else if (table->schema->dataTypes[i] == DT_STRING) { printf("intput string attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); char instrbuf[100]; scanf("%s",instrbuf); char* instr = (char *) malloc(sizeof(char)*sizeof(instrbuf)); memcpy(instr,instrbuf,sizeof(instrbuf)); MAKE_STRING_VALUE(value,instr); setAttr(result, table->schema, i, value); freeVal(value); } else if (table->schema->dataTypes[i] == DT_FLOAT) { printf("intput float attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); float infl; scanf("%fl",&infl); MAKE_VALUE(value,DT_FLOAT,infl); setAttr(result, table->schema, i, value); freeVal(value); } else if (table->schema->dataTypes[i] == DT_BOOL) { printf("intput boolean attr[%d/%d] value -- %s [T/F]: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); char inbl; scanf(" %c",&inbl); if(inbl == 'T'){ MAKE_VALUE(value,DT_BOOL,true); } else if (inbl == 'F'){ MAKE_VALUE(value,DT_BOOL,false); } setAttr(result, table->schema, i, value); freeVal(value); } if(i==table->schema->numAttr-1){ printf("\n"); } } insertRecord(table,result); break; case 3: printf("update record\n"); printf("table name: "); char buf3[100]; scanf("%s", buf3); char* tmp3 = (char *) malloc(sizeof(char)*sizeof(buf3)); memcpy(tmp3,buf3,sizeof(buf3)); table = (RM_TableData *) malloc(sizeof(RM_TableData)); openTable(table, tmp3); createRecord(&result, table->schema); r = (Record *) malloc(sizeof(Record)); RM_ScanHandle *sc = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle)); startScan(table, sc, NULL); printf("Select record to update: \n"); j=0; int rids[100][2]; while(next(sc, r) == RC_OK){ printf("(%d) ",j+1); rids[j][0] = r->id.page; rids[j][1] = r->id.slot; for(i=0;i<table->schema->numAttr;i++){ if(table->schema->dataTypes[i] == DT_INT) { getAttr(r,table->schema,i,&v); printf("%-15d",v->v.intV); } else if(table->schema->dataTypes[i] == DT_STRING) { getAttr(r,table->schema,i,&v); printf("%-15s",v->v.stringV); } else if(table->schema->dataTypes[i] == DT_FLOAT) { getAttr(r,table->schema,i,&v); printf("%-15fl",v->v.floatV); } else if(table->schema->dataTypes[i] == DT_BOOL) { getAttr(r,table->schema,i,&v); printf("%-15s",v->v.boolV ? "true" : "false"); } } printf("\n"); j++; } int sele; scanf("%d", &sele); sele--; if(sele >= 0 && sele < j+1){ createRecord(&result, table->schema); for(i=0;i<table->schema->numAttr;i++){ if(table->schema->dataTypes[i] == DT_INT) { printf("intput int attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); int intt = 0; scanf("%d",&intt); //printf("\n%d is %d\n", i, intt); MAKE_VALUE(value,DT_INT,intt); setAttr(result, table->schema, i, value); freeVal(value); } else if (table->schema->dataTypes[i] == DT_STRING) { printf("intput string attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); char instrbuf[100]; scanf("%s",instrbuf); char* instr = (char *) malloc(sizeof(char)*sizeof(instrbuf)); memcpy(instr,instrbuf,sizeof(instrbuf)); MAKE_STRING_VALUE(value,instr); setAttr(result, table->schema, i, value); freeVal(value); } else if (table->schema->dataTypes[i] == DT_FLOAT) { printf("intput float attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); float infl; scanf("%fl",&infl); MAKE_VALUE(value,DT_FLOAT,infl); setAttr(result, table->schema, i, value); freeVal(value); } else if (table->schema->dataTypes[i] == DT_BOOL) { printf("intput boolean attr[%d/%d] value -- %s [T/F]: ", i+1,table->schema->numAttr,table->schema->attrNames[i]); char inbl; scanf(" %c",&inbl); if(inbl == 'T'){ MAKE_VALUE(value,DT_BOOL,true); } else if (inbl == 'F'){ MAKE_VALUE(value,DT_BOOL,false); } setAttr(result, table->schema, i, value); freeVal(value); } if(i==table->schema->numAttr-1){ printf("\n"); } } result->id.page = rids[sele][0]; result->id.slot = rids[sele][1]; updateRecord(table,result); } break; case 4: printf("delete record\n"); printf("table name: "); char buf4[100]; scanf("%s", buf4); char* tmp4 = (char *) malloc(sizeof(char)*sizeof(buf3)); memcpy(tmp4,buf4,sizeof(buf4)); table = (RM_TableData *) malloc(sizeof(RM_TableData)); openTable(table, tmp4); createRecord(&result, table->schema); r = (Record *) malloc(sizeof(Record)); RM_ScanHandle *sc2 = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle)); startScan(table, sc2, NULL); printf("Select record to delete: \n"); j=0; int rids2[100][2]; while(next(sc2, r) == RC_OK){ printf("(%d) ",j+1); rids2[j][0] = r->id.page; rids2[j][1] = r->id.slot; for(i=0;i<table->schema->numAttr;i++){ if(table->schema->dataTypes[i] == DT_INT) { getAttr(r,table->schema,i,&v); printf("%-15d",v->v.intV); } else if(table->schema->dataTypes[i] == DT_STRING) { getAttr(r,table->schema,i,&v); printf("%-15s",v->v.stringV); } else if(table->schema->dataTypes[i] == DT_FLOAT) { getAttr(r,table->schema,i,&v); printf("%-15fl",v->v.floatV); } else if(table->schema->dataTypes[i] == DT_BOOL) { getAttr(r,table->schema,i,&v); printf("%-15s",v->v.boolV ? "true" : "false"); } } printf("\n"); j++; } int sele2; scanf("%d", &sele2); sele2--; if(sele2 >= 0 && sele2 < j+1){ RID rid; rid.page = rids[sele2][0]; rid.slot = rids[sele2][1]; deleteRecord(table,rid); } break; case 5: printf("scan\n"); break; case 6: printf("table name: "); char buf[100]; scanf("%s", buf); char* tmp = (char *) malloc(sizeof(char)*sizeof(buf)); memcpy(tmp,buf,sizeof(buf)); printtable(buf); break; case 7: cl = 1; break; default: printf("invalid input\n"); break; } } }