File: sqlite3.c

package info (click to toggle)
ruby-sqlite3 2.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 684 kB
  • sloc: ruby: 4,827; ansic: 1,868; sh: 91; makefile: 7
file content (225 lines) | stat: -rw-r--r-- 7,657 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <sqlite3_ruby.h>

VALUE mSqlite3;
VALUE cSqlite3Blob;

int
bignum_to_int64(VALUE value, sqlite3_int64 *result)
{
#ifdef HAVE_RB_INTEGER_PACK
    const int nails = 0;
    int t = rb_integer_pack(value, result, 1, sizeof(*result), nails,
                            INTEGER_PACK_NATIVE_BYTE_ORDER |
                            INTEGER_PACK_2COMP);
    switch (t) {
        case -2:
        case +2:
            return 0;
        case +1:
            if (!nails) {
                if (*result < 0) { return 0; }
            }
            break;
        case -1:
            if (!nails) {
                if (*result >= 0) { return 0; }
            } else {
                *result += INT64_MIN;
            }
            break;
    }
    return 1;
#else
# ifndef RBIGNUM_LEN
#   define RBIGNUM_LEN(x) RBIGNUM(x)->len
# endif
    const long len = RBIGNUM_LEN(value);
    if (len == 0) {
        *result = 0;
        return 1;
    }
    if (len > 63 / (SIZEOF_BDIGITS * CHAR_BIT) + 1) { return 0; }
    if (len == 63 / (SIZEOF_BDIGITS * CHAR_BIT) + 1) {
        const BDIGIT *digits = RBIGNUM_DIGITS(value);
        BDIGIT blast = digits[len - 1];
        BDIGIT bmax = (BDIGIT)1UL << (63 % (CHAR_BIT * SIZEOF_BDIGITS));
        if (blast > bmax) { return 0; }
        if (blast == bmax) {
            if (RBIGNUM_POSITIVE_P(value)) {
                return 0;
            } else {
                long i = len - 1;
                while (i) {
                    if (digits[--i]) { return 0; }
                }
            }
        }
    }
    *result = (sqlite3_int64)NUM2LL(value);
    return 1;
#endif
}

static VALUE
libversion(VALUE UNUSED(klass))
{
    return INT2NUM(sqlite3_libversion_number());
}

static VALUE
using_sqlcipher(VALUE UNUSED(klass))
{
#ifdef USING_SQLCIPHER
    return Qtrue;
#else
    return Qfalse;
#endif
}

/* Returns the compile time setting of the SQLITE_THREADSAFE flag.
 * See: https://www.sqlite.org/c3ref/threadsafe.html
 */
static VALUE
threadsafe_p(VALUE UNUSED(klass))
{
    return INT2NUM(sqlite3_threadsafe());
}

/*
 * call-seq:
 *   status(parameter) → Hash
 *   status(parameter, reset_flag = false) → Hash
 *
 * Queries the SQLite3 library for run-time status information. Passing a truthy +reset_flag+ will
 * reset the highwater mark to the current value.
 *
 * [Parameters]
 * - +parameter+ (Integer, SQLite3::Constants::Status): The status parameter to query.
 * - +reset_flag+ (Boolean): Whether to reset the highwater mark. (default is +false+)
 *
 * [Returns]
 * A Hash containing +:current+ and +:highwater+ keys for integer values.
 */
static VALUE
rb_sqlite3_status(int argc, VALUE *argv, VALUE klass)
{
    VALUE opArg, resetFlagArg;

    rb_scan_args(argc, argv, "11", &opArg, &resetFlagArg);

    int op = NUM2INT(opArg);
    bool resetFlag = RTEST(resetFlagArg);

    int pCurrent = 0;
    int pHighwater = 0;
    sqlite3_status(op, &pCurrent, &pHighwater, resetFlag);

    VALUE hash = rb_hash_new();
    rb_hash_aset(hash, ID2SYM(rb_intern("current")), INT2FIX(pCurrent));
    rb_hash_aset(hash, ID2SYM(rb_intern("highwater")), INT2FIX(pHighwater));

    return hash;
}

