/[eiffelstudio]/branches/eth/eve/Src/library/abel/libraries/ethz/src/abel/relational/database_wrapper/sqlite/ps_sqlite_connection.e
ViewVC logotype

Contents of /branches/eth/eve/Src/library/abel/libraries/ethz/src/abel/relational/database_wrapper/sqlite/ps_sqlite_connection.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88969 - (show annotations)
Fri Jun 22 11:53:00 2012 UTC (7 years, 3 months ago) by romanschmocker
File size: 4165 byte(s)
Changed structure in the ABEL library repository - e.g. pre-built backends are now collected in their own cluster
1 note
2 description: "Wrapper for a SQLite connection."
3 author: "Roman Schmocker"
4 date: "$Date$"
5 revision: "$Revision$"
6
7 class
8 PS_SQLITE_CONNECTION
9
10 inherit
11 PS_SQL_CONNECTION_ABSTRACTION
12 inherit {NONE}
13 REFACTORING_HELPER
14
15 create {PS_SQLITE_DATABASE}
16 make
17
18 feature {PS_EIFFELSTORE_EXPORT} -- Settings
19
20 set_autocommit (flag:BOOLEAN)
21 -- Enable or disable autocommit
22 do
23 -- Autocommit has to be disabled with only a single connection, so don't enable it
24 end
25
26 feature {PS_EIFFELSTORE_EXPORT} -- Database operations
27
28 execute_sql (statement: STRING)
29 -- Execute the SQL statement `statement', and store the result (if any) in `Current.last_result'
30 -- In case of an error, it will report it in `last_error' and raise an exception.
31 local
32 all_statements: LIST[STRING]
33 stmt: SQLITE_QUERY_STATEMENT
34 result_list: LINKED_LIST[PS_SQLITE_ROW]
35 res: SQLITE_STATEMENT_ITERATION_CURSOR
36 do
37 -- By default we can get multiple SQL statements - SQLite somehow handles them differently, therefore split them.
38 all_statements:=statement.split (';')
39 all_statements.do_all (agent {STRING}.trim)
40 -- Remove all empty statements, and add a `;' to all non-empty ones (as opposed to MySQL, SQLite needs this character)
41 from all_statements.start
42 until all_statements.after
43 loop
44 if all_statements.item.is_empty then
45 all_statements.remove
46 else
47 all_statements.item.extend (';')
48 all_statements.forth
49 end
50 end
51
52 -- Void safety...
53 create result_list.make
54
55 -- Execute all statements
56 across all_statements as current_statement loop
57
58 create result_list.make
59 create stmt.make (current_statement.item, internal_connection)
60 create res.make (stmt) -- This executes the statement
61
62 -- Do error handling
63 if stmt.has_error then
64 -- Print information for easier debugging
65 print (current_statement.item)
66 if attached stmt.last_exception as ex then
67 print (ex.meaning)
68 end
69 last_error:= get_error
70 last_error.raise
71 end
72
73 -- Collect the result (if any)
74 from
75 until res.after
76 loop
77 result_list.extend (create {PS_SQLITE_ROW}.make(res.item))
78 res.forth
79 end
80 end
81
82 -- Return the cursor of the result list
83 last_result:= result_list.new_cursor
84
85 rescue
86 -- Information to faciliate error handling
87 if attached internal_connection.last_exception as ex then
88 print ("%N%N"+statement + "%N")
89 print (ex.meaning + "%N")
90 end
91 end
92
93 commit
94 -- Commit the currently active transaction.
95 -- In case of an error, including a failed commit, it will report it in `last_error' and raise an exception.
96 do
97 internal_connection.commit
98 internal_connection.begin_transaction (False)
99 end
100
101 rollback
102 -- Rollback the currently active transaction.
103 -- In case of an error, it will report it in `last_error' and raise an exception.
104 do
105 internal_connection.rollback
106 internal_connection.begin_transaction (False)
107 end
108
109 feature {PS_EIFFELSTORE_EXPORT} -- Database results
110
111 last_result: ITERATION_CURSOR[PS_SQL_ROW_ABSTRACTION]
112 -- The result of the last database operation
113
114
115 last_error: PS_ERROR
116 -- The last occured error
117
118
119 feature {PS_SQLITE_DATABASE} -- Initialization
120
121 make (connection: SQLITE_DATABASE)
122 -- Initialization for `Current'.
123 do
124 internal_connection:= connection
125 create {PS_NO_ERROR} last_error
126
127 last_result:= (create {LINKED_LIST[PS_SQL_ROW_ABSTRACTION]}.make).new_cursor
128
129 -- The default isolation level is Serializable, and it can't be changed to something else
130 create transaction_isolation_level
131 transaction_isolation_level:= transaction_isolation_level.serializable
132 end
133
134 internal_connection: SQLITE_DATABASE
135
136 feature {NONE} -- Error handling
137
138 get_error: PS_ERROR
139 -- Translate the SQLite specific error to an ABEL error object.
140 local
141 error_number: INTEGER
142 do
143 if attached internal_connection.last_exception as exception then
144 error_number:= exception.result_code
145
146 fixme ("TODO: Extend this for all types of errors")
147 create {PS_GENERAL_ERROR} Result.make (exception.meaning)
148
149 else
150 create {PS_GENERAL_ERROR} Result.make ("Unknown error")
151 end
152
153 end
154 end

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.23