amalgalite 1.8.0-x64-mingw-ucrt
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CONTRIBUTING.md +60 -0
- data/HISTORY.md +386 -0
- data/LICENSE +31 -0
- data/Manifest.txt +105 -0
- data/README.md +62 -0
- data/Rakefile +27 -0
- data/TODO.md +57 -0
- data/bin/amalgalite-pack +147 -0
- data/examples/a.rb +9 -0
- data/examples/blob.rb +88 -0
- data/examples/bootstrap.rb +36 -0
- data/examples/define_aggregate.rb +75 -0
- data/examples/define_function.rb +104 -0
- data/examples/fts5.rb +152 -0
- data/examples/gem-db.rb +94 -0
- data/examples/require_me.rb +11 -0
- data/examples/requires.rb +42 -0
- data/examples/schema-info.rb +34 -0
- data/ext/amalgalite/c/amalgalite.c +355 -0
- data/ext/amalgalite/c/amalgalite.h +151 -0
- data/ext/amalgalite/c/amalgalite_blob.c +240 -0
- data/ext/amalgalite/c/amalgalite_constants.c +1432 -0
- data/ext/amalgalite/c/amalgalite_database.c +1188 -0
- data/ext/amalgalite/c/amalgalite_requires_bootstrap.c +282 -0
- data/ext/amalgalite/c/amalgalite_statement.c +649 -0
- data/ext/amalgalite/c/extconf.rb +71 -0
- data/ext/amalgalite/c/gen_constants.rb +353 -0
- data/ext/amalgalite/c/notes.txt +134 -0
- data/ext/amalgalite/c/sqlite3.c +243616 -0
- data/ext/amalgalite/c/sqlite3.h +12894 -0
- data/ext/amalgalite/c/sqlite3_options.h +4 -0
- data/ext/amalgalite/c/sqlite3ext.h +705 -0
- data/lib/amalgalite/3.1/amalgalite.so +0 -0
- data/lib/amalgalite/aggregate.rb +73 -0
- data/lib/amalgalite/blob.rb +186 -0
- data/lib/amalgalite/boolean.rb +42 -0
- data/lib/amalgalite/busy_timeout.rb +47 -0
- data/lib/amalgalite/column.rb +99 -0
- data/lib/amalgalite/core_ext/kernel/require.rb +21 -0
- data/lib/amalgalite/csv_table_importer.rb +75 -0
- data/lib/amalgalite/database.rb +933 -0
- data/lib/amalgalite/function.rb +61 -0
- data/lib/amalgalite/index.rb +43 -0
- data/lib/amalgalite/memory_database.rb +15 -0
- data/lib/amalgalite/packer.rb +231 -0
- data/lib/amalgalite/paths.rb +80 -0
- data/lib/amalgalite/profile_tap.rb +131 -0
- data/lib/amalgalite/progress_handler.rb +21 -0
- data/lib/amalgalite/requires.rb +151 -0
- data/lib/amalgalite/schema.rb +225 -0
- data/lib/amalgalite/sqlite3/constants.rb +95 -0
- data/lib/amalgalite/sqlite3/database/function.rb +48 -0
- data/lib/amalgalite/sqlite3/database/status.rb +68 -0
- data/lib/amalgalite/sqlite3/status.rb +60 -0
- data/lib/amalgalite/sqlite3/version.rb +55 -0
- data/lib/amalgalite/sqlite3.rb +6 -0
- data/lib/amalgalite/statement.rb +421 -0
- data/lib/amalgalite/table.rb +91 -0
- data/lib/amalgalite/taps/console.rb +27 -0
- data/lib/amalgalite/taps/io.rb +74 -0
- data/lib/amalgalite/taps.rb +2 -0
- data/lib/amalgalite/trace_tap.rb +35 -0
- data/lib/amalgalite/type_map.rb +63 -0
- data/lib/amalgalite/type_maps/default_map.rb +166 -0
- data/lib/amalgalite/type_maps/storage_map.rb +38 -0
- data/lib/amalgalite/type_maps/text_map.rb +21 -0
- data/lib/amalgalite/version.rb +8 -0
- data/lib/amalgalite/view.rb +26 -0
- data/lib/amalgalite.rb +51 -0
- data/spec/aggregate_spec.rb +158 -0
- data/spec/amalgalite_spec.rb +4 -0
- data/spec/blob_spec.rb +78 -0
- data/spec/boolean_spec.rb +24 -0
- data/spec/busy_handler.rb +157 -0
- data/spec/data/iso-3166-country.txt +242 -0
- data/spec/data/iso-3166-schema.sql +22 -0
- data/spec/data/iso-3166-subcountry.txt +3995 -0
- data/spec/data/make-iso-db.sh +12 -0
- data/spec/database_spec.rb +505 -0
- data/spec/default_map_spec.rb +92 -0
- data/spec/function_spec.rb +78 -0
- data/spec/integeration_spec.rb +97 -0
- data/spec/iso_3166_database.rb +58 -0
- data/spec/json_spec.rb +24 -0
- data/spec/packer_spec.rb +60 -0
- data/spec/paths_spec.rb +28 -0
- data/spec/progress_handler_spec.rb +91 -0
- data/spec/requires_spec.rb +54 -0
- data/spec/rtree_spec.rb +66 -0
- data/spec/schema_spec.rb +131 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/sqlite3/constants_spec.rb +108 -0
- data/spec/sqlite3/database_status_spec.rb +36 -0
- data/spec/sqlite3/status_spec.rb +22 -0
- data/spec/sqlite3/version_spec.rb +28 -0
- data/spec/sqlite3_spec.rb +53 -0
- data/spec/statement_spec.rb +168 -0
- data/spec/storage_map_spec.rb +38 -0
- data/spec/tap_spec.rb +57 -0
- data/spec/text_map_spec.rb +20 -0
- data/spec/type_map_spec.rb +14 -0
- data/spec/version_spec.rb +8 -0
- data/tasks/custom.rake +101 -0
- data/tasks/default.rake +244 -0
- data/tasks/extension.rake +28 -0
- data/tasks/this.rb +208 -0
- metadata +325 -0
@@ -0,0 +1,63 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
module Amalgalite
|
6
|
+
##
|
7
|
+
# TypeMap defines the protocol used between Ruby and SQLite for mapping
|
8
|
+
# binding types, used in prepared statements; and result types, used in
|
9
|
+
# returning objects from a query.
|
10
|
+
#
|
11
|
+
#
|
12
|
+
class TypeMap
|
13
|
+
##
|
14
|
+
# :call-seq:
|
15
|
+
# map.bind_type_of( obj ) -> DataType constant
|
16
|
+
#
|
17
|
+
# bind_type_of is called during the Statement#bind process to convert the
|
18
|
+
# bind parameter to the appropriate SQLite types. This method MUST return
|
19
|
+
# one of the valid constants in the namespace
|
20
|
+
# Amalgalite::SQLite::Constants::DataType
|
21
|
+
#
|
22
|
+
def bind_type_of( obj )
|
23
|
+
raise NotImplementedError, "bind_type_of has not been implemented"
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# :call-seq:
|
28
|
+
# map.result_value_of( declared_type, value ) -> String
|
29
|
+
#
|
30
|
+
# result_value_of is called during the result processing of column values
|
31
|
+
# to convert an SQLite database value into the appropriate Ruby class.
|
32
|
+
#
|
33
|
+
# +declared_type+ is the string from the original CREATE TABLE statment
|
34
|
+
# from which the column value originates. It may also be nil if the origin
|
35
|
+
# column cannot be determined.
|
36
|
+
#
|
37
|
+
# +value+ is the SQLite value from the column as either a Ruby String,
|
38
|
+
# Integer, Float or Amalgalite::Blob.
|
39
|
+
#
|
40
|
+
# result_value should return the value that is to be put into the result set
|
41
|
+
# for the query. It may do nothing, or it may do massive amounts of
|
42
|
+
# conversion.
|
43
|
+
def result_value_of( delcared_type, value )
|
44
|
+
raise NotImplementedError, "result_value_of has not been implemented"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# The TypeMaps module holds all typemaps that ship with Amagalite. They
|
50
|
+
# currently are:
|
51
|
+
#
|
52
|
+
# DefaultMap:: does a 'best-guess' mapping to convert as many types as
|
53
|
+
# possible to known ruby classes from known SQL types.
|
54
|
+
# StorageMap:: converts to a limited set of classes directly based
|
55
|
+
# upon the SQLite storage types
|
56
|
+
# TextMap:: Everything is Text ... everything everything everything
|
57
|
+
#
|
58
|
+
module TypeMaps
|
59
|
+
end
|
60
|
+
end
|
61
|
+
require 'amalgalite/type_maps/default_map'
|
62
|
+
require 'amalgalite/type_maps/storage_map'
|
63
|
+
require 'amalgalite/type_maps/text_map'
|
@@ -0,0 +1,166 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
|
6
|
+
require 'time'
|
7
|
+
require 'date'
|
8
|
+
|
9
|
+
module Amalgalite::TypeMaps
|
10
|
+
##
|
11
|
+
# An Amalgalite::TypeMap that does its best to convert between Ruby classes
|
12
|
+
# and known SQL data types.
|
13
|
+
#
|
14
|
+
# Upon instantiation, DefaultMap generates a conversion map to try to figure
|
15
|
+
# out the best way to convert between populate SQL 'types' and ruby classes
|
16
|
+
#
|
17
|
+
class DefaultMap
|
18
|
+
class << self
|
19
|
+
def methods_handling_sql_types # :nodoc:
|
20
|
+
@methods_handling_sql_types ||= {
|
21
|
+
'date' => %w[ date ],
|
22
|
+
'datetime' => %w[ datetime ],
|
23
|
+
'time' => %w[ timestamp time ],
|
24
|
+
'float' => %w[ double float real numeric decimal ],
|
25
|
+
'integer' => %w[ integer tinyint smallint int int2 int4 int8 bigint serial bigserial ],
|
26
|
+
'string' => %w[ text char string varchar character json ],
|
27
|
+
'boolean' => %w[ bool boolean ],
|
28
|
+
'blob' => %w[ binary blob ],
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
# say what method to call to convert an sql type to a ruby type
|
33
|
+
#
|
34
|
+
def sql_to_method( sql_type ) # :nodoc:
|
35
|
+
unless defined? @sql_to_method
|
36
|
+
@sql_to_method = {}
|
37
|
+
methods_handling_sql_types.each_pair do |method, sql_types|
|
38
|
+
sql_types.each { |t| @sql_to_method[t] = method }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
return_method = @sql_to_method[sql_type]
|
42
|
+
|
43
|
+
# the straight lookup didn't work, try iterating through the types and
|
44
|
+
# see what is found
|
45
|
+
unless return_method
|
46
|
+
@sql_to_method.each_pair do |sql, method|
|
47
|
+
if sql_type.index(sql) then
|
48
|
+
return_method = method
|
49
|
+
break
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
return return_method
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def initialize
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if
|
62
|
+
# nothing can be found then default to TEXT.
|
63
|
+
#
|
64
|
+
def bind_type_of( obj )
|
65
|
+
case obj
|
66
|
+
when Float
|
67
|
+
::Amalgalite::SQLite3::Constants::DataType::FLOAT
|
68
|
+
when Integer
|
69
|
+
::Amalgalite::SQLite3::Constants::DataType::INTEGER
|
70
|
+
when NilClass
|
71
|
+
::Amalgalite::SQLite3::Constants::DataType::NULL
|
72
|
+
when ::Amalgalite::Blob
|
73
|
+
::Amalgalite::SQLite3::Constants::DataType::BLOB
|
74
|
+
else
|
75
|
+
::Amalgalite::SQLite3::Constants::DataType::TEXT
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Map the incoming value to an outgoing value. For some incoming values,
|
81
|
+
# there will be no change, but for some (i.e. Dates and Times) there is some
|
82
|
+
# conversion
|
83
|
+
#
|
84
|
+
def result_value_of( declared_type, value )
|
85
|
+
case value
|
86
|
+
when Numeric
|
87
|
+
return value
|
88
|
+
when NilClass
|
89
|
+
return value
|
90
|
+
when Amalgalite::Blob
|
91
|
+
return value
|
92
|
+
when String
|
93
|
+
if declared_type then
|
94
|
+
conversion_method = DefaultMap.sql_to_method( declared_type.downcase )
|
95
|
+
if conversion_method then
|
96
|
+
return send(conversion_method, value)
|
97
|
+
else
|
98
|
+
raise ::Amalgalite::Error, "Unable to convert SQL type of #{declared_type} to a Ruby class"
|
99
|
+
end
|
100
|
+
else
|
101
|
+
# unable to do any other conversion, just return what we have.
|
102
|
+
return value
|
103
|
+
end
|
104
|
+
else
|
105
|
+
raise ::Amalgalite::Error, "Unable to convert a class #{value.class.name} with value #{value.inspect}"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
##
|
110
|
+
# convert a string to a date
|
111
|
+
#
|
112
|
+
def date( str )
|
113
|
+
Date.parse( str )
|
114
|
+
end
|
115
|
+
|
116
|
+
##
|
117
|
+
# convert a string to a datetime, if no timzone is found in the parsed
|
118
|
+
# string, set it to the local offset.
|
119
|
+
#
|
120
|
+
def datetime( str )
|
121
|
+
DateTime.parse( str )
|
122
|
+
end
|
123
|
+
|
124
|
+
##
|
125
|
+
# convert a string to a Time
|
126
|
+
#
|
127
|
+
def time( str )
|
128
|
+
Time.parse( str )
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# convert a string to a Float
|
133
|
+
#
|
134
|
+
def float( str )
|
135
|
+
Float( str )
|
136
|
+
end
|
137
|
+
|
138
|
+
##
|
139
|
+
# convert an string to an Integer
|
140
|
+
#
|
141
|
+
def integer( str )
|
142
|
+
Float( str ).to_i
|
143
|
+
end
|
144
|
+
|
145
|
+
##
|
146
|
+
# convert a string to a String, yes redundant I know.
|
147
|
+
#
|
148
|
+
def string( str )
|
149
|
+
str
|
150
|
+
end
|
151
|
+
|
152
|
+
##
|
153
|
+
# convert a string to true of false
|
154
|
+
#
|
155
|
+
def boolean( str )
|
156
|
+
::Amalgalite::Boolean.to_bool( str )
|
157
|
+
end
|
158
|
+
|
159
|
+
##
|
160
|
+
# convert a string to a blob
|
161
|
+
#
|
162
|
+
def blob( str )
|
163
|
+
::Amalgalite::Blob.new( :string => str )
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
|
6
|
+
module Amalgalite::TypeMaps
|
7
|
+
##
|
8
|
+
# An Amalagliate TypeMap that has a one-to-one conversion between SQLite types
|
9
|
+
# and Ruby classes
|
10
|
+
#
|
11
|
+
class StorageMap < ::Amalgalite::TypeMap
|
12
|
+
##
|
13
|
+
# A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if
|
14
|
+
# nothing can be found then default to TEXT.
|
15
|
+
#
|
16
|
+
def bind_type_of( obj )
|
17
|
+
case obj
|
18
|
+
when Float
|
19
|
+
::Amalgalite::SQLite3::Constants::DataType::FLOAT
|
20
|
+
when Integer
|
21
|
+
::Amalgalite::SQLite3::Constants::DataType::INTEGER
|
22
|
+
when NilClass
|
23
|
+
::Amalgalite::SQLite3::Constants::DataType::NULL
|
24
|
+
when ::Amalgalite::Blob
|
25
|
+
::Amalgalite::SQLite3::Constants::DataType::BLOB
|
26
|
+
else
|
27
|
+
::Amalgalite::SQLite3::Constants::DataType::TEXT
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Do no mapping, just return the value as it was retrieved from SQLite.
|
33
|
+
#
|
34
|
+
def result_value_of( delcared_type, value )
|
35
|
+
return value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
#
|
6
|
+
|
7
|
+
module Amalgalite::TypeMaps
|
8
|
+
##
|
9
|
+
# An Amalagliate TypeMap that converts both bind parameters and result
|
10
|
+
# parameters to a String, no matter what.
|
11
|
+
#
|
12
|
+
class TextMap < ::Amalgalite::TypeMap
|
13
|
+
def bind_type_of( obj )
|
14
|
+
return ::Amalgalite::SQLite3::Constants::DataType::TEXT
|
15
|
+
end
|
16
|
+
|
17
|
+
def result_value_of( delcared_type, value )
|
18
|
+
return value.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
|
6
|
+
module Amalgalite
|
7
|
+
#
|
8
|
+
# a class representing the meta information about an SQLite view
|
9
|
+
#
|
10
|
+
class View
|
11
|
+
# the schame this view is assciated with
|
12
|
+
attr_accessor :schema
|
13
|
+
|
14
|
+
# the table name
|
15
|
+
attr_reader :name
|
16
|
+
|
17
|
+
# the original sql that was used to create this table
|
18
|
+
attr_reader :sql
|
19
|
+
|
20
|
+
def initialize( name, sql )
|
21
|
+
@name = name
|
22
|
+
@sql = sql
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/lib/amalgalite.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2008 Jeremy Hinegardner
|
3
|
+
# All rights reserved. See LICENSE and/or COPYING for details.
|
4
|
+
#++
|
5
|
+
|
6
|
+
# check if sqlite3 has already been required. Amalgalite conflicts with system
|
7
|
+
# level sqlite3 libraries.
|
8
|
+
unless $LOADED_FEATURES.grep( /\Asqlite3/ ).empty? then
|
9
|
+
raise LoadError, "amalgalite conflicts with sqlite3, please choose one or the other."
|
10
|
+
end
|
11
|
+
|
12
|
+
module Amalgalite
|
13
|
+
#
|
14
|
+
# Base class of all errors in Amalgalite
|
15
|
+
#
|
16
|
+
class Error < ::StandardError; end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Load the binary extension, try loading one for the specific version of ruby
|
20
|
+
# and if that fails, then fall back to one in the top of the library.
|
21
|
+
# this is the method recommended by rake-compiler
|
22
|
+
begin
|
23
|
+
# this will be for windows
|
24
|
+
require "amalgalite/#{RUBY_VERSION.sub(/\.\d$/,'')}/amalgalite"
|
25
|
+
rescue LoadError
|
26
|
+
# everyone else.
|
27
|
+
require 'amalgalite/amalgalite'
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
require 'amalgalite/aggregate'
|
32
|
+
require 'amalgalite/blob'
|
33
|
+
require 'amalgalite/boolean'
|
34
|
+
require 'amalgalite/busy_timeout'
|
35
|
+
require 'amalgalite/column'
|
36
|
+
require 'amalgalite/database'
|
37
|
+
require 'amalgalite/function'
|
38
|
+
require 'amalgalite/index'
|
39
|
+
require 'amalgalite/memory_database'
|
40
|
+
require 'amalgalite/paths'
|
41
|
+
require 'amalgalite/profile_tap'
|
42
|
+
require 'amalgalite/progress_handler'
|
43
|
+
require 'amalgalite/schema'
|
44
|
+
require 'amalgalite/sqlite3'
|
45
|
+
require 'amalgalite/statement'
|
46
|
+
require 'amalgalite/table'
|
47
|
+
require 'amalgalite/taps'
|
48
|
+
require 'amalgalite/trace_tap'
|
49
|
+
require 'amalgalite/type_map'
|
50
|
+
require 'amalgalite/version'
|
51
|
+
require 'amalgalite/view'
|
@@ -0,0 +1,158 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class AggregateTest1 < ::Amalgalite::Aggregate
|
4
|
+
def initialize
|
5
|
+
super
|
6
|
+
@name = 'atest1'
|
7
|
+
@arity = -1
|
8
|
+
@count = 0
|
9
|
+
end
|
10
|
+
def step( *args )
|
11
|
+
@count += 1
|
12
|
+
end
|
13
|
+
def finalize
|
14
|
+
return @count
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
describe "Aggregate SQL Functions" do
|
20
|
+
|
21
|
+
it "must have a finalize method implemented" do
|
22
|
+
ag = ::Amalgalite::Aggregate.new
|
23
|
+
lambda { ag.finalize }.should raise_error( NotImplementedError, /Aggregate#finalize must be implemented/ )
|
24
|
+
end
|
25
|
+
|
26
|
+
it "can define a custom SQL aggregate as a class with N params" do
|
27
|
+
@iso_db.define_aggregate("atest1", AggregateTest1 )
|
28
|
+
r = @iso_db.execute("SELECT atest1(id,name) as a, count(*) as c FROM country")
|
29
|
+
r.first['a'].should eql(r.first['c'])
|
30
|
+
r.first['a'].should eql(242)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "can remove a custom SQL aggregate by class" do
|
34
|
+
@iso_db.define_aggregate("atest1", AggregateTest1 )
|
35
|
+
@iso_db.aggregates.size.should eql(1)
|
36
|
+
r = @iso_db.execute("SELECT atest1(id,name) as a, count(*) as c FROM country")
|
37
|
+
r.first['a'].should eql(r.first['c'])
|
38
|
+
r.first['a'].should eql(242)
|
39
|
+
@iso_db.remove_aggregate( "atest1", AggregateTest1 )
|
40
|
+
@iso_db.aggregates.size.should eql(0)
|
41
|
+
lambda{ @iso_db.execute("SELECT atest1(id,name) as a, count(*) as c FROM country") }.should raise_error(::Amalgalite::SQLite3::Error, /no such function: atest1/ )
|
42
|
+
end
|
43
|
+
|
44
|
+
it "can remove a custom SQL aggregate by arity" do
|
45
|
+
@iso_db.define_aggregate("atest1", AggregateTest1 )
|
46
|
+
@iso_db.aggregates.size.should eql(1)
|
47
|
+
r = @iso_db.execute("SELECT atest1(id,name) as a, count(*) as c FROM country")
|
48
|
+
r.first['a'].should eql(r.first['c'])
|
49
|
+
r.first['a'].should eql(242)
|
50
|
+
@iso_db.remove_aggregate( "atest1", -1)
|
51
|
+
@iso_db.aggregates.size.should eql(0)
|
52
|
+
lambda{ @iso_db.execute("SELECT atest1(id,name) as a, count(*) as c FROM country") }.should raise_error(::Amalgalite::SQLite3::Error, /no such function: atest1/ )
|
53
|
+
end
|
54
|
+
|
55
|
+
it "can remove all custom SQL aggregates with the same name" do
|
56
|
+
class AT2 < AggregateTest1
|
57
|
+
def arity() 1; end
|
58
|
+
end
|
59
|
+
@iso_db.define_aggregate("atest1", AggregateTest1 )
|
60
|
+
@iso_db.define_aggregate("atest1", AT2)
|
61
|
+
@iso_db.aggregates.size.should eql(2)
|
62
|
+
r = @iso_db.execute("SELECT atest1(id,name) as a, atest1(id), count(*) as c FROM country")
|
63
|
+
r.first['a'].should eql(r.first['c'])
|
64
|
+
r.first['a'].should eql(242)
|
65
|
+
@iso_db.remove_aggregate( "atest1" )
|
66
|
+
@iso_db.aggregates.size.should eql(0)
|
67
|
+
lambda{ @iso_db.execute("SELECT atest1(id,name) as a, count(*) as c FROM country") }.should raise_error(::Amalgalite::SQLite3::Error, /no such function: atest1/ )
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
it "does not allow mixing of arbitrary and mandatory arguments to an SQL function" do
|
73
|
+
class AggregateTest2 < AggregateTest1
|
74
|
+
def name() "atest2"; end
|
75
|
+
def arity() -2; end
|
76
|
+
end
|
77
|
+
lambda { @iso_db.define_aggregate("atest2", AggregateTest2 ) }.should raise_error( ::Amalgalite::Database::AggregateError,
|
78
|
+
/Use only mandatory or arbitrary parameters in an SQL Aggregate, not both/ )
|
79
|
+
end
|
80
|
+
|
81
|
+
it "does not allow outrageous arity" do
|
82
|
+
class AggregateTest3 < AggregateTest1
|
83
|
+
def name() "atest3"; end
|
84
|
+
def arity() 128; end
|
85
|
+
end
|
86
|
+
lambda { @iso_db.define_aggregate("atest3", AggregateTest3 ) }.should raise_error( ::Amalgalite::SQLite3::Error, /SQLITE_ERROR .* Library used incorrectly/ )
|
87
|
+
end
|
88
|
+
|
89
|
+
it "does not allow registering a function which does not match the defined name " do
|
90
|
+
class AggregateTest4 < AggregateTest1
|
91
|
+
def name() "name_mismatch"; end
|
92
|
+
end
|
93
|
+
lambda { @iso_db.define_aggregate("atest4", AggregateTest4 ) }.should raise_error( ::Amalgalite::Database::AggregateError,
|
94
|
+
/Aggregate implementation name 'name_mismatch' does not match defined name 'atest4'/)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "handles an error being thrown during the step function" do
|
98
|
+
class AggregateTest5 < AggregateTest1
|
99
|
+
def initialize
|
100
|
+
super
|
101
|
+
@name = "atest5"
|
102
|
+
@arity = -1
|
103
|
+
@count = 0
|
104
|
+
end
|
105
|
+
|
106
|
+
def step( *args )
|
107
|
+
@count += 1
|
108
|
+
if @count > 50 then
|
109
|
+
raise "Stepwise error!" if @count > 50
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
@iso_db.define_aggregate( "atest5", AggregateTest5 )
|
116
|
+
lambda { @iso_db.execute( "SELECT atest5(*) AS a FROM country" ) }.should raise_error( ::Amalgalite::SQLite3::Error, /Stepwise error!/ )
|
117
|
+
end
|
118
|
+
|
119
|
+
it "handles an error being thrown during the finalize function" do
|
120
|
+
class AggregateTest6 < AggregateTest1
|
121
|
+
def initialize
|
122
|
+
super
|
123
|
+
@name = "atest6"
|
124
|
+
@count = 0
|
125
|
+
@arity = -1
|
126
|
+
end
|
127
|
+
def finalize
|
128
|
+
raise "Finalize error!"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
@iso_db.define_aggregate( "atest6", AggregateTest6 )
|
132
|
+
lambda { @iso_db.execute( "SELECT atest6(*) AS a FROM country" ) }.should raise_error( ::Amalgalite::SQLite3::Error, /Finalize error!/ )
|
133
|
+
end
|
134
|
+
|
135
|
+
it "handles an error being thrown during initialization in the C extension" do
|
136
|
+
class AggregateTest7 < AggregateTest1
|
137
|
+
@called = false
|
138
|
+
def self.called?
|
139
|
+
if @called then
|
140
|
+
raise "Initialization error!"
|
141
|
+
else
|
142
|
+
@called = true
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def initialize
|
147
|
+
super
|
148
|
+
@name = "atest7"
|
149
|
+
@count = 0
|
150
|
+
@arity = -1
|
151
|
+
self.class.called?
|
152
|
+
end
|
153
|
+
end
|
154
|
+
@iso_db.define_aggregate( "atest7", AggregateTest7 )
|
155
|
+
lambda { @iso_db.execute( "SELECT atest7(*) AS a FROM country" ) }.should raise_error( ::Amalgalite::SQLite3::Error, /Initialization error!/ )
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
data/spec/blob_spec.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Amalgalite::Blob do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@blob_db_name = File.join(File.dirname( __FILE__ ), "blob.db")
|
7
|
+
File.unlink @blob_db_name if File.exist?( @blob_db_name )
|
8
|
+
@db = Amalgalite::Database.new( @blob_db_name )
|
9
|
+
@schema_sql = <<-SQL
|
10
|
+
CREATE TABLE blobs(
|
11
|
+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
12
|
+
name VARCHAR(128) NOT NULL UNIQUE,
|
13
|
+
data TEXT );
|
14
|
+
SQL
|
15
|
+
@db.execute( @schema_sql )
|
16
|
+
@country_data_file = Amalgalite::Iso3166Database.country_data_file
|
17
|
+
@junk_file = File.join( File.dirname(__FILE__), "test_output")
|
18
|
+
end
|
19
|
+
|
20
|
+
after(:each) do
|
21
|
+
@db.close
|
22
|
+
File.unlink @blob_db_name if File.exist?( @blob_db_name )
|
23
|
+
File.unlink @junk_file if File.exist?( @junk_file )
|
24
|
+
end
|
25
|
+
|
26
|
+
{ :file => Amalgalite::Iso3166Database.country_data_file,
|
27
|
+
:string => IO.read( Amalgalite::Iso3166Database.country_data_file),
|
28
|
+
:io => StringIO.new( IO.read( Amalgalite::Iso3166Database.country_data_file) ) }.each_pair do |style, data |
|
29
|
+
describe "inserts a blob from a #{style}" do
|
30
|
+
before(:each) do
|
31
|
+
column = @db.schema.tables['blobs'].columns['data']
|
32
|
+
@db.execute("INSERT INTO blobs(name, data) VALUES ($name, $data)",
|
33
|
+
{ "$name" => @country_data_file,
|
34
|
+
"$data" => Amalgalite::Blob.new( style => data,
|
35
|
+
:column => column ) } )
|
36
|
+
@db.execute("VACUUM")
|
37
|
+
end
|
38
|
+
|
39
|
+
after(:each) do
|
40
|
+
@db.execute("DELETE FROM blobs")
|
41
|
+
data.rewind if data.respond_to?( :rewind )
|
42
|
+
end
|
43
|
+
|
44
|
+
it "and retrieves the data as a single value" do
|
45
|
+
all_rows = @db.execute("SELECT name,data FROM blobs")
|
46
|
+
all_rows.size.should eql(1)
|
47
|
+
all_rows.first['name'].should eql(@country_data_file)
|
48
|
+
all_rows.first['data'].should_not be_incremental
|
49
|
+
all_rows.first['data'].to_string_io.string.should eql(IO.read( @country_data_file ))
|
50
|
+
end
|
51
|
+
|
52
|
+
it "and retrieves the data using incremental IO" do
|
53
|
+
all_rows = @db.execute("SELECT * FROM blobs")
|
54
|
+
all_rows.size.should eql(1)
|
55
|
+
all_rows.first['name'].should eql(@country_data_file)
|
56
|
+
all_rows.first['data'].should be_incremental
|
57
|
+
all_rows.first['data'].to_string_io.string.should eql(IO.read( @country_data_file ))
|
58
|
+
end
|
59
|
+
|
60
|
+
it "writes the data to a file " do
|
61
|
+
all_rows = @db.execute("SELECT * FROM blobs")
|
62
|
+
all_rows.size.should eql(1)
|
63
|
+
all_rows.first['name'].should eql(@country_data_file)
|
64
|
+
all_rows.first['data'].should be_incremental
|
65
|
+
all_rows.first['data'].write_to_file( @junk_file )
|
66
|
+
IO.read( @junk_file).should eql(IO.read( @country_data_file ))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
it "raises an error if initialized incorrectly" do
|
74
|
+
lambda{ Amalgalite::Blob.new( :file => "/dev/null", :string => "foo" ) }.should raise_error( Amalgalite::Blob::Error )
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'amalgalite'
|
4
|
+
require 'amalgalite/boolean'
|
5
|
+
|
6
|
+
describe Amalgalite::Boolean do
|
7
|
+
%w[ True Y Yes T 1 ].each do |v|
|
8
|
+
it "converts #{v} to true" do
|
9
|
+
Amalgalite::Boolean.to_bool(v).should == true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
%w[ False F f No n 0 ].each do |v|
|
14
|
+
it "converts #{v} to false " do
|
15
|
+
Amalgalite::Boolean.to_bool(v).should == false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
%w[ other things nil ].each do |v|
|
20
|
+
it "converts #{v} to nil" do
|
21
|
+
Amalgalite::Boolean.to_bool(v).should == nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|