preparing new release
[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 * 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         // Postgresql defaults to lower case identifiers
65         if (tableName.equals(tableName.toLowerCase())) return tableName;
66         // We quote the table name because it has mixed case
67         if (QuantumUtil.getSchemaName(tableName).equals("")) {
68             return "\"" + tableName + "\""; //$NON-NLS-1$
69         } else {
70             return "\"" + QuantumUtil.getSchemaName(tableName) + "\".\"" + 
71                     QuantumUtil.getTableName(tableName) + "\"";
72         }
73     }
74     /**
75      * @see com.quantum.adapters.DatabaseAdapter#getDefaultSchema(java.lang.String)
76      */
77     public String getDefaultSchema(String userid) {
78         return "public";
79     }
80
81         public Map getDefaultConnectionParameters() {
82                 Map map = new HashMap();
83                 map.put("port", "5432");
84                 map.put("hostname", "localhost");
85                 return map;
86         }
87 }