1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 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 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #ifndef TENSORFLOW_CORE_KERNELS_DATA_EXPERIMENTAL_SQL_QUERY_CONNECTION_H_ 16 #define TENSORFLOW_CORE_KERNELS_DATA_EXPERIMENTAL_SQL_QUERY_CONNECTION_H_ 17 18 #include "tensorflow/core/framework/tensor.h" 19 20 namespace tensorflow { 21 namespace data { 22 23 class IteratorContext; 24 25 namespace experimental { 26 27 namespace sql { 28 // This interface allows a user to connect to a database, execute a query, and 29 // iterate over the result set, putting the results into an output tensor. 30 // A subclass implementation is required for each type of database 31 // (e.g. sqlite3, mysql, etc.) 32 // 33 // Presently, a `QueryConnection` instance can only handle one query at a time. 34 // In a future extension, this class may be refactored so that it creates 35 // instances of a new class (named, say, `Statement`) which could have a 36 // one-to-one correspondence with queries. This would make `QueryConnection` 37 // more consistent with `Connection` classes of other database APIs. 38 // `QueryConnection` would then be renamed simply `Connection`. 39 // 40 // This class is not thread safe. Access to it is guarded by a mutex in 41 // `SqlDatasetOp::Dataset::Iterator`. 42 class QueryConnection { 43 public: ~QueryConnection()44 virtual ~QueryConnection() {} 45 // Opens a connection to the database named by `data_source_name`. Prepares to 46 // execute `query` against the database. 47 // 48 // The client must call `Close()` to release the connection resources, even 49 // if `Open()` fails. `Close()` must be called before making another call 50 // to `Open()`. 51 virtual Status Open(const string& data_source_name, const string& query, 52 const DataTypeVector& output_types) = 0; 53 // Closes an opened connection. 54 virtual Status Close() = 0; 55 // Retrieves the next row of the result set of the query from the most recent 56 // call to `Open()`. 57 // 58 // If such a row exists, then the row will be stored in `*out_tensors`, and 59 // `false` will be stored in `*end_of_sequence`. 60 // 61 // If there are no more rows in the result set, then instead `true` will be 62 // stored in `*end_of_sequence`, and the content of `*out_tensors` will be 63 // undefined. 64 virtual Status GetNext(IteratorContext* ctx, std::vector<Tensor>* out_tensors, 65 bool* end_of_sequence) = 0; 66 }; 67 68 } // namespace sql 69 } // namespace experimental 70 } // namespace data 71 } // namespace tensorflow 72 73 #endif // TENSORFLOW_CORE_KERNELS_DATA_EXPERIMENTAL_SQL_QUERY_CONNECTION_H_ 74