Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 15. EXTENDING SQLITE > Utility Methods

Utility Methods

By overriding all the query methods of the SQLiteDatabase class we ensure that any failed query throws an exception. This done, we needn't worry about error trapping whenever we perform a query. The remaining methods of our derived class are utility methods aimed at helping verify data posted from a form. These methods give us an opportunity to explore some of the ways to retrieve metadata from an SQLite database. Find those methods in Listing 15-8.

Listing 15-8. Metadata methods

/**
 Get all table names in database
*/
    public function getTableNames(){
        if (!isset($this->tablenames)){
            $this->setTablenames();
        }
        return $this->tablenames;
    }
/////////////////////////////////////////////////////////////
/**
 Retrieve field names/types for specified table
*/
    public function getFields($tablename){
        if (!isset($this->tablenames)){
            $this->setTablenames();
        }
        if (!in_array($tablename, $this->tablenames)){
            throw new SQLiteException("Table $tablename not in database.");
        }
        $fieldnames = array();
        $sql = "PRAGMA table_info('$tablename')";
        $result = $this->unbufferedQuery($sql);
        //no error - bad pragma ignored
        //get name and data type as defined upon creation
        foreach ($result as $row){
            $fieldnames[$row['name']] = $row['type'];
        }
        return $fieldnames;
    }
//////////////////////////////////////////////////////////////
//private methods
/**
    private method - initializes table names array
*/
    private function ❶setTableNames(){
        $sql = "SELECT name ".
            "FROM sqlite_master ".
            "WHERE type = 'table' ".
            "OR type = 'view'";
        $result = $this->unbufferedQuery($sql);
        foreach  ($result as $row){
            $this->tablenames[] = $row['name'];
        }
    }


					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint