Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:07:31+00:00 2026-06-08T19:07:31+00:00

I’ve been working on my own SQL library/ Query builder for a little while.

  • 0

I’ve been working on my own SQL library/ Query builder for a little while. (https://github.com/aviat4ion/Query) For the most part, I’m pretty happy with how things work.

The one problem is with query joins.

Say something like

$db->join($table, 'table1.field1=table2.field2', 'inner');

I’m rather stumped as to how to parse the second argument, which needs to properly escape table identifiers.

I want to be able to handle functions in the condition as well.

My current implementation is rather naive – spliting the conditional on spaces, so ‘table1.field1=table2.field2’ would fail, and ‘table1.field1 = table2.field2’ would work.

Each database driver has a function to abstract the identifier escaping, which works on table identifiers like database.table.field, so that it is escaped as "database"."table"."field".

So my basic problem is this: how to parse out identifiers to escape them in the join conditional.

Edit:

I need to do this in a way that can be used for MySQL, Postgres, SQLite, and Firebird.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:07:34+00:00Added an answer on June 8, 2026 at 7:07 pm

    If you only want to parse the where expression, a simple operator-precedence parser will do the trick. You have to apply a few checks on the parse tree to ensure the expression is valid, but this is not hard.

    You can download an excellent guide to parsing in general here http://dickgrune.com/Books/PTAPG_1st_Edition/ (“Parsing Techniques – A Practical Guide “). Precedence parsing is covered in 9.2 PRECEDENCE PARSING, page 187.

    The technique assumes you have 2 things:

    1. a tokenizer. this should recognize tokens like: identifiers/keywords, numbers, operators, whitespace/comments etc.
    2. a precedence table.

    You read tokens from the tokenizer, one by one. When you find the token is an operator (you know it is because those are stored in the precedence table) then you determine whether the current token has a higher or lower precedence than the previous operator. If the current operator’s precedence is lower than the previous token’s precedence, then you have to write the previous operator, along with it’s operands, to the parse tree, and look back from there to find what the previous operator of the formerly previous operator was. These operations work best if the tokenizer delivers the tokens as a double-linked list so that you can easily traverse the tokens.

    If this all sounds to hard, then either:

    1. use an existing SQL parser. See for example http://code.google.com/p/php-sql-parser/
    2. reconsider your API.

    Regarding option #2, Instead of allowing people to specify expressions as raw text, you could require them to pass it in as an array, or as a easily parsible format like JSON or even XML.

    for instance, you could have it like this:

    $db->join->inner($table, array(
        '=' => array(
            'left' => array (
                'table' => 'tab1'
            ,   'column' => 'col1' 
            )
        ,   'right' => array (
                'table' => 'tab2'
            ,   'column' => 'col2' 
            )
        )
    ));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.