Solved

extend results from SQLite3

Posted on 2015-01-30
4
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

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 110

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 34

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

687 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