d: utils_response, rely on module (remove class) extend module
[software/sisu] / lib / sisu / develop / db_drop.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** modules shared by the different db types, dbi, postgresql, sqlite
8
9 ** Author: Ralph Amissah
10 <ralph@amissah.com>
11 <ralph.amissah@gmail.com>
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see <http://www.gnu.org/licenses/>.
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 <http://www.fsf.org/licensing/licenses/gpl.html>
39 <http://www.gnu.org/licenses/gpl.html>
40
41 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
42
43 ** SiSU uses:
44 * Standard SiSU markup syntax,
45 * Standard SiSU meta-markup syntax, and the
46 * Standard SiSU object citation numbering and system
47
48 ** Hompages:
49 <http://www.jus.uio.no/sisu>
50 <http://www.sisudoc.org>
51
52 ** Git
53 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/db_drop.rb;hb=HEAD>
55
56 =end
57 module SiSU_DbDrop
58 require_relative 'utils_response' # utils_response.rb
59 class Drop
60 include SiSU_Response
61 def initialize(opt,conn,db_info,sql_type)
62 @opt,@conn,@db_info,@sql_type=opt,conn,db_info,sql_type
63 case @sql_type
64 when :sqlite
65 cascade=''
66 else
67 cascade='CASCADE'
68 end
69 @drop_table=[
70 "DROP TABLE metadata_and_text #{cascade};",
71 "DROP TABLE doc_objects #{cascade};",
72 "DROP TABLE urls #{cascade};",
73 "DROP TABLE endnotes #{cascade};",
74 "DROP TABLE endnotes_asterisk #{cascade};",
75 "DROP TABLE endnotes_plus #{cascade};",
76 ]
77 end
78 def drop
79 def tables #% drop all tables
80 begin
81 msg_sqlite="as not all disk space is recovered after dropping the database << #{@db_info.sqlite.db} >>, you may be better off deleting the file, and recreating it as necessary"
82 case @sql_type
83 when :sqlite
84 puts msg_sqlite
85 ans=response?('remove sql database?')
86 if ans \
87 and File.exist?(@db_info.sqlite.db)
88 @conn.close
89 File.unlink(@db_info.sqlite.db)
90 db=SiSU_Env::InfoDb.new
91 conn=db.sqlite.conn_sqlite3
92 sdb=SiSU_DbDBI::Create.new(@opt,conn,@db_info,@sql_type)
93 sdb_index=SiSU_DbDBI::Index.new(@opt,conn,@db_info,@sql_type)
94 sdb.output_dir?
95 begin
96 sdb.create_db
97 sdb.create_table.metadata_and_text
98 sdb.create_table.doc_objects
99 sdb.create_table.endnotes
100 sdb.create_table.endnotes_asterisk
101 sdb.create_table.endnotes_plus
102 sdb.create_table.urls
103 sdb_index.create_indexes
104 rescue
105 SiSU_Errors::Rescued.new($!,$@,'-D').location do
106 __LINE__.to_s + ':' + __FILE__
107 end
108 sdb.output_dir?
109 end
110 exit
111 else
112 @conn.transaction
113 @drop_table.each do |d|
114 begin
115 @conn.exec_params(d)
116 rescue
117 next
118 end
119 end
120 @conn.commit
121 end
122 when :pg
123 @conn.transaction
124 @drop_table.each do |d|
125 begin
126 @conn.exec_params(d)
127 rescue
128 next
129 end
130 end
131 @conn.commit
132 end
133 rescue
134 case @sql_type
135 when :sqlite
136 ans=response?('remove sql database?')
137 if ans and File.exist?(@db_info.sqlite.db); File.unlink(@db_info.sqlite.db)
138 end
139 else
140 @drop_table.each do |d|
141 begin
142 @conn.exec_params(d)
143 rescue
144 next
145 end
146 end
147 end
148 ensure
149 end
150 end
151 def indexes
152 def conn_execute_array(sql_arr)
153 @conn.transaction do |conn|
154 sql_arr.each do |sql|
155 begin
156 conn.exec_params(sql)
157 rescue
158 next
159 end
160 end
161 end
162 end
163 def base #% drop base indexes
164 print "\n drop documents common indexes\n" unless @opt.act[:quiet][:set]==:on
165 sql_arr=[
166 %{DROP INDEX idx_title;},
167 %{DROP INDEX idx_author;},
168 %{DROP INDEX idx_filename;},
169 %{DROP INDEX idx_topics;},
170 %{DROP INDEX idx_ocn;},
171 %{DROP INDEX idx_digest_clean;},
172 %{DROP INDEX idx_digest_all;},
173 %{DROP INDEX idx_lev0;},
174 %{DROP INDEX idx_lev1;},
175 %{DROP INDEX idx_lev2;},
176 %{DROP INDEX idx_lev3;},
177 %{DROP INDEX idx_lev4;},
178 %{DROP INDEX idx_lev5;},
179 %{DROP INDEX idx_lev6;},
180 %{DROP INDEX idx_endnote_nr;},
181 %{DROP INDEX idx_digest_en;},
182 %{DROP INDEX idx_endnote_nr_asterisk;},
183 %{DROP INDEX idx_endnote_asterisk;},
184 %{DROP INDEX idx_digest_en_asterisk;},
185 %{DROP INDEX idx_endnote_nr_plus;},
186 %{DROP INDEX idx_endnote_plus;},
187 %{DROP INDEX idx_digest_en_plus},
188 ]
189 conn_execute_array(sql_arr)
190 end
191 def text #% drop TEXT indexes, sqlite
192 print "\n drop documents TEXT indexes\n" unless @opt.act[:quiet][:set]==:on
193 sql_arr=[
194 %{DROP INDEX idx_clean;},
195 %{DROP INDEX idx_endnote},
196 ]
197 conn_execute_array(sql_arr)
198 end
199 self
200 end
201 indexes.base
202 @opt.act[:psql][:set]==:on ? '' : indexes.text
203 self
204 end
205 end
206 end
207 __END__