Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 165 lines (135 sloc) 4.226 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
35 sub mysql_query( CPointer $mysql_client, Str $sql_command )
36 returns Int
37 is native('libmysqlclient')
38 { ... }
39
40 sub mysql_store_result( CPointer $mysql_client )
41 returns CPointer
42 is native('libmysqlclient')
43 { ... }
44
45 sub mysql_use_result( CPointer $mysql_client )
46 returns CPointer
47 is native('libmysqlclient')
48 { ... }
49
50 sub mysql_field_count( CPointer $mysql_client )
51 returns Int
52 is native('libmysqlclient')
53 { ... }
54
55 sub mysql_fetch_row( CPointer $result_set )
56 returns CPointer
57 is native('libmysqlclient')
58 { ... }
59
60 sub mysql_num_rows( CPointer $result_set )
61 returns Int
62 is native('libmysqlclient')
63 { ... }
64
65 # ----------------------- main example program -------------------------
66
67 say "init";
68 my $client = mysql_init( pir::null__P() );
69 print mysql_error($client);
70
71 say "connect";
72 mysql_real_connect( pir::descalarref__PP($client), 'localhost', 'testuser',
73 'testpass', 'mysql', 0, pir::null__P(), 0 );
74 print mysql_error($client);
75
76 say "DROP DATABASE zavolaj";
77 mysql_query( pir::descalarref__PP($client), "
78 DROP DATABASE zavolaj
79 ");
80 print mysql_error($client);
81
82 say "CREATE DATABASE zavolaj";
83 mysql_query( pir::descalarref__PP($client), "
84 CREATE DATABASE zavolaj
85 ");
86 print mysql_error($client);
87
88 say "USE zavolaj";
89 mysql_query( pir::descalarref__PP($client), "
90 USE zavolaj
91 ");
92 print mysql_error($client);
93
94 say "CREATE TABLE nom";
95 mysql_query( pir::descalarref__PP($client),"
96 CREATE TABLE nom (
97 name char(4),
98 description char(30),
99 quantity int,
100 price numeric(5,2)
101 )
102 ");
103 print mysql_error($client);
104
105 say "INSERT nom";
106 mysql_query( pir::descalarref__PP($client), "
107 INSERT nom (name, description, price)
108 VALUES ( 'BUBH', 'Hot beef burrito', 1, 4.95 ),
109 ( 'TAFM', 'Mild fish taco', 1, 4.85 ),
110 ( 'BEOM', 'Medium size orange juice', 2, 1.20 )
111 ");
112 print mysql_error($client);
113
114 say "SELECT * FROM nom";
115 mysql_query( pir::descalarref__PP($client), "
116 SELECT * FROM nom
117 ");
118 print mysql_error($client);
119
120 print "field_count ";
121 my $field_count = mysql_field_count($client);
122 say $field_count;
123 print mysql_error($client);
124
125 # There are two ways to retrieve result sets: all at once in a single
126 # batch, or one row at a time. Choose according to the amount of data
127 # and the overhead on the server and the client.
128 my $batch-mode = True;
129 if $batch-mode {
130 # Retrieve all the rows in a single batch operation
131 say "store_result";
132 my $result_set = mysql_store_result($client);
133 print mysql_error($client);
134
135 print "num_rows ";
136 my $num_rows = mysql_num_rows($result_set);
137 if $num_rows == 0 {
138 # fudge the row count until the unsigned long long return type works
139 print "*FUDGED* ";
140 $num_rows = 3; # because we know there are 3 rows
141 }
142 say $num_rows;
143 print mysql_error($client);
144
145 say "fetch_row";
146 my $row_number;
147 loop ( $row_number=0; $row_number<$num_rows; $row_number++ ) {
148 # my $row_data = mysql_fetch_row( $result_set );
149 }
150 }
151 else {
152 # Retrieve rows one at a time from the server
153 say "use_result";
154 my $result_set = mysql_use_result($client);
155 print mysql_error($client);
156
157 my $row;
158 while ( $row = mysql_fetch_row($result_set) ) {
159 say "row";
160 }
161 }
162
163 say " ";
164 say "mysqlclient.p6 done";
Something went wrong with that request. Please try again.