1 /*
2 * $Source: /usr/cvsroot/melati/poem/src/main/java/org/melati/poem/dbms/Mckoi.java,v $
3 * $Revision: 1.15 $
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 * {@inheritDoc}
107 * @see org.melati.poem.dbms.AnsiStandard#getSqlDefinition(java.lang.String)
108 * @todo Check against modern McKoi
109 */
110 public String getSqlDefinition(String sqlTypeName) {
111 if (sqlTypeName.equals("INT")) {
112 return ("INTEGER");
113 }
114 /*
115 if (sqlTypeName.equals("DOUBLE PRECISION")) {
116 return ("DOUBLE");
117 }
118 */
119 return super.getSqlDefinition(sqlTypeName);
120 }
121
122 /**
123 * {@inheritDoc}
124 * @see org.melati.poem.dbms.AnsiStandard#canRepresent
125 */
126 public PoemType canRepresent(PoemType storage, PoemType type) {
127 if (storage instanceof StringPoemType &&
128 type instanceof StringPoemType) {
129
130 if (((StringPoemType)storage).getSize() == mckoiTextHack &&
131 ((StringPoemType)type).getSize() == -1
132 && !(!storage.getNullable() && type.getNullable()) // Nullable may represent not nullable
133 ) {
134 return type;
135 } else {
136 return storage.canRepresent(type);
137 }
138 } else if (storage instanceof BinaryPoemType &&
139 type instanceof BinaryPoemType
140 && !(!storage.getNullable() && type.getNullable()) // Nullable may represent not nullable
141 ) {
142 if (((BinaryPoemType)storage).getSize() == mckoiBinaryHack &&
143 ((BinaryPoemType)type).getSize() == -1) {
144 return type;
145 } else {
146 return storage.canRepresent(type);
147 }
148 } else {
149 return storage.canRepresent(type);
150 }
151 }
152
153 /**
154 * {@inheritDoc}
155 * @see org.melati.poem.dbms.AnsiStandard#defaultPoemTypeOfColumnMetaData
156 */
157 public SQLPoemType defaultPoemTypeOfColumnMetaData(ResultSet md)
158 throws SQLException {
159 // ResultSetMetaData rsmd= md.getMetaData();
160
161 if(md.getString("TYPE_NAME").equals("NUMERIC"))
162 return new DoublePoemType(md.getInt("NULLABLE")==
163 DatabaseMetaData.columnNullable);
164 else
165 return super.defaultPoemTypeOfColumnMetaData(md);
166 }
167
168
169 /**
170 * {@inheritDoc}
171 * @see org.melati.poem.dbms.AnsiStandard#givesCapabilitySQL
172 */
173 public String givesCapabilitySQL(Integer userTroid, String capabilityExpr) {
174 return
175 "SELECT " + getQuotedName("groupmembership") + ".* " +
176 "FROM " + getQuotedName("groupmembership") + " LEFT JOIN " +
177 getQuotedName("groupcapability") +
178 " ON " + getQuotedName("groupmembership") + "." + getQuotedName("group") +
179 " = " + getQuotedName("groupcapability") + "." + getQuotedName("group") +
180 " WHERE " + getQuotedName("user") + " = " + userTroid + " " +
181 "AND " + getQuotedName("groupcapability") + "." + getQuotedName("group") +
182 " IS NOT NULL " +
183 "AND " + getQuotedName("capability") + " = " + capabilityExpr;
184 }
185
186 }
187
188
189
190
191
192
193
194
195
196
197
198