diff options
author | David M <davidmj@users.sourceforge.net> | 2008-04-02 08:28:21 +0000 |
---|---|---|
committer | David M <davidmj@users.sourceforge.net> | 2008-04-02 08:28:21 +0000 |
commit | 2deee69e2f9ecd239e59bf651e73c6780227c824 (patch) | |
tree | 4ea9c652cfdb24bf862e2ba4db7d6ebd33ab4786 /phpBB/includes | |
parent | 3e42df098cedbd4c70f893ea23de7f06df519fe2 (diff) | |
download | forums-2deee69e2f9ecd239e59bf651e73c6780227c824.tar forums-2deee69e2f9ecd239e59bf651e73c6780227c824.tar.gz forums-2deee69e2f9ecd239e59bf651e73c6780227c824.tar.bz2 forums-2deee69e2f9ecd239e59bf651e73c6780227c824.tar.xz forums-2deee69e2f9ecd239e59bf651e73c6780227c824.zip |
Some neat stuff, the new DBAL function has the potential of removing somewhere in the ballpark of 40% of all the DBAL code. It combines a few very common idioms into one statement, allowing us to implicitly use prepared statements. Short term advantages are the ability to remove the gross code that attempts to work around DB2, Oracle and Firebird. Long term advantages include removing the burden of sanitizing data (at least on input) from our end and placing it on shoulders of the backend PHP driver.
Also included is a new posting API I am working on. It is not a real API in so much as it is a backend that a "nice" API could use. User submitted functions are welcome :) It represents a massive simplification in post and topic accounting and a generalization of concepts like "soft deletion" as it works across both topics and posts (yes, this has preliminary support for soft deletions). The only "interesting" problem left is global topics, this has yet to be solved.
Enough talk, time for sleep.
P.S.
Sorry if I broke stuff, was not done on purpose :)
git-svn-id: file:///svn/phpbb/trunk@8485 89ea8834-ac86-4346-8a33-228a782c2dd0
Diffstat (limited to 'phpBB/includes')
-rw-r--r-- | phpBB/includes/acp/acp_attachments.php | 1 | ||||
-rw-r--r-- | phpBB/includes/db/db2.php | 31 | ||||
-rw-r--r-- | phpBB/includes/db/firebird.php | 31 | ||||
-rw-r--r-- | phpBB/includes/db/mssql.php | 15 | ||||
-rw-r--r-- | phpBB/includes/db/mssql_odbc.php | 31 | ||||
-rw-r--r-- | phpBB/includes/db/mysql.php | 15 | ||||
-rw-r--r-- | phpBB/includes/db/mysqli.php | 35 | ||||
-rw-r--r-- | phpBB/includes/db/oracle.php | 33 | ||||
-rw-r--r-- | phpBB/includes/db/postgres.php | 35 | ||||
-rw-r--r-- | phpBB/includes/db/sqlite.php | 15 |
10 files changed, 242 insertions, 0 deletions
diff --git a/phpBB/includes/acp/acp_attachments.php b/phpBB/includes/acp/acp_attachments.php index d2beba55c5..0d0f058392 100644 --- a/phpBB/includes/acp/acp_attachments.php +++ b/phpBB/includes/acp/acp_attachments.php @@ -535,6 +535,7 @@ class acp_attachments $group_ary['download_mode'] = INLINE_LINK; } + // @TODO: rewrite with the new param db functions $sql = ($action == 'add') ? 'INSERT INTO ' . EXTENSION_GROUPS_TABLE . ' ' : 'UPDATE ' . EXTENSION_GROUPS_TABLE . ' SET '; $sql .= $db->sql_build_array((($action == 'add') ? 'INSERT' : 'UPDATE'), $group_ary); $sql .= ($action == 'edit') ? " WHERE group_id = $group_id" : ''; diff --git a/phpBB/includes/db/db2.php b/phpBB/includes/db/db2.php index 40909050c9..ecabc1c2e1 100644 --- a/phpBB/includes/db/db2.php +++ b/phpBB/includes/db/db2.php @@ -351,6 +351,37 @@ class dbal_db2 extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type == 'INSERT') + { + $stmt = db2_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')'); + } + else + { + $query = "UPDATE $table SET "; + + $set = array(); + foreach (array_keys($data) as $key) + { + $set[] = "$key = ?"; + } + $query .= implode(', ', $set); + + if ($where !== '') + { + $query .= $where; + } + + $stmt = db2_prepare($this->db_connect_id, $query); + } + + // get the stmt onto the top of the function arguments + array_unshift($data, $stmt); + + call_user_func_array('db2_execute', $data); + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index afb649a4f4..48eb22046e 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -352,6 +352,37 @@ class dbal_firebird extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type == 'INSERT') + { + $stmt = ibase_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')'); + } + else + { + $query = "UPDATE $table SET "; + + $set = array(); + foreach (array_keys($data) as $key) + { + $set[] = "$key = ?"; + } + $query .= implode(', ', $set); + + if ($where !== '') + { + $query .= $where; + } + + $stmt = ibase_prepare($this->db_connect_id, $query); + } + + // get the stmt onto the top of the function arguments + array_unshift($data, $stmt); + + call_user_func_array('ibase_execute', $data); + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php index bf6190c79c..a6c8d26a4d 100644 --- a/phpBB/includes/db/mssql.php +++ b/phpBB/includes/db/mssql.php @@ -303,6 +303,21 @@ class dbal_mssql extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type === 'UPDATE') + { + $this->sql_query('INSERT INTO ' . $table . ' ' . + $this->sql_build_array('INSERT', $data)); + } + else + { + $this->sql_query('UPDATE ' . $table . ' + SET ' . $db->sql_build_array('UPDATE', $data) . + $where); + } + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php index de96939035..f79cd55af4 100644 --- a/phpBB/includes/db/mssql_odbc.php +++ b/phpBB/includes/db/mssql_odbc.php @@ -313,6 +313,37 @@ class dbal_mssql_odbc extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type === 'INSERT') + { + $stmt = odbc_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')'); + } + else + { + $query = "UPDATE $table SET "; + + $set = array(); + foreach (array_keys($data) as $key) + { + $set[] = "$key = ?"; + } + $query .= implode(', ', $set); + + if ($where !== '') + { + $query .= $where; + } + + $stmt = odbc_prepare($this->db_connect_id, $query); + } + + // get the stmt onto the top of the function arguments + array_unshift($data, $stmt); + + call_user_func_array('odbc_execute', $data); + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php index 1014fe6780..1e32b8178a 100644 --- a/phpBB/includes/db/mysql.php +++ b/phpBB/includes/db/mysql.php @@ -284,6 +284,21 @@ class dbal_mysql extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type === 'UPDATE') + { + $this->sql_query('INSERT INTO ' . $table . ' ' . + $this->sql_build_array('INSERT', $data)); + } + else + { + $this->sql_query('UPDATE ' . $table . ' + SET ' . $db->sql_build_array('UPDATE', $data) . + $where); + } + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php index ffe39d63bf..48ea8d3c53 100644 --- a/phpBB/includes/db/mysqli.php +++ b/phpBB/includes/db/mysqli.php @@ -269,6 +269,41 @@ class dbal_mysqli extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type === 'INSERT') + { + $stmt = mysqli_prepare($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (" . substr(str_repeat('?, ', sizeof($data)) ,0, -1) . ')'); + } + else + { + $query = "UPDATE $table SET "; + + $set = array(); + foreach (array_keys($data) as $key) + { + $set[] = "$key = ?"; + } + $query .= implode(', ', $set); + + if ($where !== '') + { + $query .= $where; + } + + $stmt = mysqli_prepare($this->db_connect_id, $query); + } + + // get the stmt onto the top of the function arguments + array_unshift($data, $stmt); + + call_user_func_array('mysqli_stmt_bind_param', $data); + mysqli_stmt_execute($stmt); + + mysqli_stmt_close($stmt); + } + + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php index fc7d177377..09648d9841 100644 --- a/phpBB/includes/db/oracle.php +++ b/phpBB/includes/db/oracle.php @@ -514,6 +514,39 @@ class dbal_oracle extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type === 'INSERT') + { + $stmt = oci_parse($this->db_connect_id, "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES (:" . implode(', :', array_keys($data)) . ')'); + } + else + { + $query = "UPDATE $table SET "; + + $set = array(); + foreach (array_keys($data) as $key) + { + $set[] = "$key = :$key"; + } + $query .= implode(', ', $set); + + if ($where !== '') + { + $query .= $where; + } + + $stmt = oci_parse($this->db_connect_id, $query); + } + + foreach ($data as $column => $value) + { + oci_bind_by_name($stmt, ":$column", $data[$column], -1); + } + + oci_execute($stmt); + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index 0cce581558..f4237999ff 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -334,6 +334,41 @@ class dbal_postgres extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + // for now, stmtname is an empty string, it might change to something more unique in the future + if ($type === 'INSERT') + { + $stmt = pg_prepare($this->dbms_type, '', "INSERT INTO $table (". implode(', ', array_keys($data)) . ") VALUES ($" . implode(', $', range(1, sizeof($data))) . ')'); + } + else + { + $query = "UPDATE $table SET "; + + $set = array(); + foreach (array_keys($data) as $key_id => $key) + { + $set[] = $key . ' = $' . $key_id; + } + $query .= implode(', ', $set); + + if ($where !== '') + { + $query .= $where; + } + + $stmt = pg_prepare($this->db_connect_id, '', $query); + } + + // add the stmtname to the top + array_unshift($data, ''); + + // add the connection resource + array_unshift($data, $this->db_connect_id); + + call_user_func_array('pg_execute', $data); + } + /** * Build LIKE expression * @access private diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php index 580a9fb1fb..64621496c4 100644 --- a/phpBB/includes/db/sqlite.php +++ b/phpBB/includes/db/sqlite.php @@ -258,6 +258,21 @@ class dbal_sqlite extends dbal } } + function sql_handle_data($type, $table, $data, $where = '') + { + if ($type === 'UPDATE') + { + $this->sql_query('INSERT INTO ' . $table . ' ' . + $this->sql_build_array('INSERT', $data)); + } + else + { + $this->sql_query('UPDATE ' . $table . ' + SET ' . $db->sql_build_array('UPDATE', $data) . + $where); + } + } + /** * return sql error array * @access private |