Merge pull request #159 from wenshao/master

支持SqlLite
This commit is contained in:
温高铁
2012-09-28 18:25:23 -07:00
5 changed files with 198 additions and 10 deletions
@@ -39,6 +39,8 @@ import java.util.Calendar;
import com.alibaba.druid.pool.PreparedStatementPool.MethodType;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.OracleUtils;
/**
@@ -46,6 +48,8 @@ import com.alibaba.druid.util.OracleUtils;
*/
public class DruidPooledPreparedStatement extends DruidPooledStatement implements PreparedStatement {
private final static Log LOG = LogFactory.getLog(DruidPooledPreparedStatement.class);
private final PreparedStatementHolder holder;
private final PreparedStatement stmt;
private final String sql;
@@ -68,11 +72,36 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
this.sql = holder.getKey().sql;
// Remember the defaults
defaultMaxFieldSize = stmt.getMaxFieldSize();
defaultMaxRows = stmt.getMaxRows();
defaultQueryTimeout = stmt.getQueryTimeout();
defaultFetchDirection = stmt.getFetchDirection();
defaultFetchSize = stmt.getFetchSize();
try {
defaultMaxFieldSize = stmt.getMaxFieldSize();
} catch (SQLException e) {
LOG.error("getMaxFieldSize error", e);
}
try {
defaultMaxRows = stmt.getMaxRows();
} catch (SQLException e) {
LOG.error("getMaxRows error", e);
}
try {
defaultQueryTimeout = stmt.getQueryTimeout();
} catch (SQLException e) {
LOG.error("getMaxRows error", e);
}
try {
defaultFetchDirection = stmt.getFetchDirection();
} catch (SQLException e) {
LOG.error("getFetchDirection error", e);
}
try {
defaultFetchSize = stmt.getFetchSize();
} catch (SQLException e) {
LOG.error("getFetchSize error", e);
}
currentMaxFieldSize = defaultMaxFieldSize;
currentMaxRows = defaultMaxRows;
currentQueryTimeout = defaultQueryTimeout;
@@ -180,7 +209,7 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
conn.beforeExecute();
try {
ResultSet rs = stmt.executeQuery();
if (rs == null) {
return null;
}
@@ -461,7 +490,7 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
if (holder.getHitCount() == 0) {
return;
}
int fetchRowPeak = holder.getFetchRowPeak();
if (fetchRowPeak < 0) {
@@ -1004,7 +1033,7 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
}
}
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface == PreparedStatement.class) {
@@ -1013,11 +1042,11 @@ public class DruidPooledPreparedStatement extends DruidPooledStatement implement
}
return (T) stmt;
}
if (iface == PreparedStatementHolder.class) {
return (T) this.holder;
}
return super.unwrap(iface);
}
}
@@ -137,6 +137,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
private String handleName(String ident) {
ident = ident.replaceAll("\"", "");
ident = ident.replaceAll("`", "");
ident = ident.replaceAll(" ", "");
ident = aliasWrap(ident);
@@ -0,0 +1,42 @@
package com.alibaba.druid.support.profile;
public class ProfileEntry {
private final Key key;
private final String type;
public ProfileEntry(Key key, String type){
super();
this.key = key;
this.type = type;
}
public Key getKey() {
return key;
}
public String getType() {
return type;
}
public static class Key {
private String parent;
private String name;
public Key(String parentName, String name){
super();
this.parent = parentName;
this.name = name;
}
public String getParent() {
return parent;
}
public String getName() {
return name;
}
}
}
@@ -0,0 +1,59 @@
package com.alibaba.druid.support.profile;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.druid.support.profile.ProfileEntry.Key;
public class Profiler {
public static ThreadLocal<Boolean> enableLocal = new ThreadLocal<Boolean>();
private static ThreadLocal<Map<ProfileEntry.Key, ProfileEntry>> entriesLocal = new ThreadLocal<Map<Key, ProfileEntry>>();
private final static ThreadLocal<ProfileEntry> currentLocal = new ThreadLocal<ProfileEntry>();
public static void setThreadLocalEnable() {
enableLocal.set(true);
}
public static void setThreadLocalDisable() {
enableLocal.remove();
}
public static boolean isEnable() {
return Boolean.TRUE == enableLocal.get();
}
public static void enter(String name, String type, long startNano) {
ProfileEntry parent = currentLocal.get();
String parentName = null;
if (parent != null) {
parentName = parent.getKey().getName();
}
ProfileEntry.Key key = new ProfileEntry.Key(parentName, name);
Map<ProfileEntry.Key, ProfileEntry> entries = entriesLocal.get();
if (entries == null) {
entries = new HashMap<ProfileEntry.Key, ProfileEntry>();
entriesLocal.set(entries);
}
ProfileEntry entry = entries.get(key);
if (entry == null) {
entry = new ProfileEntry(key, type);
entries.put(key, entry);
}
currentLocal.set(entry);
}
public static ProfileEntry current() {
return currentLocal.get();
}
public static void release(long nanoSpan) {
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
public class MySqlUpdateTest_0 extends MysqlTest {
public void test_0() throws Exception {
String sql = "UPDATE `z_code` SET `key`='INTRANT_NOTALLOWED_CATEGORY_C'";
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("z_code")));
Assert.assertTrue(visitor.getColumns().contains(new Column("z_code", "key")));
}
}