streemのインストール
- 作者: まつもとゆきひろ
- 出版社/メーカー: 日経BP社
- 発売日: 2016/12/22
- メディア: Kindle版
- この商品を含むブログを見る
作業場所の作成
foo:work03 $ mkdir streem foo:work03 $ cd streem
ソースコードの取得
foo:streem $ git clone https://github.com/matz/streem.git Cloning into 'streem'... remote: Counting objects: 4041, done. remote: Compressing objects: 100% (8/8), done. remote: Total 4041 (delta 3), reused 0 (delta 0), pack-reused 4033 Receiving objects: 100% (4041/4041), 758.46 KiB | 263.00 KiB/s, done. Resolving deltas: 100% (2857/2857), done. Checking connectivity... done. foo:streem $ pwd /Users//work03/streem foo:streem $ ls streem
取得内容の確認
foo:streem $ tree ./streem/ ./streem/ ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── examples │ ├── 01cat.strm │ ├── 02hello.strm │ ├── 03fizzbuzz.strm │ ├── 04emit.strm │ ├── 05emit.strm │ ├── 05filter.strm │ ├── 06echo.strm │ ├── 07netcat.strm │ ├── 08chat.strm │ └── 09match.strm ├── src │ ├── Makefile │ ├── args.c │ ├── array.c │ ├── atomic.h │ ├── core.c │ ├── csv.c │ ├── env.c │ ├── exec.c │ ├── graph.c │ ├── init.c │ ├── io.c │ ├── iter.c │ ├── khash.h │ ├── kvs.c │ ├── latch.c │ ├── lex.l │ ├── main.c │ ├── math.c │ ├── ncpu.c │ ├── node.c │ ├── node.h │ ├── ns.c │ ├── number.c │ ├── parse.y │ ├── pollfd.c │ ├── pollfd.h │ ├── queue.c │ ├── queue.h │ ├── random.c │ ├── signal.c │ ├── socket.c │ ├── sort.c │ ├── stat.c │ ├── string.c │ ├── strm.h │ ├── strptime.c │ ├── time.c │ └── value.c └── vagrant ├── Vagrantfile └── bootstrap.sh 3 directories, 54 files
ブランチ作成
foo:streem $ git branch * master foo:streem $ git branch test01 foo:streem $ git checkout test01 Switched to branch 'test01' foo:streem $ git branch master * test01
コンパイル(ワーニングがでますが、実行には問題ないようです。)
foo:streem $ make /Applications/Xcode.app/Contents/Developer/usr/bin/make -C src all gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o args.o args.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o array.o array.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o core.o core.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o csv.o csv.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o env.o env.c env.c:4:1: warning: unused function 'kh_destroy_env' [-Wunused-function] KHASH_MAP_INIT_INT64(env, strm_value); ^ ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:205:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_destroy_##name(kh_##name##_t *h) \ ^ <scratch space>:243:1: note: expanded from here kh_destroy_env ^ env.c:4:1: warning: unused function 'kh_clear_env' [-Wunused-function] ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:213:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_clear_##name(kh_##name##_t *h) \ ^ <scratch space>:246:1: note: expanded from here kh_clear_env ^ 2 warnings generated. gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o exec.o exec.c exec.c:399:12: warning: variable 'n' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized] else if (lambda->body->type == NODE_PLAMBDA) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ exec.c:429:7: note: uninitialized use occurs here if (n == STRM_NG && strm) { ^ exec.c:399:8: note: remove the 'if' if its condition is always true else if (lambda->body->type == NODE_PLAMBDA) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ exec.c:377:11: note: initialize the variable 'n' to silence this warning int i, n; ^ = 0 1 warning generated. gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o graph.o graph.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o init.o init.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o io.o io.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o iter.o iter.c iter.c:555:1: warning: unused function 'kh_destroy_rbk' [-Wunused-function] KHASH_MAP_INIT_INT64(rbk, strm_value); ^ ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:205:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_destroy_##name(kh_##name##_t *h) \ ^ <scratch space>:243:1: note: expanded from here kh_destroy_rbk ^ iter.c:555:1: warning: unused function 'kh_clear_rbk' [-Wunused-function] ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:213:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_clear_##name(kh_##name##_t *h) \ ^ <scratch space>:246:1: note: expanded from here kh_clear_rbk ^ iter.c:555:1: warning: unused function 'kh_get_rbk' [-Wunused-function] ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:220:16: note: expanded from macro 'KHASH_IMPL' SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ ^ <scratch space>:249:1: note: expanded from here kh_get_rbk ^ 3 warnings generated. gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o kvs.o kvs.c kvs.c:5:1: warning: unused function 'kh_clear_kvs' [-Wunused-function] KHASH_INIT(kvs, strm_string, strm_value, 1, kh_int64_hash_func, kh_int64_hash_equal); ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:213:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_clear_##name(kh_##name##_t *h) \ ^ <scratch space>:122:1: note: expanded from here kh_clear_kvs ^ kvs.c:6:1: warning: unused function 'kh_clear_txn' [-Wunused-function] KHASH_INIT(txn, strm_string, strm_value, 1, kh_int64_hash_func, kh_int64_hash_equal); ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:213:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_clear_##name(kh_##name##_t *h) \ ^ <scratch space>:150:1: note: expanded from here kh_clear_txn ^ 2 warnings generated. gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o latch.o latch.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o main.o main.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o math.o math.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o ncpu.o ncpu.c flex --header-file=lex.yy.h -olex.yy.c lex.l bison -y -d -o y.tab.c parse.y gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o node.o node.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o ns.o ns.c ns.c:4:1: warning: unused function 'kh_destroy_ns' [-Wunused-function] KHASH_MAP_INIT_INT64(ns, strm_state*); ^ ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:205:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_destroy_##name(kh_##name##_t *h) \ ^ <scratch space>:243:1: note: expanded from here kh_destroy_ns ^ ns.c:4:1: warning: unused function 'kh_clear_ns' [-Wunused-function] ./khash.h:584:2: note: expanded from macro 'KHASH_MAP_INIT_INT64' KHASH_INIT(name, khint64_t, khval_t, 1, kh_int64_hash_func, kh_int64_hash_equal) ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:213:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_clear_##name(kh_##name##_t *h) \ ^ <scratch space>:246:1: note: expanded from here kh_clear_ns ^ 2 warnings generated. gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o number.o number.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o pollfd.o pollfd.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o queue.o queue.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o random.o random.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o signal.o signal.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o socket.o socket.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o sort.o sort.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o stat.o stat.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o string.o string.c string.c:62:1: warning: unused function 'kh_destroy_sym' [-Wunused-function] KHASH_INIT(sym, struct sym_key, strm_string, 1, sym_hash, sym_eq); ^ ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:205:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_destroy_##name(kh_##name##_t *h) \ ^ <scratch space>:119:1: note: expanded from here kh_destroy_sym ^ string.c:62:1: warning: unused function 'kh_clear_sym' [-Wunused-function] ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:213:13: note: expanded from macro 'KHASH_IMPL' SCOPE void kh_clear_##name(kh_##name##_t *h) \ ^ <scratch space>:122:1: note: expanded from here kh_clear_sym ^ string.c:62:1: warning: unused function 'kh_get_sym' [-Wunused-function] ./khash.h:349:2: note: expanded from macro 'KHASH_INIT' KHASH_INIT2(name, static kh_inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:346:2: note: expanded from macro 'KHASH_INIT2' KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) ^ ./khash.h:220:16: note: expanded from macro 'KHASH_IMPL' SCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \ ^ <scratch space>:125:1: note: expanded from here kh_get_sym ^ 3 warnings generated. gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o strptime.o strptime.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o time.o time.c gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE -MMD -MP -c -o value.o value.c gcc -g -MMD -MP -c y.tab.c -o parse.o mkdir -p "$(dirname ../bin/streem)" gcc -std=gnu99 -g -ggdb -Wall -DNO_LOCKFREE_QUEUE args.o array.o core.o csv.o env.o exec.o graph.o init.o io.o iter.o kvs.o latch.o main.o math.o ncpu.o node.o ns.o number.o pollfd.o queue.o random.o signal.o socket.o sort.o stat.o string.o strptime.o time.o value.o parse.o -o ../bin/streem -lpthread -lm foo:streem $
実行(vで構文木の内容が出力されます。eはコマンドラインで直接プログラムを指定する方法です。)
foo:streem $ ./bin/streem -ve "1+1" NODES: OP: op: + VALUE(NUMBER): 1 VALUE(NUMBER): 1 foo:streem $ ./bin/streem -ve "print 1+1" -e:1:syntax error, unexpected lit_number, expecting $end foo:streem $ ./bin/streem -ve "print (1+1)" NODES: CALL: print ARRAY: OP: op: + VALUE(NUMBER): 1 VALUE(NUMBER): 1 2 foo:streem $ ./bin/streem -ve "stdin | stdout" NODES: OP: op: | IDENT: stdin IDENT: stdout aa aa bb bb ^C
ファイルからの実行
foo:streem $ ./bin/streem ./examples/05filter.strm 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 foo:streem $
統計情報
sorce | line | word | char |
---|---|---|---|
../streem/src/args.c | 240 | 569 | 5502 |
../streem/src/array.c | 161 | 399 | 3291 |
../streem/src/core.c | 273 | 593 | 5418 |
../streem/src/csv.c | 480 | 1326 | 10733 |
../streem/src/env.c | 117 | 322 | 2485 |
../streem/src/exec.c | 1144 | 3135 | 28917 |
../streem/src/graph.c | 243 | 558 | 4419 |
../streem/src/init.c | 29 | 51 | 787 |
../streem/src/io.c | 393 | 977 | 7996 |
../streem/src/iter.c | 841 | 2117 | 17859 |
../streem/src/kvs.c | 431 | 1231 | 10174 |
../streem/src/latch.c | 235 | 521 | 4733 |
../streem/src/lex.yy.c | 2466 | 10114 | 70602 |
../streem/src/main.c | 340 | 791 | 7755 |
../streem/src/math.c | 94 | 231 | 2335 |
../streem/src/ncpu.c | 37 | 48 | 433 |
../streem/src/node.c | 712 | 1537 | 12889 |
../streem/src/ns.c | 69 | 172 | 1396 |
../streem/src/number.c | 167 | 420 | 4205 |
../streem/src/pollfd.c | 254 | 803 | 6150 |
../streem/src/queue.c | 194 | 435 | 3608 |
../streem/src/random.c | 249 | 647 | 5168 |
../streem/src/signal.c | 84 | 213 | 1599 |
../streem/src/socket.c | 203 | 548 | 4924 |
../streem/src/sort.c | 593 | 1566 | 13128 |
../streem/src/stat.c | 541 | 1573 | 12498 |
../streem/src/string.c | 437 | 1048 | 8580 |
../streem/src/strptime.c | 359 | 1088 | 12730 |
../streem/src/time.c | 605 | 1742 | 14295 |
../streem/src/value.c | 531 | 1200 | 10454 |
../streem/src/y.tab.c | 2779 | 11110 | 86882 |
total | 15301 | 47085 | 381945 |
find ../streem/src -name '*.c' | xargs wc | awk '{print "|" $4 "|" $1 "|" $2 "|" $3 "|"}'
Haskellでのデータベースアクセス
PostgresqlのHDBCを利用しました。
HDBC: Haskell Database Connectivity
作業場所の作成
$ mkdir haskelldatabase $ cd haskelldatabase
サンドボックスの作成
$ cabal sandbox init Writing a default package environment file to /Users/bar/work03/haskelldatabase/cabal.sandbox.config Creating a new sandbox at /Users/bar/work03/haskelldatabase/.cabal-sandbox
HDBCのインストール
$ cabal install hdbc Resolving dependencies... Notice: installing into a sandbox located at /Users/bar/work03/haskelldatabase/.cabal-sandbox Configuring mtl-2.2.1... Configuring old-locale-1.0.0.7... Downloading utf8-string-1.0.1.1... Configuring text-1.2.2.1... Configuring utf8-string-1.0.1.1... Building old-locale-1.0.0.7... Building mtl-2.2.1... Building text-1.2.2.1... Building utf8-string-1.0.1.1... Installed old-locale-1.0.0.7 Configuring old-time-1.1.0.3... Building old-time-1.1.0.3... Installed mtl-2.2.1 Installed utf8-string-1.0.1.1 Installed old-time-1.1.0.3 Installed text-1.2.2.1 Downloading convertible-1.1.1.0... Configuring convertible-1.1.1.0... Building convertible-1.1.1.0... Installed convertible-1.1.1.0 Downloading HDBC-2.4.0.1... Configuring HDBC-2.4.0.1... Building HDBC-2.4.0.1... Installed HDBC-2.4.0.1 Updating documentation index /Users/bar/work03/haskelldatabase/.cabal-sandbox/share/doc/x86_64-osx-ghc-8.0.1/index.html
postgresqlドライバのインストール
$ cabal install hdbc-postgresql Resolving dependencies... Notice: installing into a sandbox located at /Users/bar/work03/haskelldatabase/.cabal-sandbox Configuring parsec-3.1.11... Building parsec-3.1.11... Installed parsec-3.1.11 Downloading HDBC-postgresql-2.3.2.4... Configuring HDBC-postgresql-2.3.2.4... Building HDBC-postgresql-2.3.2.4... Installed HDBC-postgresql-2.3.2.4 Updating documentation index /Users/bar/work03/haskelldatabase/.cabal-sandbox/share/doc/x86_64-osx-ghc-8.0.1/index.html $
データベースの準備
$ createuser -a -d -U bar -P user03 Enter password for new role: Enter it again: $ createdb -E UTF8 -O user03 -U user03 testdb03
データベスの確認
$ psql -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+------------+----------+-------------+-------------+--------------------------- testdb03 | user03 | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
テーブルの作成
$ psql -d testdb03 psql (9.6.1) Type "help" for help. testdb03=# testdb03=# create table foo(name varchar(50)); testdb03=# \dt; List of relations Schema | Name | Type | Owner --------+------+-------+------------ public | foo | table | bar (1 row)
テーブル情報の確認
> :t connectPostgreSQL connectPostgreSQL :: String -> IO Connection > :t getTables getTables :: IConnection conn => conn -> IO [String] > connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= (\x -> getTables x) ["foo"] >
データ挿入、オートコミットはされない。
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= (\x -> run x "insert into foo values('abc')" []) 1 >
データ挿入+コミット
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= (\x -> run x "insert into foo values('ghi')" [] >>= (\_ -> commit x)) >
検索(prepare -> execute -> fetch->結果はMaybeというパターン)
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo" >>= (\y -> execute y [] >>= \_ -> fetchRow y) Just [SqlByteString "abc"]
列名付き検索
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo" >>= (\y -> execute y [] >>= \_ -> fetchRowAL y) Just [("name",SqlByteString "abc")]
列名付き検索をMapで取得
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo" >>= (\y -> execute y [] >>= \_ -> fetchRowMap y) Just (fromList [("name",SqlByteString "abc")])
複数行取得
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo" >>= (\y -> execute y [] >>= \_ -> fetchAllRows y) [[SqlByteString "abc"],[SqlByteString "def"],[SqlByteString "ghi"]]
検索結果をMapで取得
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo" >>= (\y -> execute y [] >>= \_ -> fetchAllRowsMap y) [fromList [("name",SqlByteString "abc")],fromList [("name",SqlByteString "def")],fromList [("name",SqlByteString "ghi")]]
日本語の取り扱い
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= (\x -> run x "insert into foo values('あいうえお')" [] >>= (\_ -> commit x)) > connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo" >>= (\y -> execute y [] >>= \_ -> fetchAllRowsMap y) [fromList [("name",SqlByteString "abc")],fromList [("name",SqlByteString "def")],fromList [("name",SqlByteString "ghi")],fromList [("name",SqlByteString "\227\129\130\227\129\132\227\129\134\227\129\136\227\129\138")]]
NULLの取り扱い SqlNullというデータがある
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> prepare x "select * from foo where name is null" >>= (\y -> execute y [] >>= \_ -> fetchAllRowsMap y) [fromList [("name",SqlNull)]]
トランザクションを使う
> connectPostgreSQL "host=localhost dbname=testdb03 user=user03" >>= \x -> withTransaction x (\y -> run y "insert into foo values('klm')" []) 1
do記法
import Database.HDBC import Database.HDBC.PostgreSQL main = do con <- connectPostgreSQL "host=localhost dbname=testdb03 user=user03" run con "insert into foo values('qwerty')" [] commit con
HaskellのMonoidの情報
Blenderで立方体を床に落とすサンプル
Cubeが床に落ちることをやります。
1: キューブを追加する(Shift+A -> Mesh -> Cube)。
2: Cubeを上に動かす。
3: Physics -> Rigid Body -> Type : Active とする。
4: 床を追加する(Shift+A -> Mesh -> Plane)。
5: Planeの位置と大きさを調整する。
6: Physics -> Rigid Body -> Type : Passive とする。
7: 再生する
8: キューブが床に落ちる