Index: phpgwapi/inc/class.schema_proc_pgsql.inc.php
===================================================================
--- phpgwapi/inc/class.schema_proc_pgsql.inc.php	(révision 18614)
+++ phpgwapi/inc/class.schema_proc_pgsql.inc.php	(copie de travail)
@@ -219,200 +219,127 @@
 			
 		function _GetColumns($oProc, $sTableName, &$sColumns, $sDropColumn = '', $sAlteredColumn = '', $sAlteredColumnType = '')
 		{
-			$sdb = clone($oProc->m_odb);
-			$sdc = clone($oProc->m_odb);
-
 			$sColumns = '';
 			$this->pk = array();
 			$this->fk = array();
 			$this->ix = array();
 			$this->uc = array();
 
-			$query = "SELECT a.attname,a.attnum FROM pg_attribute a,pg_class b WHERE ";
-			$query .= "b.oid=a.attrelid AND a.attnum>0 and b.relname='$sTableName'";
-			if ($sDropColumn != '')
+			//XXX This is the real call but ... use the source luke !
+			//$aColumns = $oProc->m_odb->adodb->MetaColumns($sTableName);
+			$aColumns = $oProc->m_odb->metadata($sTableName);
+			if(!is_array($aColumns))
 			{
-				$query .= " AND a.attname != '$sDropColumn'";
+				//XXX Trigger error
+				return true;
 			}
-			$query .= ' ORDER BY a.attnum';
-
-			$sdb->query($query, __LINE__, __FILE__);
-			while ($oProc->m_odb->next_record())
+			foreach($aColumns as $oCol)
 			{
-				if ($sColumns != '')
+				$acol = array();
+				if(isset($oCol->type))
 				{
-					$sColumns .= ',';
+					$acol[] = $this->rTranslateType($oCol->type, $oCol->max_length);
 				}
-
-				$sFieldName = $oProc->m_odb->f(0);
-				$sColumns .= $sFieldName;
-				if ($sAlteredColumn == $sFieldName && $sAlteredColumnType != '')
+				if(isset($oCol->not_null) && $oCol->not_null)
 				{
-					$sColumns .= '::' . $sAlteredColumnType;
+					$acol[] = "'nullable' => False";
 				}
-			}
-			//$qdefault = "SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c "
-			//	. "WHERE c.relname = $sTableName AND c.oid = d.adrelid AND d.adnum =" . $oProc->m_odb->f(1);
-			$sql_get_fields = "
-				SELECT
-					a.attnum,
-					a.attname AS field,
-					t.typname AS type,
-					a.attlen AS length,
-					a.atttypmod AS lengthvar,
-					a.attnotnull AS notnull
-				FROM
-					pg_class c,
-					pg_attribute a,
-					pg_type t
-				WHERE
-					c.relname = '$sTableName'
-					and a.attnum > 0
-					and a.attrelid = c.oid
-					and a.atttypid = t.oid
-					ORDER BY a.attnum";
-			/* attnum field type length lengthvar notnull(Yes/No) */
-			$sdb->query($sql_get_fields, __LINE__, __FILE__);
-			while ($sdb->next_record())
-			{
-				$colnum  = $sdb->f(0);
-				$colname = $sdb->f(1);
-
-				if ($sdb->f(5) == 'f')
-				{
-					$null = "'nullable' => True";
-				}
 				else
 				{
-					$null = "'nullable' => False";
+					$acol[] = "'nullable' => True";
 				}
-
-				if ($sdb->f(2) == 'numeric')
+				if(isset($oCol->has_default) && $oCol->has_default)
 				{
-					$prec  = $sdb->f(3);
-					$scale = $sdb->f(4);
+					if(ereg('nextval', $oCol->default_value))
+					{
+						$acol = array("'type' => 'auto', 'nullable' => False");
+					}
+					else
+					{
+						$acol[] = "'default' => '".preg_replace("/'/", "", str_replace(array('::bpchar','::character varying','now()',"('now'::text)::timestamp(6) without time zone", '::date'),array('','','current_timestamp','current_timestamp',''),$oCol->default_value))."'";
+					}
 				}
-				elseif ($sdb->f(3) > 0)
+				if(isset($oCol->primary_key) && $oCol->primary_key)
 				{
-					$prec  = $sdb->f(3);
-					$scale = 0;
+					$this->pk[] = $oCol->name;
 				}
-				elseif ($sdb->f(4) > 0)
+				/* XXX This is done in the MetaIndexes ?
+				if(isset($oCol->unique) && $oCol->unique)
 				{
-					$prec = $sdb->f(4) - 4;
-					$scale = 0;
+					$this->uc[] = $oCol->name;
 				}
-				else
-				{
-					$prec = 0;
-					$scale = 0;
-				}
+				*/
+				$this->sCol[] = "\t\t\t\t'{$oCol->name}' => array(" .implode(', ', $acol)."),\n";
+			}
 
-				$type = $this->rTranslateType($sdb->f(2), $prec, $scale);
-
-				$sql_get_default = "
-					SELECT d.adsrc AS rowdefault
-						FROM pg_attrdef d, pg_class c
-						WHERE
-							c.relname = '$sTableName' AND
-							c.oid = d.adrelid AND
-							d.adnum = $colnum
-					";
-				$sdc->query($sql_get_default, __LINE__, __FILE__);
-				$sdc->next_record();
-				if ($sdc->f(0))
+			//Then indexes / unique (it's the same for pgsql):
+			//XXX the adodb->MetaIndexes should be added to the class.db.inc.php
+			$aIndexes = $oProc->m_odb->adodb->MetaIndexes($sTableName);
+			if(is_array($aIndexes))
+			{
+				foreach($aIndexes as $i)
 				{
-					if (ereg('nextval',$sdc->f(0)))
+					if($i['unique'])
 					{
-						$default = '';
-						$nullcomma = '';
-						$type = "'type' => 'auto'";
+						$quote = "";
+						$a = &$this->uc;
 					}
 					else
 					{
-						$default = "'default' => '" . str_replace(array('::bpchar','::character varying','now()',"('now'::text)::timestamp(6) without time zone"),array('','','current_timestamp','current_timestamp'),$sdc->f(0));
-						// For db-functions - add an apos
-						if(substr($default,-1)!= "'")
-						{
-							$default .= "'"; 
-						}
-						$nullcomma = ',';
+						$quote = "'";
+						$a = &$this->ix;
 					}
+					if(count($i['columns']) > 1)
+					{
+						$a[] = $i['columns'];
+					}
+					else
+					{
+						$a[] = "$quote{$i['columns'][0]}$quote";
+					}
 				}
-				else
-				{
-					$default = '';
-					$nullcomma = '';
-				}
-				$default = ereg_replace("''","'",$default);
-
-				$this->sCol[] = "\t\t\t\t'" . $colname . "' => array(" . $type . ',' . $null . $nullcomma . $default . '),' . "\n";
 			}
-			$sql_pri_keys = "
-				SELECT
-					ic.relname AS index_name,
-					bc.relname AS tab_name,
-					ta.attname AS column_name,
-					i.indisunique AS unique_key,
-					i.indisprimary AS primary_key
-				FROM
-					pg_class bc,
-					pg_class ic,
-					pg_index i,
-					pg_attribute ta,
-					pg_attribute ia
-				WHERE
-					bc.oid = i.indrelid
-					AND ic.oid = i.indexrelid
-					AND ia.attrelid = i.indexrelid
-					AND ta.attrelid = bc.oid
-					AND bc.relname = '$sTableName'
-					AND ta.attrelid = i.indrelid
-					AND ta.attnum = i.indkey[ia.attnum-1]
-				ORDER BY
-					index_name, tab_name, column_name";
-			$sdc->query($sql_pri_keys, __LINE__, __FILE__);
-			while ($sdc->next_record())
+			//Foreign key at least
+			$aForeigns = $oProc->m_odb->MetaForeignKeys($sTableName);
+			if(is_array($aForeigns))
 			{
-				//echo '<br> checking: ' . $sdc->f(4);
-				if ($sdc->f(4) == 't')
+				foreach($aForeigns as $t => $af)
 				{
-					$this->pk[] = $sdc->f(2);
+					$astr = array();
+					foreach($af as $f)
+					{
+						$fp = explode('=',$f);
+						$astr[] = "'{$fp[0]}' => '{$fp[1]}'";
+					}
+					$this->fk[] = "$t' => array(".implode(', ',$astr).')';
 				}
-				else if ($sdc->f(3) == 't')
-				{
-					$this->uc[] = $sdc->f(2);
-				}
 			}
-
-			$ForeignKeys = $sdc->MetaForeignKeys($sTableName);
-
-			foreach($ForeignKeys as $table => $keys)
+			/* XXX This need to be implemented
+			$query = "SELECT a.attname,a.attnum FROM pg_attribute a,pg_class b WHERE ";
+			$query .= "b.oid=a.attrelid AND a.attnum>0 and b.relname='$sTableName'";
+			if ($sDropColumn != '')
 			{
-				$keystr = array();
-				foreach ($keys as $keypair)
-				{
-					$keypair = explode('=',$keypair);
-					$keystr[] = "'" . $keypair[0] . "' => '" . $keypair[1] . "'";
-				}
-				$this->fk[] = $table . "' => array(" . implode(', ',$keystr)  . ')';
+				$query .= " AND a.attname != '$sDropColumn'";
 			}
+			$query .= ' ORDER BY a.attnum';
 
-			$MetaIndexes = $sdc->adodb->MetaIndexes($sTableName);
-
-			//FIXME: looks like unique is reported as index
-			foreach($MetaIndexes as $key => $index)
+			$sdb->query($query, __LINE__, __FILE__);
+			//XXX How this could have ever be working ?
+			while ($oProc->m_odb->next_record())
 			{
-				if(count($index['columns']) > 1)
+				if ($sColumns != '')
 				{
-					$this->ix[] = $index['columns'];
+					$sColumns .= ',';
 				}
-				else
+
+				$sFieldName = $oProc->m_odb->f(0);
+				$sColumns .= $sFieldName;
+				if ($sAlteredColumn == $sFieldName && $sAlteredColumnType != '')
 				{
-					$this->ix[] = $index['columns'][0];	
+					$sColumns .= '::' . $sAlteredColumnType;
 				}
 			}
-
+			 */
 			/* ugly as heck, but is here to chop the trailing comma on the last element (for php3) */
 			if($this->sCol)
 			{
