Ejemplo n.º 1
0
Archivo: pq.go Proyecto: dvarrazzo/pger
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
}
Ejemplo n.º 2
0
// 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
}
Ejemplo n.º 3
0
// Close closes the database connection and frees its associated memory.
func (c *Conn) Close() {
	if c != nil && c.db != nil {
		C.PQfinish(c.db)
		c.db = nil
		runtime.SetFinalizer(c, nil)
	}
}
Ejemplo n.º 4
0
func (c *driverConn) Close() error {
	if c != nil && c.db != nil {
		C.PQfinish(c.db)
		c.db = nil
		runtime.SetFinalizer(c, nil)
	}
	return nil
}
Ejemplo n.º 5
0
func (c *libpqConn) Close() error {
	C.PQfinish(c.db)
	// free cached prepared statement names
	for _, v := range c.stmtCache {
		if stmt, ok := v.(*libpqStmt); ok {
			C.free(unsafe.Pointer(stmt.name))
		}
	}
	return nil
}
Ejemplo n.º 6
0
// 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
}
Ejemplo n.º 7
0
// 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
}
Ejemplo n.º 8
0
Archivo: pq.go Proyecto: dvarrazzo/pger
func close_conn(conn *PgConn) error {
	C.PQfinish(conn.conn)
	conn.conn = nil
	return nil
}
Ejemplo n.º 9
0
Archivo: low.go Proyecto: newblue/sql
func (self *pqConnection) pqClose() {
	C.PQfinish(self.handle)
	// ensure we don't use this handle again
	self.handle = nil
}