diff options
Diffstat (limited to 'user_guide_src/source/database')
-rw-r--r-- | user_guide_src/source/database/configuration.rst | 28 | ||||
-rw-r--r-- | user_guide_src/source/database/connecting.rst | 8 | ||||
-rw-r--r-- | user_guide_src/source/database/examples.rst | 8 | ||||
-rw-r--r-- | user_guide_src/source/database/helpers.rst | 6 | ||||
-rw-r--r-- | user_guide_src/source/database/index.rst | 4 | ||||
-rw-r--r-- | user_guide_src/source/database/queries.rst | 19 | ||||
-rw-r--r-- | user_guide_src/source/database/query_builder.rst (renamed from user_guide_src/source/database/active_record.rst) | 42 | ||||
-rw-r--r-- | user_guide_src/source/database/results.rst | 29 | ||||
-rw-r--r-- | user_guide_src/source/database/utilities.rst | 8 |
9 files changed, 104 insertions, 48 deletions
diff --git a/user_guide_src/source/database/configuration.rst b/user_guide_src/source/database/configuration.rst index 4f88c25ab..7a19c840f 100644 --- a/user_guide_src/source/database/configuration.rst +++ b/user_guide_src/source/database/configuration.rst @@ -28,6 +28,10 @@ prototype:: $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; +If you use PDO as your dbdriver, you can specify the full DSN string describe a connection to the database like this:: + + $db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name'; + You can also specify failovers for the situation when the main connection cannot connect for some reason. These failovers can be specified by setting the failover for a connection like this:: @@ -102,18 +106,18 @@ Note: The name "test" is arbitrary. It can be anything you want. By default we've used the word "default" for the primary connection, but it too can be renamed to something more relevant to your project. -Active Record +Query Builder ------------- -The :doc:`Active Record Class <active_record>` is globally enabled or -disabled by setting the $active_record variable in the database +The :doc:`Query Builder Class <query_builder>` is globally enabled or +disabled by setting the $query_builder variable in the database configuration file to TRUE/FALSE (boolean). If you are not using the -active record class, setting it to FALSE will utilize fewer resources +query builder class, setting it to FALSE will utilize fewer resources when the database classes are initialized. :: - $active_record = TRUE; + $query_builder = TRUE; .. note:: that some CodeIgniter classes such as Sessions require Active Records be enabled to access certain functionality. @@ -128,9 +132,9 @@ Explanation of Values: **username** The username used to connect to the database. **password** The password used to connect to the database. **database** The name of the database you want to connect to. -**dbdriver** The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case. +**dbdriver** The database type. ie: mysql, postgre, odbc, etc. Must be specified in lower case. **dbprefix** An optional table prefix which will added to the table name when running :doc: - `Active Record <active_record>` queries. This permits multiple CodeIgniter installations + `Query Builder <query_builder>` queries. This permits multiple CodeIgniter installations to share one database. **pconnect** TRUE/FALSE (boolean) - Whether to use a persistent connection. **db_debug** TRUE/FALSE (boolean) - Whether database errors should be displayed. @@ -162,8 +166,8 @@ Explanation of Values: $db['default']['port'] = 5432; ====================== ================================================================================================== -.. note:: Depending on what database platform you are using (MySQL, - Postgres, etc.) not all values will be needed. For example, when using - SQLite you will not need to supply a username or password, and the - database name will be the path to your database file. The information - above assumes you are using MySQL. +.. note:: Depending on what database platform you are using (MySQL, PostgreSQL, + etc.) not all values will be needed. For example, when using SQLite you + will not need to supply a username or password, and the database name + will be the path to your database file. The information above assumes + you are using MySQL. diff --git a/user_guide_src/source/database/connecting.rst b/user_guide_src/source/database/connecting.rst index a834cc0f7..5822ca62c 100644 --- a/user_guide_src/source/database/connecting.rst +++ b/user_guide_src/source/database/connecting.rst @@ -36,7 +36,7 @@ Available Parameters string. #. TRUE/FALSE (boolean). Whether to return the connection ID (see Connecting to Multiple Databases below). -#. TRUE/FALSE (boolean). Whether to enable the Active Record class. Set +#. TRUE/FALSE (boolean). Whether to enable the Query Builder class. Set to TRUE by default. Manually Connecting to a Database @@ -122,6 +122,12 @@ return the database object. | $DB1->result(); | etc... +.. note:: You don't need to create separate database configurations if you + only need to use a different database on the same connection. You + can switch to a different database when you need to, like this: + + | $this->db->db_select($database2_name); + Reconnecting / Keeping the Connection Alive =========================================== diff --git a/user_guide_src/source/database/examples.rst b/user_guide_src/source/database/examples.rst index d1cd48837..8b3cc4701 100644 --- a/user_guide_src/source/database/examples.rst +++ b/user_guide_src/source/database/examples.rst @@ -104,10 +104,10 @@ Standard Insert $this->db->query($sql); echo $this->db->affected_rows(); -Active Record Query +Query Builder Query =================== -The :doc:`Active Record Pattern <active_record>` gives you a simplified +The :doc:`Query Builder Pattern <query_builder>` gives you a simplified means of retrieving data:: $query = $this->db->get('table_name'); @@ -118,10 +118,10 @@ means of retrieving data:: } The above get() function retrieves all the results from the supplied -table. The :doc:`Active Record <active_record>` class contains a full +table. The :doc:`Query Builder <query_builder>` class contains a full compliment of functions for working with data. -Active Record Insert +Query Builder Insert ==================== :: diff --git a/user_guide_src/source/database/helpers.rst b/user_guide_src/source/database/helpers.rst index 7ea19e9f6..e8a5ac801 100644 --- a/user_guide_src/source/database/helpers.rst +++ b/user_guide_src/source/database/helpers.rst @@ -7,9 +7,9 @@ $this->db->insert_id() The insert ID number when performing database inserts. -.. note:: If using the PDO driver with PostgreSQL, this function requires - a $name parameter, which specifies the appropriate sequence to check - for the insert id. +.. note:: If using the PDO driver with PostgreSQL, or using the Interbase + driver, this function requires a $name parameter, which specifies the + appropriate sequence to check for the insert id. $this->db->affected_rows() =========================== diff --git a/user_guide_src/source/database/index.rst b/user_guide_src/source/database/index.rst index ab12b7cb7..7ccb8fb00 100644 --- a/user_guide_src/source/database/index.rst +++ b/user_guide_src/source/database/index.rst @@ -3,7 +3,7 @@ The Database Class ################## CodeIgniter comes with a full-featured and very fast abstracted database -class that supports both traditional structures and Active Record +class that supports both traditional structures and Query Builder patterns. The database functions offer clear, simple syntax. .. toctree:: @@ -15,7 +15,7 @@ patterns. The database functions offer clear, simple syntax. Running Queries <queries> Generating Query Results <results> Query Helper Functions <helpers> - Active Record Class <active_record> + Query Builder Class <query_builder> Transactions <transactions> Table MetaData <table_data> Field MetaData <fields> diff --git a/user_guide_src/source/database/queries.rst b/user_guide_src/source/database/queries.rst index 971d5d61d..d23efecb3 100644 --- a/user_guide_src/source/database/queries.rst +++ b/user_guide_src/source/database/queries.rst @@ -50,7 +50,7 @@ Protecting identifiers ********************** In many databases it is advisable to protect table and field names - for -example with backticks in MySQL. **Active Record queries are +example with backticks in MySQL. **Query Builder queries are automatically protected**, however if you need to manually protect an identifier you can use:: @@ -112,3 +112,20 @@ The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. You don't have to remember to manually escape data; the engine does it automatically for you. + +*************** +Handling Errors +*************** + +$this->db->error(); +=================== + +If you need to get the last error that has occured, the error() method +will return an array containing its code and message. Here's a quick +example:: + + if ( ! $this->db->simple_query('SELECT `example_field` FROM `example_table`')) + { + $error = $this->db->error(); // Has keys 'code' and 'message' + } + diff --git a/user_guide_src/source/database/active_record.rst b/user_guide_src/source/database/query_builder.rst index c04e67d2a..54e8df6b5 100644 --- a/user_guide_src/source/database/active_record.rst +++ b/user_guide_src/source/database/query_builder.rst @@ -1,15 +1,15 @@ ################### -Active Record Class +Query Builder Class ################### -CodeIgniter uses a modified version of the Active Record Database -Pattern. This pattern allows information to be retrieved, inserted, and -updated in your database with minimal scripting. In some cases only one -or two lines of code are necessary to perform a database action. +CodeIgniter gives you access to a Query Builder class. This pattern +allows information to be retrieved, inserted, and updated in your +database with minimal scripting. In some cases only one or two lines +of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface. -Beyond simplicity, a major benefit to using the Active Record features +Beyond simplicity, a major benefit to using the Query Builder features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the @@ -67,7 +67,7 @@ Example:: // Produces string: SELECT * FROM mytable -The second parameter enables you to set whether or not the active record query +The second parameter enables you to set whether or not the query builder query will be reset (by default it will be—just like `$this->db->get()`):: echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE); @@ -512,7 +512,7 @@ $this->db->count_all_results() ============================== Permits you to determine the number of rows in a particular Active -Record query. Queries will accept Active Record restrictors such as +Record query. Queries will accept Query Builder restrictors such as where(), or_where(), like(), or_like(), etc. Example:: echo $this->db->count_all_results('my_table'); // Produces an integer, like 25 @@ -533,7 +533,7 @@ Query grouping ************** Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow -you to create queries with complex WHERE clauses. Nested groups are supported. Example: +you to create queries with complex WHERE clauses. Nested groups are supported. Example:: $this->db->select('*')->from('my_table') ->group_start() @@ -636,7 +636,7 @@ Example:: // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') -The second parameter enables you to set whether or not the active record query +The second parameter enables you to set whether or not the query builder query will be reset (by default it will be--just like `$this->db->insert()`_):: echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); @@ -921,19 +921,19 @@ Method chaining allows you to simplify your syntax by connecting multiple functions. Consider this example:: $query = $this->db->select('title') - ->where('id', $id) - ->limit(10, 20) - ->get('mytable'); + ->where('id', $id) + ->limit(10, 20) + ->get('mytable'); .. _ar-caching: ********************* -Active Record Caching +Query Builder Caching ********************* -While not "true" caching, Active Record enables you to save (or "cache") +While not "true" caching, Query Builder enables you to save (or "cache") certain parts of your queries for reuse at a later point in your -script's execution. Normally, when an Active Record call is completed, +script's execution. Normally, when an Query Builder call is completed, all stored information is reset for the next call. With caching, you can prevent this reset, and reuse information easily. @@ -944,7 +944,7 @@ There are three Caching functions available: $this->db->start_cache() ======================== -This function must be called to begin caching. All Active Record queries +This function must be called to begin caching. All Query Builder queries of the correct type (see below for supported queries) are stored for later use. @@ -956,7 +956,7 @@ This function can be called to stop caching. $this->db->flush_cache() ======================== -This function deletes all items from the Active Record cache. +This function deletes all items from the Query Builder cache. Here's a usage example:: @@ -983,12 +983,12 @@ Here's a usage example:: $this->db->reset_query() ======================== -Resetting Active Record allows you to start fresh with your query without +Resetting Query Builder allows you to start fresh with your query without executing it first using a method like $this->db->get() or $this->db->insert(). Just like the methods that execute a query, this will *not* reset items you've -cached using `Active Record Caching`_. +cached using `Query Builder Caching`_. -This is useful in situations where you are using Active Record to generate SQL +This is useful in situations where you are using Query Builder to generate SQL (ex. ``$this->db->get_compiled_select()``) but then choose to, for instance, run the query:: diff --git a/user_guide_src/source/database/results.rst b/user_guide_src/source/database/results.rst index 4f93c794d..ac4fc3733 100644 --- a/user_guide_src/source/database/results.rst +++ b/user_guide_src/source/database/results.rst @@ -136,6 +136,26 @@ parameter: | **$row = $query->next_row('array')** | **$row = $query->previous_row('array')** +.. note:: all the functions above will load the whole result into memory (prefetching) use unbuffered_row() for processing large result sets. + +unbuffered_row($type) +===== + +This function returns a single result row without prefetching the whole result in memory as row() does. +If your query has more than one row, it returns the current row and moves the internal data pointer ahead. +The result is returned as $type could be 'object' (default) or 'array' that will return an associative array. + + + + $query = $this->db->query("YOUR QUERY"); + + while ($row = $query->unbuffered_row()) + { + echo $row->title; + echo $row->name; + echo $row->body; + } + *********************** Result Helper Functions *********************** @@ -150,6 +170,12 @@ is the variable that the query result object is assigned to:: echo $query->num_rows(); +.. note:: + Not all database drivers have a native way of getting the total + number of rows for a result set. When this is the case, all of + the data is prefetched and count() is manually called on the + resulting array in order to achieve the same functionality. + $query->num_fields() ===================== @@ -182,5 +208,4 @@ Example:: $row = $query2->row(); echo $row->name; - $query2->free_result();// The $query2 result object will no longer be available - + $query2->free_result(); // The $query2 result object will no longer be available diff --git a/user_guide_src/source/database/utilities.rst b/user_guide_src/source/database/utilities.rst index b0920109f..4e83929b2 100644 --- a/user_guide_src/source/database/utilities.rst +++ b/user_guide_src/source/database/utilities.rst @@ -117,7 +117,7 @@ query. Example:: echo $this->dbutil->csv_from_result($query); The second, third, and fourth parameters allow you to set the delimiter -newline, and enclosure characters respectively. By default tabs are +newline, and enclosure characters respectively. By default commas are used as the delimiter, "\n" is used as a new line, and a double-quote is used as the enclosure. Example:: @@ -161,7 +161,11 @@ $this->dbutil->backup() Permits you to backup your full database or individual tables. The backup data can be compressed in either Zip or Gzip format. -.. note:: This features is only available for MySQL databases. +.. note:: This features is only available for MySQL and Interbase/Firebird databases. + +.. note:: For Interbase/Firebird databases, the backup file name is the only parameter. + + Eg. $this->dbutil->backup('db_backup_filename'); .. note:: Due to the limited execution time and memory available to PHP, backing up very large databases may not be possible. If your database is |