Home » Database » DB » Bug #7068
Auto Detect AutoPrepare
Details
| Request #7068 | Auto Detect AutoPrepare |
|---|---|
| Submitted | 2006-03-09 07:58 UTC |
| From | dranger at gmail dot com |
| Status | Wont fix |
| Package | DB |
| PHP Version | Irrelevant |
| Roadmaps | (Not assigned) |
Comments
[2006-03-09 07:58 UTC] dranger at gmail dot com
Description:
------------
AutoPrepare and AutoExecute should have a mode automatically decides if you should INSERT or UPDATE according to the WHERE clause (it sees if any rows match the WHERE clause, then chooses INSERT or UPDATE), similar to the patch below.
Test script:
---------------
--- DB/common.php 2005-04-07 23:27: 35.000000000 +0900
+++ common.php 2006-03-07 14:14:44.000000000 +0900
@@ -789,6 +789,7 @@
* @param array $table_fields the array of field names
* @param int $mode a type of query to make:
* DB_AUTOQUERY_INSERT or
DB_AUTOQUERY_UPDATE
+ * or DB_AUTOQUERY_AUTO
* @param string $where for update queries: the WHERE clause to
* append to the SQL statement. Don't
* include the "WHERE" keyword.
@@ -800,6 +801,14 @@
function autoPrepare($table, $table_fields, $mode =3D
DB_AUTOQUERY_INSERT,
$where =3D false)
{
+ if($mode =3D=3D DB_AUTOQUERY_AUTO) {
+ $querystring =3D "SELECT COUNT(*) FROM $table";
+ $querystring .=3D ($where) ? " WHERE $where" : "";
+ $checkquery =3D $this->query("$querystring");
+ if(DB::isError($checkquery)) {
+ return $checkquery;
+ }
+ $checkquery->fetchInto($countrow, DB_FETCHMODE_ORDERED);
+ $mode =3D ($countrow[0] > 0) ? DB_AUTOQUERY_UPDATE :
DB_AUTOQUERY_INSERT;
+ }
$query =3D $this->buildManipSQL($table, $table_fields, $mode,
$where);
if (DB::isError($query)) {
return $query;
@@ -819,6 +828,7 @@
* field name and $value its value
* @param int $mode a type of query to make:
* DB_AUTOQUERY_INSERT or
DB_AUTOQUERY_UPDATE
+ * or DB_AUTOQUERY_AUTO
* @param string $where for update queries: the WHERE clause to
* append to the SQL statement. Don't
* include the "WHERE" keyword.
--- DB.php 2005-02-16 12:16:00.000000000 +0900
+++ DB.php 2006-03-07 13:58:54.000000000 +0900
@@ -319,6 +319,7 @@
*/
define('DB_AUTOQUERY_INSERT', 1);
define('DB_AUTOQUERY_UPDATE', 2);
+define('DB_AUTOQUERY_AUTO', 3);
/**#@-*/