ensembl-hive  2.5
SqlCmd.pm
Go to the documentation of this file.
1 =pod
2 
3 =head1 NAME
4 
6 
7 =head1 SYNOPSIS
8 
9  standaloneJob.pl Bio::EnsEMBL::Hive::RunnableDB::SqlCmd --db_conn mysql://ensadmin:${ENSADMIN_PSW}@127.0.0.1:2912/lg4_compara_families_64 \
10  --sql "INSERT INTO meta(meta_key,meta_value) VALUES ('Hello', 'world')"
11 
12  standaloneJob.pl Bio::EnsEMBL::Hive::RunnableDB::SqlCmd --db_conn mysql://ensadmin:${ENSADMIN_PSW}@127.0.0.1:2913/lg4_compara_homology_merged_64 \
13  --sql "[ 'CREATE TABLE meta_foo LIKE meta', 'INSERT INTO meta_foo SELECT * FROM meta' ]"
14 
15 =head1 DESCRIPTION
16 
17  This RunnableDB module acts as a wrapper for an SQL command
18  run against either the current hive database (default) or against one specified by 'db_conn' parameter
19  (--db_conn becomes obligatory in standalone mode, because there is no hive_db).
20 
21  The Sql command must be stored in the parameters() as the value corresponding to the 'sql' key.
22  It allows to pass in other parameters and use the parameter substitution mechanism in its full glory.
23 
24  The Sql command(s) can be wrapped in a global transaction if the "wrap_in_transaction" flag is switched on (off by default)
25 
26 =head1 LICENSE
27 
28  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
29  Copyright [2016-2022] EMBL-European Bioinformatics Institute
30 
31  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
32  You may obtain a copy of the License at
33 
34  http://www.apache.org/licenses/LICENSE-2.0
35 
36  Unless required by applicable law or agreed to in writing, software distributed under the License
37  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38  See the License for the specific language governing permissions and limitations under the License.
39 
40 =head1 CONTACT
41 
42  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
43 
44 =cut
45 
46 
47 package Bio::EnsEMBL::Hive::RunnableDB::SqlCmd;
48 
49 use strict;
50 use warnings;
51 
52 use base ('Bio::EnsEMBL::Hive::Process');
53 
54 sub param_defaults {
55  return {
56  'wrap_in_transaction' => 0,
57  }
58 }
59 
60 
61 =head2 run
62 
63  Description : Implements run() interface method of Bio::EnsEMBL::Hive::Process that is used to perform the main bulk of the job (minus input and output).
64  Here it is a simple decision step based on the "wrap_in_transaction" parameter. If the latter is set, call _exec_sql() in a transaction,
65  otherwise call it directly.
66 
67  param('wrap_in_transaction'): Whether or not run the commands in a global transaction
68 
69 =cut
70 
71 sub run {
72  my $self = shift;
73 
74  if ($self->param('wrap_in_transaction')) {
75  $self->data_dbc()->run_in_transaction( sub {
76  $self->_exec_sql();
77  } );
78  } else {
79  $self->_exec_sql();
80  }
81 }
82 
83 
84 =head2 _exec_sql
85 
86  Description : Actually run the sql command(s). If a list of commands is given, they are run in succession within the same session
87  (so you can create a temporary tables and use it in another command within the same sql command list).
88 
89  param('sql'): Either a scalar SQL command or an array of SQL commands.
90 
91  param('db_conn'): An optional hash to pass in connection parameters to the database upon which the sql command(s) will have to be run.
92 
93  param('*'): Any other parameters can be freely used for parameter substitution.
94 
95 =cut
96 
97 sub _exec_sql {
98  my $self = shift;
99 
100  my $sqls = $self->param_required('sql');
101  $sqls = [$sqls] if(ref($sqls) ne 'ARRAY');
102 
103  my $data_dbc = $self->data_dbc();
104 
105  my %output_id;
106 
107  my $counter = 0;
108  foreach my $sql (@$sqls) {
109 
110  $self->say_with_header(qq{sql = "$sql"});
111 
112  $data_dbc->do( $sql ) or die "Could not run '$sql': ".$data_dbc->db_handle->errstr;
113 
114  my $insert_id_name = '_insert_id_'.$counter++;
115  # FIXME: does this work if the "MySQL server has gone away" ?
116  my $insert_id_value = $data_dbc->db_handle->last_insert_id(undef, undef, undef, undef);
117 
118  $output_id{$insert_id_name} = $insert_id_value;
119  $self->param($insert_id_name, $insert_id_value); # for templates
120  }
121 
122  $self->param('output_id', \%output_id);
123 }
124 
125 
126 =head2 write_output
127 
128  Description : Implements write_output() interface method of Bio::EnsEMBL::Hive::Process that is used to deal with job's output after the execution.
129  Here we only flow out the insert_ids.
130 
131 =cut
132 
133 sub write_output {
134  my $self = shift;
135 
136  $self->dataflow_output_id( $self->param('output_id'), 2);
137 }
138 
139 1;
140 
public main()