poefy-sqlite3 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.rspec +1 -1
- data/LICENSE +13 -13
- data/README.md +7 -7
- data/Rakefile +8 -8
- data/lib/poefy/sqlite3/version.rb +24 -24
- data/lib/poefy/sqlite3.rb +275 -275
- data/poefy-sqlite3.gemspec +3 -1
- data/spec/poefy_sqlite3_spec.rb +701 -701
- data/spec/spec_helper.rb +11 -11
- metadata +5 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: daf74cad1a2dc701ac781b586a78f5ae3f990eaf4d4000bfadaa0f9c9e06f73b
|
4
|
+
data.tar.gz: 2f9fd9ae80276649b77683c4344f1f28aaae8f709b09c89921e7ac9ba73e01cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc19e0f72f898aa30400d54f35ef9e519e2f8d94543abcc055b054e919652deaad9a63c84a12878851f6c3b06ae4dd85a246f8ac5f4e2d788c4db3635320d209
|
7
|
+
data.tar.gz: b110bd3ed13b0cf748f78c19e50414c3347eb6e182e3eca2f073188fc065747ceec79f0a20df62e140fda3fe48e2baa6119db012df4fc52086e7dee1330a51f6
|
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--require spec_helper
|
1
|
+
--require spec_helper
|
data/LICENSE
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
Copyright (C) 2017 Paul Thompson
|
2
|
-
|
3
|
-
This program is free software: you can redistribute it and/or modify
|
4
|
-
it under the terms of the GNU General Public License as published by
|
5
|
-
the Free Software Foundation, either version 3 of the License, or
|
6
|
-
(at your option) any later version.
|
7
|
-
|
8
|
-
This program is distributed in the hope that it will be useful,
|
9
|
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
-
GNU General Public License for more details.
|
12
|
-
|
13
|
-
Full text of this licence: <https://www.gnu.org/licenses/gpl.html>
|
1
|
+
Copyright (C) 2017 Paul Thompson
|
2
|
+
|
3
|
+
This program is free software: you can redistribute it and/or modify
|
4
|
+
it under the terms of the GNU General Public License as published by
|
5
|
+
the Free Software Foundation, either version 3 of the License, or
|
6
|
+
(at your option) any later version.
|
7
|
+
|
8
|
+
This program is distributed in the hope that it will be useful,
|
9
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
10
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
11
|
+
GNU General Public License for more details.
|
12
|
+
|
13
|
+
Full text of this licence: <https://www.gnu.org/licenses/gpl.html>
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# poefy-sqlite3
|
2
|
-
|
3
|
-
by Paul Thompson - nossidge@gmail.com
|
4
|
-
|
5
|
-
SQLite interface for the 'poefy' gem.
|
6
|
-
|
7
|
-
Please see the [poefy](https://github.com/nossidge/poefy) documentation.
|
1
|
+
# poefy-sqlite3
|
2
|
+
|
3
|
+
by Paul Thompson - nossidge@gmail.com
|
4
|
+
|
5
|
+
SQLite interface for the 'poefy' gem.
|
6
|
+
|
7
|
+
Please see the [poefy](https://github.com/nossidge/poefy) documentation.
|
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'bundler/gem_tasks'
|
2
|
-
require 'rspec/core/rake_task'
|
3
|
-
|
4
|
-
RSpec::Core::RakeTask.new(:spec) do |t|
|
5
|
-
t.verbose = false
|
6
|
-
end
|
7
|
-
task :default => :spec
|
8
|
-
task :test => :spec
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
5
|
+
t.verbose = false
|
6
|
+
end
|
7
|
+
task :default => :spec
|
8
|
+
task :test => :spec
|
@@ -1,24 +1,24 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: UTF-8
|
3
|
-
|
4
|
-
module Poefy
|
5
|
-
|
6
|
-
module Sqlite3
|
7
|
-
|
8
|
-
def self.version_number
|
9
|
-
major =
|
10
|
-
minor =
|
11
|
-
tiny = 0
|
12
|
-
pre = nil
|
13
|
-
|
14
|
-
string = [major, minor, tiny, pre].compact.join('.')
|
15
|
-
Gem::Version.new string
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.version_date
|
19
|
-
'
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Encoding: UTF-8
|
3
|
+
|
4
|
+
module Poefy
|
5
|
+
|
6
|
+
module Sqlite3
|
7
|
+
|
8
|
+
def self.version_number
|
9
|
+
major = 2
|
10
|
+
minor = 0
|
11
|
+
tiny = 0
|
12
|
+
pre = nil
|
13
|
+
|
14
|
+
string = [major, minor, tiny, pre].compact.join('.')
|
15
|
+
Gem::Version.new string
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.version_date
|
19
|
+
'2025-06-28'
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/lib/poefy/sqlite3.rb
CHANGED
@@ -1,275 +1,275 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# Encoding: UTF-8
|
3
|
-
|
4
|
-
################################################################################
|
5
|
-
# Extend 'Database' class for connecting to a sqlite3 database.
|
6
|
-
# These methods are specific to sqlite3.
|
7
|
-
# Other databases should be implemented in separate gems.
|
8
|
-
################################################################################
|
9
|
-
|
10
|
-
require 'sqlite3'
|
11
|
-
|
12
|
-
################################################################################
|
13
|
-
|
14
|
-
module Poefy
|
15
|
-
|
16
|
-
class Database
|
17
|
-
|
18
|
-
# Open a connection, execute a query, close the connection.
|
19
|
-
def self.single_exec! database_name, sql
|
20
|
-
path = Database::path database_name
|
21
|
-
con = SQLite3::Database.open path
|
22
|
-
rs = con.execute sql
|
23
|
-
con.close
|
24
|
-
rs
|
25
|
-
end
|
26
|
-
|
27
|
-
# List all database files in the directory.
|
28
|
-
# Does not include databases used for testing.
|
29
|
-
def self.list
|
30
|
-
Dir[Poefy.root + '/data/*.db'].map do |i|
|
31
|
-
File.basename(i, '.db')
|
32
|
-
end.reject do |i|
|
33
|
-
i.start_with?('spec_')
|
34
|
-
end.sort - ['test']
|
35
|
-
end
|
36
|
-
|
37
|
-
# Get the description of a database.
|
38
|
-
def self.desc database_name
|
39
|
-
begin
|
40
|
-
sql = "SELECT comment FROM comment;"
|
41
|
-
Database::single_exec!(database_name, sql).flatten.first
|
42
|
-
rescue
|
43
|
-
''
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# List all database files and their descriptions.
|
48
|
-
def self.list_with_desc
|
49
|
-
Database::list.map do |i|
|
50
|
-
begin
|
51
|
-
[i, Database::desc(i)]
|
52
|
-
rescue
|
53
|
-
[i, '']
|
54
|
-
end
|
55
|
-
end.to_h
|
56
|
-
end
|
57
|
-
|
58
|
-
# Get the path of a database.
|
59
|
-
def self.path database_name
|
60
|
-
Poefy.root + '/data/' + File.basename(database_name, '.db') + '.db'
|
61
|
-
end
|
62
|
-
|
63
|
-
############################################################################
|
64
|
-
|
65
|
-
# This is the type of database that is being used.
|
66
|
-
# It is also used as a signifier that a database has been specified.
|
67
|
-
def type
|
68
|
-
'sqlite3'
|
69
|
-
end
|
70
|
-
|
71
|
-
# Get/set the description of the database.
|
72
|
-
def desc
|
73
|
-
Database::desc @name
|
74
|
-
end
|
75
|
-
def desc=(description)
|
76
|
-
execute! "DELETE FROM comment;"
|
77
|
-
execute! "INSERT INTO comment VALUES ( ? );", description.to_s
|
78
|
-
end
|
79
|
-
|
80
|
-
# The number of lines in the table.
|
81
|
-
def count
|
82
|
-
return 0
|
83
|
-
sql = "SELECT COUNT(*) AS num FROM #{table};"
|
84
|
-
execute!(sql).first['num'].to_i
|
85
|
-
end
|
86
|
-
|
87
|
-
# See if the database file exists or not.
|
88
|
-
def
|
89
|
-
File.
|
90
|
-
end
|
91
|
-
|
92
|
-
# Get all rhyming lines for the word.
|
93
|
-
def rhymes word, key = nil
|
94
|
-
return nil if word.nil?
|
95
|
-
|
96
|
-
sql = <<-SQL
|
97
|
-
SELECT rhyme, final_word, syllables, line
|
98
|
-
FROM lines
|
99
|
-
WHERE rhyme = ?
|
100
|
-
ORDER BY rhyme, final_word, syllables, line
|
101
|
-
SQL
|
102
|
-
output = word.to_phrase.rhymes.keys.map do |rhyme|
|
103
|
-
rs = execute!(sql, [rhyme]).to_a
|
104
|
-
rs.each{ |a| a.reject!{ |k| k.is_a? Numeric }}
|
105
|
-
end.flatten
|
106
|
-
|
107
|
-
if !key.nil? and %w[rhyme final_word syllables line].include?(key)
|
108
|
-
output.map!{ |i| i[key] }
|
109
|
-
end
|
110
|
-
output
|
111
|
-
end
|
112
|
-
|
113
|
-
private
|
114
|
-
|
115
|
-
# The name of the table.
|
116
|
-
def table
|
117
|
-
'lines'
|
118
|
-
end
|
119
|
-
|
120
|
-
# Create a new database.
|
121
|
-
def new_connection
|
122
|
-
File.delete(db_file) if File.
|
123
|
-
@db = SQLite3::Database.new(db_file)
|
124
|
-
@db.results_as_hash = true
|
125
|
-
end
|
126
|
-
|
127
|
-
# Open a connection to the database.
|
128
|
-
def open_connection
|
129
|
-
@db = SQLite3::Database.open(db_file)
|
130
|
-
@db.results_as_hash = true
|
131
|
-
end
|
132
|
-
|
133
|
-
# Execute a query.
|
134
|
-
def execute! sql, *args
|
135
|
-
db.execute sql, *args
|
136
|
-
end
|
137
|
-
|
138
|
-
# Insert an array of poefy-described lines.
|
139
|
-
def insert_lines table_name, rows
|
140
|
-
sql = "INSERT INTO #{table_name} VALUES ( ?, ?, ?, ? )"
|
141
|
-
db.transaction do |db_tr|
|
142
|
-
rows.each do |line|
|
143
|
-
db_tr.execute sql, line
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
##########################################################################
|
149
|
-
|
150
|
-
# Find the correct database file.
|
151
|
-
# If local, just use the value.
|
152
|
-
# Else, use the database in /data/ directory.
|
153
|
-
def db_file
|
154
|
-
if @local
|
155
|
-
@name
|
156
|
-
elsif @db_file
|
157
|
-
@db_file
|
158
|
-
else
|
159
|
-
path = Poefy.root + '/data'
|
160
|
-
file = File.basename(@name, '.db')
|
161
|
-
@db_file = path + '/' + file + '.db'
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
##########################################################################
|
166
|
-
|
167
|
-
# Create the table and the index.
|
168
|
-
def create_table table_name, description = nil
|
169
|
-
execute! <<-SQL
|
170
|
-
CREATE TABLE #{table_name} (
|
171
|
-
line TEXT,
|
172
|
-
syllables SMALLINT,
|
173
|
-
final_word TEXT,
|
174
|
-
rhyme TEXT
|
175
|
-
);
|
176
|
-
SQL
|
177
|
-
execute! <<-SQL
|
178
|
-
CREATE TABLE comment (
|
179
|
-
comment TEXT
|
180
|
-
);
|
181
|
-
SQL
|
182
|
-
execute! <<-SQL
|
183
|
-
CREATE INDEX idx ON #{table_name} (
|
184
|
-
rhyme, final_word, line
|
185
|
-
);
|
186
|
-
SQL
|
187
|
-
self.desc = description
|
188
|
-
end
|
189
|
-
|
190
|
-
##########################################################################
|
191
|
-
|
192
|
-
# Define SQL of the stored procedures.
|
193
|
-
def sprocs_sql_hash
|
194
|
-
sql = {}
|
195
|
-
sql[:rbc] = <<-SQL
|
196
|
-
SELECT rhyme, COUNT(rhyme) AS count
|
197
|
-
FROM (
|
198
|
-
SELECT rhyme, final_word, COUNT(final_word) AS wc
|
199
|
-
FROM #{table}
|
200
|
-
GROUP BY rhyme, final_word
|
201
|
-
)
|
202
|
-
GROUP BY rhyme
|
203
|
-
HAVING count >= ?
|
204
|
-
SQL
|
205
|
-
sql[:rbcs] = <<-SQL
|
206
|
-
SELECT rhyme, COUNT(rhyme) AS count
|
207
|
-
FROM (
|
208
|
-
SELECT rhyme, final_word, COUNT(final_word) AS wc
|
209
|
-
FROM #{table}
|
210
|
-
WHERE syllables BETWEEN ? AND ?
|
211
|
-
GROUP BY rhyme, final_word
|
212
|
-
)
|
213
|
-
GROUP BY rhyme
|
214
|
-
HAVING count >= ?
|
215
|
-
SQL
|
216
|
-
sql[:la] = <<-SQL
|
217
|
-
SELECT line, syllables, final_word, rhyme
|
218
|
-
FROM #{table} WHERE rhyme = ?
|
219
|
-
SQL
|
220
|
-
sql[:las] = <<-SQL
|
221
|
-
SELECT line, syllables, final_word, rhyme
|
222
|
-
FROM #{table} WHERE rhyme = ?
|
223
|
-
AND syllables BETWEEN ? AND ?
|
224
|
-
SQL
|
225
|
-
sql
|
226
|
-
end
|
227
|
-
|
228
|
-
# Create the stored procedures in the database.
|
229
|
-
def create_sprocs
|
230
|
-
sprocs_sql_hash.each do |key, value|
|
231
|
-
@sproc[key] = db.prepare value
|
232
|
-
end
|
233
|
-
rescue
|
234
|
-
msg = "ERROR: Database table structure is invalid." +
|
235
|
-
"\n Please manually DROP the corrupt table and recreate it."
|
236
|
-
raise Poefy::StructureInvalid.new(msg)
|
237
|
-
end
|
238
|
-
|
239
|
-
# Find rhymes and counts greater than a certain length.
|
240
|
-
def sproc_rhymes_by_count rhyme_count
|
241
|
-
@sproc[:rbc].reset!
|
242
|
-
@sproc[:rbc].bind_param(1, rhyme_count)
|
243
|
-
@sproc[:rbc].execute.to_a
|
244
|
-
end
|
245
|
-
|
246
|
-
# Also adds syllable selection.
|
247
|
-
def sproc_rhymes_by_count_syllables rhyme_count, syllable_min_max
|
248
|
-
@sproc[:rbcs].reset!
|
249
|
-
@sproc[:rbcs].bind_param(1, syllable_min_max[:min])
|
250
|
-
@sproc[:rbcs].bind_param(2, syllable_min_max[:max])
|
251
|
-
@sproc[:rbcs].bind_param(3, rhyme_count)
|
252
|
-
@sproc[:rbcs].execute.to_a
|
253
|
-
end
|
254
|
-
|
255
|
-
# Find all lines for a certain rhyme.
|
256
|
-
def sproc_lines_by_rhyme rhyme
|
257
|
-
@sproc[:la].reset!
|
258
|
-
@sproc[:la].bind_param(1, rhyme)
|
259
|
-
@sproc[:la].execute.to_a
|
260
|
-
end
|
261
|
-
|
262
|
-
# Also adds syllable selection.
|
263
|
-
def sproc_lines_by_rhyme_syllables rhyme, syllable_min_max
|
264
|
-
@sproc[:las].reset!
|
265
|
-
@sproc[:las].bind_param(1, rhyme)
|
266
|
-
@sproc[:las].bind_param(2, syllable_min_max[:min])
|
267
|
-
@sproc[:las].bind_param(3, syllable_min_max[:max])
|
268
|
-
@sproc[:las].execute.to_a
|
269
|
-
end
|
270
|
-
|
271
|
-
end
|
272
|
-
|
273
|
-
end
|
274
|
-
|
275
|
-
################################################################################
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Encoding: UTF-8
|
3
|
+
|
4
|
+
################################################################################
|
5
|
+
# Extend 'Database' class for connecting to a sqlite3 database.
|
6
|
+
# These methods are specific to sqlite3.
|
7
|
+
# Other databases should be implemented in separate gems.
|
8
|
+
################################################################################
|
9
|
+
|
10
|
+
require 'sqlite3'
|
11
|
+
|
12
|
+
################################################################################
|
13
|
+
|
14
|
+
module Poefy
|
15
|
+
|
16
|
+
class Database
|
17
|
+
|
18
|
+
# Open a connection, execute a query, close the connection.
|
19
|
+
def self.single_exec! database_name, sql
|
20
|
+
path = Database::path database_name
|
21
|
+
con = SQLite3::Database.open path
|
22
|
+
rs = con.execute sql
|
23
|
+
con.close
|
24
|
+
rs
|
25
|
+
end
|
26
|
+
|
27
|
+
# List all database files in the directory.
|
28
|
+
# Does not include databases used for testing.
|
29
|
+
def self.list
|
30
|
+
Dir[Poefy.root + '/data/*.db'].map do |i|
|
31
|
+
File.basename(i, '.db')
|
32
|
+
end.reject do |i|
|
33
|
+
i.start_with?('spec_')
|
34
|
+
end.sort - ['test']
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get the description of a database.
|
38
|
+
def self.desc database_name
|
39
|
+
begin
|
40
|
+
sql = "SELECT comment FROM comment;"
|
41
|
+
Database::single_exec!(database_name, sql).flatten.first
|
42
|
+
rescue
|
43
|
+
''
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# List all database files and their descriptions.
|
48
|
+
def self.list_with_desc
|
49
|
+
Database::list.map do |i|
|
50
|
+
begin
|
51
|
+
[i, Database::desc(i)]
|
52
|
+
rescue
|
53
|
+
[i, '']
|
54
|
+
end
|
55
|
+
end.to_h
|
56
|
+
end
|
57
|
+
|
58
|
+
# Get the path of a database.
|
59
|
+
def self.path database_name
|
60
|
+
Poefy.root + '/data/' + File.basename(database_name, '.db') + '.db'
|
61
|
+
end
|
62
|
+
|
63
|
+
############################################################################
|
64
|
+
|
65
|
+
# This is the type of database that is being used.
|
66
|
+
# It is also used as a signifier that a database has been specified.
|
67
|
+
def type
|
68
|
+
'sqlite3'
|
69
|
+
end
|
70
|
+
|
71
|
+
# Get/set the description of the database.
|
72
|
+
def desc
|
73
|
+
Database::desc @name
|
74
|
+
end
|
75
|
+
def desc=(description)
|
76
|
+
execute! "DELETE FROM comment;"
|
77
|
+
execute! "INSERT INTO comment VALUES ( ? );", description.to_s
|
78
|
+
end
|
79
|
+
|
80
|
+
# The number of lines in the table.
|
81
|
+
def count
|
82
|
+
return 0 unless exist?
|
83
|
+
sql = "SELECT COUNT(*) AS num FROM #{table};"
|
84
|
+
execute!(sql).first['num'].to_i
|
85
|
+
end
|
86
|
+
|
87
|
+
# See if the database file exists or not.
|
88
|
+
def exist?
|
89
|
+
File.exist?(db_file)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Get all rhyming lines for the word.
|
93
|
+
def rhymes word, key = nil
|
94
|
+
return nil if word.nil?
|
95
|
+
|
96
|
+
sql = <<-SQL
|
97
|
+
SELECT rhyme, final_word, syllables, line
|
98
|
+
FROM lines
|
99
|
+
WHERE rhyme = ?
|
100
|
+
ORDER BY rhyme, final_word, syllables, line
|
101
|
+
SQL
|
102
|
+
output = word.to_phrase.rhymes.keys.map do |rhyme|
|
103
|
+
rs = execute!(sql, [rhyme]).to_a
|
104
|
+
rs.each{ |a| a.reject!{ |k| k.is_a? Numeric }}
|
105
|
+
end.flatten
|
106
|
+
|
107
|
+
if !key.nil? and %w[rhyme final_word syllables line].include?(key)
|
108
|
+
output.map!{ |i| i[key] }
|
109
|
+
end
|
110
|
+
output
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
# The name of the table.
|
116
|
+
def table
|
117
|
+
'lines'
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a new database.
|
121
|
+
def new_connection
|
122
|
+
File.delete(db_file) if File.exist?(db_file)
|
123
|
+
@db = SQLite3::Database.new(db_file)
|
124
|
+
@db.results_as_hash = true
|
125
|
+
end
|
126
|
+
|
127
|
+
# Open a connection to the database.
|
128
|
+
def open_connection
|
129
|
+
@db = SQLite3::Database.open(db_file)
|
130
|
+
@db.results_as_hash = true
|
131
|
+
end
|
132
|
+
|
133
|
+
# Execute a query.
|
134
|
+
def execute! sql, *args
|
135
|
+
db.execute sql, *args
|
136
|
+
end
|
137
|
+
|
138
|
+
# Insert an array of poefy-described lines.
|
139
|
+
def insert_lines table_name, rows
|
140
|
+
sql = "INSERT INTO #{table_name} VALUES ( ?, ?, ?, ? )"
|
141
|
+
db.transaction do |db_tr|
|
142
|
+
rows.each do |line|
|
143
|
+
db_tr.execute sql, line
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
##########################################################################
|
149
|
+
|
150
|
+
# Find the correct database file.
|
151
|
+
# If local, just use the value.
|
152
|
+
# Else, use the database in /data/ directory.
|
153
|
+
def db_file
|
154
|
+
if @local
|
155
|
+
@name
|
156
|
+
elsif @db_file
|
157
|
+
@db_file
|
158
|
+
else
|
159
|
+
path = Poefy.root + '/data'
|
160
|
+
file = File.basename(@name, '.db')
|
161
|
+
@db_file = path + '/' + file + '.db'
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
##########################################################################
|
166
|
+
|
167
|
+
# Create the table and the index.
|
168
|
+
def create_table table_name, description = nil
|
169
|
+
execute! <<-SQL
|
170
|
+
CREATE TABLE #{table_name} (
|
171
|
+
line TEXT,
|
172
|
+
syllables SMALLINT,
|
173
|
+
final_word TEXT,
|
174
|
+
rhyme TEXT
|
175
|
+
);
|
176
|
+
SQL
|
177
|
+
execute! <<-SQL
|
178
|
+
CREATE TABLE comment (
|
179
|
+
comment TEXT
|
180
|
+
);
|
181
|
+
SQL
|
182
|
+
execute! <<-SQL
|
183
|
+
CREATE INDEX idx ON #{table_name} (
|
184
|
+
rhyme, final_word, line
|
185
|
+
);
|
186
|
+
SQL
|
187
|
+
self.desc = description
|
188
|
+
end
|
189
|
+
|
190
|
+
##########################################################################
|
191
|
+
|
192
|
+
# Define SQL of the stored procedures.
|
193
|
+
def sprocs_sql_hash
|
194
|
+
sql = {}
|
195
|
+
sql[:rbc] = <<-SQL
|
196
|
+
SELECT rhyme, COUNT(rhyme) AS count
|
197
|
+
FROM (
|
198
|
+
SELECT rhyme, final_word, COUNT(final_word) AS wc
|
199
|
+
FROM #{table}
|
200
|
+
GROUP BY rhyme, final_word
|
201
|
+
)
|
202
|
+
GROUP BY rhyme
|
203
|
+
HAVING count >= ?
|
204
|
+
SQL
|
205
|
+
sql[:rbcs] = <<-SQL
|
206
|
+
SELECT rhyme, COUNT(rhyme) AS count
|
207
|
+
FROM (
|
208
|
+
SELECT rhyme, final_word, COUNT(final_word) AS wc
|
209
|
+
FROM #{table}
|
210
|
+
WHERE syllables BETWEEN ? AND ?
|
211
|
+
GROUP BY rhyme, final_word
|
212
|
+
)
|
213
|
+
GROUP BY rhyme
|
214
|
+
HAVING count >= ?
|
215
|
+
SQL
|
216
|
+
sql[:la] = <<-SQL
|
217
|
+
SELECT line, syllables, final_word, rhyme
|
218
|
+
FROM #{table} WHERE rhyme = ?
|
219
|
+
SQL
|
220
|
+
sql[:las] = <<-SQL
|
221
|
+
SELECT line, syllables, final_word, rhyme
|
222
|
+
FROM #{table} WHERE rhyme = ?
|
223
|
+
AND syllables BETWEEN ? AND ?
|
224
|
+
SQL
|
225
|
+
sql
|
226
|
+
end
|
227
|
+
|
228
|
+
# Create the stored procedures in the database.
|
229
|
+
def create_sprocs
|
230
|
+
sprocs_sql_hash.each do |key, value|
|
231
|
+
@sproc[key] = db.prepare value
|
232
|
+
end
|
233
|
+
rescue
|
234
|
+
msg = "ERROR: Database table structure is invalid." +
|
235
|
+
"\n Please manually DROP the corrupt table and recreate it."
|
236
|
+
raise Poefy::StructureInvalid.new(msg)
|
237
|
+
end
|
238
|
+
|
239
|
+
# Find rhymes and counts greater than a certain length.
|
240
|
+
def sproc_rhymes_by_count rhyme_count
|
241
|
+
@sproc[:rbc].reset!
|
242
|
+
@sproc[:rbc].bind_param(1, rhyme_count)
|
243
|
+
@sproc[:rbc].execute.to_a
|
244
|
+
end
|
245
|
+
|
246
|
+
# Also adds syllable selection.
|
247
|
+
def sproc_rhymes_by_count_syllables rhyme_count, syllable_min_max
|
248
|
+
@sproc[:rbcs].reset!
|
249
|
+
@sproc[:rbcs].bind_param(1, syllable_min_max[:min])
|
250
|
+
@sproc[:rbcs].bind_param(2, syllable_min_max[:max])
|
251
|
+
@sproc[:rbcs].bind_param(3, rhyme_count)
|
252
|
+
@sproc[:rbcs].execute.to_a
|
253
|
+
end
|
254
|
+
|
255
|
+
# Find all lines for a certain rhyme.
|
256
|
+
def sproc_lines_by_rhyme rhyme
|
257
|
+
@sproc[:la].reset!
|
258
|
+
@sproc[:la].bind_param(1, rhyme)
|
259
|
+
@sproc[:la].execute.to_a
|
260
|
+
end
|
261
|
+
|
262
|
+
# Also adds syllable selection.
|
263
|
+
def sproc_lines_by_rhyme_syllables rhyme, syllable_min_max
|
264
|
+
@sproc[:las].reset!
|
265
|
+
@sproc[:las].bind_param(1, rhyme)
|
266
|
+
@sproc[:las].bind_param(2, syllable_min_max[:min])
|
267
|
+
@sproc[:las].bind_param(3, syllable_min_max[:max])
|
268
|
+
@sproc[:las].execute.to_a
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
################################################################################
|