| 1 | # ----------------------------------------------------------------------------- | ||||||||||
| 2 | |||||||||||
| 3 | =encoding utf8 | ||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Quiq::PostgreSql::CopyFormat - Erzeuge Daten für PostgreSQL COPY-Kommando | ||||||||||
| 8 | |||||||||||
| 9 | =head1 BASE CLASS | ||||||||||
| 10 | |||||||||||
| 11 | L<Quiq::Hash> | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Quiq::PostgreSql::CopyFormat; | ||||||||||
| 16 | |||||||||||
| 17 | # Instantiiere Objekt | ||||||||||
| 18 | my $cpy = Quiq::PostgreSql::CopyFormat->new($width); | ||||||||||
| 19 | |||||||||||
| 20 | # Übersetze Array in COPY-Zeile | ||||||||||
| 21 | my $line = $cpy->arrayToLine(\@arr); | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | Die Klasse dient zur Umwandlung von Daten, so dass sie vom | ||||||||||
| 26 | PostgreSQL COPY-Kommando verarbeitet werden können. | ||||||||||
| 27 | |||||||||||
| 28 | =head1 ATTRIBUTES | ||||||||||
| 29 | |||||||||||
| 30 | =over 4 | ||||||||||
| 31 | |||||||||||
| 32 | =item width => $n | ||||||||||
| 33 | |||||||||||
| 34 | Anzahl der Kolumnen pro Zeile. | ||||||||||
| 35 | |||||||||||
| 36 | =back | ||||||||||
| 37 | |||||||||||
| 38 | =cut | ||||||||||
| 39 | |||||||||||
| 40 | # ----------------------------------------------------------------------------- | ||||||||||
| 41 | |||||||||||
| 42 | package Quiq::PostgreSql::CopyFormat; | ||||||||||
| 43 | 7 | ▶ | use base qw/Quiq::Hash/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 7 | ▶ | use v5.10; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | 7 | our $VERSION = '1.240'; | |||||||||
| 50 | |||||||||||
| 51 | # ----------------------------------------------------------------------------- | ||||||||||
| 52 | |||||||||||
| 53 | =head1 METHODS | ||||||||||
| 54 | |||||||||||
| 55 | =head2 Klassenmethoden | ||||||||||
| 56 | |||||||||||
| 57 | =head3 new() - Konstruktor | ||||||||||
| 58 | |||||||||||
| 59 | =head4 Synopsis | ||||||||||
| 60 | |||||||||||
| 61 | $cpy = $class->new($width); | ||||||||||
| 62 | |||||||||||
| 63 | =head4 Arguments | ||||||||||
| 64 | |||||||||||
| 65 | =over 4 | ||||||||||
| 66 | |||||||||||
| 67 | =item $width | ||||||||||
| 68 | |||||||||||
| 69 | Anahl der Kolumnen pro Zeile. | ||||||||||
| 70 | |||||||||||
| 71 | =back | ||||||||||
| 72 | |||||||||||
| 73 | =head4 Returns | ||||||||||
| 74 | |||||||||||
| 75 | Objekt | ||||||||||
| 76 | |||||||||||
| 77 | =head4 Description | ||||||||||
| 78 | |||||||||||
| 79 | Instantiiere ein Objekt der Klasse für die Behandlung von Daten mit | ||||||||||
| 80 | $width Kolumnen und liefere eine Referenz auf dieses Objekt zurück. | ||||||||||
| 81 | |||||||||||
| 82 | =cut | ||||||||||
| 83 | |||||||||||
| 84 | # ----------------------------------------------------------------------------- | ||||||||||
| 85 | |||||||||||
| 86 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 87 | 1 | my ($class,$width) = @_; | |||||||||
| 88 | |||||||||||
| 89 | 1 | return $class->SUPER::new( | |||||||||
| 90 | width => $width, | ||||||||||
| 91 | ); | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # ----------------------------------------------------------------------------- | ||||||||||
| 95 | |||||||||||
| 96 | =head2 Objektmethoden | ||||||||||
| 97 | |||||||||||
| 98 | =head3 arrayToLine() - Übersetze Array in eine COPY-Zeile | ||||||||||
| 99 | |||||||||||
| 100 | =head4 Synopsis | ||||||||||
| 101 | |||||||||||
| 102 | $line = $cpy->arrayToLine(\@arr); | ||||||||||
| 103 | |||||||||||
| 104 | =head4 Arguments | ||||||||||
| 105 | |||||||||||
| 106 | =over 4 | ||||||||||
| 107 | |||||||||||
| 108 | =item \@arr | ||||||||||
| 109 | |||||||||||
| 110 | Array mit $width Komponenten. | ||||||||||
| 111 | |||||||||||
| 112 | =back | ||||||||||
| 113 | |||||||||||
| 114 | =head4 Returns | ||||||||||
| 115 | |||||||||||
| 116 | Zeile für eine COPY-Datei (String) | ||||||||||
| 117 | |||||||||||
| 118 | =head4 Description | ||||||||||
| 119 | |||||||||||
| 120 | Erzeuge aus den Komponenten des Arrays @arr eine Datenzeile für | ||||||||||
| 121 | das PostgreSQL COPY Kommando. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | # ----------------------------------------------------------------------------- | ||||||||||
| 126 | |||||||||||
| 127 | 2 | ▶ | sub arrayToLine { | ||||||||
|
Subroutine
arrayToLine: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 128 | 2 | my ($self,$arr) = @_; | |||||||||
| 129 | |||||||||||
| 130 | 2 | ▶ | if (@$arr != $self->{'width'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | $self->throw( | ||||||||||
| 132 | 'PG-00001: Wrong array length', | ||||||||||
| 133 | ArrayLength => scalar @$arr, | ||||||||||
| 134 | 1 | ExpectedLength => $self->{'width'}, | |||||||||
| 135 | ); | ||||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 1 | my $str = ''; | |||||||||
| 139 | 1 | for (@$arr) { | |||||||||
| 140 | 2 | my $val = $_; # Wert kopieren | |||||||||
| 141 | 2 | $val =~ s/\t/\\t/g; | |||||||||
| 142 | 2 | $val =~ s/\n/\\n/g; | |||||||||
| 143 | 2 | ▶ | if ($str) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 1 | $str .= "\t"; | |||||||||
| 145 | } | ||||||||||
| 146 | 2 | $str .= $val; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 1 | return $str; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | # ----------------------------------------------------------------------------- | ||||||||||
| 153 | |||||||||||
| 154 | =head1 VERSION | ||||||||||
| 155 | |||||||||||
| 156 | 1.240 | ||||||||||
| 157 | |||||||||||
| 158 | =head1 AUTHOR | ||||||||||
| 159 | |||||||||||
| 160 | Frank Seitz, L<http://fseitz.de/> | ||||||||||
| 161 | |||||||||||
| 162 | =head1 COPYRIGHT | ||||||||||
| 163 | |||||||||||
| 164 | Copyright (C) 2026 Frank Seitz | ||||||||||
| 165 | |||||||||||
| 166 | =head1 LICENSE | ||||||||||
| 167 | |||||||||||
| 168 | This code is free software; you can redistribute it and/or modify | ||||||||||
| 169 | it under the same terms as Perl itself. | ||||||||||
| 170 | |||||||||||
| 171 | =cut | ||||||||||
| 172 | |||||||||||
| 173 | # ----------------------------------------------------------------------------- | ||||||||||
| 174 | |||||||||||
| 175 | 7 | 1; | |||||||||
| 176 | |||||||||||
| 177 | # eof | ||||||||||