001: package net.sourceforge.jtds.test;
002:
003: import java.sql.*;
004: import java.util.Map;
005: import java.math.BigDecimal;
006:
007: public abstract class DatabaseTestCase extends TestBase {
008: private static Map typemap = null;
009:
010: public DatabaseTestCase(String name) {
011: super (name);
012: }
013:
014: protected void dropTable(String tableName) throws SQLException {
015: String sobName = "sysobjects";
016: String tableLike = tableName;
017:
018: if (tableName.startsWith("#")) {
019: sobName = "tempdb.dbo.sysobjects";
020: tableLike = tableName + "%";
021: }
022:
023: Statement stmt = con.createStatement();
024: stmt.executeUpdate("if exists (select * from " + sobName
025: + " where name like '" + tableLike
026: + "' and type = 'U') " + "drop table " + tableName);
027: stmt.close();
028: }
029:
030: protected void dropProcedure(String procname) throws SQLException {
031: Statement stmt = con.createStatement();
032: dropProcedure(stmt, procname);
033: stmt.close();
034: }
035:
036: protected void dropProcedure(Statement stmt, String procname)
037: throws SQLException {
038: String sobName = "sysobjects";
039: if (procname.startsWith("#")) {
040: sobName = "tempdb.dbo.sysobjects";
041: }
042: stmt.executeUpdate("if exists (select * from " + sobName
043: + " where name like '" + procname
044: + "%' and type = 'P') " + "drop procedure " + procname);
045: }
046:
047: protected void dropFunction(String procname) throws SQLException {
048: String sobName = "sysobjects";
049: Statement stmt = con.createStatement();
050: stmt.executeUpdate("if exists (select * from " + sobName
051: + " where name like '" + procname
052: + "%' and type = 'FN') " + "drop function " + procname);
053: stmt.close();
054: }
055:
056: // return -1 if a1<a2, 0 if a1==a2, 1 if a1>a2
057: static int compareBytes(byte a1[], byte a2[]) {
058: if (a1 == a2) {
059: return 0;
060: }
061:
062: if (a1 == null) {
063: return -1;
064: }
065:
066: if (a2 == null) {
067: return 1;
068: }
069:
070: int length = (a1.length < a2.length ? a1.length : a2.length);
071:
072: for (int i = 0; i < length; i++) {
073: if (a1[i] != a2[i]) {
074: return ((a1[i] & 0xff) > (a2[i] & 0xff) ? 1 : -1);
075: }
076: }
077:
078: if (a1.length == a2.length) {
079: return 0;
080: }
081:
082: if (a1.length < a2.length) {
083: return -1;
084: }
085:
086: return 1;
087: }
088:
089: protected static Map getTypemap() {
090: if (typemap != null) {
091: return typemap;
092: }
093:
094: Map map = new java.util.HashMap(15);
095: map.put(BigDecimal.class, new Integer(java.sql.Types.DECIMAL));
096: map.put(Boolean.class, new Integer(java.sql.Types.BIT));
097: map.put(Byte.class, new Integer(java.sql.Types.TINYINT));
098: map.put(byte[].class, new Integer(java.sql.Types.VARBINARY));
099: map.put(java.sql.Date.class, new Integer(java.sql.Types.DATE));
100: map.put(double.class, new Integer(java.sql.Types.DOUBLE));
101: map.put(Double.class, new Integer(java.sql.Types.DOUBLE));
102: map.put(float.class, new Integer(java.sql.Types.REAL));
103: map.put(Float.class, new Integer(java.sql.Types.REAL));
104: map.put(Integer.class, new Integer(java.sql.Types.INTEGER));
105: map.put(Long.class, new Integer(java.sql.Types.NUMERIC));
106: map.put(Short.class, new Integer(java.sql.Types.SMALLINT));
107: map.put(String.class, new Integer(java.sql.Types.VARCHAR));
108: map.put(java.sql.Timestamp.class, new Integer(
109: java.sql.Types.TIMESTAMP));
110:
111: typemap = map;
112: return typemap;
113: }
114:
115: protected static int getType(Object o) throws SQLException {
116: if (o == null) {
117: throw new SQLException(
118: "You must specify a type for a null parameter");
119: }
120:
121: Map map = getTypemap();
122: Object ot = map.get(o.getClass());
123:
124: if (ot == null) {
125: throw new SQLException(
126: "Support for this type is not implemented");
127: }
128:
129: return ((Integer) ot).intValue();
130: }
131:
132: protected String getLongString(int length) {
133: StringBuffer result = new StringBuffer(length);
134:
135: for (int i = 0; i < length; i++) {
136: result.append('a');
137: }
138:
139: return result.toString();
140: }
141:
142: protected String getLongString(char ch) {
143: StringBuffer str255 = new StringBuffer(255);
144:
145: for (int i = 0; i < 255; i++) {
146: str255.append(ch);
147: }
148:
149: return str255.toString();
150: }
151: }
|