Giter Site home page Giter Site logo

pgsql.go's Issues

COPY from stdin?

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 )))

for ubuntu

get package:
sudo apt-get install libpq-dev

edit Makefile:
CGO_CFLAGS=-I/usr/include/postgresql
CGO_LDFLAGS=/usr/lib/libpq.so

Rows() and Cols()

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?

Cannot install pgpsql.go on Go 1.0

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
teo@teo:
/go/src/pkg/github.com/jbarham/pgsql.go$ make install
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.

build on MacOS

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.