Quantum version 2.4.1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / adapters / PostgresAdapter.java
1 package com.quantum.adapters;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import com.quantum.util.QuantumUtil;
7 import com.quantum.util.sql.TypesHelper;
8
9
10 public class PostgresAdapter extends DatabaseAdapter {
11         protected PostgresAdapter() {
12                 super(AdapterFactory.POSTGRES);
13         }
14         public String getShowTableQuery(String qualifier) {
15         return "SELECT SCHEMANAME, TABLENAME FROM PG_TABLES WHERE SCHEMANAME = '" 
16             + qualifier + "'";
17     }
18     public String getShowViewQuery(String qualifier) {
19         return "SELECT SCHEMANAME, VIEWNAME FROM PG_VIEWS WHERE SCHEMANAME = '" 
20             + qualifier + "'";
21     }
22     public String getShowSequenceQuery(String qualifier) {
23         return "select pg_namespace.nspname, relname " +
24                         "from pg_class, pg_namespace where relkind = 'S' " +
25                         "and relnamespace = pg_namespace.oid " +
26                         "and pg_namespace.nspname = '" + qualifier + "'";
27     }
28         public String getNextValue(String sequence, String owner) {
29                 return "SELECT NEXTVAL('" + getQualifiedName(owner, sequence) + "')";
30         }
31         public String getPrevValue(String sequence, String owner) {
32                 return "SELECT * FROM " + getQualifiedName(owner, sequence);
33         }
34
35         /**
36          * Quotes a string according to the type of the column 
37          * @param string to be quoted
38          * @param type according to java.sql.Types
39          * @return
40          */
41         public String quote(String string, int type, String typeString) {
42                 // Booleans in PostgreSQL are queried "t" or "f", but require "true" or "false" when inputed.
43                 if (type == TypesHelper.BIT || type == TypesHelper.BOOLEAN ) // Postgresql seems to identify booleans as BITs
44                 {
45                         if (string.indexOf('t') >= 0 || string.indexOf('T') >= 0 )
46                                 return "true";
47                         else if (string.indexOf('f') >= 0 || string.indexOf('F') >= 0 )
48                                 return "false";
49                         else 
50                                 return string;
51                 }
52                 // use the default (upper type)
53                 return super.quote(string, type, typeString);
54         }
55
56         public String getTableQuery(String table) {
57                 return "SELECT OID, * FROM " + filterTableName(table); //$NON-NLS-1$
58         }
59     /**
60      * @see com.quantum.adapters.DatabaseAdapter#filterTableName(java.lang.String)
61      */
62     public String filterTableName(String tableName) {
63         // If there is no mixed case, better not quote, it's prettier on display
64         if (tableName.equals(tableName.toUpperCase())) return tableName;
65         // We quote the table name (and only the table name) because it has mixed case
66         if (QuantumUtil.getSchemaName(tableName).equals("")) {
67             return "\"" + tableName +"\""; //$NON-NLS-1$
68         } else {
69             return QuantumUtil.getSchemaName(tableName) + ".\"" + 
70                     QuantumUtil.getTableName(tableName) + "\"";
71         }
72     }
73     /**
74      * @see com.quantum.adapters.DatabaseAdapter#getDefaultSchema(java.lang.String)
75      */
76     public String getDefaultSchema(String userid) {
77         return "public";
78     }
79
80         public Map getDefaultConnectionParameters() {
81                 Map map = new HashMap();
82                 map.put("port", "5432");
83                 map.put("hostname", "localhost");
84                 return map;
85         }
86 }