I just wanted to check if a table exists, from within a drupal module of course.

I found out, that the difference between the MySQL and the PostgreSQL syntax for this problem is vast.

Maybe someone wants to write two database specific functions, db_table_exists($table) ? I could only do the MySQL part...

Comments

maggam’s picture

Has this been completed? If not, I can work on this.

maggam’s picture

//Input: tablename
//Return: 0, table does NOT exist
//Return: 1, table does exist

function db_table_exists($tablename)
{
$dbconn = pg_connect("host=127.0.01 dbname=test user=drupal password=foo")
or die('Could not connect: ' . pg_last_error());

$query="SELECT exists( select * from pg_tables where tablename='$tablename' and schemaname='public')";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());

$answer=pg_fetch_array($result,0,PGSQL_NUM);
if ($answer[0]=='t'){ return 1; } else {return 0;}
}

Example:
echo db_table_exists(tsearch_index);

Let me know if you need anything else :-)

Cvbge’s picture

Hi,

thanks for your help, but please read some existing drupal core code to see how we do things, and http://drupal.org/node/316 for contrubutor's guide. Also browsing some other issues with patches might be a good idea.

maggam’s picture

I was not too sure what the Parent needed, so I wrote this up for him. He can incorporate this into his code, whichever way he likes, with standards ofcourse.

webchick’s picture

Version: x.y.z » 5.x-dev
Status: Active » Fixed

This was actually fixed in Drupal 5. :)

Anonymous’s picture

Status: Fixed » Closed (fixed)