Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 196 lines (159 sloc) 5.051 kb
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
1 # mysqlclient.p6
2
3 # Fortunately supported by explicitly by hardcoded support in
4 # parrot/src/nci/extra_thunks.nci.
5 # See /usr/include/mysql.h for what should be callable.
6
7 # Status:
8 # Works: init, connect, query.
9 # Fails: create_db.
10 # Needs: fetch_row_array (returns UnManagedStruct)
11
12 use NativeCall;
13
14 class CPointer { ... }
15 # hack compensating for too-late import of CPointer from NativeCall.pm
16
17 # ------------------- foreign function definitions ---------------------
18
19 sub mysql_init( CPointer $mysql_client)
20 returns CPointer
21 is native('libmysqlclient')
22 { ... }
23
24 sub mysql_real_connect( CPointer $mysql_client, Str $host, Str $user,
25 Str $password, Str $database, Int $port, Str $socket, Int $flag )
26 returns CPointer
27 is native('libmysqlclient')
28 { ... }
29
30 sub mysql_error( CPointer $mysql_client)
31 returns Str
32 is native('libmysqlclient')
33 { ... }
34
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
35 sub mysql_stat( CPointer $mysql_client)
36 returns Str
37 is native('libmysqlclient')
38 { ... }
39
40 sub mysql_get_client_info( CPointer $mysql_client)
41 returns Str
42 is native('libmysqlclient')
43 { ... }
44
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
45 sub mysql_query( CPointer $mysql_client, Str $sql_command )
46 returns Int
47 is native('libmysqlclient')
48 { ... }
49
50 sub mysql_store_result( CPointer $mysql_client )
51 returns CPointer
52 is native('libmysqlclient')
53 { ... }
54
55 sub mysql_use_result( CPointer $mysql_client )
56 returns CPointer
57 is native('libmysqlclient')
58 { ... }
59
60 sub mysql_field_count( CPointer $mysql_client )
61 returns Int
62 is native('libmysqlclient')
63 { ... }
64
65 sub mysql_fetch_row( CPointer $result_set )
66 returns CPointer
67 is native('libmysqlclient')
68 { ... }
69
70 sub mysql_num_rows( CPointer $result_set )
71 returns Int
72 is native('libmysqlclient')
73 { ... }
74
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
75 sub mysql_fetch_field( CPointer $result_set )
76 returns CPointer
77 is native('libmysqlclient')
78 { ... }
79
80 sub mysql_free_result( CPointer $result_set )
81 returns CPointer
82 is native('libmysqlclient')
83 { ... }
84
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
85 # ----------------------- main example program -------------------------
86
87 say "init";
88 my $client = mysql_init( pir::null__P() );
89 print mysql_error($client);
90
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
91 say "real_connect";
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
92 mysql_real_connect( pir::descalarref__PP($client), 'localhost', 'testuser',
93 'testpass', 'mysql', 0, pir::null__P(), 0 );
94 print mysql_error($client);
95
96 say "DROP DATABASE zavolaj";
97 mysql_query( pir::descalarref__PP($client), "
98 DROP DATABASE zavolaj
99 ");
100 print mysql_error($client);
101
102 say "CREATE DATABASE zavolaj";
103 mysql_query( pir::descalarref__PP($client), "
104 CREATE DATABASE zavolaj
105 ");
106 print mysql_error($client);
107
108 say "USE zavolaj";
109 mysql_query( pir::descalarref__PP($client), "
110 USE zavolaj
111 ");
112 print mysql_error($client);
113
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
114 print "stat: ";
115 say mysql_stat($client);
116
117 print "get_client_info: ";
118 say mysql_get_client_info($client);
119
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
120 say "CREATE TABLE nom";
121 mysql_query( pir::descalarref__PP($client),"
122 CREATE TABLE nom (
123 name char(4),
124 description char(30),
125 quantity int,
126 price numeric(5,2)
127 )
128 ");
129 print mysql_error($client);
130
131 say "INSERT nom";
132 mysql_query( pir::descalarref__PP($client), "
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
133 INSERT nom (name, description, quantity, price)
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
134 VALUES ( 'BUBH', 'Hot beef burrito', 1, 4.95 ),
135 ( 'TAFM', 'Mild fish taco', 1, 4.85 ),
136 ( 'BEOM', 'Medium size orange juice', 2, 1.20 )
137 ");
138 print mysql_error($client);
139
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
140 say "SELECT *, quantity*price AS amount FROM nom";
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
141 mysql_query( pir::descalarref__PP($client), "
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
142 SELECT *, quantity*price AS amount FROM nom
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
143 ");
144 print mysql_error($client);
145
146 print "field_count ";
147 my $field_count = mysql_field_count($client);
148 print mysql_error($client);
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
149 say $field_count;
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
150
151 # There are two ways to retrieve result sets: all at once in a single
152 # batch, or one row at a time. Choose according to the amount of data
153 # and the overhead on the server and the client.
154 my $batch-mode = True;
155 if $batch-mode {
156 # Retrieve all the rows in a single batch operation
157 say "store_result";
158 my $result_set = mysql_store_result($client);
159 print mysql_error($client);
160
161 print "num_rows ";
162 my $num_rows = mysql_num_rows($result_set);
163 print mysql_error($client);
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
164 say $num_rows;
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
165
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
166 say "fetch_row and fetch_field";
167 loop ( my $row_number=0; $row_number<$num_rows; $row_number++ ) {
168 print "row $row_number: ";
169 my $row_data = mysql_fetch_row( $result_set );
170 # It would be better to be able to call mysql_fetch_fields().
171 loop ( my $field_number=0; $field_number<$field_count; $field_number++ ) {
172 print "field $field_number ";
173 # my $field = mysql_fetch_field( $result_set );
174 }
175 say " ";
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
176 }
ee1bc1c [examples/mysqlclient.p6] renamed from connect.p6, and shinier
Martin Berends authored
177 say "free_result";
178 mysql_free_result($result_set);
179 print mysql_error($client);
180
c379285 [examples/mysqlclient] add CREATE TABLE and INSERT, but not yet SELECT
Martin Berends authored
181 }
182 else {
183 # Retrieve rows one at a time from the server
184 say "use_result";
185 my $result_set = mysql_use_result($client);
186 print mysql_error($client);
187
188 my $row;
189 while ( $row = mysql_fetch_row($result_set) ) {
190 say "row";
191 }
192 }
193
194 say " ";
195 say "mysqlclient.p6 done";
Something went wrong with that request. Please try again.