-
Notifications
You must be signed in to change notification settings - Fork 10
Qore Programming FAQs
FAQs
Note: Code examples here assume the recommended parse options.
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}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\"]}"
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
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)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.
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
oracledriver 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 onlywith the
freetdsdriver 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
%vcharacters in the generated SQL examples above are bind by value placeholders)
See also: