Skip to content

Commit 8c5f174

Browse files
committed
Merge branch 'master' into deprecated_cancel_methods
2 parents 92d23da + 52ed83e commit 8c5f174

File tree

4 files changed

+122
-104
lines changed

4 files changed

+122
-104
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ void main(string[] args)
6868
"123 as field_3, 456.78 as field_4, '{\"JSON field name\": 123.456}'::json"
6969
);
7070
71-
writeln( "Text query result by name: ", answer[0]["current_time"].as!PGtext );
72-
writeln( "Text query result by index: ", answer[0][3].as!PGtext );
71+
writeln( "Text query result by name: ", answer[0]["current_time"].as!string );
72+
writeln( "Text query result by index: ", answer[0][3].as!string );
7373
7474
// It is possible to read values of unknown type using BSON:
7575
auto firstRow = answer[0];
@@ -98,15 +98,15 @@ void main(string[] args)
9898
auto r = conn.execParams(p);
9999
scope(exit) destroy(r);
100100
101-
writeln( "0: ", r[0]["double_field"].as!PGdouble_precision );
102-
writeln( "1: ", r.oneRow[1].as!PGtext ); // .oneRow additionally checks that here is only one row was returned
101+
writeln( "0: ", r[0]["double_field"].as!double );
102+
writeln( "1: ", r.oneRow[1].as!string ); // .oneRow additionally checks that here is only one row was returned
103103
writeln( "2.1 isNull: ", r[0][2].isNull );
104104
writeln( "2.2 isNULL: ", r[0].isNULL(2) );
105-
writeln( "3.1: ", r[0][3].asArray[0].as!PGtext );
106-
writeln( "3.2: ", r[0][3].asArray[1].as!PGtext );
105+
writeln( "3.1: ", r[0][3].asArray[0].as!string );
106+
writeln( "3.2: ", r[0][3].asArray[1].as!string );
107107
writeln( "3.3: ", r[0]["array_field"].asArray[2].isNull );
108108
writeln( "3.4: ", r[0]["array_field"].asArray.isNULL(2) );
109-
writeln( "4.1: ", r[0]["multi_array"].asArray.getValue(1, 2).as!PGinteger );
109+
writeln( "4.1: ", r[0]["multi_array"].asArray.getValue(1, 2).as!int );
110110
writeln( "4.2: ", r[0]["multi_array"].as!(int[][]) );
111111
writeln( "5.1 Json: ", r[0]["json_value"].as!Json);
112112
writeln( "5.2 Bson: ", r[0]["json_value"].as!Bson);

example/example.d

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ void main(string[] args)
2020
"123 as field_3, 456.78 as field_4, '{\"JSON field name\": 123.456}'::json"
2121
);
2222

23-
writeln( "Text query result by name: ", answer[0]["current_time"].as!PGtext );
24-
writeln( "Text query result by index: ", answer[0][3].as!PGtext );
23+
writeln( "Text query result by name: ", answer[0]["current_time"].as!string );
24+
writeln( "Text query result by index: ", answer[0][3].as!string );
2525

2626
// It is possible to read values of unknown type using BSON:
2727
auto firstRow = answer[0];
@@ -50,15 +50,15 @@ void main(string[] args)
5050
auto r = conn.execParams(p);
5151
scope(exit) destroy(r);
5252

53-
writeln( "0: ", r[0]["double_field"].as!PGdouble_precision );
54-
writeln( "1: ", r.oneRow[1].as!PGtext ); // .oneRow additionally checks that here is only one row was returned
53+
writeln( "0: ", r[0]["double_field"].as!double );
54+
writeln( "1: ", r.oneRow[1].as!string ); // .oneRow additionally checks that here is only one row was returned
5555
writeln( "2.1 isNull: ", r[0][2].isNull );
5656
writeln( "2.2 isNULL: ", r[0].isNULL(2) );
57-
writeln( "3.1: ", r[0][3].asArray[0].as!PGtext );
58-
writeln( "3.2: ", r[0][3].asArray[1].as!PGtext );
57+
writeln( "3.1: ", r[0][3].asArray[0].as!string );
58+
writeln( "3.2: ", r[0][3].asArray[1].as!string );
5959
writeln( "3.3: ", r[0]["array_field"].asArray[2].isNull );
6060
writeln( "3.4: ", r[0]["array_field"].asArray.isNULL(2) );
61-
writeln( "4.1: ", r[0]["multi_array"].asArray.getValue(1, 2).as!PGinteger );
61+
writeln( "4.1: ", r[0]["multi_array"].asArray.getValue(1, 2).as!int );
6262
writeln( "4.2: ", r[0]["multi_array"].as!(int[][]) );
6363
writeln( "5.1 Json: ", r[0]["json_value"].as!Json);
6464
writeln( "5.2 Bson: ", r[0]["json_value"].as!Bson);

