[Mapbender-commits] r2637 - branches/spsneo_dev/mapbender/update

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Jul 10 03:37:10 EDT 2008


Author: spsneo
Date: 2008-07-10 03:37:10 -0400 (Thu, 10 Jul 2008)
New Revision: 2637

Added:
   branches/spsneo_dev/mapbender/update/updaters.inc
Log:
Database Update Routine ready

Added: branches/spsneo_dev/mapbender/update/updaters.inc
===================================================================
--- branches/spsneo_dev/mapbender/update/updaters.inc	                        (rev 0)
+++ branches/spsneo_dev/mapbender/update/updaters.inc	2008-07-10 07:37:10 UTC (rev 2637)
@@ -0,0 +1,65 @@
+<?php
+
+#@author: Siddharth Prakash Singh (spsneo)
+#Google SoC 2008 project 
+#This is the core file of Database Updater
+#locates the correct directory containing the sql files to be executed.
+#parse the sql files and execute the queries 
+#
+#SQL files are to be placed in /update/db/SYS_DBTYPE/CHARSET directory. 
+#where SYS_DBTYPE is the constant defining the database type and CHARSET defines the encoding 
+# these constants are defined in /conf/mapbender.conf file
+#SQL files should have .sql extension
+#
+#This function executes the required sql files.
+function do_db_updates() {
+	
+	#Connect the database server
+	$con = db_connect(DBSERVER,OWNER,PW);
+	#select the database
+	db_select_db(DB,$con);
+	#locate the correct directory according to SYS_DBTYPE and CHARSET
+	$db_dir = dir('./db/'.SYS_DBTYPE.'/'.CHARSET);
+	#Get the path of directory containing the sql files for later use in this function
+	$db_dir_path = $db_dir->path;
+	#this loops iterates for every file in the directory located
+	while (false !== ($db_file = $db_dir->read())) {
+		
+		#check for sql files by checking the extension
+		if(preg_match("/\.sql$/", $db_file)){
+			
+			#get the content of the entire sql file in an array 
+			#where every element of the array contains one line 
+			$file_content = file($db_dir_path.'/'.$db_file);
+			
+			#check whether file opened correctly
+			if($file_content === false) {
+				echo "Unable to read SQL files. Please try again..\n";
+			}
+			
+			#Query initiated
+			$query = "";
+			
+			#Parse the SQL file content
+			#this loops for every line in the sql file 
+			foreach($file_content as $sql_line) {
+				if(trim($sql_line) != "" && strpos($sql_line, "--") !== 0) {
+					$query .= $sql_line;
+					#Finding Valid SQL statements.
+					if(preg_match("/(.*);/", $sql_line)) {
+						#Preparing the query
+						$query = substr($query, 0, strlen($query)-1);
+						#Execute the prepared sql query
+						$result = db_query($query);
+						#Reset the query for next one
+						$query = "";
+					} //End of preg_match 2
+				} //End of trim
+			} //End of foreach
+		}//End of preg_match 1
+	}//End of while
+}
+
+?>
+
+



More information about the Mapbender_commits mailing list