high-cities-sorted3.taq

NOTE: This program references the “cities” database created by cities.taq.

$ java -jar taq.jar high-cities-sorted3

Running query sort_cities in global scope`

$ java -jar taq.jar show-high-cities

Exported cities:

1   denver,5280
2   flagstaff,6970
3   addis ababa,8000
4   leadville,10200

Description

high-cities-sorted3.taq demonstrates two database resources sharing an entity class used to define the database records. This program depends on running cities.taq first.

The “sort_cities” query takes a list of cities read from one database and writes to another database the high cities in order of ascending altitude. The database records are revealed by a separate show-high-cities.taq program using a custom SQL resource provider. Each record id is shown to verify the sort took place.

Note that the data source role qualifier has the “City” entity class and -> right arrow placed in front of it.

“entities_axioms.City” ->

axiom city(altitude, name)


resource data_source : "cities"
(database="db/cities", type="H2")
{
"entities_axioms.City" ->
axiom city(altitude, name)
}
(
data_source.set(user="sa", password="secret?")
)

resource data_consumer : "sort_cities"
(database="db/sorted-cities", type="H2")
{
list global.insert_sort.high_cities ->
"entities_axioms.City"
}
(
data_consumer.drop_tables()
)

// Performs insert sort on high_cities using a cursor
flow insert_sort
{
list<axiom> high_cities,
cursor city(high_cities)
}
(
// Filter out cities below 5,000 feet
altitude? > 5000,
// Append next city from source
high_cities += axiom high_city { name, altitude },
// Point cursor at city just inserted
-city,
// Save city and point to preceding city in list
inserted = city--,
// Skip case only one city is in the list
? fact city,
// Remember start position
start = city.index(),
// Shuffle list until sort order restored
{
? altitude < city->altitude,
city[1] = city--,
? fact city
},
// All done if inserted city was in sort order
?: city.index() == start,
// Place latest city in correct position
city[1] = inserted
)

query sort_cities(city : insert_sort)