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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
package UR::Namespace::Command::Define::Db;
use warnings;
use strict;
use UR;
our $VERSION = "0.47"; # UR $VERSION;
use IO::File; # required to import symbols used below
UR::Object::Type->define(
class_name => __PACKAGE__,
is => "UR::Namespace::Command::Base",
has_input => [
uri => {
is => 'Text',
shell_args_position => 1,
doc => 'a DBI connect string like dbi:mysql:someserver or user/passwd@dbi:Oracle:someserver~defaultns'
},
name => {
is => 'Text',
shell_args_position => 2,
default_value => 'Db1',
doc => "the name for this data source (used for class naming)",
},
],
has_output_optional => [
_class_name=> {
is => 'Text',
calculate_from => ['name'],
calculate => q|
my $namespace = $self->namespace_name;
my $dsid = $namespace . '::DataSource::' . $name;
return $dsid
|,
doc => "The full class name to give this data source.",
},
_ds => {
is_transient => 1,
},
],
doc => 'add a data source to the current namespace'
);
sub sub_command_sort_position { 2 }
sub help_synopsis {
return <<'EOS'
ur define db dbi:SQLite:/some/file.db Db1
ur define db me@dbi:mysql:myserver MainDb
ur define db me@dbi:Oracle:someserver ProdDb
ur define db me@dbi:Oracle:someserver~schemaname BigDb
ur define db me@dbi:Pg:prod Db1
ur define db me@dbi:Pg:dev Testing::Db1 # alternate for "Testing" (arbitrary) context
ur define db me@dbi:Pg:stage Staging::Db1 # alternate for "Staging" (arbitrary) context
EOS
}
sub data_source_module_pathname {
my $self = shift;
my $class_name = shift;
my $ns_path = $self->namespace_path;
my @ds_parts = split(/::/, $class_name);
shift @ds_parts; # Get rid of the namespace name
my $filename = pop @ds_parts;
$filename .= '.pm';
my $path = join('/', $ns_path, @ds_parts, $filename);
return $path;
}
sub execute {
my $self = shift;
my $namespace = $self->namespace_name;
unless ($namespace) {
$self->error_message("This command must be run from a namespace directory.");
return;
}
my $uri = $self->uri;
my ($protocol,$driver,$login,$server,$owner) = ($uri =~ /^([^\:\W]+):(.*?):(.*@|)(.*?)(~.*|)$/);
unless ($protocol) {
$self->error_message("error parsing URI $uri\n" . 'expected dbi:$driver:$user@$server with optional trailing ~$namespace');
return;
}
unless ($protocol eq 'dbi') {
$self->error_message("currently only the 'dbi' protocol is supported with this command. Other data sources must be hand-written.");
return;
}
$login =~ s/\@$// if defined $login;
$owner =~ s/^~// if defined $owner;
$self->status_message("protocol: $protocol");
$self->status_message("driver: $driver");
$self->status_message("server: $server");
my $password;
if (defined $login) {
if ($login =~ /\//) {
($login,$password) = split('/',$login);
}
$self->status_message("login: $login") if defined $login;
$self->status_message("password: $password") if defined $password;
}
$self->status_message("owner: $owner") if defined $owner;
# Force an autoload of the namespace module
eval "use $namespace";
if ($@) {
$self->error_message("Can't load namespace $namespace: $@");
return;
}
my $class_name = $self->namespace_name . '::DataSource::' . $self->name;
$self->_class_name($class_name);
my $c = eval { UR::DataSource->get($class_name) || $class_name->get() };
if ($c) {
$self->error_message("A data source named $class_name already exists\n");
return;
}
my $src = "package $class_name;\nuse strict;\nuse warnings;\nuse $namespace;\n\n";
$src .= "class $class_name {\n";
my $parent_ds_class = 'UR::DataSource::' . $driver; #$self->_data_source_sub_class_name();
$driver =~ s/mysql/MySQL/g;
my @parent_classes = ( $parent_ds_class );
push @parent_classes, 'UR::Singleton';
$src .= sprintf(" is => [ '%s' ],\n", join("', '", @parent_classes));
$src .= "};\n";
my $module_body = $self->_resolve_module_body($class_name,$namespace,$driver,$server,$login);
$src .= "\n$module_body\n1;\n";
my $module_path = $self->data_source_module_pathname($class_name);
my $fh = IO::File->new($module_path, O_WRONLY | O_CREAT | O_EXCL);
unless ($fh) {
$self->error_message("Can't open $module_path for writing: $!");
return;
}
$fh->print($src);
$fh->close();
$self->status_message("A $class_name (" . join(',', @parent_classes) . ")\n");
#TODO: call a method on the datasource to init the new file
my $method = '_post_module_written_' . lc($driver);
$self->$method($module_path,$server);
unless (UR::Object::Type->use_module_with_namespace_constraints($class_name)) {
#if ($@) {
$self->error_message("Error in module $class_name!?: $@");
return;
}
my $ds = $class_name->get();
unless ($ds) {
$self->error_message("Failed to get data source for $class_name!");
return;
}
$self->_ds($ds);
if ($self->_try_connect()) {
return 1;
} else {
return;
}
}
sub _resolve_module_body {
my ($self,$class_name,$namespace,$driver,$server,$login,$owner) = @_;
$owner ||= $login;
my $src = <<EOS;
sub driver { '$driver' };
sub server { '$server' };
EOS
# TODO: key this off of a property on the datasource
# so datasource writers don't have to make a custom command here
if ($driver ne 'SQLite') {
$src .= "sub login { '$login' }\n";
$src .= "sub auth { warn 'Set db password at ' . __LINE__ . ' in ' . __FILE__; return '' }\n";
$src .= "sub owner { '$owner' }\n";
}
return $src;
}
sub _post_module_written_sqlite {
my ($self, $pathname, $server) = @_;
# Create a new, empty DB if it dosen't exist yet
IO::File->new($server, O_WRONLY | O_CREAT) unless (-f $server);
$self->status_message("A $server (empty database schema)");
$pathname =~ s/\.pm$/.sqlite3/;
unless ($pathname eq $server) {
symlink ($server, $pathname) or die "no symline $pathname for $server! $!";
}
return 1;
}
sub _post_module_written_pg {
my ($self, $pathname, $server) = @_;
return 1;
}
sub _post_module_written_oracle {
my ($self, $pathname, $server) = @_;
return 1;
}
sub _post_module_written_mysql {
my ($self, $pathname, $server) = @_;
return 1;
}
sub _post_module_written_file {
my ($self, $pathname, $server) = @_;
return 1;
}
sub _post_module_written_filemux {
my ($self, $pathname, $server) = @_;
return 1;
}
sub _try_connect {
my $self = shift;
$self->status_message(" ...connecting...");
my $ds = $self->_ds;
my $dbh = $ds->get_default_handle();
if ($dbh) {
$self->status_message(" ....ok\n");
return 1;
} else {
$self->error_message(" ERROR: " . $ds->error_message);
return;
}
}
1;
|