do_sqlite3 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. data/LICENSE +20 -0
  2. data/README +4 -0
  3. data/Rakefile +51 -0
  4. data/TODO +5 -0
  5. data/ext/extconf.rb +9 -0
  6. data/ext/sqlite3_c.c +4725 -0
  7. data/ext/sqlite_c.i +168 -0
  8. data/lib/do_sqlite3.rb +165 -0
  9. metadata +63 -0
@@ -0,0 +1,168 @@
1
+ %module sqlite3_c
2
+ %include "typemaps.i"
3
+ %{
4
+ #include <sqlite3.h>
5
+ typedef void BLOB;
6
+ typedef void VALBLOB;
7
+ %}
8
+
9
+ %typemap(in,numinputs=0) sqlite3 **OUTPUT(sqlite3 *) {
10
+ $1 = (sqlite3**)malloc( sizeof( sqlite3* ) );
11
+ };
12
+ %typemap(argout,fragment="output_helper") sqlite3 **OUTPUT (sqlite3 *) {
13
+ $result = output_helper($result, SWIG_NewPointerObj( *$1, SWIGTYPE_p_sqlite3, 0 ));
14
+ }
15
+ %typemap(freearg) sqlite3 ** {
16
+ free((sqlite3 *)$1);
17
+ }
18
+
19
+ %typemap(in,numinputs=0) sqlite3_stmt **OUTPUT(sqlite3_stmt *) {
20
+ $1 = (sqlite3_stmt**)malloc( sizeof( sqlite3_stmt* ) );
21
+ };
22
+ %typemap(argout,fragment="output_helper") sqlite3_stmt **OUTPUT (sqlite3_stmt *) {
23
+ $result = output_helper($result, SWIG_NewPointerObj( *$1, SWIGTYPE_p_sqlite3_stmt, 0 ));
24
+ }
25
+ %typemap(freearg) sqlite3_stmt ** {
26
+ free((sqlite3_stmt *)$1);
27
+ }
28
+
29
+ %typemap(in,numinputs=0) char **OUTPUT(char *) {
30
+ $1 = (char**)malloc( sizeof( char* ) );
31
+ };
32
+ %typemap(argout,fragment="output_helper") char **OUTPUT (char *) {
33
+ $result = output_helper($result, rb_str_new2(*$1));
34
+ }
35
+ %typemap(freearg) char ** {
36
+ free((char *)$1);
37
+ }
38
+
39
+ %typemap(out) sqlite_int64 {
40
+ $result = LONG2NUM(result);
41
+ }
42
+
43
+ const char *sqlite3_libversion(void);
44
+ int sqlite3_close(sqlite3*);
45
+
46
+ sqlite_int64 sqlite3_last_insert_rowid(sqlite3*);
47
+
48
+ int sqlite3_changes(sqlite3*);
49
+ int sqlite3_total_changes(sqlite3*);
50
+ void sqlite3_interrupt(sqlite3*);
51
+
52
+ int sqlite3_complete(const char*);
53
+ int sqlite3_complete16(const void *str);
54
+
55
+ int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
56
+ int sqlite3_busy_timeout(sqlite3*,int);
57
+ int sqlite3_set_authorizer(sqlite3*, int(*)(void*,int,const char*,const char*,const char*,const char*), void*);
58
+ int sqlite3_trace(sqlite3*, void(*)(void*,const char*), void*);
59
+
60
+ int sqlite3_open(const char *filename, sqlite3 **OUTPUT);
61
+ int sqlite3_open16(const void *filename, sqlite3 **);
62
+
63
+ int sqlite3_errcode(sqlite3*);
64
+ const char *sqlite3_errmsg(sqlite3*);
65
+ const void *sqlite3_errmsg16(sqlite3*);
66
+
67
+ int sqlite3_prepare(sqlite3*,const char* sql,int,sqlite3_stmt**OUTPUT,const char**OUTPUT);
68
+ int sqlite3_prepare_v2(sqlite3*,const char* sql,int,sqlite3_stmt**OUTPUT,const char**OUTPUT);
69
+ int sqlite3_prepare16(sqlite3*,const void* sql,int,sqlite3_stmt**,const void**);
70
+
71
+ int sqlite3_bind_blob(sqlite3_stmt*,int,const void *blob,int,void(*free)(void*));
72
+ int sqlite3_bind_double(sqlite3_stmt*,int,double);
73
+ int sqlite3_bind_int(sqlite3_stmt*,int,int);
74
+ int sqlite3_bind_int64(sqlite3_stmt*,int,sqlite_int64);
75
+ int sqlite3_bind_null(sqlite3_stmt*,int);
76
+ int sqlite3_bind_text(sqlite3_stmt*,int,const char*text,int,void(*free)(void*));
77
+ int sqlite3_bind_text16(sqlite3_stmt*,int,const void*utf16,int,void(*free)(void*));
78
+
79
+ int sqlite3_bind_parameter_count(sqlite3_stmt*);
80
+ const char *sqlite3_bind_parameter_name(sqlite3_stmt*,int);
81
+ int sqlite3_bind_parameter_index(sqlite3_stmt*,const char*);
82
+
83
+ int sqlite3_column_count(sqlite3_stmt*);
84
+ const char *sqlite3_column_name(sqlite3_stmt*,int);
85
+ const void *sqlite3_column_name16(sqlite3_stmt*,int);
86
+ const char *sqlite3_column_decltype(sqlite3_stmt*,int);
87
+ const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
88
+
89
+ int sqlite3_step(sqlite3_stmt*);
90
+
91
+ int sqlite3_data_count(sqlite3_stmt*);
92
+
93
+ const BLOB *sqlite3_column_blob(sqlite3_stmt*,int);
94
+ int sqlite3_column_bytes(sqlite3_stmt*,int);
95
+ int sqlite3_column_bytes16(sqlite3_stmt*,int);
96
+ double sqlite3_column_double(sqlite3_stmt*,int);
97
+ double sqlite3_column_int(sqlite3_stmt*,int);
98
+ sqlite_int64 sqlite3_column_int64(sqlite3_stmt*,int);
99
+ const char *sqlite3_column_text(sqlite3_stmt*,int);
100
+ const void *sqlite3_column_text16(sqlite3_stmt*,int);
101
+ int sqlite3_column_type(sqlite3_stmt*,int);
102
+
103
+ int sqlite3_finalize(sqlite3_stmt*);
104
+ int sqlite3_reset(sqlite3_stmt*);
105
+
106
+ int sqlite3_create_function(sqlite3*,const char*str,int,int,void*,void(*func)(sqlite3_context*,int,sqlite3_value**),void(*step)(sqlite3_context*,int,sqlite3_value**),void(*final)(sqlite3_context*));
107
+
108
+ int sqlite3_create_function16(sqlite3*,const void*str,int,int,void*,void(*func)(sqlite3_context*,int,sqlite3_value**),void(*step)(sqlite3_context*,int,sqlite3_value**),void(*final)(sqlite3_context*));
109
+
110
+ int sqlite3_aggregate_count(sqlite3_context*);
111
+
112
+ const VALBLOB *sqlite3_value_blob(sqlite3_value*);
113
+ int sqlite3_value_bytes(sqlite3_value*);
114
+ int sqlite3_value_bytes16(sqlite3_value*);
115
+ double sqlite3_value_double(sqlite3_value*);
116
+ int sqlite3_value_int(sqlite3_value*);
117
+ sqlite_int64 sqlite3_value_int64(sqlite3_value*);
118
+ const char *sqlite3_value_text(sqlite3_value*);
119
+ const void *sqlite3_value_text16(sqlite3_value*);
120
+ const void *sqlite3_value_text16le(sqlite3_value*);
121
+ const void *sqlite3_value_text16be(sqlite3_value*);
122
+ int sqlite3_value_type(sqlite3_value*);
123
+
124
+ void sqlite3_result_blob(sqlite3_context*,const void *blob,int,void(*free)(void*));
125
+ void sqlite3_result_double(sqlite3_context*,double);
126
+ void sqlite3_result_error(sqlite3_context*,const char *text,int);
127
+ void sqlite3_result_error16(sqlite3_context*,const void *blob,int);
128
+ void sqlite3_result_int(sqlite3_context*,int);
129
+ void sqlite3_result_int64(sqlite3_context*,sqlite_int64);
130
+ void sqlite3_result_text(sqlite3_context*,const char* text,int,void(*free)(void*));
131
+ void sqlite3_result_text16(sqlite3_context*,const void* utf16,int,void(*free)(void*));
132
+ void sqlite3_result_text16le(sqlite3_context*,const void* utf16,int,void(*free)(void*));
133
+ void sqlite3_result_text16be(sqlite3_context*,const void* utf16,int,void(*free)(void*));
134
+ void sqlite3_result_value(sqlite3_context*,sqlite3_value*);
135
+
136
+ VALUE *sqlite3_aggregate_context(sqlite3_context*,int data_size);
137
+
138
+ #define SQLITE_OK 0 /* Successful result */
139
+ /* beginning-of-error-codes */
140
+ #define SQLITE_ERROR 1 /* SQL error or missing database */
141
+ #define SQLITE_INTERNAL 2 /* NOT USED. Internal logic error in SQLite */
142
+ #define SQLITE_PERM 3 /* Access permission denied */
143
+ #define SQLITE_ABORT 4 /* Callback routine requested an abort */
144
+ #define SQLITE_BUSY 5 /* The database file is locked */
145
+ #define SQLITE_LOCKED 6 /* A table in the database is locked */
146
+ #define SQLITE_NOMEM 7 /* A malloc() failed */
147
+ #define SQLITE_READONLY 8 /* Attempt to write a readonly database */
148
+ #define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/
149
+ #define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */
150
+ #define SQLITE_CORRUPT 11 /* The database disk image is malformed */
151
+ #define SQLITE_NOTFOUND 12 /* NOT USED. Table or record not found */
152
+ #define SQLITE_FULL 13 /* Insertion failed because database is full */
153
+ #define SQLITE_CANTOPEN 14 /* Unable to open the database file */
154
+ #define SQLITE_PROTOCOL 15 /* NOT USED. Database lock protocol error */
155
+ #define SQLITE_EMPTY 16 /* Database is empty */
156
+ #define SQLITE_SCHEMA 17 /* The database schema changed */
157
+ #define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */
158
+ #define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */
159
+ #define SQLITE_MISMATCH 20 /* Data type mismatch */
160
+ #define SQLITE_MISUSE 21 /* Library used incorrectly */
161
+ #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
162
+ #define SQLITE_AUTH 23 /* Authorization denied */
163
+ #define SQLITE_FORMAT 24 /* Auxiliary database format error */
164
+ #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
165
+ #define SQLITE_NOTADB 26 /* File opened that is not a database file */
166
+ #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
167
+ #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
168
+ /* end-of-error-codes */
@@ -0,0 +1,165 @@
1
+ require 'sqlite3_c'
2
+ require 'data_objects'
3
+
4
+ module DataObject
5
+ module Sqlite3
6
+
7
+ QUOTE_STRING = "\""
8
+ QUOTE_COLUMN = "'"
9
+
10
+ class Connection < DataObject::Connection
11
+
12
+ attr_reader :db
13
+
14
+ def initialize(connection_string)
15
+ @state = STATE_CLOSED
16
+ @connection_string = connection_string
17
+ @conn = Hash[*connection_string.split(" ").map {|x| x.split("=")}.flatten]["dbname"]
18
+ end
19
+
20
+ def open
21
+ r, d = Sqlite3_c.sqlite3_open(@conn)
22
+ unless r == Sqlite3_c::SQLITE_OK
23
+ raise ConnectionFailed, "The connection with connection string #{@connection_string} failed\n#{Sqlite3_c.sqlite3_errmsg(d)}"
24
+ else
25
+ @db = d
26
+ end
27
+ @state = STATE_OPEN
28
+ true
29
+ end
30
+
31
+ def close
32
+ Sqlite3_c.sqlite3_close(@db)
33
+ @state = STATE_CLOSED
34
+ true
35
+ end
36
+
37
+ def create_command(text)
38
+ Command.new(self, text)
39
+ end
40
+
41
+ end
42
+
43
+ class Reader < DataObject::Reader
44
+
45
+ def initialize(db, reader)
46
+ @reader = reader
47
+ result = Sqlite3_c.sqlite3_step(reader)
48
+ rows_affected, field_count = Sqlite3_c.sqlite3_changes(db), Sqlite3_c.sqlite3_column_count(reader)
49
+ if field_count == 0
50
+ @records_affected = rows_affected
51
+ close
52
+ else
53
+ @field_count = field_count
54
+ @fields, @field_types = [], []
55
+ i = 0
56
+ while(i < @field_count)
57
+ @field_types.push(Sqlite3_c.sqlite3_column_type(reader, i))
58
+ @fields.push(Sqlite3_c.sqlite3_column_name(reader, i))
59
+ i += 1
60
+ end
61
+ case result
62
+ when Sqlite3_c::SQLITE_BUSY, Sqlite3_c::SQLITE_ERROR, Sqlite3_c::SQLITE_MISUSE
63
+ raise ReaderError, "An error occurred while trying to get the next row\n#{Sqlite3_c.sqlite3_errmsg(db)}"
64
+ else
65
+ @has_rows = result == Sqlite3_c::SQLITE_ROW
66
+ @state = STATE_OPEN
67
+ close unless @has_rows
68
+ end
69
+ end
70
+ end
71
+
72
+ def real_close
73
+ Sqlite3_c.sqlite3_finalize(@reader)
74
+ end
75
+
76
+ def name(idx)
77
+ super
78
+ @fields[idx]
79
+ end
80
+
81
+ def get_index(name)
82
+ super
83
+ @fields.index(name)
84
+ end
85
+
86
+ def null?(idx)
87
+ super
88
+ item(idx).nil?
89
+ end
90
+
91
+ def item(idx)
92
+ super
93
+ case @field_types[idx]
94
+ when 1 # SQLITE_INTEGER
95
+ Sqlite3_c.sqlite3_column_int(@reader, idx).to_i
96
+ when 2 # SQLITE_FLOAT
97
+ Sqlite3_c.sqlite3_column_double(@reader, idx)
98
+ else
99
+ Sqlite3_c.sqlite3_column_text(@reader, idx)
100
+ end
101
+ end
102
+
103
+ def each
104
+ return unless has_rows?
105
+
106
+ while(true) do
107
+ yield
108
+ break unless Sqlite3_c.sqlite3_step(@reader) == Sqlite3_c::SQLITE_ROW
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ class Command < DataObject::Command
115
+
116
+ def execute_reader(*args)
117
+ super
118
+ sql = escape_sql(args)
119
+ @connection.logger.debug { sql }
120
+ result, ptr = Sqlite3_c.sqlite3_prepare_v2(@connection.db, sql, sql.size + 1)
121
+ unless result == Sqlite3_c::SQLITE_OK
122
+ raise QueryError, "Your query failed.\n#{Sqlite3_c.sqlite3_errmsg(@connection.db)}\nQUERY: \"#{sql}\""
123
+ else
124
+ reader = Reader.new(@connection.db, ptr)
125
+
126
+ if block_given?
127
+ return_value = yield(reader)
128
+ reader.close
129
+ return_value
130
+ else
131
+ reader
132
+ end
133
+ end
134
+ end
135
+
136
+ def execute_non_query(*args)
137
+ super
138
+ sql = escape_sql(args)
139
+ @connection.logger.debug { sql }
140
+ result, reader = Sqlite3_c.sqlite3_prepare_v2(@connection.db, sql, -1)
141
+ unless result == Sqlite3_c::SQLITE_OK
142
+ Sqlite3_c.sqlite3_finalize(reader)
143
+ raise QueryError, "Your query failed.\n#{Sqlite3_c.sqlite3_errmsg(@connection.db)}\nQUERY: \"#{sql}\""
144
+ else
145
+ exec_result = Sqlite3_c.sqlite3_step(reader)
146
+ Sqlite3_c.sqlite3_finalize(reader)
147
+ if exec_result == Sqlite3_c::SQLITE_DONE
148
+ ResultData.new(@connection, Sqlite3_c.sqlite3_changes(@connection.db), Sqlite3_c.sqlite3_last_insert_rowid(@connection.db))
149
+ else
150
+ raise QueryError, "Your query failed or you tried to execute a SELECT query through execute_non_reader\n#{Sqlite3_c.sqlite3_errmsg(@connection.db)}\nQUERY: \"#{@text}\""
151
+ end
152
+ end
153
+ end
154
+
155
+ def quote_symbol(value)
156
+ value.to_s
157
+ end
158
+
159
+ def quote_boolean(value)
160
+ value ? '1' : '0'
161
+ end
162
+ end
163
+
164
+ end
165
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: do_sqlite3
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.2.0
7
+ date: 2007-11-11 00:00:00 -08:00
8
+ summary: A DataObject.rb driver for sqlite3
9
+ require_paths:
10
+ - lib
11
+ email: wycats@gmail.com
12
+ homepage: http://dataobjects.devjavu.com
13
+ rubyforge_project:
14
+ description: A DataObject.rb driver for sqlite3
15
+ autorequire: do_sqlite3
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Yehuda Katz
31
+ files:
32
+ - LICENSE
33
+ - README
34
+ - Rakefile
35
+ - TODO
36
+ - lib/do_sqlite3.rb
37
+ - ext/extconf.rb
38
+ - ext/sqlite3_c.c
39
+ - ext/sqlite_c.i
40
+ test_files: []
41
+
42
+ rdoc_options: []
43
+
44
+ extra_rdoc_files:
45
+ - README
46
+ - LICENSE
47
+ - TODO
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ requirements: []
53
+
54
+ dependencies:
55
+ - !ruby/object:Gem::Dependency
56
+ name: data_objects
57
+ version_requirement:
58
+ version_requirements: !ruby/object:Gem::Version::Requirement
59
+ requirements:
60
+ - - ">"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.0.0
63
+ version: