예제 #1
0
func (s *libpqStmt) exec(args []driver.Value) (*C.PGresult, error) {
	// if we have no arguments, use plain exec instead of more complicated PQexecPrepared
	if len(args) == 0 {
		if s.cquery == nil {
			s.cquery = C.CString(s.query)
		}
		cres := C.PQexec(s.c.db, s.cquery)
		if err := resultError(cres); err != nil {
			C.PQclear(cres)
			return nil, err
		}
		return cres, nil
	}

	// convert args into C array-of-strings
	cargs, err := buildCArgs(args)
	if err != nil {
		return nil, err
	}
	defer returnCharArrayToPool(len(args), cargs)

	// execute
	cres := C.PQexecPrepared(s.c.db, s.name, C.int(len(args)), cargs, nil, nil, 0)
	if err = resultError(cres); err != nil {
		C.PQclear(cres)
		return nil, err
	}
	return cres, nil
}
예제 #2
0
func (c *driverConn) exec(stmt string, args []driver.Value) (cres *C.PGresult) {
	stmtstr := C.CString(stmt)
	defer C.free(unsafe.Pointer(stmtstr))
	if len(args) == 0 {
		cres = C.PQexec(c.db, stmtstr)
	} else {
		cargs := buildCArgs(args)
		defer C.freeCharArray(cargs, C.int(len(args)))
		cres = C.PQexecParams(c.db, stmtstr, C.int(len(args)), nil, cargs, nil, nil, 0)
	}
	return cres
}
예제 #3
0
func (c *Conn) exec(stmt string, params ...interface{}) (cres *C.PGresult) {
	stmtstr := C.CString(stmt)
	defer C.free(unsafe.Pointer(stmtstr))
	if len(params) == 0 {
		cres = C.PQexec(c.db, stmtstr)
	} else {
		cparams := buildCArgs(params...)
		defer C.freeCharArray(cparams, C.int(len(params)))
		cres = C.PQexecParams(c.db, stmtstr, C.int(len(params)), nil, cparams, nil, nil, 0)
	}
	return cres
}
예제 #4
0
파일: pq.go 프로젝트: dvarrazzo/pger
// Execute a single command in a string without parameters.
// Return the error if any, else nil. Result is discarded.
func exec_string(conn *PgConn, stmt string) error {
	cstmt := C.CString(stmt)
	defer C.free(unsafe.Pointer(cstmt))

	res := C.PQexec(conn.conn, cstmt)
	if err := errorFromPGresult(res); err != nil {
		pqclear(res)
		return err
	}

	pqclear(res)
	return nil
}
예제 #5
0
func (d *libpqDriver) getOids(db *C.PGconn, dsn string) (*pqoid, error) {
	var err error
	d.Lock()
	defer d.Unlock()

	// check cache
	if oids, ok := d.oids[dsn]; ok {
		return oids, nil
	}

	// not in cache - query the database
	oids := &pqoid{}
	names := []struct {
		kind string
		dest *int
	}{
		{"'bytea'", &oids.Bytea},
		{"'date'", &oids.Date},
		{"'timestamp'", &oids.Timestamp},
		{"'timestamp with time zone'", &oids.TimestampTz},
		{"'time'", &oids.Time},
		{"'time with time zone'", &oids.TimeTz},
	}

	// fetch all the OIDs we care about
	for _, n := range names {
		ccmd := C.CString("SELECT " + n.kind + "::regtype::oid")
		defer C.free(unsafe.Pointer(ccmd))
		cres := C.PQexec(db, ccmd)
		defer C.PQclear(cres)
		if err := resultError(cres); err != nil {
			return nil, err
		}
		sval := C.GoString(C.PQgetvalue(cres, 0, 0))
		*n.dest, err = strconv.Atoi(sval)
		if err != nil {
			return nil, ErrFetchingOids
		}
	}

	// save in cache for next time
	d.oids[dsn] = oids

	return oids, nil
}
예제 #6
0
// Execute a query, possibly getting a result object (unless the caller doesn't
// want it, as in the case of BEGIN/COMMIT/ROLLBACK).
// the caller doesn't care about that (e.g., Begin(), Commit(), Rollback()).
//func (c *libpqConn) exec(cmd string, res *libpqResult) error {
func (c *libpqConn) exec(cmd string, wantResult bool) (driver.Result, error) {
	ccmd := C.CString(cmd)
	defer C.free(unsafe.Pointer(ccmd))
	cres := C.PQexec(c.db, ccmd)
	defer C.PQclear(cres)
	if err := resultError(cres); err != nil {
		return nil, err
	}

	if !wantResult {
		return nil, nil
	}

	nrows, err := getNumRows(cres)
	if err != nil {
		return nil, err
	}

	return libpqResult(nrows), nil
}