API Overview API Index Package Overview Direct link to this page
JavaOnTracks 0.1.2
  net.jot.persistance.query. JOTQueryManager View Javadoc
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

/*
------------------------------------
JavaOnTracks          Thibaut Colar
tcolar-jot AT colar DOT net
Artistic Licence 2.0
http://www.javaontracks.net
------------------------------------
 */
package net.jot.persistance.query;

import net.jot.persistance.JOTTransaction;
import java.util.Hashtable;

import net.jot.logger.JOTLogger;
import net.jot.persistance.JOTModel;
import net.jot.persistance.JOTModelMapping;
import net.jot.persistance.JOTQueryResult;
import net.jot.persistance.JOTStatementFlags;

/**
 * Low(medium) level database access
 * Usually you would rather use JOTMOdel methods or the JOTQueryBuilder
 * unless you want to make a completely manual SQL query
 * @author thibautc
 * 
 *
 */
public final class JOTQueryManager {

    private static Hashtable modelMappings = new Hashtable();
    private static Hashtable implementations = new Hashtable();

    /*public static void delete(JOTTransaction transaction,JOTModel model) throws Exception
    {        
        JOTModelMapping mapping = getMapping(model.getClass());
        JOTQueryInterface impl = JOTQueryManager.getImplementation(mapping.getQueryClassName());
        impl.delete(transaction, model);
    }*/
    public static void save(JOTTransaction transaction, JOTModel model) throws Exception
    {
        JOTModelMapping mapping = getMapping(transaction, model.getClass());
        JOTQueryInterface impl = JOTQueryManager.getImplementation(mapping.getQueryClassName());
        impl.save(transaction, model);
    }

    /**
     * 
     * @param modelClass
     * @return
     * @throws java.lang.Exception
     */
    public static JOTModelMapping getMapping(JOTTransaction transaction, Class modelClass) throws Exception
    {
        return getMapping(transaction, modelClass, true, true);
    }

    /**
     * Internal Method, that usually would not be called by end-user.<br>
     * Get/Loads a Model mapping.<br>
     * modelClass MUST be of type JOTModel or subclass
     * @param modelClass
     * @return
     * @throws Exception
     */
    public static JOTModelMapping getMapping(JOTTransaction transaction, Class modelClass, boolean runValidation, boolean createMissingTables) throws Exception
    {
        String className = modelClass.getName();
        if (!modelMappings.containsKey(className))
        {
            synchronized (JOTQueryManager.class)
            {
                if (!modelMappings.containsKey(className))
                {
                    JOTLogger.log(JOTLogger.CAT_DB, JOTLogger.DEBUG_LEVEL, JOTModelMapping.class, "will create a new instance of: " + modelClass.getName());
                    Object modelImpl = modelClass.newInstance();
                    JOTModel model = (JOTModel) modelImpl;
                    model.initQueryImplClass();
                    Class impl = model.getQueryImplClass();
                    if (!implementations.containsKey(impl.getName()))
                    {
                        JOTLogger.log(JOTLogger.CAT_DB, JOTLogger.DEBUG_LEVEL, JOTModelMapping.class, "Adding impl: " + impl.getName());
                        implementations.put(impl.getName(), (JOTQueryInterface) impl.newInstance());
                    }
                    JOTModelMapping mapping = model.getMapping(transaction);
                    JOTLogger.log(JOTLogger.CAT_DB, JOTLogger.DEBUG_LEVEL, JOTModelMapping.class, "Adding mapping for : " + className);
                    modelMappings.put(className, mapping);
                }
            }

        }
        return (JOTModelMapping) modelMappings.get(className);
    }

    /**
     * Internal Method, Not to be used by end-user.<br>
     * Returns the QueryImplementation object, which deals with raw CRUD implementation.
     * @param className
     * @return
     * @throws Exception
     */
    public static JOTQueryInterface getImplementation(String className) throws Exception
    {
        // If this is called before the getmapping, it will return null.
        // this should never happen, so return null if it ever does.
        JOTLogger.log(JOTLogger.CAT_DB, JOTLogger.TRACE_LEVEL, JOTModelMapping.class, "Retrieving impl : " + className);
        return (JOTQueryInterface) implementations.get(className);
    }

    /**
     * This is here, if you want to make manual custom SQL calls not covered by the other methods<br>
     * 
     * NOTE: your request MUST return records matching your model.<br>
     * 
     * @param sql   ie: "select * from 'users' where first=?, last=? order by name" ... etc ...
     * @param params ie: ['John','Doe']
     * @return a Vector of JOTModel objects 
     */
    public static JOTQueryResult executeSQL(JOTTransaction transaction, Class modelClass, String sql, Object[] params, JOTStatementFlags flags) throws Exception
    {
        JOTModelMapping mapping = getMapping(transaction, modelClass);
        JOTQueryInterface impl = getImplementation(mapping.getQueryClassName());
        return impl.executeSQL(transaction,mapping, modelClass, sql, params, flags);
    }
    public static void updateSQL(JOTTransaction transaction, Class modelClass, String sql, Object[] params, JOTStatementFlags flags) throws Exception
    {
        JOTModelMapping mapping = getMapping(transaction, modelClass);
        JOTQueryInterface impl = getImplementation(mapping.getQueryClassName());
        impl.updateSQL(transaction,mapping, sql, params, flags);
    }

    public static String getTableName(JOTTransaction transaction, Class modelClass)
    {
        String name=null;
        try
        {
            JOTModelMapping mapping = getMapping(transaction, modelClass);
            name=mapping.getTableName();
        }
        catch(Exception e)
        {
            JOTLogger.logException(JOTLogger.CAT_DB, JOTQueryManager.class, "SQL Safe Table name is missing ??",e);
        }
        return name;
    }
    
}

Generated By: JavaOnTracks Doclet 0.1.5     ©Thibaut Colar