-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathedbi-bridge.pl
201 lines (174 loc) · 4.67 KB
/
edbi-bridge.pl
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
#!/usr/bin/perl
use strict;
use RPC::EPC::Service;
use Data::Dumper;
use DBI;
our $dbh = undef;
our $sth = undef;
sub edbi_connect {
my ($args) = @_;
my ($data_source,$username,$auth) = @$args;
# No input _probably_ means "no password" rather than empty string
$auth = undef if($auth eq "");
if ($dbh) {
eval {
$dbh->disconnect();
}
}
our $dbh = DBI->connect($data_source, $username, $auth)
or die("Could not connect to database:
Data Source ($data_source)
User Name: ($username):
DBI error: ($DBI::errstr)
");
return $dbh->get_info(18);
}
sub edbi_do {
return undef unless $dbh;
my ($args) = @_;
my ($sql, $params) = @$args;
my $rows = $dbh->do($sql, undef, @$params);
return $rows;
}
sub edbi_select_all {
return undef unless $dbh;
my ($args) = @_;
my ($sql, $params) = @$args;
my $rows = $dbh->selectall_arrayref($sql, undef, @$params); return $rows;
}
sub edbi_prepare {
return undef unless $dbh;
$sth->finish() if $sth;
my ($sql) = @_;
our $sth = $dbh->prepare($sql) or return undef;
return 'sth';
}
sub edbi_execute {
return undef unless $sth;
my ($params) = @_;
return $sth->execute(@$params) or return undef;
}
sub edbi_fetch_columns {
return undef unless $sth;
return $sth->{NAME};
}
sub edbi_fetch {
return undef unless $sth;
my ($num) = @_;
if ($num eq undef) {
return $sth->fetchall_arrayref();
} else {
my $ret = [];
for (my $i = 0; $i < $num; $i++) {
my $row = $sth->fetchrow_arrayref();
last if $row eq undef;
push @$ret, [@$row];
}
return $ret;
}
}
sub edbi_auto_commit {
return undef unless $dbh;
my ($flag) = @_;
if ($flag eq "true") {
$dbh->{AutoCommit} = 1;
return 1;
} else {
$dbh->{AutoCommit} = 0;
return 0;
}
}
sub edbi_commit {
return undef unless $dbh;
$dbh->commit();
return 1;
}
sub edbi_rollback {
return undef unless $dbh;
$dbh->rollback();
return 1;
}
sub edbi_disconnect {
return undef unless $dbh;
$dbh->disconnect();
return 1;
}
sub edbi_status {
return undef unless $dbh;
return [$dbh->err, $dbh->errstr, $dbh->state];
}
sub edbi_type_info_all {
return undef unless $dbh;
my $ret = $dbh->type_info_all;
print STDERR Dumper $ret;
return $dbh->type_info_all;
}
sub edbi_table_info {
return undef unless $dbh;
eval {
$sth->finish() if $sth;
};
my ($args) = @_;
my ($catalog, $schema, $table, $type) = @$args;
$sth = $dbh->table_info( $catalog, $schema, $table, $type );
return [$sth->{NAME}, $sth->fetchall_arrayref()];
}
sub edbi_column_info {
return undef unless $dbh;
eval {
$sth->finish() if $sth;
};
my ($args) = @_;
my ($catalog, $schema, $table, $column) = @$args;
$sth = $dbh->column_info( $catalog, $schema, $table, $column );
return [[],[]] unless $sth;
return [$sth->{NAME}, $sth->fetchall_arrayref()];
}
sub edbi_primary_key_info {
return undef unless $dbh;
eval {
$sth->finish() if $sth;
};
my ($args) = @_;
my ($catalog, $schema, $table) = @$args;
$sth = $dbh->primary_key_info( $catalog, $schema, $table );
return undef unless $sth;
return [$sth->{NAME}, $sth->fetchall_arrayref()];
}
sub edbi_foreign_key_info {
return undef unless $dbh;
eval {
$sth->finish() if $sth;
};
my ($args) = @_;
my ($pkcatalog, $pkschema, $pktable, $fkcatalog, $fkschema, $fktable) = @$args;
$sth = $dbh->foreign_key_info( $pkcatalog, $pkschema, $pktable,
$fkcatalog, $fkschema, $fktable );
return undef unless $sth;
return [$sth->{NAME}, $sth->fetchall_arrayref()];
}
sub main {
my $methods =
{
'connect' => [\&edbi_connect,"data_source, username, auth", ""],
'do' => [\&edbi_do, "sql, params", ""],
'select-all' => [\&edbi_select_all, "sql, params", ""],
'prepare' => [\&edbi_prepare, "sql", ""],
'execute' => [\&edbi_execute, "params", ""],
'fetch-columns' => [\&edbi_fetch_columns, "", ""],
'fetch' => [\&edbi_fetch, "[number]", ""],
'auto-commit' => [\&edbi_auto_commit, "false/true", ""],
'commit' => [\&edbi_commit, "", ""],
'rollback' => [\&edbi_rollback, "", ""],
'disconnect' => [\&edbi_disconnect, "", ""],
'status' => [\&edbi_status, "", ""],
'type-info-all' => [\&edbi_type_info_all, "", ""],
'table-info' => [\&edbi_table_info, "catalog, schema, table, type", ""],
'column-info' => [\&edbi_column_info, "catalog, schema, table, column", ""],
'primary-key-info' => [\&edbi_primary_key_info, "catalog, schema, table", ""],
'foreign-key-info' => [\&edbi_foreign_key_info, "pk_catalog, pk_schema, pk_table, fk_catalog, fk_schema, fk_table", ""],
};
my $server = RPC::EPC::Service->new(0, $methods);
$server->start;
}
main;