English

Nesta Pocket スクリプト入門


Nesta Pocket スクリプトとは ?

Nesta Pocket はデータの登録と検索に対応したカード型データベースのアプリです。
データの照合や計算などの機能はサポートされていません。
Nesta Pocket でデータ処理を可能にするのが、このスクリプトになります。
スクリプトを作成する事でデータの加工などを繰り返し実行することができます。

Nesta Pocket は複数のデータベースをデータストアとして管理しています。
データベースは複数のエンティティ(レコード)から構成されます。
エンティティは複数のプロパティ(フィールド)から構成されています。
エンティティやプロパティはカーソル機能を使用して操作します。
検索条件を指定してカーソルを作成することでデータの絞り込みが可能です。

トランザクション処理に対応していますので複数の更新をまとめて行えます。
トランザクション中の更新が失敗した場合は、すべての更新が元に戻されます。

Neste Pocket はスクリプト言語としてシンプルで高速な Luaを内蔵しています。
Lua で用意されている標準関数も使用する事が可能です。
Lua のバージョンは 5.2 です。

Lua から Nesta Pocket のデータストアにアクセスするための関数が用意されています。

※本稿でのスクリーンショットは英語版のものになります。

スクリプト一覧

作成されたスクリプトが一覧として表示されます。
新規にスクリプトを作成する場合は + アイコンをタップします。
編集モードでスクリプト名をタップすることでスクリプト名を変更できます。

新規スクリプトの作成

スクリーン上部にある + アイコンをタップすることでスクリプト名を入力するビューが表示されます。
スクリプト名を60文字以内で入力します。

スクリプトの編集

スクリプトをキーボードを使用して編集します。
Wi-Fi環境が使える場合は、他のコンピュータで作成したスクリプトをアップロードしたり、ダウンロードすることが可能です。
日本語を使用したスクリプトファイルは utf-8 で保存してください。

簡単なスクリプト

一番簡単な Luaプログラムです。

print("Hello World!")

スクリプトの実行

スクリーン右下にあるプレイアイコンをタップすることでスクリプトが実行されます。
print関数の出力は Output 領域に表示されます。

Lua 標準関数

スクリプト内で Lua が用意している標準関数を使用することが可能です。

a = "one string"
b = string.gsub(a, "one", "another")
print(a)
print(b)

Nesta Pocket 関数

キーボードの f(x) をタップすることで Nesta Pocket にアクセスするための関数の一覧が表示されます。
対象のセルをタップすることで関数が編集中のスクリプトのキャレット位置に挿入されます。
セルの右側にある詳細ボタンをタップすることで関数の説明に切り替わります。

データベースの新規作成

データベースを新規に作成するスクリプトです。
すでに作成するデータベース名が存在している場合はエラーになります。
作成されたデータベースのフィールドは定義されていません。

-- create database
dbname = "sampledb"
result = ndb_create(dbname)
if (result ~= true) then
  print(result)
end

データベースの削除

データベースを削除するスクリプトです。
削除するデータベースが存在するかをチェックしてから削除しています。

-- find database
dbname = "sampledb"
if (ndb_find(dbname) == true) then
  -- remove database
  result = ndb_remove(dbname)
  if (result ~= true) then
    print(result)
  end
end

新規エンティティの作成

データベースにフィールドを設定してエンティティを追加します。
フィールドは「学籍番号, 氏名, 英語, 数学, 科学」になります。

--
-- Function:
--  create database
--
function createDB(dbname)
  if (ndb_find(dbname) == true) then
    ndb_remove(dbname)
  end
  local result = ndb_create(dbname)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  return true
end

