Skip to content

Commit d74909d

Browse files
committed
Many fixes :
* remove old-style lanbda functions * fix many occurences of ponylang/ponyc#1983 * fixes in the batch mechanism
1 parent 0831bcc commit d74909d

11 files changed

+181
-252
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PKG=pg
22
BUILD_DIR=build
3-
PONYC=ponyc
3+
PONYC=/usr/local/bin/ponyc
44
PONY_SRC=$(shell find . -name "*.pony")
55
BIN_DIR=$(BUILD_DIR)/release
66
BIN=$(BIN_DIR)/example

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ of this PoC is to experiment user-facing API and internal design alternatives.
2525

2626
### Needed in the proof of concept
2727

28+
- Cursors (prevents from loading the entire dataset of a result)
2829
- Prepared statements
2930
- Codecs for values
3031
- binary values first
@@ -43,7 +44,6 @@ of this PoC is to experiment user-facing API and internal design alternatives.
4344
- Get and set connection parameters (these are currently fetched from the
4445
server but not exposed on the API)
4546
- Transaction Management
46-
- Cursors (prevents from loading the entire dataset of a result)
4747
- Support logical decoding (I've already wrote a C/Python library for
4848
that, it's heavily asynchronous, it'll be a pleasure to do this in pony)
4949

example/main.pony

+55-47
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
22
main.pony
3-
4-
53
"""
4+
65
use "pg"
76
use "pg/codec"
87
use "pg/introspect"
@@ -34,34 +33,38 @@ class User
3433

3534

3635
class BlogEntryRecordNotify is FetchNotify
37-
var entries: (Array[BlogEntry val] trn | Array[BlogEntry val] val) = recover trn Array[BlogEntry val] end
36+
var entries: Array[BlogEntry val] trn = recover trn Array[BlogEntry val] end
3837
let view: BlogEntriesView tag
3938
let logger: Logger[String val] val
39+
4040
new iso create(v: BlogEntriesView tag, out: OutStream) =>
4141
view = v
4242
logger = StringLogger(Warn, out)
4343

44-
fun ref descirption(desc: RowDescription) => None
45-
fun size(): USize => 10
44+
fun ref descirption(desc: RowDescription) =>
45+
logger.log("Got description")
46+
47+
fun size(): USize => 1000
48+
4649
fun ref batch(b: Array[Record val] val, next: FetchNotifyNext val) =>
47-
logger(Fine) and logger.log("Fetch entries")
48-
if b.size() == size() then
49-
try
50-
for r in b.values() do
51-
let e = recover val BlogEntry(
52-
r(0) as I32,
53-
2, 3
54-
/*r(1) as I32,*/
55-
/*r(2) as I32*/
56-
) end
57-
Debug.out(e.string())
58-
(entries as Array[BlogEntry val] trn).push(e)
59-
end
50+
logger.log("Fetch entries")
51+
try
52+
for r in b.values() do
53+
// logger.log((r(0) as I32).string())
54+
let e = recover val BlogEntry(
55+
r(0) as I32,
56+
2, 3
57+
//r(1) as I32,
58+
//r(2) as I32
59+
) end
60+
// logger.log(e.string())
61+
entries.push(e)
6062
end
61-
next(None)
6263
end
64+
next(None)
65+
6366
fun ref record(r: Record val) =>
64-
Debug.out(".")
67+
logger.log(".")
6568
try
6669
let e = recover val BlogEntry(
6770
r(0) as I32,
@@ -74,19 +77,20 @@ class BlogEntryRecordNotify is FetchNotify
7477

7578
end
7679
fun ref stop() =>
77-
try
78-
entries = recover val entries as Array[BlogEntry val] trn end
79-
view.entries(entries)
80-
end
80+
logger.log("stop")
81+
let entries' = entries = recover trn Array[BlogEntry val] end
82+
view.entries(consume val entries')
8183

8284

8385
class UserRecordNotify is FetchNotify
8486
let entries: Array[BlogEntry] = Array[BlogEntry]
8587
let view: BlogEntriesView tag
8688
let logger: Logger[String val] val
89+
8790
new create(v: BlogEntriesView tag, out: OutStream) =>
8891
view = v
8992
logger = StringLogger(Fine, out)
93+
9094
fun ref descirption(desc: RowDescription) => None
9195

9296
fun ref batch(b: Array[Record val] val, next: FetchNotifyNext val) =>
@@ -103,6 +107,7 @@ class UserRecordNotify is FetchNotify
103107
try
104108
view.user(recover User(r("id") as I32) end)
105109
end
110+
106111
fun ref stop() => None
107112

108113

@@ -122,24 +127,27 @@ actor BlogEntriesView
122127
Debug("fetch_entries")
123128
(_conn as Connection).fetch(
124129
/*"SELECT 1 as user_id, 2, 3 UNION ALL SELECT 4 as user_id, 5, 6 UNION ALL SELECT 7 as user_id, 8, 9",*/
125-
"SELECT generate_series(0,100)",
130+
"SELECT generate_series(0,10000)",
126131
recover BlogEntryRecordNotify(this, out) end)
127132
end
128133

129134
be fetch_user() =>
135+
logger.log("fetch_user")
130136
try
131137
(_conn as Connection).fetch(
132138
"SELECT 1 as id",
133139
recover UserRecordNotify(this, out) end)
134140
end
135141

136142
be user(u: User iso) =>
143+
logger.log("got user #" + u.id.string())
137144
_user = recover val consume u end
138145
Debug.out("###")
139146
fetch_entries()
140147

141148
be render(entries': Array[BlogEntry val] val) =>
142149
Debug.out("render")
150+
logger.log("render")
143151
try logger.log(entries'.size().string() + " " + entries'(0).string()) end
144152
/*logger.log(entries'.size().string())*/
145153
try (_conn as Connection).release() end
@@ -151,62 +159,62 @@ actor BlogEntriesView
151159
be apply(conn: Connection tag) =>
152160
_conn = conn
153161
fetch_user()
154-
_entries.next[BlogEntriesView](recover this~render() end)
162+
_entries.next[None](recover this~render() end)
155163

156164

157165
actor Main
158166
let session: Session
159167
let _env: Env
168+
let logger: Logger[String val] val
160169

161170
new create(env: Env) =>
162171
_env = env
163-
let logger = StringLogger(Fine, env.out)
164-
session = Session(env where user="macflytest",
165-
password=EnvPasswordProvider(env),
166-
database="macflytest")
172+
logger = StringLogger(Fine, env.out)
173+
session = Session(env where password=EnvPasswordProvider(env))
167174
let that = recover tag this end
168-
"""
169175
session.execute("SELECT generate_series(0,1)",
170176
recover val
171-
lambda(r: Rows val)(that) =>
177+
{(r: Rows val)(that) =>
172178
that.raw_count(r)
173179
None
174-
end
180+
}
175181
end)
176182