src/dpq2/connection.d

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -208,44 +208,11 @@ class Connection
208208
}
209209

210210
/// Obtains duplicate file descriptor number of the connection socket to the server
211-
version(Posix)
212-
socket_t posixSocketDuplicate()
211+
auto posixSocketDuplicate()
213212
{
214-
import core.sys.posix.unistd: dup;
213+
import dpq2.socket_stuff;
215214

216-
static assert(socket_t.sizeof == int.sizeof);
217-
218-
return cast(socket_t) dup(cast(socket_t) posixSocket);
219-
}
220-
221-
/// Obtains duplicate file descriptor number of the connection socket to the server
222-
version(Windows)
223-
SOCKET posixSocketDuplicate()
224-
{
225-
import core.stdc.stdlib: malloc, free;
226-
import core.sys.windows.winbase: GetCurrentProcessId;
227-
228-
auto protocolInfo = cast(WSAPROTOCOL_INFOW*) malloc(WSAPROTOCOL_INFOW.sizeof);
229-
scope(failure) free(protocolInfo);
230-
231-
int dupStatus = WSADuplicateSocketW(posixSocket, GetCurrentProcessId, protocolInfo);
232-
233-
if(dupStatus)
234-
throw new ConnectionException("WSADuplicateSocketW error, code "~WSAGetLastError().to!string);
235-
236-
SOCKET s = WSASocketW(
237-
FROM_PROTOCOL_INFO,
238-
FROM_PROTOCOL_INFO,
239-
FROM_PROTOCOL_INFO,
240-
protocolInfo,
241-
0,
242-
0
243-
);
244-
245-
if(s == INVALID_SOCKET)
246-
throw new ConnectionException("WSASocket error, code "~WSAGetLastError().to!string);
247-
248-
return s;
215+
return posixSocket.duplicateSocket;
249216
}
250217

251218
/// Obtains std.socket.Socket of the connection to the server
@@ -254,8 +221,6 @@ class Connection
254221
/// duplicate of internal posix socket will be used.
255222
Socket socket()
256223
{
257-
version(Windows) static assert(SOCKET.sizeof == socket_t.sizeof);
258-
259224
return new Socket(cast(socket_t) posixSocketDuplicate, AddressFamily.UNSPEC);
260225
}
261226

@@ -430,58 +395,6 @@ class Connection
430395
}
431396
}
432397