--
-- Function:
--  add fields
--    学籍番号(5), 氏名, 英語(3), 数学(3), 科学(3)
--
function addFields(db)
  local result = ndb_field_add(db, "学籍番号", NDB_NUMBER, 0, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "氏名", NDB_TEXT, 0, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "英語", NDB_NUMBER, 3, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "数学", NDB_NUMBER, 3, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "科学", NDB_NUMBER, 3, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  return true
end

-- 
-- Function:
--  add new Entity via cursor
-- 
function newEntity(cursor, number, name, english, mathematics, science)
  -- create new Entity
  local result = ndb_cursor_new(cursor)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end

  -- Number field
  local property = ndb_property_get(cursor, "学籍番号")
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  result = ndb_property_putnum(property, number)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end

  -- Name field
  property = ndb_property_get(cursor, "氏名")
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  result = ndb_property_putstr(property, name)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end

  -- English field
  property = ndb_property_get(cursor, "英語")
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  if (string.len(english) > 0) then
    result = ndb_property_putnum(property, english)
    if (result ~= true) then
      -- error
      print(result)
      return false
    end
  end

  -- Math field
  property = ndb_property_get(cursor, "数学")
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  if (string.len(mathematics) > 0) then
    result = ndb_property_putnum(property, mathematics)
    if (result ~= true) then
      -- error
      print(result)
      return false
    end
  end

  -- Science field
  property = ndb_property_get(cursor, "科学")
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  if (string.len(science) > 0) then
    result = ndb_property_putnum(property, science)
    if (result ~= true) then
      -- error
      print(result)
      return false
    end
  end

  -- Success
  return true
end

--
-- スクリプトはここから実行されます。
--
local db = "sampledb"
if (createDB(db) == false) then
  return
end
if (addFields(db) == false) then
  return
end
local cursor = ndb_cursor_db(db)
if (type(cursor) == "string") then
  -- error
  print(cursor)
  return
end

-- add entity
newEntity(cursor, 12001, "山田 太郎", 80, 100, 95)
newEntity(cursor, 12002, "田中 花子", 95, 65, 80)
newEntity(cursor, 12003, "斉藤 一郎", 92, 54, 65)
newEntity(cursor, 12004, "Alice Goto", 100, 54, 47)
newEntity(cursor, 12005, "Lynne Richards", 84, 85, 86)
newEntity(cursor, 12006, "Rabbit Bertleman", 54, 37, "")
newEntity(cursor, 12007, "Shaun Lopez", 78, "", 63)
newEntity(cursor, 12008, "Susumu Inaba", 64, 54, 48)
newEntity(cursor, 12009, "Lisa Sano", 96, 74, 76)
newEntity(cursor, 12010, "Mark Tomson", "", 18, 0)

-- close cursor
ndb_cursor_close(cursor)

フィールドの追加

平均のフィールドを追加して、すべてのエンティティの英語、数学、科学の平均点を計算して出力します。

--
-- Function:
--  add average field
--
function addFieldAverage(db)
  ndb_field_remove(db, "平均")
  local result = ndb_field_add(db, "平均", NDB_NUMBER, 3, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
end

--
-- Function:
--  get score via cursor(field type is number)
--
function getScore(cursor, fieldname)
  local property = ndb_property_get(cursor, fieldname)
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  return ndb_property_getnum(property)
end

--
-- Function:
--  To calculate average of entity.
--  Blank field is not the target of average.
--
function calcEntity(cursor)
  local total_score = 0
  local score_count = 0
  local english = getScore(cursor, "英語")
  if (english == false) then
    -- error
    return false
  end
  if (english ~= nil) then
    total_score = total_score + english
    score_count = score_count + 1
  end

  local mathematics = getScore(cursor, "数学")
  if (mathematics == false) then
    -- error
    return false
  end
  if (mathematics ~= nil) then
    total_score = total_score + mathematics
    score_count = score_count + 1
  end

  local science = getScore(cursor, "科学")
  if (science == false) then
    -- error
    return false
  end
  if (science ~= nil) then
    total_score = total_score + science
    score_count = score_count + 1
  end

  local average = 0
  if (score_count > 0) then
    average = total_score / score_count
    -- truncate the decimal point
    average = math.floor(average)
  end

  property = ndb_property_get(cursor, "平均")
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  result = ndb_property_putnum(property, average)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  return true
end

--
-- スクリプトはここから実行されます。
--
local db = "sampledb"

-- add average field
if (addFieldAverage(db) == false) then
  return
end
local cursor = ndb_cursor_db(db)
if (type(cursor) == "string") then
  print(cursor)
  return
end

-- calculate average via cursor
local count = ndb_cursor_count(cursor)
for i = 1, count, 1 do
  result = calcEntity(cursor)
  if (result == true) then
    result = ndb_cursor_next(cursor)
    if (result == false) then
      break
    end
  end
end

-- close cursor
ndb_cursor_close(cursor)

検索条件を指定したデータ抽出

平均が60点未満のデータを表示します。

--
-- Function:
--  Gets string field value via cursor.
--
function getString(cursor, fieldname)
  local property = ndb_property_get(cursor, fieldname)
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  return ndb_property_getstr(property)
end

--
-- Function:
--  Gets number field value via cursor.
--
function getNumber(cursor, fieldname)
  local property = ndb_property_get(cursor, fieldname)
  if (type(property) == "string") then
    -- error
    print(property)
    return false
  end
  return ndb_property_getnum(property)
end

--
-- Function:
--  Display Number, Name and Average of the entity.
--
function displayEntity(cursor)
  -- get value of the Number field.
  local number = getNumber(cursor, "学籍番号")
  if (number == false) then
    return false
  end

  -- get value of the Name field.
  local name = getString(cursor, "氏名")
  if (name == false) then
    return false
  end

  -- get value of the Average field.
  local average = getNumber(cursor, "平均")
  if (average == false) then
    return false
  end

  print(number .. " " .. name .. " : " .. average)
  return true
end

--
-- スクリプトはここから実行されます。
--
local db = "sampledb"

-- new query
local query = ndb_query_create(db)
if (type(query) == "string") then
  -- error
  print(query)
  return
end

-- new search filter
local filter = ndb_filter_create(query)
if (type(filter) == "string") then
  -- error
  print(filter)
  return
end

-- add the filter condition
-- condition: Average < 60
ndb_filter_add(filter, "平均", NDB_LT, 60)

-- add the filter condition to the query.
ndb_query_add(query, filter)

-- close filter
ndb_filter_close(filter)

-- gets the cursor that matches the filter conditions.
local cursor = ndb_cursor_query(query)
if (type(cursor) == "string") then
  -- error
  print(cursor)
  return
end
-- close query
ndb_query_close(query)

-- cursor is target data.
local count = ndb_cursor_count(cursor)
for i = 1, count, 1 do
  result = displayEntity(cursor)
  if (result == true) then
    result = ndb_cursor_next(cursor)
    if (result == false) then
      break
    end
  end
end

-- close cursor
ndb_cursor_close(cursor)

Nesta Pocket スクリプト関数


関数名説明
ndb_createデータベースを作成します。
ndb_findデータベースが存在するか調べます。
ndb_removeデータベースを削除します。
ndb_renameデータベース名を変更します。
ndb_current_dbname現在表示されているデータベース名を取得します。
ndb_field_addフィールドを追加します。
ndb_field_removeフィールドを削除します。
ndb_field_updateフィールドを変更します。
ndb_field_renameフィールド名を変更します。
ndb_field_nameデータベースに定義されている n 番目のフィールド名を取得します。
ndb_cursor_dbカーソルを取得します。
ndb_cursor_queryクエリを実行してカーソルを取得します。
ndb_cursor_current現在表示されているカーソルを取得します。
ndb_cursor_closeカーソルをクローズします。
ndb_cursor_countカーソルの件数を取得します。
ndb_cursor_posカーソルの現在位置を設定します。
ndb_cursor_nextカーソルの現在位置を次に進めます。
ndb_cursor_prevカーソルの現在位置を前に戻します。
ndb_cursor_newカーソルに新しいエンティティを追加します。
ndb_cursor_deleteカーソルの現在位置のエンティティを削除します。
ndb_cursor_delallカーソルのすべてのエンティティを削除します。
ndb_query_createデータを絞り込むためのクエリを作成します。
ndb_query_addクエリに検索フィルタを追加します。
ndb_query_closeクエリをクローズします。
ndb_filter_create検索フィルタを作成します。
ndb_filter_add検索フィルタに条件を追加します。
ndb_filter_close検索フィルタをクローズします。
ndb_property_getフィールドのプロパティを取得します。
ndb_property_getstrプロパティ値を文字列で取得します。
ndb_property_getnumプロパティ値を数値で取得します。
ndb_property_putstrプロパティに文字列を設定します。
ndb_property_putnumプロパティに数値を設定します。
ndb_property_deleteプロパティを現在のエンティティから削除します。
ndb_trx_beginトランザクションを開始します。
ndb_trx_commitトランザクションを確定して終了します。
ndb_trx_rollbackトランザクションを破棄して終了します。

Author: YAMAMOTO Naoki
Create date: 2012/12/23
Last modified: 2014/03/31