Database : Handling Queries

  • query : The query method directive is the top level sql setter method. It simply sets queries that can be executed later.

    Method 1 (Raw sql queries)
      $db->query('select * from users where id = 1');
        


    Method 2 (Binded Parameters)
      $db->query('select * from users where id = ?', [1]);
        


    Method 3 (Binded Parameters and Storage)
      $db->query('select * from users where id = ?', [1], 'sql_storage_name');
        


    Footnote: Query method supports raw sql queries and binded parameters as seen in method 1 and 2 above. However, the database handler class supports a storage system known as sql state. This means that sql queries can be stored at top level and updated later. To do this, an identifier query name must be supplied. The stored query can then be pulled later. For example:

    Method 4 (Storage: query state)
      $db->query('select * from users where id = ?', [1], 'sql_storage_name');
      $db->stateSet(':sql_storage_name', [2]);
        


    Footnote: In method 4 above, a query was first saved in first line and then imported in second line using the colon followed by the state name. The second argument (optional) is used to update the previously defined binded value of [1] to [2] . This means that sql query will remain the same and only the binded parameters will be changed.

  • queryState : This is used to store sql queries just like query method above

    Method 5 (Query state)
      $db->queryState('select * from users where id = ?', [1])
          ->saveState('state_name');
    
      if( $db->stateSet('state_name') ) {
    
          //run code here...
    
      }
        

  • The stateSet() method is used to check if a state exists in storage. However, it can also be used to select an sql query state at the same time. This is done by applying a colon before the state name. For example:

    Method 6 (queryState colons)
      $db->queryState('select * from users where id = ?', [1])->saveState('state_name');
    
      if( $db->stateSet(':state_name') ) {
    
          //execute sql query (i.e state_name) 
          $db->process();
    
      }
        
    Binded parameters can be updated when using a state set. For example:
    Method 7 (stateSet - update)
      $db->queryState('select * from users where id = ?', [1])->saveState('user');
    
      if( $db->stateSet(':user', [2]) ) {
    
          //execute query (i.e state_name) here
          $db->process();
    
      }