当前位置: 首页>>代码示例>>Golang>>正文


Golang DbConnection.GetTableDefinition方法代码示例

本文整理汇总了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.")
	}
}
开发者ID:jrbudnack,项目名称:cf-sql-broker,代码行数:36,代码来源:postgresdbconnection.go


注:本文中的github.com/jrbudnack/cf-sql-broker/dbconnection/baseDbConnection.DbConnection.GetTableDefinition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。