swift 0.14.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/API.rdoc +14 -14
- data/README.md +110 -61
- data/Rakefile +2 -5
- data/VERSION +1 -1
- data/lib/swift/adapter/mysql.rb +30 -0
- data/lib/swift/adapter/postgres.rb +27 -0
- data/lib/swift/adapter/sql.rb +23 -29
- data/lib/swift/adapter/sqlite3.rb +59 -0
- data/lib/swift/adapter.rb +129 -70
- data/lib/swift/attribute.rb +19 -8
- data/lib/swift/eventmachine.rb +49 -0
- data/lib/swift/identity_map.rb +7 -7
- data/lib/swift/migrations.rb +12 -12
- data/lib/swift/{scheme.rb → record.rb} +16 -17
- data/lib/swift/result.rb +24 -0
- data/lib/swift/statement.rb +25 -0
- data/lib/swift/synchrony.rb +38 -0
- data/lib/swift/validations.rb +2 -2
- data/lib/swift.rb +8 -6
- data/swift.gemspec +19 -31
- data/test/helper.rb +11 -6
- data/test/test_adapter.rb +11 -25
- data/test/test_async.rb +9 -12
- data/test/test_encoding.rb +2 -2
- data/test/test_error.rb +8 -8
- data/test/test_io.rb +2 -2
- data/test/{test_scheme.rb → test_record.rb} +6 -6
- data/test/test_swift.rb +9 -51
- data/test/test_timestamps.rb +1 -1
- data/test/test_transactions.rb +2 -2
- data/test/test_types.rb +3 -3
- data/test/test_validations.rb +2 -2
- metadata +20 -27
- data/ext/adapter.cc +0 -479
- data/ext/adapter.h +0 -13
- data/ext/adapter_io.cc +0 -62
- data/ext/adapter_io.h +0 -24
- data/ext/attribute.cc +0 -22
- data/ext/attribute.h +0 -8
- data/ext/datetime.cc +0 -96
- data/ext/datetime.h +0 -12
- data/ext/extconf.rb +0 -61
- data/ext/query.cc +0 -104
- data/ext/query.h +0 -20
- data/ext/result.cc +0 -229
- data/ext/result.h +0 -27
- data/ext/statement.cc +0 -116
- data/ext/statement.h +0 -22
- data/ext/swift.cc +0 -114
- data/ext/swift.h +0 -60
- data/lib/swift/db.rb +0 -89
data/ext/swift.cc
DELETED
@@ -1,114 +0,0 @@
|
|
1
|
-
#include "swift.h"
|
2
|
-
|
3
|
-
static VALUE mSwift;
|
4
|
-
|
5
|
-
VALUE eSwiftError;
|
6
|
-
VALUE eSwiftArgumentError;
|
7
|
-
VALUE eSwiftRuntimeError;
|
8
|
-
VALUE eSwiftConnectionError;
|
9
|
-
|
10
|
-
/*
|
11
|
-
Initialize Swift with a non standard dbic++ path.
|
12
|
-
|
13
|
-
@note
|
14
|
-
By default Swift looks in '/usr/lib/dbic++/'. Not normally required unless you install dbic++ somewhere funny.
|
15
|
-
|
16
|
-
@overload init(path)
|
17
|
-
@param [path] path Non standard dbic++ path.
|
18
|
-
*/
|
19
|
-
VALUE swift_init(VALUE self, VALUE path) {
|
20
|
-
try { dbi::dbiInitialize(CSTRING(path)); } CATCH_DBI_EXCEPTIONS();
|
21
|
-
return Qtrue;
|
22
|
-
}
|
23
|
-
|
24
|
-
/*
|
25
|
-
Trace statement execution.
|
26
|
-
|
27
|
-
@example Toggle tracing.
|
28
|
-
Swift.trace true
|
29
|
-
Swift.db.execute 'select * from users'
|
30
|
-
Swift.trace false
|
31
|
-
@example Block form.
|
32
|
-
Swift.trace true do
|
33
|
-
Swift.db.execute 'select * from users'
|
34
|
-
end
|
35
|
-
|
36
|
-
@overload trace(show = true, output = $stderr)
|
37
|
-
@param [true, false] show Optional trace toggle boolean.
|
38
|
-
@param [IO] output Optional output. Defaults to stderr.
|
39
|
-
*/
|
40
|
-
VALUE swift_trace(int argc, VALUE *argv, VALUE self) {
|
41
|
-
VALUE flag, io;
|
42
|
-
rb_io_t *fptr;
|
43
|
-
int status, fd = 2; // defaults to stderr
|
44
|
-
|
45
|
-
rb_scan_args(argc, argv, "02", &flag, &io);
|
46
|
-
|
47
|
-
if (NIL_P(flag))
|
48
|
-
flag = Qtrue;
|
49
|
-
|
50
|
-
if (TYPE(flag) != T_TRUE && TYPE(flag) != T_FALSE)
|
51
|
-
rb_raise(eSwiftArgumentError, "Swift#trace expects a boolean flag, got %s", CSTRING(flag));
|
52
|
-
|
53
|
-
if (!NIL_P(io)) {
|
54
|
-
GetOpenFile(rb_convert_type(io, T_FILE, "IO", "to_io"), fptr);
|
55
|
-
fd = fptr->fd;
|
56
|
-
}
|
57
|
-
|
58
|
-
// block form trace
|
59
|
-
if (rb_block_given_p()) {
|
60
|
-
// orig values
|
61
|
-
bool orig_trace = dbi::_trace;
|
62
|
-
int orig_trace_fd = dbi::_trace_fd;
|
63
|
-
|
64
|
-
dbi::trace(flag == Qtrue ? true : false, fd);
|
65
|
-
VALUE block_result = rb_protect(rb_yield, Qnil, &status);
|
66
|
-
dbi::trace(orig_trace, orig_trace_fd);
|
67
|
-
|
68
|
-
if (status)
|
69
|
-
rb_jump_tag(status);
|
70
|
-
else
|
71
|
-
return block_result;
|
72
|
-
}
|
73
|
-
else {
|
74
|
-
dbi::trace(flag == Qtrue ? true : false, fd);
|
75
|
-
return flag;
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
VALUE atexit_gc(...) {
|
80
|
-
rb_gc();
|
81
|
-
return Qnil;
|
82
|
-
}
|
83
|
-
|
84
|
-
void atexit_caller(VALUE data) {
|
85
|
-
rb_proc_call(data, rb_ary_new());
|
86
|
-
}
|
87
|
-
|
88
|
-
extern "C" {
|
89
|
-
void Init_swift(void) {
|
90
|
-
mSwift = rb_define_module("Swift");
|
91
|
-
|
92
|
-
eSwiftError = rb_define_class("SwiftError", CONST_GET(rb_mKernel, "StandardError"));
|
93
|
-
eSwiftArgumentError = rb_define_class("SwiftArgumentError", eSwiftError);
|
94
|
-
eSwiftRuntimeError = rb_define_class("SwiftRuntimeError", eSwiftError);
|
95
|
-
eSwiftConnectionError = rb_define_class("SwiftConnectionError", eSwiftError);
|
96
|
-
|
97
|
-
init_swift_adapter();
|
98
|
-
init_swift_attribute();
|
99
|
-
init_swift_result();
|
100
|
-
init_swift_statement();
|
101
|
-
init_swift_query();
|
102
|
-
init_swift_datetime();
|
103
|
-
|
104
|
-
rb_define_module_function(mSwift, "init", RUBY_METHOD_FUNC(swift_init), 1);
|
105
|
-
rb_define_module_function(mSwift, "trace", RUBY_METHOD_FUNC(swift_trace), -1);
|
106
|
-
|
107
|
-
// NOTE
|
108
|
-
// Swift adapter and statement objects need to be deallocated or garbage collected in the reverse
|
109
|
-
// allocation order. rb_gc() does that but gc at exit time seems to do it in allocation order which
|
110
|
-
// stuffs up dbic++ destructors.
|
111
|
-
rb_set_end_proc(atexit_caller, rb_proc_new(atexit_gc, mSwift));
|
112
|
-
}
|
113
|
-
}
|
114
|
-
|
data/ext/swift.h
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
#ifndef SWIFT_H
|
2
|
-
#define SWIFT_H
|
3
|
-
|
4
|
-
#include <dbic++.h>
|
5
|
-
#include <ruby/ruby.h>
|
6
|
-
#include <ruby/io.h>
|
7
|
-
#include <stdint.h>
|
8
|
-
#include <unistd.h>
|
9
|
-
#include <sys/types.h>
|
10
|
-
#include <pwd.h>
|
11
|
-
|
12
|
-
#define CONST_GET(scope, constant) rb_funcall(scope, rb_intern("const_get"), 1, rb_str_new2(constant))
|
13
|
-
#define TO_S(v) rb_funcall(v, rb_intern("to_s"), 0)
|
14
|
-
#define CSTRING(v) RSTRING_PTR(TO_S(v))
|
15
|
-
#define RUBY_STATIC_FUNC(func) ((int (*)(ANYARGS))func)
|
16
|
-
|
17
|
-
extern VALUE eSwiftError;
|
18
|
-
extern VALUE eSwiftArgumentError;
|
19
|
-
extern VALUE eSwiftRuntimeError;
|
20
|
-
extern VALUE eSwiftConnectionError;
|
21
|
-
|
22
|
-
#define CATCH_DBI_EXCEPTIONS() \
|
23
|
-
catch (dbi::ConnectionError &error) { \
|
24
|
-
rb_raise(eSwiftConnectionError, "%s", error.what()); \
|
25
|
-
} \
|
26
|
-
catch (dbi::Error &error) { \
|
27
|
-
rb_raise(eSwiftRuntimeError, "%s", error.what()); \
|
28
|
-
} \
|
29
|
-
catch (std::bad_alloc &error) { \
|
30
|
-
rb_raise(rb_eNoMemError, "%s", error.what()); \
|
31
|
-
} \
|
32
|
-
catch (std::exception &error) { \
|
33
|
-
rb_raise(rb_eRuntimeError, "%s", error.what()); \
|
34
|
-
}
|
35
|
-
|
36
|
-
|
37
|
-
// works without a controlling tty, getlogin() will fail when process is daemonized.
|
38
|
-
inline VALUE current_user() {
|
39
|
-
struct passwd *ptr = getpwuid(getuid());
|
40
|
-
return ptr ? rb_str_new2(ptr->pw_name) : rb_str_new2("root");
|
41
|
-
}
|
42
|
-
|
43
|
-
#include "adapter.h"
|
44
|
-
#include "adapter_io.h"
|
45
|
-
#include "query.h"
|
46
|
-
#include "result.h"
|
47
|
-
#include "statement.h"
|
48
|
-
#include "attribute.h"
|
49
|
-
#include "datetime.h"
|
50
|
-
|
51
|
-
#undef SIZET2NUM
|
52
|
-
#ifdef HAVE_LONG_LONG
|
53
|
-
#define SIZET2NUM(x) ULL2NUM(x)
|
54
|
-
#define DAYMICROSECS 86400000000LL
|
55
|
-
#else
|
56
|
-
#define SIZET2NUM(x) ULONG2NUM(x)
|
57
|
-
#define DAYMICROSECS 86400000000L
|
58
|
-
#endif
|
59
|
-
|
60
|
-
#endif
|
data/lib/swift/db.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
require 'swift/adapter/sql'
|
2
|
-
|
3
|
-
module Swift
|
4
|
-
module DB
|
5
|
-
class Mysql < Adapter::Sql
|
6
|
-
def initialize options = {}
|
7
|
-
super options.update(driver: 'mysql')
|
8
|
-
end
|
9
|
-
|
10
|
-
def returning?
|
11
|
-
false
|
12
|
-
end
|
13
|
-
|
14
|
-
# TODO Swift::Type::Bignum ?
|
15
|
-
# serial is an alias for bigint in mysql, we want integer type to be migrated as integer
|
16
|
-
# type in the database (not bigint or smallint or shortint or whatever).
|
17
|
-
def field_type attribute
|
18
|
-
case attribute
|
19
|
-
when Type::Integer then attribute.serial ? 'integer auto_increment' : 'integer'
|
20
|
-
else super
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def tables
|
25
|
-
execute("show tables").map(&:values).flatten
|
26
|
-
end
|
27
|
-
end # Mysql
|
28
|
-
|
29
|
-
class Sqlite3 < Adapter::Sql
|
30
|
-
def initialize options = {}
|
31
|
-
super options.update(driver: 'sqlite3')
|
32
|
-
end
|
33
|
-
|
34
|
-
def returning?
|
35
|
-
false
|
36
|
-
end
|
37
|
-
|
38
|
-
def migrate! scheme
|
39
|
-
keys = scheme.header.keys
|
40
|
-
serial = scheme.header.find(&:serial)
|
41
|
-
fields = scheme.header.map{|p| field_definition(p)}.join(', ')
|
42
|
-
fields += ", primary key (#{keys.join(', ')})" unless serial or keys.empty?
|
43
|
-
|
44
|
-
execute("drop table if exists #{scheme.store}")
|
45
|
-
execute("create table #{scheme.store} (#{fields})")
|
46
|
-
end
|
47
|
-
|
48
|
-
def field_type attribute
|
49
|
-
case attribute
|
50
|
-
when Type::String then 'text'
|
51
|
-
when Type::Integer then attribute.serial ? 'integer primary key' : 'integer'
|
52
|
-
when Type::Float then 'float'
|
53
|
-
when Type::BigDecimal then 'numeric'
|
54
|
-
when Type::Time then 'timestamp' # deprecated
|
55
|
-
when Type::DateTime then 'timestamp'
|
56
|
-
when Type::Date then 'date'
|
57
|
-
when Type::Boolean then 'boolean'
|
58
|
-
when Type::IO then 'blob'
|
59
|
-
else 'text'
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def tables
|
64
|
-
execute('select name from sqlite_master where type = ?', 'table').map(&:values).flatten
|
65
|
-
end
|
66
|
-
end # Sqlite3
|
67
|
-
|
68
|
-
class Postgres < Adapter::Sql
|
69
|
-
def initialize options = {}
|
70
|
-
super options.update(driver: 'postgresql')
|
71
|
-
end
|
72
|
-
|
73
|
-
def returning?
|
74
|
-
true
|
75
|
-
end
|
76
|
-
|
77
|
-
def field_type attribute
|
78
|
-
case attribute
|
79
|
-
when Type::IO then 'bytea'
|
80
|
-
else super
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def tables
|
85
|
-
execute('select tablename from pg_tables where schemaname = current_schema').map(&:values).flatten
|
86
|
-
end
|
87
|
-
end # Postgres
|
88
|
-
end # DB
|
89
|
-
end # Swift
|