Next: Introduction, Previous: (dir), Up: (dir) [Contents][Index]
This is the datafly Reference Manual, version 0.1, generated automatically by Declt version 3.0 "Montgomery Scott" on Sun May 15 04:35:02 2022 GMT+0.
• Introduction | What datafly is all about | |
• Systems | The systems documentation | |
• Modules | The modules documentation | |
• Files | The files documentation | |
• Packages | The packages documentation | |
• Definitions | The symbols documentation | |
• Indexes | Concepts, functions, variables and data types |
Datafly is a lightweight database library for Common Lisp.
Datafly provides 3 functions which wrap CL-DBI — retrieve-one
, retrieve-all
and execute
.
They take a SxQL statement.
(use-package :sxql)
(connect-toplevel :sqlite3 :database-name #P"myapp.db")
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot"))))
;=> (:ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
(retrieve-all
(select :*
(from :user)))
;=> ((:ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
; (:ID 2 :NAME "m2ym" :EMAIL "m2ym@example.com" :REGISTERED-AT "2014-04-15T01:03:42"))
(execute
(insert-into :tweet
(set= :id 1
:user_id 1
:body "Hi."
:created_at (princ-to-string *now*))))
If you specify :as
option with a class name to retrieval functions, they create instances of the class for each rows.
(defstruct user
id
name
email
registered-at)
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as 'user)
;=> #S(USER :ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
(retrieve-all
(select :*
(from :user))
:as 'user)
;=> (#S(USER :ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
; #S(USER :ID 2 :NAME "m2ym" :EMAIL "m2ym@example.com" :REGISTERED-AT "2014-04-15T01:03:42"))
The structure doesn't require having slots same as an existing table's in a database. It is acceptable even if the names are different. This might be convenient when you'd like to treat a JOINed table result as a structure object.
Datafly provides a macro defmodel
which defines a flavored structure class.
(defmodel (user (:inflate registered-at #'datetime-to-timestamp))
id
name
email
registered-at)
;; Same as the above.
(syntax:use-syntax :annot)
@model
(defstruct (user (:inflate registered-at #'datetime-to-timestamp))
id
name
email
registered-at)
(:inflate <columns> <inflation-function>)
options mean inflation-function
will be applied to each <columns>
when creating an instance.
(defvar *user*
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as 'user))
;; Returns a local-time:timestamp.
(user-registered-at *user*)
;=> @2014-04-15T04:20:13.000000+09:00
defmodel
also allows :has-a
and :has-many
options.
(use-package :sxql)
(defmodel (user (:inflate registered-at #'datetime-to-timestamp)
(:has-a config (where (:= :user_id id)))
(:has-many (tweets tweet)
(select :*
(from :tweet)
(where (:= :user_id id))
(order-by (:desc :created_at)))))
id
name
email
registered-at)
(defvar *user*
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as 'user))
(user-config *user*)
;=> #S(CONFIG :ID 4 :USER-ID 1 :TIMEZONE "JST" :COUNTRY "jp" :LANGUAGE "ja")
(user-tweets *user*)
;=> (#S(TWEET :ID 2 :USER-ID 1 :BODY "Is it working?" :CREATED-AT @2014-04-16T11:02:31.000000+09:00)
; #S(TWEET :ID 1 :USER-ID 1 :BODY "Hi." :CREATED-AT @2014-04-15T18:58:20.000000+09:00))
tinyint-to-boolean
datetime-to-timestamp
unixtime-to-timestamp
string-to-keyword
octet-vector-to-string
retrieve-one
and retrieve-all
return row(s) as a property list or a list of property lists by default.
If you'd like they were other types, for example "Association List" or "Hash Table", you can do it by passing :as
parameter.
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as 'trivial-types:association-list)
;=> ((:ID . 1) (:NAME . "nitro_idiot") (:EMAIL . "nitro_idiot@example.com") (:REGISTERED-AT . "2014-04-14T19:20:13"))
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as 'hash-table)
;=> #<HASH-TABLE :TEST EQL :COUNT 4 {1007AE3CD3}>
If no :as
parameter is specified, *default-row-type*
will be used.
(let ((*default-row-type* 'hash-table))
(retrieve-all
(select :*
(from :user))))
;=> (#<HASH-TABLE :TEST EQL :COUNT 4 {100815FA03}> #<HASH-TABLE :TEST EQL :COUNT 4 {100815FE43}>)
Copyright (c) 2014 Eitaro Fukamachi (e.arrows@gmail.com)
Licensed under the BSD 3-Clause License.
Next: Modules, Previous: Introduction, Up: Top [Contents][Index]
The main system appears first, followed by any subsystem dependency.
• The datafly system |
Eitaro Fukamachi
BSD 3-Clause
Lightweight database library.
# datafly
[](https://travis-ci.org/fukamachi/datafly)
Datafly is a lightweight database library for Common Lisp.
## Quickstart
Datafly provides 3 functions which wrap [CL-DBI](https://github.com/fukamachi/cl-dbi) — ‘retrieve-one‘, ‘retrieve-all‘ and ‘execute‘.
They take a [SxQL](https://github.com/fukamachi/sxql) statement.
“‘common-lisp
(use-package :sxql)
(connect-toplevel :sqlite3 :database-name #P"myapp.db")
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot"))))
;=> (:ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
(retrieve-all
(select :*
(from :user)))
;=> ((:ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
; (:ID 2 :NAME "m2ym" :EMAIL "m2ym@example.com" :REGISTERED-AT "2014-04-15T01:03:42"))
(execute
(insert-into :tweet
(set= :id 1
:user_id 1
:body "Hi."
:created_at (princ-to-string *now*))))
“‘
If you specify ‘:as‘ option with a class name to retrieval functions, they create instances of the class for each rows.
“‘common-lisp
(defstruct user
id
name
email
registered-at)
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as ’user)
;=> #S(USER :ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
(retrieve-all
(select :*
(from :user))
:as ’user)
;=> (#S(USER :ID 1 :NAME "nitro_idiot" :EMAIL "nitro_idiot@example.com" :REGISTERED-AT "2014-04-14T19:20:13")
; #S(USER :ID 2 :NAME "m2ym" :EMAIL "m2ym@example.com" :REGISTERED-AT "2014-04-15T01:03:42"))
“‘
The structure doesn’t require having slots same as an existing table’s in a database. It is acceptable even if the names are different. This might be convenient when you’d like to treat a JOINed table result as a structure object.
## Model Definitions
Datafly provides a macro ‘defmodel‘ which defines a flavored structure class.
“‘common-lisp
(defmodel (user (:inflate registered-at #’datetime-to-timestamp))
id
name
email
registered-at)
;; Same as the above.
(syntax:use-syntax :annot)
@model
(defstruct (user (:inflate registered-at #’datetime-to-timestamp))
id
name
email
registered-at)
“‘
‘(:inflate <columns> <inflation-function>)‘ options mean ‘inflation-function‘ will be applied to each ‘<columns>‘ when creating an instance.
“‘common-lisp
(defvar *user*
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as ’user))
;; Returns a local-time:timestamp.
(user-registered-at *user*)
;=> @2014-04-15T04:20:13.000000+09:00
“‘
‘defmodel‘ also allows ‘:has-a‘ and ‘:has-many‘ options.
“‘common-lisp
(use-package :sxql)
(defmodel (user (:inflate registered-at #’datetime-to-timestamp)
(:has-a config (where (:= :user_id id)))
(:has-many (tweets tweet)
(select :*
(from :tweet)
(where (:= :user_id id))
(order-by (:desc :created_at)))))
id
name
email
registered-at)
(defvar *user*
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as ’user))
(user-config *user*)
;=> #S(CONFIG :ID 4 :USER-ID 1 :TIMEZONE "JST" :COUNTRY "jp" :LANGUAGE "ja")
(user-tweets *user*)
;=> (#S(TWEET :ID 2 :USER-ID 1 :BODY "Is it working?" :CREATED-AT @2014-04-16T11:02:31.000000+09:00)
; #S(TWEET :ID 1 :USER-ID 1 :BODY "Hi." :CREATED-AT @2014-04-15T18:58:20.000000+09:00))
“‘
## Provided inflation functions
* ‘tinyint-to-boolean‘
* ‘datetime-to-timestamp‘
* ‘unixtime-to-timestamp‘
* ‘string-to-keyword‘
* ‘octet-vector-to-string‘
## Tips: Getting Association List or Hash Table for each rows
‘retrieve-one‘ and ‘retrieve-all‘ return row(s) as a property list or a list of property lists by default.
If you’d like they were other types, for example "Association List" or "Hash Table", you can do it by passing ‘:as‘ parameter.
“‘common-lisp
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as ’trivial-types:association-list)
;=> ((:ID . 1) (:NAME . "nitro_idiot") (:EMAIL . "nitro_idiot@example.com") (:REGISTERED-AT . "2014-04-14T19:20:13"))
(retrieve-one
(select :*
(from :user)
(where (:= :name "nitro_idiot")))
:as ’hash-table)
;=> #<HASH-TABLE :TEST EQL :COUNT 4 {1007AE3CD3}>
“‘
If no ‘:as‘ parameter is specified, ‘*default-row-type*‘ will be used.
“‘common-lisp
(let ((*default-row-type* ’hash-table))
(retrieve-all
(select :*
(from :user))))
;=> (#<HASH-TABLE :TEST EQL :COUNT 4 {100815FA03}> #<HASH-TABLE :TEST EQL :COUNT 4 {100815FE43}>)
“‘
## See Also
* [SxQL](https://github.com/fukamachi/sxql)
* [CL-DBI](https://github.com/fukamachi/cl-dbi)
* [cl-annot](https://github.com/arielnetworks/cl-annot), [CL-SYNTAX](https://github.com/m2ym/cl-syntax)
* [function-cache](https://github.com/AccelerationNet/function-cache)
## Author
* Eitaro Fukamachi (e.arrows@gmail.com)
## Copyright
Copyright (c) 2014 Eitaro Fukamachi (e.arrows@gmail.com)
# License
Licensed under the BSD 3-Clause License.
0.1
datafly.asd (file)
src (module)
Modules are listed depth-first from the system components tree.
• The datafly/src module |
datafly (system)
src/
Files are sorted by type and then listed depth-first from the systems components trees.
• Lisp files |
Next: The datafly/src/datafly․lisp file, Previous: Lisp files, Up: Lisp files [Contents][Index]
datafly.asd
datafly (system)
Next: The datafly/src/model․lisp file, Previous: The datafly․asd file, Up: Lisp files [Contents][Index]
src (module)
src/datafly.lisp
Next: The datafly/src/db․lisp file, Previous: The datafly/src/datafly․lisp file, Up: Lisp files [Contents][Index]
src (module)
src/model.lisp
Next: The datafly/src/cache․lisp file, Previous: The datafly/src/model․lisp file, Up: Lisp files [Contents][Index]
logger.lisp (file)
src (module)
src/db.lisp
Next: The datafly/src/logger․lisp file, Previous: The datafly/src/db․lisp file, Up: Lisp files [Contents][Index]
src (module)
src/cache.lisp
Next: The datafly/src/inflate․lisp file, Previous: The datafly/src/cache․lisp file, Up: Lisp files [Contents][Index]
src (module)
src/logger.lisp
*sql-logger-pattern* (special variable)
Next: The datafly/src/json․lisp file, Previous: The datafly/src/logger․lisp file, Up: Lisp files [Contents][Index]
db.lisp (file)
src (module)
src/inflate.lisp
Next: The datafly/src/util․lisp file, Previous: The datafly/src/inflate․lisp file, Up: Lisp files [Contents][Index]
src (module)
src/json.lisp
Previous: The datafly/src/json․lisp file, Up: Lisp files [Contents][Index]
src (module)
src/util.lisp
Next: Definitions, Previous: Files, Up: Top [Contents][Index]
Packages are listed by definition order.
Next: The datafly․model package, Previous: Packages, Up: Packages [Contents][Index]
datafly.lisp (file)
common-lisp
Next: The datafly․db package, Previous: The datafly package, Up: Packages [Contents][Index]
model.lisp (file)
Next: The datafly․cache package, Previous: The datafly․model package, Up: Packages [Contents][Index]
db.lisp (file)
Next: The datafly․logger package, Previous: The datafly․db package, Up: Packages [Contents][Index]
cache.lisp (file)
Next: The datafly․inflate package, Previous: The datafly․cache package, Up: Packages [Contents][Index]
logger.lisp (file)
common-lisp
*sql-logger-pattern* (special variable)
Next: The datafly․json package, Previous: The datafly․logger package, Up: Packages [Contents][Index]
inflate.lisp (file)
common-lisp
Next: The datafly․util package, Previous: The datafly․inflate package, Up: Packages [Contents][Index]
json.lisp (file)
Previous: The datafly․json package, Up: Packages [Contents][Index]
util.lisp (file)
common-lisp
Definitions are sorted by export status, category, package, and then by lexicographic order.
• Exported definitions | ||
• Internal definitions |
Next: Internal definitions, Previous: Definitions, Up: Definitions [Contents][Index]
• Exported special variables | ||
• Exported macros | ||
• Exported functions | ||
• Exported generic functions |
Next: Exported macros, Previous: Exported definitions, Up: Exported definitions [Contents][Index]
db.lisp (file)
db.lisp (file)
logger.lisp (file)
logger.lisp (file)
Next: Exported functions, Previous: Exported special variables, Up: Exported definitions [Contents][Index]
cache.lisp (file)
model.lisp (file)
model.lisp (file)
Next: Exported generic functions, Previous: Exported macros, Up: Exported definitions [Contents][Index]
cache.lisp (file)
cache.lisp (file)
db.lisp (file)
db.lisp (file)
model.lisp (file)
db.lisp (file)
inflate.lisp (file)
db.lisp (file)
db.lisp (file)
model.lisp (file)
inflate.lisp (file)
util.lisp (file)
util.lisp (file)
db.lisp (file)
db.lisp (file)
db.lisp (file)
db.lisp (file)
inflate.lisp (file)
inflate.lisp (file)
inflate.lisp (file)
Previous: Exported functions, Up: Exported definitions [Contents][Index]
json.lisp (file)
json.lisp (file)
Previous: Exported definitions, Up: Definitions [Contents][Index]
• Internal special variables | ||
• Internal functions |
Next: Internal functions, Previous: Internal definitions, Up: Internal definitions [Contents][Index]
db.lisp (file)
cache.lisp (file)
logger.lisp (file)
Previous: Internal special variables, Up: Internal definitions [Contents][Index]
cache.lisp (file)
json.lisp (file)
db.lisp (file)
db.lisp (file)
db.lisp (file)
json.lisp (file)
db.lisp (file)
db.lisp (file)
db.lisp (file)
json.lisp (file)
Previous: Definitions, Up: Top [Contents][Index]
• Concept index | ||
• Function index | ||
• Variable index | ||
• Data type index |
Next: Function index, Previous: Indexes, Up: Indexes [Contents][Index]
Jump to: | D F L M |
---|
Jump to: | D F L M |
---|
Next: Variable index, Previous: Concept index, Up: Indexes [Contents][Index]
Jump to: | A C D E F G M O P R S T U |
---|
Jump to: | A C D E F G M O P R S T U |
---|
Next: Data type index, Previous: Function index, Up: Indexes [Contents][Index]
Jump to: | *
S |
---|
Jump to: | *
S |
---|
Previous: Variable index, Up: Indexes [Contents][Index]
Jump to: | D P S |
---|
Jump to: | D P S |
---|