[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-cafe
Subject:    [Haskell-cafe] Adding support for user defined data types to cassandra-cql
From:       Cody Goodman <codygman.consulting () gmail ! com>
Date:       2017-05-26 6:24:45
Message-ID: CADq_+R3-iQZb5B3x5Z6-4jTi-_QRQ1ZEp=EONbF8vbDzasqhQA () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello all,

I wrote some template Haskell code that looks up data in a cassandra
database in the system.schema_columns table using this library:
https://github.com/the-real-blackh/cassandra-cql/blob/master/Database/Cassandra/CQL.hs

Currently cassandra-cql lets you make queries like this:

getColFamInfo :: Query Rows () (Text, Text, Text, Text, Text)
getColFamInfo = "select keyspace_name, columnfamily_name, column_name,
type, validator from system.schema_columns"

And then it returns type (Text,Text,Text,Text,Text).

I'd prefer to be able to write a query like:

data MyRecord = MyRecord { a :: Text, b :: Text, c :: Text, d :: Text }

getColFamInfo :: Query Rows () (Maybe MyRecord)
getColFamInfo = "select keyspace_name, columnfamily_name, column_name,
type, validator from system.schema_columns"


Point being: I have types being generated from cassandra, but I have no
idea where I'd start modifying the cassandra-cql library to use those types.

Could anyone offer any direction? Also...

Below is a snippet of how Query is implemented, but you can find the full
code here:
https://github.com/the-real-blackh/cassandra-cql/blob/master/Database/Cassandra/CQL.hs

Query Looks like:

{ - START CODE -}

-- | The first type argument for Query. Tells us what kind of query it is.
data Style = Schema   -- ^ A query that modifies the schema, such as DROP
TABLE or CREATE TABLE
           | Write    -- ^ A query that writes data, such as an INSERT or
UPDATE
           | Rows     -- ^ A query that returns a list of rows, such as
SELECT

-- | The text of a CQL query, along with type parameters to make the query
type safe.
-- The type arguments are 'Style', followed by input and output column
types for the
-- query each represented as a tuple.
--
-- The /DataKinds/ language extension is required for 'Style'.
data Query :: Style -> * -> * -> * where
    Query :: QueryID -> Text -> Query style i o
    deriving Show

queryText :: Query s i o -> Text
queryText (Query _ txt) = txt

instance IsString (Query style i o) where
    fromString = query . T.pack

-- | Construct a query. Another way to construct one is as an overloaded
string through
-- the 'IsString' instance if you turn on the /OverloadedStrings/ language
extension, e.g.
--
-- > {-# LANGUAGE OverloadedStrings #-}
-- > ...
-- >
-- > getOneSong :: Query Rows UUID (Text, Text, Maybe Text)
-- > getOneSong = "select title, artist, comment from songs where id=?"
query :: Text -> Query style i o
query cql = Query (QueryID . hash . T.encodeUtf8 $ cql) cql
{ - END CODE -}


Thanks,

Cody Goodman

[Attachment #5 (text/html)]

<div dir="ltr"><div><div><div><div><div>Hello all,<br><br></div>I wrote some template \
Haskell code that looks up data in a cassandra database in the system.schema_columns \
table using this library: <a \
href="https://github.com/the-real-blackh/cassandra-cql/blob/master/Database/Cassandra/ \
CQL.hs">https://github.com/the-real-blackh/cassandra-cql/blob/master/Database/Cassandra/CQL.hs</a><br><br></div>Currently \
cassandra-cql lets you make queries like this:<br><br>getColFamInfo :: Query Rows () \
(Text, Text, Text, Text, Text)<br>getColFamInfo = &quot;select keyspace_name, \
columnfamily_name, column_name, type, validator from \
system.schema_columns&quot;<br><br></div>And then it returns type \
(Text,Text,Text,Text,Text).<br><br></div><div>I&#39;d prefer to be able to write a \
query like:<br><br></div><div>data MyRecord = MyRecord { a :: Text, b :: Text, c :: \
Text, d :: Text }<br><br>getColFamInfo :: Query Rows () (Maybe \
MyRecord)<br>getColFamInfo = &quot;select keyspace_name, columnfamily_name, \
column_name, type, validator from \
system.schema_columns&quot;<br><br><br></div><div>Point being: I have types being \
generated from cassandra, but I have no idea where I&#39;d start modifying the \
cassandra-cql library to use those types.<br><br></div><div>Could anyone offer any \
direction? Also...<br><br></div><div>Below is a snippet of how Query is implemented, \
but you can find the full code here: <a \
href="https://github.com/the-real-blackh/cassandra-cql/blob/master/Database/Cassandra/ \
CQL.hs">https://github.com/the-real-blackh/cassandra-cql/blob/master/Database/Cassandra/CQL.hs</a></div><div><br></div>Query \
Looks like:<br><br></div>{ - START CODE -}<br><div><br>-- | The first type argument \
for Query. Tells us what kind of query it is.<br>data Style = Schema     -- ^ A query \
that modifies the schema, such as DROP TABLE or CREATE TABLE<br>                     \
| Write       -- ^ A query that writes data, such as an INSERT or UPDATE<br>          \
| Rows         -- ^ A query that returns a list of rows, such as SELECT<br><br>-- | \
The text of a CQL query, along with type parameters to make the query type \
safe.<br>-- The type arguments are &#39;Style&#39;, followed by input and output \
column types for the<br>-- query each represented as a tuple.<br>--<br>-- The \
/DataKinds/ language extension is required for &#39;Style&#39;.<br>data Query :: \
Style -&gt; * -&gt; * -&gt; * where<br>       Query :: QueryID -&gt; Text -&gt; Query \
style i o<br>       deriving Show<br><br>queryText :: Query s i o -&gt; \
Text<br>queryText (Query _ txt) = txt<br><br>instance IsString (Query style i o) \
where<br>       fromString = query . T.pack<br><br>-- | Construct a query. Another \
way to construct one is as an overloaded string through<br>-- the &#39;IsString&#39; \
instance if you turn on the /OverloadedStrings/ language extension, e.g.<br>--<br>-- \
&gt; {-# LANGUAGE OverloadedStrings #-}<br>-- &gt; ...<br>-- &gt;<br>-- &gt; \
getOneSong :: Query Rows UUID (Text, Text, Maybe Text)<br>-- &gt; getOneSong = \
&quot;select title, artist, comment from songs where id=?&quot;<br>query :: Text \
-&gt; Query style i o<br>query cql = Query (QueryID . hash . T.encodeUtf8 $ cql) \
cql<br>{ - END CODE -}<br><br><br></div><div>Thanks,<br><br></div><div>Cody \
Goodman<br></div></div>


[Attachment #6 (text/plain)]

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic