|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #if SQL_ENABLED |
|
22 |
|
23 #include "SQLBundleStore.h" |
|
24 #include "SQLStore.h" |
|
25 #include "StorageConfig.h" |
|
26 #include "bundling/Bundle.h" |
|
27 |
|
28 namespace dtn { |
|
29 |
|
30 /****************************************************************************** |
|
31 * |
|
32 * SQLBundleStore |
|
33 * |
|
34 *****************************************************************************/ |
|
35 |
|
36 /** |
|
37 * Constructor. |
|
38 */ |
|
39 |
|
40 SQLBundleStore::SQLBundleStore(oasys::SQLImplementation* db, const char* table_name) |
|
41 : BundleStore() |
|
42 { |
|
43 Bundle tmpobj(this); |
|
44 |
|
45 store_ = new SQLStore(table_name, db); |
|
46 store_->create_table(&tmpobj); |
|
47 store_->set_key_name("bundleid"); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Create a new bundle instance, then make a generic call into the |
|
52 * bundleStore to look up the bundle and fill in it's members if |
|
53 * found. |
|
54 */ |
|
55 |
|
56 Bundle* |
|
57 SQLBundleStore::get(int bundle_id) |
|
58 { |
|
59 Bundle* bundle = new Bundle(); |
|
60 if (store_->get(bundle, bundle_id) != 0) { |
|
61 delete bundle; |
|
62 return NULL; |
|
63 } |
|
64 |
|
65 return bundle; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Store the given bundle in the persistent store. |
|
70 */ |
|
71 bool |
|
72 SQLBundleStore::insert(Bundle* bundle) |
|
73 { |
|
74 return store_->insert(bundle) == 0; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Update the given bundle in the persistent store. |
|
79 */ |
|
80 bool |
|
81 SQLBundleStore::update(Bundle* bundle) |
|
82 { |
|
83 return store_->update(bundle, bundle->bundleid_) == 0; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Delete the given bundle from the persistent store. |
|
88 */ |
|
89 bool |
|
90 SQLBundleStore::del(int bundle_id) |
|
91 { |
|
92 return store_->del(bundle_id) == 0; |
|
93 } |
|
94 |
|
95 |
|
96 |
|
97 int |
|
98 SQLBundleStore::delete_expired(const time_t now) |
|
99 { |
|
100 const char* field = "expiration"; |
|
101 oasys::StringBuffer query ; |
|
102 query.appendf("DELETE FROM %s WHERE %s > %lu", store_->table_name(), field, now); |
|
103 |
|
104 int retval = store_->exec_query(query.c_str()); |
|
105 return retval; |
|
106 } |
|
107 |
|
108 |
|
109 |
|
110 bool |
|
111 SQLBundleStore::is_custodian(int bundle_id) |
|
112 { |
|
113 NOTIMPLEMENTED ; |
|
114 |
|
115 /** |
|
116 * One possible implementation. Currently b.is_custodian() |
|
117 * function does not exist |
|
118 * Bundle b = get(bundle_id); |
|
119 * if (b == NULL) return 0; |
|
120 * return (b.is_custodian()) ; |
|
121 */ |
|
122 } |
|
123 |
|
124 } // namespace dtn |
|
125 |
|
126 #endif /* SQL_ENABLED */ |