Source Code Cross Referenced for DataTableColumnDef.java in  » Database-DBMS » mckoi » com » mckoi » database » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » mckoi » com.mckoi.database 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * com.mckoi.database.DataTableColumnDef  27 Jul 2000
003:         *
004:         * Mckoi SQL Database ( http://www.mckoi.com/database )
005:         * Copyright (C) 2000, 2001, 2002  Diehl and Associates, Inc.
006:         *
007:         * This program is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU General Public License
009:         * Version 2 as published by the Free Software Foundation.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License Version 2 for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License
017:         * Version 2 along with this program; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
019:         *
020:         * Change Log:
021:         * 
022:         * 
023:         */package com.mckoi.database;
024:
025:        import java.io.*;
026:        import com.mckoi.database.global.ColumnDescription;
027:        import com.mckoi.database.global.SQLTypes;
028:
029:        /**
030:         * All the information regarding a column in a table.
031:         *
032:         * @author Tobias Downer
033:         */
034:
035:        public class DataTableColumnDef {
036:
037:            /**
038:             * A string that contains some constraints.  This string contains
039:             * information about whether the column is not null, unique, primary key,
040:             * etc.
041:             */
042:            private byte[] constraints_format = new byte[16];
043:
044:            /**
045:             * The name of the column.
046:             */
047:            private String name;
048:
049:            /**
050:             * The sql column type (as defined in java.sql.Types).
051:             */
052:            private int sql_type;
053:
054:            /**
055:             * The actual column type in the database (as defined in
056:             * com.mckoi.database.global.Types).
057:             */
058:            private int db_type;
059:
060:            /**
061:             * The size of the data.
062:             */
063:            private int size;
064:
065:            /**
066:             * The scale of the data.
067:             */
068:            private int scale;
069:
070:            /**
071:             * The locale string if this column represents a string.  If this is an
072:             * empty string, the column has no locale (the string is collated
073:             * lexicographically).
074:             */
075:            private String locale_str = "";
076:
077:            /**
078:             * The locale Collator strength if this column represents a string.  The
079:             * value here is taken from java.text.Collator.
080:             */
081:            private int str_strength;
082:
083:            /**
084:             * The locale Collator decomposition if this column represents a string.  The
085:             * value here is taken from java.text.Collator.
086:             */
087:            private int str_decomposition;
088:
089:            /**
090:             * The default expression string.
091:             */
092:            private String default_expression_string;
093:
094:            //  /**
095:            //   * The expression that is executed to set the default value.
096:            //   */
097:            //  private Expression default_exp;
098:
099:            /**
100:             * If this is a foreign key, the table.column that this foreign key
101:             * refers to.
102:             * @deprecated
103:             */
104:            private String foreign_key = "";
105:
106:            /**
107:             * The type of index to use on this column.
108:             */
109:            private String index_desc = "";
110:
111:            /**
112:             * If this is a Java Object column, this is a constraint that the object
113:             * must be derived from to be added to this column.  If not specified,
114:             * it defaults to 'java.lang.Object'.
115:             */
116:            private String class_constraint = "";
117:
118:            /**
119:             * The constraining Class object itself.
120:             */
121:            private Class constraining_class;
122:
123:            /**
124:             * The TType object for this column.
125:             */
126:            public TType type;
127:
128:            /**
129:             * Constructs the column definition.
130:             */
131:            public DataTableColumnDef() {
132:            }
133:
134:            /**
135:             * Creates a copy of the given column definition.
136:             */
137:            public DataTableColumnDef(DataTableColumnDef column_def) {
138:                System.arraycopy(column_def.constraints_format, 0,
139:                        constraints_format, 0, constraints_format.length);
140:                name = column_def.name;
141:                sql_type = column_def.sql_type;
142:                db_type = column_def.db_type;
143:                size = column_def.size;
144:                scale = column_def.scale;
145:                locale_str = column_def.locale_str;
146:                str_strength = column_def.str_strength;
147:                str_decomposition = column_def.str_decomposition;
148:                if (column_def.default_expression_string != null) {
149:                    default_expression_string = column_def.default_expression_string;
150:                    //      default_exp = new Expression(column_def.default_exp);
151:                }
152:                foreign_key = column_def.foreign_key;
153:                index_desc = column_def.index_desc;
154:                class_constraint = column_def.class_constraint;
155:                type = column_def.type;
156:            }
157:
158:            // ---------- Set methods ----------
159:
160:            public void setName(String name) {
161:                this .name = name;
162:            }
163:
164:            public void setNotNull(boolean status) {
165:                constraints_format[0] = (byte) (status ? 1 : 0);
166:            }
167:
168:            //  public void setUnique(boolean status) {
169:            //    constraints_format[1] = (byte) (status ? 1 : 0);
170:            //  }
171:            //
172:            //  public void setPrimaryKey(boolean status) {
173:            //    constraints_format[2] = (byte) (status ? 1 : 0);
174:            //  }
175:
176:            public void setSQLType(int sql_type) {
177:                this .sql_type = sql_type;
178:                if (sql_type == SQLTypes.BIT || sql_type == SQLTypes.BOOLEAN) {
179:                    db_type = com.mckoi.database.global.Types.DB_BOOLEAN;
180:                } else if (sql_type == SQLTypes.TINYINT
181:                        || sql_type == SQLTypes.SMALLINT
182:                        || sql_type == SQLTypes.INTEGER
183:                        || sql_type == SQLTypes.BIGINT
184:                        || sql_type == SQLTypes.FLOAT
185:                        || sql_type == SQLTypes.REAL
186:                        || sql_type == SQLTypes.DOUBLE
187:                        || sql_type == SQLTypes.NUMERIC
188:                        || sql_type == SQLTypes.DECIMAL) {
189:                    db_type = com.mckoi.database.global.Types.DB_NUMERIC;
190:                } else if (sql_type == SQLTypes.CHAR
191:                        || sql_type == SQLTypes.VARCHAR
192:                        || sql_type == SQLTypes.LONGVARCHAR) {
193:                    db_type = com.mckoi.database.global.Types.DB_STRING;
194:                } else if (sql_type == SQLTypes.DATE
195:                        || sql_type == SQLTypes.TIME
196:                        || sql_type == SQLTypes.TIMESTAMP) {
197:                    db_type = com.mckoi.database.global.Types.DB_TIME;
198:                } else if (sql_type == SQLTypes.BINARY
199:                        || sql_type == SQLTypes.VARBINARY
200:                        || sql_type == SQLTypes.LONGVARBINARY) {
201:                    db_type = com.mckoi.database.global.Types.DB_BLOB;
202:                } else if (sql_type == SQLTypes.JAVA_OBJECT) {
203:                    db_type = com.mckoi.database.global.Types.DB_OBJECT;
204:                } else {
205:                    db_type = com.mckoi.database.global.Types.DB_UNKNOWN;
206:                }
207:            }
208:
209:            public void setDBType(int db_type) {
210:                this .db_type = db_type;
211:                if (db_type == com.mckoi.database.global.Types.DB_NUMERIC) {
212:                    sql_type = SQLTypes.NUMERIC;
213:                } else if (db_type == com.mckoi.database.global.Types.DB_STRING) {
214:                    sql_type = SQLTypes.LONGVARCHAR;
215:                } else if (db_type == com.mckoi.database.global.Types.DB_BOOLEAN) {
216:                    sql_type = SQLTypes.BIT;
217:                } else if (db_type == com.mckoi.database.global.Types.DB_TIME) {
218:                    sql_type = SQLTypes.TIMESTAMP;
219:                } else if (db_type == com.mckoi.database.global.Types.DB_BLOB) {
220:                    sql_type = SQLTypes.LONGVARBINARY;
221:                } else if (db_type == com.mckoi.database.global.Types.DB_OBJECT) {
222:                    sql_type = SQLTypes.JAVA_OBJECT;
223:                } else {
224:                    throw new Error("Unrecognised internal type.");
225:                }
226:            }
227:
228:            public void setSize(int size) {
229:                this .size = size;
230:            }
231:
232:            public void setScale(int scale) {
233:                this .scale = scale;
234:            }
235:
236:            public void setStringLocale(String locale_str, int strength,
237:                    int decomposition) {
238:                // Sets this column to be of the given locale.  For example, the string
239:                // "frFR" denotes french/france.  See com/mckoi/database/TStringType.java
240:                // for more information.
241:                if (locale_str == null) {
242:                    this .locale_str = "";
243:                } else {
244:                    this .locale_str = locale_str;
245:                    this .str_strength = strength;
246:                    this .str_decomposition = decomposition;
247:                }
248:            }
249:
250:            public void setDefaultExpression(Expression expression) {
251:                this .default_expression_string = new String(expression.text()
252:                        .toString());
253:            }
254:
255:            /**
256:             * @deprecated
257:             */
258:            public void setForeignKey(String foreign_key) {
259:                this .foreign_key = foreign_key;
260:            }
261:
262:            /**
263:             * Sets the indexing scheme for this column.  Either 'InsertSearch' or
264:             * 'BlindSearch'.  If not set, then default to insert search.
265:             */
266:            public void setIndexScheme(String index_scheme) {
267:                index_desc = index_scheme;
268:            }
269:
270:            /**
271:             * If this column represents a Java object, this must be a class the object
272:             * is derived from to be added to this column.
273:             */
274:            public void setClassConstraint(String class_constraint) {
275:                this .class_constraint = class_constraint;
276:                try {
277:                    // Denotes an array
278:                    if (class_constraint.endsWith("[]")) {
279:                        String array_class = class_constraint.substring(0,
280:                                class_constraint.length() - 2);
281:                        Class ac;
282:                        // Arrays of primitive types,
283:                        if (array_class.equals("boolean")) {
284:                            ac = boolean.class;
285:                        } else if (array_class.equals("byte")) {
286:                            ac = byte.class;
287:                        } else if (array_class.equals("char")) {
288:                            ac = char.class;
289:                        } else if (array_class.equals("short")) {
290:                            ac = short.class;
291:                        } else if (array_class.equals("int")) {
292:                            ac = int.class;
293:                        } else if (array_class.equals("long")) {
294:                            ac = long.class;
295:                        } else if (array_class.equals("float")) {
296:                            ac = float.class;
297:                        } else if (array_class.equals("double")) {
298:                            ac = double.class;
299:                        } else {
300:                            // Otherwise a standard array.
301:                            ac = Class.forName(array_class);
302:                        }
303:                        // Make it into an array
304:                        constraining_class = java.lang.reflect.Array
305:                                .newInstance(ac, 0).getClass();
306:                    } else {
307:                        // Not an array
308:                        constraining_class = Class.forName(class_constraint);
309:                    }
310:                } catch (ClassNotFoundException e) {
311:                    throw new Error("Unable to resolve class: "
312:                            + class_constraint);
313:                }
314:            }
315:
316:            /**
317:             * Sets this DataTableColumnDef object up from information in the TType
318:             * object.  This is useful when we need to create a DataTableColumnDef object
319:             * to store information based on nothing more than a TType object.  This
320:             * comes in useful for purely functional tables.
321:             */
322:            public void setFromTType(TType type) {
323:                setSQLType(type.getSQLType());
324:                if (type instanceof  TStringType) {
325:                    TStringType str_type = (TStringType) type;
326:                    setSize(str_type.getMaximumSize());
327:                    setStringLocale(str_type.getLocaleString(), str_type
328:                            .getStrength(), str_type.getDecomposition());
329:                } else if (type instanceof  TNumericType) {
330:                    TNumericType num_type = (TNumericType) type;
331:                    setSize(num_type.getSize());
332:                    setScale(num_type.getScale());
333:                } else if (type instanceof  TBooleanType) {
334:                    // Nothing necessary for booleans
335:                    //      TBooleanType bool_type = (TBooleanType) type;
336:                } else if (type instanceof  TDateType) {
337:                    // Nothing necessary for dates
338:                    //      TDateType date_type = (TDateType) type;
339:                } else if (type instanceof  TNullType) {
340:                    // Nothing necessary for nulls
341:                } else if (type instanceof  TBinaryType) {
342:                    TBinaryType binary_type = (TBinaryType) type;
343:                    setSize(binary_type.getMaximumSize());
344:                } else if (type instanceof  TJavaObjectType) {
345:                    TJavaObjectType java_object_type = (TJavaObjectType) type;
346:                    setClassConstraint(java_object_type
347:                            .getJavaClassTypeString());
348:                } else {
349:                    throw new Error("Don't know how to handle this type: "
350:                            + type.getClass());
351:                }
352:                this .type = type;
353:
354:            }
355:
356:            /**
357:             * Initializes the TType information for a column.  This should be called
358:             * at the last part of a DataTableColumnDef setup.
359:             */
360:            public void initTTypeInfo() {
361:                if (type == null) {
362:                    type = createTTypeFor(getSQLType(), getSize(), getScale(),
363:                            getLocaleString(), getStrength(),
364:                            getDecomposition(), getClassConstraint());
365:                }
366:            }
367:
368:            // ---------- Get methods ----------
369:
370:            public String getName() {
371:                return name;
372:            }
373:
374:            public boolean isNotNull() {
375:                return constraints_format[0] != 0;
376:            }
377:
378:            public int getSQLType() {
379:                return sql_type;
380:            }
381:
382:            /**
383:             * Returns the type as a String.
384:             */
385:            public String getSQLTypeString() {
386:                return sqlTypeToString(getSQLType());
387:            }
388:
389:            /**
390:             * Returns the type as a String.
391:             */
392:            public String getDBTypeString() {
393:                switch (getDBType()) {
394:                case com.mckoi.database.global.Types.DB_NUMERIC:
395:                    return "DB_NUMERIC";
396:                case com.mckoi.database.global.Types.DB_STRING:
397:                    return "DB_STRING";
398:                case com.mckoi.database.global.Types.DB_BOOLEAN:
399:                    return "DB_BOOLEAN";
400:                case com.mckoi.database.global.Types.DB_TIME:
401:                    return "DB_TIME";
402:                case com.mckoi.database.global.Types.DB_BLOB:
403:                    return "DB_BLOB";
404:                case com.mckoi.database.global.Types.DB_OBJECT:
405:                    return "DB_OBJECT";
406:                default:
407:                    return "UNKNOWN(" + getDBType() + ")";
408:                }
409:            }
410:
411:            /**
412:             * Returns the Class of Java object that represents this column.
413:             */
414:            public Class classType() {
415:                return com.mckoi.database.global.TypeUtil.toClass(getDBType());
416:            }
417:
418:            public int getDBType() {
419:                return db_type;
420:            }
421:
422:            public int getSize() {
423:                return size;
424:            }
425:
426:            public int getScale() {
427:                return scale;
428:            }
429:
430:            public String getLocaleString() {
431:                return locale_str;
432:            }
433:
434:            public int getStrength() {
435:                return str_strength;
436:            }
437:
438:            public int getDecomposition() {
439:                return str_decomposition;
440:            }
441:
442:            public Expression getDefaultExpression(TransactionSystem system) {
443:                if (default_expression_string == null) {
444:                    return null;
445:                }
446:                Expression exp = Expression.parse(default_expression_string);
447:                return exp;
448:            }
449:
450:            public String getDefaultExpressionString() {
451:                return default_expression_string;
452:            }
453:
454:            /**
455:             * @deprecated
456:             */
457:            public String getForeignKey() {
458:                return foreign_key;
459:            }
460:
461:            /**
462:             * Returns the name of the scheme we use to index this column.  It will
463:             * be either 'InsertSearch' or 'BlindSearch'.
464:             */
465:            public String getIndexScheme() {
466:                if (index_desc.equals("")) {
467:                    return "InsertSearch";
468:                }
469:                return index_desc;
470:            }
471:
472:            /**
473:             * Returns true if this type of column is able to be indexed.
474:             */
475:            public boolean isIndexableType() {
476:                if (getDBType() == com.mckoi.database.global.Types.DB_BLOB
477:                        || getDBType() == com.mckoi.database.global.Types.DB_OBJECT) {
478:                    return false;
479:                }
480:                return true;
481:            }
482:
483:            /**
484:             * If this column represents a Java Object, this returns the name of the
485:             * class the objects stored in the column must be derived from.
486:             */
487:            public String getClassConstraint() {
488:                return class_constraint;
489:            }
490:
491:            /**
492:             * If this column represents a Java Object, this returns the class object
493:             * that is the constraining class for the column.
494:             */
495:            public Class getClassConstraintAsClass() {
496:                return constraining_class;
497:            }
498:
499:            /**
500:             * Returns the TType for this column.
501:             */
502:            public TType getTType() {
503:                if (type == null) {
504:                    throw new Error("'type' variable was not set.");
505:                }
506:                return type;
507:            }
508:
509:            //  /**
510:            //   * Returns this column as a TableField object.
511:            //   * 
512:            //   * @deprecated TableField shouldn't be used anymore
513:            //   */
514:            //  public TableField tableFieldValue() {
515:            //    TableField field =
516:            //               new TableField(getName(), getDBType(), getSize(), isNotNull());
517:            ////    if (isUnique()) {
518:            ////      field.setUnique();
519:            ////    }
520:            //    field.setScale(getScale());
521:            //    field.setSQLType(getSQLType());
522:            //
523:            //    return field;
524:            //  }
525:
526:            /**
527:             * Returns this column as a ColumnDescription object and gives the column
528:             * description the given name.
529:             */
530:            public ColumnDescription columnDescriptionValue(String column_name) {
531:                ColumnDescription field = new ColumnDescription(column_name,
532:                        getDBType(), getSize(), isNotNull());
533:                field.setScale(getScale());
534:                field.setSQLType(getSQLType());
535:
536:                return field;
537:            }
538:
539:            /**
540:             * Dumps information about this object to the PrintStream.
541:             */
542:            public void dump(PrintStream out) {
543:                out.print(getName());
544:                out.print("(");
545:                out.print(getSQLTypeString());
546:                out.print(")");
547:            }
548:
549:            // ---------- For compatibility with older versions only --------
550:            // These are made available only because we need to convert from the
551:            // pre table constraint versions.
552:
553:            boolean compatIsUnique() {
554:                return constraints_format[1] != 0;
555:            }
556:
557:            boolean compatIsPrimaryKey() {
558:                return constraints_format[2] != 0;
559:            }
560:
561:            // ---------- Convenient static methods ----------
562:
563:            /**
564:             * Returns a string that represents the given SQLType enumeration passed
565:             * to it.  For example, pass SQLTypes.BIT and it returns the string "BIT"
566:             */
567:            public static String sqlTypeToString(int sql_type) {
568:                switch (sql_type) {
569:                case SQLTypes.BIT:
570:                    return "BIT";
571:                case SQLTypes.TINYINT:
572:                    return "TINYINT";
573:                case SQLTypes.SMALLINT:
574:                    return "SMALLINT";
575:                case SQLTypes.INTEGER:
576:                    return "INTEGER";
577:                case SQLTypes.BIGINT:
578:                    return "BIGINT";
579:                case SQLTypes.FLOAT:
580:                    return "FLOAT";
581:                case SQLTypes.REAL:
582:                    return "REAL";
583:                case SQLTypes.DOUBLE:
584:                    return "DOUBLE";
585:                case SQLTypes.NUMERIC:
586:                    return "NUMERIC";
587:                case SQLTypes.DECIMAL:
588:                    return "DECIMAL";
589:                case SQLTypes.CHAR:
590:                    return "CHAR";
591:                case SQLTypes.VARCHAR:
592:                    return "VARCHAR";
593:                case SQLTypes.LONGVARCHAR:
594:                    return "LONGVARCHAR";
595:                case SQLTypes.DATE:
596:                    return "DATE";
597:                case SQLTypes.TIME:
598:                    return "TIME";
599:                case SQLTypes.TIMESTAMP:
600:                    return "TIMESTAMP";
601:                case SQLTypes.BINARY:
602:                    return "BINARY";
603:                case SQLTypes.VARBINARY:
604:                    return "VARBINARY";
605:                case SQLTypes.LONGVARBINARY:
606:                    return "LONGVARBINARY";
607:                case SQLTypes.JAVA_OBJECT:
608:                    return "JAVA_OBJECT";
609:                case SQLTypes.NULL:
610:                    return "NULL";
611:                case SQLTypes.BOOLEAN:
612:                    return "BOOLEAN";
613:                default:
614:                    return "UNKNOWN(" + sql_type + ")";
615:                }
616:            }
617:
618:            /**
619:             * Returns a TType object for a column with the given type information.  The
620:             * type information is the sql_type, the size and the scale of the type.
621:             */
622:            static TType createTTypeFor(int sql_type, int size, int scale,
623:                    String locale, int str_strength, int str_decomposition,
624:                    String java_class) {
625:                switch (sql_type) {
626:                case (SQLTypes.BIT):
627:                case (SQLTypes.BOOLEAN):
628:                    return TType.BOOLEAN_TYPE;
629:
630:                case (SQLTypes.TINYINT):
631:                case (SQLTypes.SMALLINT):
632:                case (SQLTypes.INTEGER):
633:                case (SQLTypes.BIGINT):
634:                case (SQLTypes.FLOAT):
635:                case (SQLTypes.REAL):
636:                case (SQLTypes.DOUBLE):
637:                case (SQLTypes.NUMERIC):
638:                case (SQLTypes.DECIMAL):
639:                    return new TNumericType(sql_type, size, scale);
640:
641:                case (SQLTypes.CHAR):
642:                case (SQLTypes.VARCHAR):
643:                case (SQLTypes.LONGVARCHAR):
644:                case (SQLTypes.CLOB):
645:                    return new TStringType(sql_type, size, locale,
646:                            str_strength, str_decomposition);
647:
648:                case (SQLTypes.DATE):
649:                case (SQLTypes.TIME):
650:                case (SQLTypes.TIMESTAMP):
651:                    return new TDateType(sql_type);
652:
653:                case (SQLTypes.BINARY):
654:                case (SQLTypes.VARBINARY):
655:                case (SQLTypes.LONGVARBINARY):
656:                case (SQLTypes.BLOB):
657:                    return new TBinaryType(sql_type, size);
658:
659:                case (SQLTypes.JAVA_OBJECT):
660:                    return new TJavaObjectType(java_class);
661:
662:                case (SQLTypes.ARRAY):
663:                    return TType.ARRAY_TYPE;
664:
665:                case (SQLTypes.NULL):
666:                    return TType.NULL_TYPE;
667:
668:                default:
669:                    throw new Error("SQL type not recognized.");
670:                }
671:            }
672:
673:            /**
674:             * Convenience helper - creates a DataTableColumnDef that
675:             * holds a numeric value.
676:             */
677:            public static DataTableColumnDef createNumericColumn(String name) {
678:                DataTableColumnDef column = new DataTableColumnDef();
679:                column.setName(name);
680:                column.setSQLType(java.sql.Types.NUMERIC);
681:                column.initTTypeInfo();
682:                return column;
683:            }
684:
685:            /**
686:             * Convenience helper - creates a DataTableColumnDef that
687:             * holds a boolean value.
688:             */
689:            public static DataTableColumnDef createBooleanColumn(String name) {
690:                DataTableColumnDef column = new DataTableColumnDef();
691:                column.setName(name);
692:                column.setSQLType(java.sql.Types.BIT);
693:                column.initTTypeInfo();
694:                return column;
695:            }
696:
697:            /**
698:             * Convenience helper - creates a DataTableColumnDef that
699:             * holds a string value.
700:             */
701:            public static DataTableColumnDef createStringColumn(String name) {
702:                DataTableColumnDef column = new DataTableColumnDef();
703:                column.setName(name);
704:                column.setSQLType(java.sql.Types.VARCHAR);
705:                column.setSize(Integer.MAX_VALUE);
706:                column.initTTypeInfo();
707:                return column;
708:            }
709:
710:            /**
711:             * Convenience helper - creates a DataTableColumnDef that
712:             * holds a binary value.
713:             */
714:            public static DataTableColumnDef createBinaryColumn(String name) {
715:                DataTableColumnDef column = new DataTableColumnDef();
716:                column.setName(name);
717:                column.setSQLType(java.sql.Types.LONGVARBINARY);
718:                column.setSize(Integer.MAX_VALUE);
719:                column.setIndexScheme("BlindSearch");
720:                column.initTTypeInfo();
721:                return column;
722:            }
723:
724:            // ---------- IO Methods ----------
725:
726:            /**
727:             * Writes this column information out to a DataOutputStream.
728:             */
729:            void write(DataOutput out) throws IOException {
730:
731:                out.writeInt(2); // The version
732:
733:                out.writeUTF(name);
734:                out.writeInt(constraints_format.length);
735:                out.write(constraints_format);
736:                out.writeInt(sql_type);
737:                out.writeInt(db_type);
738:                out.writeInt(size);
739:                out.writeInt(scale);
740:
741:                if (default_expression_string != null) {
742:                    out.writeBoolean(true);
743:                    out.writeUTF(default_expression_string);
744:                    //new String(default_exp.text().toString()));
745:                } else {
746:                    out.writeBoolean(false);
747:                }
748:
749:                out.writeUTF(foreign_key);
750:                out.writeUTF(index_desc);
751:                out.writeUTF(class_constraint); // Introduced in version 2.
752:
753:                // Format the 'other' string
754:                StringBuffer other = new StringBuffer();
755:                other.append("|");
756:                other.append(locale_str);
757:                other.append("|");
758:                other.append(str_strength);
759:                other.append("|");
760:                other.append(str_decomposition);
761:                other.append("|");
762:                // And write it
763:                out.writeUTF(new String(other));
764:            }
765:
766:            /**
767:             * Reads this column from a DataInputStream.
768:             */
769:            static DataTableColumnDef read(DataInput in) throws IOException {
770:
771:                int ver = in.readInt();
772:
773:                DataTableColumnDef cd = new DataTableColumnDef();
774:                cd.name = in.readUTF();
775:                int len = in.readInt();
776:                in.readFully(cd.constraints_format, 0, len);
777:                cd.sql_type = in.readInt();
778:                cd.db_type = in.readInt();
779:                cd.size = in.readInt();
780:                cd.scale = in.readInt();
781:
782:                boolean b = in.readBoolean();
783:                if (b) {
784:                    cd.default_expression_string = in.readUTF();
785:                    //      cd.default_exp = Expression.parse(in.readUTF());
786:                }
787:                cd.foreign_key = in.readUTF();
788:                cd.index_desc = in.readUTF();
789:                if (ver > 1) {
790:                    String cc = in.readUTF();
791:                    if (!cc.equals("")) {
792:                        cd.setClassConstraint(cc);
793:                    }
794:                } else {
795:                    cd.class_constraint = "";
796:                }
797:
798:                // Parse the 'other' string
799:                String other = in.readUTF();
800:                if (other.length() > 0) {
801:                    if (other.startsWith("|")) {
802:                        // Read the string locale, collation strength and disposition
803:                        int cur_i = 1;
804:                        int next_break = other.indexOf("|", cur_i);
805:                        cd.locale_str = other.substring(cur_i, next_break);
806:
807:                        cur_i = next_break + 1;
808:                        next_break = other.indexOf("|", cur_i);
809:                        cd.str_strength = Integer.parseInt(other.substring(
810:                                cur_i, next_break));
811:
812:                        cur_i = next_break + 1;
813:                        next_break = other.indexOf("|", cur_i);
814:                        cd.str_decomposition = Integer.parseInt(other
815:                                .substring(cur_i, next_break));
816:
817:                    } else {
818:                        throw new Error(
819:                                "Incorrectly formatted DataTableColumnDef data.");
820:                    }
821:                }
822:
823:                cd.initTTypeInfo();
824:
825:                return cd;
826:            }
827:
828:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.