Skip to content

Qore Programming FAQs

David Nichols edited this page Nov 19, 2020 · 16 revisions

FAQs

Note: Code examples here assume the recommended parse options.

General Qore Programming FAQs

How can I evaluate Qore code from the command-line?

Use the -X option (or -nX for %new-style) to evaluate and expression and then display the output as follows:

prompt$ qore -nX '2016-07-01T15:32:22.357Z'
2016-07-01 15:32:22.357000 Fri Z (UTC)
prompt$ qore -lUtil -nX 'get_random_string()'
"STU6q5mjxnSYnIz"
prompt$ qore -lUtil -nX 'get_random_string(50, 4)'
"OeMd{cT,iaxi4!r-Y2a-X0viAv<<18&+ORZy8c6C;|H<H0s(#T"

Or a more complicated expression:

prompt$ qore -lSqlUtil -nX 'map $1.name, (new Table("pgsql:omquser/omquser@omquser", "vine_object")).describe().iterator()'
list: (5 elements)
  [0]="id"
  [1]="name"
  [2]="typeid"
  [3]="created"
  [4]="modified"

To evaluate a statement or series of statements on the command-line, use -e (or -ne for %new-style) as in the following example:

prompt$ qore -l SqlUtil -ne 'Table t("pgsql:omquser/omquser@omquser", "vine_object"); printf("%y\n", t.selectRow(("columns": cop_count())));'
{count: 45}

How can I output / log a complex data structure (hash / list / etc)?

Use the %y (single-line YAML-like format) or %N (multi-line "node" format) string format placeholders with printf(), sprintf(), or related functions.

For example:

prompt$ qore -nX 'sprintf("%y", {"a": 1, "b": ("two", "three")})'
"{a: 1, b: [\"two\", \"three\"]}"

How do I generate a relative date/time value (duration) from an expression at runtime?

Use one of the following functions:

For example:

# returns a delta based on the argument as a number of 15 minute quanta
date sub get_delta(int i) {
    return minutes(i * 15);
}

See also: Relative Dates

How do I get a date/time value with the time value all zeros?

Use <date>::midnight(): now().midnight()

For example:

prompt% qore -X 'now().midnight()'
2016-06-16 00:00:00.000000 Thu +02:00 (CEST)
prompt% qore -X 'now_utc().midnight()'
2016-06-16 00:00:00.000000 Thu Z (UTC)
prompt% qore -X '(new TimeZone("America/Chicago")).date(now()).midnight()'
2016-06-16 00:00:00.000000 Thu -05:00 (CDT)

SQL and Database FAQs

How can I dump a table's DDL on the command-line?

The sqlutil example program can be used to dump table DDL (as well as DDL for other objects). This program uses the SqlUtil module for its functionality.

For example:

prompt$ sqlutil pgsql:staging/staging@staging -t gsi_uom
-- DDL of tables gsi_uom
create table gsi_uom (
  id numeric not null,
  uom_code varchar(3) not null,
...

To list tables, try:

prompt$ sqlutil pgsql:staging/staging@staging -L
gsi_brep_trans                 tables
gsi_brep_trans2                tables
gsi_business_processes         tables
gsi_business_sub_processes     tables
...

You can also synchronize table data structures and data from one database to another using sqlutil; type sqlutil -h for more information.

How do I generate a query that returns immediately as soon as a single row is found using SqlUtil?

Sometimes you just need to find if there is at least one row matching some given select criteria and return immediately. SqlUtil will generate such SQL for you if you use the AbstractTable::findSingle() method or the "limit" select option with methods that take a select option hash (see the previous link for a list of such methods).

For example:

const SelectCond = {
    "message_type": "PO_INTERNAL_REQ",
    "status": 'N',
};

Table table(ds, "h3g_it_req_trx");
# set to True if there is at least one row matching the SelectCond criteria
bool has_data = exists table.findSingle(SelectCond);

and:

const SelectHash = {
    "where": {
        "message_type": "PO_INTERNAL_REQ",
        "status": 'N',
    },
    "limit": 1,
};

Table table(ds, "h3g_it_req_trx");
# set to True if there is at least one row matching the SelectCond criteria
bool has_data = exists table.selectRow(SelectHash);

Note: you can log the SQL generated with AbstractTable::selectRow() as follows:

string sql;
bool has_data = exists table.selectRow(SelectHash, \sql);
printf("sql: %y, has_data: %y\n", sql, has_data);

with the oracle driver with an Oracle 12c DB, the SQL generated would look like:

select * from staging.h3g_it_brep_trans where message_type = %v and status = %v fetch next %v rows only

with the freetds driver and an MS SQL Server DB, the SQL generated would look like:

select top 1 * from dbo.h3g_it_brep_trans where message_type = %v and status = %v

(the %v characters in the generated SQL examples above are bind by value placeholders)

See also:

Clone this wiki locally