Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / instruction / FieldFactory.java @ 1956

History | View | Annotate | Download (4.19 KB)

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

    
3
import com.hardcode.gdbms.engine.data.DataSource;
4
import com.hardcode.gdbms.engine.data.driver.DriverException;
5

    
6

    
7
/**
8
 * Clase que se encarga de crear los objetos Field de las instrucciones Select
9
 * a  partir del nombre del campo
10
 *
11
 * @author Fernando Gonz?lez Cort?s
12
 */
13
public class FieldFactory {
14
        /**
15
         * Dada una lista de tablas y el nombre de un campo, devuelve el objeto
16
         * Field conteniendo la informaci?n del ?ndice de la tabla a la que
17
         * pertenece el campo y el ?ndice del campo dentro de dicha tabla
18
         *
19
         * @param tables Array de tablas donde se buscar? el campo
20
         * @param fieldName Nombre del campo que se est? buscando
21
         * @param source Fuente de datos para el campo que se crea. El campo
22
         *                   obtendr? sus valores de dicha fuente.
23
         *
24
         * @return Objeto Field
25
         *
26
         * @throws AmbiguousFieldNameException Si hay dos tablas que pueden tener
27
         *                    el campo
28
         * @throws DriverException Si se produce un error accediendo a los campos
29
         * @throws FieldNotFoundException Si el campo no se encuentra en ninguna de
30
         *                    las tablas
31
         */
32
        public static Field createField(DataSource[] tables, String fieldName,
33
                DataSource source)
34
                throws AmbiguousFieldNameException, DriverException, 
35
                        FieldNotFoundException {
36
                if (fieldName.indexOf(".") != -1) {
37
                        return createWithTable(tables, fieldName, source);
38
                } else {
39
                        return createWithoutTable(tables, fieldName, source);
40
                }
41
        }
42

    
43
        /**
44
         * Crea un campo que viene especificado por el nombre de campo sin el
45
         * nombre de la tabla a la que pertenece
46
         *
47
         * @param tables Array de tablas donde se buscar? el campo
48
         * @param fieldName Nombre del campo que se est? buscando
49
         * @param source Fuente de datos para el campo que se crea
50
         *
51
         * @return Objeto Field
52
         *
53
         * @throws FieldNotFoundException Si el campo no se encuentra en ninguna de
54
         *                    las tablas
55
         * @throws AmbiguousFieldNameException Si hay dos tablas que pueden tener
56
         *                    el campo
57
         * @throws DriverException Si se produce un error accediendo a los campos
58
         */
59
        private static Field createWithoutTable(DataSource[] tables,
60
                String fieldName, DataSource source)
61
                throws FieldNotFoundException, AmbiguousFieldNameException, 
62
                        DriverException {
63
                int retIndex = -1;
64
                int dataSource = -1;
65

    
66
                for (int i = 0; i < tables.length; i++) {
67
                        int index = tables[i].getFieldIndexByName(fieldName);
68

    
69
                        if (index != -1) {
70
                                //Si ya se hab?a encontrado uno
71
                                if (retIndex != -1) {
72
                                        throw new AmbiguousFieldNameException(fieldName);
73
                                } else {
74
                                        retIndex = index;
75
                                        dataSource = i;
76
                                }
77
                        }
78
                }
79

    
80
                if (retIndex == -1) {
81
                        throw new FieldNotFoundException(fieldName);
82
                }
83

    
84
                Field ret = new Field();
85
                ret.setDataSourceIndex(dataSource);
86
                ret.setFieldId(retIndex);
87
                ret.setTables(tables);
88
                ret.setDataSource(source);
89

    
90
                return ret;
91
        }
92

    
93
        /**
94
         * Crea un campo que viene especificado por el nombre de la tabla seguido
95
         * de "." y del nombre del campo de dicha tabla
96
         *
97
         * @param tables Array de tablas donde se buscar? el campo
98
         * @param fieldName Nombre del campo que se est? buscando
99
         * @param source Fuente de datos para el campo que se crea
100
         *
101
         * @return Objeto Field
102
         *
103
         * @throws FieldNotFoundException Si el campo no se encuentra en ninguna de
104
         *                    las tablas
105
         * @throws AmbiguousFieldNameException Si hay dos tablas que pueden tener
106
         *                    el campo
107
         * @throws DriverException Si se produce un error accediendo a los campos
108
         */
109
        private static Field createWithTable(DataSource[] tables, String fieldName,
110
                DataSource source)
111
                throws FieldNotFoundException, AmbiguousFieldNameException, 
112
                        DriverException {
113
                int retIndex = -1;
114
                int dataSource = -1;
115

    
116
                //Se obtiene el nombre de la tabla y del campo
117
                String[] nombres = fieldName.split("[.]");
118
                String tableName = nombres[0].trim();
119
                fieldName = nombres[1].trim();
120

    
121
                for (int i = 0; i < tables.length; i++) {
122
                        if (tables[i].getName().equals(tableName)) {
123
                                retIndex = tables[i].getFieldIndexByName(fieldName);
124
                                dataSource = i;
125

    
126
                                break;
127
                        }
128
                }
129

    
130
                if (retIndex == -1) {
131
                        throw new FieldNotFoundException(fieldName);
132
                }
133

    
134
                Field ret = new Field();
135
                ret.setDataSourceIndex(dataSource);
136
                ret.setFieldId(retIndex);
137
                ret.setTables(tables);
138
                ret.setDataSource(source);
139

    
140
                return ret;
141
        }
142
}