blog/content/post/2012/04/22/2012-04-22-00001554.md

33 KiB

title author date wordtwit_post_info categories
もう少しgauche-dbd-sqlite3で遊んでみた kazu634 2012-04-22
O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:5459;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}
gauche
sqlite3

使い方をもう少し勉強してみました。

ソース

#!/usr/local/bin/gosh
;; ==============================
;; Libraries:
;; ==============================
(use dbi)
(use gauche.collection)
(use gauche.test)
;; ==============================
;; Variables:
;; ==============================
(define *db-file* "test.db")
;; ==============================
;; Functions:
;; ==============================
;; (execute-sql dbfile sql)
;;   Arguments:
;;     - dbfile: path-to-sqlite3-dbfile
;;     - sql: the sql to be executed
;;
;;   Output:
;;     Collection objects that dbi module returns
(define (execute-sql dbfile sql)
(guard
(e ((<dbi-parameter-error> e) (dbi-close connect))
((<dbi-error> e) (exit 1))
(else (raise e)))
(let* ((connect (dbi-connect (string-append "dbi:sqlite3:"
dbfile)))
(query (dbi-prepare connect sql))
(result (dbi-execute query)))
(dbi-close connect)
result)))
(define (test-db-execute sql)
(test* (format "~s" sql)
#t
(dbi-open? (execute-sql *db-file* sql))))
(define (gen-list al proc)
(map (lambda (l)
(proc l))
al))
(define (gen-key-list al)
(gen-list al car))
(define (gen-value-list al)
(gen-list al cdr))
(define (append-list-string l)
(fold (lambda (data seed)
(if (string=? seed "")
(format #f
(cond
[(number? data) "~A"]
[(string? data) "'~A'"]
[(symbol? data) "~A"])
data)
(format #f
(cond
[(number? data) "~A, ~A"]
[(string? data) "~A, '~A'"]
[(symbol? data) "~A, ~A"])
seed
data)))
""
l))
(define (gen-insert-sql table al)
(format "INSERT INTO ~A (~A) VALUES (~A);"
table
(append-list-string (gen-key-list al))
(append-list-string (gen-value-list al))))
(define (append-alist-string al)
(fold (lambda (data seed)
(if (string=? seed "")
(format "~A ~A"
(car data)
(cdr data))
(format "~A, ~A ~A"
seed
(car data)
(cdr data))))
""
al))
(define (gen-create-sql table schema)
(format "CREATE TABLE ~A (~A);"
table
(append-alist-string schema)))
(define (test-db-select vecl sql)
(test* (format "~s" sql)
vecl
(map identity
(execute-sql *db-file*
sql))))
;; ==============================
;; Tests:
;; ==============================
(test-start "Gauche-dbd-sqlite3")
(test-section "Test Utilities:")
(test* "Key Arrangement Utility:"
'(date quote source md5)
(gen-key-list
'((date . 1334152804)
(quote . "Do not be evil.")
(source . "http://www.google.com/")
(md5 . "ada3ff1732557af042540d34f37ed0cd"))))
(test* "Value Arrangement Utility:"
'(1334152804
"Do not be evil."
"http://www.google.com/"
"ada3ff1732557af042540d34f37ed0cd")
(gen-value-list
'((date . 1334152804)
(quote . "Do not be evil.")
(source . "http://www.google.com/")
(md5 . "ada3ff1732557af042540d34f37ed0cd"))))
(test* "Concatnation Utility (in case of keys):"
"date, quote, source, md5"
(append-list-string '(date quote source md5)))
(test* "Concatnation Utility (in case of values):"
"1334152804, 'Do not be evil.', 'http://www.google.com/', 'ada3ff1732557af042540d34f37ed0cd'"
(append-list-string
'(1334152804
"Do not be evil."
"http://www.google.com/"
"ada3ff1732557af042540d34f37ed0cd")))
(test* "Generating Insertion SQL:"
"INSERT INTO quote (date, quote, source, md5) VALUES (1334152804, 'Do not be evil.', 'http://www.google.com/', 'ada3ff1732557af042540d34f37ed0cd');"
(gen-insert-sql
"quote"
'((date . 1334152804)
(quote . "Do not be evil.")
(source . "http://www.google.com/")
(md5 . "ada3ff1732557af042540d34f37ed0cd"))))
(test* "Concatnation Utility (in case of CREATE sql)"
"id INTEGER PRIMARY KEY AUTOINCREMENT, date NUMERIC, quote TEXT, source TEXT, md5 TEXT"
(append-alist-string
'((id . "INTEGER PRIMARY KEY AUTOINCREMENT")
(date . "NUMERIC")
(quote . "TEXT")
(source . "TEXT")
(md5 . "TEXT"))))
(test* "Generating Creation SQL:"
"CREATE TABLE quote (id INTEGER PRIMARY KEY AUTOINCREMENT, date NUMERIC, quote TEXT, source TEXT, md5 TEXT);"
(gen-create-sql
"quote"
'((id . "INTEGER PRIMARY KEY AUTOINCREMENT")
(date . "NUMERIC")
(quote . "TEXT")
(source . "TEXT")
(md5 . "TEXT"))))
(test-section "Table Creation:")
(test-db-execute
(gen-create-sql
"quote"
'((id . "INTEGER PRIMARY KEY AUTOINCREMENT")
(date . "NUMERIC")
(quote . "TEXT")
(source . "TEXT")
(md5 . "TEXT"))))
(test-section "Data Insertion:")
(test-db-execute
(gen-insert-sql
"quote"
'((date . 1334152804)
(quote . "Do not be evil.")
(source . "http://www.google.com/")
(md5 . "ada3ff1732557af042540d34f37ed0cd"))))
(test-db-execute
(gen-insert-sql
"quote"
'((id . 2)
(date . 1334359203)
(quote . "抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)")
(source . "http://twitter.com/medtoolz/status/190930863851184129")
(md5 . "6392e235cebdae0ac9c87ee958648fc0"))))
(test-section "Data Check:")
(test-db-select
'(#(1
1334152804
"Do not be evil."
"http://www.google.com/"
"ada3ff1732557af042540d34f37ed0cd"))
"SELECT * FROM quote WHERE id=1;")
(test-db-select
'(#(2
1334359203
"抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)"
"http://twitter.com/medtoolz/status/190930863851184129"
"6392e235cebdae0ac9c87ee958648fc0"))
"SELECT * FROM quote WHERE id=2;")
(test-db-select
'(#(1
1334152804
"Do not be evil."
"http://www.google.com/"
"ada3ff1732557af042540d34f37ed0cd")
#(2
1334359203
"抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)"
"http://twitter.com/medtoolz/status/190930863851184129"
"6392e235cebdae0ac9c87ee958648fc0"))
"SELECT * FROM quote;")
(test-section "Database Manipulation:")
(test-db-execute
"UPDATE quote SET date=1000000000 WHERE id=1;")
(test-db-select
'(#(1
1000000000
"Do not be evil."
"http://www.google.com/"
"ada3ff1732557af042540d34f37ed0cd"))
"SELECT * FROM quote WHERE id=1;")
(test-db-execute
"DELETE FROM quote WHERE id=1;")
(test-db-select
'(#(2
1334359203
"抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)"
"http://twitter.com/medtoolz/status/190930863851184129"
"6392e235cebdae0ac9c87ee958648fc0"))
"SELECT * FROM quote;")
;; ==============================
;; Teardown:
;; ==============================
(sys-unlink *db-file*)
(test-end)

