DBIx::JSON - Perl extension for creating JSON from existing DBI datasources
This module is perl extension for creating JSON from existing DBI datasources.
One use of this module might be to extract data on the web server, and send the raw data (in JSON format) to a client's browser, and then JavaScript do eval it to generate dynamic HTML.
This module was inspired by DBIx::XML_RDB.
Version 0.02
my $dsn = "dbname=$dbname;host=$host;port=$port"; print DBIx::JSON->new( $dsn, "mysql", $dbusername, $dbpasswd ) ->do_select("select * from table;")->get_json; or my $dsn = "dbname=$dbname;host=$host;port=$port"; my $obj = DBIx::JSON->new($dsn, "mysql", $dbusername, $dbpasswd); $obj->do_select("select * from table;", "colmun1", 1); $obj->err && die $obj->errstr; print $obj->get_json;
None.
DBIx::JSON->new( $dsn, $dbidriver, $dbusername, $dbpasswd );
This method is a constructor. See the DBI documentation for what each of these means.
$obj->do_select( $sql [, $key_field [, $hash_array] ] );
This takes a SELECT command string, and calls DBI::selectall_arrayref method. If $key_field is given, this calls DBI::selectall_hashref method. See the DBI documentation for details. $hash_array affects get_json method.
This doesn't do any checking if the sql is valid. Subsequent calls to do_select do overwrite the output.
$obj->do_sql( $sql );
This takes a non SELECT command string (e.g. UPDATE/INSERT/DELETE), and calls DBI::do method. See the DBI documentation for details.
$obj->get_json;
Simply returns the JSON generated from the previous SQL call. The format of the JSON output is something like this:
# default [ ["user1", "data1"], ["user2", "data2"], ["user3", "data3"], ... ] # if do_select was called with $key_field { "user1":{"data":"data1", "name":"user1"}, "user2":{"data":"data2", "name":"user2"}, "user3":{"data":"data3", "name":"user3"}, ... } # if do_select was called with $hash_array [ {"data":"data1", "name":"user1"}, {"data":"data2", "name":"user2"}, {"data":"data3", "name":"user3"}, ... ]
$obj->has_data;
This returns whether get_json method can be called or not.
$obj->clear_data;
This clears the results from the previous SQL call.
$obj->err;
This returns $DBI::err.
$obj->errstr;
This returns $DBI::errstr.
JSON::Syck by Tatsuhiko Miyagawa, <miyagawa@bulknews.net> Tweaked by Koji Komatsu, <yosty@cpan.org>
<miyagawa@bulknews.net>
<yosty@cpan.org>
Please report any bugs or feature requests to bug-dbix-json at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-JSON. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-dbix-json at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc DBIx::JSON
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/DBIx-JSON
CPAN Ratings
http://cpanratings.perl.org/d/DBIx-JSON
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-JSON
Search CPAN
http://search.cpan.org/dist/DBIx-JSON
Copyright 2006 Koji Komatsu, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install DBIx::JSON, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::JSON
CPAN shell
perl -MCPAN -e shell install DBIx::JSON
For more information on module installation, please visit the detailed CPAN module installation guide.