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

import org.litesoft.commonfoundation.typeutils.*;
import org.litesoft.orsup.base.*;
import org.litesoft.orsup.nonpublic.*;
import org.litesoft.orsup.selection.*;
import org.litesoft.orsup.selection.nonpublic.*;

public class SQL_OR_Helper extends SQLHelper
{
    public interface PersistingAccessorSCDprocessor
    {
        /**
         * @return true if anything Added
         */
        boolean process( StringBuilder pSB, String pPrefix, AttributeAccessorSCD pSCD );
    }

    private static PersistingAccessorSCDprocessor BUILD_ALL_SELECT_COLUMNS = new PersistingAccessorSCDprocessor()
    {
        @Override
        public boolean process( StringBuilder pSB, String pPrefix, AttributeAccessorSCD pSCD )
        {
            pSB.append( pPrefix ).append( pSCD.getColumnName() );
            return true;
        }
    };

    private static PersistingAccessorSCDprocessor BUILD_ALL_TABLE_COLUMNS = new PersistingAccessorSCDprocessor()
    {
        @Override
        public boolean process( StringBuilder pSB, String pPrefix, AttributeAccessorSCD pSCD )
        {
            pSB.append( pPrefix ).append( pSCD.getColumnName() );

            if ( pSCD.hasSearchColumn() )
            {
                pSB.append( ", " ).append( pSCD.getSearchColumnName() );
            }
            return true;
        }
    };

    protected static String processPersistingAccessorSCDs( StringBuilder pSB, String pInitialPrefix, MetaDataForPO pMetaDataForPO, PersistingAccessorSCDprocessor pProcessor )
    {
        String zPrefix = pInitialPrefix;
        for ( AttributeAccessorSCD zSCD : pMetaDataForPO.getPersistingAccessorSCDs() )
        {
            if ( pProcessor.process( pSB, zPrefix, zSCD ) )
            {
                zPrefix = ", ";
            }
        }
        return pSB.toString();
    }

    protected static String buildAllColumns( MetaDataForPO pMetaDataForPO, PersistingAccessorSCDprocessor pProcessor )
    {
        return processPersistingAccessorSCDs( new StringBuilder(), "", pMetaDataForPO, pProcessor );
    }

    protected static String buildAllSelectColumns( MetaDataForPO pMetaDataForPO )
    {
        return buildAllColumns( pMetaDataForPO, BUILD_ALL_SELECT_COLUMNS );
    }

    protected static String buildAllTableColumns( MetaDataForPO pMetaDataForPO )
    {
        return buildAllColumns( pMetaDataForPO, BUILD_ALL_TABLE_COLUMNS );
    }

    protected static String buildQuery( boolean pIncludingImmortalInactives, //
                                        MetaDataForPO pMetaDataForPO,//
                                        WhereClause pSelectionWhereClause, OrderBy pOrderBy )
    {
        return buildQuery( pIncludingImmortalInactives, pMetaDataForPO, buildAllSelectColumns( pMetaDataForPO ), pSelectionWhereClause, pOrderBy );
    }

    protected static String buildQuery( boolean pIncludingImmortalInactives, //
                                        MetaDataForPO pMetaDataForPO,//
                                        String pColumns, WhereClause pSelectionWhereClause, OrderBy pOrderBy )
    {
        return "SELECT " + pColumns + //
               " FROM " + pMetaDataForPO.getTableName() + //
               " " + toSQL( pIncludingImmortalInactives, pMetaDataForPO, pSelectionWhereClause ) + //
               " " + toSQL( pMetaDataForPO, pOrderBy );
    }

    protected static String toSQL( boolean pIncludingImmortalInactives, //
                                   MetaDataForPO pMetaDataForPO, //
                                   WhereClause pSelectionWhereClause )
    {
        if ( pIncludingImmortalInactives )
        {
            return toSQL( pSelectionWhereClause );
        }
        WhereClause wc = ExcludeImmortalInactives.augment( pMetaDataForPO, pSelectionWhereClause );
        return (wc != null) ? wc.toSQL( ExcludeImmortalInactives.INSTANCE ) : ALL_WHERECLAUSE;
    }

    protected static String toSQL( WhereClause pSelectionWhereClause )
    {
        return (pSelectionWhereClause != null) ? pSelectionWhereClause.toSQL() : ALL_WHERECLAUSE;
    }

    protected static String toSQL( MetaDataForPO pMetaDataForPO, OrderBy pOrderBy )
    {
        if ( pOrderBy != null )
        {
            return pOrderBy.toSQL();
        }
        String orderby = Strings.noEmpty( pMetaDataForPO.getDefaultOrderByAttributeName() );
        if ( orderby != null )
        {
            return BuilderMetaDataAwareOrderBy.create( pMetaDataForPO, orderby ).toSQL();
        }
        return "";
    }

    private static class ExcludeImmortalInactives implements WCtoSqlHelper
    {
        public static final ExcludeImmortalInactives INSTANCE = new ExcludeImmortalInactives();

        public static WhereClause augment( MetaDataForPO pMetaDataForPO, WhereClause pWhereClause )
        {
            if ( !pMetaDataForPO.isPotentiallyImmortallyInActive() )
            {
                return pWhereClause;
            }
            WhereClauseFactory f = WhereClauseFactory.INSTANCE;
            String inactiveAttributeName = pMetaDataForPO.getImmortalInactiveAttributeName();
            AttributeAccessorSCD scd = pMetaDataForPO.getAccessorSCDrequired( inactiveAttributeName );
            WhereClause wcAugmentation = f.not( f.isEqual( scd, Boolean.TRUE ) );
            return (pWhereClause == null) ? wcAugmentation : f.and( wcAugmentation, pWhereClause );
        }

        @Override
        public boolean preRender( WhereClause pWC, StringBuilder pSB )
        {
            return false;
        }

        @Override
        public void postRender( WhereClause pWC, StringBuilder pSB )
        {
        }

        @Override
        public boolean preRender( SingleColumnSelect pSCS, StringBuilder pSB )
        {
            SimpleFromIdentifier from = pSCS.getFromIdentifier();
            if ( from instanceof MetaDataForPO )
            {
                pSCS.LLtoSqlHelper( this, pSB, augment( (MetaDataForPO) from, pSCS.getWhereClause() ) );
                return true;
            }
            return false;
        }

        @Override
        public void postRender( SingleColumnSelect pSCS, StringBuilder pSB )
        {
        }
    }
}

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

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

Extracting commonfoundation

801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
222 Diff Diff GeorgeS picture GeorgeS Fri 20 May, 2011 18:04:31 +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