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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.sql;

import org.litesoft.db.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.nonpublic.*;
import org.litesoft.orsup.selection.*;

public class SQLORPersistenceHelper extends SQLPersistenceHelper implements ORPersistenceHelper
{
    public SQLORPersistenceHelper( SQLDBconnectionProvider pSQLDBconnectionProvider )
    {
        super( pSQLDBconnectionProvider );
    }

    // ORPersistenceHelper vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

    @Override
    public long count( boolean pIncludingImmortalInactives, //
                       MetaDataForPO pMetaDataForPO, //
                       WhereClause pSelectionWhereClause )
            throws DBException
    {
        String whereClause = SQL_OR_Helper.toSQL( pIncludingImmortalInactives, pMetaDataForPO, pSelectionWhereClause );
        return rawCount( pMetaDataForPO.getTableName(), whereClause );
    }

    /**
     * @param pFinder               - either a NonTransactionalFinder or a Transaction
     * @param pMetaDataForPO        - Persisted Object MetaData
     * @param pSelectionWhereClause - null means all
     * @param pOrderBy              - null means order immaterial
     * @param pOffset               - Rows to Skip (>= 0 verified)
     * @param pLimit                - Max Rows to return (>= 1 verified)
     *
     * @return !Null
     *
     * @throws DBException - Exception regarding the OR layer
     */
    @Override
    public POQueryIterator findSubset( Finder pFinder, //
                                       boolean pIncludingImmortalInactives, //
                                       MetaDataForPOinternalExtension pMetaDataForPO, //
                                       WhereClause pSelectionWhereClause, OrderBy pOrderBy, //
                                       long pOffset, int pLimit )
            throws DBException
    {
        DBconnection zDBconnection = createConnection();
        try
        {
            String selectColumns = SQL_OR_Helper.buildAllSelectColumns( pMetaDataForPO );
            String tableName = pMetaDataForPO.getTableName();
            String wc = SQL_OR_Helper.toSQL( pIncludingImmortalInactives, pMetaDataForPO, pSelectionWhereClause );
            String orderBy = SQL_OR_Helper.toSQL( pMetaDataForPO, pOrderBy );
            String zCmd = getHelper().buildQuery( zDBconnection.getDBinfo().getProductVersion(), //
                                                  tableName, selectColumns, wc, orderBy, pOffset, pLimit );

            return new SQLORPersistenceHelper.MyPOQueryIterator( pMetaDataForPO, pFinder, this, zCmd, zDBconnection );
        }
        catch ( DBException e )
        {
            zDBconnection.dispose();
            throw e;
        }
    }

    @Override
    public POQueryIterator findAllCursored( Finder pFinder, POQueryFilter pPOQueryFilter, //
                                            boolean pIncludingImmortalInactives, //
                                            MetaDataForPOinternalExtension pMetaDataForPO, //
                                            WhereClause pSelectionWhereClause, OrderBy pOrderBy )
            throws DBException
    {
        return new SQLORPersistenceHelper.MyPOQueryIterator( pMetaDataForPO, pFinder, pPOQueryFilter, this, SQL_OR_Helper.buildQuery( pIncludingImmortalInactives, //
                                                                                                                                      pMetaDataForPO, //
                                                                                                                                      pSelectionWhereClause, pOrderBy ) );
    }

    @Override
    public POKeyIterator findAllKeysCursored( POKeyQueryFilter pPOKeyQueryFilter, //
                                              boolean pIncludingImmortalInactives, //
                                              MetaDataForPOinternalExtension pMetaDataForPO, //
                                              SimpleColumnDefinition pKeyColumn, WhereClause pSelectionWhereClause, OrderBy pOrderBy )
            throws DBException
    {
        return new SQLORPersistenceHelper.MyPOKeyIterator( pPOKeyQueryFilter, this, //
                                                           SQL_OR_Helper.buildQuery( pIncludingImmortalInactives, //
                                                                                     pMetaDataForPO, pKeyColumn.getColumnName(),
                                                                                     //
                                                                                     pSelectionWhereClause, pOrderBy ) );
    }

    @Override
    public void createTables( TableManager pTableManager, MetaDataForPO pMetaDataForPO )
            throws DBException
    {
        createTable( pMetaDataForPO );
        for ( MetaDataForPO.Index index : pMetaDataForPO.getIndexes() )
        {
            createIndex( pMetaDataForPO, index );
        }
        pTableManager.addTable( pMetaDataForPO.getTableName() );
    }

    @Override
    public void reCreateIndices( MetaDataForPO pMetaDataForPO )
            throws DBException
    {
        for ( MetaDataForPO.Index index : pMetaDataForPO.getIndexes() )
        {
            dropIndex( index );
            createIndex( pMetaDataForPO, index );
        }
    }

