01: /*
02: * BlobFormatterFactory.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.storage;
13:
14: import workbench.db.DbMetadata;
15: import workbench.db.DbSettings;
16: import workbench.util.StringUtil;
17:
18: /**
19: * @author support@sql-workbench.net
20: */
21: public class BlobFormatterFactory {
22:
23: public static BlobLiteralFormatter createAnsiFormatter() {
24: // SQL Server, MySQL support the ANSI Syntax
25: // using 0xABCDEF...
26: // we use that for all others as well.
27: DefaultBlobFormatter f = new DefaultBlobFormatter();
28: f.setPrefix("0x");
29:
30: return f;
31: }
32:
33: public static BlobLiteralFormatter createInstance(DbMetadata meta) {
34: // Check for a user-defined formatter definition
35: // for the current DBMS
36: DbSettings s = meta.getDbSettings();
37: String prefix = s.getBlobLiteralPrefix();
38: String suffix = s.getBlobLiteralSuffix();
39: if (!StringUtil.isEmptyString(prefix)
40: && !StringUtil.isEmptyString(suffix)) {
41: DefaultBlobFormatter f = new DefaultBlobFormatter();
42: String type = s.getBlobLiteralType();
43:
44: BlobLiteralType literalType = null;
45: try {
46: literalType = BlobLiteralType.valueOf(type);
47: } catch (Throwable e) {
48: literalType = BlobLiteralType.hex;
49: }
50:
51: BlobLiteralType.valueOf(type);
52: f.setUseUpperCase(s.getBlobLiteralUpperCase());
53: f.setLiteralType(literalType);
54: f.setPrefix(prefix);
55: f.setSuffix(suffix);
56: return f;
57: }
58:
59: // No user-defined formatter definition found, use the built-in settings
60: if (meta.isPostgres()) {
61: return new PostgresBlobFormatter();
62: } else if (meta.isOracle()) {
63: // this might only work with Oracle 10g...
64: // and will probably fail on BLOBs > 4KB
65: DefaultBlobFormatter f = new DefaultBlobFormatter();
66: f.setUseUpperCase(true);
67: f.setPrefix("to_blob(utl_raw.cast_to_raw('0x");
68: f.setSuffix("'))");
69: return f;
70: } else if (meta.getDbId().startsWith("db2")
71: || "h2".equals(meta.getDbId())) {
72: // Although the DB2 Manuals says it supports
73: // binary string constants, it is very likely
74: // that this will be rejected by DB2 due to the
75: // max.length of 32K for binary strings.
76: DefaultBlobFormatter f = new DefaultBlobFormatter();
77: f.setUseUpperCase(true);
78: f.setPrefix("X'");
79: f.setSuffix("'");
80: return f;
81: } else if (meta.isHsql()) {
82: DefaultBlobFormatter f = new DefaultBlobFormatter();
83: f.setUseUpperCase(false);
84: f.setPrefix("'");
85: f.setSuffix("'");
86: return f;
87: }
88:
89: // Still no luck, use the ANSI format.
90: return createAnsiFormatter();
91: }
92:
93: }
|