Solved

extend results from SQLite3

Posted on 2015-01-30
4
156 Views
Last Modified: 2015-02-06
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
Comment
Question by:tonelm54
  • 2
4 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40580919
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
 

Author Comment

by:tonelm54
ID: 40581165
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40581304
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
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40581697
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now