Revision 1956 trunk/libraries/libGDBMS/src/com/hardcode/gdbms/engine/instruction/UnionAdapter.java

View differences:

UnionAdapter.java
1 1
package com.hardcode.gdbms.engine.instruction;
2 2

  
3
import java.io.IOException;
3
import com.hardcode.driverManager.DriverLoadException;
4 4

  
5
import com.hardcode.driverManager.DriverLoadException;
6 5
import com.hardcode.gdbms.engine.data.DataSource;
7 6
import com.hardcode.gdbms.engine.data.DataSourceFactory;
8
import com.hardcode.gdbms.engine.data.DriverException;
9 7
import com.hardcode.gdbms.engine.data.NoSuchTableException;
8
import com.hardcode.gdbms.engine.data.driver.DriverException;
9
import com.hardcode.gdbms.parser.ParseException;
10 10

  
11
import java.io.IOException;
11 12

  
13

  
12 14
/**
13 15
 * Adaptador de la instrucci?n UNION
14 16
 *
15 17
 * @author Fernando Gonz?lez Cort?s
16 18
 */
17 19
public class UnionAdapter extends Adapter {
20
	/**
21
	 * DOCUMENT ME!
22
	 *
23
	 * @param table DOCUMENT ME!
24
	 *
25
	 * @return DOCUMENT ME!
26
	 *
27
	 * @throws SemanticException DOCUMENT ME!
28
	 * @throws RuntimeException DOCUMENT ME!
29
	 * @throws IllegalStateException DOCUMENT ME!
30
	 */
31
	private DataSource getTable(int table) throws SemanticException {
32
		Adapter hijo = getChilds()[table];
18 33

  
19
	private DataSource getTable(int table) throws SemanticException{
20
        Adapter hijo = getChilds()[table];
34
		if (hijo instanceof TableRefAdapter) {
35
			String name = Utilities.getText(hijo.getEntity());
21 36

  
22
        if (hijo instanceof TableRefAdapter) {
23
            String name = Utilities.getText(hijo.getEntity());
24

  
25
            return getTableByName(name);
26
        } else if (hijo instanceof SelectAdapter) {
27
            try {
28
                return getTableBySelect((SelectAdapter)hijo);
29
            } catch (DriverException e) {
30
                throw new RuntimeException(e);
31
            } catch (IOException e) {
32
                throw new RuntimeException(e);
37
			return getTableByName(name);
38
		} else if (hijo instanceof SelectAdapter) {
39
			try {
40
				return getTableBySelect((SelectAdapter) hijo);
41
			} catch (DriverException e) {
42
				throw new RuntimeException(e);
43
			} catch (IOException e) {
44
				throw new RuntimeException(e);
33 45
			} catch (DriverLoadException e) {
34
                throw new RuntimeException(e);
46
				throw new RuntimeException(e);
47
			} catch (ParseException e) {
48
				throw new RuntimeException(e);
35 49
			}
36
        } else {
37
            throw new IllegalStateException("Cannot crate the DataSource");
38
        }
39
		
50
		} else {
51
			throw new IllegalStateException("Cannot create the DataSource");
52
		}
40 53
	}
54

  
41 55
	/**
42
     * @see com.hardcode.gdbms.engine.instruction.UnionInstruction#getFirstTable()
43
     */
44
    public DataSource getFirstTable() throws SemanticException {
45
    	return getTable(0);
46
    }
56
	 * @see com.hardcode.gdbms.engine.instruction.UnionInstruction#getFirstTable()
57
	 */
58
	public DataSource getFirstTable() throws SemanticException {
59
		return getTable(0);
60
	}
47 61

  
48
    /**
49
     * Obtiene el data source a partir de una select
50
     *
51
     * @param select
52
     *
53
     * @return
54
     * @throws IOException
55
     * @throws SemanticException
56
     * @throws DriverException
57
     * @throws DriverLoadException
58
     * @throws DriverException
59
     * @throws SemanticException
60
     * @throws IOException
61
     * @throws IllegalAccessException
62
     * @throws InstantiationException
63
     */
64
    private DataSource getTableBySelect(SelectAdapter select) throws DriverLoadException, DriverException, SemanticException, IOException
65
         {
66
        return DataSourceFactory.createRandomDataSource(select);
67
    }
62
	/**
63
	 * Obtiene el data source a partir de una select
64
	 *
65
	 * @param select
66
	 *
67
	 * @return
68
	 *
69
	 * @throws DriverLoadException
70
	 * @throws DriverException
71
	 * @throws SemanticException
72
	 * @throws IOException
73
	 * @throws ParseException
74
	 */
75
	private DataSource getTableBySelect(SelectAdapter select)
76
		throws DriverLoadException, DriverException, SemanticException, 
77
			IOException, ParseException {
78
		return getInstructionContext().getDSFActory().createRandomDataSource(select, DataSourceFactory.MANUAL_MODE);
79
	}
68 80

  
69
    /**
70
     * Obtiene un data source por el nombre
71
     *
72
     * @param name
73
     *
74
     * @return
75
     *
76
     * @throws TableNotFoundException Si nop hay ninguna tabla con el nombre
77
     *         'name'
78
     * @throws RuntimeException
79
     */
80
    private DataSource getTableByName(String name)
81
        throws TableNotFoundException {
82
        String[] tabla = name.split(" ");
81
	/**
82
	 * Obtiene un data source por el nombre
83
	 *
84
	 * @param name
85
	 *
86
	 * @return
87
	 *
88
	 * @throws TableNotFoundException Si nop hay ninguna tabla con el nombre
89
	 * 		   'name'
90
	 * @throws RuntimeException
91
	 */
92
	private DataSource getTableByName(String name)
93
		throws TableNotFoundException {
94
		String[] tabla = name.split(" ");
83 95

  
84
        try {
85
            if (tabla.length == 1) {
86
                return DataSourceFactory.createRandomDataSource(name);
87
            } else {
88
                return DataSourceFactory.createRandomDataSource(tabla[0],
89
                    tabla[1]);
90
            }
91
        } catch (DriverLoadException e) {
96
		try {
97
			if (tabla.length == 1) {
98
				return getInstructionContext().getDSFActory()
99
						   .createRandomDataSource(name, DataSourceFactory.MANUAL_MODE);
100
			} else {
101
				return getInstructionContext().getDSFActory()
102
						   .createRandomDataSource(tabla[0], tabla[1],
103
					DataSourceFactory.MANUAL_MODE);
104
			}
105
		} catch (DriverLoadException e) {
106
			throw new RuntimeException(e);
107
		} catch (NoSuchTableException e) {
108
			throw new TableNotFoundException(e);
109
		} catch (DriverException e) {
92 110
            throw new RuntimeException(e);
93
        } catch (NoSuchTableException e) {
94
            throw new TableNotFoundException(e);
95 111
        }
96
    }
112
	}
97 113

  
98
    /**
99
     * @see com.hardcode.gdbms.engine.instruction.UnionInstruction#getSecondTable()
100
     */
101
    public DataSource getSecondTable() throws SemanticException {
102
    	return getTable(1);
103
    }
114
	/**
115
	 * @see com.hardcode.gdbms.engine.instruction.UnionInstruction#getSecondTable()
116
	 */
117
	public DataSource getSecondTable() throws SemanticException {
118
		return getTable(1);
119
	}
104 120
}

Also available in: Unified diff