void
init_sqlite3_constants(void)
{
    VALUE mSqlite3Constants;
    VALUE mSqlite3Open;

    mSqlite3Constants = rb_define_module_under(mSqlite3, "Constants");

    /* sqlite3_open_v2 flags for Database::new */
    mSqlite3Open = rb_define_module_under(mSqlite3Constants, "Open");

    /* symbols = IO.readlines('sqlite3.h').map { |n| /\A#define\s+(SQLITE_OPEN_\w+)\s/ =~ n && $1 }.compact
     * pad = symbols.map(&:length).max - 9
     * symbols.each { |s| printf %Q{  rb_define_const(mSqlite3Open, %-#{pad}s INT2FIX(#{s}));\n}, '"' + s[12..-1] + '",' }
     */
    rb_define_const(mSqlite3Open, "READONLY",       INT2FIX(SQLITE_OPEN_READONLY));
    rb_define_const(mSqlite3Open, "READWRITE",      INT2FIX(SQLITE_OPEN_READWRITE));
    rb_define_const(mSqlite3Open, "CREATE",         INT2FIX(SQLITE_OPEN_CREATE));
    rb_define_const(mSqlite3Open, "DELETEONCLOSE",  INT2FIX(SQLITE_OPEN_DELETEONCLOSE));
    rb_define_const(mSqlite3Open, "EXCLUSIVE",      INT2FIX(SQLITE_OPEN_EXCLUSIVE));
    rb_define_const(mSqlite3Open, "MAIN_DB",        INT2FIX(SQLITE_OPEN_MAIN_DB));
    rb_define_const(mSqlite3Open, "TEMP_DB",        INT2FIX(SQLITE_OPEN_TEMP_DB));
    rb_define_const(mSqlite3Open, "TRANSIENT_DB",   INT2FIX(SQLITE_OPEN_TRANSIENT_DB));
    rb_define_const(mSqlite3Open, "MAIN_JOURNAL",   INT2FIX(SQLITE_OPEN_MAIN_JOURNAL));
    rb_define_const(mSqlite3Open, "TEMP_JOURNAL",   INT2FIX(SQLITE_OPEN_TEMP_JOURNAL));
    rb_define_const(mSqlite3Open, "SUBJOURNAL",     INT2FIX(SQLITE_OPEN_SUBJOURNAL));
    rb_define_const(mSqlite3Open, "MASTER_JOURNAL",
                    INT2FIX(SQLITE_OPEN_MASTER_JOURNAL)); /* pre-3.33.0 */
    rb_define_const(mSqlite3Open, "SUPER_JOURNAL",  INT2FIX(SQLITE_OPEN_MASTER_JOURNAL));
    rb_define_const(mSqlite3Open, "NOMUTEX",        INT2FIX(SQLITE_OPEN_NOMUTEX));
    rb_define_const(mSqlite3Open, "FULLMUTEX",      INT2FIX(SQLITE_OPEN_FULLMUTEX));
#ifdef SQLITE_OPEN_AUTOPROXY
    /* SQLITE_VERSION_NUMBER>=3007002 */
    rb_define_const(mSqlite3Open, "AUTOPROXY",      INT2FIX(SQLITE_OPEN_AUTOPROXY));
    rb_define_const(mSqlite3Open, "SHAREDCACHE",    INT2FIX(SQLITE_OPEN_SHAREDCACHE));
    rb_define_const(mSqlite3Open, "PRIVATECACHE",   INT2FIX(SQLITE_OPEN_PRIVATECACHE));
    rb_define_const(mSqlite3Open, "WAL",            INT2FIX(SQLITE_OPEN_WAL));
#endif
#ifdef SQLITE_OPEN_URI
    /* SQLITE_VERSION_NUMBER>=3007007 */
    rb_define_const(mSqlite3Open, "URI",            INT2FIX(SQLITE_OPEN_URI));
#endif
#ifdef SQLITE_OPEN_MEMORY
    /* SQLITE_VERSION_NUMBER>=3007013 */
    rb_define_const(mSqlite3Open, "MEMORY",         INT2FIX(SQLITE_OPEN_MEMORY));
#endif
}

RUBY_FUNC_EXPORTED
void
Init_sqlite3_native(void)
{
    /*
     * SQLite3 is a wrapper around the popular database
     * sqlite[http://sqlite.org].
     *
     * For an example of usage, see SQLite3::Database.
     */
    mSqlite3     = rb_define_module("SQLite3");

    /* A class for differentiating between strings and blobs, when binding them
     * into statements.
     */
    cSqlite3Blob = rb_define_class_under(mSqlite3, "Blob", rb_cString);

    /* Initialize the sqlite3 library */
#ifdef HAVE_SQLITE3_INITIALIZE
    sqlite3_initialize();
#endif

    init_sqlite3_constants();
    init_sqlite3_database();
    init_sqlite3_statement();
#ifdef HAVE_SQLITE3_BACKUP_INIT
    init_sqlite3_backup();
#endif
    rb_define_singleton_method(mSqlite3, "sqlcipher?", using_sqlcipher, 0);
    rb_define_singleton_method(mSqlite3, "libversion", libversion, 0);
    rb_define_singleton_method(mSqlite3, "threadsafe", threadsafe_p, 0);
    rb_define_singleton_method(mSqlite3, "status", rb_sqlite3_status, -1);

    /* (String) The version of the sqlite3 library compiled with (e.g., "3.46.1") */
    rb_define_const(mSqlite3, "SQLITE_VERSION", rb_str_new2(SQLITE_VERSION));

    /* (Integer) The version of the sqlite3 library compiled with (e.g., 346001) */
    rb_define_const(mSqlite3, "SQLITE_VERSION_NUMBER", INT2FIX(SQLITE_VERSION_NUMBER));

    /* (String) The version of the sqlite3 library loaded at runtime (e.g., "3.46.1") */
    rb_define_const(mSqlite3, "SQLITE_LOADED_VERSION", rb_str_new2(sqlite3_libversion()));

#ifdef USING_PACKAGED_LIBRARIES
    rb_define_const(mSqlite3, "SQLITE_PACKAGED_LIBRARIES", Qtrue);
#else
    rb_define_const(mSqlite3, "SQLITE_PACKAGED_LIBRARIES", Qfalse);
#endif

#ifdef USING_PRECOMPILED_LIBRARIES
    rb_define_const(mSqlite3, "SQLITE_PRECOMPILED_LIBRARIES", Qtrue);
#else
    rb_define_const(mSqlite3, "SQLITE_PRECOMPILED_LIBRARIES", Qfalse);
#endif
}