2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package com.mysql.clusterj.tie;
20 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst;
21 import com.mysql.ndbjtie.ndbapi.NdbDictionary.IndexConst;
22 import com.mysql.ndbjtie.ndbapi.NdbDictionary.TableConst;
23 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.Element;
24 import com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.ListConst.ElementArray;
26 import com.mysql.clusterj.core.store.Index;
27 import com.mysql.clusterj.core.store.Table;
29 import com.mysql.clusterj.core.util.I18NHelper;
30 import com.mysql.clusterj.core.util.Logger;
31 import com.mysql.clusterj.core.util.LoggerFactoryService;
36 class DictionaryImpl implements com.mysql.clusterj.core.store.Dictionary {
38 /** My message translator */
39 static final I18NHelper local = I18NHelper
40 .getInstance(DictionaryImpl.class);
43 static final Logger logger = LoggerFactoryService.getFactory()
44 .getInstance(DictionaryImpl.class);
46 private DictionaryConst ndbDictionary;
48 public DictionaryImpl(DictionaryConst ndbDictionary) {
49 this.ndbDictionary = ndbDictionary;
52 public Table getTable(String tableName) {
53 TableConst ndbTable = ndbDictionary.getTable(tableName);
54 if (ndbTable == null) {
55 // try the lower case table name
56 ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
58 if (ndbTable == null) {
61 return new TableImpl(ndbTable, getIndexNames(ndbTable.getName()));
64 public Index getIndex(String indexName, String tableName, String indexAlias) {
65 if ("PRIMARY$KEY".equals(indexName)) {
66 // create a pseudo index for the primary key hash
67 TableConst ndbTable = ndbDictionary.getTable(tableName);
68 if (ndbTable == null) {
69 // try the lower case table name
70 ndbTable = ndbDictionary.getTable(tableName.toLowerCase());
72 handleError(ndbTable, ndbDictionary, "");
73 return new IndexImpl(ndbTable);
75 IndexConst ndbIndex = ndbDictionary.getIndex(indexName, tableName);
76 if (ndbIndex == null) {
77 // try the lower case table name
78 ndbIndex = ndbDictionary.getIndex(indexName, tableName.toLowerCase());
80 handleError(ndbIndex, ndbDictionary, indexAlias);
81 return new IndexImpl(ndbIndex, indexAlias);
84 public String[] getIndexNames(String tableName) {
85 // get all indexes for this table including ordered PRIMARY
86 com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List indexList =
87 com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.create();
88 final String[] result;
90 int returnCode = ndbDictionary.listIndexes(indexList, tableName);
91 handleError(returnCode, ndbDictionary, tableName);
92 int count = indexList.count();
93 result = new String[count];
94 if (logger.isDetailEnabled()) logger.detail("Found " + count + " indexes for " + tableName);
95 ElementArray elementArray = indexList.elements();
96 for (int i = 0; i < count; ++i) {
97 Element element = elementArray.at(i);
98 handleError(element, ndbDictionary, String.valueOf(i));
99 String indexName = element.name();
100 result[i] = indexName;
103 // free the list memory even if error
104 com.mysql.ndbjtie.ndbapi.NdbDictionary.DictionaryConst.List.delete(indexList);
109 protected static void handleError(int returnCode, DictionaryConst ndbDictionary, String extra) {
110 if (returnCode == 0) {
113 Utility.throwError(returnCode, ndbDictionary.getNdbError(), extra);
117 protected static void handleError(Object object, DictionaryConst ndbDictionary, String extra) {
118 if (object != null) {
121 Utility.throwError(null, ndbDictionary.getNdbError(), extra);