TAILIEUCHUNG - OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P18
OBJECT-ORIENTED PHP Concepts, Techniques, and Code- P18:A number of years ago, before I started using PHP, I created dynamic web pages using C. This really wasn’t too different from some of the other options available at the time, though it seems almost unthinkable now. Creating a dynamic page meant outputting HTML from your script and recompiling that script if any changes needed to be made. | 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. 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 OsetTableNames sql SELECT name . FROM sqlite_master . WHERE type table . OR type view result this- unbufferedQuery sql foreach result as row this- tablenames row name Listing 15-8 Metadata methods Extending SQLite 151 The two methods that make use of metadata are setTableNames and getFieldNames. Let s examine O the method setTableNames in Listing 15-8. This method makes use of the table sqlite_master a table that defines the schema for the database. By querying sqlite_master we can retrieve the names of all the tables and views in the database. The type field defines the kind of resource in our case a table or view. This method retrieves the names of all the tables and views and stores them in an array. Ideally this method would be called once from the constructor but the constructor for an SQLite database is declared final so it may not be .
đang nạp các trang xem trước