4 public class SqlException : Exception{
8 public SqlException(int err, String sqlmessage){
14 public class DatabaseNotSupportException : SqlException{
17 message =
"not support method:" + sqlmessage;
21 public interface ConnPoolCallback{
26 public class ConnectionPool{
27 static class connectorMgr{
32 List<connectorMgr> _pool =
new List<connectorMgr>();
33 Map<int, bool> _error_map =
new Map<int, bool>();
35 static const int TIMEOU_LIMIT = 300000;
45 Connection connector = callback.createConnection();
46 if (connector != nilptr){
53 Connection connector = nilptr;
57 if (_maxConnect != 0){
58 while ((_pool.size() == 0) && (_active_cnt >= _maxConnect)){
64 if (connector != nilptr){
72 List.Iterator<connectorMgr> iter = _pool.iterator();
74 connectorMgr mgr = iter.get();
75 connector = mgr.pconnection;
76 lastactive = mgr.timestamp;
82 }
while (test(connector, lastactive) ==
false);
84 if (connector == nilptr){
85 if ((_maxConnect == 0) || (_active_cnt < _maxConnect)){
86 connector = allocate();
93 bool test(Connection conn,
long lastactive){
95 return callback.alive(conn);
102 int mysql_error = connection.getErrorCode();
104 if (_error_map.containsKey(mysql_error) ==
false){
106 connectorMgr mgr =
new connectorMgr();
107 mgr.pconnection = connection;
110 if (_maxIdleTime != 0){
111 List.Iterator<connectorMgr> it = _pool.iterator();
113 while (it != nilptr && it.hasNext()){
114 List.Iterator<connectorMgr> tmp = it.getNext();
115 connectorMgr pmt = it.get();
117 if ((mgr.timestamp - pmt.timestamp) > _maxIdleTime){
118 pmt.pconnection.close();
129 while (_pool.size() > _maxIdle){
130 List.Iterator<connectorMgr> tmp = _pool.iterator();
131 connectorMgr pmt = tmp.get();
132 pmt.pconnection.close();
159 public bool config(ConnPoolCallback _callback,
String reset_error,
int maxWait,
int maxConnect,
int maxIdle,
int maxIdleTime,
int validtesttime){
161 if (maxIdle != 0 && maxConnect != 0 && maxIdle >= maxConnect){
166 if (_callback == nilptr){
170 callback = _callback;
172 _validtesttime = validtesttime;
174 if (reset_error != nilptr){
175 String err_list = reset_error;
177 for (
int i = 0; i < elist.
length; i++){
180 _error_map.put(str.
parseInt(),
true);
185 _wait_time = maxWait;
189 _maxConnect = maxConnect;
190 _maxIdleTime = maxIdleTime;
202 _maxIdleTime = TIMEOU_LIMIT;
207 public class Database{
208 static Map<String, ConnectionRegister> _database =
new Map<String, ConnectionRegister>();
211 synchronized(_database){
212 if (_database.containsKey(name) ==
false){
213 _database.put(name, reg);
221 synchronized(_database){
223 ConnectionRegister cr = _database.get(driverName);
224 return cr.allocConnection(driverName);
235 public interface ConnectionRegister{
239 public interface Connection{
268 public class ResultSet{
304 public class Statement{
314 public static class PreparedStatement : Statement{
322 textSql =
" " + sql +
" ";
323 strsplit = textSql.
split(
'?');
324 if (strsplit.
length == 0){
327 strparams =
new String[strsplit.length - 1];
331 str = str.
replace(
"\\",
"\\\\");
332 str = str.
replace(
"\"",
"\\\"");
333 str = str.
replace(
"\'",
"\\\'");
334 str = str.
replace(
"\`",
"\\\`");
335 return "'" + str +
"'";
339 return execute(getSql());
343 String sqlout = strsplit[0];
344 int i = 1, c = strsplit.
length;
346 sqlout = sqlout + (strparams[i - 1] + strsplit[i]);
352 return executeQuery(getSql());
356 return executeUpdate(getSql());
360 if (paramid < strparams.
length){
361 strparams[paramid++] = safety(value);
363 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
368 if (paramid < strparams.
length){
371 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
376 if (paramid < strparams.
length){
379 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
384 if (paramid < strparams.
length){
385 strparams[paramid++] =
String.
format(
"%02u",value & 0xff);
387 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
392 if (paramid < strparams.
length){
393 strparams[paramid++] =
"" + value;
395 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
400 if (paramid < strparams.
length){
401 strparams[paramid++] =
"" + value;
403 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
429 setValue((
double)value);
434 if ((
id > 0) && (
id - 1 < strparams.
length)){
435 strparams[
id - 1] = safety(value);
437 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
442 if ((
id > 0) && (
id - 1 < strparams.
length)){
445 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
450 if ((
id > 0) && (
id - 1 < strparams.
length)){
453 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
458 if ((
id > 0) && (
id - 1 < strparams.
length)){
461 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
466 if ((
id > 0) && (
id - 1 < strparams.
length)){
467 strparams[
id - 1] =
"" + value;
469 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
474 if ((
id > 0) && (
id - 1 < strparams.
length)){
475 strparams[
id - 1] =
"" + value;
477 throw new SqlException(-1,
"sql " +textSql +
" dont need value:" + value);
507 for (
int i = 0; i < args.length; i++){
508 if (args[i].instanceOf(
String)){
509 setValue((
String)args[i]);
511 if (args[i].instanceOf(
int)){
512 setValue((
int)args[i]);
514 if (args[i].instanceOf(
long)){
515 setValue((
long)args[i]);
517 if (args[i].instanceOf(
byte)){
518 setValue((
byte)args[i]);
520 if (args[i].instanceOf(
double)){
521 setValue((
double)args[i]);
523 throw new SqlException(-1,
"sql " +textSql +
" dont accept type:" + i);
529 throw new SqlException(-1,
"get_changes");
static final long currentTimeMillis()
void recycle(Connection connection)
ConnPoolCallback callback
bool config(ConnPoolCallback _callback, String reset_error, int maxWait, int maxConnect, int maxIdle, int maxIdleTime, int validtesttime)
Connection getConnector()
bool test(Connection conn, long lastactive)
static bool reigstry(String name, ConnectionRegister reg)
static Connection getConnection(String driverName)
DatabaseNotSupportException(String sqlmessage)
int get_changes() override
void setValue(double value)
void setValue(byte value)
void setDouble(int id, double value)
void setString(int id, String value)
void setValue(int id, String value)
PreparedStatement(String sql)
void setLong(int id, long value)
void setValue(String value)
String safety(String str)
void setValue(float value)
void setValue(int id, double value)
void setValue(int id, int value)
void setValue(int id, byte value)
void setDouble(double value)
void setValue(int id, float value)
void setInt(int id, int value)
void setByte(int id, byte value)
void setValues(Object [] args)
void setFloat(int id, float value)
void setString(String value)
void setValue(long value)
void setFloat(float value)
void setValue(int id, long value)
double getDouble(int columnIndex)
String getString(int columnIndex)
String getString(String columnlabel)
int getInt(int columnIndex)
bool getBoolean(String columnlabel)
int getInt(String columnlabel)
float getFloat(String columnlabel)
long getLong(String columnlabel)
long getLong(int columnIndex)
byte getByte(int columnIndex)
String getColumnName(int i)
byte getByte(String columnlabel)
double getDouble(String columnlabel)
float getFloat(int columnIndex)
bool getBoolean(int columnIndex)
int executeUpdate(String sql)
ResultSet executeQuery(String sql)
static final String format(String, Object[])
String replace(int, int, String)
Connection createConnection()
bool alive(Connection conn)
void setOption(int opt, Object option)
PreparedStatement prepareStatement(String sql)
Object getOption(int opt)
void create(String uri, String username, String pwd)
Statement createStatement()
Connection allocConnection(String drivername)