Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

search data structures using SQL

by jdporter (Chancellor)
on Nov 16, 2013 at 01:35 UTC ( #1062833=perlquestion: print w/replies, xml ) Need Help??

jdporter has asked for the wisdom of the Perl Monks concerning the following question:

I have several "tables" of related information in memory, in the form of arrays of hashes, where the elements of the hashes represent named columns of the tables.
I would like to be able to do some basic searching of this data using SQL.
Can you show me how I could do this easily, using only modules in the standard library? (Unfortunately I do not have the ability to install modules in this $environment.)

I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

Replies are listed 'Best First'.
Re: search data structures using SQL (upd: FP, LINQ, int vs ext DSL)
by LanX (Sage) on Nov 16, 2013 at 02:51 UTC
    > I would like to be able to do some basic searching of this data using SQL.

    Well how basic is basic ?

    Showing some of the features you expect might help.

    I think it shouldn't be too difficult to use functional programming to define and combine map like functions like

     select {} from {} where {}

    Not exactly SQL, but I'm not sure which complexity and performance you exactly expect without installing complicated CPAN-modules.

    But I would be surprised if nobody did this already before...

    Cheers Rolf

    ( addicted to the Perl Programming Language)


    this older discussion about LINQ may be of interest: LINQ/Ambition for Perl?

    IMHO you should at least clarify how you want the "basic SQL" DSL to be realized: internally or externally?

      Cool. Thanks. By "basic", I mean SELECTs of not more than moderate complexity. No inserts/updates/deletes. What I'm imagining is that what I want can be done by any SQL engine available in Perl.

      The data I have is definitely relationally structured; it's well normalized, and there are relation tables for many-to-many relationships.

      So if I could do relational calculus using something other than SQL, that would be fine; but remember that I'm limited to whatever is available in a standard perl installation. ActiveState, I believe, though I'm not sure which version. Probably 5.12 or so.

        Still don't know if you want an external or internal DSL.

        External means you need an interpreter (parser + compiler) for a string holding SQL.

        Internal means chaining/cascading Perl functions/methods with "SQL-like" names and semantics in combination with Perl builtins like shown before.

        I prefer the latter.

        > I mean SELECTs of not more than moderate complexity.

        Please gimme some explicit examples of those SQLs of "moderate complexity" and I'll be pleased to show an internal DSL approach built on FP.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: search data structures using SQL
by einhverfr (Friar) on Nov 16, 2013 at 05:53 UTC

    First, this is rather vague. I don't know how complex SQL you have to support and you have to figure that as soon as you implement one feature someone else will say they can't live without another feature.

    What you are essentially talking about is implementing an in-memory db in Perl. this is not likely to remain "easy" or "only using modules in the standard library" for long and it is critically important that you inform $management of these problems before you get started. Let them figure out what they *really* want from this. Otherwise you will end up trapped in a hole that will just keep getting deeper and $management will decide that the way out is to tunnel to $antipode.

    Now this being said, the next thing is to say that if you are going to support it, you are only going to support very simple searches in the form of SELECT column_list FROM table_name WHERE condition. From there simple parsers should be possible using regexps. Note that this is a *tiny* subset of SQL and if they want anything else, or to be able to have a column named "from" (not an unreasonable request) you are going to need something like Parse::RecDescent and restart this from scratch. Basically at that point, you get into hierarchy of language problems.

    However, my advice is that before you do this, stop, and have the difficult discussion with your boss. Inform him of what the real problems are, and that this will involve reinventing lots of stuff from CPAN if it goes beyond basic parsing with a total prohibition on use of reserved words as names. $management gets paid to make the decisions. Let them make it. But make sure that they know what they are getting into....

      Thanks. I can give you more details when I get back to the office on Monday. But there's no point in talking about my boss's involvement in any of this, because he has none. This is a tool I'm building only for myself (and my successor, jah forbid).

      I've written a bletcherous framework in Plain Ol' Perl, for threading through table relations and such, and I just feel like it would be cleaner to replace all that with SQL. But I'm willing to concede that I may have asked an XY question. :-)

        If you are going to do that, why not just skip SQL, take what is good from SQL, and apply Perlisms to it?

        I.e. instead of SELECT foo FROM bar WHERE baz is not true; why not come up with an SQL-inspired interface that avoids the parsing issues, something like:

        select( columns => ['foo'], start_table => 'bar', condition => '? or !defined ?', bind => ['baz', 'baz'] );

        This would avoid the parsing problems, allow you to add joins, cte's, inline views, and more if you ever need it. It would avoid the hard problems while giving you something where you and your successor could leverage sql knowledge. That means essentially passing in the parse tree rather than the declarative statement to the function, and as such it also avoids the possibility of sql injection.

Re: search data structures using SQL
by Anonymous Monk on Nov 16, 2013 at 01:54 UTC
Re: search data structures using SQL
by erix (Prior) on Nov 16, 2013 at 17:25 UTC

    I seem to remember that DBD::AnyData has code to read from arrays (instead of a database).

    See probably "Working with in-memory tables" and "Creating an in-memory table from Arrays".

    Perhaps that code can be extracted and used.

    (If all else fails, check if you do not happen to have postgres available: it has the excellent hstore)

      Brilliant. That is exactly what I was looking for. At least, I think it is, based on the doco; I haven't tried it yet. :-)

Re: search data structures using SQL
by afoken (Canon) on Nov 16, 2013 at 09:07 UTC
    I have several "tables" of related information in memory, in the form of arrays of hashes, where the elements of the hashes represent named columns of the tables.

    Why don't you put that stuff into a real table in the database?


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      If he can't install modules from CPAN wouldn't he have to write a pure perl driver from scratch?
Re: search data structures using SQL
by GrandFather (Saint) on Nov 16, 2013 at 02:10 UTC

      I appreciate the smiley. Trust me when I say I cannot install modules in this environment. It's not physically impossible, it is forbidden by $management. I'm not going to risk my job over this. That said, I could probably sneak in a few dozen lines of horked code without too much repercussions.

      That bit about "working with your manager" is particularly funny, because it isn't just a "manager", it's an entire bureaucracy.

        Do you have a personal laptop?

        Install perl. Generate some data that mirrors the nature and size of your work data. Write the program that throws it into an sqllite db and queries every which way using sql.

        Show you boss. Tell him how long it took you. Tell him how long it would take you do it without the modules you used. Ask him to ask one of your colleagues to give a similar estimate.

        I worked one place where I knew up-front such a demonstration would have been taken very badly and I wouldn't have tried it, but most places it stood a chance of making things better.

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: search data structures using SQL
by RichardK (Parson) on Nov 16, 2013 at 16:53 UTC

    Do you really need SQL or would an OO interface similar to File::Find::Rule do what you need ?

    You'd have to write it yourself, but it's not going to be that difficult, although testing it might be fun ;).

    I've written similar code in the past using iterators and closures that allowed me to program arbitrary queries very quickly.

    I think writing a full parser for SQL would be a lot more work, but that's going to depend what problem you're actually trying to solve.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1062833]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2022-01-20 08:09 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (56 votes). Check out past polls.