Skip to content

Commit

Permalink
Merge pull request #43 from feenkcom/master
Browse files Browse the repository at this point in the history
Unix Socket support added to P3Client
  • Loading branch information
svenvc authored Jan 2, 2024
2 parents 75ade4c + 8f9ee23 commit 6ad99e0
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions P3/P3Client.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ P3Client >> isServerVersionAtLeastMajor: major minor: minor [
and: [ serverVersion second >= minor ] ]
]

{ #category : #testing }
P3Client >> isUsingUnixSocket [
"Following the PostgreSQL convention a host starting with a / is a path to a Unix Socket"

^ self host notNil and: [ self host first = $/ ]
]

{ #category : #testing }
P3Client >> isWorking [
"Do a trivial query to confirm that I can interact with the server.
Expand Down Expand Up @@ -568,10 +575,14 @@ P3Client >> open [
"Open my connection with the server (do not yet #connect)"

self close.
connection := ZdcSocketStream openConnectionToHostNamed: self host port: self port.
connection := self isUsingUnixSocket
ifTrue: [ | unixSocket |
unixSocket := ZnNetworkingUtils default unixSocketOnFile: self host asFileReference.
ZdcSocketStream on: unixSocket]
ifFalse: [
ZdcSocketStream openConnectionToHostNamed: self host port: self port ].
connection timeout: self timeout.
message := P3MessageBuffer new

]

{ #category : #accessing }
Expand Down Expand Up @@ -1099,12 +1110,18 @@ P3Client >> url [
"Return my connection URL"

| url |
(url := ZnUrl new)
scheme: #psql;
host: self host;
port: self port;
username: self user;
password: self password.
(url := ZnUrl new) scheme: #postgresql.
self isUsingUnixSocket
ifTrue: [
url queryAt: #host put: self host.
self user ifNotNil: [ url queryAt: #user put: self user ].
self password ifNotNil: [ url queryAt: #password put: self password ] ]
ifFalse: [
url
host: self host;
port: self port;
username: self user;
password: self password ].
self database ifNotNil: [ url addPathSegment: self database ].
self isSSL ifTrue: [ url queryAt: #sslmode put: #require ].
^ url
Expand All @@ -1113,18 +1130,25 @@ P3Client >> url [
{ #category : #'initialize-release' }
P3Client >> url: stringOrUrl [
"Set my connection settings from stringOrUrl according to the format
psql://username:password@localhost:5432/databasename?sslmode=require
with the minimum being psql://user@localhost"
postgresql://username:password@localhost:5432/databasename?sslmode=require
with the minimum being psql://user@localhost , for a unix socket the format is
postgresql:///databasename?host=/path/to/psql.socket&user=username&password=password"

| url |
url := stringOrUrl asUrl.
self assert: (#(psql postgres postgresql) includes: url scheme).
self
host: url host;
port: (url portIfAbsent: [ 5432 ]);
user: url username;
password: url password;
database: url firstPathSegment.
url host
ifNil: [
url queryAt: #host ifPresent: [ :host | self host: host ].
url queryAt: #user ifPresent: [ :user | self user: user ].
url queryAt: #password ifPresent: [ :password | self password: password ] ]
ifNotNil: [
self
host: url host;
port: (url portIfAbsent: [ 5432 ]);
user: url username;
password: url password ].
self database: url firstPathSegment.
(url queryAt: #sslmode ifAbsent: [ #disable ]) = #require
ifTrue: [ self setSSL ]
]
Expand Down

0 comments on commit 6ad99e0

Please sign in to comment.