View Javadoc

1   /*
2    * Copyright (c) 2010.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.jdbc;
22  
23  import org.apache.hadoop.hbase.HBaseConfiguration;
24  import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
25  import org.apache.hadoop.hbase.hbql.util.Maps;
26  import org.apache.hadoop.hbase.jdbc.impl.ConnectionImpl;
27  
28  import java.sql.Connection;
29  import java.sql.DriverManager;
30  import java.sql.DriverPropertyInfo;
31  import java.sql.SQLException;
32  import java.util.Map;
33  import java.util.Properties;
34  
35  public class Driver implements java.sql.Driver {
36  
37      static {
38          try {
39              DriverManager.registerDriver(new Driver());
40          }
41          catch (SQLException e) {
42              e.printStackTrace();
43          }
44      }
45  
46      private static Map<String, String> getArgMap(final String str) throws SQLException {
47          final Map<String, String> retval = Maps.newHashMap();
48  
49          for (final String arg : str.split(";")) {
50              if (arg.contains("=")) {
51                  final String[] vals = arg.split("=");
52                  final String var = vals[0].toLowerCase();
53  
54                  if (!var.equals(HConnectionImpl.MAXTABLEREFS)
55                      && !var.equals(HConnectionImpl.MASTER))
56                      throw new SQLException("Unknown JDBC URL option: " + var);
57  
58                  retval.put(var, vals[1]);
59              }
60          }
61          return retval;
62      }
63  
64      public Connection connect(final String url, final Properties properties) throws SQLException {
65  
66          final Map<String, String> argMap = getArgMap(url);
67  
68          final HBaseConfiguration config;
69          if (argMap.containsKey(HConnectionImpl.MASTER))
70              config = HConnectionImpl.getHBaseConfiguration(argMap.get(HConnectionImpl.MASTER));
71          else
72              config = null;
73  
74          return getConnection(url, config);
75      }
76  
77      public static Connection getConnection(final String url, final HBaseConfiguration config) throws SQLException {
78          if (!validURL(url))
79              return null;
80  
81          final Map<String, String> argMap = getArgMap(url);
82  
83          final int maxtablerefs = argMap.containsKey(HConnectionImpl.MAXTABLEREFS)
84                                   ? Integer.parseInt(argMap.get(HConnectionImpl.MAXTABLEREFS)) : Integer.MAX_VALUE;
85          return new ConnectionImpl(config, maxtablerefs);
86      }
87  
88      public boolean acceptsURL(final String url) throws SQLException {
89          return validURL(url);
90      }
91  
92      private static boolean validURL(final String url) throws SQLException {
93          return (url != null && url.toLowerCase().startsWith("jdbc:hbql"));
94      }
95  
96      public DriverPropertyInfo[] getPropertyInfo(final String s, final Properties properties) throws SQLException {
97          return new DriverPropertyInfo[0];
98      }
99  
100     public int getMajorVersion() {
101         return 1;
102     }
103 
104     public int getMinorVersion() {
105         return 0;
106     }
107 
108     public boolean jdbcCompliant() {
109         return false;
110     }
111 }