Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.sql;

import org.litesoft.bo.*;
import org.litesoft.bo.attributes.*;
import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.db.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.lazyeval.*;

import java.sql.*;
import org.litesoft.commonfoundation.typeutils.Objects;
import java.util.*;

public abstract class SQLProductSpecificAbstractHelper extends SQL_OR_Helper implements SQLProductSpecificHelper
{
    // boolean  storesLowerCaseIdentifiers()
    //          Retrieves whether this database treats mixed case unquoted SQL identifiers as case insensitive and stores them in lower case.
    // boolean  storesLowerCaseQuotedIdentifiers()
    //          Retrieves whether this database treats mixed case quoted SQL identifiers as case insensitive and stores them in lower case.
    // boolean  storesMixedCaseIdentifiers()
    //          Retrieves whether this database treats mixed case unquoted SQL identifiers as case insensitive and stores them in mixed case.
    // boolean  storesMixedCaseQuotedIdentifiers()
    //          Retrieves whether this database treats mixed case quoted SQL identifiers as case insensitive and stores them in mixed case.
    // boolean  storesUpperCaseIdentifiers()
    //          Retrieves whether this database treats mixed case unquoted SQL identifiers as case insensitive and stores them in upper case.
    // boolean  storesUpperCaseQuotedIdentifiers()
    //          Retrieves whether this database treats mixed case quoted SQL identifiers as case insensitive and stores them in upper case.
    // boolean  supportsMixedCaseIdentifiers()
    //          Does the database treat mixed case unquoted SQL identifiers as case sensitive and as a result store them in mixed case? A JDBC CompliantTM driver will always return false.
    // boolean  supportsMixedCaseQuotedIdentifiers()
    //          Does the database treat mixed case quoted SQL identifiers as case sensitive and as a result store them in mixed case? A JDBC CompliantTM driver will always return true.

    protected CaseNormalizer mCaseNormalizer = LeaveCaseUnchanged.INSTANCE;
    protected SQLIndexColumnsLocator mIndexColumnsLocator = null;

    protected SQLProductSpecificAbstractHelper()
    {
        buildTypeMaps();
    }

    @Override
    public void initializeDB( Connection pConnection, DBinfo pDBinfo, SQLIndexColumnsLocator pIndexColumnsLocator )
            throws SQLException
    {
        mIndexColumnsLocator = pIndexColumnsLocator;
        DatabaseMetaData zDBMD = pConnection.getMetaData();

        if ( zDBMD.storesLowerCaseIdentifiers() )
        {
            mCaseNormalizer = ToLowercase.INSTANCE;
        }
        else if ( zDBMD.storesUpperCaseIdentifiers() )
        {
            mCaseNormalizer = ToUppercase.INSTANCE;
        }
    }

    @Override
    public void addColumnType( StringBuilder pCmd, SimpleColumnDefinitionExtended pSCD )
    {
        ColumnType zColumnType = getColumnType( pSCD );

        String zSqlType = zColumnType.getSqlType();
        String zDim = "";

        Integer zMaxLength = MaxLength.get( pSCD.getAMD() );
        if ( zColumnType.needsDim() && (zMaxLength == null) )
        {
            if ( null == (zMaxLength = zColumnType.defaultDim()) )
            {
                throw new IllegalStateException( "No AttributeMetaData 'MaxLength' for " + pSCD );
            }
        }
        if ( (zMaxLength != null) && (zColumnType.canUseDim() || zColumnType.needsDim()) )
        {
            zDim = "(" + zMaxLength + ")";
        }

        addColumnType( pCmd, zSqlType, zDim );
    }

