aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v5/db_select.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sisu/v5/db_select.rb')
-rw-r--r--lib/sisu/v5/db_select.rb166
1 files changed, 77 insertions, 89 deletions
diff --git a/lib/sisu/v5/db_select.rb b/lib/sisu/v5/db_select.rb
index 26bbd3da..333c3f0c 100644
--- a/lib/sisu/v5/db_select.rb
+++ b/lib/sisu/v5/db_select.rb
@@ -61,30 +61,87 @@
sqlite
=end
+module SiSU_DbAction
+ def db_action(sdb)
+ @sdb=sdb
+ def createdb
+ @sdb[:create].output_dir?
+ begin
+ @sdb[:create].create_db
+ rescue
+ @sdb[:create].output_dir?
+ end
+ end
+ def drop
+ @sdb[:drop].drop.tables
+ end
+ def create
+ @sdb[:create].output_dir?
+ begin
+ @sdb[:create].create_table.metadata_and_text
+ @sdb[:create].create_table.doc_objects
+ @sdb[:create].create_table.endnotes
+ @sdb[:create].create_table.endnotes_asterisk
+ @sdb[:create].create_table.endnotes_plus
+ @sdb[:create].create_table.urls
+ @sdb[:index].create_indexes
+ rescue
+ SiSU_Errors::Rescued.new($!,$@,'--sqlite').location
+ @sdb[:create].output_dir? do
+ __LINE__.to_s + ':' + __FILE__
+ end
+ end
+ end
+ def import
+ db_exist?
+ @sdb[:import].marshal_load
+ tell=case @sql_type
+ when :sqlite
+ SiSU_Screen::Ansi.new(
+ @opt.act[:color_state][:set],
+ "sqlite3 #{@db.sqlite.db} database?"
+ )
+ when :pg
+ SiSU_Screen::Ansi.new(
+ @opt.act[:color_state][:set],
+ "pgaccess or psql #{@db.psql.db} database?"
+ )
+ else '???'
+ end
+ tell.puts_grey if @opt.act[:verbose][:set]==:on
+ end
+ def remove
+ db_exist?
+ @sdb[:remove_doc].remove
+ end
+ def update
+ remove
+ import
+ end
+ self
+ end
+end
module SiSU_DbSelect
class Case
+ include SiSU_DbAction
def initialize(opt,conn='',sql_type=:pg)
@opt,@conn,@sql_type=opt,conn,sql_type
@db=SiSU_Env::InfoDb.new
@file_maint=sql_maintenance_file
- @sdb=
- SiSU_DbDBI::Create.new(@opt,@conn,@file_maint,@sql_type)
- @sdb_index=
- SiSU_DbDBI::Index.new(@opt,@conn,@file_maint,@sql_type)
- @sdb_no=
- SiSU_DbDBI::Drop.new(@opt,@conn,@db,@sql_type)
+ @sdb={
+ create: SiSU_DbDBI::Create.new(@opt,@conn,@file_maint,@sql_type),
+ index: SiSU_DbDBI::Index.new(@opt,@conn,@file_maint,@sql_type),
+ drop: SiSU_DbDBI::Drop.new(@opt,@conn,@db,@sql_type),
+ }
if (@opt.act[:psql_import][:set]==:on \
|| @opt.act[:psql_update][:set]==:on) \
or (@opt.act[:sqlite_import][:set]==:on \
|| @opt.act[:sqlite_update][:set]==:on)
- @sdb_import=
- SiSU_DbDBI::Import.new(@opt,@conn,@file_maint,@sql_type)
- @sdb_remove_doc=
- SiSU_DbDBI::Remove.new(@opt,@conn,@file_maint,@sql_type)
+ @sdb[:import]=SiSU_DbDBI::Import.new(@opt,@conn,@file_maint,@sql_type)
+ @sdb[:remove_doc]=SiSU_DbDBI::Remove.new(@opt,@conn,@file_maint,@sql_type)
elsif (@opt.act[:psql_remove][:set]==:on \
or @opt.act[:sqlite_remove][:set]==:on)
- @sdb_remove_doc=
- SiSU_DbDBI::Remove.new(@opt,@conn,@file_maint,@sql_type)
+ @sdb[:remove_doc]=SiSU_DbDBI::Remove.new(@opt,@conn,@file_maint,@sql_type)
end
end
def db_exist?
@@ -99,13 +156,7 @@ module SiSU_DbSelect
+ 'Attempting to initialize db' + "\n" \
+ 'Creating db tables'
)
- @sdb.create_table.metadata_and_text
- @sdb.create_table.doc_objects
- @sdb.create_table.endnotes
- @sdb.create_table.endnotes_asterisk
- @sdb.create_table.endnotes_plus
- @sdb.create_table.urls
- @sdb_index.create_indexes
+ db_action(@sdb).create
end
if @conn.is_a?(NilClass)
if @sql_type==:sqlite
@@ -117,13 +168,7 @@ module SiSU_DbSelect
+ 'Attempting to initialize db' + "\n" \
+ 'Creating db tables'
)
- @sdb.create_table.metadata_and_text
- @sdb.create_table.doc_objects
- @sdb.create_table.endnotes
- @sdb.create_table.endnotes_asterisk
- @sdb.create_table.endnotes_plus
- @sdb.create_table.urls
- @sdb_index.create_indexes
+ db_action(@sdb).create
@db.sqlite.db
else
puts %{no connection with pg database established, you may need to run:\n} \
@@ -156,87 +201,30 @@ module SiSU_DbSelect
end
file
end
- def db_action
- def createdb
- @sdb.output_dir?
- begin
- @sdb.create_db
- rescue
- @sdb.output_dir?
- end
- end
- def drop
- @sdb_no.drop.tables
- end
- def create
- @sdb.output_dir?
- begin
- @sdb.create_table.metadata_and_text
- @sdb.create_table.doc_objects
- @sdb.create_table.endnotes
- @sdb.create_table.endnotes_asterisk
- @sdb.create_table.endnotes_plus
- @sdb.create_table.urls
- @sdb_index.create_indexes
- rescue
- SiSU_Errors::Rescued.new($!,$@,'--sqlite').location
- @sdb.output_dir? do
- __LINE__.to_s + ':' + __FILE__
- end
- end
- end
- def import
- db_exist?
- @sdb_import.marshal_load
- tell=case @sql_type
- when :sqlite
- SiSU_Screen::Ansi.new(
- @opt.act[:color_state][:set],
- "sqlite3 #{@db.sqlite.db} database?"
- )
- when :pg
- SiSU_Screen::Ansi.new(
- @opt.act[:color_state][:set],
- "pgaccess or psql #{@db.psql.db} database?"
- )
- else '???'
- end
- tell.puts_grey if @opt.act[:verbose][:set]==:on
- end
- def remove
- db_exist?
- @sdb_remove_doc.remove
- end
- def update
- remove
- import
- end
- self
- end
def cases
if @opt.act[:psql_drop][:set] ==:on \
or @opt.act[:sqlite_drop][:set] ==:on
- db_action.drop
+ db_action(@sdb).drop
end
if @opt.act[:psql_createdb][:set] ==:on \
or @opt.act[:sqlite_createdb][:set] ==:on
- db_action.createdb
+ db_action(@sdb).createdb
end
if @opt.act[:psql_create][:set] ==:on \
or @opt.act[:sqlite_create][:set] ==:on
- db_action.create
+ db_action(@sdb).create
end
if @opt.act[:psql_update][:set] ==:on \
or @opt.act[:sqlite_update][:set] ==:on
- db_action.update
+ db_action(@sdb).update
else
if @opt.act[:psql_remove][:set] ==:on \
or @opt.act[:sqlite_remove][:set] ==:on
- db_action.remove
+ db_action(@sdb).remove
end
if @opt.act[:psql_import][:set] ==:on \
or @opt.act[:sqlite_import][:set] ==:on
- db_action.import
+ db_action(@sdb).import
end
end
end