View Javadoc
1   /*
2    * $Source$
3    * $Revision$
4    *
5    * Copyright (C) 2002 Tim Pizey
6    * 
7    * Part of Melati (http://melati.org), a framework for the rapid
8    * development of clean, maintainable web applications.
9    *
10   * Melati is free software; Permission is granted to copy, distribute
11   * and/or modify this software under the terms either:
12   *
13   * a) the GNU General Public License as published by the Free Software
14   *    Foundation; either version 2 of the License, or (at your option)
15   *    any later version,
16   *
17   *    or
18   *
19   * b) any version of the Melati Software License, as published
20   *    at http://melati.org
21   *
22   * You should have received a copy of the GNU General Public License and
23   * the Melati Software License along with this program;
24   * if not, write to the Free Software Foundation, Inc.,
25   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
26   * GNU General Public License and visit http://melati.org to obtain the
27   * Melati Software License.
28   *
29   * Feel free to contact the Developers of Melati (http://melati.org),
30   * if you would like to work out a different arrangement than the options
31   * outlined here.  It is our intention to allow Melati to be used by as
32   * wide an audience as possible.
33   *
34   * This program is distributed in the hope that it will be useful,
35   * but WITHOUT ANY WARRANTY; without even the implied warranty of
36   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   * GNU General Public License for more details.
38   *
39   * Contact details for copyright holder:
40   *
41   *     Tim Pizey (timp At paneris.org)
42   *
43   */
44  
45  package org.melati.poem.dbms;
46  
47  import java.sql.SQLException;
48  import java.sql.DatabaseMetaData;
49  import java.sql.ResultSet;
50  //import java.sql.ResultSetMetaData;
51  import org.melati.poem.PoemType;
52  import org.melati.poem.SQLPoemType;
53  import org.melati.poem.DoublePoemType;
54  import org.melati.poem.BinaryPoemType;
55  import org.melati.poem.StringPoemType;
56  
57  /**
58   * A Driver for Mckoidb (http://www.mckoi.com/).
59  */
60  public class Mckoi extends AnsiStandard {
61  
62    /** Size of text fields. */
63    public static final int mckoiTextHack = 2147483647;
64    
65    /** Size of binary fields. */
66    public static final int mckoiBinaryHack = 2147483647;
67    /**
68     * Constructor.
69     */
70    public Mckoi() {
71      setDriverClassName("com.mckoi.JDBCDriver");
72    }
73  
74    /**
75     * {@inheritDoc}
76     * @see org.melati.poem.dbms.AnsiStandard#getBinarySqlDefinition(int)
77     */
78    public String getBinarySqlDefinition(int size) {
79        // BLOBs in Postgres are represented as OIDs pointing to the data
80      return "LONGVARBINARY";
81    }
82  
83    /**
84     * {@inheritDoc}
85     * @see org.melati.poem.dbms.AnsiStandard#getStringSqlDefinition(int)
86     */
87    public String getStringSqlDefinition(int size) throws SQLException {
88      if (size < 0) { 
89        return "TEXT";
90      }
91      return super.getStringSqlDefinition(size);
92    }
93  
94    /**
95     * {@inheritDoc}
96     * @see org.melati.poem.dbms.AnsiStandard#getQuotedName(java.lang.String)
97     */
98    public String getQuotedName (String name) {
99      //McKoi doesn't quote names
100     if (name.equals("unique")) return super.getQuotedName(name);
101     if (name.equals("from")) return super.getQuotedName(name);
102     return name;
103   }
104 
105  /**
106    * TODO Check against modern McKoi
107    */
108   public String getSqlDefinition(String sqlTypeName) {
109     if (sqlTypeName.equals("INT")) {
110       return ("INTEGER");
111     }
112     /*
113     if (sqlTypeName.equals("DOUBLE PRECISION")) {
114       return ("DOUBLE");
115     }
116     */
117     return super.getSqlDefinition(sqlTypeName);
118   }
119 
120   /**
121    * {@inheritDoc}
122    * @see org.melati.poem.dbms.AnsiStandard#canRepresent
123    */
124   public <S,O>PoemType<O> canRepresent(PoemType<S> storage, PoemType<O> type) {
125     if (storage instanceof StringPoemType &&
126       type instanceof StringPoemType) {
127 
128       if (((StringPoemType)storage).getSize() == mckoiTextHack &&
129           ((StringPoemType)type).getSize() == -1
130           && !(!storage.getNullable() && type.getNullable())  // Nullable may represent not nullable
131       ) {
132            return type;
133       } else {
134         return storage.canRepresent(type);
135       }
136     } else if (storage instanceof BinaryPoemType &&
137                type instanceof BinaryPoemType
138                && !(!storage.getNullable() && type.getNullable())  // Nullable may represent not nullable
139     ) {
140       if (((BinaryPoemType)storage).getSize() == mckoiBinaryHack &&
141           ((BinaryPoemType)type).getSize() == -1) {
142         return type;
143       } else {
144         return storage.canRepresent(type);
145       }
146     } else {
147       return storage.canRepresent(type);
148     }
149   }
150 
151   /**
152    * {@inheritDoc}
153    * @see org.melati.poem.dbms.AnsiStandard#defaultPoemTypeOfColumnMetaData
154    */
155   public SQLPoemType<?> defaultPoemTypeOfColumnMetaData(ResultSet md)
156       throws SQLException {
157 
158     if(md.getString("TYPE_NAME").equals("NUMERIC"))
159       return new DoublePoemType(md.getInt("NULLABLE")==
160                                 DatabaseMetaData.columnNullable);
161     else
162       return super.defaultPoemTypeOfColumnMetaData(md);
163   }
164 
165 
166   /**
167    * {@inheritDoc}
168    * @see org.melati.poem.dbms.AnsiStandard#givesCapabilitySQL
169    */
170   public String givesCapabilitySQL(Integer userTroid, String capabilityExpr) {
171     return
172         "SELECT " + getQuotedName("groupmembership") + ".* " + 
173         "FROM " + getQuotedName("groupmembership") + " LEFT JOIN " + 
174         getQuotedName("groupcapability") +
175         " ON " + getQuotedName("groupmembership") + "." + getQuotedName("group") +
176         " =  " + getQuotedName("groupcapability") + "." + getQuotedName("group") + 
177         " WHERE " + getQuotedName("user") + " = " + userTroid + " " +
178         "AND " + getQuotedName("groupcapability") + "." + getQuotedName("group") + 
179         " IS NOT NULL " +
180         "AND " + getQuotedName("capability") + " = " + capabilityExpr;
181   }
182   
183 }
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195