Browse files

[examples/mysqlclient.p6] added several API functions and some docs

  • Loading branch information...
1 parent d27d5a8 commit 0e78155fd3e5fb7088bbf81374b4df40d5d87e00 Martin Berends committed Mar 9, 2010
Showing with 100 additions and 32 deletions.
  1. +4 −3 README
  2. +96 −29 examples/mysqlclient.p6
View
7 README
@@ -10,9 +10,10 @@ complement Blizkost, a Rakudo-to-Perl-5 integration project.
MySQL
-Initially the mysql client library is being used as a fairly substantial
-test case. The result might become a useful component in the Rakudo *
-release.
+Initially the MySQL client library is being used as a test case, because
+it is a popular application, is frequently asked about, and because
+there is specific support for MySQL in parrot/src/nci/extra_thunks.nci.
+Some of the code might become a part of the Rakudo * distribution.
The /usr/include/mysql.h file defines the mysql client library functions
and their parameters.
View
125 examples/mysqlclient.p6
@@ -1,28 +1,40 @@
# mysqlclient.p6
-# Fortunately supported by explicitly by hardcoded support in
+# 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.
+# 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: init, connect, query.
-# Fails: create_db.
-# Needs: fetch_row_array (returns UnManagedStruct)
+# 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;
class OpaquePointer { ... }
# hack compensating for too-late import of OpaquePointer from NativeCall.pm
-# ------------------- foreign function definitions ---------------------
+# -------- foreign function definitions in alphabetical order ----------
-sub mysql_init( OpaquePointer $mysql_client)
+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_real_connect( OpaquePointer $mysql_client, Str $host, Str $user,
- Str $password, Str $database, Int $port, Str $socket, Int $flag )
+sub mysql_data_seek( OpaquePointer $result_set, Int $row_number )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
@@ -32,38 +44,49 @@ sub mysql_error( OpaquePointer $mysql_client)
is native('libmysqlclient')
{ ... }
-sub mysql_stat( OpaquePointer $mysql_client)
- returns Str
+sub mysql_fetch_field( OpaquePointer $result_set )
+ returns OpaquePointer
is native('libmysqlclient')
{ ... }
-sub mysql_get_client_info( OpaquePointer $mysql_client)
- returns Str
+sub mysql_fetch_lengths( OpaquePointer $result_set )
+ returns Positional of Int
is native('libmysqlclient')
{ ... }
-sub mysql_query( OpaquePointer $mysql_client, Str $sql_command )
+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_store_result( OpaquePointer $mysql_client )
+sub mysql_free_result( OpaquePointer $result_set )
returns OpaquePointer
is native('libmysqlclient')
{ ... }
-sub mysql_use_result( OpaquePointer $mysql_client )
+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_field_count( OpaquePointer $mysql_client )
+sub mysql_library_init( Int $argc, OpaquePointer $argv,
+ OpaquePointer $group )
returns Int
is native('libmysqlclient')
{ ... }
-sub mysql_fetch_row( OpaquePointer $result_set )
- returns Positional of Str
+sub mysql_library_end()
+ returns OpaquePointer # currently not working, should be void
is native('libmysqlclient')
{ ... }
@@ -72,27 +95,62 @@ sub mysql_num_rows( OpaquePointer $result_set )
is native('libmysqlclient')
{ ... }
-sub mysql_fetch_field( OpaquePointer $result_set )
+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_free_result( OpaquePointer $result_set )
+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
@@ -111,12 +169,6 @@ mysql_query( $client, "
");
print mysql_error($client);
-print "stat: ";
-say mysql_stat($client);
-
-print "get_client_info: ";
-say mysql_get_client_info($client);
-
say "CREATE TABLE nom";
mysql_query( $client,"
CREATE TABLE nom (
@@ -137,6 +189,11 @@ mysql_query( $client, "
");
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
@@ -171,9 +228,10 @@ if $batch-mode {
# 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 and fetch_field";
+ 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);
@@ -183,7 +241,10 @@ if $batch-mode {
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;
+# 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];
@@ -236,5 +297,11 @@ else {
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";

0 comments on commit 0e78155

Please sign in to comment.