xref: /netbsd-src/external/bsd/kyua-cli/dist/store/transaction.hpp (revision f39f9c9b2b3d39fa4e71f38ebea4c5d12192a641)
1 // Copyright 2011 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 /// \file store/transaction.hpp
30 /// Implementation of transactions on the backend.
31 
32 #if !defined(STORE_TRANSACTION_HPP)
33 #define STORE_TRANSACTION_HPP
34 
35 extern "C" {
36 #include <stdint.h>
37 }
38 
39 #include <string>
40 #include <utility>
41 
42 #include "engine/test_program.hpp"
43 #include "utils/datetime.hpp"
44 #include "utils/fs/path.hpp"
45 #include "utils/optional.hpp"
46 #include "utils/shared_ptr.hpp"
47 
48 namespace engine {
49 class action;
50 class context;
51 class test_result;
52 }  // namespace engine
53 
54 namespace store {
55 
56 
57 class backend;
58 class transaction;
59 
60 
61 namespace detail {
62 
63 
64 engine::test_program_ptr get_test_program(backend&, const int64_t);
65 
66 
67 }  // namespace detail
68 
69 
70 /// Iterator for the set of test case results that are part of an action.
71 ///
72 /// \todo Note that this is not a "standard" C++ iterator.  I have chosen to
73 /// implement a different interface because it makes things easier to represent
74 /// an SQL statement state.  Rewrite as a proper C++ iterator, inheriting from
75 /// std::iterator.
76 class results_iterator {
77     struct impl;
78 
79     /// Pointer to the shared internal implementation.
80     std::shared_ptr< impl > _pimpl;
81 
82     friend class transaction;
83     results_iterator(std::shared_ptr< impl >);
84 
85 public:
86     ~results_iterator(void);
87 
88     results_iterator& operator++(void);
89     operator bool(void) const;
90 
91     const engine::test_program_ptr test_program(void) const;
92     std::string test_case_name(void) const;
93     engine::test_result result(void) const;
94     utils::datetime::delta duration(void) const;
95 
96     std::string stdout_contents(void) const;
97     std::string stderr_contents(void) const;
98 };
99 
100 
101 /// Representation of a transaction.
102 ///
103 /// Transactions are the entry place for high-level calls that access the
104 /// database.
105 class transaction {
106     struct impl;
107 
108     /// Pointer to the shared internal implementation.
109     std::shared_ptr< impl > _pimpl;
110 
111     friend class backend;
112     transaction(backend&);
113 
114 public:
115     ~transaction(void);
116 
117     void commit(void);
118     void rollback(void);
119 
120     engine::action get_action(const int64_t);
121     results_iterator get_action_results(const int64_t);
122     std::pair< int64_t, engine::action > get_latest_action(void);
123     engine::context get_context(const int64_t);
124 
125     int64_t put_action(const engine::action&, const int64_t);
126     int64_t put_context(const engine::context&);
127     int64_t put_test_program(const engine::test_program&, const int64_t);
128     int64_t put_test_case(const engine::test_case&, const int64_t);
129     utils::optional< int64_t > put_test_case_file(const std::string&,
130                                                   const utils::fs::path&,
131                                                   const int64_t);
132     int64_t put_result(const engine::test_result&, const int64_t,
133                        const utils::datetime::timestamp&,
134                        const utils::datetime::timestamp&);
135 };
136 
137 
138 }  // namespace store
139 
140 #endif  // !defined(STORE_TRANSACTION_HPP)
141