1 /*
2  *  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License, version 2.0,
6  *  as published by the Free Software Foundation.
7  *
8  *  This program is also distributed with certain software (including
9  *  but not limited to OpenSSL) that is licensed under separate terms,
10  *  as designated in a particular file or component or in included license
11  *  documentation.  The authors of MySQL hereby grant you an additional
12  *  permission to link the program and your derivative works with the
13  *  separately licensed software that they have included with MySQL.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License, version 2.0, for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23  */
24 
25 package com.mysql.clusterj.tie;
26 
27 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst;
28 import com.mysql.ndbjtie.ndbapi.NdbDictionary.IndexConst;
29 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
30 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.Element;
31 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.ElementArray;
32 
33 import com.mysql.clusterj.core.store.Index;
34 import com.mysql.clusterj.core.store.Table;
35 
36 import com.mysql.clusterj.core.util.I18NHelper;
37 import com.mysql.clusterj.core.util.Logger;
38 import com.mysql.clusterj.core.util.LoggerFactoryService;
39 
40 /**
41  *
42  */
43 class DictionaryImpl implements com.mysql.clusterj.core.store.Dictionary {
44 
45     /** My message translator */
46     static final I18NHelper local = I18NHelper
47             .getInstance(DictionaryImpl.class);
48 
49     /** My logger */
50     static final Logger logger = LoggerFactoryService.getFactory()
51             .getInstance(DictionaryImpl.class);
52 
53     private DictionaryConst ndbDictionary;
54 
DictionaryImpl(DictionaryConst ndbDictionary)55     public DictionaryImpl(DictionaryConst ndbDictionary) {
56         this.ndbDictionary = ndbDictionary;
57     }
58 
getTable(String tableName)59     public Table getTable(String tableName) {
60         TableConst ndbTable = ndbDictionary.getTable(tableName);
61         if (ndbTable == null) {
62             // try the lower case table name
63             ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
64         }
65         if (ndbTable == null) {
66             return null;
67         }
68         return new TableImpl(ndbTable, getIndexNames(ndbTable.getName()));
69     }
70 
getIndex(String indexName, String tableName, String indexAlias)71     public Index getIndex(String indexName, String tableName, String indexAlias) {
72         if ("PRIMARY$KEY".equals(indexName)) {
73             // create a pseudo index for the primary key hash
74             TableConst ndbTable = ndbDictionary.getTable(tableName);
75             if (ndbTable == null) {
76                 // try the lower case table name
77                 ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
78             }
79             handleError(ndbTable, ndbDictionary, "");
80             return new IndexImpl(ndbTable);
81         }
82         IndexConst ndbIndex = ndbDictionary.getIndex(indexName, tableName);
83         if (ndbIndex == null) {
84             // try the lower case table name
85             ndbIndex = ndbDictionary.getIndex(indexName, tableName.toLowerCase());
86         }
87         handleError(ndbIndex, ndbDictionary, indexAlias);
88         return new IndexImpl(ndbIndex, indexAlias);
89     }
90 
getIndexNames(String tableName)91     public String[] getIndexNames(String tableName) {
92         // get all indexes for this table including ordered PRIMARY
93         com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List indexList =
94             com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.create();
95         final String[] result;
96         try {
97             int returnCode = ndbDictionary.listIndexes(indexList, tableName);
98             handleError(returnCode, ndbDictionary, tableName);
99             int count = indexList.count();
100             result = new String[count];
101             if (logger.isDetailEnabled()) logger.detail("Found " + count + " indexes for " + tableName);
102             ElementArray elementArray = indexList.elements();
103             for (int i = 0; i < count; ++i) {
104                 Element element = elementArray.at(i);
105                 handleError(element, ndbDictionary, String.valueOf(i));
106                 String indexName = element.name();
107                 result[i] = indexName;
108             }
109         } finally {
110             // free the list memory even if error
111             com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.delete(indexList);
112         }
113         return result;
114     }
115 
handleError(int returnCode, DictionaryConst ndbDictionary, String extra)116     protected static void handleError(int returnCode, DictionaryConst ndbDictionary, String extra) {
117         if (returnCode == 0) {
118             return;
119         } else {
120             Utility.throwError(returnCode, ndbDictionary.getNdbError(), extra);
121         }
122     }
123 
handleError(Object object, DictionaryConst ndbDictionary, String extra)124     protected static void handleError(Object object, DictionaryConst ndbDictionary, String extra) {
125         if (object != null) {
126             return;
127         } else {
128             Utility.throwError(null, ndbDictionary.getNdbError(), extra);
129         }
130     }
131 
132 }
133