func connect(conninfo string) (*PgConn, error) { cs := C.CString(conninfo) defer C.free(unsafe.Pointer(cs)) conn := C.PQconnectdb(cs) if C.PQstatus(conn) != C.CONNECTION_OK { cerr := C.PQerrorMessage(conn) err := fmt.Errorf("connection failed: %s", C.GoString(cerr)) C.PQfinish(conn) return nil, err } // TODO: support other encodings? pname := C.CString("client_encoding") defer C.free(unsafe.Pointer(pname)) cenc := C.PQparameterStatus(conn, pname) if cenc == nil { err := errors.New("connection failed: no client encoding") C.PQfinish(conn) return nil, err } if enc := C.GoString(cenc); enc != "UTF8" { err := fmt.Errorf( "connection failed: client encoding not supported: %s", enc) C.PQfinish(conn) return nil, err } // one-line error message C.PQsetErrorVerbosity(conn, C.PQERRORS_TERSE) return &PgConn{conn: conn, conninfo: conninfo}, nil }
// dsn is passed directly to PQconnectdb func (d *libpqDriver) Open(dsn string) (driver.Conn, error) { if C.PQisthreadsafe() != 1 { return nil, ErrThreadSafety } params := C.CString(dsn) defer C.free(unsafe.Pointer(params)) db := C.PQconnectdb(params) if C.PQstatus(db) != C.CONNECTION_OK { defer C.PQfinish(db) return nil, errors.New("libpq: connection error " + C.GoString(C.PQerrorMessage(db))) } oids, err := d.getOids(db, dsn) if err != nil { defer C.PQfinish(db) return nil, ErrFetchingOids } return &libpqConn{ db: db, oids: oids, stmtCache: make(map[string]driver.Stmt), stmtNum: 0, }, nil }
// Reset closes the connection to the server and attempts to re-establish a new // connection using the parameters passed in the original Connect call. func (c *Conn) Reset() os.Error { if c == nil || c.db == nil { return os.NewError("nil postgresql connection") } C.PQreset(c.db) if C.PQstatus(c.db) != C.CONNECTION_OK { return connError(c.db) } return nil }
// Open creates a new database connection using the given connection string. // Each parameter setting is in the form 'keyword=value'. // See http://www.postgresql.org/docs/9.0/static/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS // for a list of recognized parameters. func (d *postgresDriver) Open(name string) (conn driver.Conn, err error) { cparams := C.CString(name) defer C.free(unsafe.Pointer(cparams)) db := C.PQconnectdb(cparams) if C.PQstatus(db) != C.CONNECTION_OK { err = connError(db) C.PQfinish(db) return nil, err } conn = &driverConn{db, 0} runtime.SetFinalizer(conn, (*driverConn).Close) return }
// Connect creates a new database connection using the given connection string. // Each parameter setting is in the form 'keyword=value'. // See http://www.postgresql.org/docs/9.0/static/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS // for a list of recognized parameters. func Connect(params string) (conn *Conn, err os.Error) { cparams := C.CString(params) defer C.free(unsafe.Pointer(cparams)) db := C.PQconnectdb(cparams) if C.PQstatus(db) != C.CONNECTION_OK { err = connError(db) C.PQfinish(db) return nil, err } conn = &Conn{db, 0} runtime.SetFinalizer(conn, (*Conn).Close) return }