5.5.Example - Large Objects - Picture Viewer, Part 1 - Store Pictures
Prev | Chapter5.Example Programs | Next

## 5.5.Example - Large Objects - Picture Viewer, Part 1 - Store Pictures

この用例は､GIF絵画観察者の第一部で､映像を巨大対象としてデータベースに保存する｡ 命令行上に､識別子とファイル名の対が与えられ､それぞれのファイルの内容を巨大対象として データベースに挿入し､そして､ 提供された識別子を鍵として使って巨大対象の取出しを許容する為に､ 記録を表に挿入する｡ This example is the first part of a GIF picture viewer which stores the images as large objects in the database. Given pairs of identifiers (arbitrary names used as keys) and filenames on the command line, it inserts the contents of each file into the database as a large object, and inserts a record in a table to allow retrieving the large object using the provided identifier as the key.

絵画を見る為に使われる算譜については､Section5.6, “Example - Large Objects - Picture Viewer, Part 2 - View Pictures”を参照されたい｡ 巨大対象の代りに､可変長のバイナリ列を使う別の実装は Section5.7, “Example - Prepared Queries - Picture Viewer, Part 1 - Store Pictures”. と Section5.9, “Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures” にある｡ See Section5.6, “Example - Large Objects - Picture Viewer, Part 2 - View Pictures” for the program used to view the pictures. A different implementation using `bytea` fields instead of large objects can be found in Section5.7, “Example - Prepared Queries - Picture Viewer, Part 1 - Store Pictures”. and Section5.9, “Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures”.

もし表が見付からないと､概要が自動的に生成される｡この方法は､製品には通常使われない｡ 用例は､データベース接続情報は環境を通じて提供されることを仮定している｡ The schema is created automatically if the table is not found. This method wouldn't normally be used in production, though. The example assumes database connection information is provided through the environment.

Example5.12.Large Objects - Store Pictures in Database

    #!/usr/bin/tclsh
    # Example - picture storage as Large Object - importer

    package require Pgtcl

    # Build the table schema:
    proc build {conn} {
        pg_execute $conn "CREATE TABLE pics (pname TEXT PRIMARY KEY, poid OID)"
    }

    # 鍵が 'name'のデータベースにファイル 'file' を挿入:
    # 不都合が生じると Tcl 不都合を発す｡
    # Insert file 'file' into the database with key 'name':
    # If an error occurs, throws a Tcl error.
    proc insert_file {conn name file } {
        set qname [pg_escape_string $name]
        pg_execute $conn BEGIN
        if {[catch {
            set oid [pg_lo_import $conn $file]
            pg_execute $conn "INSERT INTO pics (poid, pname) VALUES ($oid, '$qname')"
        } msg]} {
            pg_execute $conn ROLLBACK
            error "Error importing large object: $msg"
        }
        pg_execute $conn COMMIT
    }

    if {$argc < 2 || $argc % 2 != 0} {
        puts stderr "Usage: insert_picture name filename \[name filename\]..."
        exit 1
    }

    # データベースに接続する.
    # Connect to the database.
    set conn [pg_connect -conninfo ""]

    # 表の確認:
    # Check for table:
    if {[catch {pg_execute $conn "SELECT COUNT(*) AS n FROM pics"} msg]} {
         puts "Note: unable to select from table. Let's try creating it."
         build $conn
    }

    # 命令行上で名付けられた全絵画を挿入する:
    # Insert all the pictures named on the command line:
    foreach {name filename} $argv {
        if {[catch {insert_file $conn $name $filename} message]} {
            puts "$filename NOT inserted: $message"
        } else {
            puts "$filename inserted OK as '$name'"
        }
    }

    pg_disconnect $conn

Prev | Up | Next
5.4.Example - The Different Ways to Get Query Results | Home | 5.6.Example - Large Objects - Picture Viewer, Part 2 - View Pictures