Skip to content

Commit

Permalink
Merge pull request lxn#1 from cbbrowne/master
Browse files Browse the repository at this point in the history
Added a bit more to the examples
  • Loading branch information
andrewzeneski committed Nov 4, 2011
2 parents 04d0af8 + 97c60bd commit 007253e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/cmd/examples/Make.386
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
O=8
AS=${O}a
CC=${O}c
GC=${O}g
LD=${O}l
OS=568vq
CFLAGS=-FVw
7 changes: 7 additions & 0 deletions src/cmd/examples/Make.amd64
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
O=6
AS=${O}a
CC=${O}c
GC=${O}g
LD=${O}l
OS=568vq
CFLAGS=-FVw
31 changes: 31 additions & 0 deletions src/cmd/examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
include Make.$(GOARCH)
ALL=pool multipleselects scan statements

all: $(ALL)

scan: scan.$(O)
$(LD) -o scan scan.$(O)

scan.$(O): scan.go
$(GC) scan.go

multipleselects: multipleselects.$(O)
$(LD) -o multipleselects multipleselects.$(O)

multipleselects.$(O): multipleselects.go
$(GC) multipleselects.go

statements: statements.$(O)
$(LD) -o statements statements.$(O)

statements.$(O): statements.go
$(GC) statements.go

pool: pool.$(O)
$(LD) -o pool pool.$(O)

pool.$(O): pool.go
$(GC) pool.go

clean:
rm -f *.6 *.8 $(ALL)
2 changes: 1 addition & 1 deletion src/cmd/examples/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func main() {
// Create a connection pool with up to 3 connections, automatically closing
// idle connections after the default timeout period (5 minutes).
pool, err := pgsql.NewPool("dbname=testdb user=postgres", 3, 3, pgsql.DEFAULT_IDLE_TIMEOUT)
pool, err := pgsql.NewPool("dbname=postgres user=postgres", 3, 3, pgsql.DEFAULT_IDLE_TIMEOUT)
if err != nil {
log.Fatalf("Error opening connection pool: %s\n", err)
}
Expand Down
74 changes: 74 additions & 0 deletions src/pkg/pgsql/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ func (conn *Conn) parseParams(s string) *connParams {
params.Database = name2value["dbname"]
params.User = name2value["user"]
params.Password = name2value["password"]
if params.Password == "" {
params.Password,_ = passwordfromfile(params.Host, params.Port, params.Database,params.User)
}
params.TimeoutSeconds, _ = strconv.Atoi(name2value["timeout"])

if conn.LogLevel >= LogDebug {
Expand Down Expand Up @@ -342,6 +345,77 @@ func (conn *Conn) Close() (err os.Error) {
})
}

func getpgpassfilename () string {
var env string
env = os.Getenv("PGPASSFILE")
if env != "" {
return env
}
env = os.Getenv("HOME")
return fmt.Sprintf("%s/.pgpass", env)
}

func passwordfromfile (hostname string, port int, dbname string, username string) (string, os.Error) {
var sport string
var lhostname string
if dbname == "" {
return "", nil
}
if username == "" {
return "", nil
}
if hostname == "" {
lhostname = "localhost"
} else {
lhostname = hostname
}
if port == 0 {
sport = "5432"
} else {
sport = fmt.Sprintf("%d", port)
}
pgfile := getpgpassfilename()
fileinfo, err := os.Stat(pgfile)
if err != nil {
err := os.NewError(fmt.Sprintf("WARNING: password file \"%s\" is not a plain file\n", pgfile))
return "", err
}
if (fileinfo.Mode & 077) != 0 {
err := os.NewError(fmt.Sprintf("WARNING: password file \"%s\" has group or world access; permissions should be u=rw (0600) or less", pgfile))
return "", err
}
fp, err := os.Open(pgfile)
if err != nil {
err := os.NewError(fmt.Sprintf("Problem opening pgpass file \"%s\"", pgfile))
return "", err
}
br := bufio.NewReader(fp)
for {
line, ok := br.ReadString('\n')
if ok == os.EOF {
return "", nil
}
// Now, split the line into pieces
// hostname:port:database:username:password
// and * matches anything
pieces := strings.Split(line, ":")
phost := pieces[0]
pport := pieces[1]
pdb := pieces[2]
puser := pieces[3]
ppass := pieces[4]

if (phost == lhostname || phost == "*") &&
(pport == "*" || pport == sport) &&
(pdb == "*" || pdb == dbname) &&
(puser == "*" || puser == username) {

return ppass, nil
}
}
return "", nil
}

func (conn *Conn) execute(command string, params ...*Parameter) int64 {
if conn.LogLevel >= LogDebug {
defer conn.logExit(conn.logEnter("*Conn.execute"))
Expand Down

0 comments on commit 007253e

Please sign in to comment.