[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

extend results from SQLite3

Im trying to write my own function into the resultset of an SQLite3 resultset. I want to create multiple classes that will work off the returned recordset (after query is fired).

My problem is, I dont know how to create a new instance of a resultset, and pass the current resultset into it.

My current idea was to do something like:-
<?php
    class testSQLite3 extends SQLite3 {
       public function query($sqlString) {
           $tmpRS = parent::query($sqlString); 
           $newRS = new testSQLite3Result()
           return $newRS;
       }           
    }

    class testSQLite3Result extends SQLite3Result {
        function num_rows() {
            return 27;
        }          
    }
    
  $sqlDB = new testSQLite3("test.db");
  $rs = $sqlDB->query("Select * from `myTable`;");
  echo $rs->num_rows();
?>

Open in new window


But I dont know how to pass the resulting recordset into the new one.

Can anyone provide any pointers please?

Thank you
0
tonelm54
Asked:
tonelm54
  • 2
1 Solution
 
Ray PaseurCommented:
I looked at this earlier today and couldn't quite figure out what the "use case" would be.  Could you please rephrase the question, maybe in a non-technical terms?  For example, please tell us what the reasons are for extending SQLite3 and SQLite3Result.  Are there methods you need that you do not want to override?
0
 
tonelm54Author Commented:
Good morning,
Currently the only way of counting the number of rows returned in SQLite3 is to rephrase the SQL statment to do something like "select count(" (as far as I am aware), which does work but i find is just a bit of a mess. What Im trying to do is add the function num_rows() to the result set, so in this funtion i can just count the rows and return the number, just like you can in Mysqli result set.

I could create a new class independent to SQLite3 and pass the result set into that, however thought it would be tidier to expand the SQLite3Result class to include this statment, and others going forward.
0
 
Ray PaseurCommented:
That makes sense.  Just a bit of philosophy... All of these data base wrappers (even the most advanced like the Eloquent ORM) suffer from one overriding issue - there are many things that we want to know about the contents of a data base, and so there are many ways of asking questions of a data base.  And this plethora of information makes it difficult to consolidate our thinking into a few neat and tidy methods.  Apparently the authors of SQLite were confused by the many options! :-)

Have a look at this solution.  It's clunky, but my instincts tell me that it may be as good as you can get without going "under the covers."
http://php.net/manual/en/class.sqlite3result.php#115891
0
 
Slick812Commented:
greetings tonelm54, In your  "class testSQLite3"  code you have this -

class testSQLite3 extends SQLite3 {
       public function query($sqlString) {
           $tmpRS = parent::query($sqlString);
           $newRS = new testSQLite3Result()
           return $newRS;
       }          
    }

as you can see the SQLite3 query will return a "SQLite3Result class" object, SO if you need to create an extension, and use this -
   $newRS = new testSQLite3Result();
your methods like fetchArray()  will NOT have the database DATA results in it from the SQLite3 query. For you to get your  fetchArray() to return you will need to pass the $tmpRS to your initializer, maybe like -
       $tmpRS = parent::query($sqlString);
       $newRS = new testSQLite3Result($tmpRS);
and have that stored as a reference, and used in EVERY method of yours maybe something like -
      public function fetchArray ($mode = SQLITE3_BOTH ) {
           return $this->Ores->fetchArray($mode);
           }

This is not an easy thing to get correct, and trouble to DeBug, because the extended result object and the SQLite3 are LINKED to each other, although not directly, but through the database engine.

You can easily add properties to to an object so you might consider NOT extending the result, but just add a property.

class testSQLite3 extends SQLite3 {
       public function query($sqlString) {
           $r = parent::query($sqlString); 
           $r->num_rows = 0;
           while ($r->fetchArray(SQLITE3_NUM)) $r->num_rows++;
           $r->reset();
           return $r;
       }           
    }

Open in new window


then you can just get the number o rows like
echo "number of rows i ".$result->num_rows;


Just a NOTE, you seem to be thinking in an OLD database way, as you think you need the number of rows, but In newer database access methods of SQLite3 (from the old SQLite2), I almost never need to get the number of rows until after a call to
    while ($result->fetchArray(SQLITE3_NUM))
where I can count the rows in that loop. If I just need row numbers I can do that in a SQL query.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now