    private ColumnType getColumnType( SimpleColumnDefinitionExtended pSCD )
    {
        AttributeMetaData zAMD = pSCD.getAMD();
        if ( zAMD == null )
        {
            throw new IllegalStateException( "No AttributeMetaData for " + pSCD );
        }
        AttributeMetaData.Type zType = zAMD.getType();
        if ( zType == null )
        {
            throw new IllegalStateException( "No AttributeMetaData.Type for " + pSCD );
        }
        BoAttribute.AttributeType zBoAttributeType = zType.getBoAttributeType();
        if ( zBoAttributeType == null )
        {
            throw new IllegalStateException( "No AttributeMetaData.Type.BoAttributeType for " + pSCD );
        }
        Class<?> zPOclass = zBoAttributeType.getType();
        if ( zPOclass == null )
        {
            throw new IllegalStateException( "No AttributeMetaData.Type.BoAttributeType.Type for " + pSCD );
        }
        Class<?> zJDBCclass = zPOclass;
        LiteSoftToJDBCtypeSupport zSupport = LiteSoftToJDBCtypeSupport.get( zPOclass );
        if ( zSupport != null )
        {
            zJDBCclass = zSupport.getJDBC();
            if ( (zJDBCclass == null) || NonPersistable.class.equals( zJDBCclass ) )
            {
                throw new IllegalStateException( "Not Persistable " + pSCD );
            }
        }
        ColumnType zColumnType = mColumnTypeBySQLTypeMap.get( zJDBCclass );
        if ( zColumnType == null )
        {
            throw new IllegalStateException( "Persistence of type (" + zJDBCclass + ") not supported for " + pSCD );
        }
        return zColumnType;
    }

    protected void addColumnType( StringBuilder pCmd, String pSqlType, String pDim )
    {
        pCmd.append( pSqlType );
        pCmd.append( pDim );
    }

    @Override
    public String normalizeIdentifier( String pIdentifier )
    {
        return mCaseNormalizer.normalize( pIdentifier );
    }

    @Override
    public String[] getTablesFor( Connection pConnection, DBinfo pDBinfo, String pTableNamePattern )
            throws SQLException
    {
        List<String> zMatches = SQLHelper.getAllTableNames( pConnection, //
                                                            pTableNamePattern );
        String zTableNamePattern = normalizeIdentifier( pTableNamePattern );
        if ( !zTableNamePattern.equals( pTableNamePattern ) )
        {
            List<String> zMatches2 = SQLHelper.getAllTableNames( pConnection, //
                                                                 zTableNamePattern );
            if ( !zMatches2.isEmpty() )
            {
                if ( zMatches.isEmpty() )
                {
                    zMatches = zMatches2;
                }
                else
                {
                    zMatches.addAll( zMatches2 );
                }
            }
        }
        return zMatches.isEmpty() ? Strings.EMPTY_ARRAY : zMatches.toArray( new String[zMatches.size()] );
    }

    @Override
    public Map<String, DBtableColumn[]> getColumns( Connection pConnection, DBinfo pDBinfo, String pTableNamePattern )
            throws SQLException
    {
        Map<String, DBtableColumn[]> zMatches = SQLHelper.getAllTableColumns( pConnection, //
                                                                              pTableNamePattern );
        String zTableNamePattern = normalizeIdentifier( pTableNamePattern );
        if ( !zTableNamePattern.equals( pTableNamePattern ) )
        {
            Map<String, DBtableColumn[]> zMatches2 = SQLHelper.getAllTableColumns( pConnection, //
                                                                                   zTableNamePattern );
            if ( !zMatches2.isEmpty() )
            {
                if ( zMatches.isEmpty() )
                {
                    zMatches = zMatches2;
                }
                else
                {
                    zMatches.putAll( zMatches2 );
                }
            }
        }
        return zMatches;
    }

    @Override
    public Map<String, DBtableIndex[]> getIndexes( Connection pConnection, DBinfo pDBinfo, String pTableName )
            throws SQLException
    {
        Map<String, DBtableIndex[]> zFounds = new HashMap<String, DBtableIndex[]>();

        String[] zTableNames = getTablesFor( pConnection, pDBinfo, SQLHelper.escapeTableNamePattern( pTableName ) );
        for ( String zTableName : zTableNames )
        {
            List<DBtableIndex> zMatches = SQLHelper.getAllTableIndexes( pConnection, zTableName );
            zFounds.put( zTableName, zMatches.toArray( new DBtableIndex[zMatches.size()] ) );
        }
        return zFounds;
    }

