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_SQL_QUERY_CONNECTION_H_ 16 #define TENSORFLOW_CORE_KERNELS_DATA_SQL_QUERY_CONNECTION_H_ 17 18 #include "tensorflow/core/framework/tensor.h" 19 20 namespace tensorflow { 21 22 class IteratorContext; 23 24 namespace sql { 25 // This interface allows a user to connect to a database, execute a query, and 26 // iterate over the result set, putting the results into an output tensor. 27 // A subclass implementation is required for each type of database 28 // (e.g. sqlite3, mysql, etc.) 29 // 30 // Presently, a `QueryConnection` instance can only handle one query at a time. 31 // In a future extension, this class may be refactored so that it creates 32 // instances of a new class (named, say, `Statement`) which could have a 33 // one-to-one correspondence with queries. This would make `QueryConnection` 34 // more consistent with `Connection` classes of other database APIs. 35 // `QueryConnection` would then be renamed simply `Connection`. 36 // 37 // This class is not thread safe. Access to it is guarded by a mutex in 38 // `SqlDatasetOp::Dataset::Iterator`. 39 class QueryConnection { 40 public: 41 virtual ~QueryConnection() {} 42 // Opens a connection to the database named by `data_source_name`. Prepares to 43 // execute `query` against the database. 44 // 45 // The client must call `Close()` to release the connection resources, even 46 // if `Open()` fails. `Close()` must be called before making another call 47 // to `Open()`. 48 virtual Status Open(const string& data_source_name, const string& query, 49 const DataTypeVector& output_types) = 0; 50 // Closes an opened connection. 51 virtual Status Close() = 0; 52 // Retrieves the next row of the result set of the query from the most recent 53 // call to `Open()`. 54 // 55 // If such a row exists, then the row will be stored in `*out_tensors`, and 56 // `false` will be stored in `*end_of_sequence`. 57 // 58 // If there are no more rows in the result set, then instead `true` will be 59 // stored in `*end_of_sequence`, and the content of `*out_tensors` will be 60 // undefined. 61 virtual Status GetNext(IteratorContext* ctx, std::vector<Tensor>* out_tensors, 62 bool* end_of_sequence) = 0; 63 }; 64 65 } // namespace sql 66 67 } // namespace tensorflow 68 69 #endif // TENSORFLOW_CORE_KERNELS_DATA_SQL_QUERY_CONNECTION_H_ 70