Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the "dump" command #3

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Groonga/Commands/CreateTable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ sub _parse_arguments {

_is_valid_arguments($args);

if (exists($args->{'table'})) {
$parsed_arguments{'table'} = $args->{'table'};
if (exists($args->{'name'})) {
$parsed_arguments{'name'} = $args->{'name'};
} else {
croak 'Missing a require argument "table"';
}
Expand Down
115 changes: 115 additions & 0 deletions lib/Groonga/Commands/Dump.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright (C) 2022 Horimoto Yasuhiro <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

package Groonga::Commands::Delete;

use Carp 'croak';

use strict;
use warnings;
use Data::Dumper;
use JSON;
my $groonga_http_client = undef;
my $command_args = "";
my @arguments = (
'tables',
'dump_plugins',
'dump_schema',
'dump_records',
'dump_indexes',
'dump_configs',
'sort_hash_table'
);

sub new {
my ($class, %args) = @_;
my $self = {%args};

$groonga_http_client = $self->{client};
$command_args = _parse_arguments($self);

return bless $self, $class;
}

sub _is_valid_arguments {
my $args = shift;

while (my ($key, $value) = each %{$args}) {
if ($key eq 'client') {
next;
}
if (!(grep {$_ eq $key} @arguments)) {
croak "Invalid arguments: ${key}";
}
}

return 1; #true
}

sub _parse_arguments {
my $args = shift;

my %parsed_arguments = ();

_is_valid_arguments($args);

if (exists($args->{'tables'})) {
$parsed_arguments{'tables'} = $args->{'tables'};
}
if (exists($args->{'dump_plugins'})) {
$parsed_arguments{'dump_plugins'} = $args->{'dump_plugins'};
}
if (exists($args->{'dump_schema'})) {
$parsed_arguments{'dump_schema'} = $args->{'dump_schema'};
}
if (exists($args->{'dump_records'})) {
$parsed_arguments{'dump_records'} = $args->{'dump_records'};
}
if (exists($args->{'dump_indexes'})) {
$parsed_arguments{'dump_indexes'} = $args->{'dump_indexes'};
}
if (exists($args->{'dump_configs'})) {
$parsed_arguments{'dump_configs'} = $args->{'dump_configs'};
}
if (exists($args->{'sort_hash_table'})) {
$parsed_arguments{'sort_hash_table'} = $args->{'sort_hash_table'};
}

return \%parsed_arguments;
}

sub _parse_result {
my $result = shift;
my %result_set = ();

if ($result == JSON::PP::true) {
$result = 1;
} else {
$result = 0;
}
print Dumper $result;
$result_set{'is_success'} = $result;

return \%result_set;
}

sub execute {
if (defined $groonga_http_client) {
return _parse_result($groonga_http_client->send('dump', $command_args));
}
return;
}

1;
11 changes: 11 additions & 0 deletions lib/Groonga/HTTP.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use Groonga::Commands::Load;
use Groonga::Commands::Status;
use Groonga::Commands::Select;
use Groonga::Commands::CreateTable;
use Groonga::Commands::Dump;


use strict;
Expand Down Expand Up @@ -107,6 +108,16 @@ sub create_table {
return $create_table->execute;
}

sub dump {
my ($client, %args) = @_;
my $dump =
Groonga::Commands::Dump->new(
client => $groonga_http_client,
%args
);
return $dump->execute;
}

sub groonga_query_escape {
my ($client, $raw_query) = @_;
return Groonga::Escape->escape($raw_query);
Expand Down
11 changes: 10 additions & 1 deletion xt/table-create.t → xt/create-table.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,22 @@ my $groonga = Groonga::HTTP->new();
# Create Hash Table
{
my $create_table_result = $groonga->create_table(
table => 'Site'
name => 'Site'
);
is(
$create_table_result->{'is_success'},
1,
"successed create table."
);

my $dump_results = $groonga->dump(
tables => 'Site'
);
is(
$dump_results[0],
"table_create Sites TABLE_HASH_KEY",
"dump returns correct results and successed create table."
);
}

done_testing();