sqlite3 1.4.4 → 1.5.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # Ruby Interface for SQLite3
2
+
3
+ * Source code: https://github.com/sparklemotion/sqlite3-ruby
4
+ * Mailing list: http://groups.google.com/group/sqlite3-ruby
5
+ * Download: http://rubygems.org/gems/sqlite3
6
+ * Documentation: http://www.rubydoc.info/gems/sqlite3
7
+
8
+ [![Unit tests](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/sqlite3-ruby.yml/badge.svg)](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/sqlite3-ruby.yml)
9
+ [![Native packages](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/gem-install.yml/badge.svg)](https://github.com/sparklemotion/sqlite3-ruby/actions/workflows/gem-install.yml)
10
+
11
+
12
+ ## Description
13
+
14
+ This library allows Ruby programs to use the SQLite3 database engine (http://www.sqlite.org).
15
+
16
+ Note that this module is only compatible with SQLite 3.6.16 or newer.
17
+
18
+
19
+ ## Synopsis
20
+
21
+ ``` ruby
22
+ require "sqlite3"
23
+
24
+ # Open a database
25
+ db = SQLite3::Database.new "test.db"
26
+
27
+ # Create a table
28
+ rows = db.execute <<-SQL
29
+ create table numbers (
30
+ name varchar(30),
31
+ val int
32
+ );
33
+ SQL
34
+
35
+ # Execute a few inserts
36
+ {
37
+ "one" => 1,
38
+ "two" => 2,
39
+ }.each do |pair|
40
+ db.execute "insert into numbers values ( ?, ? )", pair
41
+ end
42
+
43
+ # Find a few rows
44
+ db.execute( "select * from numbers" ) do |row|
45
+ p row
46
+ end
47
+ # => ["one", 1]
48
+ # ["two", 2]
49
+
50
+ # Create another table with multiple columns
51
+ db.execute <<-SQL
52
+ create table students (
53
+ name varchar(50),
54
+ email varchar(50),
55
+ grade varchar(5),
56
+ blog varchar(50)
57
+ );
58
+ SQL
59
+
60
+ # Execute inserts with parameter markers
61
+ db.execute("INSERT INTO students (name, email, grade, blog)
62
+ VALUES (?, ?, ?, ?)", ["Jane", "me@janedoe.com", "A", "http://blog.janedoe.com"])
63
+
64
+ db.execute( "select * from students" ) do |row|
65
+ p row
66
+ end
67
+ # => ["Jane", "me@janedoe.com", "A", "http://blog.janedoe.com"]
68
+ ```
69
+
70
+ ## Installation
71
+
72
+ ### Native Gems (recommended)
73
+
74
+ As of v1.5.0 of this library, native (precompiled) gems are available for Ruby 2.6, 2.7, 3.0, and 3.1 on all these platforms:
75
+
76
+ - `aarch64-linux`
77
+ - `arm-linux`
78
+ - `arm64-darwin`
79
+ - `x64-mingw32` / `x64-mingw-ucrt`
80
+ - `x86-linux`
81
+ - `x86_64-darwin`
82
+ - `x86_64-linux`
83
+
84
+ If you are using one of these Ruby versions on one of these platforms, the native gem is the recommended way to install sqlite3-ruby.
85
+
86
+ For example, on a linux system running Ruby 3.1:
87
+
88
+ ``` text
89
+ $ ruby -v
90
+ ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
91
+
92
+ $ time gem install sqlite3
93
+ Fetching sqlite3-1.5.0-x86_64-linux.gem
94
+ Successfully installed sqlite3-1.5.0-x86_64-linux
95
+ 1 gem installed
96
+
97
+ real 0m4.274s
98
+ user 0m0.734s
99
+ sys 0m0.165s
100
+ ```
101
+
102
+ #### Avoiding the precompiled native gem
103
+
104
+ The maintainers strongly urge you to use a native gem if at all possible. It will be a better experience for you and allow us to focus our efforts on improving functionality rather than diagnosing installation issues.
105
+
106
+ If you're on a platform that supports a native gem but you want to avoid using it in your project, do one of the following:
107
+
108
+ - If you're not using Bundler, then run `gem install sqlite3 --platform=ruby`
109
+ - If you are using Bundler
110
+ - version 2.1 or later, then you'll need to run `bundle config set force_ruby_platform true`,
111
+ - version 2.0 or earlier, then you'll need to run `bundle config force_ruby_platform true`
112
+
113
+
114
+ ### Compiling the source gem
115
+
116
+ If you are on a platform or version of Ruby that is not covered by the Native Gems, then the vanilla "ruby platform" (non-native) gem will be installed by the `gem install` or `bundle` commands.
117
+
118
+
119
+ #### Packaged libsqlite3
120
+
121
+ By default, as of v1.5.0 of this library, libsqlite3 v3.38.5 is packaged with the gem and will be compiled and used automatically. This takes a bit longer than the native gem, but will provide a modern, well-supported version of libsqlite3.
122
+
123
+ For example, on a linux system running Ruby 2.5:
124
+
125
+ ``` text
126
+ $ ruby -v
127
+ ruby 2.5.9p229 (2021-04-05 revision 67939) [x86_64-linux]
128
+
129
+ $ time gem install sqlite3
130
+ Building native extensions. This could take a while...
131
+ Successfully installed sqlite3-1.5.0
132
+ 1 gem installed
133
+
134
+ real 0m20.620s
135
+ user 0m23.361s
136
+ sys 0m5.839s
137
+ ```
138
+
139
+
140
+ #### System libsqlite3
141
+
142
+ If you would prefer to build the sqlite3-ruby gem against your system libsqlite3, which requires that you install libsqlite3 and its development files yourself, you may do so by using the `--enable-system-libraries` flag at gem install time.
143
+
144
+ PLEASE NOTE:
145
+
146
+ - only versions of libsqlite3 `>= 3.5.0` are supported,
147
+ - and some library features may depend on how your libsqlite3 was compiled.
148
+
149
+ For example, on a linux system running Ruby 2.5:
150
+
151
+ ``` text
152
+ $ time gem install sqlite3 -- --enable-system-libraries
153
+ Building native extensions with: '--enable-system-libraries'
154
+ This could take a while...
155
+ Successfully installed sqlite3-1.5.0
156
+ 1 gem installed
157
+
158
+ real 0m4.234s
159
+ user 0m3.809s
160
+ sys 0m0.912s
161
+ ```
162
+
163
+ If you're using bundler, you can opt into system libraries like this:
164
+
165
+ ``` sh
166
+ bundle config build.sqlite3 --enable-system-libraries
167
+ ```
168
+
169
+ If you have sqlite3 installed in a non-standard location, you may need to specify the location of the include and lib files by using `--with-sqlite-include` and `--with-sqlite-lib` options (or a `--with-sqlite-dir` option, see [MakeMakefile#dir_config](https://ruby-doc.org/stdlib-3.1.1/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config)). If you have pkg-config installed and configured properly, this may not be necessary.
170
+
171
+ ``` sh
172
+ gem install sqlite3 -- \
173
+ --enable-system-libraries \
174
+ --with-sqlite3-include=/opt/local/include \
175
+ --with-sqlite3-lib=/opt/local/lib
176
+ ```
177
+
178
+
179
+ #### System libsqlcipher
180
+
181
+ If you'd like to link against a system-installed libsqlcipher, you may do so by using the `--with-sqlcipher` flag:
182
+
183
+ ``` text
184
+ $ time gem install sqlite3 -- --with-sqlcipher
185
+ Building native extensions with: '--with-sqlcipher'
186
+ This could take a while...
187
+ Successfully installed sqlite3-1.5.0
188
+ 1 gem installed
189
+
190
+ real 0m4.772s
191
+ user 0m3.906s
192
+ sys 0m0.896s
193
+ ```
194
+
195
+ If you have sqlcipher installed in a non-standard location, you may need to specify the location of the include and lib files by using `--with-sqlite-include` and `--with-sqlite-lib` options (or a `--with-sqlite-dir` option, see [MakeMakefile#dir_config](https://ruby-doc.org/stdlib-3.1.1/libdoc/mkmf/rdoc/MakeMakefile.html#method-i-dir_config)). If you have pkg-config installed and configured properly, this may not be necessary.
196
+
197
+
198
+ ## Support
199
+
200
+ ### Something has gone wrong! Where do I get help?
201
+
202
+ You can ask for help or support from the
203
+ [sqlite3-ruby mailing list](http://groups.google.com/group/sqlite3-ruby) which
204
+ can be found here:
205
+
206
+ > http://groups.google.com/group/sqlite3-ruby
207
+
208
+
209
+ ### I've found a bug! How do I report it?
210
+
211
+ After contacting the mailing list, you've found that you've uncovered a bug. You can file the bug at the [github issues page](https://github.com/sparklemotion/sqlite3-ruby/issues) which can be found here:
212
+
213
+ > https://github.com/sparklemotion/sqlite3-ruby/issues
214
+
215
+
216
+ ## Usage
217
+
218
+ For help figuring out the SQLite3/Ruby interface, check out the SYNOPSIS as well as the RDoc. It includes examples of usage. If you have any questions that you feel should be addressed in the FAQ, please send them to [the mailing list](http://groups.google.com/group/sqlite3-ruby).
219
+
220
+
221
+ ## Contributing
222
+
223
+ See [`CONTRIBUTING.md`](./CONTRIBUTING.md).
224
+
225
+
226
+ ## License
227
+
228
+ This library is licensed under `BSD-3-Clause`, see [`LICENSE`](./LICENSE).
229
+
230
+
231
+ ### Dependencies
232
+
233
+ The source code of `sqlite` is distributed in the "ruby platform" gem. This code is public domain, see [`LICENSE-DEPENDENCIES`](./LICENSE-DEPENDENCIES) for details.
@@ -1,7 +1,7 @@
1
1
  #include <sqlite3_ruby.h>
2
2
  #include <aggregator.h>
3
3
 
4
- #if _MSC_VER
4
+ #ifdef _MSC_VER
5
5
  #pragma warning( push )
6
6
  #pragma warning( disable : 4028 )
7
7
  #endif
@@ -40,18 +40,17 @@ static VALUE sqlite3_rb_close(VALUE self);
40
40
  static VALUE rb_sqlite3_open_v2(VALUE self, VALUE file, VALUE mode, VALUE zvfs)
41
41
  {
42
42
  sqlite3RubyPtr ctx;
43
- VALUE flags;
44
43
  int status;
45
44
 
46
45
  Data_Get_Struct(self, sqlite3Ruby, ctx);
47
46
 
48
47
  #if defined TAINTING_SUPPORT
49
- #if defined StringValueCStr
48
+ # if defined StringValueCStr
50
49
  StringValuePtr(file);
51
50
  rb_check_safe_obj(file);
52
- #else
51
+ # else
53
52
  Check_SafeStr(file);
54
- #endif
53
+ # endif
55
54
  #endif
56
55
 
57
56
  status = sqlite3_open_v2(
@@ -745,9 +744,9 @@ static VALUE exec_batch(VALUE self, VALUE sql, VALUE results_as_hash)
745
744
  REQUIRE_OPEN_DB(ctx);
746
745
 
747
746
  if(results_as_hash == Qtrue) {
748
- status = sqlite3_exec(ctx->db, StringValuePtr(sql), hash_callback_function, callback_ary, &errMsg);
747
+ status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)hash_callback_function, (void*)callback_ary, &errMsg);
749
748
  } else {
750
- status = sqlite3_exec(ctx->db, StringValuePtr(sql), regular_callback_function, callback_ary, &errMsg);
749
+ status = sqlite3_exec(ctx->db, StringValuePtr(sql), (sqlite3_callback)regular_callback_function, (void*)callback_ary, &errMsg);
751
750
  }
752
751
 
753
752
  if (status != SQLITE_OK)
@@ -849,6 +848,6 @@ void init_sqlite3_database()
849
848
  rb_sqlite3_aggregator_init();
850
849
  }
851
850
 
852
- #if _MSC_VER
851
+ #ifdef _MSC_VER
853
852
  #pragma warning( pop )
854
853
  #endif
@@ -1,100 +1,159 @@
1
- ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
2
-
3
- require 'mkmf'
4
-
5
- # :stopdoc:
6
-
7
- RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
8
-
9
- ldflags = cppflags = nil
10
- if RbConfig::CONFIG["host_os"] =~ /darwin/
11
- begin
12
- if with_config('sqlcipher')
13
- brew_prefix = `brew --prefix sqlcipher`.chomp
14
- ldflags = "#{brew_prefix}/lib"
15
- cppflags = "#{brew_prefix}/include/sqlcipher"
16
- pkg_conf = "#{brew_prefix}/lib/pkgconfig"
17
- else
18
- brew_prefix = `brew --prefix sqlite3`.chomp
19
- ldflags = "#{brew_prefix}/lib"
20
- cppflags = "#{brew_prefix}/include"
21
- pkg_conf = "#{brew_prefix}/lib/pkgconfig"
1
+ require "mkmf"
2
+ require "mini_portile2"
3
+
4
+ module Sqlite3
5
+ module ExtConf
6
+ ENV_ALLOWLIST = ["CC", "CFLAGS", "LDFLAGS", "LIBS", "CPPFLAGS", "LT_SYS_LIBRARY_PATH", "CPP"]
7
+
8
+ class << self
9
+ def configure
10
+ configure_cross_compiler
11
+
12
+ if system_libraries?
13
+ message "Building sqlite3-ruby using system #{libname}.\n"
14
+ configure_system_libraries
15
+ else
16
+ message "Building sqlite3-ruby using packaged sqlite3.\n"
17
+ configure_packaged_libraries
18
+ end
19
+
20
+ configure_extension
21
+
22
+ create_makefile('sqlite3/sqlite3_native')
23
+ end
24
+
25
+ def configure_cross_compiler
26
+ RbConfig::CONFIG["CC"] = RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"]
27
+ ENV["CC"] = RbConfig::CONFIG["CC"]
28
+ end
29
+
30
+ def system_libraries?
31
+ sqlcipher? || enable_config("system-libraries")
32
+ end
33
+
34
+ def libname
35
+ sqlcipher? ? "sqlcipher" : "sqlite3"
36
+ end
37
+
38
+ def sqlcipher?
39
+ with_config("sqlcipher")
40
+ end
41
+
42
+ def configure_system_libraries
43
+ pkg_config(libname)
44
+ append_cflags("-DUSING_SQLCIPHER") if sqlcipher?
45
+ end
46
+
47
+ def configure_packaged_libraries
48
+ minimal_recipe.tap do |recipe|
49
+ recipe.configure_options += ["--enable-shared=no", "--enable-static=yes"]
50
+ ENV.to_h.tap do |env|
51
+ env["CFLAGS"] = [env["CFLAGS"], "-fPIC"].join(" ") # needed for linking the static library into a shared library
52
+ recipe.configure_options += env.select { |k,v| ENV_ALLOWLIST.include?(k) }
53
+ .map { |key, value| "#{key}=#{value.strip}" }
54
+ end
55
+
56
+ unless File.exist?(File.join(recipe.target, recipe.host, recipe.name, recipe.version))
57
+ recipe.cook
58
+ end
59
+ recipe.activate
60
+
61
+ ENV["PKG_CONFIG_ALLOW_SYSTEM_CFLAGS"] = "t" # on macos, pkg-config will not return --cflags without this
62
+ pcfile = File.join(recipe.path, "lib", "pkgconfig", "sqlite3.pc")
63
+ if pkg_config(pcfile)
64
+ # see https://bugs.ruby-lang.org/issues/18490
65
+ libs = xpopen(["pkg-config", "--libs", "--static", pcfile], err: [:child, :out], &:read)
66
+ libs.split.each { |lib| append_ldflags(lib) } if $?.success?
67
+ else
68
+ abort("\nCould not configure the build properly. Please install either the `pkg-config` utility or the `pkg-config` rubygem.\n\n")
69
+ end
70
+ end
71
+ end
72
+
73
+ def configure_extension
74
+ if Gem::Requirement.new("< 2.7").satisfied_by?(Gem::Version.new(RUBY_VERSION))
75
+ append_cflags("-DTAINTING_SUPPORT")
76
+ end
77
+
78
+ abort_could_not_find("sqlite3.h") unless find_header("sqlite3.h")
79
+ abort_could_not_find(libname) unless find_library(libname, "sqlite3_libversion_number", "sqlite3.h")
80
+
81
+ # Functions defined in 1.9 but not 1.8
82
+ have_func('rb_proc_arity')
83
+
84
+ # Functions defined in 2.1 but not 2.0
85
+ have_func('rb_integer_pack')
86
+
87
+ # These functions may not be defined
88
+ have_func('sqlite3_initialize')
89
+ have_func('sqlite3_backup_init')
90
+ have_func('sqlite3_column_database_name')
91
+ have_func('sqlite3_enable_load_extension')
92
+ have_func('sqlite3_load_extension')
93
+
94
+ unless have_func('sqlite3_open_v2') # https://www.sqlite.org/releaselog/3_5_0.html
95
+ abort("\nPlease use a version of SQLite3 >= 3.5.0\n\n")
96
+ end
97
+
98
+ have_func('sqlite3_prepare_v2')
99
+ have_type('sqlite3_int64', 'sqlite3.h')
100
+ have_type('sqlite3_uint64', 'sqlite3.h')
101
+ end
102
+
103
+ def minimal_recipe
104
+ MiniPortile.new(libname, sqlite3_config[:version]).tap do |recipe|
105
+ recipe.files = sqlite3_config[:files]
106
+ recipe.target = File.join(package_root_dir, "ports")
107
+ recipe.patch_files = Dir[File.join(package_root_dir, "patches", "*.patch")].sort
108
+ end
109
+ end
110
+
111
+ def package_root_dir
112
+ File.expand_path(File.join(File.dirname(__FILE__), "..", ".."))
113
+ end
114
+
115
+ def sqlite3_config
116
+ mini_portile_config[:sqlite3]
117
+ end
118
+
119
+ def mini_portile_config
120
+ {
121
+ sqlite3: {
122
+ # checksum verified by first checking the published sha3(256) checksum:
123
+ #
124
+ # $ sha3sum -a 256 sqlite-autoconf-3390000.tar.gz
125
+ # b8e5b3265992350d40c4ad31efc2e6dec6256813f1d5acc8f0ea805e9f33ca2a sqlite-autoconf-3390000.tar.gz
126
+ #
127
+ # $ sha256sum sqlite-autoconf-3390000.tar.gz
128
+ # e90bcaef6dd5813fcdee4e867f6b65f3c9bfd0aec0f1017f9f3bbce1e4ed09e2 sqlite-autoconf-3390000.tar.gz
129
+ #
130
+ version: "3.39.0",
131
+ files: [{
132
+ url: "https://www.sqlite.org/2022/sqlite-autoconf-3390000.tar.gz",
133
+ sha256: "e90bcaef6dd5813fcdee4e867f6b65f3c9bfd0aec0f1017f9f3bbce1e4ed09e2",
134
+ }],
135
+ }
136
+ }
137
+ end
138
+
139
+ def abort_could_not_find(missing)
140
+ abort("\nCould not find #{missing}.\nPlease visit https://github.com/sparklemotion/sqlite3-ruby for installation instructions.\n\n")
141
+ end
142
+
143
+ def cross_build?
144
+ enable_config("cross-build")
145
+ end
146
+
147
+ def download
148
+ minimal_recipe.download
149
+ end
22
150
  end
23
-
24
- # pkg_config should be less error prone than parsing compiler
25
- # commandline options, but we need to set default ldflags and cpp flags
26
- # in case the user doesn't have pkg-config installed
27
- ENV['PKG_CONFIG_PATH'] ||= pkg_conf
28
- rescue
29
- end
30
- end
31
-
32
- if with_config('sqlcipher')
33
- pkg_config("sqlcipher")
34
- else
35
- pkg_config("sqlite3")
36
- end
37
-
38
- # --with-sqlite3-{dir,include,lib}
39
- if with_config('sqlcipher')
40
- $CFLAGS << ' -DUSING_SQLCIPHER'
41
- dir_config("sqlcipher", cppflags, ldflags)
42
- else
43
- dir_config("sqlite3", cppflags, ldflags)
44
- end
45
-
46
- if RbConfig::CONFIG["host_os"] =~ /mswin/
47
- $CFLAGS << ' -W3'
48
- end
49
-
50
- if RUBY_VERSION < '2.7'
51
- $CFLAGS << ' -DTAINTING_SUPPORT'
52
- end
53
-
54
- def asplode missing
55
- if RUBY_PLATFORM =~ /mingw|mswin/
56
- abort "#{missing} is missing. Install SQLite3 from " +
57
- "http://www.sqlite.org/ first."
58
- else
59
- abort <<-error
60
- #{missing} is missing. Try 'brew install sqlite3',
61
- 'yum install sqlite-devel' or 'apt-get install libsqlite3-dev'
62
- and check your shared library search path (the
63
- location where your sqlite3 shared library is located).
64
- error
65
151
  end
66
152
  end
67
153
 
68
- asplode('sqlite3.h') unless find_header 'sqlite3.h'
69
- find_library 'pthread', 'pthread_create' # 1.8 support. *shrug*
70
-
71
- have_library 'dl' # for static builds
72
-
73
- if with_config('sqlcipher')
74
- asplode('sqlcipher') unless find_library 'sqlcipher', 'sqlite3_libversion_number'
75
- else
76
- asplode('sqlite3') unless find_library 'sqlite3', 'sqlite3_libversion_number'
77
- end
78
-
79
- # Functions defined in 1.9 but not 1.8
80
- have_func('rb_proc_arity')
81
-
82
- # Functions defined in 2.1 but not 2.0
83
- have_func('rb_integer_pack')
84
-
85
- # These functions may not be defined
86
- have_func('sqlite3_initialize')
87
- have_func('sqlite3_backup_init')
88
- have_func('sqlite3_column_database_name')
89
- have_func('sqlite3_enable_load_extension')
90
- have_func('sqlite3_load_extension')
91
-
92
- unless have_func('sqlite3_open_v2')
93
- abort "Please use a newer version of SQLite3"
154
+ if arg_config("--download-dependencies")
155
+ Sqlite3::ExtConf.download
156
+ exit!(0)
94
157
  end
95
158
 
96
- have_func('sqlite3_prepare_v2')
97
- have_type('sqlite3_int64', 'sqlite3.h')
98
- have_type('sqlite3_uint64', 'sqlite3.h')
99
-
100
- create_makefile('sqlite3/sqlite3_native')
159
+ Sqlite3::ExtConf.configure
@@ -158,4 +158,6 @@ void Init_sqlite3_native()
158
158
  rb_define_singleton_method(mSqlite3, "threadsafe", threadsafe_p, 0);
159
159
  rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION));
160
160
  rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER));
161
+ rb_define_const(mSqlite3, "SQLITE_LOADED_VERSION", rb_str_new2(sqlite3_libversion()));
162
+
161
163
  }