bool DataOp::updateRecord(QString record) { QString id, oldRecord; id = getId(record); //保存旧数据 oldRecord = selectRecord(id); if(oldRecord==""){ qDebug() << "更新失败,未找到该条数据"<< record; return 0; } //删除原记录 if(!deleteRecord(getId(record))){ return 0; } //添加新记录 if(!insertRecord(record)){ qDebug() << "更新失败,尝试恢复旧数据"; if(!insertRecord(oldRecord)){ qDebug() << "数据重大错误"; } return 0; }else{ return 1; } return 0; }
int init_delete(int recordSelected,int *cursorArea,Record *hovered,struct RecordList *activeBuffer) { *cursorArea = UI_AREA_DELETE; setColor(R_COLOR_SCHEME_CLASSIC_DELETE); if(hovered->num != recordSelected) { selectRecord(hovered->num); } message(UI_DELETE_CONFIRM(hovered)); return TRUE; }
int init_edit(int recordSelected,int *cursorLeft,int *cursorArea, Cursor *cursor,Area rArea,Record *hovered,struct RecordList *activeBuffer) { *cursorArea = UI_AREA_EDIT_SUBJECT; if(hovered->num != recordSelected) { selectRecord(hovered->num); } *cursorLeft = RECORD_NUM_SPACE +1; fill(subject,30,'\0'); fill(body,140,'\0'); cursor->x = strlen(hovered->subject); cursor->y = getRecordY(hovered,activeBuffer,rArea); return TRUE; }
/*! *\~english * Emit signal on select record. * Signal give information about selected record database id. *\~russian * Выдает сигнал при выборе записи. * Сигнал содержит информацию об id, выбранного объекта в базе данных. *\param -\~english Not use, only for signal compatibility.\~russian * Не используются, заданы только для совместимости с сигналом таблицы.\~ *\~ */ void wDBTable::lineChange(int, int) { QSqlRecord * rec = currentRecord(); if ( !rec ) return; qulonglong id = 0; if(rec->contains("id")) id = rec->value("id").toLongLong(); //if (containerType() == "wJournal") //{ //if(rec->contains("idd")) id = rec->value("idd").toLongLong(); //} aLog::print(aLog::Info, tr("wDBTable: select document %1").arg(id)); emit( selectRecord( id ) ); }
QStringList DataOp::getItemList(QString id) { QString record = selectRecord(id); int index = 0, len=0; QStringList itemList; while(index!=-1){ len = record.indexOf('#',index+1)-index-1; len = (len>=0) ? len : record.length()-index-1; if(index==0){ itemList.append(record.mid(0,record.indexOf('#',index+1))); }else { itemList.append(record.mid(index+1,len)); } index = record.indexOf('#',index+1); } return itemList; }
//Mode Functions int scroll(Record **hovered, struct RecordList **activeBuffer,char c) { int redraw = FALSE; if(*hovered) { if(DEBUG && c == 'r') { message("refreshed | botom: %i | (*hovered)->prev:%p",(*activeBuffer)->bottom->num,(*hovered)->prev); redraw = TRUE; } if(DEBUG && c == 'p') { DUMP = TRUE; redraw = TRUE; } if(DUMP) { message("%p<-[[%p]]->%p",(*activeBuffer)->top->prev,(*activeBuffer)->top,(*activeBuffer)->top->next); } if (c == KEY_ENTER) { selectRecord((*hovered)->num); redraw = TRUE; } if (c == KEY_DOWN) { if ((*hovered) == (*activeBuffer)->bottom && (*activeBuffer)->lengthfrombot){ scrollDown((*activeBuffer)); (*hovered) = (*activeBuffer)->bottom; }else if((*hovered)->next) { (*hovered) = (*hovered)->next; } redraw = TRUE; } if (c == KEY_UP) { if((*hovered) == (*activeBuffer)->top && (*hovered)->num - 1) { scrollUp((*activeBuffer)); (*hovered) = (*activeBuffer)->top; }else if((*hovered)->prev) { (*hovered) = (*hovered)->prev; } redraw = TRUE; } } return redraw; }
bool DataOp::updateItem(QString id, int itemIndex, QString item) { QString oldRecord = selectRecord(id); if(oldRecord==""){ error = "未找到该条数据:" + id; return 0; } int index=0, n=0, len=0; while(index != -1){ //找到该项元素 if(n==itemIndex){ len = oldRecord.indexOf('#',index+1)-index-1; len = (len>=0) ? len : oldRecord.length()-index-1; oldRecord.replace(index+1,len,item); if(updateRecord(oldRecord)){ return 1; } } n++; index = oldRecord.indexOf('#',index+1); } return 0; }
/* * test2 -- 検索 */ Result test2() { RecordSet *recordSet; Condition condition; FieldList fieldList; /* * 以下の検索を実行 * select * from TABLE_NAME where age > 17 */ strcpy(condition.name, "age"); condition.dataType = TYPE_INT; condition.operator = OPR_GREATER_THAN; condition.val.intVal = 17; condition.distinct = NOT_DISTINCT; fieldList.numField = -1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select * from TABLE_NAME where age > 17\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); /* * 以下の検索を実行 * select distinct * from TABLE_NAME where age > 17 */ strcpy(condition.name, "age"); condition.dataType = TYPE_INT; condition.operator = OPR_GREATER_THAN; condition.val.intVal = 17; condition.distinct = DISTINCT; fieldList.numField = -1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select distinct * from TABLE_NAME where age > 17\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); /* * 以下の検索を実行 * select * from TABLE_NAME where address != 'Florida' */ strcpy(condition.name, "address"); condition.dataType = TYPE_VARCHAR; condition.operator = OPR_NOT_EQUAL; strcpy(condition.val.stringVal, "Florida"); condition.distinct = NOT_DISTINCT; fieldList.numField = -1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select * from TABLE_NAME where address != 'Florida'\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); /* * 以下の検索を実行 * select name from TABLE_NAME' */ strcpy(condition.name, ""); condition.dataType = TYPE_UNKNOWN; condition.operator = OPR_UNKNOWN; strcpy(condition.val.stringVal, ""); condition.distinct = NOT_DISTINCT; strcpy(fieldList.name[0], "name"); fieldList.numField = 1; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select name from TABLE_NAME\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* * 以下の検索を実行 * select name address from TABLE_NAME where address != 'Florida' */ strcpy(condition.name, "address"); condition.dataType = TYPE_VARCHAR; condition.operator = OPR_NOT_EQUAL; strcpy(condition.val.stringVal, "Florida"); condition.distinct = NOT_DISTINCT; strcpy(fieldList.name[0], "name"); strcpy(fieldList.name[1], "address"); fieldList.numField = 2; if ((recordSet = selectRecord(TABLE_NAME, &fieldList, &condition)) == NULL) { fprintf(stderr, "Cannot select records.\n"); return NG; } /* 結果を表示 */ printf("select name, address from TABLE_NAME where address != 'Florida'\n"); printRecordSet(TABLE_NAME, recordSet, &fieldList); /* 結果を解放 */ freeRecordSet(recordSet); return OK; }
/* * callSelectRecord -- select文の構文解析とselectRecordの呼び出し * * 引数: * なし * * 返り値: * なし * * selectの書式: * select * from テーブル名 where 条件式 * select フィールド名 , ... from テーブル名 where 条件式 (発展課題) */ void callSelectRecord() { char *token; char *tableName; char *selectField; char *p; Condition condition; TableInfo *tableInfo; RecordSet *recordSet; int i, j; int numField; int allSelectFlag; /* *かどうかの判断 */ char sendFieldName[MAX_FIELD][MAX_FIELD_NAME]; for (i = 0; i < MAX_FIELD; i++){ memset(&sendFieldName[i], '\0', MAX_FIELD_NAME); } /*selectの次のトークンを読み込み、distinctが存在するかをチェック */ token = getNextToken(); if (token == NULL) { /* 文法エラー */ printf("入力行に間違いがあります。:%s\n", token); return; } if (strcmp(token, "distinct") == 0){ condition.distinct = DISTINCT; /*次のトークンを読み込み、それが"*"かどうかをチェック */ token = getNextToken(); if (token == NULL) { /* 文法エラー */ printf("入力行に間違いがあります。:%s\n", token); return; } } else{ condition.distinct = NOT_DISTINCT; } selectField = (char *)malloc(sizeof(char)*MAX_FIELD_NAME*MAX_FIELD); p = selectField; numField = 0; if (strcmp(token, "*") != 0){ allSelectFlag = 0; for (;;) { memcpy(p, token, strlen(token) + 1); p += strlen(token); /* フィールド数をカウントする */ numField++; /* 次のトークンの読み込み */ if ((token = getNextToken()) == NULL) { /* 文法エラー */ printf("入力行に間違いがあります。%s\n", token); return; } /* 読み込んだトークンが"from"だったら、ループから抜ける */ if (strcmp(token, "from") == 0) { break; } else if (strcmp(token, ",") == 0) { token = " "; memcpy(p, token, strlen(token) + 1); p += strlen(token); /* 次のトークンの読み込み */ if ((token = getNextToken()) == NULL) { /* 文法エラー */ printf("入力行に間違いがあります。%s\n", token); return; } continue; } else { /* 文法エラー */ printf("入力行に間違いがあります。%s\n", token); return; } } } /* *だったら */ else if (strcmp(token, "*") == 0){ allSelectFlag = 1; token = getNextToken(); if (strcmp(token, "from") != 0) { /* 文法エラー */ printf("入力行に間違いがあります。:%s\n", token); return; } } /* テーブル名を読み込む */ if ((tableName = getNextToken()) == NULL) { /* 文法エラー */ printf("入力行に間違いがあります。:%s\n", tableName); return; } /*次のトークンを読み込み、それが"where"かどうかをチェック */ token = getNextToken(); /*条件が入力されなかったらすべてのレコードを表示*/ if (token == NULL) { /* テーブルの情報を取得する */ if ((tableInfo = getTableInfo(tableName)) == NULL) { /* エラー処理 */ return; } numField = tableInfo->numField; printTableData(tableName, numField); freeTableInfo(tableInfo); free(selectField); return; } if (strcmp(token, "where") != 0) { /* 文法エラー */ printf("入力行に間違いがあります。:%s\n", token); return; } /* 条件式のフィールド名を読み込む */ if ((token = getNextToken()) == NULL){ /* 文法エラー */ printf("入力行に間違いがあります。:%s\n", token); return; } strcpy(condition.name, token); /* テーブルの情報を取得する */ if ((tableInfo = getTableInfo(tableName)) == NULL) { /* エラー処理 */ return; } /*条件式に指定されたフィールドのデータ型を調べる*/ condition.dataType = TYPE_UNKNOWN; for (i = 0; i < tableInfo->numField; i++){ if (strcmp(tableInfo->fieldInfo[i].name, condition.name) == 0){ /* データ型をconditionにコピーしてループからぬける*/ condition.dataType = tableInfo->fieldInfo[i].dataType; break; } } /*conditionを決定する*/ conditionSet(token, &condition); /* selectRecordを呼び出し、レコードを検索 */ if ((recordSet = selectRecord(tableName, &condition)) == NULL) { printf("レコードの検索に失敗しました。\n"); } /*selectFieldがフィールド名にあるか調べる*/ if (allSelectFlag == 0){ setInputString(selectField); token = getNextToken(); for (i = 0; i < MAX_FIELD; i++){ if (token == NULL){ break; } for (j = 0; j < tableInfo->numField; j++){ /*tokenがフィールド名と一致したら*/ if (strcmp(tableInfo->fieldInfo[j].name, token) == 0){ strcpy(sendFieldName[i], token); token = getNextToken(); break; } if (j == tableInfo->numField - 1){ printf("そのフィールド名は存在しません%s\n", token); return; } } } } /* *の場合は */ else{ for (i = 0; i < tableInfo->numField; i++){ strcpy(sendFieldName[i], tableInfo->fieldInfo[i].name); } numField = tableInfo->numField; } /*検索したレコード集合を表示*/ printRecordSet(recordSet, sendFieldName, numField); /*解放*/ free(selectField); freeTableInfo(tableInfo); freeRecordSet(recordSet); }
// Asignado a Dago void Data::updateRecord(Registro _new, unsigned int index) { fstream disco; disco.open(path, ios::binary | ios::in | ios::out); if (!disco) { throw SMException("No se pudo abrir el archivo tablespace.dat"); } if(index>=getCantRegActivos()) { throw SMException("Index invalido para el bloque de Data " + header.blockID); } unsigned int offset = 4096*header.blockID + sizeof(Header); disco.seekg(offset); disco.read((char*) &info, sizeof(InfoD)); Registro _old = selectRecord(index); mapabits nulos_new(_new.getNulos()); Metadata md(info.blockIDMD); unsigned int sizeMalloc = md.getrecordsize(); offset+=sizeof(InfoD) + index*(sizeof(InfoReg)+sizeMalloc); unsigned char* buffer = (unsigned char*)malloc(sizeMalloc); for(int i=0; i<(int)md.getCant_campos(); i++) { InfoMDC campo = md.readCampo(i); if(nulos_new.getAt(i)) { switch(campo.tipo_campo){ case 1: for(int s=0; s<(int)sizeof(int);s++){ buffer[s]='#'; } buffer+=sizeof(int); break; case 2: for(int s=0; s<(int)sizeof(double);s++){ buffer[s]='#'; } buffer+=sizeof(double); break; case 3: for(int s=0; s<(int)campo.size;s++){ buffer[s]='#'; } buffer+=campo.size; break; case 4: for(int s=0; s<(int)(sizeof(unsigned int)+sizeof(unsigned int));s++){ buffer[s]='#'; } buffer+=(sizeof(unsigned int)+sizeof(unsigned int)); break; case 5: for(int s=0; s<(int)sizeof(bool);s++){ buffer[s]='#'; } buffer+=sizeof(bool); break; } } else { switch(campo.tipo_campo) { case 1://Int memcpy(buffer,_new.contentReg,sizeof(int)); buffer+=sizeof(int); _old.contentReg+=sizeof(int); _new.contentReg+=sizeof(int); break; case 2://Double memcpy(buffer,_new.contentReg,sizeof(double)); buffer+=sizeof(double); _old.contentReg+=sizeof(double); _new.contentReg+=sizeof(double); break; case 3://Char memcpy(buffer,_new.contentReg,campo.size); buffer+=campo.size; _old.contentReg+=campo.size; _new.contentReg+=campo.size; break; case 4://Varchar { //Poner en el API que me manden con terminacion nula la cadena, y poner en varchar que recibe char* y cambiar el max_size sumarle 1 unsigned int bIDV, pos; memcpy(&bIDV,_old.contentReg,sizeof(unsigned int)); _old.contentReg+=sizeof(unsigned int); memcpy(&pos,_old.contentReg,sizeof(unsigned int)); _old.contentReg+=sizeof(unsigned int); Varchar vb(bIDV); unsigned char *varchar = (unsigned char*)malloc(1+campo.size+1); if(!nulos_new.getAt(i)) { unsigned char tam; memcpy(&tam,_new.contentReg,sizeof(unsigned char)); _new.contentReg+=sizeof(unsigned char); memcpy(varchar,&tam,sizeof(unsigned char)); varchar+=sizeof(unsigned char); memcpy(varchar,_new.contentReg,(int)tam+1); _new.contentReg+=(int)tam+1; int i; for(i=(int)tam+1; i<=(int)campo.size; i++) { varchar[i]='#'; } varchar[i+1]='\0'; } else { varchar[0]=(unsigned char)0; int i; for(i=1; i<=(int)campo.size; i++) { varchar[i]='#'; } varchar[i+1]='\0'; } vb.updateVarchar(varchar,pos); memcpy(buffer,&bIDV,sizeof(unsigned int)); buffer+=sizeof(unsigned int); memcpy(buffer,&pos,sizeof(unsigned int)); buffer+=sizeof(unsigned int); free(varchar); } break; case 5://Bool memcpy(buffer,_new.contentReg,sizeof(bool)); buffer+=sizeof(bool); _old.contentReg+=sizeof(bool); _new.contentReg+=sizeof(bool); break; } } } buffer-=md.getrecordsize(); _new.setContentReg(buffer); _new.setTam(sizeMalloc); disco.seekp(offset); disco.write((const char*) &_new.info, sizeof(InfoReg)); disco.write((const char*) _new.contentReg,sizeMalloc); disco.flush(); disco.close(); free(buffer); }