/[eiffelstudio]/branches/eth/eve/Src/library/store/dbms/support/database_manager.e
ViewVC logotype

Contents of /branches/eth/eve/Src/library/store/dbms/support/database_manager.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88121 - (show annotations)
Fri Dec 16 14:42:24 2011 UTC (7 years, 10 months ago) by jasonw
File size: 11995 byte(s)
<<Merged from trunk#88120.>>
1 note
2 description: "Object that enable basic database management."
3 legal: "See notice at end of class."
4 status: "See notice at end of class."
5 date: "$Date$"
6 revision: "$Revision$"
7
8 class
9 DATABASE_MANAGER [reference G -> DATABASE create default_create end]
10
11 feature -- Connection
12
13 set_connection_information (user_name, password, data_source: STRING)
14 -- Set information required to connect to the database.
15 -- It overrides the information set by `set_connection_string_information' when login.
16 require
17 not_void: user_name /= Void and password /= Void and data_source /= Void
18 local
19 rescued: BOOLEAN
20 l_database_appl: like database_appl
21 do
22 if not rescued then
23 create l_database_appl.login (user_name, password)
24 database_appl := l_database_appl
25 l_database_appl.set_data_source (data_source)
26 else
27 has_error := True
28 error_message_32 := unexpected_error (Connection_info_name)
29 end
30 rescue
31 rescued := True
32 retry
33 end
34
35 set_connection_string_information (a_connection_string: STRING_8)
36 -- Set connection string required to connect to the database.
37 -- It overrides the information set by `set_connection_information' when login.
38 require
39 not_void: a_connection_string /= Void
40 local
41 rescued: BOOLEAN
42 do
43 if not rescued then
44 create database_appl.login_with_connection_string (a_connection_string)
45 else
46 has_error := True
47 error_message_32 := unexpected_error (Connection_string_info_name)
48 end
49 rescue
50 rescued := True
51 retry
52 end
53
54 establish_connection
55 -- Establish connection.
56 require
57 information_set: database_handle_created
58 not_connected: not is_connected
59 local
60 rescued: BOOLEAN
61 l_database_appl: like database_appl
62 l_session_control: like session_control
63 do
64 if not rescued then
65 -- Initialization of the Relational Database:
66 -- This will set various informations to perform a correct
67 -- connection to the Relational database.
68 -- This will update the handle to link EiffelStore interface
69 -- to the RDBMS represented by this class.
70 l_database_appl := database_appl
71 check l_database_appl /= Void end -- implied by precondition `information_set'
72 l_database_appl.set_base
73
74 -- Start session
75 create l_session_control.make
76 session_control := l_session_control
77 l_session_control.connect
78 has_error := not l_session_control.is_ok
79 error_message_32 := l_session_control.error_message_32
80 else
81 has_error := True
82 error_message_32 := unexpected_error (Establish_connection_name)
83 end
84 rescue
85 rescued := True
86 retry
87 end
88
89 disconnect
90 -- Disconnect from database.
91 require
92 is_connected: is_connected
93 local
94 l_session_control: like session_control
95 do
96 l_session_control := session_control
97 check l_session_control /= Void end -- implied by precondition
98 l_session_control.disconnect
99 end
100
101 feature -- Status report
102
103 has_error: BOOLEAN
104 -- Has an error occurred during last database operation?
105
106 error_message: detachable STRING
107 -- Error message if an error occurred during last
108 -- database operation.
109 obsolete
110 "Use `error_message_32' instead."
111 do
112 if attached error_message_32 as l_s then
113 Result := l_s.as_string_8
114 end
115 end
116
117 error_message_32: detachable STRING_32
118 -- Error message if an error occurred during last
119 -- database operation.
120
121 session_control_created: BOOLEAN
122 -- Is `session_control' created?
123 do
124 Result := session_control /= Void
125 end
126
127 is_connected: BOOLEAN
128 -- Is the application connected to a database?
129 local
130 l_session_control: like session_control
131 do
132 l_session_control := session_control
133 Result := l_session_control /= Void and then l_session_control.is_connected
134 end
135
136 database_handle_created: BOOLEAN
137 -- Has the database handle been created?
138 do
139 Result := database_appl /= Void
140 end
141
142 feature -- Queries
143
144 load_data_with_select (s: READABLE_STRING_GENERAL): detachable ANY
145 -- Load directly a single data from the database.
146 --| This can be used for instance to retrieve a table rows count or
147 --| a minimum value.
148 require
149 meaningful_select: s /= Void
150 created: session_control_created
151 local
152 rescued: BOOLEAN
153 tuple: DB_TUPLE
154 l_session_control: like session_control
155 l_cursor: detachable DB_RESULT
156 l_db_selection: like db_selection
157 do
158 if not rescued then
159 has_error := False
160 l_session_control := session_control
161 check l_session_control /= Void end -- implied by precondition `created'
162 l_session_control.reset
163 l_db_selection := db_selection
164 l_db_selection.no_object_convert
165 l_db_selection.unset_action
166 l_db_selection.set_query (s)
167 l_db_selection.execute_query
168 if l_db_selection.is_ok then
169 l_db_selection.load_result
170 if l_db_selection.is_ok then
171 l_cursor := l_db_selection.cursor
172 check l_cursor /= Void end -- implied by `load_result''s postcondition
173 create tuple.copy (l_cursor)
174 Result := tuple.item (1)
175 end
176 end
177 l_db_selection.terminate
178 if not l_db_selection.is_ok then
179 has_error := True
180 error_message_32 := l_session_control.error_message_32
181 end
182 else
183 has_error := True
184 error_message_32 := unexpected_error (data_select_name)
185 end
186 rescue
187 rescued := True
188 retry
189 end
190
191 load_list_with_select (s: READABLE_STRING_GENERAL; an_obj: ANY): ARRAYED_LIST [like an_obj]
192 -- Load list of objects whose type are the same as `an_obj',
193 -- following the SQL query `s'.
194 require
195 not_void: an_obj /= Void
196 meaningful_select: s /= Void
197 created: session_control_created
198 local
199 db_actions: DB_ACTION [like an_obj]
200 rescued: BOOLEAN
201 l_session_control: like session_control
202 l_result: detachable ARRAYED_LIST [like an_obj]
203 l_db_selection: like db_selection
204 do
205 if not rescued then
206 l_session_control := session_control
207 check l_session_control /= Void end -- implied by precondition `created'
208 has_error := False
209 l_session_control.reset
210 l_db_selection := db_selection
211 l_db_selection.object_convert (an_obj)
212 l_db_selection.set_query (s)
213 create db_actions.make (l_db_selection, an_obj)
214 l_db_selection.set_action (db_actions)
215 l_db_selection.execute_query
216 if l_db_selection.is_ok then
217 l_db_selection.load_result
218 if l_db_selection.is_ok then
219 l_result := db_actions.list
220 end
221 end
222 l_db_selection.terminate
223 if not l_db_selection.is_ok then
224 has_error := True
225 error_message_32 := l_session_control.error_message_32
226 create l_result.make (0)
227 end
228 else
229 has_error := True
230 error_message_32 := unexpected_error (list_select_name)
231 create l_result.make (0)
232 end
233 check l_result /= Void end -- FIXME: implied by previous if clause, bug here? `l_result' can be void if rescued
234 Result := l_result
235 ensure
236 result_not_void: Result /= Void
237 rescue
238 rescued := True
239 retry
240 end
241
242 feature -- Queries without result to load.
243
244 execute_query (a_query: READABLE_STRING_GENERAL)
245 -- Execute `a_query' and commit changes.
246 require
247 not_void: a_query /= Void
248 do
249 execute_query_without_commit (a_query)
250 commit
251 end
252
253 execute_query_without_commit (a_query: READABLE_STRING_GENERAL)
254 -- Execute `a_query' in the database.
255 -- Warning: query executed is not committed.
256 require
257 not_void: a_query /= Void
258 created: session_control_created
259 local
260 rescued: BOOLEAN
261 l_session_control: like session_control
262 do
263 if not rescued then
264 has_error := False
265 l_session_control := session_control
266 check l_session_control /= Void end -- implied by precondition
267 l_session_control.reset
268 db_change.set_query (a_query)
269 db_change.execute_query
270 else
271 has_error := True
272 error_message_32 := unexpected_error (Execute_query_name)
273 end
274 rescue
275 rescued := True
276 retry
277 end
278
279 commit
280 -- Commit updates in the database.
281 require
282 session_control_created
283 local
284 rescued: BOOLEAN
285 l_session_control: like session_control
286 do
287 if not rescued then
288 l_session_control := session_control
289 check l_session_control /= Void end -- implied by precondition
290 if l_session_control.is_ok then
291 l_session_control.commit
292 else
293 has_error := True
294 error_message_32 := l_session_control.error_message_32
295 end
296 else
297 has_error := True
298 error_message_32 := unexpected_error (Commit_name)
299 end
300 rescue
301 rescued := True
302 retry
303 end
304
305 insert_with_repository (an_obj: ANY; rep: DB_REPOSITORY)
306 -- Store in the database object `an_obj' with `db_repository'.
307 require
308 repository_loaded: rep.loaded
309 created: session_control_created
310 local
311 rescued: BOOLEAN
312 store_objects: DB_STORE
313 l_session_control: like session_control
314 do
315 if not rescued then
316 has_error := False
317 l_session_control := session_control
318 check l_session_control /= Void end -- implied by precondition
319 l_session_control.reset
320 create store_objects.make
321 store_objects.set_repository (rep)
322 store_objects.put (an_obj)
323 commit
324 else
325 has_error := True
326 error_message_32 := unexpected_error (Insert_name)
327 end
328 rescue
329 rescued := True
330 retry
331 end
332
333 feature -- Access
334
335 database_handle_name: STRING
336 -- Database handle name
337 require
338 created: database_handle_created
339 local
340 l_database_appl: like database_appl
341 do
342 l_database_appl := database_appl
343 check l_database_appl /= Void end -- implied by precondition
344 Result := l_database_appl.db_spec.database_handle_name
345 ensure
346 not_void: Result /= Void
347 end
348
349 string_format (s: READABLE_STRING_GENERAL): STRING
350 -- String representation in SQL of `s'.
351 require
352 created: database_handle_created
353 s_not_void: s /= Void
354 do
355 Result := string_format_32 (s).as_string_8
356 end
357
358 string_format_32 (s: READABLE_STRING_GENERAL): STRING_32
359 -- String representation in SQL of `s'.
360 require
361 created: database_handle_created
362 s_not_void: s /= Void
363 local
364 l_database_appl: like database_appl
365 do
366 l_database_appl := database_appl
367 check l_database_appl /= Void end -- implied by precondition
368 Result := l_database_appl.db_spec.string_format_32 (s)
369 end
370
371 session_control: detachable DB_CONTROL
372 -- Session control.
373
374 db_selection: DB_SELECTION
375 -- Performs a selection in the database (i.e. a query).
376 once
377 create Result.make
378 end
379
380 db_change: DB_CHANGE
381 -- Performs a change in the database (i.e. a command).
382 once
383 create Result.make
384 end
385
386 feature {NONE} -- Implementation
387
388 database_appl: detachable DATABASE_APPL [G]
389 -- Database application.
390
391 unexpected_error (action: STRING_32): STRING_32
392 -- Unexpected error message.
393 require
394 action_not_void: action /= Void
395 do
396 Result := {STRING_32} "Unexpected error in " + action
397 end
398
399 Connection_info_name: STRING = "set_connection_information"
400 -- `set_connection_information' feature name.
401
402 Connection_string_info_name: STRING = "set_connection_string_information"
403 -- `set_connection_string_information' feature name.
404
405 Establish_connection_name: STRING = "establish_connection"
406 -- `establish_connection' feature name.
407
408 Data_select_name: STRING = "load_data_with_select"
409 -- `load_data_with_select' feature name.
410
411 List_select_name: STRING = "load_list_with_select"
412 -- `load_list_with_select' feature name.
413
414 Execute_query_name: STRING = "execute_query_without_commit"
415 -- `execute_query_without_commit' feature name.
416
417 Commit_name: STRING = "commit"
418 -- `commit' feature name.
419
420 Insert_name: STRING = "insert_with_repository";
421 -- `insert_with_repository' feature name.
422
423 note
424 copyright: "Copyright (c) 1984-2006, Eiffel Software and others"
425 license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
426 source: "[
427 Eiffel Software
428 356 Storke Road, Goleta, CA 93117 USA
429 Telephone 805-685-1006, Fax 805-685-6869
430 Website http://www.eiffel.com
431 Customer support http://support.eiffel.com
432 ]"
433
434
435
436
437 end -- class DATABASE_MANAGER
438
439

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23