You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by vt...@apache.org on 2005/12/05 14:25:41 UTC

svn commit: r354062 - in /directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm: JDBCSelector.java Select.java Table.java

Author: vtence
Date: Mon Dec  5 05:25:34 2005
New Revision: 354062

URL: http://svn.apache.org/viewcvs?rev=354062&view=rev
Log:
Extracted Select code into its own class.

Added:
    directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Select.java
Modified:
    directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/JDBCSelector.java
    directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Table.java

Modified: directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/JDBCSelector.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/JDBCSelector.java?rev=354062&r1=354061&r2=354062&view=diff
==============================================================================
--- directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/JDBCSelector.java (original)
+++ directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/JDBCSelector.java Mon Dec  5 05:25:34 2005
@@ -17,70 +17,27 @@
 package org.apache.authx.authentication.realm;
 
 import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
 
 public class JDBCSelector implements Selector
 {
-    private final Table m_table;
-    private final Collection m_expressions;
+    private final Select m_select;
     private final SQLExpressionBuilder m_builder;
 
     public JDBCSelector( Table table )
     {
-        m_table = table;
-        m_expressions = new ArrayList();
+        m_select = table.select();
         m_builder = new SQLExpressionBuilder( table );
     }
 
     public Selector eq( Object key, Object c )
     {
-        addExpression( m_builder.eq( key, c ) );
+        m_select.where( m_builder.eq( key, c ) );
         return this;
     }
 
-    private void addExpression( SQLExpression expression )
-    {
-        m_expressions.add( expression );
-    }
-
     public Object select( Connection c ) throws SQLException
     {
-        PreparedStatement st = null;
-        Object result = null;
-        try
-        {
-            PreparedSQLStatement statement = new PreparedSQLStatement( "select * from " );
-            statement.append( m_table.getName() );
-            statement.append( " where " );
-            prepare( statement );
-            st = statement.create( c );
-            ResultSet rs = st.executeQuery();
-            if ( rs.next() )
-            {
-                result = m_table.instantiate( rs );
-            }
-            rs.close();
-        }
-        finally
-        {
-            if (st != null) st.close();
-        }
-
-        return result;
-    }
-
-    private void prepare( SQLStatement statement )
-    {
-        for ( Iterator it = m_expressions.iterator(); it.hasNext(); )
-        {
-            SQLExpression expression = (SQLExpression) it.next();
-            expression.prepare( statement );
-            if ( it.hasNext() ) statement.append( " and " );
-        }
+        return m_select.execute( c );
     }
 }

Added: directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Select.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Select.java?rev=354062&view=auto
==============================================================================
--- directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Select.java (added)
+++ directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Select.java Mon Dec  5 05:25:34 2005
@@ -0,0 +1,78 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.authx.authentication.realm;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+public class Select
+{
+    private final Table m_table;
+    private final PreparedSQLStatement m_statement;
+    private boolean m_first = true;
+
+    protected Select( Table table )
+    {
+        m_table = table;
+        m_statement = new PreparedSQLStatement( "select * from " );
+        m_statement.append( table.getName() );
+    }
+
+    public static Select from( Table table )
+    {
+        return new Select( table );
+    }
+
+    public Select where( SQLExpression expression )
+    {
+        if (m_first)
+        {
+            m_first = false;
+            m_statement.append( " where ");
+        }
+        else
+        {
+            m_statement.append( " and ");
+        }
+
+        expression.prepare( m_statement );
+        return this;
+    }
+
+    public Object execute( Connection c ) throws SQLException
+    {
+        Object result = null;
+        PreparedStatement st = null;
+        try {
+            st = m_statement.create( c );
+            ResultSet rs = st.executeQuery();
+            if ( rs.next() )
+            {
+                result = m_table.instantiate( rs );
+            }
+            rs.close();
+        }
+        finally
+        {
+            if (st != null) st.close();
+        }
+        return result;
+    }
+
+}

Modified: directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Table.java
URL: http://svn.apache.org/viewcvs/directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Table.java?rev=354062&r1=354061&r2=354062&view=diff
==============================================================================
--- directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Table.java (original)
+++ directory/authx/trunk/jdbc/src/java/org/apache/authx/authentication/realm/Table.java Mon Dec  5 05:25:34 2005
@@ -40,6 +40,10 @@
         m_mapping = mapping;
     }
 
+    public Select select() {
+        return Select.from( this );
+    }
+
     public String getName()
     {
         return m_name;