本文整理匯總了Golang中github.com/jrbudnack/cf-sql-broker/dbconnection/baseDbConnection.DbConnection.GetTableDefinition方法的典型用法代碼示例。如果您正苦於以下問題:Golang DbConnection.GetTableDefinition方法的具體用法?Golang DbConnection.GetTableDefinition怎麽用?Golang DbConnection.GetTableDefinition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/jrbudnack/cf-sql-broker/dbconnection/baseDbConnection.DbConnection
的用法示例。
在下文中一共展示了DbConnection.GetTableDefinition方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateTable
func (pgDbc PostgresDbConnection) CreateTable(table baseDbConnection.TableDefinition, dbConn *baseDbConnection.DbConnection) error {
existingTable, err := dbConn.GetTableDefinition(table.Name)
if err != nil {
return err
}
if existingTable.Name != table.Name {
sqlCreateTable := "CREATE TABLE IF NOT EXISTS " + dbConn.Schema + "." + table.Name + " (\n"
fields := make([]string, 0)
primaryKey := make([]string, 0)
for _, tableColumn := range table.Columns {
fieldDef := "\"" + tableColumn.Name + "\" " + tableColumn.Type
if tableColumn.Default != "" {
fieldDef += " DEFAULT " + tableColumn.Default
}
fields = append(fields, fieldDef)
if tableColumn.PrimaryKey {
primaryKey = append(primaryKey, tableColumn.Name)
}
}
fields = append(fields, "CONSTRAINT pk_"+existingTable.Name+" PRIMARY KEY ("+strings.Join(primaryKey, ",")+")")
sqlCreateTable = sqlCreateTable + strings.Join(fields, "\n,")
sqlCreateTable = sqlCreateTable + ") WITH (OIDS = FALSE);\n"
//TODO: Default Table Grants
//TODO: Indexes
//TODO: Partitioning
_, err := dbConn.Exec(sqlCreateTable)
return err
} else {
return errors.New("Table already exists.")
}
}