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


Golang pq.QuoteIdentifier函数代码示例

本文整理汇总了Golang中github.com/lib/pq.QuoteIdentifier函数的典型用法代码示例。如果您正苦于以下问题:Golang QuoteIdentifier函数的具体用法?Golang QuoteIdentifier怎么用?Golang QuoteIdentifier使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了QuoteIdentifier函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Grants

// Grants returns a list of SQL queries that constitute the privileges specified
// in the receiver for the target schema.
func (s Schema) Grants(target string) []string {
	const maxQueries = 2
	queries := make([]string, 0, maxQueries)

	if s.GetPrivilege(Create) {
		b := bytes.NewBufferString("GRANT CREATE ON SCHEMA ")
		fmt.Fprint(b, pq.QuoteIdentifier(target), " TO ", quoteRole(s.Role))

		if s.GetGrantOption(Create) {
			fmt.Fprint(b, " WITH GRANT OPTION")
		}

		queries = append(queries, b.String())
	}

	if s.GetPrivilege(Usage) {
		b := bytes.NewBufferString("GRANT USAGE ON SCHEMA ")
		fmt.Fprint(b, pq.QuoteIdentifier(target), " TO ", quoteRole(s.Role))

		if s.GetGrantOption(Usage) {
			fmt.Fprint(b, " WITH GRANT OPTION")
		}

		queries = append(queries, b.String())
	}

	return queries
}
开发者ID:hashicorp,项目名称:terraform,代码行数:30,代码来源:schema.go

示例2: Revokes

// Revokes returns a list of SQL queries that remove the privileges specified
// in the receiver from the target schema.
func (s Schema) Revokes(target string) []string {
	const maxQueries = 2
	queries := make([]string, 0, maxQueries)

	if s.GetPrivilege(Create) {
		b := bytes.NewBufferString("REVOKE")
		if s.GetGrantOption(Create) {
			fmt.Fprint(b, " GRANT OPTION FOR")
		}

		fmt.Fprint(b, " CREATE ON SCHEMA ")
		fmt.Fprint(b, pq.QuoteIdentifier(target), " FROM ", quoteRole(s.Role))
		queries = append(queries, b.String())
	}

	if s.GetPrivilege(Usage) {
		b := bytes.NewBufferString("REVOKE")
		if s.GetGrantOption(Usage) {
			fmt.Fprint(b, " GRANT OPTION FOR")
		}

		fmt.Fprint(b, " USAGE ON SCHEMA ")
		fmt.Fprint(b, pq.QuoteIdentifier(target), " FROM ", quoteRole(s.Role))
		queries = append(queries, b.String())
	}

	return queries
}
开发者ID:hashicorp,项目名称:terraform,代码行数:30,代码来源:schema.go

示例3: resourcePostgresqlDatabaseCreate

func resourcePostgresqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*Client)
	conn, err := client.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()

	dbName := d.Get("name").(string)
	dbOwner := d.Get("owner").(string)
	connUsername := client.username

	var dbOwnerCfg string
	if dbOwner != "" {
		dbOwnerCfg = fmt.Sprintf("WITH OWNER=%s", pq.QuoteIdentifier(dbOwner))
	} else {
		dbOwnerCfg = ""
	}

	//needed in order to set the owner of the db if the connection user is not a superuser
	err = grantRoleMembership(conn, dbOwner, connUsername)
	if err != nil {
		return err
	}

	query := fmt.Sprintf("CREATE DATABASE %s %s", pq.QuoteIdentifier(dbName), dbOwnerCfg)
	_, err = conn.Query(query)
	if err != nil {
		return fmt.Errorf("Error creating postgresql database %s: %s", dbName, err)
	}

	d.SetId(dbName)

	return nil
}
开发者ID:AssertSelenium,项目名称:terraform,代码行数:35,代码来源:resource_postgresql_database.go

示例4: resourcePostgreSQLExtensionCreate

