Is there a Perl module for accessing complex structure data with SQL?

I often save a lot of data in a var hash or get the data according to the conditions. This is not convenient, so I want a module that accesses data with SQL like NoSQL. I found DBD :: RAM , but is there a smaller module?

For example: hash data such as a MySQL table:

{ "table": "company", "rows" : [ { "name": "baidu", "location": "China" }, { "name": "taobao", "location": "China" } ] } 

In general, the insertion of such an entry:

 my %new_row = (name=>xxx, location=>yyy); push (@{$hash->{rows}}, \%new_row); 

If I do this, there will be a lot of hash variable, so I want to do it more like this:

 $handle->insert('insert into company values("xxx", "yyy")'); my ($name, $location) = $handle->select_unqiue_record(<<"_EOC_"; select name, location from company where name="baidu" _EOC_); 
+4
source share
2 answers

I recommend https://metacpan.org/module/DBIx::DataModel .

Once you set up the schema that describes the target table, and you can do it automatically by reverse engineering, you can insert your hash directly as follows:

 my $table = $schema->table($table_name); my $id = $table->insert($hash_ref); 

In fact, you can pass a DBIx :: DataModel array of hash_refs (according to your question), and it will insert each of them for you. See the documentation at: https://metacpan.org/module/DBIx::DataModel#Insert

+2
source

If I understand you correctly, you store data in complex structures that are difficult for you to manipulate. The reason you are asking for types like NoSQL is because you need an easy way to store and process your data.

Then it's time to roll up your sleeves and find out Object Oriented Perl .

Creating Perl objects is a good way to handle complex data structures, and it's really not that hard to learn. I usually write classes on the fly and simply declare them at the end of my program.

This is the data structure that you had in your original post as a company class:

 package Company; sub new { my $class = shift; my $name = shift; my $location = shift; my $self = {}; bless $self, $class; $self->Name($name); $self->Location($location); return $self; } sub Name { my $self = shift; my $name = shift; if ( defined $name ) { $self->{NAME} = $name; } return $self->{NAME}; } sub Location { my $self = shift; my $location = shift; if ( defined $location ) { $self->{LOCATION} = $location; } return $self->{$LOCATION}; } 

That's all. Now I can easily create and manipulate my companies, without worrying about how to manipulate hash hashes or try to remember exactly how I structured my data:

 # Read in all of the companies from $company_file into @company_list open my $company_fh, "<", $company_file; my @company_list; while ( my $line = <$company_fh> ) { chomp $line; my ($name, $location) = split /:/, $line; my $company = Company->new( $name, $location ); push @company_list, $company; } close $company_fh; 

Later, I can manipulate my companies as follows:

 #Print out all Chinese Companies for my $company ( @company_list ) { if ( $company->Location eq "China" ) { say $company->Name . " is located in China."; } } 
0
source

Source: https://habr.com/ru/post/1485629/


All Articles