當前位置: 首頁>>代碼示例>>Golang>>正文


Golang DbConnection.Exec方法代碼示例

本文整理匯總了Golang中github.com/jrbudnack/cf-sql-broker/dbconnection/baseDbConnection.DbConnection.Exec方法的典型用法代碼示例。如果您正苦於以下問題:Golang DbConnection.Exec方法的具體用法?Golang DbConnection.Exec怎麽用?Golang DbConnection.Exec使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/jrbudnack/cf-sql-broker/dbconnection/baseDbConnection.DbConnection的用法示例。


在下文中一共展示了DbConnection.Exec方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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

示例2: CreateUser

func (pgDbc PostgresDbConnection) CreateUser(userName string, password string, dbConn *baseDbConnection.DbConnection) error {
	_, err := dbConn.Exec("CREATE USER " + userName + " WITH PASSWORD '" + password + "'")

	if err == nil {
		_, err = dbConn.Exec("GRANT ALL PRIVILEGES ON DATABASE " + dbConn.DBName + " TO " + userName)
	}
	if err == nil {
		_, err = dbConn.Exec("GRANT ALL PRIVILEGES ON SCHEMA public TO " + userName)
	}

	return err
}
開發者ID:jrbudnack,項目名稱:cf-sql-broker,代碼行數:12,代碼來源:postgresdbconnection.go

示例3: DropUser

func (pgDbc PostgresDbConnection) DropUser(userName string, dbConn *baseDbConnection.DbConnection) error {
	_, err := dbConn.Exec("REVOKE ALL PRIVILEGES ON SCHEMA public FROM " + userName)

	if err == nil {
		_, err = dbConn.Exec("REVOKE ALL PRIVILEGES ON DATABASE " + dbConn.DBName + " FROM " + userName)
	}
	if err == nil {
		//RISK ITEM:  If the user owns any objects, this will fail.
		//HOW DO WE WANT TO HANDLE THIS?
		_, err = dbConn.Exec("DROP USER IF EXISTS " + userName)
	}

	return err
}
開發者ID:jrbudnack,項目名稱:cf-sql-broker,代碼行數:14,代碼來源:postgresdbconnection.go

示例4: DropDatabase

func (pgDbc PostgresDbConnection) DropDatabase(databaseName string, dbConn *baseDbConnection.DbConnection) error {
	_, err := dbConn.Exec("DROP DATABASE " + databaseName)
	return err
}
開發者ID:jrbudnack,項目名稱:cf-sql-broker,代碼行數:4,代碼來源:postgresdbconnection.go

示例5: CreateDatabase

func (pgDbc PostgresDbConnection) CreateDatabase(databaseName string, dbConn *baseDbConnection.DbConnection) error {
	_, err := dbConn.Exec("CREATE DATABASE " + databaseName)
	return err
}
開發者ID:jrbudnack,項目名稱:cf-sql-broker,代碼行數:4,代碼來源:postgresdbconnection.go


注:本文中的github.com/jrbudnack/cf-sql-broker/dbconnection/baseDbConnection.DbConnection.Exec方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。