initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / adapters / PostgresAdapter.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/adapters/PostgresAdapter.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/adapters/PostgresAdapter.java
new file mode 100644 (file)
index 0000000..8389bf1
--- /dev/null
@@ -0,0 +1,70 @@
+package com.quantum.adapters;
+
+import com.quantum.sql.SQLHelper;
+import com.quantum.util.QuantumUtil;
+import com.quantum.util.sql.TypesHelper;
+
+
+public class PostgresAdapter extends DatabaseAdapter {
+    public String getShowTableQuery(String qualifier, boolean isDefault) {
+        return "SELECT SCHEMANAME, TABLENAME FROM PG_TABLES WHERE SCHEMANAME = '" 
+            + qualifier + "'";
+    }
+    public String getShowViewQuery(String qualifier, boolean isDefault) {
+        return "SELECT SCHEMANAME, VIEWNAME FROM PG_VIEWS WHERE SCHEMANAME = '" 
+            + qualifier + "'";
+    }
+    public String getShowSequenceQuery(String qualifier, boolean isDefault) {
+        return "SELECT SCHEMANAME, relname FROM pg_class WHERE relkind = 'S'" +
+            "AND SCHEMANAME = '" + qualifier + "'";
+    }
+       public String getNextValue(String sequence, String owner) {
+               return "SELECT NEXTVAL('" + SQLHelper.getQualifiedName(owner, sequence) + "')";
+       }
+       public String getPrevValue(String sequence, String owner) {
+               return "SELECT * FROM " + SQLHelper.getQualifiedName(owner, sequence);
+       }
+
+       /**
+        * Quotes a string according to the type of the column 
+        * @param string to be quoted
+        * @param type according to java.sql.Types
+        * @return
+        */
+       public String quote(String string, int type, String typeString) {
+               // Booleans in PostgreSQL are queried "t" or "f", but require "true" or "false" when inputed.
+               if (type == TypesHelper.BIT || type == TypesHelper.BOOLEAN ) // Postgresql seems to identify booleans as BITs
+               {
+                       if (string.indexOf('t') >= 0 || string.indexOf('T') >= 0 )
+                               return "true";
+                       else if (string.indexOf('f') >= 0 || string.indexOf('F') >= 0 )
+                               return "false";
+                       else 
+                               return string;
+               }
+               // use the default (upper type)
+               return super.quote(string, type, typeString);
+       }
+
+    /**
+     * @see com.quantum.adapters.DatabaseAdapter#filterTableName(java.lang.String)
+     */
+    public String filterTableName(String tableName) {
+        // If there is no mixed case, better not quote, it's prettier on display
+        if (tableName.equals(tableName.toUpperCase())) return tableName;
+        // We quote the table name (and only the table name) because it has mixed case
+        if (QuantumUtil.getSchemaName(tableName).equals("")) {
+            return "\"" + tableName +"\""; //$NON-NLS-1$
+        } else {
+            return QuantumUtil.getSchemaName(tableName) + ".\"" + 
+                    QuantumUtil.getTableName(tableName) + "\"";
+        }
+    }
+    /**
+     * @see com.quantum.adapters.DatabaseAdapter#getDefaultSchema(java.lang.String)
+     */
+    public String getDefaultSchema(String userid) {
+        return "public";
+    }
+
+}
\ No newline at end of file