    private static final String SQL_STATE_CONSTRAINT_VIOLATION = "23000";
    private static final String SQL_STATE_CONSTRAINT_VIOLATION_STARTSWITH = SQL_STATE_CONSTRAINT_VIOLATION.substring( 0, 3 );

    @SuppressWarnings({"ThrowableInstanceNeverThrown"}) @Override
    public WrappedSQLException wrapSQLException( SQLException pException )
    {
        if ( isConstraintViolation( pException ) )
        {
            if ( isMissingSomethingException( pException ) )
            {
                if ( isMissingPKException( pException ) )
                {
                    return createMissingPKException( pException );
                }

                if ( isMissingFieldValueException( pException ) )
                {
                    return createMissingValueFieldException( pException );
                }
                return createMissingSomethingException( pException );
            }
            if ( isDuplicatePKException( pException ) )
            {
                return createDuplicatePKException( pException );
            }

            if ( isDuplicateFieldValueException( pException ) )
            {
                return createDuplicateFieldValueException( pException );
            }
            return createConstraintViolationException( pException );
        }

        //else
        return new WrappedSQLException( pException );
    }

    protected boolean isConstraintViolation( SQLException pException )
    {
        return pException.getSQLState().startsWith( SQL_STATE_CONSTRAINT_VIOLATION_STARTSWITH );
    }

    protected boolean isMissingSomethingException( SQLException pException )
    {
        return false;
    }

    protected boolean isMissingFieldValueException( SQLException pException )
    {
        return false;
    }

    protected boolean isMissingPKException( SQLException pException )
    {
        return false;
    }

    protected boolean isDuplicateFieldValueException( SQLException pException )
    {
        return false;
    }

    protected boolean isDuplicatePKException( SQLException pException )
    {
        return false;
    }

    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
    protected WrappedSQLException createMissingSomethingException( SQLException pException )
    {
        return new MissingPkOrValueFieldException( pException );
    }

    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
    protected WrappedSQLException createDuplicatePKException( SQLException pException )
    {
        return new DuplicatePKException( pException );
    }

    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
    protected WrappedSQLException createConstraintViolationException( SQLException pException )
    {
        return new ConstraintViolationException( pException );
    }

    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
    protected WrappedSQLException createMissingValueFieldException( SQLException pException )
    {
        List<String> pColumnNames = Collections.emptyList();
        return new MissingRequiredValueFieldException( pException, pColumnNames );
    }

    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
    protected MissingPKException createMissingPKException( SQLException pException )
    {
        return new MissingPKException( pException );
    }

    @SuppressWarnings({"ThrowableInstanceNeverThrown"})
    protected WrappedSQLException createDuplicateFieldValueException( SQLException pException )
    {
        return new DuplicateValueFieldException( pException );
    }

    protected static boolean LLvalidateConWith( Connection pConnection, String pSQL )
    {
        return execNoResultSQL( pConnection, pSQL );
    }

    private interface CaseNormalizer
    {
        public String normalize( String pIdentifier );
    }

    public static class ToUppercase implements CaseNormalizer
    {
        public static final CaseNormalizer INSTANCE = new ToUppercase();

        @Override
        public String normalize( String pIdentifier )
        {
            return pIdentifier.toUpperCase();
        }
    }

    public static class ToLowercase implements CaseNormalizer
    {
        public static final CaseNormalizer INSTANCE = new ToLowercase();

        @Override
        public String normalize( String pIdentifier )
        {
            return pIdentifier.toLowerCase();
        }
    }

    public static class LeaveCaseUnchanged implements CaseNormalizer
    {
        public static final CaseNormalizer INSTANCE = new LeaveCaseUnchanged();

        @Override
        public String normalize( String pIdentifier )
        {
            return pIdentifier;
        }
    }

    protected boolean LLtableExists( Connection pConnection, DBinfo pDBinfo, String pTableName, String pExistsOnSuccessSQL )
            throws SQLException, MultipleTablesException
    {
        String[] zTableNames = getTablesFor( pConnection, pDBinfo, SQLHelper.escapeTableNamePattern( pTableName ) );
        if ( Objects.isNullOrEmpty( zTableNames ) )
        {
            return false;
        }
        if ( zTableNames.length > 1 )
        {
            throw MultipleTablesException.createFrom( pTableName, zTableNames );
        }
        return execNoResultSQL( pConnection, pExistsOnSuccessSQL );
    }

