Search in sources :

Example 16 with DbRuntimeException

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);
}
Also used : StrUtil(cn.hutool.core.util.StrUtil) SqlBuilder(cn.hutool.db.sql.SqlBuilder) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 17 with DbRuntimeException

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());
}
Also used : SqlBuilder(cn.hutool.db.sql.SqlBuilder) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 18 with DbRuntimeException

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);
}
Also used : Setting(cn.hutool.setting.Setting) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 19 with DbRuntimeException

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;
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) Props(cn.hutool.setting.dialect.Props) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Example 20 with DbRuntimeException

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;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) DatabaseMetaData(java.sql.DatabaseMetaData) DbRuntimeException(cn.hutool.db.DbRuntimeException)

Aggregations

DbRuntimeException (cn.hutool.db.DbRuntimeException)23 Setting (cn.hutool.setting.Setting)8 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 ResultSet (java.sql.ResultSet)3 ArrayList (java.util.ArrayList)3 NotInitedException (cn.hutool.core.exceptions.NotInitedException)2 SqlBuilder (cn.hutool.db.sql.SqlBuilder)2 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 MongoClient (com.mongodb.MongoClient)2 MongoCredential (com.mongodb.MongoCredential)2 Blob (java.sql.Blob)2 Properties (java.util.Properties)2 StrUtil (cn.hutool.core.util.StrUtil)1 DataSourceWrapper (cn.hutool.db.ds.DataSourceWrapper)1 SimpleDataSource (cn.hutool.db.ds.simple.SimpleDataSource)1 Props (cn.hutool.setting.dialect.Props)1 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)1 ServerAddress (com.mongodb.ServerAddress)1