OmniSciDB  4201147b46
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HeavyAIDriver.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package ai.heavy.jdbc;
18 
19 import org.slf4j.LoggerFactory;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.DriverPropertyInfo;
26 import java.sql.SQLException;
27 import java.util.Properties;
28 import java.util.logging.Logger;
29 
30 public class HeavyAIDriver implements java.sql.Driver {
31  static int DriverMajorVersion = -1;
32  static int DriverMinorVersion = -1;
33  static String DriverVersion = "UNKNOWN";
34  final static String VERSION_FILE = "version.properties";
35  final static org.slf4j.Logger logger = LoggerFactory.getLogger(HeavyAIDriver.class);
36  public static final String OMNISCI_PREFIX = "jdbc:omnisci:";
37  public static final String MAPD_PREFIX = "jdbc:mapd:";
38  public static final String HEAVYAI_PREFIX = "jdbc:heavyai:";
39 
40  static {
41  try {
42  DriverManager.registerDriver(new HeavyAIDriver());
43  } catch (SQLException e) {
44  e.printStackTrace();
45  }
46 
47  try (InputStream input = HeavyAIDriver.class.getClassLoader().getResourceAsStream(
48  VERSION_FILE)) {
49  Properties prop = new Properties();
50  if (input == null) {
51  logger.error("Cannot read " + VERSION_FILE + " file");
52  } else {
53  prop.load(input);
54  DriverVersion = prop.getProperty("version");
55  String[] version = DriverVersion.split("\\.");
56  try {
57  DriverMajorVersion = Integer.parseInt(version[0]);
58  DriverMinorVersion = Integer.parseInt(version[1]);
59  } catch (NumberFormatException ex) {
60  logger.error("Unexpected driver version format in " + VERSION_FILE);
61  DriverVersion = "UNKNOWN";
62  }
63  }
64  } catch (IOException e) {
65  e.printStackTrace();
66  }
67  }
68 
69  @Override
70  public Connection connect(String url, Properties info)
71  throws SQLException { // logger.debug("Entered");
72  if (!isValidURL(url)) {
73  return null;
74  }
75 
76  url = url.trim();
77  return new HeavyAIConnection(url, info);
78  }
79 
80  @Override
81  public boolean acceptsURL(String url) throws SQLException { // logger.debug("Entered");
82  return isValidURL(url);
83  }
84 
91  private static boolean isValidURL(String url) {
92  return url != null
93  && (url.toLowerCase().startsWith(OMNISCI_PREFIX)
94  || url.toLowerCase().startsWith(MAPD_PREFIX)
95  || url.toLowerCase().startsWith(HEAVYAI_PREFIX));
96  }
97 
98  @Override
99  public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
100  throws SQLException { // logger.debug("Entered");
101  return null;
102  }
103 
104  @Override
105  public int getMajorVersion() {
106  return DriverMajorVersion;
107  }
108 
109  @Override
110  public int getMinorVersion() {
111  return DriverMinorVersion;
112  }
113 
114  @Override
115  public boolean jdbcCompliant() {
116  return false;
117  }
118 
119  @Override
120  public Logger getParentLogger() {
121  return null;
122  }
123 }
static final org.slf4j.Logger logger
static final String MAPD_PREFIX
boolean acceptsURL(String url)
static final String VERSION_FILE
DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
Connection connect(String url, Properties info)
string version
Definition: setup.in.py:73
static boolean isValidURL(String url)
static final String HEAVYAI_PREFIX
static final String OMNISCI_PREFIX