func resourcePostgreSQLExtensionCreate(d *schema.ResourceData, meta interface{}) error {
	c := meta.(*Client)
	c.catalogLock.Lock()
	defer c.catalogLock.Unlock()

	conn, err := c.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()

	extName := d.Get(extNameAttr).(string)

	b := bytes.NewBufferString("CREATE EXTENSION ")
	fmt.Fprint(b, pq.QuoteIdentifier(extName))

	if v, ok := d.GetOk(extSchemaAttr); ok {
		fmt.Fprint(b, " SCHEMA ", pq.QuoteIdentifier(v.(string)))
	}

	if v, ok := d.GetOk(extVersionAttr); ok {
		fmt.Fprint(b, " VERSION ", pq.QuoteIdentifier(v.(string)))
	}

	query := b.String()
	_, err = conn.Query(query)
	if err != nil {
		return errwrap.Wrapf("Error creating extension: {{err}}", err)
	}

	d.SetId(extName)

	return resourcePostgreSQLExtensionReadImpl(d, meta)
}
开发者ID:hashicorp,项目名称:terraform,代码行数:34,代码来源:resource_postgresql_extension.go

示例5: quotedMigrationTable

func (config *Config) quotedMigrationTable() string {
	if !strings.Contains(config.MigrationTable, ".") {
		return pq.QuoteIdentifier(config.MigrationTable)
	}

	tokens := strings.SplitN(config.MigrationTable, ".", 2)
	return pq.QuoteIdentifier(tokens[0]) + "." + pq.QuoteIdentifier(tokens[1])
}
开发者ID:magicalbanana,项目名称:pgmgr,代码行数:8,代码来源:config.go

示例6: NewKeyValue

// Create a new key/value
func NewKeyValue(host *Host, name string) (*KeyValue, error) {
	kv := &KeyValue{host, name}
	query := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (attr hstore)", pq.QuoteIdentifier(kvPrefix+kv.table))
	if _, err := kv.host.db.Exec(query); err != nil {
		if !strings.HasSuffix(err.Error(), "already exists") {
			return nil, err
		}
	}
	if Verbose {
		log.Println("Created HSTORE table " + pq.QuoteIdentifier(kvPrefix+kv.table) + " in database " + host.dbname)
	}
	return kv, nil
}
开发者ID:xyproto,项目名称:simplehstore,代码行数:14,代码来源:simplehstore.go

示例7: SchemaInit

func SchemaInit(dbh *sql.DB, schema string) error {
	if schema == "" {
		schema = "request_dump"
	}
	if strings.Index(schema, " ") >= 0 {
		return fmt.Errorf("schemas containing a space are not supported")
	}

	// initialize schema where request data will be stored
	exists, err := SchemaExists(dbh, schema)
	if err != nil {
		return err
	}
	if exists == false {
		log.Printf("pg.SchemaInit: creating schema [%s]\n", schema)
		_, err := dbh.Exec(fmt.Sprintf("CREATE SCHEMA %s", pq.QuoteIdentifier(schema)))
		if err != nil {
			return fmt.Errorf("pg.SchemaInit: %s", err)
		}
	}

	table := "raw_requests"
	exists, err = TableExistsInSchema(dbh, table, schema)
	if err != nil {
		return err
	}
	if exists == false {
		log.Printf("pg.SchemaInit: creating table [%s.%s]\n", schema, table)
		ddls := []string{
			fmt.Sprintf(`
				CREATE TABLE %s.%s (
					request_id bigserial primary key,
					head       text,
					data       text,
					"when"     timestamptz,
					batch_id   bigint
				)
			`, pq.QuoteIdentifier(schema), table),
			fmt.Sprintf("CREATE INDEX raw_requests_batch_id_idx ON %s.%s (batch_id)",
				schema, table),
		}
		for _, ddl := range ddls {
			_, err := dbh.Exec(ddl)
			if err != nil {
				return fmt.Errorf("pg.SchemaInit: %s", err)
			}
		}
	}

	return nil
}
开发者ID:SparkPost,项目名称:relaymsgdb,代码行数:51,代码来源:pg.go

示例8: resourcePostgresqlRoleUpdate

func resourcePostgresqlRoleUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*Client)
	conn, err := client.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()

	d.Partial(true)

	roleName := d.Get("name").(string)

	if d.HasChange("login") {
		loginAttr := getLoginStr(d.Get("login").(bool))
		query := fmt.Sprintf("ALTER ROLE %s %s", pq.QuoteIdentifier(roleName), pq.QuoteIdentifier(loginAttr))
		_, err := conn.Query(query)
		if err != nil {
			return fmt.Errorf("Error updating login attribute for role: %s", err)
		}

		d.SetPartial("login")
	}

	password := d.Get("password").(string)
	if d.HasChange("password") {
		encryptedCfg := getEncryptedStr(d.Get("encrypted").(bool))

		query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password)
		_, err := conn.Query(query)
		if err != nil {
			return fmt.Errorf("Error updating password attribute for role: %s", err)
		}

		d.SetPartial("password")
	}

	if d.HasChange("encrypted") {
		encryptedCfg := getEncryptedStr(d.Get("encrypted").(bool))

		query := fmt.Sprintf("ALTER ROLE %s %s PASSWORD '%s'", pq.QuoteIdentifier(roleName), encryptedCfg, password)
		_, err := conn.Query(query)
		if err != nil {
			return fmt.Errorf("Error updating encrypted attribute for role: %s", err)
		}

		d.SetPartial("encrypted")
	}

	d.Partial(false)
	return resourcePostgresqlRoleRead(d, meta)
}
开发者ID:AssertSelenium,项目名称:terraform,代码行数:51,代码来源:resource_postgresql_role.go

示例9: grantRoleMembership

func grantRoleMembership(conn *sql.DB, dbOwner string, connUsername string) error {
	if dbOwner != "" && dbOwner != connUsername {
		query := fmt.Sprintf("GRANT %s TO %s", pq.QuoteIdentifier(dbOwner), pq.QuoteIdentifier(connUsername))
		_, err := conn.Query(query)
		if err != nil {
			//is already member or role
			if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
				return nil
			}
			return fmt.Errorf("Error granting membership: %s", err)
		}
	}
	return nil
}
开发者ID:AssertSelenium,项目名称:terraform,代码行数:14,代码来源:resource_postgresql_database.go

示例10: resourcePostgresqlDatabaseDelete

func resourcePostgresqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*Client)
	conn, err := client.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()

	dbName := d.Get("name").(string)
	connUsername := client.username
	dbOwner := d.Get("owner").(string)
	//needed in order to set the owner of the db if the connection user is not a superuser
	err = grantRoleMembership(conn, dbOwner, connUsername)
	if err != nil {
		return err
	}

	query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName))
	_, err = conn.Query(query)
	if err != nil {
		return err
	}

	d.SetId("")

	return nil
}
开发者ID:AssertSelenium,项目名称:terraform,代码行数:27,代码来源:resource_postgresql_database.go

示例11: createSchemaUnlessExists

func createSchemaUnlessExists(c *Config, db *sql.DB) error {
	// If there's no schema name in the config, we don't need to create the schema.
	if !strings.Contains(c.MigrationTable, ".") {
		return nil
	}

	var exists bool

	schema := strings.SplitN(c.MigrationTable, ".", 2)[0]
	err := db.QueryRow(
		`SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = $1)`,
		schema,
	).Scan(&exists)

	if err != nil {
		return err
	}

	if exists {
		return nil
	}

	_, err = db.Exec(fmt.Sprintf(
		"CREATE SCHEMA %s;",
		pq.QuoteIdentifier(schema),
	))
	return err
}
开发者ID:magicalbanana,项目名称:pgmgr,代码行数:28,代码来源:pgmgr.go

示例12: GetRemoteIdentities

