スキーマ検出
次のセクションでは、スキーマ情報の取得方法について説明します。
テーブルとビューのリスト
sql パッケージはテーブルやビューをクエリするネイティブインタフェースを提供しませんが、本製品 が提供するsys_tables システムテーブルを使用できます。
tables, _ := db.Query("SELECT CatalogName, SchemaName, TableName FROM sys_tables WHERE TableType='TABLE'")
defer tables.Close()
for tables.Next() {
var (
catalog string
schema string
table string
)
rows.Scan(&catalog, &schema, &table)
fmt.Printf("Catalog: %s, Schema: %s, Table: %s", catalog, schema, table)
}
views, _ := db.Query("SELECT CatalogName, SchemaName, TableName FROM sys_tables WHERE TableType='VIEW'")
defer views.Close()
for views.Next() {
var (
catalog string
schema string
view string
)
rows.Scan(&catalog, &schema, &view)
fmt.Printf("Catalog: %s, Schema: %s, Table: %s", catalog, schema, table)
}
テーブルカラムのリスト
sys_tablecolumns を使用して、カラムに関する情報を取得できます。
columns, _ := db.Query("SELECT ColumnName, DataType, Length, NumericPrecision, IsNullable FROM sys_tablecolumns WHERE TableName = 'Dataset_Opportunity'")
defer columns.Close()
for columns.Next() {
var (
column string
datatype int
length int
precision int
nullable bool
)
rows.Scan(&column, &datatype, &length, &precision, &nullable)
fmt.Printf("Name: %s, Type: %d, Length: %d, Precision: %d, Nullable: %t\n", column, datatype, length, precision, nullable)
}