| 1 | package UR::Namespace::Command::Define::Datasource::Rdbms; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use UR; | ||
| 6 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 7 | |||
| 8 | use IO::File; | ||
| 9 | |||
| 10 | UR::Object::Type->define( | ||
| 11 | class_name => __PACKAGE__, | ||
| 12 | is => 'UR::Namespace::Command::Define::Datasource', | ||
| 13 | has => [ | ||
| 14 | server => { | ||
| 15 | is => 'String', | ||
| 16 | doc => '"server" attribute for this data source, such as a database name', | ||
| 17 | is_optional => 1, | ||
| 18 | }, | ||
| 19 | nosingleton => { | ||
| 20 | is => 'Boolean', | ||
| 21 | doc => 'Created data source should not inherit from UR::Singleton (defalt is that it will)', | ||
| 22 | default_value => 0, | ||
| 23 | }, | ||
| 24 | ], | ||
| 25 | is_abstract => 1, | ||
| 26 | ); | ||
| 27 | |||
| 28 | sub help_description { | ||
| 29 | "Define a UR datasource connected to a relational database through UR::DataSource::RDBMS and DBI"; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | sub execute { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | my $namespace = $self->namespace_name; | ||
| 37 | unless ($namespace) { | ||
| 38 | $self->error_message("This command must be run from a namespace directory."); | ||
| 39 | return; | ||
| 40 | } | ||
| 41 | |||
| 42 | unless ($self->__dsname || $self->__dsid) { | ||
| 43 | $self->error_message("Either --dsname or --dsid is required"); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | |||
| 47 | # Force an autoload of the namespace module | ||
| 48 | #my $ret = above::use_package($namespace); | ||
| 49 | eval "use $namespace"; | ||
| 50 | if ($@) { | ||
| 51 | $self->error_message("Can't load namespace $namespace: $@"); | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | unless (defined $self->server) { | ||
| 56 | $self->server($self->dsname); | ||
| 57 | } | ||
| 58 | |||
| 59 | my $ds_id = $self->dsid; | ||
| 60 | |||
| 61 | my $c = eval { UR::DataSource->get($ds_id) || $ds_id->get() }; | ||
| 62 | if ($c) { | ||
| 63 | $self->error_message("A data source named $ds_id already exists\n"); | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $src = $self->_resolve_module_header($ds_id,$namespace); | ||
| 68 | |||
| 69 | my($class_definition,$parent_classes) = $self->_resolve_class_definition_source(); | ||
| 70 | $src .= $class_definition; | ||
| 71 | |||
| 72 | my $module_body = $self->_resolve_module_body(); | ||
| 73 | $src .= "\n$module_body\n1;\n"; | ||
| 74 | |||
| 75 | my $module_path = $self->data_source_module_pathname(); | ||
| 76 | my $fh = IO::File->new($module_path, O_WRONLY | O_CREAT | O_EXCL); | ||
| 77 | unless ($fh) { | ||
| 78 | $self->error_message("Can't open $module_path for writing: $!"); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | $fh->print($src); | ||
| 83 | $fh->close(); | ||
| 84 | |||
| 85 | $self->status_message("A $ds_id (" . join(',', @$parent_classes) . ")\n"); | ||
| 86 | |||
| 87 | $self->_post_module_written(); | ||
| 88 | |||
| 89 | if ($self->_try_connect()) { | ||
| 90 | return 1; | ||
| 91 | } else { | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | sub _resolve_module_header { | ||
| 99 | my($self,$ds_id, $namespace) = @_; | ||
| 100 | |||
| 101 | return "package $ds_id;\n\nuse strict;\nuse warnings;\n\nuse $namespace;\n\n"; | ||
| 102 | } | ||
| 103 | |||
| 104 | # Subclasses can override this to have something happen after the module | ||
| 105 | # is written, but before we try connecting to the DS | ||
| 106 | sub _post_module_written { | ||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | # Subclasses must override this to indicate what abstract DS class they should | ||
| 112 | # inherit from | ||
| 113 | sub _data_source_sub_class_name { | ||
| 114 | my $self = shift; | ||
| 115 | my $class = ref($self); | ||
| 116 | die "Class $class didn't implement _data_source_sub_class_name"; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub _resolve_class_definition_source { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | my $ds_id = $self->dsid; | ||
| 124 | |||
| 125 | my $parent_ds_class = $self->_data_source_sub_class_name(); | ||
| 126 | my $src = "class $ds_id {\n"; | ||
| 127 | |||
| 128 | my @parent_classes = ( $parent_ds_class ); | ||
| 129 | if (! $self->nosingleton) { | ||
| 130 | push @parent_classes, 'UR::Singleton'; | ||
| 131 | } | ||
| 132 | |||
| 133 | $src .= sprintf(" is => [ '%s' ],\n", join("', '", @parent_classes)); | ||
| 134 | |||
| 135 | $src .= "};\n"; | ||
| 136 | |||
| 137 | return($src,\@parent_classes); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | |||
| 142 | sub _resolve_module_body { | ||
| 143 | my $self = shift; | ||
| 144 | |||
| 145 | my $server = $self->server; | ||
| 146 | my $src = "sub server { '$server' }\n"; | ||
| 147 | |||
| 148 | return $src; | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | sub _try_connect { | ||
| 154 | my $self = shift; | ||
| 155 | |||
| 156 | $self->status_message(" ...connecting..."); | ||
| 157 | |||
| 158 | my $ds_id = $self->dsid; | ||
| 159 | my $dbh = $ds_id->get_default_handle(); | ||
| 160 | if ($dbh) { | ||
| 161 | $self->status_message(" ....ok\n"); | ||
| 162 | return 1; | ||
| 163 | } else { | ||
| 164 | $self->error_message(" ERROR: " . $ds_id->error_message); | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | |||
| 171 | 1; | ||
| 172 |