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


Golang DbConnection.Query方法代碼示例

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


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

示例1: UserExists

func (pgDbc PostgresDbConnection) UserExists(userName string, dbConn *baseDbConnection.DbConnection) (bool, error) {
	queryResults, err := dbConn.Query("SELECT 1 FROM pg_roles WHERE rolname = '" + userName + "'")

	if err != nil {
		return false, err
	}

	users := baseDbConnection.ParseResults(queryResults)
	for _ = range users {
		return true, nil
	}
	return false, nil
}
開發者ID:jrbudnack,項目名稱:cf-sql-broker,代碼行數:13,代碼來源:postgresdbconnection.go

示例2: DbExists

func (pgDbc PostgresDbConnection) DbExists(databaseName string, dbConn *baseDbConnection.DbConnection) (bool, error) {
	queryResults, err := dbConn.Query("SELECT 1 FROM pg_database WHERE datname = '" + databaseName + "'")

	if err != nil {
		return false, err
	}

	databases := baseDbConnection.ParseResults(queryResults)

	for _ = range databases {
		return true, nil
	}
	return false, nil
}
開發者ID:jrbudnack,項目名稱:cf-sql-broker,代碼行數:14,代碼來源:postgresdbconnection.go

示例3: GetTableDefinition

func (pgDbc PostgresDbConnection) GetTableDefinition(tableName string, dbConn *baseDbConnection.DbConnection) (baseDbConnection.TableDefinition, error) {
	// In PostgreSQL, we query the system tables in information_schema to get the list
	// of tables and their columns.
	sqlGetTable := `
SELECT c.table_name, c.column_name, c.ordinal_position,
c.data_type, CASE WHEN primary_key = 1 THEN TRUE ELSE FALSE END AS primary_key,
c.column_default
FROM information_schema.columns c LEFT OUTER JOIN
(SELECT kcu.table_name, kcu.column_name, 1 AS primary_key
FROM information_schema.key_column_usage kcu INNER JOIN
information_schema.table_constraints tc ON
tc.table_catalog = kcu.table_catalog AND
tc.table_schema = kcu.table_schema AND
tc.table_name = kcu.table_name
WHERE tc.constraint_type = 'PRIMARY KEY') pk
ON pk.table_name = c.table_name AND
pk.column_name = c.column_name
WHERE c.table_catalog = '` + dbConn.DBName + `' AND
c.table_schema = '` + dbConn.Schema + `' AND c.table_name = '` + tableName + `'
ORDER BY c.table_name, c.ordinal_position, c.column_name`

	tableDefinition := baseDbConnection.TableDefinition{}
	tableDefinition.Columns = make([]baseDbConnection.ColumnDefinition, 0)

	queryResults, err := dbConn.Query(sqlGetTable)
	tableColumns := baseDbConnection.ParseResults(queryResults)

	if err != nil {
		return baseDbConnection.TableDefinition{}, err
	}

	for _, tableColumn := range tableColumns {
		tableDefinition.Name = tableColumn["table_name"].(string)
		newColumn :=
			baseDbConnection.ColumnDefinition{
				tableColumn["column_name"].(string),
				tableColumn["data_type"].(string),
				false,
				tableColumn["column_default"].(string),
			}
		if tableColumn["primary_key"].(bool) {
			newColumn.PrimaryKey = true
		}
		tableDefinition.Columns = append(tableDefinition.Columns, newColumn)
	}

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


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