68,3 → 68,97 |
<& /input/string.comp, %ARGS, rows=>6, cols=>60 &> |
</%method> |
|
<%method JSON_SELECT_FIELDS> |
<%perl> |
my @list; |
my @names = &Method2Array('FIELDS'); |
my %standards = map { $_ => 1 } &Method2Array('STANDARD_FIELDS'); |
foreach my $name (@names){ |
if(exists $ARGS{$name}){ |
push @list, $ARGS{$name}; |
}elsif($standards{$name}){ |
push @list, $name; |
}else{ |
push @list, "fields->>'$name' as $name"; |
} |
} |
$m->out(join ', ', @list); |
</%perl> |
</%method> |
|
<%method JSON_INSERT> |
<%args> |
$FIELDS |
</%args> |
<%perl> |
my @standards = &Method2Array('STANDARD_FIELDS'); |
my %STANDARDS; |
foreach my $name (@standards){ |
if(exists $FIELDS->{$name}){ |
$STANDARDS{$name} = delete $FIELDS->{$name}; |
} |
} |
# empty fields are replaced with undef value |
foreach my $key (keys %$FIELDS){ |
if(defined $FIELDS->{$key} && $FIELDS->{$key} eq ''){ |
$FIELDS->{$key} = undef; |
} |
} |
# fields are returned with values in the JSON hash |
return { fields => to_json($FIELDS), %STANDARDS }; |
</%perl> |
</%method> |
|
<%method JSON_UPDATE> |
<%args> |
$FIELDS |
</%args> |
<%perl> |
my @standards = &Method2Array('STANDARD_FIELDS'); |
my %STANDARDS; |
foreach my $name (@standards){ |
if(exists $FIELDS->{$name}){ |
$STANDARDS{$name} = delete $FIELDS->{$name}; |
} |
} |
# empty fields are replaced with undef value |
foreach my $key (keys %$FIELDS){ |
if(defined $FIELDS->{$key} && $FIELDS->{$key} eq ''){ |
$FIELDS->{$key} = undef; |
} |
} |
# using array ref it is possible to change assignment (default equal to '?' as value of the field) |
# fields are replaced with new values in the JSON hash |
return { fields => [q[coalesce(fields, '{}'::jsonb) || ?::jsonb], to_json($FIELDS)], %STANDARDS }; |
</%perl> |
</%method> |
|
%# chiamata per valutare la query fornita dal browser |
<%method JSON_WHERE>\ |
<%args> |
$obj_where |
$no_check => undef |
</%args> |
<%perl> |
# rielaboro la query per adattarla ai reali campi presenti nel database di tipo JSON |
my %standards = map { $_ => 1 } &Method2Array('STANDARD_FIELDS'); |
foreach my $key (keys %$obj_where){ |
if($key !~ m/^-/){ # no special key |
if(!exists $standards{$key}){ |
my $par = $obj_where->{$key}; |
if(!defined $par->[1]){ |
$par->[3] = "coalesce(fields->>'$key', '')"; |
$par->[2] = undef; |
$par->[1] = ''; |
}else{ |
$par->[3] = "fields->>'$key'"; |
} |
} |
}elsif(!$no_check && $key =~ '^-where'){ |
# verifica la sub-query alla ricerca di codice SQL non autorizzato (sql injection) |
&check_where_clause($obj_where->{$key}) |
} |
} |
return $obj_where; |
</%perl></%method> |
|