    @Override
    public void createIndex( MetaDataForPO pMetaDataForPO, MetaDataForPO.Index index )
    {
        StringBuilder cmd = new StringBuilder( "CREATE" );
        if ( index.isUnique() )
        {
            cmd.append( " UNIQUE" );
        }
        cmd.append( " INDEX " );
        cmd.append( index.getIndexName() );
        cmd.append( " ON " );
        cmd.append( pMetaDataForPO.getTableName() );
        cmd.append( " (" );
        String[] columns = index.getColumns();
        cmd.append( columns[0] );
        for ( int i = 1; i < columns.length; ++i )
        {
            cmd.append( ", " ).append( columns[i] );
        }
        cmd.append( ")" );
        executeNoResultsNoCommitSQL( cmd.toString() );
    }

    // ORPersistenceHelper ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    @SuppressWarnings({"unchecked"})
    protected WhereClause createVersionAwareWhereClause( MetaDataForPO pMetaDataForPO, PersistentObjectImpl pPersistentObject )
    {
        WhereClause wc = pPersistentObject.getPersistentObjectUniqueKey().convertToWC();
        if ( pMetaDataForPO.isRecordVersionable() )
        {
            String rvName = pMetaDataForPO.getRecordVersionAttributeName();
            AttributeAccessorSCD rvAASCD = pMetaDataForPO.getAccessorSCDrequired( rvName );
            WhereClauseFactory f = WhereClauseFactory.INSTANCE;
            wc = f.and( wc, f.isEqual( rvAASCD, rvAASCD.getValueOnPO( pPersistentObject ) ) );
        }
        return wc;
    }

    private void createTable( MetaDataForPO pMetaDataForPO )
            throws DBException
    {
        StringBuilder cmd = new StringBuilder();
        cmd.append( "CREATE TABLE " );
        cmd.append( pMetaDataForPO.getTableName() );
        cmd.append( " ( " );

        SQL_OR_Helper.processPersistingAccessorSCDs( cmd, "", pMetaDataForPO, new SQL_OR_Helper.PersistingAccessorSCDprocessor()
        {
            @Override
            public boolean process( StringBuilder pSB, String pPrefix, AttributeAccessorSCD pSCD )
            {
                pSB.append( pPrefix );
                createTableColumn( pSB, pSCD );
                return true;
            }
        } );

        AttributeAccessorKeySet keySet = pMetaDataForPO.getKeySet();
        if ( keySet != null )
        {
            cmd.append( ", CONSTRAINT " ).append( pMetaDataForPO.getTableName() ).append( "_PK PRIMARY KEY (" ).append( keySet.getColumns() ).append( ")" );
        }
        cmd.append( " )" );

        executeNoResultsNoCommitSQL( cmd.toString() );
    }

    private void dropIndex( MetaDataForPO.Index index )
    {
        String cmd = "DROP INDEX " + index.getIndexName();
        try
        {
            executeNoResultsNoCommitSQL( cmd );
        }
        catch ( DBException e )
        {
            // Whatever...
        }
    }

    private static class MyPOQueryIterator extends AbstractRowSourcePOQueryIterator<PersistentObject>
    {
        public MyPOQueryIterator( MetaDataForPOinternalExtension pMetaDataForPO, Finder pFinder, POQueryFilter pPOQueryFilter, SQLPersistenceHelper pPersistenceHelper, String pCmd )
        {
            this( pMetaDataForPO, pFinder, pPOQueryFilter, pPersistenceHelper, pCmd, null );
        }

        public MyPOQueryIterator( MetaDataForPOinternalExtension pMetaDataForPO, Finder pFinder, //
                                  SQLPersistenceHelper pPersistenceHelper, String pCmd, DBconnection pDBconnection )
        {
            this( pMetaDataForPO, pFinder, null, pPersistenceHelper, pCmd, pDBconnection );
        }

        private MyPOQueryIterator( MetaDataForPOinternalExtension pMetaDataForPO, Finder pFinder, POQueryFilter pPOQueryFilter, SQLPersistenceHelper pPersistenceHelper, String pCmd, DBconnection pDBconnection )
        {
            super( pMetaDataForPO, pFinder, pPOQueryFilter, new MyRowSource( pPersistenceHelper, pCmd, pDBconnection ) );
        }
    }

    private static class MyPOKeyIterator extends AbstractPOKeyIterator
    {
        public MyPOKeyIterator( POKeyQueryFilter pPOKeyQueryFilter, SQLPersistenceHelper pPersistenceHelper, String pCmd )
        {
            super( pPOKeyQueryFilter, new MyRowSource( pPersistenceHelper, pCmd, null ) );
        }
    }

    //private boolean indexDefinitionIsUpToDate( MetaDataForPO pMetaDataForPO, MetaDataForPO.Index pIndex )
    //        throws ORMapperException
    //{
    //    List<String> metadataColumns = Arrays.asList( pIndex.getColumns() );
    //    Collections.sort( metadataColumns );
    //    List<String> existingColumns = getIndexColumns( pMetaDataForPO.getTableName(), pIndex.getIndexName() );
    //    Collections.sort( existingColumns );
    //    return existingColumns.equals( metadataColumns );
    //}
}

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

Diff revisions: vs.
Revision Author Commited Message
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