TSDBJNIConnector.java 8.1 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
/***************************************************************************
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/
package com.taosdata.jdbc;

import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.List;

public class TSDBJNIConnector {
	static final long INVALID_CONNECTION_POINTER_VALUE = 0l;
	static volatile Boolean isInitialized = false;

	static {
		System.loadLibrary("taos");
	}

	/**
	 * Connection pointer used in C
	 */
	private long taos = INVALID_CONNECTION_POINTER_VALUE;

	/**
	 * result set status in current connection
	 */
	private boolean isResultsetClosed = true;
	private int affectedRows = -1;

	/**
	 * Whether the connection is closed
	 */
	public boolean isClosed() {
		return this.taos == INVALID_CONNECTION_POINTER_VALUE;
	}

	/**
	 * Returns the status of last result set in current connection
	 * @return
	 */
	public boolean isResultsetClosed() {
		return this.isResultsetClosed;
	}
	
	/**
	 * Initialize static variables in JNI to optimize performance
	 */
	public static void init(String configDir, String locale, String charset, String timezone) throws SQLWarning {
		synchronized(isInitialized) {
			if (!isInitialized) {
				initImp(configDir);
				if (setOptions(0, locale) < 0) {
					throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set locale: " + locale + ". System default will be used."));
				}
                if (setOptions(1, charset) < 0) {
                    throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set charset: " + charset + ". System default will be used."));
                }
				if (setOptions(2, timezone) < 0) {
					throw new SQLWarning(TSDBConstants.WrapErrMsg("Failed to set timezone: " + timezone + ". System default will be used."));
				}
				isInitialized = true;
				TaosGlobalConfig.setCharset(getTsCharset());
			}
		}
	}

	public static native void initImp(String configDir);

	public static native int setOptions(int optionIndex, String optionValue);

    public static native String getTsCharset();

	/**
	 * Get connection pointer
	 * 
	 * @throws SQLException
	 */
	public boolean connect(String host, int port, String dbName, String user, String password) throws SQLException {
		if (this.taos != INVALID_CONNECTION_POINTER_VALUE) {
			this.closeConnectionImp(this.taos);
			this.taos = INVALID_CONNECTION_POINTER_VALUE;
		}

		this.taos = this.connectImp(host, port, dbName, user, password);
		if (this.taos == INVALID_CONNECTION_POINTER_VALUE) {
			throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg()), "", this.getErrCode());
		}

		return true;
	}

	private native long connectImp(String host, int port, String dbName, String user, String password);

	/**
	 * Execute DML/DDL operation
	 * 
	 * @throws SQLException
	 */
	public int executeQuery(String sql) throws SQLException {
        if (!this.isResultsetClosed) {
            //throw new RuntimeException(TSDBConstants.WrapErrMsg("Connection already has an open result set"));
        	long resultSetPointer = this.getResultSet();
        	if (resultSetPointer == TSDBConstants.JNI_CONNECTION_NULL) {
        		//do nothing
        	} else {
        		this.freeResultSet(resultSetPointer);
        	}
        }
        
        int code;
        try {
            code = this.executeQueryImp(sql.getBytes(TaosGlobalConfig.getCharset()), this.taos);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException(TSDBConstants.WrapErrMsg("Unsupported encoding"));
        }
		affectedRows = code;
        if (code < 0) {
            affectedRows = -1;
            if (code == TSDBConstants.JNI_TDENGINE_ERROR) {
                throw new SQLException(TSDBConstants.WrapErrMsg(this.getErrMsg()), "", this.getErrCode());
            } else {
                throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode());
            }
        }
        
        return code;
	}

	private native int executeQueryImp(byte[] sqlBytes, long connection);

	/**
	 * Get recent error code by connection
	 */
	public int getErrCode() {
		return Math.abs(this.getErrCodeImp(this.taos));
	}

	private native int getErrCodeImp(long connection);

	/**
	 * Get recent error message by connection
	 */
	public String getErrMsg() {
		return this.getErrMsgImp(this.taos);
	}

	private native String getErrMsgImp(long connection);

	/**
	 * Get resultset pointer
     * Each connection should have a single open result set at a time
	 */
	public long getResultSet() {
		long res = this.getResultSetImp(this.taos);
        return res;
	}

	private native long getResultSetImp(long connection);

	/**
	 * Free resultset operation from C to release resultset pointer by JNI
	 */
	public int freeResultSet(long result) {
		int res = this.freeResultSetImp(this.taos, result);
		this.isResultsetClosed = true; // reset resultSetPointer to 0 after freeResultSetImp() return
		return res;
	}

	private native int freeResultSetImp(long connection, long result);

	/**
	 * Get affected rows count
	 */
	public int getAffectedRows() {
	    int affectedRows = this.affectedRows;
        if (affectedRows < 0) {
            affectedRows = this.getAffectedRowsImp(this.taos);
        }
        return affectedRows;
	}

	private native int getAffectedRowsImp(long connection);

	/**
	 * Get schema metadata
	 */
	public int getSchemaMetaData(long resultSet, List<ColumnMetaData> columnMetaData) {
		return this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData);
	}

	private native int getSchemaMetaDataImp(long connection, long resultSet, List<ColumnMetaData> columnMetaData);

	/**
	 * Get one row data
	 */
	public int fetchRow(long resultSet, TSDBResultSetRowData rowData) {
		return this.fetchRowImp(this.taos, resultSet, rowData);
	}

	private native int fetchRowImp(long connection, long resultSet, TSDBResultSetRowData rowData);

	/**
	 * Execute close operation from C to release connection pointer by JNI
	 * 
	 * @throws SQLException
	 */
	public void closeConnection() throws SQLException {
		int code = this.closeConnectionImp(this.taos);
		if (code < 0) {
			throw new SQLException(TSDBConstants.FixErrMsg(code), "", this.getErrCode());
		} else if (code == 0){
			this.taos = INVALID_CONNECTION_POINTER_VALUE;
		} else {
		    throw new SQLException("Undefined error code returned by TDengine when closing a connection");
        }
	}

	private native int closeConnectionImp(long connection);

	/**
	 * Subscribe to a table in TSDB
	 */
	public long subscribe(String host, String user, String password, String database, String table, long time, int period){
		return subscribeImp(host, user, password, database, table, time, period);
	}

	private native long subscribeImp(String host, String user, String password, String database, String table, long time, int period);

	/**
	 * Consume a subscribed table
	 */
	public TSDBResultSetRowData consume(long subscription) {
		return this.consumeImp(subscription);
	}

	private native TSDBResultSetRowData consumeImp(long subscription);

	/**
	 * Unsubscribe a table
	 * @param subscription
	 */
	public void unsubscribe(long subscription) {
		unsubscribeImp(subscription);
	}

	private native void unsubscribeImp(long subscription);

	/**
	 * Validate if a <I>create table</I> sql statement is correct without actually creating that table
	 */
	public boolean validateCreateTableSql(String sql) {
	    long connection = taos;
		int res = validateCreateTableSqlImp(connection, sql.getBytes());
        return res != 0 ? false : true;
	}

	private native int validateCreateTableSqlImp(long connection, byte[] sqlBytes);
}