diff options
Diffstat (limited to 'lib/sisu/v5/db_select.rb')
-rw-r--r-- | lib/sisu/v5/db_select.rb | 239 |
1 files changed, 118 insertions, 121 deletions
diff --git a/lib/sisu/v5/db_select.rb b/lib/sisu/v5/db_select.rb index 3aebe828..3f232c14 100644 --- a/lib/sisu/v5/db_select.rb +++ b/lib/sisu/v5/db_select.rb @@ -67,40 +67,65 @@ module SiSU_DbSelect @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) # db_dbi.rb - @sdb_index=SiSU_DbDBI::Index.new(@opt,@conn,@file_maint,@sql_type) # db_dbi.rb - @sdb_no=SiSU_DbDBI::Drop.new(@opt,@conn,@db,@sql_type) # db_dbi.rb - if @opt.mod.inspect =~/update|import/ - @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.mod.inspect =~/remove/ - @sdb_remove_doc=SiSU_DbDBI::Remove.new(@opt,@conn,@file_maint,@sql_type) + @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) + 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) + 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) end end def db_exist? if @sql_type==:sqlite \ - and (not (FileTest.file?(@db.sqlite.db)) or FileTest.zero?(@db.sqlite.db)) - puts %{no connection with #{@sql_type} database established, createdb "#{@db.sqlite.db}"?} + and (not (FileTest.file?(@db.sqlite.db)) \ + or FileTest.zero?(@db.sqlite.db)) + puts %{no connection with #{@sql_type} database established, you may need to run:\n} \ + + %{ sisu --#{@sql_type} --createall\n} \ + + %{ before attempting to populate the database} exit end if @conn.is_a?(NilClass) - db=@sql_type==:sqlite \ - ? @db.sqlite.db - : @db.psql.db - puts %{no connection with #{@sql_type} database established, createdb "#{db}"?} + db=if @sql_type==:sqlite + puts %{no connection with #{@sql_type} database established, you may need to run:\n} \ + + %{ sisu --#{@sql_type} --createall\n} \ + + %{ before attempting to populate the database} + @db.sqlite.db + else + puts %{no connection with #{@sql_type} database established, you may need to run:\n} \ + + %{ createdb "#{@db.psql.db}"\n} \ + + %{ after that don't forget to run:\n} \ + + %{ sisu --#{@sql_type} --createall\n} \ + + %{ before attempting to populate the database} + @db.psql.db + end exit end end def sql_maintenance_file - file=if @opt.inspect =~/M/ + file=if @opt.act[:maintenance][:set]==:on if @opt.fns and not @opt.fns.empty? @env=SiSU_Env::InfoEnv.new(@opt.fns) if @opt.fns - puts "\n#{@env.processing_path.sqlite}/#{@opt.fns}.sql" if @sql_type ==:sqlite and @opt.act[:maintenance][:set]==:on + if @sql_type ==:sqlite and @opt.act[:maintenance][:set]==:on + puts "\n#{@env.processing_path.sqlite}/#{@opt.fns}.sql" + end @db=SiSU_Env::InfoDb.new @job="sqlite3 #{@db.sqlite.db} < #{@env.processing_path.sqlite}/#{@opt.fns}.sql" File.new("#{@env.processing_path.sqlite}/#{@opt.fns}.sql",'w+') elsif @opt.fns \ - and @opt.fns.inspect =~/create/ + and (@opt.act[:sqlite_create][:set] ==:on \ + || @opt.act[:psql_create][:set] ==:on) nil #sort variations later else nil end @@ -108,116 +133,88 @@ module SiSU_DbSelect end file end - def cases - @opt.mod.each do |mod| - case mod - when /^--createdb$/ - @sdb.output_dir? - begin - @sdb.create_db - rescue; @sdb.output_dir? - end - when /^--(?:init(?:ialize)?|create(?:all)?)$/ - @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($!,$@,'-D').location; @sdb.output_dir? do - __LINE__.to_s + ':' + __FILE__ - end - end - when /^--createtables?$/ + def db_action + def createdb + @sdb.output_dir? + begin + @sdb.create_db + rescue @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; @sdb.output_dir? - end - when /^--recreate$/ - @sdb.output_dir? - begin - @sdb_no.drop.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 - rescue; @sdb.output_dir? - end - when /^--cr(eate)?lex$/ - @sdb.output_dir? - begin - @sdb.create_table.doc_objects - rescue; @sdb.output_dir? - end - when /^--cr(eate)?metadata$/ - @sdb.output_dir? - begin - @sdb.create_table.metadata_and_text - rescue; @sdb.output_dir? - end - when /^--import$/ - db_exist? - @sdb_import.marshal_load - tell=case @sql_type - when :sqlite then SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],"sqlite3 #{@db.sqlite.db} database?") - when :pg then 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 - when /^--update$/ - db_exist? - @sdb_remove_doc.remove - @sdb_import.marshal_load - tell=case @sql_type - when :sqlite then SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],"sqlite3 #{@db.sqlite.db} database?") - when :pg then 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 - when /^--remove$/ - db_exist? - @sdb_remove_doc.remove - when /^--index$/ - db_exist? + 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 - when /^--droptable(s)?$/ - db_exist? - @sdb_no.drop.tables - when /^--dropindex(es)?$/ - db_exist? - @sdb_no.drop.indexes - when /^--(?:dropall|drop)$/ - db_exist? - @sdb_no.drop.tables - when /^--(?:db=)?(?:(?:sq)?lite|pg(?:sql)?|my(?:sql)?)$/ - when /^--(?:v\d+|dev)$/ - else + rescue + SiSU_Errors::Rescued.new($!,$@,'--sqlite').location + @sdb.output_dir? do + __LINE__.to_s + ':' + __FILE__ + end end - if @opt.act[:maintenance][:set]==:on \ - and @opt.cmd =~/d/ - puts @job + 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 - if @opt.act[:psql][:set]==:on \ - and @opt.mod.inspect !~/--(?:createdb|init(?:ialize)?|create(?:all)?|createtables?|recreate|cr(eate)?lex|cr(eate)?metadata|import|update|remove|index|droptable(s)?|dropindex(es)?|dropall|drop)/ - SiSU_Screen::Ansi.new(@opt.act[:color_state][:set],"--pg requires further instruction").warn unless @opt.act[:quiet][:set]==:on + def update + remove + import end - begin - rescue; @sdb.output_dir? + self + end + def cases + if @opt.act[:psql_drop][:set] ==:on \ + or @opt.act[:sqlite_drop][:set] ==:on + db_action.drop + end + if @opt.act[:psql_createdb][:set] ==:on \ + or @opt.act[:sqlite_createdb][:set] ==:on + db_action.createdb + end + if @opt.act[:psql_create][:set] ==:on \ + or @opt.act[:sqlite_create][:set] ==:on + db_action.create + end + if @opt.act[:psql_update][:set] ==:on \ + or @opt.act[:sqlite_update][:set] ==:on + db_action.update + else + if @opt.act[:psql_remove][:set] ==:on \ + or @opt.act[:sqlite_remove][:set] ==:on + db_action.remove + end + if @opt.act[:psql_import][:set] ==:on \ + or @opt.act[:sqlite_import][:set] ==:on + db_action.import + end end end end |