amalgalite 1.6.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +49 -0
  3. data/HISTORY.md +346 -0
  4. data/LICENSE +31 -0
  5. data/Manifest.txt +104 -0
  6. data/README.md +65 -0
  7. data/Rakefile +26 -0
  8. data/TODO.md +57 -0
  9. data/bin/amalgalite-pack +147 -0
  10. data/examples/a.rb +9 -0
  11. data/examples/blob.rb +88 -0
  12. data/examples/bootstrap.rb +36 -0
  13. data/examples/define_aggregate.rb +75 -0
  14. data/examples/define_function.rb +104 -0
  15. data/examples/fts5.rb +152 -0
  16. data/examples/gem-db.rb +94 -0
  17. data/examples/require_me.rb +11 -0
  18. data/examples/requires.rb +42 -0
  19. data/examples/schema-info.rb +34 -0
  20. data/ext/amalgalite/c/amalgalite.c +355 -0
  21. data/ext/amalgalite/c/amalgalite.h +151 -0
  22. data/ext/amalgalite/c/amalgalite_blob.c +240 -0
  23. data/ext/amalgalite/c/amalgalite_constants.c +1226 -0
  24. data/ext/amalgalite/c/amalgalite_database.c +1178 -0
  25. data/ext/amalgalite/c/amalgalite_requires_bootstrap.c +282 -0
  26. data/ext/amalgalite/c/amalgalite_statement.c +649 -0
  27. data/ext/amalgalite/c/extconf.rb +62 -0
  28. data/ext/amalgalite/c/gen_constants.rb +330 -0
  29. data/ext/amalgalite/c/notes.txt +134 -0
  30. data/ext/amalgalite/c/sqlite3.c +205352 -0
  31. data/ext/amalgalite/c/sqlite3.h +10727 -0
  32. data/ext/amalgalite/c/sqlite3_options.h +4 -0
  33. data/ext/amalgalite/c/sqlite3ext.h +578 -0
  34. data/lib/amalgalite.rb +51 -0
  35. data/lib/amalgalite/2.0/amalgalite.so +0 -0
  36. data/lib/amalgalite/2.1/amalgalite.so +0 -0
  37. data/lib/amalgalite/2.2/amalgalite.so +0 -0
  38. data/lib/amalgalite/2.3/amalgalite.so +0 -0
  39. data/lib/amalgalite/2.4/amalgalite.so +0 -0
  40. data/lib/amalgalite/aggregate.rb +67 -0
  41. data/lib/amalgalite/blob.rb +186 -0
  42. data/lib/amalgalite/boolean.rb +42 -0
  43. data/lib/amalgalite/busy_timeout.rb +47 -0
  44. data/lib/amalgalite/column.rb +99 -0
  45. data/lib/amalgalite/core_ext/kernel/require.rb +21 -0
  46. data/lib/amalgalite/csv_table_importer.rb +74 -0
  47. data/lib/amalgalite/database.rb +984 -0
  48. data/lib/amalgalite/function.rb +61 -0
  49. data/lib/amalgalite/index.rb +43 -0
  50. data/lib/amalgalite/memory_database.rb +15 -0
  51. data/lib/amalgalite/packer.rb +231 -0
  52. data/lib/amalgalite/paths.rb +80 -0
  53. data/lib/amalgalite/profile_tap.rb +131 -0
  54. data/lib/amalgalite/progress_handler.rb +21 -0
  55. data/lib/amalgalite/requires.rb +151 -0
  56. data/lib/amalgalite/schema.rb +225 -0
  57. data/lib/amalgalite/sqlite3.rb +6 -0
  58. data/lib/amalgalite/sqlite3/constants.rb +95 -0
  59. data/lib/amalgalite/sqlite3/database/function.rb +48 -0
  60. data/lib/amalgalite/sqlite3/database/status.rb +68 -0
  61. data/lib/amalgalite/sqlite3/status.rb +60 -0
  62. data/lib/amalgalite/sqlite3/version.rb +55 -0
  63. data/lib/amalgalite/statement.rb +418 -0
  64. data/lib/amalgalite/table.rb +91 -0
  65. data/lib/amalgalite/taps.rb +2 -0
  66. data/lib/amalgalite/taps/console.rb +27 -0
  67. data/lib/amalgalite/taps/io.rb +71 -0
  68. data/lib/amalgalite/trace_tap.rb +35 -0
  69. data/lib/amalgalite/type_map.rb +63 -0
  70. data/lib/amalgalite/type_maps/default_map.rb +166 -0
  71. data/lib/amalgalite/type_maps/storage_map.rb +38 -0
  72. data/lib/amalgalite/type_maps/text_map.rb +21 -0
  73. data/lib/amalgalite/version.rb +8 -0
  74. data/lib/amalgalite/view.rb +26 -0
  75. data/spec/aggregate_spec.rb +154 -0
  76. data/spec/amalgalite_spec.rb +4 -0
  77. data/spec/blob_spec.rb +78 -0
  78. data/spec/boolean_spec.rb +24 -0
  79. data/spec/busy_handler.rb +157 -0
  80. data/spec/data/iso-3166-country.txt +242 -0
  81. data/spec/data/iso-3166-schema.sql +22 -0
  82. data/spec/data/iso-3166-subcountry.txt +3995 -0
  83. data/spec/data/make-iso-db.sh +12 -0
  84. data/spec/database_spec.rb +508 -0
  85. data/spec/default_map_spec.rb +92 -0
  86. data/spec/function_spec.rb +78 -0
  87. data/spec/integeration_spec.rb +97 -0
  88. data/spec/iso_3166_database.rb +58 -0
  89. data/spec/packer_spec.rb +60 -0
  90. data/spec/paths_spec.rb +28 -0
  91. data/spec/progress_handler_spec.rb +91 -0
  92. data/spec/requires_spec.rb +54 -0
  93. data/spec/rtree_spec.rb +66 -0
  94. data/spec/schema_spec.rb +131 -0
  95. data/spec/spec_helper.rb +48 -0
  96. data/spec/sqlite3/constants_spec.rb +108 -0
  97. data/spec/sqlite3/database_status_spec.rb +36 -0
  98. data/spec/sqlite3/status_spec.rb +22 -0
  99. data/spec/sqlite3/version_spec.rb +28 -0
  100. data/spec/sqlite3_spec.rb +53 -0
  101. data/spec/statement_spec.rb +168 -0
  102. data/spec/storage_map_spec.rb +38 -0
  103. data/spec/tap_spec.rb +57 -0
  104. data/spec/text_map_spec.rb +20 -0
  105. data/spec/type_map_spec.rb +14 -0
  106. data/spec/version_spec.rb +8 -0
  107. data/tasks/custom.rake +102 -0
  108. data/tasks/default.rake +240 -0
  109. data/tasks/extension.rake +38 -0
  110. data/tasks/this.rb +208 -0
  111. metadata +318 -0
