sqlite 1.0.2
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 +7 -0
- data/MIT-LICENSE +22 -0
- data/Rakefile +50 -0
- data/ext/sqlite/extconf.rb +14 -0
- data/ext/sqlite/main.c +12 -0
- data/ext/sqlite/ruby_sqlite3.c +368 -0
- data/ext/sqlite/ruby_sqlite3.h +11 -0
- data/ext/sqlite/ruby_sqlite3_stmt.c +342 -0
- data/ext/sqlite/ruby_sqlite3_stmt.h +13 -0
- data/ext/sqlite/sqlite3_dist.c +221817 -0
- data/lib/sqlite.rb +267 -0
- metadata +53 -0
data/lib/sqlite.rb
ADDED
@@ -0,0 +1,267 @@
|
|
1
|
+
require 'sqlite/sqlite'
|
2
|
+
|
3
|
+
module SQLite
|
4
|
+
|
5
|
+
VERSION = '1.0.2'
|
6
|
+
|
7
|
+
class Database
|
8
|
+
|
9
|
+
def query()
|
10
|
+
return SQLite::Query.new(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
class Query
|
16
|
+
|
17
|
+
attr_reader :db
|
18
|
+
|
19
|
+
def initialize(db)
|
20
|
+
@db = db
|
21
|
+
end
|
22
|
+
|
23
|
+
def exec(sql)
|
24
|
+
stmt = @db.prepare(sql)
|
25
|
+
|
26
|
+
return nil if stmt == nil
|
27
|
+
|
28
|
+
recordset = Recordset.new()
|
29
|
+
recordset.rows = 0
|
30
|
+
|
31
|
+
# Iterate over result set
|
32
|
+
while true
|
33
|
+
row = []
|
34
|
+
ordinal = 0
|
35
|
+
|
36
|
+
stmt.each do |name, value|
|
37
|
+
if recordset.rows == 0
|
38
|
+
recordset.columns << name
|
39
|
+
recordset.headers[name] = ordinal
|
40
|
+
recordset.header_names[ordinal] = name
|
41
|
+
recordset.header_indexes[name] = ordinal
|
42
|
+
recordset.types << stmt.columnType(ordinal)
|
43
|
+
ordinal += 1
|
44
|
+
else
|
45
|
+
row << value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
recordset.data << row if recordset.rows > 0
|
50
|
+
|
51
|
+
recordset.rows += 1
|
52
|
+
|
53
|
+
if stmt.step() != SQLITE_ROW
|
54
|
+
break
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Subtract 1 for header
|
59
|
+
recordset.rows -= 1
|
60
|
+
|
61
|
+
return recordset
|
62
|
+
end
|
63
|
+
|
64
|
+
end # class Query
|
65
|
+
|
66
|
+
# Row/Recordset classes to simplify running queries
|
67
|
+
class Row
|
68
|
+
|
69
|
+
def initialize(rowset, row)
|
70
|
+
@rowset = rowset
|
71
|
+
@row = row
|
72
|
+
end
|
73
|
+
|
74
|
+
def keys()
|
75
|
+
return @rowset.columns
|
76
|
+
end
|
77
|
+
|
78
|
+
def values()
|
79
|
+
return @rowset.data[@row]
|
80
|
+
end
|
81
|
+
|
82
|
+
def each
|
83
|
+
data = @rowset.data[@row]
|
84
|
+
0.upto(data.size()-1) do |i|
|
85
|
+
yield @rowset.header_names[i], data[i]
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def hashify()
|
90
|
+
hash = {}
|
91
|
+
|
92
|
+
self.each do |k,v|
|
93
|
+
hash[k] = v
|
94
|
+
end
|
95
|
+
|
96
|
+
return hash
|
97
|
+
end
|
98
|
+
|
99
|
+
def [](index)
|
100
|
+
# If column name
|
101
|
+
if index.class == String
|
102
|
+
# Convert to ordinal
|
103
|
+
index = @rowset.header_indexes[index]
|
104
|
+
end
|
105
|
+
|
106
|
+
if index != nil
|
107
|
+
return @rowset.data[@row][index]
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end # class Row
|
112
|
+
|
113
|
+
class Recordset
|
114
|
+
|
115
|
+
attr_accessor :columns, :headers
|
116
|
+
attr_accessor :header_names, :header_indexes
|
117
|
+
attr_accessor :data, :rows, :types
|
118
|
+
|
119
|
+
def initialize()
|
120
|
+
@columns = []
|
121
|
+
@types = []
|
122
|
+
@header_names = {}
|
123
|
+
@header_indexes = {}
|
124
|
+
@data = []
|
125
|
+
@rows = 0
|
126
|
+
@headers = {}
|
127
|
+
end
|
128
|
+
|
129
|
+
# Return row at given index
|
130
|
+
def [](row)
|
131
|
+
return nil if row > @rows - 1
|
132
|
+
|
133
|
+
return Row.new(self, row)
|
134
|
+
end
|
135
|
+
|
136
|
+
def each()
|
137
|
+
0.upto(@rows-1) do |i|
|
138
|
+
yield Row.new(self, i)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def toMsgPack(data=[])
|
143
|
+
data << @columns << @types << @data << @headers
|
144
|
+
return data.to_msgpack
|
145
|
+
end
|
146
|
+
|
147
|
+
def toJson(data=[])
|
148
|
+
data << @columns << @types << @data << @headers
|
149
|
+
return data.to_json()
|
150
|
+
end
|
151
|
+
|
152
|
+
def fromMsgPack(binary)
|
153
|
+
load MessagePack.unpack(binary)
|
154
|
+
end
|
155
|
+
|
156
|
+
def load(data)
|
157
|
+
@columns = data[0]
|
158
|
+
@types = data[1]
|
159
|
+
@data = data[2]
|
160
|
+
@headers = data[3]
|
161
|
+
@rows = @data.size
|
162
|
+
|
163
|
+
i = 0
|
164
|
+
@columns.each do |c|
|
165
|
+
@header_names[i] = c
|
166
|
+
@header_indexes[c] = i
|
167
|
+
i += 1
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def to_json()
|
172
|
+
content = [@columns, @types, @data]
|
173
|
+
return JSON::generate(content, {:pretty_print => true})
|
174
|
+
end
|
175
|
+
|
176
|
+
def dump()
|
177
|
+
cols = {}
|
178
|
+
0.upto(@columns.size-1) do |i|
|
179
|
+
cols[i] = @columns[i].size || 0
|
180
|
+
end
|
181
|
+
|
182
|
+
self.each do |row|
|
183
|
+
0.upto(@columns.size-1) do |i|
|
184
|
+
size = row[i].size
|
185
|
+
if size > cols[i]
|
186
|
+
cols[i] = size
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
0.upto(@columns.size-1) do |i|
|
192
|
+
printf "%-#{cols[i]+1}s", @columns[i]
|
193
|
+
print " | " if i >= 0 and i < (@columns.size - 1)
|
194
|
+
end
|
195
|
+
puts
|
196
|
+
|
197
|
+
self.each do |row|
|
198
|
+
0.upto(@columns.size-1) do |i|
|
199
|
+
printf "%-#{cols[i]+1}s", row[i]
|
200
|
+
print " | " if i >= 0 and i < (@columns.size - 1)
|
201
|
+
end
|
202
|
+
puts
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
end # class Recordset
|
207
|
+
|
208
|
+
# Used to serialize/deserialize Recordsets over AMQP message payloads
|
209
|
+
|
210
|
+
class Recordsets
|
211
|
+
|
212
|
+
attr_accessor :headers, :recordsets
|
213
|
+
|
214
|
+
# @param binary Optional MesagePack payload. Will deserialize if provided.
|
215
|
+
def initialize(binary = nil)
|
216
|
+
@headers = {}
|
217
|
+
@recordsets = []
|
218
|
+
|
219
|
+
if not binary.nil?
|
220
|
+
from_mpack binary
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
def <<(element)
|
225
|
+
if element.class != Recordset
|
226
|
+
raise "Invalide element type. Must be Recordset"
|
227
|
+
end
|
228
|
+
|
229
|
+
@recordsets << element
|
230
|
+
end
|
231
|
+
|
232
|
+
# Serilize MessagePack payload
|
233
|
+
# @return Returns the serialized data in MessagePack binary format
|
234
|
+
def toMsgPack()
|
235
|
+
# Convert Ruby Recordset instances to raw array format (JSON-like)
|
236
|
+
recordsets = []
|
237
|
+
@recordsets.each do |recordset|
|
238
|
+
recordsets << [recordset.columns, recordset.types, recordset.rows]
|
239
|
+
end
|
240
|
+
|
241
|
+
return MessagePack.pack([@headers, recordsets])
|
242
|
+
end
|
243
|
+
|
244
|
+
# Deserilize MessagePack payload
|
245
|
+
# @param binary MesagePack payload. Will deserialize if provided.
|
246
|
+
def fromMsgPack(binary)
|
247
|
+
@recordsets = []
|
248
|
+
|
249
|
+
data = MessagePack.unpack(binary)
|
250
|
+
|
251
|
+
if data[0].class != Hash or data[1].class != Array
|
252
|
+
raise "Invalid messagepack format"
|
253
|
+
end
|
254
|
+
|
255
|
+
@headers = data[0]
|
256
|
+
|
257
|
+
# Convert raw recordset format into Ruby Recordset classes
|
258
|
+
data[1].each do |rs|
|
259
|
+
recordset = Recordset.new()
|
260
|
+
recordset.load(rs)
|
261
|
+
@recordsets << recordset
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
end # class Recordsets
|
266
|
+
|
267
|
+
end # module SQLite
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sqlite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mikeowens@gmail.com
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions:
|
17
|
+
- ext/sqlite/extconf.rb
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- MIT-LICENSE
|
21
|
+
- Rakefile
|
22
|
+
- ext/sqlite/extconf.rb
|
23
|
+
- ext/sqlite/main.c
|
24
|
+
- ext/sqlite/ruby_sqlite3.c
|
25
|
+
- ext/sqlite/ruby_sqlite3.h
|
26
|
+
- ext/sqlite/ruby_sqlite3_stmt.c
|
27
|
+
- ext/sqlite/ruby_sqlite3_stmt.h
|
28
|
+
- ext/sqlite/sqlite3_dist.c
|
29
|
+
- lib/sqlite.rb
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.5.2.1
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Small, Simple SQLite extension
|
53
|
+
test_files: []
|