Welcome, guest | Sign In | My Account | Store | Cart

This tcl script, called from a windows bat file generates java structure definitions for DB2 tables using the db2 db2dclgn and describe utilities.

Output is sent to two files.

*.txt

452 CHARACTER 1 ACCESS_STATUS 13 485 DECIMAL 8, 0 CUSTOMER 8

*.java

java.lang.String access_status; java.math.BigDecimal customer;

Tcl, 69 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
echo off

echo **********************************************************
echo * call tcl script from within windows DB2 shell.
echo ********************************************************** 

set tclshell=c:\tcl\bin\tclsh84

echo db2cmd /w /i /c %tclshell% c:\scripts\tcl\dclgendb.tcl
db2cmd /w /i /c %tclshell% c:\scripts\tcl\dclgendb.tcl

-------------------------------------------------------------------

tcl script:

#!d:\\Tcl\\bin\\tclsh84

######################################
# generate describe reports.
######################################

proc dclgen {schema userid password database outputdir} {

    exec db2 connect to $database user $userid using $password

    set tables [split [exec db2 list tables for schema $schema] \n]

    set schema [string toupper $schema] 
    set spaces_     {\s+} 
    set characters_ {\S+} 
    set regexp_string ($characters_)($spaces_)($schema)

    foreach line $tables {

        if {[regexp $regexp_string $line match tablename spaces schema_name] == 1} {
	    puts "outputfile = $outputdir\\$tablename\.java"
	    exec db2dclgn -d $database -u $userid -p $password -t $schema.$tablename -l java -a replace -o $outputdir\\$tablename\.java
	    puts "outputfile = $outputdir\\$tablename\.txt"
            exec db2 describe "select * from $schema\.$tablename" >& $outputdir\\$tablename\.txt
	} else {
	    set continue true
	}
    }

    exec db2 terminate
}

######################################
# generate describe reports for db_one.
######################################

set userid     user1
set password   pass1

set database   db_one 
set schema     schema1
set outputdir  c:\\db2_reports\\db_one\\describe

dclgen $schema $userid $password $database $outputdir

######################################
# generate describe reports for db_two.
######################################

set database   db_two 
set schema     schema2
set outputdir  d:\\db2_reports\\db_two\\describe

dclgen $schema $userid $password $database $outputdir
Created by Patrick Finnegan on Wed, 18 Jun 2003 (MIT)
Tcl recipes (162)
Patrick Finnegan's recipes (56)

Required Modules

  • (none specified)

Other Information and Tasks