[Fusionforge-commits] r9247 - in trunk: gforge/common/forum gforge/common/include gforge/cronjobs gforge/plugins/cvssyncmail/db gforge/plugins/mantis/lib gforge/plugins/mediawiki/bin gforge/plugins/mediawiki/cronjobs gforge/plugins/projects_hierarchy/db gforge/www/admin plugins/templates/helloworld/db tests/code/deprecations

Olaf Lenz olenz at libremir.placard.fr.eu.org
Fri Mar 26 15:24:50 CET 2010


Author: olenz
Date: 2010-03-26 15:24:50 +0100 (Fri, 26 Mar 2010)
New Revision: 9247

Removed:
   trunk/gforge/plugins/cvssyncmail/db/cvssyncmail-init.sql
   trunk/gforge/plugins/mantis/lib/mantis-init.sql
Modified:
   trunk/gforge/common/forum/Forum.class.php
   trunk/gforge/common/include/Group.class.php
   trunk/gforge/common/include/database-pgsql.php
   trunk/gforge/cronjobs/calculate_user_metric.php
   trunk/gforge/plugins/mediawiki/bin/drop-wiki.php
   trunk/gforge/plugins/mediawiki/cronjobs/create-wikis.php
   trunk/gforge/plugins/projects_hierarchy/db/projects_hierarchy-init.sql
   trunk/gforge/www/admin/pluginman.php
   trunk/plugins/templates/helloworld/db/helloworld-init.sql
   trunk/tests/code/deprecations/DeprecationsTest.php
Log:
DB function cleanup.
* Added function db_query_from_file().
* Deprecated function db_mquery().
* Removed mysql specific queries using db_mquery().
* Removed "INSERT INTO plugins*"-statements from plugins' *-init.sql files.