@@ -0,0 +1,91 @@
1
+ #--
2
+ # Copyright (c) 2008 Jeremy Hinegardner
3
+ # All rights reserved. See LICENSE and/or COPYING for details.
4
+ #++
5
+ require 'set'
6
+ module Amalgalite
7
+ #
8
+ # a class representing the meta information about an SQLite table
9
+ #
10
+ class Table
11
+ # the schema object the table is associated 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
+ # hash of Index objects holding the meta informationa about the indexes
21
+ # on this table. The keys of the indexes variable is the index name
22
+ attr_accessor :indexes
23
+
24
+ # a hash of Column objects holding the meta information about the columns
25
+ # in this table. keys are the column names
26
+ attr_accessor :columns
27
+
28
+ def initialize( name, sql = nil )
29
+ @name = name
30
+ @sql = sql
31
+ @indexes = {}
32
+ @columns = {}
33
+ @schema = nil
34
+ @primary_key = nil
35
+ end
36
+
37
+ # Is the table a temporary table or not
38
+ def temporary?
39
+ schema.temporary?
40
+ end
41
+
42
+ # the Columns in original definition order
43
+ def columns_in_order
44
+ @columns.values.sort_by { |c| c.order }
45
+ end
46
+
47
+ # the column names in original definition order
48
+ def column_names
49
+ columns_in_order.map { |c| c.name }
50
+ end
51
+
52
+ # the columns that make up the primary key
53
+ def primary_key_columns
54
+ @columns.values.find_all { |c| c.primary_key? }
55
+ end
56
+
57
+ # the array of colmuns that make up the primary key of the table
58
+ # since a primary key has an index, we loop over all the indexes for the
59
+ # table and pick the first one that is unique, and all the columns in the
60
+ # index have primary_key? as true.
61
+ #
62
+ # we do this instead of just looking for the columns where primary key is
63
+ # true because we want the columns in primary key order
64
+ def primary_key
65
+ unless @primary_key
66
+ pk_column_names = Set.new( primary_key_columns.collect { |c| c.name } )
67
+ unique_indexes = indexes.values.find_all { |i| i.unique? }
68
+
69
+ pk_result = []
70
+
71
+ unique_indexes.each do |idx|
72
+ idx_column_names = Set.new( idx.columns.collect { |c| c.name } )
73
+ r = idx_column_names ^ pk_column_names
74
+ if r.size == 0 then
75
+ pk_result = idx.columns
76
+ break
77
+ end
78
+ end
79
+
80
+ # no joy, see about just using all the columns that say the are primary
81
+ # keys
82
+ if pk_result.empty? then
83
+ pk_result = self.primary_key_columns
84
+ end
85
+ @primary_key = pk_result
86
+ end
87
+ return @primary_key
88
+ end
89
+ end
90
+ end
91
+
@@ -0,0 +1,2 @@
1
+ require 'amalgalite/taps/console'
2
+ require 'amalgalite/taps/io'
@@ -0,0 +1,27 @@
1
+ #--
2
+ # Copyright (c) 2008 Jeremy Hinegardner
3
+ # All rights reserved. See LICENSE and/or COPYING for details.
4
+ #++
5
+
6
+ require 'amalgalite/taps/io'
7
+
8
+ module Amalgalite::Taps
9
+ #
10
+ # Class provide an IO tap that can write to $stdout
11
+ #
12
+ class Stdout < ::Amalgalite::Taps::IO
13
+ def initialize
14
+ super( $stdout )
15
+ end
16
+ end
17
+
18
+ #
19
+ # This class provide an IO tap that can write to $stderr
20
+ #
21
+ class Stderr < ::Amalgalite::Taps::IO
22
+ def initialize
23
+ super( $stderr )
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,71 @@
1
+ #--
2
+ # Copyright (c) 2008 Jeremy Hinegardner
3
+ # All rights reserved. See LICENSE and/or COPYING for details.
4
+ #++
5
+
6
+ require 'amalgalite/profile_tap'
7
+ require 'stringio'
8
+
9
+ module Amalgalite
10
+ module Taps
11
+ #
12
+ # An IOTap is an easy way to send all top information to andy IO based
13
+ # object. Both profile and trace tap information can be captured
14
+ # This means you can send the events to STDOUT with:
15
+ #
16
+ # db.profile_tap = db.trace_tap = Amalgalite::Taps::Stdout.new
17
+ #
18
+ #
19
+ class IO
20
+
21
+ attr_reader :profile_tap
22
+ attr_reader :io
23
+
24
+ def initialize( io )
25
+ @io = io
26
+ @profile_tap = ProfileTap.new( self, 'output_profile_event' )
27
+ end
28
+
29
+ def trace( msg )
30
+ io.puts msg
31
+ end
32
+
33
+ # need a profile method, it routes through the profile tap which calls back
34
+ # to output_profile_event
35
+ def profile( msg, time )
36
+ @profile_tap.profile(msg, time)
37
+ end
38
+
39
+ def output_profile_event( msg, time )
40
+ io.puts "#{time} : #{msg}"
41
+ end
42
+
43
+ def dump_profile
44
+ samplers.each_pair do |k,v|
45
+ io.puts v.to_s
46
+ end
47
+ end
48
+
49
+ def samplers
50
+ profile_tap.samplers
51
+ end
52
+ end
53
+
54
+ #
55
+ # This class provides an IO tap that writes to a StringIO. The result is
56
+ # available via .to_s or .string.
57
+ #
58
+ class StringIO < ::Amalgalite::Taps::IO
59
+ def initialize
60
+ @stringio = ::StringIO.new
61
+ super( @stringio )
62
+ end
63
+
64
+ def to_s
65
+ @stringio.string
66
+ end
67
+ alias :string :to_s
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,35 @@
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 TraceTap receives tracing information from SQLite3. It receives the SQL
9
+ # statement being executed as a +msg+ just before the statement first begins
10
+ # executing.
11
+ #
12
+ # A TraceTap is a wrapper around another object and a method. The Tap object
13
+ # will receive the call to +trace+ and redirect that call to another object
14
+ # and method.
15
+ #
16
+ class TraceTap
17
+
18
+ attr_reader :delegate_obj
19
+ attr_reader :delegate_method
20
+
21
+ def initialize( wrapped_obj, send_to = 'trace' )
22
+ unless wrapped_obj.respond_to?( send_to )
23
+ raise Amalgalite::Error, "#{wrapped_obj.class.name} does not respond to #{send_to.to_s} "
24
+ end
25
+
26
+ @delegate_obj = wrapped_obj
27
+ @delegate_method = send_to
28
+ end
29
+
30
+ def trace( msg )
31
+ delegate_obj.send( delegate_method, msg )
32
+ end
33
+ end
34
+ end
35
+
@@ -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 ],
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