func (r *userRepo) GetRemoteIdentities(tx repo.Transaction, userID string) ([]user.RemoteIdentity, error) {
	ex := r.executor(tx)
	if userID == "" {
		return nil, user.ErrorInvalidID
	}

	qt := pq.QuoteIdentifier(remoteIdentityMappingTableName)
	rims, err := ex.Select(&remoteIdentityMappingModel{},
		fmt.Sprintf("select * from %s where user_id = $1", qt), userID)

	if err != nil {
		if err != sql.ErrNoRows {
			return nil, err
		}
		return nil, err
	}
	if len(rims) == 0 {
		return nil, nil
	}

	var ris []user.RemoteIdentity
	for _, m := range rims {
		rim, ok := m.(*remoteIdentityMappingModel)
		if !ok {
			return nil, errors.New("unrecognized model")
		}

		ris = append(ris, user.RemoteIdentity{
			ID:          rim.RemoteID,
			ConnectorID: rim.ConnectorID,
		})
	}

	return ris, nil
}
开发者ID:no2key,项目名称:dex,代码行数:35,代码来源:user.go

示例13: secretCredsRevoke

func (b *backend) secretCredsRevoke(
	req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
	// Get the username from the internal data
	usernameRaw, ok := req.Secret.InternalData["username"]
	if !ok {
		return nil, fmt.Errorf("secret is missing username internal data")
	}
	username, ok := usernameRaw.(string)

	// Get our connection
	db, err := b.DB(req.Storage)
	if err != nil {
		return nil, err
	}

	// Drop this user
	stmt, err := db.Prepare(fmt.Sprintf(
		"DROP ROLE IF EXISTS %s;", pq.QuoteIdentifier(username)))
	if err != nil {
		return nil, err
	}
	defer stmt.Close()
	if _, err := stmt.Exec(); err != nil {
		return nil, err
	}

	return nil, nil
}
开发者ID:beornf,项目名称:vault,代码行数:28,代码来源:secret_creds.go

示例14: resourcePostgreSQLSchemaDelete

func resourcePostgreSQLSchemaDelete(d *schema.ResourceData, meta interface{}) error {
	c := meta.(*Client)
	c.catalogLock.Lock()
	defer c.catalogLock.Unlock()

	conn, err := c.Connect()
	if err != nil {
		return err
	}
	defer conn.Close()

	txn, err := conn.Begin()
	if err != nil {
		return err
	}
	defer txn.Rollback()

	schemaName := d.Get(schemaNameAttr).(string)

	// NOTE([email protected]): Deliberately not performing a cascading drop.
	query := fmt.Sprintf("DROP SCHEMA %s", pq.QuoteIdentifier(schemaName))
	_, err = txn.Query(query)
	if err != nil {
		return errwrap.Wrapf("Error deleting schema: {{err}}", err)
	}

	if err := txn.Commit(); err != nil {
		return errwrap.Wrapf("Error committing schema: {{err}}", err)
	}

	d.SetId("")

	return nil
}
开发者ID:hashicorp,项目名称:terraform,代码行数:34,代码来源:resource_postgresql_schema.go

示例15: resourcePostgreSQLDatabaseDelete

func resourcePostgreSQLDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
	c := meta.(*Client)
	conn, err := c.Connect()
	if err != nil {
		return errwrap.Wrapf("Error connecting to PostgreSQL: {{err}}", err)
	}
	defer conn.Close()

	dbName := d.Get(dbNameAttr).(string)

	if isTemplate := d.Get(dbIsTemplateAttr).(bool); isTemplate {
		// Template databases must have this attribute cleared before
		// they can be dropped.
		if err := doSetDBIsTemplate(conn, dbName, false); err != nil {
			return errwrap.Wrapf("Error updating database IS_TEMPLATE during DROP DATABASE: {{err}}", err)
		}
	}

	if err := setDBIsTemplate(conn, d); err != nil {
		return err
	}

	query := fmt.Sprintf("DROP DATABASE %s", pq.QuoteIdentifier(dbName))
	_, err = conn.Query(query)
	if err != nil {
		return errwrap.Wrapf("Error dropping database: {{err}}", err)
	}

	d.SetId("")

	return nil
}
开发者ID:hooklift,项目名称:terraform,代码行数:32,代码来源:resource_postgresql_database.go


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