DBBrowser/Table/Extensions/ScalarFunctions/To.pm untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 120 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 348 CC 61 SCAR 58.7
CC
61
Cov
0%
__format_history
870.0
function_to_epoch
210.0
__format_function
156.0
CRAP 355.6
1 package # hide from PAUSE
2 App::DBBrowser::Table::Extensions::ScalarFunctions::To;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use App::DBBrowser::Auxil;
9 use App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments;
10 use App::DBBrowser::Table::Extensions::ScalarFunctions::To::EpochTo;
11  
12  
13 sub new {
14 my ( $class, $info, $options, $d ) = @_;
15 bless {
16 i => $info,
17 o => $options,
18 d => $d
19 }, $class;
20 }
21  
22  
23 sub function_to_char {
24 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
25 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
26 }
27  
28 sub function_to_date {
29 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
30 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
31 }
32  
33 sub function_strftime {
34 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
35 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
36 }
37  
38 sub function_strptime {
39 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
40 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
41 }
42  
43 sub function_to_timestamp {
44 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
45 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
46 }
47  
48 sub function_to_timestamp_tz {
49 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
50 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
51 }
52  
53 sub function_to_number {
54 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
55 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
56 }
57  
58 sub function_date_format {
59 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
60 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
61 }
62  
63 sub function_format {
64 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
65 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
66 }
67  
68 sub function_str_to_date {
69 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
70 return $sf->__format_function( $sql, $clause, $func, $cols, $r_data );
71 }
72  
73 sub __format_function {
74 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
75 my $dbms = $sf->{i}{dbms};
76 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
77 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
78 if ( ! defined $col ) {
79 return;
80 }
81 if ( $dbms eq 'DuckDB' && $func eq 'TO_TIMESTAMP' ) {
82 return "$func($col)";
83 }
84 my $args_data = [
85 { prompt => 'Format: ', history => $sf->__format_history( $func ) },
86 ];
87 if ( $func eq 'FORMAT' ) {
88 push @$args_data, { prompt => 'Locale: ', history => [] };
89 }
90 if ( $dbms eq 'Oracle' ) {
91 push @$args_data, { prompt => 'nls_parameter: ' };
92 }
93 elsif ( $dbms eq 'DB2' ) {
94 push @$args_data, { prompt => 'Locale: ' } if $func eq 'TO_CHAR';
95 push @$args_data, { prompt => 'Decimals: ', is_numeric => 1 }, { prompt => 'Locale: ' } if $func eq 'TO_DATE'
96 }
97 my ( $format, @args ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data );
98 if ( ! defined $format ) {
99 return;
100 }
101 if ( $dbms eq 'SQLite' ) {
102 my $modifiers = $ga->sqlite_modifiers( $sql, $r_data );
103 return "$func($format,$col,$modifiers)" if length $modifiers;
104 return "$func($format,$col)";
105 }
106 my $additional_args = join ',', grep { length } @args;
107 return "$func($col,$format,$additional_args)" if length $additional_args;
108 return "$func($col,$format)";
109 }
110  
111  
112 sub __format_history {
113 my ( $sf, $func ) = @_;
114 my $dbms = $sf->{i}{dbms};
115 if ( $dbms eq 'SQLite' ) {
116 return [ '%Y-%m-%d %H:%M:%f', '%Y-%m-%d %H:%M:%S' ] if $func eq 'STRFTIME';
117 return [];
118 }
119 elsif ( $dbms =~ /^(?:mysql|MariaDB)\z/ ) {
120 return [ '%Y-%m-%d %H:%i:%S.%f', '%a %d %b %Y %h:%i:%S %p' ] if $func eq 'DATE_FORMAT';
121 return [ '%Y-%m-%d %H:%i:%S.%f', '%a %d %b %Y %h:%i:%S %p' ] if $func eq 'STR_TO_DATE';
122 return [ ] if $func eq 'FORMAT';
123 return [];
124 }
125 elsif ( $dbms eq 'Pg' ) {
126 return [ 'YYYY-MM-DD HH24:MI:SS.FF6TZH:TZM', 'Dy DD Mon YYYY HH:MI:SS AM TZ OF' ] if $func eq 'TO_CHAR'; # TZ and OF only in to_char
127 return [ 'YYYY-MM-DD' ] if $func eq 'TO_DATE';
128 return [ 'YYYY-MM-DD HH24:MI:SS.FF6TZH:TZM', 'Dy DD Mon YYYY HH:MI:SS AM' ] if $func eq 'TO_TIMESTAMP';
129 return [ ] if $func eq 'TO_NUMBER';
130 return [];
131 }
132  
133 elsif ( $dbms eq 'DuckDB' ) { ##
134 return [ '%Y-%m-%d %H:%M:%f', '%Y-%m-%d %H:%M:%S' ] if $func eq 'STRFTIME';
135 return [ '%Y-%m-%d %H:%M:%f', '%Y-%m-%d %H:%M:%S' ] if $func eq 'STRPTIME';
136 return [];
137 }
138  
139 elsif ( $dbms eq 'DB2' ) {
140 return [ 'YYYY-MM-DD HH24:MI:SS.FF6', 'Dy DD Mon YYYY HH:MI:SS AM' ] if $func eq 'TO_CHAR';
141 return [ 'YYYY-MM-DD HH24:MI:SS.FF6', 'Dy DD Mon YYYY HH:MI:SS AM' ] if $func eq 'TO_DATE';
142 return [ ] if $func eq 'TO_NUMBER';
143 return [];
144 }
145 elsif ( $dbms eq 'Informix' ) {
146 return [ '%Y-%m-%d %H:%M:%S.%F5', '%a %d %b %Y %I:%M:%S %p' ] if $func eq 'TO_CHAR';
147 return [ '%Y-%m-%d %H:%M:%S.%F5', '%a %d %b %Y %I:%M:%S %p' ] if $func eq 'TO_DATE';
148 return [ ] if $func eq 'TO_NUMBER';
149 return [];
150 }
151 elsif ( $dbms eq 'Oracle' ) {
152 return [ 'YYYY-MM-DD HH24:MI:SS.FF9TZH:TZM', 'Dy DD Mon YYYY HH:MI:SSXFF AM TZR TZD' ] if $func eq 'TO_CHAR';
153 return [ 'YYYY-MM-DD HH24:MI:SS' , 'Dy DD Mon YYYY HH:MI:SS AM' ] if $func eq 'TO_DATE'; # not in the DATE format: FF, TZD, TZH, TZM, and TZR. Max length 22
154 return [ 'YYYY-MM-DD HH24:MI:SS.FF' , 'Dy DD Mon YYYY HH:MI:SS AM' ] if $func eq 'TO_TIMESTAMP';
155 return [ 'YYYY-MM-DD HH24:MI:SS.FFTZH:TZM' , 'Dy DD Mon YYYY HH:MI:SS AM TZD' ] if $func eq 'TO_TIMESTAMP_TZ';
156 return [ ] if $func eq 'TO_NUMBER';
157 return [];
158 }
159 else {
160 return [];
161 }
162 }
163  
164  
165 sub function_str {
166 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
167 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
168 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
169 if ( ! defined $col ) {
170 return;
171 }
172 my $args_data = [
173 { prompt => 'Length: ', is_numeric => 1 },
174 { prompt => 'Decimal: ', is_numeric => 1 },
175 ];
176 my ( $length, $decimal ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data );
177 return "$func($col)" if ! length $length;
178 return "$func($col,$length)" if ! length $decimal;
179 return "$func($col,$length,$decimal)";
180 }
181  
182  
183 sub function_to_epoch {
184 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
185 #my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
186 my $dbms = $sf->{i}{dbms};
187 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
188 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
189 if ( ! defined $col ) {
190 return;
191 }
192 if ( $dbms eq 'SQLite' ) {
193 return "UNIXEPOCH($col,'utc','subsec')"; # subsec: sqlite 3.42.0
194 }
195 elsif ( $dbms =~ /^(?:mysql|MariaDB)\z/ ) {
196 return "UNIX_TIMESTAMP($col)";
197 }
198 elsif ( $dbms eq 'Pg' ) {
199 return "EXTRACT(EPOCH FROM ${col}::timestamp with time zone)";
200 }
201 elsif ( $dbms eq 'DuckDB' ) { ##
202 return "EPOCH(${col}::timestamp with time zone)";
203 #return "EPOCH($col)";
204 }
205 elsif ( $dbms eq 'Firebird' ) {
206 #my $firebird_major_version = $ax->major_server_version();
207 my $firebird_major_version = 3; ##
208 return "DATEDIFF(SECOND,TIMESTAMP '1970-01-01 00:00:00 UTC',$col)" if $firebird_major_version >= 4;
209 return "DATEDIFF(SECOND,TIMESTAMP '1970-01-01 00:00:00',$col)"; # no timezone
210 #return "DATEDIFF(MILLISECOND,TIMESTAMP '1970-01-01 00:00:00',$col) * 0.001"; # * 0.001 doesn't work in version 4 ##
211 }
212 elsif ( $dbms eq 'DB2' ) {
213 return "EXTRACT(EPOCH FROM $col)"; # no timezone
214 }
215 elsif ( $dbms eq 'Oracle' ) {
216 my $args_data = [
217 { prompt => 'Column type: ', unquote => 1, history => [ qw(DATE TIMESTAMP TIMESTAMP_TZ) ], history_only => 1 } ##
218 ];
219 my ( $column_type ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data );
220 $column_type = uc $column_type;
221 return "TRUNC((CAST($col AT TIME ZONE 'UTC' AS DATE) - DATE '1970-01-01') * 86400)" if $column_type eq 'TIMESTAMP_TZ';
222 return "TRUNC((CAST(FROM_TZ($col,SESSIONTIMEZONE) AT TIME ZONE 'UTC' AS DATE) - DATE '1970-01-01') * 86400)" if $column_type eq 'TIMESTAMP';
223 return "TRUNC((CAST(FROM_TZ(CAST($col AS TIMESTAMP),SESSIONTIMEZONE) AT TIME ZONE 'UTC' AS DATE) - DATE '1970-01-01') * 86400)" if $column_type eq 'DATE';
224 }
225 elsif ( $dbms eq 'MSSQL' ) {
226 return "CAST(DATEDIFF(s,'1970-01-01 00:00:00',$col)AS BIGINT)";
227 }
228 }
229  
230  
231 sub function_unixepoch {
232 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
233 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
234 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
235 if ( ! defined $col ) {
236 return;
237 }
238 my $modifiers = $ga->sqlite_modifiers( $sql, $r_data );
239 return "$func($col,$modifiers)" if length $modifiers;
240 return "$func($col)";
241 }
242  
243  
244 sub function_epoch_to_date {
245 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
246 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
247 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
248 if ( ! defined $col ) {
249 return;
250 }
251 my $new_et = App::DBBrowser::Table::Extensions::ScalarFunctions::To::EpochTo->new( $sf->{i}, $sf->{o}, $sf->{d} );
252 return $new_et->epoch_to( $sql, $col, $func );
253 }
254  
255  
256  
257 sub function_epoch_to_datetime {
258 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
259 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
260 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
261 if ( ! defined $col ) {
262 return;
263 }
264 my $new_et = App::DBBrowser::Table::Extensions::ScalarFunctions::To::EpochTo->new( $sf->{i}, $sf->{o}, $sf->{d} );
265 return $new_et->epoch_to( $sql, $col, $func );
266 }
267  
268  
269 sub function_epoch_to_timestamp {
270 my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_;
271 my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} );
272 my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data );
273 if ( ! defined $col ) {
274 return;
275 }
276 my $new_et = App::DBBrowser::Table::Extensions::ScalarFunctions::To::EpochTo->new( $sf->{i}, $sf->{o}, $sf->{d} );
277 return $new_et->epoch_to( $sql, $col, $func );
278 }
279  
280  
281  
282  
283  
284 1;