^ Linux - Friheden til at skrive dokumentation: Version 2.7.20040524 - 2020-12-31 ^|
| [[dokumentation:c1211|forrige]] | Kapitel 3. DocBook i XML : Ett konkret exempel | [[dokumentation:x1300|næste]] |
----
==== 3.2. Skaffa fram rådatat ====
Vi utgår från att vi har PostgreSQL körande, med möjlighet att koppla upp via TCP/IP. Vi har endast 2 tabeller tills vidare, skapade så här:
create table master (
m_id integer,
some_data char(20),
primary key (m_id));
create table slave (
m_id integer,
s_id integer,
some_other_data char(30),
primary key (m_id, s_id),
foreign key (m_id) references master (m_id));
Vi ska nu få till en fil med detta useende:
.
.
Vi bryr oss inte om relationerna i detta exempel. Ett sätt att få fram detta utseende är att köra detta tcl-skript. Vi kör det genom att skicka stdout tilltables.xml. Databasen vi skall dokumentera heter bnl. Se till att skriptet är exekverbart.
[bnl@della source]$make_tables.tcl bnl > tables.xml
# ExtractDefinition :
# Get the description of tables in database provided
# as first argument to the script, in XML format, to stdout
# Björn Lundin 2003-06-21
#!/bin/sh
# \
exec pgtclsh $0 $*
proc getFields {Connection The_Table} {
set Fields {}
set res [pg_exec $Connection \
"SELECT a.attname, \
pg_catalog.format_type(a.atttypid, a.atttypmod), \
a.attnotnull, a.atthasdef, a.attnum \
FROM pg_catalog.pg_attribute a \
WHERE a.attrelid = (select relfilenode from pg_catalog.pg_class \
where relname = '$The_Table') \
AND a.attnum > 0 AND NOT a.attisdropped \
ORDER BY a.attnum"]
set ntups [pg_result $res -numTuples]
for {set i 0} {$i < $ntups} {incr i} {
lappend Fielddata [pg_result $res -getTuple $i]
}
puts " "
foreach Field $Fielddata {
puts " "
puts " [lindex $Field 0]"
puts " [lindex $Field 1]"
puts " [lindex $Field 2]"
puts " [lindex $Field 3]"
puts " [lindex $Field 4]"
puts " "
}
puts " "
pg_result $res -clear
}
proc getKeys {Connection The_Table} {
set Fields {}
set res [pg_exec $Connection \
"SELECT c2.relname, i.indisprimary, i.indisunique, \
pg_catalog.pg_get_indexdef(i.indexrelid) \
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i \
WHERE c.oid = (select relfilenode from pg_catalog.pg_class \
where relname = '$The_Table') \
AND c.oid = i.indrelid AND i.indexrelid = c2.oid \
ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname"]
set ntups [pg_result $res -numTuples]
for {set i 0} {$i < $ntups} {incr i} {
lappend Keydata [pg_result $res -getTuple $i]
}
puts " "
foreach Key $Keydata {
puts " "
puts " [lindex $Key 0]"
puts " [lindex $Key 1]"
puts " [lindex $Key 2]"
puts " [lindex $Key 3]"
puts " "
}
puts " "
pg_result $res -clear
}
proc getRelations {Connection The_Table} {
puts " "
puts " "
}
proc ExtractDefinition { db {host "localhost"} {port "5432"} } {
set conn [pg_connect $db -host $host -port $port]
#Put all tables in list
set res [pg_exec $conn \
"SELECT c.relname as Name \
FROM pg_catalog.pg_class c \
LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner \
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
WHERE c.relkind ='r' \
AND n.nspname NOT IN ('pg_catalog', 'pg_toast') \
AND pg_catalog.pg_table_is_visible(c.oid) \
ORDER BY c.relname"]
set ntups [pg_result $res -numTuples]
for {set i 0} {$i < $ntups} {incr i} {
lappend Tables [pg_result $res -getTuple $i]
}
pg_result $res -clear
puts ""
foreach Table $Tables {
puts " "
puts " $Table"
#For each table put keys
getKeys $conn $Table
#For each table put relations (implemented as dummy)
getRelations $conn $Table
#For each table put fields
getFields $conn $Table
puts "
"
}
puts ""
pg_disconnect $conn
return ""
}
#Main
ExtractDefinition [lindex $argv 0]
----
| [[dokumentation:c1211|forrige]] | [[dokumentation:index|hjem]] | [[dokumentation:x1300|næste]] |
| DocBook i XML : Ett konkret exempel | [[dokumentation:c1211|op]] | Transformera till DocBook |