# Add #column\_types and #column\_type(index : Int32) to DB::ResultSet

**URL:** <https://forum.crystal-lang.org/t/add-column-types-and-column-type-index-int32-to-db-resultset/6370>\
**Category:** Crystal Contrib\
**Created:** [January 26, 2024, 6:10pm UTC](https://forum.crystal-lang.org/t/add-column-types-and-column-type-index-int32-to-db-resultset/6370 "2024-01-26T18:10:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trans](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/trans/32/1255_2.png) [@trans](https://forum.crystal-lang.org/u/trans)\
**Post date:** [January 26, 2024, 6:10pm UTC](https://forum.crystal-lang.org/t/add-column-types-and-column-type-index-int32-to-db-resultset/6370/1 "2024-01-26T18:10:41Z")

</div>

Hi, I am working on an app that does a lot of code generation. One part of it generates code for SQL queries. While I plan to allow the end-user to specify the types of columns explicitly, I really also need a way to have a default. But I don’t see a way to get the database column type from the ResultSet in order to decide what that default should be. At first I wondered maybe it is not be possible, but I checked the SQLite docs and it is (and in fact has to be for the API to be useful/work).

From [Result Values From A Query](https://www.sqlite.org/c3ref/column_blob.html)

> The sqlite3\_column\_type() routine returns the [datatype code](https://www.sqlite.org/c3ref/c_blob.html) for the initial data type of the result column. The returned value is one of [SQLITE\_INTEGER](https://www.sqlite.org/c3ref/c_blob.html), [SQLITE\_FLOAT](https://www.sqlite.org/c3ref/c_blob.html), [SQLITE\_TEXT](https://www.sqlite.org/c3ref/c_blob.html), [SQLITE\_BLOB](https://www.sqlite.org/c3ref/c_blob.html), or [SQLITE\_NULL](https://www.sqlite.org/c3ref/c_blob.html).

I can check, but I imagine other supported databases are going to have a way to get this info too.

So I propose adding `#column_types` and `#column_type(index : Int32)` to `DB::ResultSet`. Can we add these abstract methods, so that maintainers of the various backends can proceed to implement?

To do this, I suppose we will need a standardized Enum for the variety of possible types supported across various databases, basically [these](https://www.w3schools.com/sql/sql_datatypes.asp).

And ideally we probably should add a method for getting the size of the column too, for column types such as VARCHAR, BLOB, etc.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [January 26, 2024, 7:15pm UTC](https://forum.crystal-lang.org/t/add-column-types-and-column-type-index-int32-to-db-resultset/6370/2 "2024-01-26T19:15:08Z")

</div>

I would post an issue on the [`crystal-db` issue tracker](https://github.com/crystal-lang/crystal-db/issues) for this, but here are my initial thoughts:

> [@trans](#):
>
> I imagine other supported databases are going to have a way to get this info too.

Looks like Postgres can. It [passes the `oid` information](https://github.com/will/crystal-pg/blob/74dd9b64c3f53c772ff80e92212c4208aa5ad733/src/pg/result_set.cr#L177-L179) for each column into the `ResultSet`. `oid`s are Postgres type ids (stands for “object identifiers”), equivalent to the SQLite datatype code.

> [@trans](#):
>
> To do this, I suppose we will need a standardized Enum for the variety of possible types supported across various databases, basically [these](https://www.w3schools.com/sql/sql_datatypes.asp).

An enum won’t work here, at least not for all DB implementations because the sets of supported data types aren’t the same across all of them. Especially when you take into account types provided by DB plugins/extensions.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 27, 2024, 2:37pm UTC](https://forum.crystal-lang.org/t/add-column-types-and-column-type-index-int32-to-db-resultset/6370/3 "2024-01-27T14:37:52Z")

</div>

For reference: [Add #column\_types and #column\_type(index : Int32) to DB::ResultSet · Issue #204 · crystal-lang/crystal-db · GitHub](https://github.com/crystal-lang/crystal-db/issues/204)
