/****************************************************************
* This application is used by Visual JavaScript. *
* DO NOT ALTER THIS CODE, or the operation of Visual JavaScript *
* will be affected. See the Visual JavaScript documentation. *
* *
* Copyright 1997 Netscape Communications Corp. *
*****************************************************************/
function writeln(str) {
write(str + "\n")
}
function notSet( val ) {
if (val == null)
val = ""
if (val == "" )
return true
else
return false
}
function ConnectToDB(params) {
retstr = ""
with ( params ) {
if ( notSet(dbType) )
retstr +="Database Type Not Specified."
else
retstr += "
Database Type is " + dbType
if ( notSet(dbServer) )
retstr +="
Database Server Not Specified."
else
retstr += "
Database Server is " + dbServer
if ( notSet(userName) )
retstr +="
User Name Not Specified."
else
retstr += "
User Name is " + userName
if ( notSet(password) )
retstr +="
Password Not Specified."
else
retstr += "
Password is " + password
if ( notSet(dbName) )
retstr +="
Database Name Not Specified."
else
retstr += "
Database Name is " + dbName
if ( dbType.indexOf("ODBC") != -1 )
database.connect("ODBC", dbServer, userName, password, dbName)
else
database.connect(dbType, dbServer, userName, password, dbName)
} //with
if (!database.connected()) {
retstr+="
Error: Unable to connect to database."
retstr+="
Major Error Message: " + database.majorErrorMessage()
retstr+="
Minor Error Message: " + database.minorErrorMessage()
retstr+="
Major Error Code: " + database.majorErrorCode()
retstr+="
Minor Error Code: " + database.minorErrorCode()
} else
retstr = "" // If connect to DB, don't return err msg.
return retstr
} //ConnectToDB
function datatype( val ) {
if ( (dbType == "ODBC-SQL Anywhere") || (dbType == "ORACLE") )
return val
else if (dbType == "ODBC-SQL Server")
return ODBCdatatype[val]
else if (dbType == "SYBASE")
return sybaseDatatype[val]
else if (dbType == "INFORMIX") {
val = val % 256
if ( val == 0 )
return "char"
else if ( val == 1 )
return "smallint"
else if ( val == 2 )
return "integer"
else if ( val == 3 )
return "float"
else if ( val == 4 )
return "smallfloat"
else if ( val == 5 )
return "decimal"
else if ( val == 6 )
return "serial"
else if ( val == 7 )
return "date"
else if ( val == 8 )
return "money"
else if ( val == 10 )
return "datetime"
else if ( val == 11 )
return "byte"
else if ( val == 12 )
return "text"
else if ( val == 13 )
return "varchar"
else if ( val == 14 )
return "interval"
else if ( val == 15 )
return "nchar"
else if ( val == 16 )
return "nvarchar"
else if ( val > 16 )
return "unknown type"
} //else
} // datatype
/*
* Returns object that encapsulates mapping between unique table id and table name.
*/
function showTables(SQL) {
tables = database.cursor(SQL)
write("
Tables")
numTables = 0
t = new Object()
// First column in cursor is assumed to be table name; second is unique table id
while (tables.next()) {
write("" + tables[0] + " |
")
t[tables[1]] = tables[0]
numTables++
}
writeln("
")
return t
}
function nullable(val) {
if ( (dbType == "ODBC-SQL Anywhere") || (dbType == "ORACLE") )
return ( (val=="Y") ? "nullable" : "not null" )
else if (dbType == "ODBC-SQL Server")
return ( (val==8) ? "nullable" : "not null" )
else if (dbType == "INFORMIX")
return ( (val > 256) ? "not null" : "nullable" )
else if (dbType == "SYBASE")
return ( (val == 0) ? "not null" : "nullable" )
else
return "Unknown"
}
function showColumns(SQL, tableID) {
cols = database.cursor(SQL)
write("" + tableNames[tableID] + "")
/* For debugging:
writeln(" sql = " + SQL + "\n no cols = " + cols.columns())
cols.next(); writeln(cols[0])
writeln(cols[1])
writeln(cols[2])
*/
PK = primaryKey(tableID)
// First property of cursor is the column name, second indicates datatype, third indicates nullability
while (cols.next()) {
write("" + cols[0] + " | ")
write("" + datatype(cols[1]) + " | ")
write("" + nullable(cols[2]) + " | ")
write("" + isColPrimaryKey(cols[0]) + " | ")
write("
")
}
writeln("
")
}
/*
* Returns "primary key" if col is primary key, null string otherwise
*/
function isColPrimaryKey(colName) {
retVal = ""
if (PK != null) {
for (col in PK) {
if (colName == PK[col])
retVal = "Primary Key"
}
}
return retVal
}
/*
* Returns an object containing a property that is name of each key column for table; null if no keys
*/
function primaryKey(tableID) {
keyCols = null
if (dbType == "SYBASE") {
colNames = new Object() // Stores mapping between colid & colName
colsInTable = database.cursor("SELECT colid, name FROM syscolumns WHERE id = " + tableID)
while (colsInTable.next())
colNames[colsInTable.colid] = colsInTable.name
key = database.cursor("SELECT keycnt, key1, key2, key3, key4, key5, key6, key7, key8 FROM syskeys WHERE type = 1 AND id = " + tableID)
key.next()
if (key.keycnt != 0)
keyCols = new Object()
for (i = 0; i < key.keycnt; i++)
keyCols[i] = colNames[key[i+1]] // Each property is name of a key column
} else if (dbType == "INFORMIX") {
colNames = new Object() // Stores mapping between colid & colName
colsInTable = database.cursor("SELECT colno, colname FROM syscolumns where tabid = " + tableID)
while (colsInTable.next())
colNames[colsInTable.colno] = colsInTable.colname
key = database.cursor("SELECT * FROM sysindexes where idxtype = 'U' and tabid = " + tableID)
key.next()
for (i = 5; i < 21; i++) {
if (keyCols == null)
keyCols = new Object()
if (key[i] != 0) {
keyCols[i] = colNames[key[i]] // Each property is name of a key column
}
}
} else if (dbType == "ORACLE") {
debug("FOR TABLE " + tableID)
key = database.cursor("SELECT constraint_name from user_constraints where constraint_type='P' and table_name = '" + tableID + "'")
key.next()
debug("CONSTRAINT NAME IS " + key.constraint_name )
keyColsCursor = database.cursor("SELECT column_name from user_cons_columns where constraint_name='" + key.constraint_name + "'")
i = 0
while ( keyColsCursor.next() ){
if (keyCols == null)
keyCols = new Object()
keyCols[i] = keyColsCursor.column_name
debug("COLUMN NAME IS " + keyColsCursor.column_name)
i++
}
} else {
// TBD
}
return keyCols
} //primaryKey
//--------------------- Connect to DB ----------------------------------------
write( ConnectToDB(request) )
dbType = request.dbType
// --------------------------- INFORMIX --------------------------------------
if (dbType == "INFORMIX" ) {
tableNames = showTables("select tabname, tabid from systables WHERE tabid > 99")
for ( id in tableNames )
showColumns("select colname, coltype, coltype from syscolumns where tabid = " + id, id)
// --------------------------- ORACLE -----------------------------------------
} else if (dbType == "ORACLE") {
tableNames = showTables("SELECT TABLE_NAME, TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME NOT LIKE 'DEF$_%'")
for ( tname in tableNames )
showColumns("SELECT COLUMN_NAME, DATA_TYPE, NULLABLE FROM USER_TAB_COLUMNS WHERE TABLE_NAME='" + tname +"'", tname)
// --------------------------- SYBASE ------------------------------------------
} else if (dbType == "SYBASE") {
tableNames = showTables("select name, id from sysobjects WHERE type='U'")
sybaseDatatype = new Object() // Store Datatype mappings
dt = database.cursor("select name, usertype from systypes")
while (dt.next()) {
sybaseDatatype[dt.usertype] = dt.name
}
for ( id in tableNames ) {
showColumns("select name, usertype, status from syscolumns where id=" + id, id)
}
// --------------------------- DB2 ----------------------------------------------
} else if (dbType == "DB2") {
write("Metadata retrieval not currently supported for " + dbType)
// --------------------------- ODBC SQL Anywhere --------------------------------
} else if (dbType == "ODBC-SQL Anywhere") {
tableNames = showTables("select name, id from sysobjects where type='U'")
for ( id in tableNames ) {
showColumns("select cname, coltype, nulls from syscolumns where tname='" + tableNames[id]+"'", id)
}
// --------------------------- ODBC SQL SERVER ----------------------------------
} else if (dbType == "ODBC-SQL Server") {
tableNames = showTables("select name, id from sysobjects WHERE type='U' and category=0")
ODBCdatatype= new Object() // Store Datatype mappings in ODBCdatatype object
dtypes = database.cursor("select type, name from systypes")
while (dtypes.next()) {
ODBCdatatype[dtypes.type] = dtypes.name
}
for ( id in tableNames ) {
showColumns("select name, type, status from syscolumns where id=" + id, id)
}
// --------------------------- ODBC Access --------------------------------
} else if (dbType == "ODBC-Access") {
write("
Metadata retrieval not currently supported for " + dbType)
} else
write("
Unknown database type.")