Skip to content

Commit

Permalink
Redesigned the JDBC API.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 22, 2016
1 parent a8cf476 commit ec1291a
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 300 deletions.
Expand Up @@ -39,13 +39,9 @@ public C3P0ConnectionPool(String jdbcUrl, String driverClass, String username, S
init(jdbcUrl, driverClass, username, password);
}

public C3P0ConnectionPool(String jdbcUrl, String driverClass) {
this(jdbcUrl, driverClass, null, null);
}

public C3P0ConnectionPool(SQLAPI sqlapi) {
this(sqlapi.url(), sqlapi.driver(), sqlapi.user(), sqlapi.password());
sqlapi.connectionPool(this);
public C3P0ConnectionPool(JdbcClient jdbc) {
this(jdbc.url(), jdbc.driver(), jdbc.username(), jdbc.password());
jdbc.pool(this);
}

private void init(String jdbcUrl, String driverClass, String username, String password) {
Expand Down
152 changes: 152 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/sql/JDBC.java
@@ -0,0 +1,152 @@
package org.rapidoid.sql;

/*
* #%L
* rapidoid-commons
* %%
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors
* %%
* 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.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.u.U;

import java.sql.*;
import java.util.List;
import java.util.Map;

@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
public class JDBC {

private static volatile JdbcClient DEFAULT;

public static JdbcClient newApi() {
return new JdbcClient();
}

public static synchronized JdbcClient setup(String url, String driver, String username, String password) {
DEFAULT = new JdbcClient();

DEFAULT.url(url);
DEFAULT.driver(driver);
DEFAULT.username(username);
DEFAULT.password(password);

return DEFAULT;
}

public static synchronized JdbcClient defaultApi() {
if (DEFAULT == null) {
String url = JDBCConfig.url();
String driver = JDBCConfig.driver();
String username = JDBCConfig.username();
String password = JDBCConfig.password();

DEFAULT = setup(url, driver, username, password);
}

return DEFAULT;
}

public static JdbcClient username(String username) {
return defaultApi().username(username);
}

public static JdbcClient password(String password) {
return defaultApi().password(password);
}

public static JdbcClient driver(String driver) {
return defaultApi().driver(driver);
}

public static JdbcClient url(String url) {
return defaultApi().url(url);
}

public static JdbcClient mysql(String host, int port, String databaseName) {
return defaultApi().mysql(host, port, databaseName);
}

public static JdbcClient h2(String databaseName) {
return defaultApi().h2(databaseName);
}

public static JdbcClient hsql(String databaseName) {
return defaultApi().hsql(databaseName);
}

public static void execute(String sql, Object... args) {
defaultApi().execute(sql, args);
}

public static void tryToExecute(String sql, Object... args) {
defaultApi().tryToExecute(sql, args);
}

public static <T> List<Map<String, Object>> query(String sql, Object... args) {
return defaultApi().query(sql, args);
}

public static Connection getConnection() {
return defaultApi().getConnection();
}

public static void release(Connection connection) {
defaultApi().release(connection);
}

public static PreparedStatement prepare(Connection conn, String sql, Object... args) {
try {
PreparedStatement stmt = conn.prepareStatement(sql);

for (int i = 0; i < args.length; i++) {
Object arg = args[i];
stmt.setObject(i + 1, arg);
}

return stmt;
} catch (SQLException e) {
throw new RuntimeException("Cannot create prepared statement!", e);
}
}

public static List<Map<String, Object>> rows(ResultSet rs) throws SQLException {
List<Map<String, Object>> rows = U.list();

while (rs.next()) {
rows.add(row(rs));
}

return rows;
}

public static Map<String, Object> row(ResultSet rs) throws SQLException {
Map<String, Object> row = U.map();

ResultSetMetaData meta = rs.getMetaData();
int columnsNumber = meta.getColumnCount();

for (int i = 1; i <= columnsNumber; i++) {
Object obj = rs.getObject(i);
row.put(meta.getColumnLabel(i), obj);
}

return row;
}

}

0 comments on commit ec1291a

Please sign in to comment.