-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
296 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ enum class PhysicalOperatorType | |
CALC, | ||
STRING_LIST, | ||
DELETE, | ||
UPDATE, | ||
INSERT, | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "sql/operator/update_logical_operator.h" | ||
|
||
UpdateLogicalOperator::UpdateLogicalOperator(Table *table, Value value, const char *field_name) { | ||
table_ = table; | ||
value_ = std::move(value); | ||
char *tmp = (char *)malloc(sizeof(char) * (strlen(field_name) + 1)); | ||
strcpy(tmp, field_name); | ||
field_name_ = tmp; | ||
} | ||
|
||
UpdateLogicalOperator::~UpdateLogicalOperator() { | ||
if (field_name_ != nullptr) { | ||
free(field_name_); | ||
} | ||
} | ||
|
||
// UpdateLogicalOperator::~UpdateLogicalOperator() { | ||
// if (update_map_ != nullptr) { | ||
// delete update_map_; | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef MINIOB_SQL_OPERATOR_UPDATE_LOGICAL_OPERATOR_H | ||
#define MINIOB_SQL_OPERATOR_UPDATE_LOGICAL_OPERATOR_H | ||
|
||
#include "sql/operator/logical_operator.h" | ||
#include "sql/parser/parse_defs.h" | ||
#include <unordered_map> | ||
|
||
/** | ||
* @brief 逻辑算子,用于执行delete语句 | ||
* @ingroup LogicalOperator | ||
*/ | ||
class UpdateLogicalOperator : public LogicalOperator { | ||
|
||
public: | ||
UpdateLogicalOperator(Table *table, Value value, const char *field_name); | ||
|
||
~UpdateLogicalOperator() override; | ||
|
||
LogicalOperatorType type() const override { | ||
return LogicalOperatorType::UPDATE; | ||
} | ||
|
||
inline Table * table() const { | ||
return table_; | ||
} | ||
|
||
inline Value& value() { | ||
return value_; | ||
} | ||
|
||
inline char * field_name() const { | ||
return field_name_; | ||
} | ||
// const Value* find_value_by_field(const FieldMeta& field_meta) { | ||
// return &update_map_->at(field_meta); | ||
// } | ||
|
||
private: | ||
Table * table_ = nullptr; | ||
// std::unordered_map<FieldMeta, Value> *update_map_ = nullptr; // TODO 多字段更新 | ||
Value value_; | ||
char * field_name_ = nullptr; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "sql/operator/update_physical_operator.h" | ||
#include "storage/trx/trx.h" | ||
#include "storage/record/record.h" | ||
|
||
UpdatePhysicalOperator::UpdatePhysicalOperator(Table *table, Value& value, const char * field_name) { | ||
table_ = table; | ||
value_ = value; | ||
char *tmp = (char *)malloc(sizeof(char) * (strlen(field_name) + 1)); | ||
strcpy(tmp, field_name); | ||
field_name_ = tmp; | ||
} | ||
|
||
UpdatePhysicalOperator::~UpdatePhysicalOperator() { | ||
if (field_name_ != nullptr) { | ||
delete field_name_; | ||
} | ||
} | ||
|
||
RC UpdatePhysicalOperator::open(Trx *trx) { | ||
if (children_.empty()) { | ||
return RC::SUCCESS; | ||
} | ||
|
||
std::unique_ptr<PhysicalOperator> &child = children_[0]; | ||
RC rc = child->open(trx); | ||
if (rc != RC::SUCCESS) { | ||
LOG_WARN("failed to open child operator: %s", strrc(rc)); | ||
return rc; | ||
} | ||
|
||
trx_ = trx; | ||
|
||
return RC::SUCCESS; | ||
} | ||
|
||
RC UpdatePhysicalOperator::close() { | ||
if (!children_.empty()) { | ||
children_[0]->close(); | ||
} | ||
return RC::SUCCESS; | ||
} | ||
|
||
RC UpdatePhysicalOperator::next() { | ||
RC rc = RC::SUCCESS; | ||
if (children_.empty()) { | ||
return RC::RECORD_EOF; | ||
} | ||
|
||
PhysicalOperator *child = children_[0].get(); | ||
while (RC::SUCCESS == (rc = child->next())) { | ||
Tuple *tuple = child->current_tuple(); | ||
if (nullptr == tuple) { | ||
LOG_WARN("failed to get current record: %s", strrc(rc)); | ||
return rc; | ||
} | ||
|
||
RowTuple *row_tuple = static_cast<RowTuple *>(tuple); | ||
Record &record = row_tuple->record(); | ||
const FieldMeta *field = table_->table_meta().field(field_name_); | ||
int offset = field->offset(); | ||
rc = trx_->update_record(table_, record, offset, value_); | ||
if (rc != RC::SUCCESS) { | ||
LOG_WARN("failed to delete record: %s", strrc(rc)); | ||
return rc; | ||
} | ||
} | ||
|
||
return RC::RECORD_EOF; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef MINIOB_UPDATE_PHYSICAL_OPERATOR_H | ||
#define MINIOB_UPDATE_PHYSICAL_OPERATOR_H | ||
|
||
#include "sql/operator/physical_operator.h" | ||
#include "sql/parser/parse.h" | ||
|
||
class UpdatePhysicalOperator : public PhysicalOperator { | ||
public: | ||
|
||
UpdatePhysicalOperator(Table *table, Value& value, const char * field_name); | ||
|
||
virtual ~UpdatePhysicalOperator() override; | ||
|
||
PhysicalOperatorType type() const override { | ||
return PhysicalOperatorType::UPDATE; | ||
} | ||
|
||
RC open(Trx *trx) override; | ||
RC next() override; | ||
RC close() override; | ||
|
||
Tuple *current_tuple() override { return nullptr; } | ||
|
||
private: | ||
Table * table_ = nullptr; | ||
Value value_; | ||
const char * field_name_ = nullptr; | ||
Trx *trx_ = nullptr; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.