/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/statement.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | //-------------------------------------------------------------------------------------------------- |
15 | | |
16 | | #include "yb/yql/cql/ql/statement.h" |
17 | | |
18 | | #include "yb/util/result.h" |
19 | | |
20 | | #include "yb/yql/cql/ql/ptree/list_node.h" |
21 | | #include "yb/yql/cql/ql/ptree/pt_dml.h" |
22 | | #include "yb/yql/cql/ql/ql_processor.h" |
23 | | #include "yb/yql/cql/ql/util/errcodes.h" |
24 | | |
25 | | namespace yb { |
26 | | namespace ql { |
27 | | |
28 | | using std::list; |
29 | | using std::shared_ptr; |
30 | | using std::string; |
31 | | using std::unique_ptr; |
32 | | |
33 | | Statement::Statement(const string& keyspace, const string& text) |
34 | 8.54k | : keyspace_(keyspace), text_(text) { |
35 | 8.54k | } |
36 | | |
37 | 422 | Statement::~Statement() { |
38 | 422 | } |
39 | | |
40 | | Status Statement::Prepare(QLProcessor *processor, const MemTrackerPtr& mem_tracker, |
41 | 11.5k | const bool internal, PreparedResult::UniPtr *result) { |
42 | | // Prepare the statement (parse and semantically analysis). Do so within an exclusive lock. |
43 | 11.5k | if (!prepared_.load(std::memory_order_acquire)) { |
44 | 3.97k | std::lock_guard<std::mutex> guard(parse_tree_mutex_); |
45 | | |
46 | 3.97k | if (parse_tree_ == nullptr) { |
47 | 3.90k | ParseTree::UniPtr parse_tree; |
48 | 3.90k | RETURN_NOT_OK(processor->Prepare(text_, &parse_tree, false /* reparsed */, mem_tracker, |
49 | 3.90k | internal)); |
50 | 3.73k | parse_tree_ = std::move(parse_tree); |
51 | 3.73k | prepared_.store(true, std::memory_order_release); |
52 | 3.73k | } |
53 | 3.97k | } |
54 | | |
55 | | // Return prepared result if requested and the statement is a DML. Do not need a lock here |
56 | | // because we have verified that the parse tree is either present already or we have successfully |
57 | | // prepared the statement above. The parse tree is guaranteed read-only afterwards. |
58 | 11.4k | if (result != nullptr && parse_tree_->root() != nullptr5.52k ) { |
59 | 5.52k | const TreeNode& stmt = *parse_tree_->root(); |
60 | 5.52k | switch (stmt.opcode()) { |
61 | 636 | case TreeNodeOpcode::kPTSelectStmt: FALLTHROUGH_INTENDED; |
62 | 4.67k | case TreeNodeOpcode::kPTInsertStmt: FALLTHROUGH_INTENDED; |
63 | 4.91k | case TreeNodeOpcode::kPTUpdateStmt: FALLTHROUGH_INTENDED; |
64 | 4.91k | case TreeNodeOpcode::kPTExplainStmt: FALLTHROUGH_INTENDED; |
65 | 4.99k | case TreeNodeOpcode::kPTDeleteStmt: |
66 | 4.99k | result->reset(new PreparedResult(static_cast<const PTDmlStmt&>(stmt))); |
67 | 4.99k | break; |
68 | 15 | case TreeNodeOpcode::kPTListNode: |
69 | 15 | result->reset(new PreparedResult(static_cast<const PTListNode&>(stmt))); |
70 | 15 | break; |
71 | 515 | default: |
72 | 515 | break; |
73 | 5.52k | } |
74 | 5.52k | } |
75 | | |
76 | 11.4k | return Status::OK(); |
77 | 11.4k | } |
78 | | |
79 | 9.12M | Result<const ParseTree&> Statement::GetParseTree() const { |
80 | | // Validate that the statement has been prepared and is not stale. |
81 | 9.12M | if (!prepared_.load(std::memory_order_acquire)) { |
82 | 0 | return ErrorStatus(ErrorCode::UNPREPARED_STATEMENT); |
83 | 0 | } |
84 | 9.12M | DCHECK(parse_tree_ != nullptr) << "Parse tree missing"1.20k ; |
85 | 9.12M | if (parse_tree_->stale()) { |
86 | 2 | return ErrorStatus(ErrorCode::STALE_METADATA); |
87 | 2 | } |
88 | 9.12M | return static_cast<const ParseTree&>(*parse_tree_); |
89 | 9.12M | } |
90 | | |
91 | | Status Statement::ExecuteAsync(QLProcessor* processor, const StatementParameters& params, |
92 | 8.75M | StatementExecutedCallback cb) const { |
93 | 8.75M | const ParseTree& parse_tree = VERIFY_RESULT(GetParseTree()); |
94 | 0 | processor->ExecuteAsync(parse_tree, params, std::move(cb)); |
95 | 8.75M | return Status::OK(); |
96 | 8.75M | } |
97 | | |
98 | | } // namespace ql |
99 | | } // namespace yb |