本文整理汇总了Golang中github.com/cockroachdb/cockroach/testutils/sqlutils.PGUrl函数的典型用法代码示例。如果您正苦于以下问题:Golang PGUrl函数的具体用法?Golang PGUrl怎么用?Golang PGUrl使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PGUrl函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestPGWireDBName
func TestPGWireDBName(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
pgURL, cleanupFn := sqlutils.PGUrl(t, s.ServingAddr(), security.RootUser, "TestPGWireDBName")
pgURL.Path = "foo"
defer cleanupFn()
{
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE DATABASE foo`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE bar (i INT PRIMARY KEY)`); err != nil {
t.Fatal(err)
}
}
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`INSERT INTO bar VALUES ($1)`, 1); err != nil {
t.Fatal(err)
}
}
示例2: TestPGWireMetrics
func TestPGWireMetrics(t *testing.T) {
defer leaktest.AfterTest(t)
s := server.StartTestServer(t)
defer s.Stop()
// Setup pgwire client.
pgUrl, cleanupFn := sqlutils.PGUrl(t, s, security.RootUser, "TestPGWireMetrics")
defer cleanupFn()
const minbytes = 20
// Make sure we're starting at 0.
if _, _, err := checkPGWireMetrics(s, 0, 0, 0, 0); err != nil {
t.Fatal(err)
}
// A single query should give us some I/O.
if err := trivialQuery(pgUrl); err != nil {
t.Fatal(err)
}
bytesIn, bytesOut, err := checkPGWireMetrics(s, minbytes, minbytes, 300, 300)
if err != nil {
t.Fatal(err)
}
if err := trivialQuery(pgUrl); err != nil {
t.Fatal(err)
}
// A second query should give us more I/O.
_, _, err = checkPGWireMetrics(s, bytesIn+minbytes, bytesOut+minbytes, 300, 300)
if err != nil {
t.Fatal(err)
}
}
示例3: TestDropAndCreateTable
func TestDropAndCreateTable(t *testing.T) {
defer leaktest.AfterTest(t)()
s := server.StartTestServer(t)
defer s.Stop()
pgURL, cleanupFn := sqlutils.PGUrl(t, s.ServingAddr(),
security.RootUser, "TestDropAndCreateTable")
pgURL.Path = "test"
defer cleanupFn()
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE DATABASE test`); err != nil {
t.Fatal(err)
}
for i := 0; i < 20; i++ {
if _, err := db.Exec(`DROP TABLE IF EXISTS foo`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE foo (k INT PRIMARY KEY)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO foo VALUES (1), (2), (3)`); err != nil {
t.Fatal(err)
}
}
}
示例4: setUser
// setUser sets the DB client to the specified user.
// It returns a cleanup function to be run when the credentials
// are no longer needed.
func (t *logicTest) setUser(user string) func() {
if t.db != nil {
var dbName string
if err := t.db.QueryRow("SHOW DATABASE").Scan(&dbName); err != nil {
t.Fatal(err)
}
defer func() {
// Propagate the DATABASE setting to the newly-live connection.
if _, err := t.db.Exec(fmt.Sprintf("SET DATABASE = %s", dbName)); err != nil {
t.Fatal(err)
}
}()
}
if t.clients == nil {
t.clients = map[string]*sql.DB{}
}
if db, ok := t.clients[user]; ok {
t.db = db
// No cleanup necessary, but return a no-op func to avoid nil pointer dereference.
return func() {}
}
pgUrl, cleanupFunc := sqlutils.PGUrl(t.T, &t.srv.TestServer, user, "TestLogic")
db, err := sql.Open("postgres", pgUrl.String())
if err != nil {
t.Fatal(err)
}
t.clients[user] = db
t.db = db
return cleanupFunc
}
示例5: setUser
// setUser sets the DB client to the specified user.
func (t *logicTest) setUser(tempDir, user string) {
if t.db != nil {
var dbName string
if err := t.db.QueryRow("SHOW DATABASE").Scan(&dbName); err != nil {
t.Fatal(err)
}
defer func() {
// Propagate the DATABASE setting to the newly-live connection.
if _, err := t.db.Exec(fmt.Sprintf("SET DATABASE = %s", dbName)); err != nil {
t.Fatal(err)
}
}()
}
if t.clients == nil {
t.clients = map[string]*sql.DB{}
}
if db, ok := t.clients[user]; ok {
t.db = db
return
}
// The entire tempDir will be deleted later, so the cleanup function can be
// ignored.
pgUrl, _ := sqlutils.PGUrl(t.T, t.srv, user, tempDir, "TestLogic")
db, err := sql.Open("postgres", pgUrl.String())
if err != nil {
t.Fatal(err)
}
t.clients[user] = db
t.db = db
}
示例6: TestDropAndCreateTable
func TestDropAndCreateTable(t *testing.T) {
defer leaktest.AfterTest(t)()
t.Skip(`TODO(andrei, dt): Fails with 'table "foo" does not exist'`)
s := server.StartTestServer(t)
defer s.Stop()
pgURL, cleanupFn := sqlutils.PGUrl(t, s.ServingAddr(),
security.RootUser, "TestDropAndCreateTable")
pgURL.Path = "test"
defer cleanupFn()
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE DATABASE test`); err != nil {
t.Fatal(err)
}
for i := 0; i < 20; i++ {
if _, err := db.Exec(`DROP TABLE IF EXISTS foo`); err != nil {
t.Fatal(err)
}
//NB: a `time.Sleep(time.Second)` here makes this pass.
if _, err := db.Exec(`CREATE TABLE foo (k INT PRIMARY KEY)`); err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`INSERT INTO foo VALUES (1), (2), (3)`); err != nil {
t.Fatal(err)
}
}
}
示例7: TestPGWireConnectionCloseReleasesLeases
// Test that abruptly closing a pgwire connection releases all leases held by
// that session.
func TestPGWireConnectionCloseReleasesLeases(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
url, cleanupConn := sqlutils.PGUrl(t, s.ServingAddr(), security.RootUser, "SetupServer")
defer cleanupConn()
conn, err := pq.Open(url.String())
if err != nil {
t.Fatal(err)
}
ex := conn.(driver.Execer)
if _, err := ex.Exec("CREATE DATABASE test", nil); err != nil {
t.Fatal(err)
}
if _, err := ex.Exec("CREATE TABLE test.t (i INT PRIMARY KEY)", nil); err != nil {
t.Fatal(err)
}
// Start a txn so leases are accumulated by queries.
if _, err := ex.Exec("BEGIN", nil); err != nil {
t.Fatal(err)
}
// Get a table lease.
if _, err := ex.Exec("SELECT * FROM test.t", nil); err != nil {
t.Fatal(err)
}
// Abruptly close the connection.
if err := conn.Close(); err != nil {
t.Fatal(err)
}
// Verify that there are no leases held.
tableDesc := sqlbase.GetTableDescriptor(kvDB, "test", "t")
lm := s.LeaseManager().(*LeaseManager)
// Looking for a table state validates that there used to be a lease on the
// table.
ts := lm.findTableState(tableDesc.ID, false /* create */)
if ts == nil {
t.Fatal("table state not found")
}
ts.mu.Lock()
leases := ts.active.data
ts.mu.Unlock()
if len(leases) != 1 {
t.Fatalf("expected one lease, found: %d", len(leases))
}
// Wait for the lease to be released.
util.SucceedsSoon(t, func() error {
ts.mu.Lock()
refcount := ts.active.data[0].refcount
ts.mu.Unlock()
if refcount != 0 {
return errors.Errorf(
"expected lease to be unused, found refcount: %d", refcount)
}
return nil
})
}
示例8: BenchmarkPgbenchExec_Cockroach
func BenchmarkPgbenchExec_Cockroach(b *testing.B) {
defer tracing.Disable()()
s := server.StartInsecureTestServer(b)
defer s.Stop()
pgUrl, cleanupFn := sqlutils.PGUrl(b, s, security.RootUser, "benchmarkCockroach")
pgUrl.RawQuery = "sslmode=disable"
defer cleanupFn()
execPgbench(b, pgUrl)
}
示例9: BenchmarkPgbenchExec_Cockroach
func BenchmarkPgbenchExec_Cockroach(b *testing.B) {
defer tracing.Disable()()
s, _, _ := serverutils.StartServer(b, base.TestServerArgs{Insecure: true})
defer s.Stopper().Stop()
pgUrl, cleanupFn := sqlutils.PGUrl(b, s.ServingAddr(), security.RootUser, "benchmarkCockroach")
pgUrl.RawQuery = "sslmode=disable"
defer cleanupFn()
execPgbench(b, pgUrl)
}
示例10: setupWithContext
func setupWithContext(t *testing.T, ctx *server.Context) (*testServer, *sql.DB, *client.DB) {
s := setupTestServerWithContext(t, ctx)
// SQL requests use security.RootUser which has ALL permissions on everything.
url, cleanupFn := sqlutils.PGUrl(t, &s.TestServer, security.RootUser, "setupWithContext")
sqlDB, err := sql.Open("postgres", url.String())
if err != nil {
t.Fatal(err)
}
s.cleanupFns = append(s.cleanupFns, cleanupFn)
return s, sqlDB, s.DB()
}
示例11: TestDumpBytes
func TestDumpBytes(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
url, cleanup := sqlutils.PGUrl(t, s.ServingAddr(), security.RootUser, "TestDumpBytes")
defer cleanup()
conn := makeSQLConn(url.String())
defer conn.Close()
if err := conn.Exec(`
CREATE DATABASE d;
SET DATABASE = d;
CREATE TABLE t (b BYTES PRIMARY KEY);
`, nil); err != nil {
t.Fatal(err)
}
for i := int64(0); i < 256; i++ {
if err := conn.Exec("INSERT INTO t VALUES ($1)", []driver.Value{[]byte{byte(i)}}); err != nil {
t.Fatal(err)
}
}
var b bytes.Buffer
if err := dumpTable(&b, conn, "d", "t"); err != nil {
t.Fatal(err)
}
dump := b.String()
b.Reset()
if err := conn.Exec(`
CREATE DATABASE o;
SET DATABASE = o;
`, nil); err != nil {
t.Fatal(err)
}
if err := conn.Exec(dump, nil); err != nil {
t.Fatal(err)
}
if err := dumpTable(&b, conn, "o", "t"); err != nil {
t.Fatal(err)
}
dump2 := b.String()
if dump != dump2 {
t.Fatalf("unmatching dumps:\n%s\n%s", dump, dump2)
}
}
示例12: setUser
// setUser sets the DB client to the specified user.
// It returns a cleanup function to be run when the credentials
// are no longer needed.
func (t *logicTest) setUser(user string) func() {
var outDBName string
if t.db != nil {
var inDBName string
if err := t.db.QueryRow("SHOW DATABASE").Scan(&inDBName); err != nil {
t.Fatal(err)
}
defer func() {
if inDBName != outDBName {
// Propagate the DATABASE setting to the newly-live connection.
if _, err := t.db.Exec(fmt.Sprintf("SET DATABASE = %s", inDBName)); err != nil {
t.Fatal(err)
}
}
}()
}
if t.clients == nil {
t.clients = map[string]*gosql.DB{}
}
if db, ok := t.clients[user]; ok {
t.db = db
t.user = user
if err := t.db.QueryRow("SHOW DATABASE").Scan(&outDBName); err != nil {
t.Fatal(err)
}
// No cleanup necessary, but return a no-op func to avoid nil pointer dereference.
return func() {}
}
pgURL, cleanupFunc := sqlutils.PGUrl(t.T, t.srv.ServingAddr(), user, "TestLogic")
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
t.clients[user] = db
t.db = db
t.user = user
if t.verbose {
fmt.Printf("--- new user: %s\n", user)
}
return cleanupFunc
}
示例13: TestPGPrepareNameQual
// Names should be qualified automatically during Prepare when a database name
// was given in the connection string.
func TestPGPrepareNameQual(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
pgURL, cleanupFn := sqlutils.PGUrl(t, s.ServingAddr(), security.RootUser, "TestPGPrepareNameQual")
defer cleanupFn()
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err := db.Exec(`CREATE DATABASE IF NOT EXISTS testing`); err != nil {
t.Fatal(err)
}
pgURL.Path = "/testing"
db2, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db2.Close()
statements := []string{
`CREATE TABLE IF NOT EXISTS f (v INT)`,
`INSERT INTO f VALUES (42)`,
`SELECT * FROM f`,
`DELETE FROM f WHERE v = 42`,
`DROP TABLE IF EXISTS f`,
}
for _, stmtString := range statements {
if _, err = db2.Exec(stmtString); err != nil {
t.Fatal(err)
}
stmt, err := db2.Prepare(stmtString)
if err != nil {
t.Fatal(err)
}
if _, err = stmt.Exec(); err != nil {
t.Fatal(err)
}
}
}
示例14: TestPGPrepareFail
func TestPGPrepareFail(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()
pgURL, cleanupFn := sqlutils.PGUrl(t, s.ServingAddr(), security.RootUser, "TestPGPrepareFail")
defer cleanupFn()
db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
testFailures := map[string]string{
"SELECT $1 = $1": "pq: could not determine data type of placeholder $1",
"SELECT $1": "pq: could not determine data type of placeholder $1",
"SELECT $1 + $1": "pq: could not determine data type of placeholder $1",
"SELECT CASE WHEN TRUE THEN $1 END": "pq: could not determine data type of placeholder $1",
"SELECT CASE WHEN TRUE THEN $1 ELSE $2 END": "pq: could not determine data type of placeholder $1",
"SELECT $1 > 0 AND NOT $1": "pq: incompatible NOT argument type: int",
"CREATE TABLE $1 (id INT)": "pq: syntax error at or near \"1\"\nCREATE TABLE $1 (id INT)\n ^\n",
"UPDATE d.t SET s = i + $1": "pq: unsupported binary operator: <int> + <placeholder> (desired <string>)",
"SELECT $0 > 0": "pq: invalid placeholder name: $0",
"SELECT $2 > 0": "pq: could not determine data type of placeholder $1",
"SELECT 3 + CASE (4) WHEN 4 THEN $1 END": "pq: could not determine data type of placeholder $1",
"SELECT ($1 + $1) + CURRENT_DATE()": "pq: could not determine data type of placeholder $1",
"SELECT $1 + $2, $2::FLOAT": "pq: could not determine data type of placeholder $1",
"SELECT ($1 + 2) + ($1 + 2.5)": "pq: unsupported binary operator: <int> + <decimal>",
}
if _, err := db.Exec(`CREATE DATABASE d; CREATE TABLE d.t (i INT, s STRING, d INT)`); err != nil {
t.Fatal(err)
}
for query, reason := range testFailures {
if stmt, err := db.Prepare(query); err == nil {
t.Errorf("expected error: %s", query)
if err := stmt.Close(); err != nil {
t.Fatal(err)
}
} else if err.Error() != reason {
t.Errorf(`%s: got: %q, expected: %q`, query, err, reason)
}
}
}
示例15: TestPGPrepareFail
func TestPGPrepareFail(t *testing.T) {
defer leaktest.AfterTest(t)()
s := server.StartTestServer(t)
defer s.Stop()
pgURL, cleanupFn := sqlutils.PGUrl(t, s, security.RootUser, "TestPGPrepareFail")
defer cleanupFn()
db, err := sql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()
testFailures := map[string]string{
"SELECT $1 = $1": "pq: unsupported comparison operator: <parameter> = <parameter>",
"SELECT $1 > 0 AND NOT $1": "pq: incompatible NOT argument type: int",
"SELECT $1": "pq: could not determine data type of parameter $1",
"SELECT $1 + $1": "pq: unsupported binary operator: <parameter> + <parameter>",
"SELECT now() + $1": "pq: unsupported binary operator: <timestamp> + <parameter>",
"SELECT CASE $1 WHEN 1 THEN 1 END": "pq: could not determine data type of parameter $1",
"SELECT CASE WHEN TRUE THEN $1 END": "pq: could not determine data type of parameter $1",
"SELECT CASE WHEN TRUE THEN $1 ELSE $2 END": "pq: could not determine data type of parameter $2",
"SELECT CASE WHEN TRUE THEN 1 ELSE $1 END": "pq: could not determine data type of parameter $1",
"UPDATE d.t SET d = CASE WHEN TRUE THEN $1 END": "pq: could not determine data type of parameter $1",
"CREATE TABLE $1 (id INT)": "pq: syntax error at or near \"1\"\nCREATE TABLE $1 (id INT)\n ^\n",
"UPDATE d.t SET s = i + $1": "pq: value type int doesn't match type STRING of column \"s\"",
"SELECT $0 > 0": "pq: there is no parameter $0",
"SELECT $2 > 0": "pq: could not determine data type of parameter $1",
}
if _, err := db.Exec(`CREATE DATABASE d; CREATE TABLE d.t (i INT, s STRING, d INT)`); err != nil {
t.Fatal(err)
}
for query, reason := range testFailures {
if stmt, err := db.Prepare(query); err == nil {
t.Errorf("expected error: %s", query)
if err := stmt.Close(); err != nil {
t.Fatal(err)
}
} else if err.Error() != reason {
t.Errorf("%s: unexpected error: %s", query, err)
}
}
}