jbarham / pgsql.go Goto Github PK
View Code? Open in Web Editor NEWPostgreSQL high-level client library wrapper for Go
License: Other
PostgreSQL high-level client library wrapper for Go
License: Other
may be add few lines of code like:
func resultStatus(res *C.PGresult) (int) {
return int(C.PQresultStatus(res))
}
func (c *Conn) CopyFrom(stmt string, reader io.Reader) os.Error {
// buffer
sb := [1024]byte{}
stmtstr := C.CString(stmt)
defer C.free(unsafe.Pointer(stmtstr))
cres := C.PQexec(c.db, stmtstr)
defer C.PQclear(cres)
// check for ready
if resultStatus(cres) != C.PGRES_COPY_IN {
return resultError(cres)
}
L: // main loop
for {
n, e := reader.Read(sb[:])
if n > 0 {
switch C.PQputCopyData(c.db, (*C.char)(unsafe.Pointer(&sb[0])), C.int(n)) {
// block send ok
case 1:
continue L
// in manual for nonblocking connection , may be not need
case 0:
return os.NewError("COPY: connection blocked")
// error
case -1:
return connError(c.db)
}
} else {
if e != os.EOF {
return e
}
// send "EOF" marker
switch C.PQputCopyEnd(c.db, nil) {
// in manual for nonblocking connection , may be not need
case 0:
return os.NewError("COPY: connection blocked")
// error
case -1:
return connError(c.db)
}
// read summary of COPY
cres = C.PQgetResult(c.db)
println(resultStatus(cres))
defer C.PQclear(cres)
return resultError(cres)
}
}
return nil
}
use that expromt like:
func bye(e os.Error) {
if e != nil {
println(e.String())
os.Exit(1)
}
}
func main(){
c, e := pgsql.Connect("....")
defer c.Close()
bye(e)
e=c.Exec("begin")
bye(e)
e=c.Exec("create temp table a( b text, c text)on commit drop;")
bye(e)
e=c.Exec("create temp table b( d text, e text)on commit drop;")
bye(e)
// prepare test reader data
testfile := bytes.NewBufferString(" row11\trow12\nrow21\trow22\n")
// send data to pg server
e = c.CopyFrom("copy b(d,e) from stdin;\n", testfile)
bye(e)
// any commands after copy
e=c.Exec("insert into a select d,e from b except select b,c from a;")
bye(e)
e=c.Exec("end;")
bye(e)
println("complete")
}
this badcode work well) But, need some rewrites )))
get package:
sudo apt-get install libpq-dev
edit Makefile:
CGO_CFLAGS=-I/usr/include/postgresql
CGO_LDFLAGS=/usr/lib/libpq.so
It would be nice to have functions that return ncols and nrows. I would be happy to do that.
I also have it working for go 1.1 do you want those changes also?
I succeeded in installing and working just fine on 8g version but when I upgraded to Go 1.0 installing won't work! :-(
teo@teo:~/go/src/pkg$ go get github.com/jbarham/pgsql.go
package github.com/jbarham/pgsql.go
imports github.com/jbarham/pgsql.go
imports github.com/jbarham/pgsql.go: found packages pgsql (pgsql.go) and main (pool_example.go) in /home/teo/go...
teo@teo:/go/src/pkg$ cd github.com/jbarham/pgsql.go/go/src/pkg/github.com/jbarham/pgsql.go$ make install
teo@teo:
Makefile:1: /home/teo/go/src/Make.inc: No such file or directory
Makefile:12: /home/teo/go/src/Make.pkg: No such file or directory
make: *** No rule to make target `/home/teo/go/src/Make.pkg'. Stop.
the Makefile assumes libpq.so, but on mac it's .dylib instead of .so. I think this can be easily fixed for everyone:
diff --git a/Makefile b/Makefile
index 6eb4583..a802897 100644
--- a/Makefile
+++ b/Makefile
@@ -7,6 +7,6 @@ CGOFILES=
pool.go\
CGO_CFLAGS=-Ipg_config --includedir
-CGO_LDFLAGS=pg_config --libdir
/libpq.so
+CGO_LDFLAGS=-Lpg_config --libdir
-lpq
include $(GOROOT)/src/Make.pkg
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.