amalgalite 1.8.0-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +60 -0
  3. data/HISTORY.md +386 -0
  4. data/LICENSE +31 -0
  5. data/Manifest.txt +105 -0
  6. data/README.md +62 -0
  7. data/Rakefile +27 -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 +1432 -0
  24. data/ext/amalgalite/c/amalgalite_database.c +1188 -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 +71 -0
  28. data/ext/amalgalite/c/gen_constants.rb +353 -0
  29. data/ext/amalgalite/c/notes.txt +134 -0
  30. data/ext/amalgalite/c/sqlite3.c +243616 -0
  31. data/ext/amalgalite/c/sqlite3.h +12894 -0
  32. data/ext/amalgalite/c/sqlite3_options.h +4 -0
  33. data/ext/amalgalite/c/sqlite3ext.h +705 -0
  34. data/lib/amalgalite/3.1/amalgalite.so +0 -0
  35. data/lib/amalgalite/aggregate.rb +73 -0
  36. data/lib/amalgalite/blob.rb +186 -0
  37. data/lib/amalgalite/boolean.rb +42 -0
  38. data/lib/amalgalite/busy_timeout.rb +47 -0
  39. data/lib/amalgalite/column.rb +99 -0
  40. data/lib/amalgalite/core_ext/kernel/require.rb +21 -0
  41. data/lib/amalgalite/csv_table_importer.rb +75 -0
  42. data/lib/amalgalite/database.rb +933 -0
  43. data/lib/amalgalite/function.rb +61 -0
  44. data/lib/amalgalite/index.rb +43 -0
  45. data/lib/amalgalite/memory_database.rb +15 -0
  46. data/lib/amalgalite/packer.rb +231 -0
  47. data/lib/amalgalite/paths.rb +80 -0
  48. data/lib/amalgalite/profile_tap.rb +131 -0
  49. data/lib/amalgalite/progress_handler.rb +21 -0
  50. data/lib/amalgalite/requires.rb +151 -0
  51. data/lib/amalgalite/schema.rb +225 -0
  52. data/lib/amalgalite/sqlite3/constants.rb +95 -0
  53. data/lib/amalgalite/sqlite3/database/function.rb +48 -0
  54. data/lib/amalgalite/sqlite3/database/status.rb +68 -0
  55. data/lib/amalgalite/sqlite3/status.rb +60 -0
  56. data/lib/amalgalite/sqlite3/version.rb +55 -0
  57. data/lib/amalgalite/sqlite3.rb +6 -0
  58. data/lib/amalgalite/statement.rb +421 -0
  59. data/lib/amalgalite/table.rb +91 -0
  60. data/lib/amalgalite/taps/console.rb +27 -0
  61. data/lib/amalgalite/taps/io.rb +74 -0
  62. data/lib/amalgalite/taps.rb +2 -0
  63. data/lib/amalgalite/trace_tap.rb +35 -0
  64. data/lib/amalgalite/type_map.rb +63 -0
  65. data/lib/amalgalite/type_maps/default_map.rb +166 -0
  66. data/lib/amalgalite/type_maps/storage_map.rb +38 -0
  67. data/lib/amalgalite/type_maps/text_map.rb +21 -0
  68. data/lib/amalgalite/version.rb +8 -0
  69. data/lib/amalgalite/view.rb +26 -0
  70. data/lib/amalgalite.rb +51 -0
  71. data/spec/aggregate_spec.rb +158 -0
  72. data/spec/amalgalite_spec.rb +4 -0
  73. data/spec/blob_spec.rb +78 -0
  74. data/spec/boolean_spec.rb +24 -0
  75. data/spec/busy_handler.rb +157 -0
  76. data/spec/data/iso-3166-country.txt +242 -0
  77. data/spec/data/iso-3166-schema.sql +22 -0
  78. data/spec/data/iso-3166-subcountry.txt +3995 -0
  79. data/spec/data/make-iso-db.sh +12 -0
  80. data/spec/database_spec.rb +505 -0
  81. data/spec/default_map_spec.rb +92 -0
  82. data/spec/function_spec.rb +78 -0
  83. data/spec/integeration_spec.rb +97 -0
  84. data/spec/iso_3166_database.rb +58 -0
  85. data/spec/json_spec.rb +24 -0
  86. data/spec/packer_spec.rb +60 -0
  87. data/spec/paths_spec.rb +28 -0
  88. data/spec/progress_handler_spec.rb +91 -0
  89. data/spec/requires_spec.rb +54 -0
  90. data/spec/rtree_spec.rb +66 -0
  91. data/spec/schema_spec.rb +131 -0
  92. data/spec/spec_helper.rb +48 -0
  93. data/spec/sqlite3/constants_spec.rb +108 -0
  94. data/spec/sqlite3/database_status_spec.rb +36 -0
  95. data/spec/sqlite3/status_spec.rb +22 -0
  96. data/spec/sqlite3/version_spec.rb +28 -0
  97. data/spec/sqlite3_spec.rb +53 -0
  98. data/spec/statement_spec.rb +168 -0
  99. data/spec/storage_map_spec.rb +38 -0
  100. data/spec/tap_spec.rb +57 -0
  101. data/spec/text_map_spec.rb +20 -0
  102. data/spec/type_map_spec.rb +14 -0
  103. data/spec/version_spec.rb +8 -0
  104. data/tasks/custom.rake +101 -0
  105. data/tasks/default.rake +244 -0
  106. data/tasks/extension.rake +28 -0
  107. data/tasks/this.rb +208 -0
  108. metadata +325 -0
