「posts」…このWebアプリを示すURLのディレクトリ名 この後、コマンド終盤に記載の、lib/sample_web/router.ex へのパス追加と、モデルのマイグレーションを行います # mix phx.gen.html DbAccessor Post posts title:string body:text … Add the resource to your browser scope in lib/sample_web/router.ex: resources "/posts", PostController Remember to update your repository by running migrations: $ mix ecto.migrate 再掲: Elixir入門#3より
use SampleWeb, :router … scope "/", SampleWeb do pipe_through :browser # Use the default browser stack get "/", PageController, :index resources "/posts", PostController end … end 再掲: Elixir入門#3より
Type "help" for help. postgres=# ¥c sample_dev You are now connected to database “sample_dev" as user "postgres". sample_dev=# ¥d posts Table "public.posts" Column | Type | Modifiers -------------+-----------------------------+---------------------------------------------------- id | integer | not null default nextval('posts_id_seq'::regclass) title | character varying(255) | body | text | inserted_at | timestamp without time zone | not null updated_at | timestamp without time zone | not null Indexes: "posts_pkey" PRIMARY KEY, btree (id) 再掲: Elixir入門#3より
Ecto.Schema import Ecto.Changeset alias Sample.DbAccessor.Post schema "post" do field :title, :string field :body, :string timestamps() end @doc false def changeset(%Post{} = post, attrs) do post |> cast(attrs, [:title, :body]) |> validate_required([:title, :body]) end end lib/sample/db_accessor/post.ex
query( sql ) when sql != "" do { :ok, result } = Ecto.Adapters.SQL.query( Sample.Repo, sql, [] ) result end def columns( %{ columns: columns } = _result ), do: columns def rows( %{ rows: rows } = _result ), do: rows def columns_rows( result ) do result |> rows |> Enum.map( &( MapList.zip( columns( result ), &1 ) ) ) end def viewable( value ) when is_tuple( value ), do: Timex.to_datetime( value ) def viewable( value ), do: value end lib/util/db.ex Smallexを使うと、リスト同士を結合してマップとして 構成し直す、といったような処理が簡単に書ける
(GenServer、Agent) のデータとして持たせる ② ETSやmnesia等、Elixir標準のオンメモリDBにロード ③ Redis等のElixir標準で無いオンメモリDBにロード このうち、最もてっとり早いのは、②のETSやmnesia等のElixir 標準のオンメモリDBを使う方法です ※なお、定期的なバッチによるロードは、後の章で解説します
do def get() do HTTPoison.get!( "https://qiita.com/api/v2/items?query=Elixir" ) |> body |> Poison.decode! |> Enum.map( fn( %{ "title" => title } ) -> title end ) end def body( %{ status_code: 200, body: json_body } ), do: json_body end 再掲: Elixir入門#1より
(違いはjson指定のみ) 前述のWeb用のモデルと全く同じモデルで作ってみます # mix phx.new api --no-brunch # cd api # mix phx.gen.json Tool Post posts title:string body:text … Add the resource to your api scope in lib/api_web/router.ex: resources "/posts", PostController, except: [:new, :edit] Remember to update your repository by running migrations: $ mix ecto.migrate # mix ecto.create 再掲: Elixir入門#3より
(9.3.18) Type "help" for help. postgres=# ¥c api_dev You are now connected to database “api_dev" as user "postgres". web_dev=# ¥d posts Table "public.posts" Column | Type | Modifiers -------------+-----------------------------+---------------------------------------------------- id | integer | not null default nextval('posts_id_seq'::regclass) title | character varying(255) | body | text | inserted_at | timestamp without time zone | not null updated_at | timestamp without time zone | not null Indexes: "posts_pkey" PRIMARY KEY, btree (id) 再掲: Elixir入門#3より
lines1 = new String[ lines0.length ]; for ( i = 0; i < lines0.length; i++ ) lines1[ i ] = lines0[ i ].replace( ",", "¥t" ); // ②CRLF→LF String[] lines2 = new String[ lines1.length ]; for ( i = 0; i < lines1.length; i++ ) lines2[ i ] = lines1[ i ].replace( "¥r¥n", "¥n" ); // ③ダブルクォート外し String[] lines3 = new String[ lines2.length ]; for ( i = 0; i < lines2.length; i++ ) lines3[ i ] = lines2[ i ].replace( "¥"", "" ); // 集計 // ④タブで分割 String[][] lines4 = new String[ lines3.length ][]; for ( i = 0; i < lines3.length; i++ ) lines4[ i ] = lines3[ i ].split( "¥t" ); … 1ページ目 再掲: Elixir入門#9より
[subtype] bot_message, [username] segment_generate@PBAT-16, [pretext] [critical] failed to generate segment at 2017-09-14 03:45:56 UTC, [fields] <<account>> [id: 108] 株式会社hogehoge <<segment>> [id: 6959] 【mail】point <<detail>> PG::UndefinedTable: ERROR: relation customers_bd_wyna8r_1 does not exist : insert into segments_bd_wyna8r_1__20170914034556(segment_id, visitor_id) select 6959, visitor_id from ( select visitors.visitor_id from visitors_bd_wyna8r_1 visitors inner join customers_bd_wyna8r_1 customers on visitors.unique_visitor_id = customers.unique_visitor_id inner join ( select unique_visitor_id, max(id) as max_id from customers_bd_wyna8r_1 where unique_visitor_id is not null and unique_visitor_id <> group by unique_visitor_id ) tmp_customers on customers.id = tmp_customers.max_id and customers.unique_visitor_id = tmp_customers.unique_visitor_id inner join ( select unique_visitor_id, max(first_visit_date) as max_first_visit_date from visitors_bd_wyna8r_1 where unique_visitor_id is not null and unique_visitor_id <> group by unique_visitor_id ) tmp_visitors on visitors.first_visit_date = tmp_visitors.max_first_visit_date and visitors.unique_visitor_id = tmp_visitors.unique_visitor_id where customers.customer_id in (select x_4608.c_148046 as customer_id from x_4608 where x_4608.c_148046 not in (select x_4608.c_148046 as customer_id from x_4608 where x_4608.c_148074 like %1% group by x_4608.c_148046) group by x_4608.c_148046 intersect select x_4608.c_148046 as customer_id from x_4608 group by x_4608.c_148046 having sum(case when 1 = 1 then x_4608.c_148069 else 0 end) > 299 intersect select x_4608.c_148046 as customer_id from x_4608 where cast(x_4608.c_148065 as BIGINT) <= 1497657599000 group by x_4608.c_148046) group by visitors.visitor_id ) _tmp <!channel>, [channel_id] C2T611ZP1, [channel_name] 再掲: Elixir入門#9より