| 1 | package HTTP::AppServer::Plugin::JsonCrud; | ||
| 2 | # Plugin for HTTP::AppServer that provides CRUD methods for | ||
| 3 | # storing JSON documents in a DBI (relational) database. | ||
| 4 | # 2010 by Tom Kirchner | ||
| 5 | |||
| 6 | ################################################################################ | ||
| 7 | # | ||
| 8 | # OUT OF DATE | ||
| 9 | # | ||
| 10 | ################################################################################ | ||
| 11 | |||
| 12 | use 5.010000; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use Data::UUID; | ||
| 16 | use DBI; | ||
| 17 | use JSON; | ||
| 18 | use HTTP::AppServer::Plugin; | ||
| 19 | use base qw(HTTP::AppServer::Plugin); | ||
| 20 | |||
| 21 | our $VERSION = '0.01'; | ||
| 22 | |||
| 23 | # database connection settings | ||
| 24 | my $DBEngine = 'mysql'; | ||
| 25 | my $DBName = 'jsondb'; | ||
| 26 | my $DBUsername = 'root'; | ||
| 27 | my $DBPassword = ''; | ||
| 28 | my $DBHost = 'localhost'; | ||
| 29 | |||
| 30 | # database settings | ||
| 31 | my $DBTablePrefix = 'jsondb_'; | ||
| 32 | |||
| 33 | # UUID generator instance | ||
| 34 | my $UUIDGenerator = Data::UUID->new(); | ||
| 35 | |||
| 36 | # called by the server when the plugin is installed | ||
| 37 | # to determine which routes are handled by the plugin | ||
| 38 | sub init | ||
| 39 | { | ||
| 40 | my ($class, $server, %options) = @_; | ||
| 41 | |||
| 42 | # analyse options | ||
| 43 | $DBEngine = $options{'DBEngine'} if exists $options{'DBEngine'}; | ||
| 44 | $DBName = $options{'DBName'} if exists $options{'DBName'}; | ||
| 45 | $DBUsername = $options{'DBUsername'} if exists $options{'DBUsername'}; | ||
| 46 | $DBPassword = $options{'DBPassword'} if exists $options{'DBPassword'}; | ||
| 47 | $DBHost = $options{'DBHost'} if exists $options{'DBHost'}; | ||
| 48 | $DBTablePrefix = $options{'DBTablePrefix'} if exists $options{'DBTablePrefix'}; | ||
| 49 | |||
| 50 | # install some properties in the server | ||
| 51 | $server->set('dbh', undef); | ||
| 52 | $server->set('getdbh', \&_get_dbh); | ||
| 53 | $server->set('tableprefix', $DBTablePrefix); | ||
| 54 | $server->set('uuidgen', $UUIDGenerator); | ||
| 55 | |||
| 56 | $options{'AllowedActions'} = [qw(create read update delete)] | ||
| 57 | unless exists $options{'AllowedActions'}; | ||
| 58 | |||
| 59 | my %handlers; | ||
| 60 | $handlers{'^\/create.*$'} = \&_handle_create | ||
| 61 | if scalar grep { $_ eq 'create' } @{$options{'AllowedActions'}}; | ||
| 62 | $handlers{'^\/read\/([A-FXa-fx0-9]+).*$'} = \&_handle_read | ||
| 63 | if scalar grep { $_ eq 'read' } @{$options{'AllowedActions'}}; | ||
| 64 | $handlers{'^\/update\/([A-FXa-fx0-9]+).*$'} = \&_handle_update | ||
| 65 | if scalar grep { $_ eq 'update' } @{$options{'AllowedActions'}}; | ||
| 66 | $handlers{'^\/delete\/([A-FXa-fx0-9]+).*$'} = \&_handle_delete | ||
| 67 | if scalar grep { $_ eq 'delete' } @{$options{'AllowedActions'}}; | ||
| 68 | |||
| 69 | return %handlers; | ||
| 70 | |||
| 71 | return ( | ||
| 72 | # creation of a JSON document | ||
| 73 | '^\/create.*$' => \&_handle_create, | ||
| 74 | # reading a JSON document | ||
| 75 | '^\/read\/([A-FXa-fx0-9]+).*$' => \&_handle_read, | ||
| 76 | # updating an existing JSON document | ||
| 77 | '^\/update\/([A-FXa-fx0-9]+).*$' => \&_handle_update, | ||
| 78 | # deleting a JSON document | ||
| 79 | '^\/delete\/([A-FXa-fx0-9]+).*$' => \&_handle_delete, | ||
| 80 | ); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _get_dbh | ||
| 84 | { | ||
| 85 | my ($server) = @_; | ||
| 86 | $server->dbh(_connect_db()); | ||
| 87 | if !defined $server->dbh() || !$server->dbh()->ping(); | ||
| 88 | return $server->dbh(); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _connect_db | ||
| 92 | { | ||
| 93 | my $dbh = | ||
| 94 | DBI->connect("DBI:".$DBEngine.":".$DBName.":".$DBHost, $DBUsername, $DBPassword) | ||
| 95 | or die("Could not connect to database: $! $@\n"); | ||
| 96 | |||
| 97 | # create db tables if nessessary | ||
| 98 | my $sth = $dbh->table_info("", $DBName, $DBTablePrefix.'data', "TABLE"); | ||
| 99 | unless ($sth->fetch()) { | ||
| 100 | my $sql = 'create table `'.$DBTablePrefix.'data` (`uuid` varchar(34), `content` text)'; | ||
| 101 | my $sth = $dbh->prepare($sql); | ||
| 102 | $sth->execute() or die "Failed to create tables: $! $@\n"; | ||
| 103 | } | ||
| 104 | |||
| 105 | die "could not connect to database: $! $@\n" | ||
| 106 | unless defined $dbh; | ||
| 107 | |||
| 108 | return $dbh; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _handle_create | ||
| 112 | { | ||
| 113 | my ($server, $cgi, @parts) = @_; | ||
| 114 | |||
| 115 | my $json = $cgi->param('data'); | ||
| 116 | my $uuid = $server->uuidgen()->create_hex(); | ||
| 117 | my $sql = 'insert into `'.$server->tableprefix().'data` (`uuid`, `content`) values ('.$server->dbh()->quote($uuid).', '.$server->dbh()->quote($json).')'; | ||
| 118 | my $sth = $server->getdbh($server)->prepare($sql); | ||
| 119 | $sth->execute(); | ||
| 120 | |||
| 121 | print $cgi->header('application/json'); | ||
| 122 | print to_json({'status' => 1, 'info' => 'created object with new uuid', 'uuid' => $uuid}); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _handle_read | ||
| 126 | { | ||
| 127 | my ($server, $cgi, $uuid) = @_; | ||
| 128 | |||
| 129 | my $sql = 'select `content` from `'.$server->tableprefix().'data` where `uuid` = '.$server->dbh()->quote($uuid); | ||
| 130 | my $sth = $server->getdbh($server)->prepare($sql); | ||
| 131 | $sth->execute(); | ||
| 132 | my $result = '{}';to_json({'stmt' => $sql}); | ||
| 133 | if (my $row = $sth->fetchrow_arrayref()) { | ||
| 134 | $result = $row->[0]; | ||
| 135 | } | ||
| 136 | |||
| 137 | print $cgi->header('application/json'); | ||
| 138 | print $result; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _handle_update | ||
| 142 | { | ||
| 143 | my ($server, $cgi, $uuid) = @_; | ||
| 144 | |||
| 145 | my $json = $cgi->param('data'); | ||
| 146 | my $sql = 'update `'.$server->tableprefix().'data` set `content` = '.$server->dbh()->quote($json).' where `uuid` = '.$server->dbh()->quote($uuid); | ||
| 147 | my $sth = $server->getdbh($server)->prepare($sql); | ||
| 148 | $sth->execute(); | ||
| 149 | |||
| 150 | print $cgi->header('application/json'); | ||
| 151 | print to_json({'status' => 1, 'info' => 'updated object', 'uuid' => $uuid}); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _handle_delete | ||
| 155 | { | ||
| 156 | my ($server, $cgi, $uuid) = @_; | ||
| 157 | |||
| 158 | my $sql = 'delete from `'.$server->tableprefix().'data` where `uuid` = '.$server->dbh()->quote($uuid); | ||
| 159 | my $sth = $server->getdbh($server)->prepare($sql); | ||
| 160 | $sth->execute(); | ||
| 161 | |||
| 162 | print $cgi->header('application/json'); | ||
| 163 | print to_json({'status' => 1, 'info' => 'deleted object', 'uuid' => $uuid}); | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; | ||
| 167 | __END__ |