use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class AnsiSqlDialect method psForInsertBatch.
@Override
public PreparedStatement psForInsertBatch(Connection conn, Entity... entities) throws SQLException {
if (ArrayUtil.isEmpty(entities)) {
throw new DbRuntimeException("Entities for batch insert is empty !");
}
// 批量,根据第一行数据结构生成SQL占位符
final SqlBuilder insert = SqlBuilder.create(wrapper).insert(entities[0], this.dialectName());
final Set<String> fields = CollUtil.filter(entities[0].keySet(), StrUtil::isNotBlank);
return StatementUtil.prepareStatementForBatch(conn, insert.build(), fields, entities);
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class AnsiSqlDialect method psForPage.
@Override
public PreparedStatement psForPage(Connection conn, Query query) throws SQLException {
Assert.notNull(query, "query must be not null !");
if (StrUtil.hasBlank(query.getTableNames())) {
throw new DbRuntimeException("Table name must be not empty !");
}
final SqlBuilder find = SqlBuilder.create(wrapper).query(query);
return psForPage(conn, find, query.getPage());
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class AbstractDSFactory method createDataSource.
/**
* 创建数据源
*
* @param group 分组
* @return {@link DataSourceWrapper} 数据源包装
*/
private DataSourceWrapper createDataSource(String group) {
if (group == null) {
group = StrUtil.EMPTY;
}
final Setting config = setting.getSetting(group);
if (MapUtil.isEmpty(config)) {
throw new DbRuntimeException("No config for group: [{}]", group);
}
// 基本信息
final String url = config.getAndRemoveStr(KEY_ALIAS_URL);
if (StrUtil.isBlank(url)) {
throw new DbRuntimeException("No JDBC URL for group: [{}]", group);
}
// 移除用户可能误加入的show sql配置项
// issue#I3VW0R@Gitee
DbUtil.removeShowSqlParams(config);
// 自动识别Driver
String driver = config.getAndRemoveStr(KEY_ALIAS_DRIVER);
if (StrUtil.isBlank(driver)) {
driver = DriverUtil.identifyDriver(url);
}
final String user = config.getAndRemoveStr(KEY_ALIAS_USER);
final String pass = config.getAndRemoveStr(KEY_ALIAS_PASSWORD);
return DataSourceWrapper.wrap(createDataSource(url, driver, user, pass, config), driver);
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class C3p0DSFactory method createDataSource.
@Override
protected DataSource createDataSource(String jdbcUrl, String driver, String user, String pass, Setting poolSetting) {
final ComboPooledDataSource ds = new ComboPooledDataSource();
// remarks等特殊配置,since 5.3.8
final Props connProps = new Props();
String connValue;
for (String key : KEY_CONN_PROPS) {
connValue = poolSetting.getAndRemoveStr(key);
if (StrUtil.isNotBlank(connValue)) {
connProps.setProperty(key, connValue);
}
}
if (MapUtil.isNotEmpty(connProps)) {
ds.setProperties(connProps);
}
ds.setJdbcUrl(jdbcUrl);
try {
ds.setDriverClass(driver);
} catch (PropertyVetoException e) {
throw new DbRuntimeException(e);
}
ds.setUser(user);
ds.setPassword(pass);
// 注入属性
poolSetting.toBean(ds);
return ds;
}
use of cn.hutool.db.DbRuntimeException in project hutool by looly.
the class MetaUtil method getTableMeta.
/**
* 获得表的元信息<br>
* 注意如果需要获取注释,某些数据库如MySQL,需要在配置中添加:
* <pre>
* remarks = true
* useInformationSchema = true
* </pre>
*
* @param ds 数据源
* @param tableName 表名
* @param catalog catalog name,{@code null}表示自动获取,见:{@link #getCataLog(Connection)}
* @param schema a schema name pattern,{@code null}表示自动获取,见:{@link #getSchema(Connection)}
* @return Table对象
* @since 5.7.22
*/
public static Table getTableMeta(DataSource ds, String catalog, String schema, String tableName) {
final Table table = Table.create(tableName);
Connection conn = null;
try {
conn = ds.getConnection();
// catalog和schema获取失败默认使用null代替
if (null == catalog) {
catalog = getCataLog(conn);
}
table.setCatalog(catalog);
if (null == schema) {
schema = getSchema(conn);
}
table.setSchema(schema);
final DatabaseMetaData metaData = conn.getMetaData();
// 获得表元数据(表注释)
try (ResultSet rs = metaData.getTables(catalog, schema, tableName, new String[] { TableType.TABLE.value() })) {
if (null != rs) {
if (rs.next()) {
table.setComment(rs.getString("REMARKS"));
}
}
}
// 获得主键
try (ResultSet rs = metaData.getPrimaryKeys(catalog, schema, tableName)) {
if (null != rs) {
while (rs.next()) {
table.addPk(rs.getString("COLUMN_NAME"));
}
}
}
// 获得列
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
if (null != rs) {
while (rs.next()) {
table.setColumn(Column.create(table, rs));
}
}
}
} catch (SQLException e) {
throw new DbRuntimeException("Get columns error!", e);
} finally {
DbUtil.close(conn);
}
return table;
}
Aggregations