forked from cxreg/zavolaj
/
mysqlclient.p6
324 lines (275 loc) · 9.49 KB
/
mysqlclient.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# mysqlclient.p6
# Requirements:
# 1. MySQL client lib (eg on Debian, sudo apt-get install mysqlclient-dev
# 2. An account and a database on a MySQL server, for this example:
# username = testuser
# password = testpass
# database = zavolaj
# 3. Permissions, eg "grant all privileges on zavolaj.* to testuser@localhost"
# Fortunately made possible by explicitly by hardcoded support in
# parrot/src/nci/extra_thunks.nci.
# See /usr/include/mysql.h for what should be callable, or browse
# http://dev.mysql.com/doc/refman/5.1/en/c-api-function-overview.html
# Status:
# Works: affected_rows close connect error fetch_field fetch_row
# field_count free_result get_client_info init num_rows query
# real_connect stat store_result use_result
# Fails: create_db library_end library_init
# The reason for most failures is that a mapping is not available
# between the native data types and the Perl 6 data types that must be
# used in the foreign function definitions below.
# As NativeCall.pm, Rakudo and Parrot continue to evolve, more of the
# functions that have not worked may become usable.
# Volunteers, please test from time to time and give feedback in #perl6.
use NativeCall;
# -------- foreign function definitions in alphabetical order ----------
sub mysql_affected_rows( OpaquePointer $mysql_client )
returns Int
is native('libmysqlclient')
{ ... }
sub mysql_close( OpaquePointer $mysql_client )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
sub mysql_data_seek( OpaquePointer $result_set, Int $row_number )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
sub mysql_error( OpaquePointer $mysql_client)
returns Str
is native('libmysqlclient')
{ ... }
sub mysql_fetch_field( OpaquePointer $result_set )
returns Positional of Str
is native('libmysqlclient')
{ ... }
sub mysql_fetch_lengths( OpaquePointer $result_set )
returns Positional of Int
is native('libmysqlclient')
{ ... }
sub mysql_fetch_row( OpaquePointer $result_set )
returns Positional of Str
is native('libmysqlclient')
{ ... }
sub mysql_field_count( OpaquePointer $mysql_client )
returns Int
is native('libmysqlclient')
{ ... }
sub mysql_free_result( OpaquePointer $result_set )
# returns OpaquePointer
is native('libmysqlclient')
{ ... }
sub mysql_get_client_info( OpaquePointer $mysql_client)
returns Str
is native('libmysqlclient')
{ ... }
sub mysql_init( OpaquePointer $mysql_client )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
sub mysql_library_init( Int $argc, OpaquePointer $argv,
OpaquePointer $group )
returns Int
is native('libmysqlclient')
{ ... }
sub mysql_library_end()
# returns OpaquePointer # currently not working, should be void
is native('libmysqlclient')
{ ... }
sub mysql_num_rows( OpaquePointer $result_set )
returns Int
is native('libmysqlclient')
{ ... }
sub mysql_query( OpaquePointer $mysql_client, Str $sql_command )
returns Int
is native('libmysqlclient')
{ ... }
sub mysql_real_connect( OpaquePointer $mysql_client, Str $host, Str $user,
Str $password, Str $database, Int $port, Str $socket, Int $flag )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
sub mysql_stat( OpaquePointer $mysql_client)
returns Str
is native('libmysqlclient')
{ ... }
sub mysql_store_result( OpaquePointer $mysql_client )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
sub mysql_use_result( OpaquePointer $mysql_client )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
# ----------------------- main example program -------------------------
# Structure as recommended in the MySQL 5.1 Reference Manual, C API.
# Follow the 5 step general outline after the function summary in
# http://dev.mysql.com/doc/refman/5.1/en/c-api-function-overview.html
# strictly necessary only in multithreaded programs
print "library_init [currently not working] ";
#my $library_init_result = mysql_library_init( 0, pir::null__P(),
# pir::null__P() );
#if $library_init_result != 0 {
# die "could not initialize MySQL library, returned $library_init_result";
#}
say "";
say "init";
my $client = mysql_init( pir::null__P() );
print mysql_error($client);
print "get_client_info: ";
say mysql_get_client_info($client);
say "real_connect";
mysql_real_connect( $client, 'localhost', 'testuser', 'testpass',
'mysql', 0, pir::null__P(), 0 );
print mysql_error($client);
print "stat: ";
say mysql_stat($client);
say "DROP DATABASE zavolaj";
mysql_query( $client, "
DROP DATABASE zavolaj
");
print mysql_error($client);
say "CREATE DATABASE zavolaj";
mysql_query( $client, "
CREATE DATABASE zavolaj
");
print mysql_error($client);
say "USE zavolaj";
mysql_query( $client, "
USE zavolaj
");
print mysql_error($client);
say "CREATE TABLE nom";
mysql_query( $client,"
CREATE TABLE nom (
name char(4),
description char(30),
quantity int,
price numeric(5,2)
)
");
print mysql_error($client);
say "INSERT nom";
mysql_query( $client, "
INSERT nom (name, description, quantity, price)
VALUES ( 'BUBH', 'Hot beef burrito', 1, 4.95 ),
( 'TAFM', 'Mild fish taco', 1, 4.85 ),
( 'BEOM', 'Medium size orange juice', 2, 1.20 )
");
print mysql_error($client);
print "affected rows ";
my $affected_rows = mysql_affected_rows( $client );
print mysql_error($client);
say $affected_rows;
say "SELECT *, quantity*price AS amount FROM nom";
mysql_query( $client, "
SELECT *, quantity*price AS amount FROM nom
");
print mysql_error($client);
print "field_count ";
my $field_count = mysql_field_count($client);
print mysql_error($client);
say $field_count;
my @rows;
my $row_count;
my @width = 0 xx $field_count;
# There are two ways to retrieve result sets: all at once in a single
# batch, or one row at a time. Choose according to the amount of data
# and the overhead on the server and the client.
my $batch-mode;
$batch-mode = (True,False).pick; # aha, you came looking for this line :-)
#$batch-mode = False;
if $batch-mode {
# Retrieve all the rows in a single batch operation
say "store_result";
my $result_set = mysql_store_result($client);
print mysql_error($client);
say "Columns:";
loop ( my $column_number=0; $column_number<$field_count; $column_number++ ) {
my $field_info = mysql_fetch_field($result_set);
my $column_name = $field_info[0];
say " $column_name";
}
print "row_count ";
$row_count = mysql_num_rows($result_set);
print mysql_error($client);
say $row_count;
# Since mysql_fetch_fields() is not usable yet, derive the
# column widths from the maximum widths of the data in each
# column.
say "fetch_row, fetch_lengths and fetch_field";
loop ( my $row_number=0; $row_number<$row_count; $row_number++ ) {
my $row_data = mysql_fetch_row( $result_set );
my $field_length_array = mysql_fetch_lengths( $result_set );
# It would be better to be able to call mysql_fetch_fields().
# my @row = mysql_fetch_fields($result_set);
# But that cannot be implmented yet in Rakudo because the
# returned result is a packed binary record of character
# pointers, unsigned longs and unsigned ints. See mysql.h
my @row = ();
loop ( my $field_number=0; $field_number<$field_count; $field_number++ ) {
my $field = $row_data[$field_number];
# array of unsigned long segfaults
# my $chars = $field_length_array[$field_number];
my $chars = $field.chars;
@width[$field_number] = max @width[$field_number], $chars;
push @row, $field;
}
push @rows, [@row];
}
say "free_result";
mysql_free_result($result_set);
print mysql_error($client);
# Having determined the column widths by measuring every field,
# it is finally possible to pretty print the table.
loop ( my $j=0; $j<$field_count; $j++ ) {
print "+--";
print '-' x @width[$j];
}
say '+';
loop ( my $i=0; $i<$row_count; $i++ ) {
my @row = @rows[$i];
loop ( my $j=0; $j<$field_count; $j++ ) {
my $field = @row[0][$j];
print "| $field ";
print ' ' x ( @width[$j] - $field.chars );
}
say '|';
}
loop ( my $k=0; $k<$field_count; $k++ ) {
print "+--";
print '-' x @width[$k];
}
say '+';
}
else {
# Retrieve rows one at a time from the server
say "use_result";
my $result_set = mysql_use_result($client);
print mysql_error($client);
say "Columns:";
loop ( my $column_number=0; $column_number<$field_count; $column_number++ ) {
my $field_info = mysql_fetch_field($result_set);
my $column_name = $field_info[0];
say " $column_name";
}
while my $row_data = mysql_fetch_row($result_set) {
my @row;
loop ( my $field_number=0; $field_number<$field_count; $field_number++ ) {
my $field = $row_data[$field_number];
@width[$field_number] = max @width[$field_number], $field.chars;
push @row, $field;
}
@row.join(', ').say;
# no fancy boxes this time because the width of later fields is unknown
}
say "free_result";
mysql_free_result($result_set);
print mysql_error($client);
}
say "close";
mysql_close($client);
print mysql_error($client);
say "library_end [currently not working]";
#mysql_library_end();
say "mysqlclient.p6 done";