Japanese

Nesta Pocket Script Tutorial


What is Nesta Pocket Script ?

The script can be used to process the database of Nesta Pocket.
The dedicated functions are provided to manipulate the database.

Nesta Pocket handles multiple databases as the datastore.
The database is composed of multiple entities. The entity is composed of multiple properties.
The property means the field.
The entity is operated via the cursor. The cursor can be set the search conditions.

The script supports transaction processing. Can be treated as multiple updates.
If the update of the transaction fails, all updates will be returned to the original.

Neste Pocket Script is using the Lua programming language.
Can use the standard functions of the Lua.
Lua version is 5.2.

Script List

List of scripts that have been created are displayed.

Create new script

To create the script, tap the + icon at the top of the screen.

Enter the script name to create.
The script name can be up to 60 characters.

Edit the script

Enter the script code in a text editor.

Simple Example

This is a simple Lua program.

print("Hello World!")

Execute the script

Will be displayed in the Output View When you run the following script.

Lua standard functions

You can also use standard functions of the Lua.

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

Nesta Pocket functions

Function list will be shown when you tap the "f(x)" on the keyboard.
Function will be inserted when you tap the cell.
Description of the function appears when you tap the disclosure button of the cell.

Create Database

Create a new database.
If the database name already exists will result in an error.
Database has been created field is undefined.

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

Remove Database

Remove the database.
If the database exists to remove.

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

New Entity

Add a new entity to create the database.
Field defines the number, name, English score, Mathematics score and Science score.

--
-- 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
--    Number(5), Name, English(3), Mathematics(3), Science(3)
--
function addFields(db)
  local result = ndb_field_add(db, "Number", NDB_NUMBER, 0, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "Name", NDB_TEXT, 0, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "English", NDB_NUMBER, 3, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "Mathematics", NDB_NUMBER, 3, true)
  if (result ~= true) then
    -- error
    print(result)
    return false
  end
  result = ndb_field_add(db, "Science", 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, "Number")
  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, "Name")
  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, "English")
  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, "Mathematics")
  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, "Science")
  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

--
-- Will be executed for script from here.
--
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, "Taro Yamada", 80, 100, 95)
newEntity(cursor, 12002, "Hanako Tanaka", 95, 65, 80)
newEntity(cursor, 12003, "Ichiro Saito", 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)

Calculate the average score

To add average score of English, Mathematics and Science to the new field.

--
-- Function:
--  add average field
--
function addFieldAverage(db)
  ndb_field_remove(db, "Average")
  local result = ndb_field_add(db, "Average", 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, "English")
  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, "Mathematics")
  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, "Science")
  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, "Average")
  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

--
-- Will be executed for script from here.
--
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)

Gets the data to set the search filter

Displays the data of less than average 60 points.

--
-- 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, "Number")
  if (number == false) then
    return false
  end

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

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

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

--
-- Will be executed for script from here.
--
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, "Average", 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 Script functions


NameDescription
ndb_createCreate a new database.
ndb_findFind the database name.
ndb_removeDelete the database.
ndb_renameRename the database.
ndb_current_dbnameGets current database name.
ndb_field_addAdd field to the database.
ndb_field_removeRemove the field in the database.
ndb_field_updateUpdate the field in the database.
ndb_field_renameRename the field in the database.
ndb_field_nameGets the name of the nth field in the database.
ndb_cursor_dbGets the cursor in the database.
ndb_cursor_queryExecute the query.
ndb_cursor_currentGets current cursor.
ndb_cursor_closeClose the cursor.
ndb_cursor_countGets the number of the cursor.
ndb_cursor_posSets the current position of the cursor.
ndb_cursor_nextAdvance the current position of the cursor.
ndb_cursor_prevTo move previous the current position of the cursor.
ndb_cursor_newCursor to create a new entity.
ndb_cursor_deleteDelete the entity of the current position of the cursor.
ndb_cursor_delallDelete all the entities of the cursor.
ndb_query_createCreate a query to filter the data.
ndb_query_addAdd filter to the query.
ndb_query_closeClose the query.
ndb_filter_createCreate the search filter to be added to the query.
ndb_filter_addAdd search condition to the filter.
ndb_filter_closeClose the filter.
ndb_property_getGets the property of the field name.
ndb_property_getstrGet string value of the property.
ndb_property_getnumGet numeric value of the property.
ndb_property_putstrSet string value to the property.
ndb_property_putnumSet numeric value to the property.
ndb_property_deleteDelete the property from the entity.
ndb_trx_beginStart the transaction.
ndb_trx_commitCommit the transaction.
ndb_trx_rollbackRollback the transaction.

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