実行結果

実行すると、こんな感じになります:

Testing Gauche-dbd-sqlite3 ...
<Test Utilities:>--------------------------------------------------------------
test Key Arrangement Utility:, expects (date quote source md5) ==> ok
test Value Arrangement Utility:, expects (1334152804 "Do not be evil." "http://www.google.com/" "ada3ff1732557af042540d34f37ed0cd") ==> ok
test Concatnation Utility (in case of keys):, expects "date, quote, source, md5" ==> ok
test Concatnation Utility (in case of values):, expects "1334152804, 'Do not be evil.', 'http://www.google.com/', 'ada3ff1732557af042540d34f37ed0cd'" ==> ok
test Generating Insertion SQL:, expects "INSERT INTO quote (date, quote, source, md5) VALUES (1334152804, 'Do not be evil.', 'http://www.google.com/', 'ada3ff1732557af042540d34f37ed0cd');" ==> ok
test Concatnation Utility (in case of CREATE sql), expects "id INTEGER PRIMARY KEY AUTOINCREMENT, date NUMERIC, quote TEXT, source TEXT, md5 TEXT" ==> ok
test Generating Creation SQL:, expects "CREATE TABLE quote (id INTEGER PRIMARY KEY AUTOINCREMENT, date NUMERIC, quote TEXT, source TEXT, md5 TEXT);" ==> ok
<Table Creation:>--------------------------------------------------------------
test "CREATE TABLE quote (id INTEGER PRIMARY KEY AUTOINCREMENT, date NUMERIC, quote TEXT, source TEXT, md5 TEXT);", expects #t ==> ok
<Data Insertion:>--------------------------------------------------------------
test "INSERT INTO quote (date, quote, source, md5) VALUES (1334152804, 'Do not be evil.', 'http://www.google.com/', 'ada3ff1732557af042540d34f37ed0cd');", expects #t ==> ok
test "INSERT INTO quote (id, date, quote, source, md5) VALUES (2, 1334359203, '抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)', 'http://twitter.com/medtoolz/status/190930863851184129', '6392e235cebdae0ac9c87ee958648fc0');", expects #t ==> ok
<Data Check:>------------------------------------------------------------------
test "SELECT * FROM quote WHERE id=1;", expects (#(1 1334152804 "Do not be evil." "http://www.google.com/" "ada3ff1732557af042540d34f37ed0cd")) ==> ok
test "SELECT * FROM quote WHERE id=2;", expects (#(2 1334359203 "抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)" "http://twitter.com/medtoolz/status/190930863851184129" "6392e235cebdae0ac9c87ee958648fc0")) ==> ok
test "SELECT * FROM quote;", expects (#(1 1334152804 "Do not be evil." "http://www.google.com/" "ada3ff1732557af042540d34f37ed0cd") #(2 1334359203 "抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)" "http://twitter.com/medtoolz/status/190930863851184129" "6392e235cebdae0ac9c87ee958648fc0")) ==> ok
<Database Manipulation:>-------------------------------------------------------
test "UPDATE quote SET date=1000000000 WHERE id=1;", expects #t ==> ok
test "SELECT * FROM quote WHERE id=1;", expects (#(1 1000000000 "Do not be evil." "http://www.google.com/" "ada3ff1732557af042540d34f37ed0cd")) ==> ok
test "DELETE FROM quote WHERE id=1;", expects #t ==> ok
test "SELECT * FROM quote;", expects (#(2 1334359203 "抑止力とは、「戦う意思を持たない暴力」ではなく、「戦わない意志を持った暴力」のことなのだと思う。小さな違いだけれど、意思を示さない限り、それは「力」たりえない。 (via @medtoolz)" "http://twitter.com/medtoolz/status/190930863851184129" "6392e235cebdae0ac9c87ee958648fc0")) ==> ok
passed.