Added persistence support for Eiffel sqlite3 wrapper.

Updated existing persistency solution to be more generic to any db solution.
This commit is contained in:
2015-10-05 16:04:10 +02:00
parent 7fcacad5eb
commit eb5ae32e46
18 changed files with 859 additions and 160 deletions

View File

@@ -86,8 +86,23 @@ feature -- Query
sql_post_execution
end
sql_change (a_sql_statement: STRING; a_params: detachable STRING_TABLE [detachable ANY])
-- Execute an sql query change `a_sql_statement' with the params `a_params'.
sql_finalize
-- <Precursor>
do
-- N/A
end
sql_insert (a_sql_statement: STRING; a_params: detachable STRING_TABLE [detachable ANY])
-- <Precursor>
do
check_sql_query_validity (a_sql_statement, a_params)
db_handler.set_query (create {DATABASE_QUERY}.data_reader (a_sql_statement, a_params))
db_handler.execute_change
sql_post_execution
end
sql_modify (a_sql_statement: STRING; a_params: detachable STRING_TABLE [detachable ANY])
-- <Precursor>
do
check_sql_query_validity (a_sql_statement, a_params)
db_handler.set_query (create {DATABASE_QUERY}.data_reader (a_sql_statement, a_params))
@@ -133,4 +148,32 @@ feature -- Query
end
end
sql_read_integer_32 (a_index: INTEGER): INTEGER_32
-- Retrieved value at `a_index' position in `item'.
local
l_item: like sql_item
do
l_item := sql_item (a_index)
if attached {INTEGER_32} l_item as i then
Result := i
elseif attached {INTEGER_32_REF} l_item as l_value then
Result := l_value.item
else
check is_integer_32: False end
end
end
sql_read_date_time (a_index: INTEGER): detachable DATE_TIME
-- Retrieved value at `a_index' position in `item'.
local
l_item: like sql_item
do
l_item := sql_item (a_index)
if attached {DATE_TIME} l_item as dt then
Result := dt
else
check is_date_time_or_null: l_item = Void end
end
end
end

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-13-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-13-0 http://www.eiffel.com/developers/xml/configuration-1-13-0.xsd" name="persistence_sqlite3" uuid="4E536C92-A09F-4305-8230-2EC5ABC51416" library_target="persistence_sqlite3">
<target name="persistence_sqlite3">
<root all_classes="true"/>
<option warning="true" void_safety="all">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<setting name="console_application" value="true"/>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="cms" location="..\..\..\cms-safe.ecf"/>
<library name="crypto" location="$ISE_LIBRARY\unstable\library\text\encryption\crypto\crypto-safe.ecf"/>
<library name="encoder" location="$ISE_LIBRARY\contrib\library\web\framework\ewf\text\encoder\encoder-safe.ecf"/>
<library name="error" location="$ISE_LIBRARY\contrib\library\utility\general\error\error-safe.ecf"/>
<library name="json" location="$ISE_LIBRARY\contrib\library\text\parser\json\library\json-safe.ecf" readonly="false"/>
<library name="app_env" location="..\..\app_env\app_env-safe.ecf"/>
<library name="logging" location="$ISE_LIBRARY\library\runtime\logging\logging-safe.ecf"/>
<library name="model" location="..\..\model\cms_model-safe.ecf"/>
<library name="sqlite3" location="$ISE_LIBRARY\unstable\library\persistency\database\sqlite3\sqlite-safe.ecf" readonly="false"/>
<library name="thread" location="$ISE_LIBRARY\library\thread\thread-safe.ecf"/>
<library name="time" location="$ISE_LIBRARY\library\time\time-safe.ecf"/>
<!--
<cluster name="common" location="..\implementation\store\" recursive="true"/>
-->
<cluster name="persistence_sqlite" location=".\src\" recursive="true">
<file_rule>
<exclude>/EIFGENs$</exclude>
<exclude>/CVS$</exclude>
<exclude>/.svn$</exclude>
<exclude>/old$</exclude>
</file_rule>
</cluster>
</target>
</system>

View File

