TableBuilder 类

class TableBuilder {

private static final Map<String, Class<?>> strToType = new HashMap<String, Class<?>>() {
	private static final long serialVersionUID = -8651755311062618532L;
	{

		// varchar, char, enum, set, text, tinytext, mediumtext, longtext
		put("java.lang.String", java.lang.String.class);

		// int, integer, tinyint, smallint, mediumint
		put("java.lang.Integer", java.lang.Integer.class);

		// bigint
		put("java.lang.Long", java.lang.Long.class);

		// java.util.Data can not be returned
		// java.sql.Date, java.sql.Time, java.sql.Timestamp all extends
		// java.util.Data so getDate can return the three types data
		// put("java.util.Date", java.util.Date.class);

		// date, year
		put("java.sql.Date", java.sql.Date.class);

		// real, double
		put("java.lang.Double", java.lang.Double.class);

		// float
		put("java.lang.Float", java.lang.Float.class);

		// bit
		put("java.lang.Boolean", java.lang.Boolean.class);

		// time
		put("java.sql.Time", java.sql.Time.class);

		// timestamp, datetime
		put("java.sql.Timestamp", java.sql.Timestamp.class);

		// decimal, numeric
		put("java.math.BigDecimal", java.math.BigDecimal.class);

		// binary, varbinary, tinyblob, blob, mediumblob, longblob
		// qjd project: print_info.content varbinary(61800);
		put("[B", byte[].class);
	}
};

static void build(List<Table> tableList, Config config) {
	Table temp = null;
	Connection conn = null;
	try {
		conn = config.dataSource.getConnection();
		TableMapping tableMapping = TableMapping.me();
		for (Table table : tableList) {
			temp = table;
			doBuild(table, conn, config);
			tableMapping.putTable(table);
			DbKit.addModelToConfigMapping(table.getModelClass(), config);
		}
	} catch (Exception e) {
		if (temp != null)
			System.err
					.println("Can not create Table object, maybe the table "
							+ temp.getName() + " is not exists.");
		throw new ActiveRecordException(e);
	} finally {
		config.close(conn);
	}
}

@SuppressWarnings("unchecked")
private static void doBuild(Table table, Connection conn, Config config)
		throws SQLException {
	table.setColumnTypeMap(config.containerFactory.getAttrsMap());
	if (table.getPrimaryKey() == null)
		table.setPrimaryKey(config.dialect.getDefaultPrimaryKey());

sql = "select * from tableName where 1 = 2"(通过此sql可以获取到表的列名和列数据类型)

	String sql = config.dialect.forTableBuilderDoBuild(table.getName());
	Statement stm = conn.createStatement();
	ResultSet rs = stm.executeQuery(sql);
	ResultSetMetaData rsmd = rs.getMetaData();

	for (int i = 1; i <= rsmd.getColumnCount(); i++) {

获取表的列名和数据类型

		String colName = rsmd.getColumnName(i);
		String colClassName = rsmd.getColumnClassName(i);

		Class<?> clazz = strToType.get(colClassName);
		if (clazz != null) {
			table.setColumnType(colName, clazz);
		} else {
			int type = rsmd.getColumnType(i);
			if (type == Types.BLOB) {
				table.setColumnType(colName, byte[].class);
			} else if (type == Types.CLOB || type == Types.NCLOB) {
				table.setColumnType(colName, String.class);
			} else {
				table.setColumnType(colName, String.class);
			}
			// core.TypeConverter
			// throw new
			// RuntimeException("You've got new type to mapping. Please add code in "
			// + TableBuilder.class.getName() +
			// ". The ColumnClassName can't be mapped: " + colClassName);
		}
	}

	rs.close();
	stm.close();
}

}

转载于:https://my.oschina.net/u/1258399/blog/786775

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Status TableBuilder::Finish() { Rep* r = rep_; Flush(); assert(!r->closed); r->closed = true; BlockHandle filter_block_handle, metaindex_block_handle, index_block_handle; // Write filter block if (ok() && r->filter_block != nullptr) { WriteRawBlock(r->filter_block->Finish(), kNoCompression, &filter_block_handle); } // Write metaindex block if (ok()) { BlockBuilder meta_index_block(&r->options); if (r->filter_block != nullptr) { // Add mapping from "filter.Name" to location of filter data std::string key = "filter."; key.append(r->options.filter_policy->Name()); std::string handle_encoding; filter_block_handle.EncodeTo(&handle_encoding); meta_index_block.Add(key, handle_encoding); } // TODO(postrelease): Add stats and other meta blocks WriteBlock(&meta_index_block, &metaindex_block_handle); } // Write index block if (ok()) { if (r->pending_index_entry) { r->options.comparator->FindShortSuccessor(&r->last_key); std::string handle_encoding; r->pending_handle.EncodeTo(&handle_encoding); r->index_block.Add(r->last_key, Slice(handle_encoding)); r->pending_index_entry = false; } WriteBlock(&r->index_block, &index_block_handle); } // Write footer if (ok()) { Footer footer; footer.set_metaindex_handle(metaindex_block_handle); footer.set_index_handle(index_block_handle); std::string footer_encoding; footer.EncodeTo(&footer_encoding); r->status = r->file->Append(footer_encoding); if (r->status.ok()) { r->offset += footer_encoding.size(); } } return r->status; }在这段代码里 GenerateFiler()函数被调用了吗
最新发布
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值