    protected boolean LLcolumnExists( Connection pConnection, DBinfo pDBinfo, String pTableName, String pColumnName, String pExistsOnSuccessSQL )
            throws SQLException, MultipleTablesException
    {
        Map<String, DBtableColumn[]> zFound = getColumns( pConnection, pDBinfo, SQLHelper.escapeTableNamePattern( pTableName ) );
        if ( zFound.isEmpty() )
        {
            return false;
        }
        if ( zFound.size() > 1 )
        {
            throw MultipleTablesException.createFrom( pTableName, zFound.keySet() );
        }
        return columnIn( pColumnName, zFound.values().iterator().next() ) && execNoResultSQL( pConnection, pExistsOnSuccessSQL );
    }

    private boolean columnIn( String pColumnName, DBtableColumn[] pColumns )
    {
        for ( DBtableColumn zColumn : pColumns )
        {
            if ( zColumn.getColumnName().equalsIgnoreCase( pColumnName ) )
            {
                return true;
            }
        }
        return false;
    }

    private void buildTypeMaps()
    {

        add( ColumnType.of( java.sql.Date.class, "DATE" ) );
        add( ColumnType.of( java.sql.Time.class, "TIME" ) );
        add( ColumnType.of( java.sql.Timestamp.class, "TIMESTAMP" ) );
        add( ColumnType.of( Boolean.class, "BOOLEAN" ) );
        add( ColumnType.dimable( String.class, "VARCHAR" ) );
        add( ColumnType.of( Integer.class, "INTEGER" ) );
        add( ColumnType.of( Long.class, "BIGINT" ) );
        add( ColumnType.of( Float.class, "FLOAT" ) );
        add( ColumnType.of( Double.class, "FLOAT" ) );
    }

    protected static class ColumnType
    {
        private Class mJDBCclass;
        private String mSqlType;
        private boolean mCanUseDim;
        private boolean mNeedsDim;
        private Integer mDefaultDim;

        private ColumnType( Class pJDBCclass, String pSqlType, boolean pCanUseDim, boolean pNeedsDim, Integer pDefaultDim )
        {
            mJDBCclass = pJDBCclass;
            mSqlType = pSqlType;
            mCanUseDim = pCanUseDim;
            mNeedsDim = pNeedsDim;
            mDefaultDim = pDefaultDim;
        }

        public static ColumnType of( Class pJDBCclass, String pSqlType )
        {
            return new ColumnType( pJDBCclass, pSqlType, false, false, null );
        }

        public static ColumnType dimable( Class pJDBCclass, String pSqlType )
        {
            return new ColumnType( pJDBCclass, pSqlType, true, false, null );
        }

        public static ColumnType dimmed( Class pJDBCclass, String pSqlType, Integer pDefaultDim )
        {
            return new ColumnType( pJDBCclass, pSqlType, false, true, pDefaultDim );
        }

        public static ColumnType dimmed( Class pJDBCclass, String pSqlType )
        {
            return dimmed( pJDBCclass, pSqlType, null );
        }

        public Class getJDBCclass()
        {
            return mJDBCclass;
        }

        public String getSqlType()
        {
            return mSqlType;
        }

        public boolean canUseDim()
        {
            return mCanUseDim;
        }

        public boolean needsDim()
        {
            return mNeedsDim;
        }

        public Integer defaultDim()
        {
            return mDefaultDim;
        }
    }

    protected void add( ColumnType pColumnType )
    {
        mColumnTypeBySQLTypeMap.put( pColumnType.getJDBCclass(), pColumnType );
    }

    protected final HashMap<Class, ColumnType> mColumnTypeBySQLTypeMap = new HashMap<Class, ColumnType>();
}

Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/sql/SQLProductSpecificAbstractHelper.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
800 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:33:38 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

24 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 01:51:38 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000