Table
Container for semi-structured data
Example
table := Table->New("users", ["name", "age"]);
row := table->Insert();
row->Set("name", "Alice");
row->Set("age", IntRef->New(30));
results := table->Query("select * from users where age > 25");
each(i : results) {
results->Get(i)->ToString()->PrintLine();
};Operations
- New
- Average
- Count
- Delete
- Filter
- FromCsv
- FromFilesystem
- Get
- GetAll
- GetColumnNames
- GetError
- GetIndex
- GetName
- Insert
- Query
- Size
- Sum
- ToString
- Unique
Average #
Gets the column average
method : public : Average(name:String) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
Return
| Type | Description |
|---|---|
| Float | column average |
Example
table := Query.Structured.Table->New("scores", ["score"]);
r1 := table->Insert(); r1->Set("score", IntRef->New(80));
r2 := table->Insert(); r2->Set("score", IntRef->New(90));
avg := table->Average("score"); # returns 85.0
"avg={$avg}"->PrintLine();Delete # native
Delete row by primary key
method : public : native : Delete(pk:Int) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| pk | Int | primary key |
Return
| Type | Description |
|---|---|
| Bool | true if successful, false otherwise |
Example
table := Query.Structured.Table->New("items", ["name"]);
row := table->Insert(); row->Set("name", "Temp");
ok := table->Delete(0); # remove row with pk=0
"deleted={$ok}"->PrintLine();Filter # native
Filters table based upon conditional criteria
method : public : native : Filter(cond:Conditional) ~ Vector<Row>Parameters
| Name | Type | Description |
|---|---|---|
| cond | Conditional | conditional filter |
Return
| Type | Description |
|---|---|
| Vector<Row> | filtered rows |
FromCsv # function
Loads a table from a CSV file
function : FromCsv(table_name:String, path:String) ~ TableParameters
| Name | Type | Description |
|---|---|---|
| table_name | String | table name |
| path | String | to CSV |
Return
| Type | Description |
|---|---|
| Table | new table |
Example
table := Query.Structured.Table->FromCsv("orders", "orders.csv");
if(table <> Nil) {
rs := table->Query("select * from orders where total > 100");
rs->ToString()->PrintLine();
};FromFilesystem # function
Loads a table from a directory file system. Columns are: name, path_name, create_date, owner, is_dir and is_readonly.
function : FromFilesystem(table_name:String, path:String) ~ TableParameters
| Name | Type | Description |
|---|---|---|
| table_name | String | table name |
| path | String | to directory |
Return
| Type | Description |
|---|---|
| Table | new table |
Example
table := Query.Structured.Table->FromFilesystem("files", "/tmp/");
rs := table->Query("select name from files where is_dir = false");
rs->ToString()->PrintLine(); # list only files, not directoriesGet # native
Get row by primary key
method : public : native : Get(pk:Int) ~ RowParameters
| Name | Type | Description |
|---|---|---|
| pk | Int | primary key |
Return
| Type | Description |
|---|---|
| Row | row |
Example
table := Query.Structured.Table->New("items", ["name"]);
row := table->Insert(); row->Set("name", "Beta");
found := table->Get(0); # fetch row with pk=0
found->ToString()->PrintLine();GetAll # native
Get all rows
method : public : native : GetAll() ~ Vector<Row>Return
| Type | Description |
|---|---|
| Vector<Row> | all rows |
Example
table := Query.Structured.Table->New("items", ["name"]);
row := table->Insert(); row->Set("name", "Alpha");
all := table->GetAll(); # retrieve every row
each(i : all) { all->Get(i)->ToString()->PrintLine(); };GetColumnNames #
Gets all column names
method : public : GetColumnNames() ~ String[]Return
| Type | Description |
|---|---|
| String | column names |
GetError #
Gets the last query error
method : public : GetError() ~ StringReturn
| Type | Description |
|---|---|
| String | last query error |
GetIndex # native
Get column index by name
method : public : native : GetIndex(name:String) ~ IntParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
Return
| Type | Description |
|---|---|
| Int | column name |
Example
table := Query.Structured.Table->New("items", ["name", "qty"]);
idx := table->GetIndex("qty"); # returns 2 (pk=0, name=1, qty=2)
"qty column index={$idx}"->PrintLine();GetName #
Gets the table name
method : public : GetName() ~ StringReturn
| Type | Description |
|---|---|
| String | table name |
Example
table := Query.Structured.Table->New("products", ["sku", "price"]);
table->GetName()->PrintLine(); # prints "products"Insert # native
Inserts a new row into the table. After the row has been added its values will need to be set.
method : public : native : Insert() ~ RowReturn
| Type | Description |
|---|---|
| Row | newly inserted row |
Example
table := Query.Structured.Table->New("tasks", ["title", "done"]);
row := table->Insert(); # add empty row
row->Set("title", "Write tests");
row->Set("done", Row->False());
row->ToString()->PrintLine();New # constructor
Constructor
New(name:String, column_names:String[])Parameters
| Name | Type | Description |
|---|---|---|
| name | String | table name |
| column_names | String | column names |
Query #
Query table using SQL-like syntax. Support for 'select', 'from', 'where', 'distinct', 'order by' and logical operators.
method : public : Query(statement:String) ~ TableParameters
| Name | Type | Description |
|---|---|---|
| statement | String | query statement |
Return
| Type | Description |
|---|---|
| Table | result table |
Example
table := Query.Structured.Table->New("emp", ["name", "dept"]);
r := table->Insert(); r->Set("name", "Carol"); r->Set("dept", "Eng");
rs := table->Query("select name from emp where dept = 'Eng'");
rs->ToString()->PrintLine();Sum #
Gets the column sum
method : public : Sum(name:String) ~ FloatParameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
Return
| Type | Description |
|---|---|
| Float | column sum |
Example
table := Query.Structured.Table->New("sales", ["amount"]);
r1 := table->Insert(); r1->Set("amount", FloatRef->New(19.99));
r2 := table->Insert(); r2->Set("amount", FloatRef->New(5.01));
total := table->Sum("amount"); # returns 25.0
"total={$total}"->PrintLine();ToString #
String representation of table
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | all row1 as a string |
Unique #
Gets unique rows by column name
method : public : Unique(name:String) ~ Vector<Row>Parameters
| Name | Type | Description |
|---|---|---|
| name | String | column name |
Return
| Type | Description |
|---|---|
| Vector<Row> | unique rows |
Example
table := Query.Structured.Table->New("sales", ["region"]);
r1 := table->Insert(); r1->Set("region", "East");
r2 := table->Insert(); r2->Set("region", "East"); # duplicate
r3 := table->Insert(); r3->Set("region", "West");
unique := table->Unique("region"); # 2 rows: East, West
"unique regions: {$unique->Size()}"->PrintLine();