433-
// Socket duplication stuff for Win32
434-
version(Windows)
435-
private
436-
{
437-
import core.sys.windows.windef;
438-
import core.sys.windows.basetyps: GUID;
439-
440-
alias GROUP = uint;
441-
442-
enum INVALID_SOCKET = 0;
443-
enum FROM_PROTOCOL_INFO =-1;
444-
enum MAX_PROTOCOL_CHAIN = 7;
445-
enum WSAPROTOCOL_LEN = 255;
446-
447-
struct WSAPROTOCOLCHAIN
448-
{
449-
int ChainLen;
450-
DWORD[MAX_PROTOCOL_CHAIN] ChainEntries;
451-
}
452-
453-
struct WSAPROTOCOL_INFOW
454-
{
455-
DWORD dwServiceFlags1;
456-
DWORD dwServiceFlags2;
457-
DWORD dwServiceFlags3;
458-
DWORD dwServiceFlags4;
459-
DWORD dwProviderFlags;
460-
GUID ProviderId;
461-
DWORD dwCatalogEntryId;
462-
WSAPROTOCOLCHAIN ProtocolChain;
463-
int iVersion;
464-
int iAddressFamily;
465-
int iMaxSockAddr;
466-
int iMinSockAddr;
467-
int iSocketType;
468-
int iProtocol;
469-
int iProtocolMaxOffset;
470-
int iNetworkByteOrder;
471-
int iSecurityScheme;
472-
DWORD dwMessageSize;
473-
DWORD dwProviderReserved;
474-
WCHAR[WSAPROTOCOL_LEN+1] szProtocol;
475-
}
476-
477-
extern(Windows) nothrow @nogc
478-
{
479-
import core.sys.windows.winsock2: WSAGetLastError;
480-
int WSADuplicateSocketW(SOCKET s, DWORD dwProcessId, WSAPROTOCOL_INFOW* lpProtocolInfo);
481-
SOCKET WSASocketW(int af, int type, int protocol, WSAPROTOCOL_INFOW*, GROUP, DWORD dwFlags);
482-
}
483-
}
484-
485398
private auto keyValToPQparamsArrays(in string[string] keyValueParams)
486399
{
487400
static struct PQparamsArrays

src/dpq2/socket_stuff.d

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
///
2+
module dpq2.socket_stuff;
3+
4+
import dpq2.connection: ConnectionException;
5+
import std.socket;
6+
7+
/// Obtains duplicate file descriptor number of the socket
8+
version(Posix)
9+
socket_t duplicateSocket(int socket)
10+
{
11+
import core.sys.posix.unistd: dup;
12+
13+
static assert(socket_t.sizeof == int.sizeof);
14+
15+
int ret = dup(socket);
16+
17+
if(ret == -1)
18+
throw new ConnectionException("Socket duplication error");
19+
20+
return cast(socket_t) ret;
21+
}
22+
23+
/// Obtains duplicate file descriptor number of the socket
24+
version(Windows)
25+
SOCKET duplicateSocket(int socket)
26+
{
27+
import core.stdc.stdlib: malloc, free;
28+
import core.sys.windows.winbase: GetCurrentProcessId;
29+
30+
static assert(SOCKET.sizeof == socket_t.sizeof);
31+
32+
auto protocolInfo = cast(WSAPROTOCOL_INFOW*) malloc(WSAPROTOCOL_INFOW.sizeof);
33+
scope(failure) free(protocolInfo);
34+
35+
int dupStatus = WSADuplicateSocketW(socket, GetCurrentProcessId, protocolInfo);
36+
37+
if(dupStatus)
38+
throw new ConnectionException("WSADuplicateSocketW error, code "~WSAGetLastError().to!string);
39+
40+
SOCKET s = WSASocketW(
41+
FROM_PROTOCOL_INFO,
42+
FROM_PROTOCOL_INFO,
43+
FROM_PROTOCOL_INFO,
44+
protocolInfo,
45+
0,
46+
0
47+
);
48+
49+
if(s == INVALID_SOCKET)
50+
throw new ConnectionException("WSASocket error, code "~WSAGetLastError().to!string);
51+
52+
return s;
53+
}
54+
55+
// Socket duplication structs for Win32
56+
version(Windows)
57+
private
58+
{
59+
import core.sys.windows.windef;
60+
import core.sys.windows.basetyps: GUID;
61+
62+
alias GROUP = uint;
63+
64+
enum INVALID_SOCKET = 0;
65+
enum FROM_PROTOCOL_INFO =-1;
66+
enum MAX_PROTOCOL_CHAIN = 7;
67+
enum WSAPROTOCOL_LEN = 255;
68+
69+
struct WSAPROTOCOLCHAIN
70+
{
71+
int ChainLen;
72+
DWORD[MAX_PROTOCOL_CHAIN] ChainEntries;
73+
}
74+
75+
struct WSAPROTOCOL_INFOW
76+
{
77+
DWORD dwServiceFlags1;
78+
DWORD dwServiceFlags2;
79+
DWORD dwServiceFlags3;
80+
DWORD dwServiceFlags4;
81+
DWORD dwProviderFlags;
82+
GUID ProviderId;
83+
DWORD dwCatalogEntryId;
84+
WSAPROTOCOLCHAIN ProtocolChain;
85+
int iVersion;
86+
int iAddressFamily;
87+
int iMaxSockAddr;
88+
int iMinSockAddr;
89+
int iSocketType;
90+
int iProtocol;
91+
int iProtocolMaxOffset;
92+
int iNetworkByteOrder;
93+
int iSecurityScheme;
94+
DWORD dwMessageSize;
95+
DWORD dwProviderReserved;
96+
WCHAR[WSAPROTOCOL_LEN+1] szProtocol;
97+
}
98+
99+
extern(Windows) nothrow @nogc
100+
{
101+
import core.sys.windows.winsock2: WSAGetLastError;
102+
int WSADuplicateSocketW(SOCKET s, DWORD dwProcessId, WSAPROTOCOL_INFOW* lpProtocolInfo);
103+
SOCKET WSASocketW(int af, int type, int protocol, WSAPROTOCOL_INFOW*, GROUP, DWORD dwFlags);
104+
}
105+
}

0 commit comments

Comments
 (0)