177183
session.execute("SELECT 42, 24 as foo;;",
178184
recover val
179-
lambda(r: Rows val)(that) =>
180-
that.raw_handler(r)
181-
end
185+
{(r: Rows val)(that) =>
186+
that.raw_handler(r)
187+
}
182188
end)
183189

184190

185191
session.execute("SELECT $1, $2 as foo",
186192
recover val
187-
lambda(r: Rows val)(that) =>
193+
{(r: Rows val)(that) =>
188194
that.execute_handler(r)
189-
end
195+
}
190196
end,
191-
recover val [as PGValue: I32(70000), I32(-100000)] end)
192-
"""
197+
recover val [as PGValue: I32(70000); I32(-100000)] end)
198+
199+
193200
let p = session.connect(recover val
194-
lambda(c: Connection tag)(env) =>
201+
{(c: Connection tag)(env) =>
195202
BlogEntriesView(env.out)(c)
196-
end
203+
}
197204
end)
198205

206+
199207
be raw_count(rows: Rows val) =>
200-
_env.out.print(rows.size().string())
208+
logger.log("rows: " + rows.size().string())
201209

202210
be raw_handler(rows: Rows val) =>
203211
for row in rows.values() do
204-
try Debug.out(row(0) as I32) end
205-
try Debug.out(row("foo") as I32) end
212+
try logger.log((row(0) as I32).string()) end
213+
try logger.log((row("foo") as I32).string()) end
206214
end
207215

208216
be execute_handler(rows: Rows val) =>
209217
for row in rows.values() do
210-
try Debug.out(row(0) as I32) end
211-
try Debug.out(row("foo") as I32) end
218+
try logger.log((row(0) as I32).string()) end
219+
try logger.log((row("foo") as I32).string()) end
212220
end

pg/codec/registry.pony

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ primitive DecodeText
5656
fun apply(type_oid: I32, value: Array[U8] val): PGValue ? =>
5757
match type_oid
5858
| 23 => String.from_array(value).i32()
59+
//| 23 => I32(1)
5960
else
6061
Debug.out("Unknown type OID: " + type_oid.string()); error
6162
end
@@ -69,6 +70,7 @@ primitive DecodeBinary
6970
result = (result << 8) + i.i32()
7071
end
7172
result
73+
// I32(1)
7274
else
7375
Debug.out("Unknown type OID: " + type_oid.string()); error
7476
end

0 commit comments

Comments
 (0)