Modified: trunk/gforge/common/forum/Forum.class.php
===================================================================
--- trunk/gforge/common/forum/Forum.class.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/common/forum/Forum.class.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -276,19 +276,8 @@
 	function getNextThreadID() {
 		global $sys_database_type;
 
-		if ($sys_database_type == "mysql") {
-			$sql="call newval('forum_thread_seq', @res)";
-			$result=db_mquery($sql);
-			if (!$result) {
-				echo db_error();
-				return false;
-			}
-			$sql="select @res";
-			$result = db_query_mysql ($sql);
-		} else {
-			$result = db_query_params ('SELECT nextval($1)',
-						   array ('forum_thread_seq')) ;
-		}
+		$result = db_query_params ('SELECT nextval($1)',
+					   array ('forum_thread_seq')) ;
 		if (!$result || db_numrows($result) < 1) {
 			echo db_error();
 			return false;

Modified: trunk/gforge/common/include/Group.class.php
===================================================================
--- trunk/gforge/common/include/Group.class.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/common/include/Group.class.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -1828,20 +1828,7 @@
 			//	first have to purge any assignments that would cause 
 			//	conflict with existing assignment to 100
 			//
-			if ($sys_database_type == 'mysql') {
-				$res=db_mquery("
-					SELECT pt.project_task_id 
-					FROM project_task pt, project_group_list pgl, project_assigned_to pat 
-					WHERE pt.group_project_id = pgl.group_project_id 
-					AND pat.project_task_id=pt.project_task_id
-					AND pt.status_id='1' AND pgl.group_id='".$this->getID()."'
-					AND pat.assigned_to_id='$user_id' INTO @task_list;
-					DELETE FROM project_assigned_to WHERE project_task_id IN ( @task_list ) AND assigned_to_id='100'");
-				if ($res) {
-					$res = db_next_result();
-				}
-			} else {
-				$res = db_query_params ('DELETE FROM project_assigned_to
+			$res = db_query_params ('DELETE FROM project_assigned_to
 					WHERE project_task_id IN (SELECT pt.project_task_id 
 					FROM project_task pt, project_group_list pgl, project_assigned_to pat 
 					WHERE pt.group_project_id = pgl.group_project_id 
@@ -1849,9 +1836,8 @@
 					AND pt.status_id=1 AND pgl.group_id=$1
 					AND pat.assigned_to_id=$2)
 					AND assigned_to_id=100',
-							array ($this->getID(),
-							       $user_id)) ;
-			}
+						array ($this->getID(),
+						       $user_id)) ;
 			if (!$res) {
 				$this->setError(sprintf(_('ERROR: DB: project_assigned_to %d: %s'),1,db_error()));
 				db_rollback();

Modified: trunk/gforge/common/include/database-pgsql.php
===================================================================
--- trunk/gforge/common/include/database-pgsql.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/common/include/database-pgsql.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -107,6 +107,8 @@
 /**
  *  db_query() - Query the database.
  *
+ *  @deprecated since 4.8. Use db_query_params() instead!
+ *
  *  @param text SQL statement.
  *  @param int How many rows do you want returned.
  *  @param int Of matching rows, return only rows starting here.
@@ -138,6 +140,42 @@
 }
 
 /**
+ *  db_query() - Query the database, from a file.
+ *
+ *  @param string File that contains the SQL statements.
+ *  @param int How many rows do you want returned.
+ *  @param int Of matching rows, return only rows starting here.
+ *  @param int ability to spread load to multiple db servers.
+ *  @return int result set handle.
+ */
+function db_query_from_file($file,$limit='-1',$offset=0,$dbserver=SYS_DB_PRIMARY) {
+	global $QUERY_COUNT;
+	$QUERY_COUNT++;
+
+	$qstring = file_get_contents($file);
+	if (!$qstring) {
+		error_log('db_query_from_file(): Cannot read file $file!');
+		return false;
+	}
+	if (!$limit || !is_numeric($limit) || $limit < 0) {
+		$limit=0;
+	}
+	if ($limit > 0) {
+		if (!$offset || !is_numeric($offset) || $offset < 0) {
+			$offset=0;
+		}
+		$qstring=$qstring." LIMIT $limit OFFSET $offset";
+	}
+	$res = @pg_query($dbserver,$qstring);
+	if (!$res) {
+		error_log('SQL: '. preg_replace('/\n\t+/', ' ',$qstring));
+		error_log('SQL> '.db_error());
+	}
+	//echo "\n<br />|*| [$qstring]: ".db_error();
+	return $res;
+}
+
+/**
  *  db_query_params() - Query the database, with parameters
  *
  *  @param text SQL statement.
@@ -190,6 +228,8 @@
 /**
  *  db_mquery() - Query the database.
  *
+ *  @deprecated since 4.8. Use db_query_params() instead!
+ *
  *  @param text SQL statement.
  *  @param int How many rows do you want returned.
  *  @param int Of matching rows, return only rows starting here.

Modified: trunk/gforge/cronjobs/calculate_user_metric.php
===================================================================
--- trunk/gforge/cronjobs/calculate_user_metric.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/cronjobs/calculate_user_metric.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -95,40 +95,17 @@
 db_query_params ('UPDATE user_metric0 SET ranking=ranking-1',
 			array()) ;
 
-
-if ($sys_database_type == 'mysql') {
-	$sql="
-	SELECT count(*) FROM user_metric0 INTO @total;
-	UPDATE user_metric0 SET
-	metric=(log(times_ranked)*avg_rating),
-	percentile=(100-(100*(ranking-1.0)/@total));";
-
-	db_mquery($sql);
-	$err .= db_error();
-	db_next_result();
-	$err .= db_error();
-} else {
-	$sql=";";
-
-	db_query_params ('UPDATE user_metric0 SET metric=(log(times_ranked::float)*avg_rating::float)::float, percentile=(100-(100*((ranking::float-1)/(select count(*) from user_metric0))))::float',
-			 array());
-	$err .= db_error();
-}
-
-if ($sys_database_type == 'mysql') {
-	$sql="UPDATE user_metric0 SET importance_factor=(1+((percentile/100.0)*.5));";
-	db_query_mysql($sql);
-} else {
-	db_query_params ('UPDATE user_metric0 SET importance_factor=(1+((percentile::float/100)*.5))::float',
-			 array()) ;
-}
-
+db_query_params ('UPDATE user_metric0 SET metric=(log(times_ranked::float)*avg_rating::float)::float, percentile=(100-(100*((ranking::float-1)/(select count(*) from user_metric0))))::float', 
+		 array());
 $err .= db_error();
+db_query_params ('UPDATE user_metric0 SET importance_factor=(1+((percentile::float/100)*.5))::float', 
+		 array()) ;
+$err .= db_error();
 
 db_query_params ('SELECT * INTO TEMPORARY TABLE user_metric_cur FROM user_metric0',
 		 array()) ;
+$err .= db_error();
 
-
 for ($i=1; $i<9; $i++) {
 	$j=($i-1);
 

Deleted: trunk/gforge/plugins/cvssyncmail/db/cvssyncmail-init.sql
===================================================================
--- trunk/gforge/plugins/cvssyncmail/db/cvssyncmail-init.sql	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/plugins/cvssyncmail/db/cvssyncmail-init.sql	2010-03-26 14:24:50 UTC (rev 9247)
@@ -1 +0,0 @@
-INSERT INTO plugins (plugin_name,plugin_desc) VALUES ('cvssyncmail','CVS Syncmail Plugin');

Deleted: trunk/gforge/plugins/mantis/lib/mantis-init.sql
===================================================================
--- trunk/gforge/plugins/mantis/lib/mantis-init.sql	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/plugins/mantis/lib/mantis-init.sql	2010-03-26 14:24:50 UTC (rev 9247)
@@ -1 +0,0 @@
-INSERT INTO plugins (plugin_name,plugin_desc) VALUES ('mantis','Mantis GForge Plugin');
\ No newline at end of file

Modified: trunk/gforge/plugins/mediawiki/bin/drop-wiki.php
===================================================================
--- trunk/gforge/plugins/mediawiki/bin/drop-wiki.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/plugins/mediawiki/bin/drop-wiki.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -49,7 +49,7 @@
   $schema = "plugin_mediawiki_$project";
   strtr($schema, "-", "_");
   echo "  Dropping database schema $schema.\n";
-  $res = db_mquery("DROP SCHEMA $schema CASCADE");
+  $res = db_query_params("DROP SCHEMA $schema CASCADE", array());
   if (!$res) {
     echo db_error();
   }

Modified: trunk/gforge/plugins/mediawiki/cronjobs/create-wikis.php
===================================================================
--- trunk/gforge/plugins/mediawiki/cronjobs/create-wikis.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/plugins/mediawiki/cronjobs/create-wikis.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -109,13 +109,16 @@
 		// Sanitize schema name
 		strtr($schema, "-", "_");
 
+		db_begin();
+
 		cron_debug("  Creating schema $schema.");
-		$res = db_mquery("CREATE SCHEMA $schema");
+		$res = db_query_params("CREATE SCHEMA $schema", array());
 		if (!$res) {
 			$err =  "Error: Schema Creation Failed: " . 
 				db_error();
 			cron_debug($err);
 			cron_entry(23,$err);
+			db_rollback();
 			exit;
 		}
 
@@ -125,23 +128,48 @@
 			$err =  "Error: Couldn't find Mediawiki Database Creation File $mediawiki_creation_file!";
 			cron_debug($err);
 			cron_entry(23,$err);
+			db_rollback();
 			exit;
 		}
 			
+		$res = db_query_params("SET search_path=$schema", array());
+		if (!$res) {
+			$err =  "Error: DB Query Failed: " . 
+				db_error();
+			cron_debug($err);
+			cron_entry(23,$err);
+			db_rollback();
+			exit;
+		}
+
 		$creation_query = file_get_contents($table_file);
-		$res = db_mquery("SET search_path='$schema';"
-				 . $creation_query
-				 . "CREATE TEXT SEARCH CONFIGURATION $schema.default ( COPY = pg_catalog.english );"
-				 . "COMMIT;");
+		$res = db_query_from_file($table_file);
 		if (!$res) {
 			$err =  "Error: Mediawiki Database Creation Failed: " . 
 				db_error();
 			cron_debug($err);
 			cron_entry(23,$err);
+			db_rollback();
 			exit;
 		}
+
+		$res = db_query_params("CREATE TEXT SEARCH CONFIGURATION $schema.default ( COPY = pg_catalog.english )", array());
+		if (!$res) {
+			$err =  "Error: DB Query Failed: " . 
+				db_error();
+			cron_debug($err);
+			cron_entry(23,$err);
+			db_rollback();
+			exit;
+		}
 		
-
+		if (!db_commit()) {
+			$err =  "Error: DB Commit Failed: " . 
+				db_error();
+			cron_debug($err);
+			cron_entry(23,$err);
+			exit;
+		}
 	} else {
 		cron_debug("  Nothing to be done.");
 	}

Modified: trunk/gforge/plugins/projects_hierarchy/db/projects_hierarchy-init.sql
===================================================================
--- trunk/gforge/plugins/projects_hierarchy/db/projects_hierarchy-init.sql	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/plugins/projects_hierarchy/db/projects_hierarchy-init.sql	2010-03-26 14:24:50 UTC (rev 9247)
@@ -5,5 +5,3 @@
 	activated boolean DEFAULT false NOT NULL,
 	com char(255) NOT NULL
 	);
-
-INSERT INTO plugins (plugin_name,plugin_desc) VALUES ('hierarchy','Hierarchy GForge Plugin');
\ No newline at end of file

Modified: trunk/gforge/www/admin/pluginman.php
===================================================================
--- trunk/gforge/www/admin/pluginman.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/gforge/www/admin/pluginman.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -156,9 +156,7 @@
 				}
 					
 				if ($db_init) {
-					$arch = file_get_contents($db_init);
-					$arch = preg_replace('/(INSERT INTO plugins.*$)/','',$arch); // remove the line that inserts into plugins table, we are already doing that (and this would return error otherwise)
-					$res = db_mquery($arch);
+					$res = db_query_from_file($db_init);
 					
 					if ($res) {
 						while ($res) {

Modified: trunk/plugins/templates/helloworld/db/helloworld-init.sql
===================================================================
--- trunk/plugins/templates/helloworld/db/helloworld-init.sql	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/plugins/templates/helloworld/db/helloworld-init.sql	2010-03-26 14:24:50 UTC (rev 9247)
@@ -2,5 +2,3 @@
 	domain text,
 	ip_address text
 ) ;
-
-INSERT INTO plugins (plugin_name,plugin_desc) VALUES ('helloworld','HelloWorld GForge Plugin');
\ No newline at end of file

Modified: trunk/tests/code/deprecations/DeprecationsTest.php
===================================================================
--- trunk/tests/code/deprecations/DeprecationsTest.php	2010-03-26 13:52:22 UTC (rev 9246)
+++ trunk/tests/code/deprecations/DeprecationsTest.php	2010-03-26 14:24:50 UTC (rev 9247)
@@ -13,11 +13,11 @@
 class Deprecations_Tests extends PHPUnit_Framework_TestCase
 {
 	/**
-	 * Check that no code uses db_query()
+	 * Check that no code uses db_query() or db_mquery()
 	 */
 	public function testdb_query()
 	{
-		$output = `cd .. ; find gforge tests -name '*.php' -type f | xargs pcregrep -l '\bdb_query\b' \
+		$output = `cd .. ; find gforge tests -name '*.php' -type f | xargs pcregrep -l '\bdb_m?query\b' \
 					   | grep -v ^tests/code/deprecations/DeprecationsTest.php \
 					   | grep -v ^gforge/db/upgrade-db.php \
 					   | grep -v ^gforge/www/include/database-oci8.php \




More information about the Fusionforge-commits mailing list