@@ -0,0 +1,408 @@
note
description: "Summary description for {CMS_STORAGE_MYSQL}."
date: "$Date: 2015-02-09 22:29:56 +0100 (lun., 09 févr. 2015) $"
revision: "$Revision: 96596 $"
class
CMS_STORAGE_SQLITE3
inherit
CMS_STORAGE_SQL
redefine
sql_read_date_time, sql_read_integer_32
end
CMS_CORE_STORAGE_SQL_I
redefine
sql_read_date_time, sql_read_integer_32
end
CMS_USER_STORAGE_SQL_I
redefine
sql_read_date_time, sql_read_integer_32
end
SQLITE_BIND_ARG_MARSHALLER
REFACTORING_HELPER
create
make
feature {NONE} -- Initialization
make (db: SQLITE_DATABASE)
do
sqlite := db
create error_handler.make
end
sqlite: SQLITE_DATABASE
-- Associated SQLite database.
feature -- Status report
is_initialized: BOOLEAN
-- Is storage initialized?
do
Result := has_user
end
feature -- Status report
is_available: BOOLEAN
-- Is storage available?
do
Result := sqlite.is_interface_usable
end
feature -- Basic operation
close
-- Close/disconnect current storage.
do
sqlite.close
end
feature -- Execution
transaction_depth: INTEGER
sql_begin_transaction
-- Start a database transtaction.
do
if transaction_depth = 0 then
sqlite.begin_transaction (False)
end
transaction_depth := transaction_depth + 1
end
sql_rollback_transaction
-- Rollback updates in the database.
do
if sqlite.is_in_transaction then
sqlite.rollback
end
transaction_depth := transaction_depth - 1
end
sql_commit_transaction
-- Commit updates in the database.
do
if sqlite.is_in_transaction then
sqlite.commit
end
transaction_depth := transaction_depth - 1
end
sql_post_execution
-- Post database execution.
-- note: execute after each `sql_query' and `sql_change'.
do
-- FIXME
if sqlite.has_error then
write_critical_log (generator + ".post_execution Error occurred!")
end
end
feature -- Operation
last_statement: detachable SQLITE_STATEMENT
last_sqlite_result_cursor: detachable SQLITE_STATEMENT_ITERATION_CURSOR
sql_query (a_sql_statement: STRING; a_params: detachable STRING_TABLE [detachable ANY])
-- <Precursor>
local
st: SQLITE_QUERY_STATEMENT
do
last_sqlite_result_cursor := Void
create st.make (a_sql_statement, sqlite)
last_statement := st
if st.is_compiled then
if a_params /= Void then
check st.has_arguments end
last_sqlite_result_cursor := st.execute_new_with_arguments (sqlite_arguments (a_params))
else
last_sqlite_result_cursor := st.execute_new
end
else
error_handler.add_custom_error (1, "invalid query", "query compilation failed!")
end
end
sql_finalize
-- Finalize sql query (i.e destroy previous query statement.
do
if attached last_statement as st then
st.dispose
end
last_sqlite_result_cursor := Void
last_statement := Void
end
sql_insert (a_sql_statement: STRING; a_params: detachable STRING_TABLE [detachable ANY])
-- <Precursor>
local
st: SQLITE_INSERT_STATEMENT
do
last_sqlite_result_cursor := Void
create st.make (a_sql_statement, sqlite)
last_statement := st
if st.is_compiled then
if a_params /= Void then
check st.has_arguments end
last_sqlite_result_cursor := st.execute_new_with_arguments (sqlite_arguments (a_params))
else
last_sqlite_result_cursor := st.execute_new
end
else
error_handler.add_custom_error (1, "invalid query", "query compilation failed!")
end
end
sql_modify (a_sql_statement: STRING; a_params: detachable STRING_TABLE [detachable ANY])
-- <Precursor>
local
st: SQLITE_MODIFY_STATEMENT
do
last_sqlite_result_cursor := Void
create st.make (a_sql_statement, sqlite)
last_statement := st
if st.is_compiled then
if a_params /= Void then
check st.has_arguments end
last_sqlite_result_cursor := st.execute_new_with_arguments (sqlite_arguments (a_params))
else
last_sqlite_result_cursor := st.execute_new
end
else
error_handler.add_custom_error (1, "invalid query", "query compilation failed!")
end
end
sqlite_arguments (a_params: STRING_TABLE [detachable ANY]): ARRAYED_LIST [SQLITE_BIND_ARG [ANY]]
local
k: READABLE_STRING_GENERAL
k8: STRING
do
create Result.make (a_params.count)
across
a_params as ic
loop
k := ic.key
if k.is_valid_as_string_8 then
k8 := k.as_string_8
else
k8 := (create {UTF_CONVERTER}).utf_32_string_to_utf_8_string_8 (k)
end
if attached {DATE_TIME} ic.item as dt then
Result.force (new_binding_argument (date_time_to_string (dt), ":" + k8))
else
Result.force (new_binding_argument (ic.item, ":" + k8))
end
end
end
date_time_to_string (dt: DATE_TIME): STRING
do
create Result.make (16)
Result.append_integer (dt.year)
Result.append_character ('-')
if dt.month <= 9 then
Result.append_character ('0')
end
Result.append_integer (dt.month)
Result.append_character ('-')
if dt.day <= 9 then
Result.append_character ('0')
end
Result.append_integer (dt.day)
Result.append_character (' ')
if dt.hour <= 9 then
Result.append_character ('0')
end
Result.append_integer (dt.hour)
Result.append_character (':')
if dt.minute <= 9 then
Result.append_character ('0')
end
Result.append_integer (dt.minute)
Result.append_character (':')
if dt.second <= 9 then
Result.append_character ('0')
end
Result.append_integer (dt.second)
end
string_to_date_time (a_string: READABLE_STRING_GENERAL): DATE_TIME
local
y,m,d: INTEGER
h,min,sec: INTEGER
s: detachable READABLE_STRING_GENERAL
i,j: INTEGER
do
i := 1
-- YYYY
j := a_string.index_of ('-', i)
s := a_string.substring (i, j - 1)
y := s.to_integer
i := j + 1
-- /MM
j := a_string.index_of ('-', i)
s := a_string.substring (i, j - 1)
m := s.to_integer
i := j + 1
-- /DD
j := a_string.index_of (' ', i)
s := a_string.substring (i, j - 1)
d := s.to_integer
i := j + 1
-- %THour
j := a_string.index_of (':', i)
s := a_string.substring (i, j - 1)
h := s.to_integer
i := j + 1
-- :Min
j := a_string.index_of (':', i)
s := a_string.substring (i, j - 1)
min := s.to_integer
i := j + 1
-- :Sec
j := a_string.count + 1
s := a_string.substring (i, j - 1)
sec := s.to_integer
create Result.make (y,m,d,h,min,sec)
end
feature -- Access
-- sql_rows_count: INTEGER
-- -- Number of rows for last sql execution.
-- do
-- if attached last_sqlite_result_cursor as l_cursor then
-- -- FIXME: find better solution!
-- from
-- Result := 1
-- until
-- not l_cursor.after
-- loop
-- Result := Result + 1
-- end
-- end
-- end
sql_start
-- Set the cursor on first element.
do
-- Already at first position if any ?
if attached last_sqlite_result_cursor as l_cursor then
-- l_cursor.start
end
end
sql_after: BOOLEAN
-- Are there no more items to iterate over?
do
if attached last_sqlite_result_cursor as l_cursor then
Result := l_cursor.after
end
end
sql_forth
-- Fetch next row from last sql execution, if any.
do
if attached last_sqlite_result_cursor as l_cursor then
l_cursor.forth
end
end
sql_valid_item_index (a_index: INTEGER): BOOLEAN
local
l_row: SQLITE_RESULT_ROW
do
if attached last_sqlite_result_cursor as l_cursor then
l_row := l_cursor.item
Result := a_index > 0 and a_index.to_natural_32 <= l_row.count
end
end
sql_item (a_index: INTEGER): detachable ANY
local
l_row: SQLITE_RESULT_ROW
do
if attached last_sqlite_result_cursor as l_cursor then
l_row := l_cursor.item
Result := l_row.value (a_index.to_natural_32)
end
end
sql_read_integer_32 (a_index: INTEGER): INTEGER_32
-- Retrieved value at `a_index' position in `item'.
local
l_item: like sql_item
i64: INTEGER_64
do
l_item := sql_item (a_index)
if attached {INTEGER_32} l_item as i then
Result := i
elseif attached {INTEGER_32_REF} l_item as l_value then
Result := l_value.item
else
if attached {INTEGER_64} l_item as i then
i64 := i
elseif attached {INTEGER_64_REF} l_item as l_value then
i64 := l_value.item
else
check is_integer_32: False end
end
if i64 <= {INTEGER_32}.max_value then
Result := i64.to_integer_32
else
check is_integer_32: False end
end
end
end
sql_read_date_time (a_index: INTEGER): detachable DATE_TIME
-- Retrieved value at `a_index' position in `item'.
local
l_item: like sql_item
do
l_item := sql_item (a_index)
if attached {DATE_TIME} l_item as dt then
Result := dt
elseif attached {READABLE_STRING_GENERAL} l_item as s then
Result := string_to_date_time (s)
else
check is_date_time_nor_null: l_item = Void end
end
end
feature -- Conversion
sql_statement (a_statement: STRING): STRING
-- <Precursor>.
local
i: INTEGER
do
Result := a_statement
from
i := 1
until
i = 0
loop
i := a_statement.substring_index ("AUTO_INCREMENT", i)
if i > 0 then
if Result = a_statement then
create Result.make_from_string (a_statement)
end
Result.remove (i + 4)
i := i + 14
end
end
end
end

View File

@@ -0,0 +1,90 @@
note
description: "[
Objects that ...
]"
author: "$Author: jfiat $"
date: "$Date: 2015-02-13 13:08:13 +0100 (ven., 13 févr. 2015) $"
revision: "$Revision: 96616 $"
class
CMS_STORAGE_SQLITE3_BUILDER
inherit
CMS_STORAGE_SQL_BUILDER
create
make
feature {NONE} -- Initialization
make
-- Initialize `Current'.
do
end
feature -- Factory
storage (a_setup: CMS_SETUP; a_error_handler: ERROR_HANDLER): detachable CMS_STORAGE_SQLITE3
local
s: detachable READABLE_STRING_32
p: PATH
db: detachable SQLITE_DATABASE
l_source: SQLITE_FILE_SOURCE
i,j: INTEGER
do
if
attached (create {APPLICATION_JSON_CONFIGURATION_HELPER}).new_database_configuration (a_setup.environment.application_config_path) as l_database_config
then
if l_database_config.driver.is_case_insensitive_equal ("sqlite3") then
s := l_database_config.database_string
i := s.substring_index ("Database=", 1)
if i > 0 then
i := s.index_of ('=', i) + 1
j := s.index_of (';', i)
if j = 0 then
j := s.count + 1
end
create p.make_from_string (s.substring (i, j - 1))
else
create p.make_from_string (s)
end
if attached reuseable_connection.item as d then
if p.same_as (d.path) then
db := d.database
end
end
if db = Void or else db.is_closed then
create l_source.make (p.name)
create db.make (l_source)
if l_source.exists then
db.open_read_write
else
db.open_create_read_write
end
end
if not db.is_closed then
db.set_busy_timeout (1_000) -- FIXME
create Result.make (db)
-- set_map_zero_null_value (False) --| This way we map 0 to 0, instead of Null as default.
if Result.is_available then
if not Result.is_initialized then
initialize (a_setup, Result)
end
end
else
a_error_handler.add_custom_error (0, "Could not connect to the ODBC storage", Void)
end
else
-- Wrong mapping between storage name and storage builder!
end
end
end
reuseable_connection: CELL [detachable TUPLE [path: PATH; database: SQLITE_DATABASE]]
once
create Result.put (Void)
end
end