5.10.Example - Bytea Escaping - Picture Viewer, Part 2 - View Pictures
Prev | Chapter5.Example Programs | Next

## 5.10.Example - Bytea Escaping - Picture Viewer, Part 2 - View Pictures

この用例は､ Section5.8, “Example - Prepared Queries - Picture Viewer, Part 2 - View Pictures” で示した､GIF 絵画鑑賞の変種である｡これも､映像をデータベース内に可変長のバイナリ列の領域として 保存し､ 二進様式内で準備された質問を使うより､通常の文書様式質問を使う｡ 通常のSQL質問そのままを通して二進データを得る為に､ pg_escape_bytea と pg_unescape_bytea が､使われる｡ この版は､二進の準備された質問の使用より､効果が劣り､本質的に遅いかもしれない｡ This example is a variation of the GIF picture viewer shown in Section5.8, “Example - Prepared Queries - Picture Viewer, Part 2 - View Pictures”. It also stores the images as `bytea` fields in the database, but rather than using prepared queries in binary mode, it uses normal text mode queries. In order to get the binary data through normal SQL queries intact, the commands pg_escape_bytea and pg_unescape_bytea are used. This version is generally less efficient that using binary prepared queries, and can be significantly slower.

### 註意 Note

pg_escape_bytea と pg_unescape_bytea は､pgtcl-ng 第1.5.2版と､pgin.tcl 第 2.2.0 版に追加された 拡張である｡この用例は､他の版や実装では動かない｡ `pg_escape_bytea` and `pg_unescape_bytea` are extensions added to pgtcl-ng version 1.5.2 and pgin.tcl version 2.2.0. This example will not work with other versions or implementations.

命令行上に識別子が与えられ､保存された絵画をその識別子で取出し表示しようと試みる｡ Given an identifier on the command line, it attempts to retrieve and display the stored picture with that identifier.

絵画をデータベースに挿入する為に使われた算譜は､ Section5.9, “Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures” を参照されたい｡ (同じ形式が使われているので､記述譜の Section5.7, “Example - Prepared Queries - Picture Viewer, Part 1 - Store Pictures” も､使用できる｡) See Section5.9, “Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures” for the program used to insert the pictures in the database. (Since the same schema is used, the script in Section5.7, “Example - Prepared Queries - Picture Viewer, Part 1 - Store Pictures” can also be used.)

この用例はwishの下で動き､tclshでは動かない｡ データベース接続の情報は環境を通して適用されることを仮定している｡ This example runs under wish, not tclsh. It also assumes database connection information is provided through the environment.

Example5.17.Bytea Escaping - View Pictures Stored in Database

    #!/usr/bin/wish
    # Example - picture storage as bytea, using escape/unescape:

    package require Pgtcl

    # Return the picture data identified by 'name'.
    # Throw an error if it can't be read.
    proc get_picture {conn name} {

        # Note that for a text-mode query on a BYTEA column, PostgreSQL will escape
        # the data for us.
        set result [pg_exec $conn "SELECT picture FROM pics\
                WHERE pname='[pg_escape_string $name]'"]
        if {[pg_result $result -status] != "PGRES_TUPLES_OK"} {
            set message [pg_result $result -error]
            pg_result $result -clear
            error "Error: Query failed: $message"
        }

        if {[set n [pg_result $result -numTuples]] != 1} {
            pg_result $result -clear
            error "Error: Query returned $n results"
        }
        # Unescape and return the data, which is the only column in the only row:
        set data [pg_unescape_bytea [lindex [pg_result $result -getTuple 0] 0]]
        pg_result $result -clear
        return $data
    }

    if {$argc != 1} {
        puts stderr "Usage: view_picture name"
        exit 1
    }
    set name [lindex $argv 0]

    # Connect to the database.
    set conn [pg_connect -conninfo ""]

    # Get the picture data:
    set failed [catch {get_picture $conn $name} data]

    # Done with database connection:
    pg_disconnect $conn

    # Exit if unable to retrieve the data:
    if {$failed} {
        puts "Failed to view picture '$name': $data"
        exit
    }

    # Make the viewer and show the picture:
    wm title . "Picture: $name"
    image create photo p -data $data -format GIF
    label .top -image p
    button .quit -text Close -command exit -default active
    bind . <Return> exit
    pack .top -side top
    pack .quit

Prev | Up | Next
5.9.Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures | Home | 5.11.Example - Asynchronous Queries