@@ -0,0 +1,282 @@
1
+ /**
2
+ * Copyright (c) 2008 Jeremy Hinegardner
3
+ * All rights reserved. See LICENSE and/or COPYING for details.
4
+ *
5
+ * vim: shiftwidth=4
6
+ */
7
+
8
+ #include "amalgalite.h"
9
+ #include <stdio.h>
10
+ extern VALUE mA;
11
+ VALUE cAR;
12
+ VALUE cARB;
13
+ VALUE eARB_Error;
14
+
15
+ /*
16
+ * cleanup the datatbase and statment values if they are currently open and then
17
+ * raise the message. It is assumed that the *msg pointer passed in is NOT
18
+ * allocated via malloc() or the like. It should be a local static buffer
19
+ * that we do not have to worry about freeing.
20
+ */
21
+ void am_bootstrap_cleanup_and_raise( const char* msg, sqlite3* db, sqlite3_stmt* stmt )
22
+ {
23
+
24
+ if ( NULL != stmt ) { sqlite3_finalize( stmt ); stmt = NULL; }
25
+ if ( NULL != db ) { sqlite3_close( db ); }
26
+
27
+ rb_raise(eARB_Error, "%s", msg );
28
+ }
29
+
30
+
31
+ void am_bootstrap_from_db( sqlite3* db, VALUE args )
32
+ {
33
+ sqlite3_stmt* stmt = NULL;
34
+ int rc;
35
+ char raise_msg[BUFSIZ];
36
+ int last_row_good;
37
+
38
+ VALUE am_tbl_c = rb_const_get( cARB, rb_intern("DEFAULT_BOOTSTRAP_TABLE") );
39
+ VALUE am_pk_c = rb_const_get( cARB, rb_intern("DEFAULT_ROWID_COLUMN") );
40
+ VALUE am_fname_c = rb_const_get( cARB, rb_intern("DEFAULT_FILENAME_COLUMN") );
41
+ VALUE am_content_c = rb_const_get( cARB, rb_intern("DEFAULT_CONTENTS_COLUMN") );
42
+
43
+ char* tbl_name = NULL;
44
+ char* pk_col = NULL;
45
+ char* fname_col = NULL;
46
+ char* content_col = NULL;
47
+
48
+
49
+ char sql[BUFSIZ];
50
+ const char* sql_tail = NULL;
51
+ int sql_bytes = 0;
52
+
53
+ const unsigned char* result_text = NULL;
54
+ int result_length = 0;
55
+
56
+ VALUE require_name = Qnil; /* ruby string of the file name for use in eval */
57
+ VALUE eval_this_code = Qnil; /* ruby string of the code to eval from the db */
58
+ VALUE toplevel_binding = rb_const_get( rb_cObject, rb_intern("TOPLEVEL_BINDING") ) ;
59
+ VALUE tmp = Qnil;
60
+
61
+ ID eval_id = rb_intern("eval");
62
+
63
+
64
+
65
+ tbl_name = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "table_name" ) ) ) ) ? StringValuePtr( am_tbl_c ) : StringValuePtr( tmp );
66
+ pk_col = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "rowid_column" ) ) ) ) ? StringValuePtr( am_pk_c ) : StringValuePtr( tmp );
67
+ fname_col = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "filename_column" ) ) ) ) ? StringValuePtr( am_fname_c ) : StringValuePtr( tmp );
68
+ content_col = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "contents_column" ) ) ) ) ? StringValuePtr( am_content_c ) : StringValuePtr( tmp );
69
+
70
+
71
+ /* prepare the db query */
72
+ memset( sql, 0, BUFSIZ );
73
+ sql_bytes = snprintf( sql, BUFSIZ, "SELECT %s, %s FROM %s ORDER BY %s", fname_col, content_col, tbl_name, pk_col );
74
+ rc = sqlite3_prepare_v2( db, sql, sql_bytes, &stmt, &sql_tail ) ;
75
+ if ( SQLITE_OK != rc) {
76
+ memset( raise_msg, 0, BUFSIZ );
77
+ snprintf( raise_msg, BUFSIZ,
78
+ "Failure to prepare bootload select statement table = '%s', rowid col = '%s', filename col ='%s', contents col = '%s' : [SQLITE_ERROR %d] %s\n",
79
+ tbl_name, pk_col, fname_col, content_col, rc, sqlite3_errmsg( db ));
80
+ am_bootstrap_cleanup_and_raise( raise_msg, db, stmt );
81
+ }
82
+
83
+ /* loop over the resulting rows, eval'ing and loading $LOADED_FEATURES */
84
+ last_row_good = -1;
85
+ while ( SQLITE_ROW == ( rc = sqlite3_step( stmt ) ) ) {
86
+ /* file name */
87
+ result_text = sqlite3_column_text( stmt, 0 );
88
+ result_length = sqlite3_column_bytes( stmt, 0 );
89
+ require_name = rb_str_new( (const char*)result_text, result_length );
90
+
91
+ /* ruby code */
92
+ result_text = sqlite3_column_text( stmt, 1 );
93
+ result_length = sqlite3_column_bytes( stmt, 1 );
94
+ eval_this_code = rb_str_new( (const char*)result_text, result_length );
95
+
96
+ /* Kernel.eval( code, TOPLEVEL_BINDING, filename, 1 ) */
97
+ rb_funcall(rb_mKernel, eval_id, 4, eval_this_code, toplevel_binding, require_name, INT2FIX(1) );
98
+
99
+ /* TODO: for ruby 1.9 -- put in ? sqlite3://path/to/database?tablename=tbl_name#require_name */
100
+ /* update $LOADED_FEATURES */
101
+ rb_ary_push( rb_gv_get( "$LOADED_FEATURES" ), require_name );
102
+ }
103
+
104
+ /* if there was some sqlite error in the processing of the rows */
105
+ if ( SQLITE_DONE != rc ) {
106
+ memset( raise_msg, 0, BUFSIZ );
107
+ snprintf( raise_msg, BUFSIZ, "Failure in bootloading, last successfully loaded rowid was %d : [SQLITE_ERROR %d] %s\n",
108
+ last_row_good, rc, sqlite3_errmsg( db ) );
109
+ am_bootstrap_cleanup_and_raise( raise_msg, db, stmt );
110
+ }
111
+
112
+ /* finalize the statement */
113
+ rc = sqlite3_finalize( stmt );
114
+ if ( SQLITE_OK != rc ) {
115
+ memset( raise_msg, 0, BUFSIZ );
116
+ snprintf( raise_msg, BUFSIZ, "Failure to finalize bootload statement : [SQLITE_ERROR %d] %s\n", rc, sqlite3_errmsg( db ) );
117
+ am_bootstrap_cleanup_and_raise( raise_msg, db, stmt );
118
+ }
119
+
120
+ }
121
+
122
+
123
+ /**
124
+ * call-seq:
125
+ * Amalgalite::Requires::Bootstrap.lift( 'dbfile' => "lib.db", 'table_name' => "bootload", 'rowid_column' => "id", 'filename_column' => "filename", 'content_column' => "contents" )
126
+ *
127
+ * *WARNING* *WARNING* *WARNING* *WARNING* *WARNING* *WARNING* *WARNING*
128
+ *
129
+ * This is a boostrap mechanism to eval all the code in a particular column in a
130
+ * specially formatted table in an sqlite database. It should only be used for
131
+ * a specific purpose, mainly loading the Amalgalite ruby code directly from an
132
+ * sqlite table.
133
+ *
134
+ * Amalgalite::Requires adds in the ability to _require_ code that is in an
135
+ * sqlite database. Since Amalgalite::Requires is itself ruby code, if
136
+ * Amalgalite::Requires was in an sqlite database, it could not _require_
137
+ * itself. Therefore this method is made available. It is a pure C extension
138
+ * method that directly calls the sqlite3 C functions directly and uses the ruby
139
+ * C api to eval the data in the table.
140
+ *
141
+ * This method attaches to an sqlite3 database (filename) and then does:
142
+ *
143
+ * SELECT filename_column_name, content_column_name
144
+ * FROM table_name
145
+ * ORDER BY rowid_column_name
146
+ *
147
+ * For each row returned it does an _eval_ on the code in the
148
+ * *content_column_name* and then updates _$LOADED_FEATURES_ directly with the value from
149
+ * *filename_column_name*.
150
+ *
151
+ * The database to be opened by _lift_ *must* be an sqlite3 UTF-8 database.
152
+ *
153
+ */
154
+ VALUE am_bootstrap_lift( VALUE self, VALUE args )
155
+ {
156
+ sqlite3 *db = NULL;
157
+ int rc;
158
+ char raise_msg[BUFSIZ];
159
+ VALUE tmp = Qnil;
160
+
161
+ VALUE am_db_c = rb_const_get( cARB, rb_intern("DEFAULT_DB") );
162
+
163
+ char *dbfile = NULL;
164
+
165
+
166
+ if ( Qnil == args ) {
167
+ args = rb_hash_new();
168
+ } else {
169
+ args = rb_ary_shift( args );
170
+ }
171
+
172
+ Check_Type( args, T_HASH );
173
+
174
+ /* get the arguments */
175
+ dbfile = ( Qnil == (tmp = rb_hash_aref( args, rb_str_new2( "dbfile" ) ) ) ) ? StringValuePtr( am_db_c ) : StringValuePtr( tmp );
176
+
177
+ /* open the database */
178
+ rc = sqlite3_open_v2( dbfile , &db, SQLITE_OPEN_READONLY, NULL);
179
+ if ( SQLITE_OK != rc ) {
180
+ memset( raise_msg, 0, BUFSIZ );
181
+ snprintf(raise_msg, BUFSIZ, "Failure to open database %s for bootload: [SQLITE_ERROR %d] : %s", dbfile, rc, sqlite3_errmsg( db ) );
182
+ am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
183
+ }
184
+
185
+ am_bootstrap_from_db( db, args );
186
+
187
+ /* close the database */
188
+ rc = sqlite3_close( db );
189
+ if ( SQLITE_OK != rc ) {
190
+ memset( raise_msg, 0, BUFSIZ );
191
+ snprintf( raise_msg, BUFSIZ, "Failure to close database : [SQLITE_ERROR %d] : %s\n", rc, sqlite3_errmsg( db )),
192
+ am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
193
+ }
194
+
195
+ return Qnil;
196
+ }
197
+
198
+ /**
199
+ * call-seq:
200
+ * Amalgalite::Requires::Bootstrap.lift_str( sql, 'table_name' => "bootload", 'rowid_column' => "id", 'filename_column' => "filename", 'content_column' => "contents" )
201
+ *
202
+ * Bootstrap Amalgalite from a string containing an SQL dump. See Amalgalite::Requires::Bootstrap.lift.
203
+ *
204
+ * For example:
205
+ *
206
+ * Amalgalite::Requires::Bootstrap.lifts(File.read("lib.sql"))
207
+ */
208
+ VALUE am_bootstrap_lift_str( VALUE self, VALUE args )
209
+ {
210
+ sqlite3 *db = NULL;
211
+ int rc;
212
+ char raise_msg[BUFSIZ];
213
+ VALUE sql = Qnil;
214
+ VALUE args_hsh = Qnil;
215
+
216
+ sql = rb_ary_shift(args);
217
+ StringValue(sql);
218
+ if ( Qnil == sql ) { rb_raise(eARB_Error, "SQL required." ); }
219
+
220
+ args_hsh = rb_ary_shift(args);
221
+ if ( Qnil == args_hsh ) { args_hsh = rb_hash_new(); }
222
+
223
+
224
+ rc = sqlite3_open_v2( ":memory:", &db, SQLITE_OPEN_READWRITE, NULL );
225
+ if ( SQLITE_OK != rc ) {
226
+ memset( raise_msg, 0, BUFSIZ );
227
+ snprintf(raise_msg, BUFSIZ, "Failure to open database :memory: for bootload: [SQLITE_ERROR %d] : %s", rc, sqlite3_errmsg( db ) );
228
+ am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
229
+ }
230
+
231
+ /* Load the bootstrap SQL into the database */
232
+ rc = sqlite3_exec( db, StringValuePtr( sql ), NULL, NULL, NULL );
233
+
234
+ if ( SQLITE_OK != rc ) {
235
+ memset( raise_msg, 0, BUFSIZ );
236
+ snprintf(raise_msg, BUFSIZ, "Failure to import bootload sql: [SQLITE_ERROR %d] : %s", rc, sqlite3_errmsg( db ) );
237
+ am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
238
+ }
239
+
240
+ am_bootstrap_from_db( db, args_hsh );
241
+
242
+ rc = sqlite3_close( db );
243
+
244
+ if ( SQLITE_OK != rc ) {
245
+ memset( raise_msg, 0, BUFSIZ );
246
+ snprintf( raise_msg, BUFSIZ, "Failure to close database : [SQLITE_ERROR %d] : %s\n", rc, sqlite3_errmsg( db )),
247
+ am_bootstrap_cleanup_and_raise( raise_msg, db, NULL );
248
+ }
249
+
250
+ return Qnil;
251
+
252
+ }
253
+
254
+
255
+ /**
256
+ * Bootstrapping module to help _require_ when Amalgalite::Requires is not
257
+ * availble in files.
258
+ */
259
+ void Init_amalgalite_requires_bootstrap()
260
+ {
261
+
262
+ mA = rb_define_module("Amalgalite");
263
+ cAR = rb_define_class_under(mA, "Requires", rb_cObject);
264
+ cARB = rb_define_class_under(cAR, "Bootstrap", rb_cObject);
265
+
266
+ eARB_Error = rb_define_class_under(cARB, "Error", rb_eStandardError);
267
+
268
+ rb_define_module_function(cARB, "lift", am_bootstrap_lift, -2);
269
+ rb_define_module_function(cARB, "lifts", am_bootstrap_lift, -2);
270
+
271
+ /* constants for default db, table, column, rowid, contents */
272
+ rb_define_const(cARB, "DEFAULT_DB", rb_str_new2( "lib.db" ));
273
+ rb_define_const(cARB, "DEFAULT_TABLE", rb_str_new2( "rubylibs" ));
274
+ rb_define_const(cARB, "DEFAULT_BOOTSTRAP_TABLE", rb_str_new2( "bootstrap" ));
275
+ rb_define_const(cARB, "DEFAULT_ROWID_COLUMN", rb_str_new2( "id" ));
276
+ rb_define_const(cARB, "DEFAULT_FILENAME_COLUMN", rb_str_new2( "filename" ));
277
+ rb_define_const(cARB, "DEFAULT_CONTENTS_COLUMN", rb_str_new2( "contents" ));
278
+ rb_define_const(cARB, "DEFAULT_COMPRESSED_COLUMN", rb_str_new2( "compressed" ));
279
+
280
+ return;
281
+ }
282
+