C++/Qt Multiline strings; also, multiple queries - c++

Two [hopefully] quick questions regarding C++/Qt. Is the following proper for writing a string on multiple lines?
QString strQuery;
strQuery="\
CREATE TABLE foo\
(bar integer primary key,\
baz varchar(20))";
I believe this is right, but in Qt Creator it doesn't highlight as though it is one big string.
Secondly, will QSqlQuery.exec() run multiple queries in a single execution or does each query need to be run through exec()? For example, I'm trying something like:
QSqlQuery query;
QString strQuery;
strQuery="\
CREATE TABLE foo \
(bar integer primary key,\
baz varchar(10));\
CREATE TABLE herp\
(de integer primary key, \
derp varchar(10))";
query.exec(strQuery);
From what I can see, only that first table is being created. I don't know if this is related to my multiline string, to my database type (SQLite), or just QSqlQuery in general.
Thanks for the help!

I think that will be the right way:
QString strQuery;
strQuery="CREATE TABLE foo " \
"(bar integer primary key, " \
"baz varchar(20));";
// with this style `strQuery` will be single line, like "CREATE TABLE foo (bar integer primary key, baz varchar(20));"
QSqlQuery query;
QString strQuery;
strQuery="CREATE TABLE foo " \
"(bar integer primary key, " \
"baz varchar(10));"
query.exec(strQuery);
strQuery="CREATE TABLE herp " \
"(de integer primary key, " \
"derp varchar(10))";
query.exec(strQuery);

Related

Filtering Lookup for String x++

I had lookup fields (type:int64, Extended from RefRecId) and I had written lookup filtering code for them but I had to convert them into strings ( I had added relations to make them lookup fields) But since they are "String" instead of "reference group" my code doesn't work. How can I filter them when they are 'string'. Here is my code for filtering for reference groups:
[FormControlEventHandler(formControlStr(InventSite, InventSite_MyField), FormControlEventType::Lookup)]
public static void InventSite_MAndEDay_OnLookup(FormControl sender, FormControlEventArgs e)
{
SysReferenceTableLookup tableLookup = SysReferenceTableLookup::newParameters(tableNum(ReferenceTables), sender);
Query query = new Query();
InventSite inventSite;
QueryBuildDataSource qbds = query.addDataSource(tableNum(ReferenceTables));
qbds.addRange(fieldNum(ReferenceTables,ReferenceTablesType )).value(queryValue(ReferenceTablesTypeBaseEnum::MyField));
tableLookup.addLookupField(fieldNum(ReferenceTables, Name ));
tableLookup.addLookupField(fieldNum(ReferenceTables, Description ));
tableLookup.parmQuery(query);
tableLookup.performFormLookup();
Can you please help me with this?
Thanks and regards...
use sysTableLookup instead of SysReferenceTableLookup

Postgres: add description of an ENUM value?

I've got an ENUM column in Postgres 9.6:
CREATE TYPE my_type AS ENUM('foo', 'bar');
I'd like to add a human-readable description for each value in the enum, e.g. for foo, This is the foo value and it does stuff.
Is there any way to do this in Postgres? I'd like something like Django's choices field.
Nothing fancy can be done I think. Standard comment?..
t=# \x
Expanded display is on.
t=# comment on type my_type is 'foo: something fooish, bar: a place to avoid';
COMMENT
t=# \dT+ my_type
List of data types
-[ RECORD 1 ]-----+---------------------------------------------
Schema | public
Name | my_type
Internal name | my_type
Size | 4
Elements | foo +
| bar
Owner | postgres
Access privileges |
Description | foo: something fooish, bar: a place to avoid
with some nerd sniping:
t=# comment on type my_type is '{"foo": "something fooish", "bar": "a place to avoid"}';
COMMENT
t=# select pg_catalog.obj_description(t.oid, 'pg_type')::json->>'foo' from pg_type t where typname = 'my_type';
?column?
------------------
something fooish
(1 row)
Teoretically you can create two type with same dimension and use something like this:
CREATE TYPE my_type AS ENUM('foo', 'bar');
CREATE TYPE my_type_description AS ENUM('foo desc', 'bar desc');
CREATE FUNCTION show_desc(i my_type) RETURNS my_type_description AS $sql$
SELECT ((enum_range(NULL::my_type_description))[array_length(enum_range(NULL, i), 1)])::my_type_description;
$sql$ LANGUAGE SQL STABLE;
SELECT show_desc('foo');
show_desc
-----------
foo desc
(1 row)
SELECT show_desc('bar');
show_desc
-----------
bar desc
(1 row)

How can I get the HBase table name from a Result object as the mapreduce parameter?

HBASE-3996
Support multiple tables and scanners as input to the mapper in map/reduce job.
The map function always looks as follows:
public void map(ImmutableBytesWritable row, Result value, Context context)
In the map function, how can I distinguish which table the (Result)value comes from?
You can extract the TableSplit from the context, this should work for you (not tested):
public void map(ImmutableBytesWritable row, Result value, Context context) {
TableSplit currentSplit = (TableSplit)context.getInputSplit();
byte[] tableName = split.getTableName();
....
}

QSqlField name() method returns ""

I have a class (ServicesTableModel) which inherits from QSqlRelationalTableModel. In the constructor I have:
ServicesTableModel::ServicesTableModel( QWidget* parent, QSqlDatabase db )
: QSqlRelationalTableModel( parent, db )
{
setTable( "servicios" );
select();
...
}
Now, if I place the line
qDebug() << primaryKey();
where the dots are I get
QSqlRecord( 1 )
" 0:" QSqlField("ser_id", int, required: no, generated: yes)
which makes perfect sense, but after
qDebug() << primaryKey().name();
the answer is
""
so I can't reach the name of the primary key.
The table is sqlite, defined with
CREATE TABLE servicios (ser_id integer primary key, ... )
This matters because I'm trying to generalize the class for objects closely related to the rows in a table, and it seems natural not to have to provide the primary key name to the constructor.
I must surely be doing something wrong, or I don't understand what the name()_ method from QSqlField does.
primaryKey.name() returns the name of the index.
In SQL, an index can be named, and this is independent from the name of the field(s) used for the key.
An index can work on several fields.
Each field name can be retrieved with key.fieldName(i), with 0<i<key.count()

CASTING attributes for Ordering on a Doctrine2 DQL Query

I am trying to get Doctrine2 Entities, ordered by their ID which apparently is a String even though it contains only Numbers.
So what I would like to do is something like this:
SELECT entity1, cast (entity1.id AS integer) AS orderId
FROM Namespace\Bla\MyEntity
ORDER BY orderId
Is there a way to do something like this in Doctrine2?
Or, what would be the best practise to get my Result if i can't change the type of the id (due to customer requirements of course)?
Attention: I am not asking SQL Code, i am asking for a Doctrine2 Solution, preferably in DQL
You should be able to add your own function to implement this feature.
The class would look something like this:
namespace MyProject\Query;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class CastAsInteger extends FunctionNode
{
public $stringPrimary;
public function getSql(SqlWalker $sqlWalker)
{
return 'CAST(' . $this->stringPrimary->dispatch($sqlWalker) . ' AS integer)';
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->stringPrimary = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
You'll need to register your function:
$config = $em->getConfiguration();
$config->addCustomNumericFunction('INT', CastAsInteger::class);
Then you can use it:
SELECT e, INT(e.id) AS HIDDEN orderId
FROM Namespace\Bla\MyEntity e
ORDER BY orderId
PS: By adding the HIDDEN keyword, the alias orderId won't be in the results (and is only used for ordering).
Based on Jasper N. Brouwer answer, this is a little bit enhanced solution:
<?php
namespace MyProject\Query;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class Cast extends FunctionNode
{
/** #var \Doctrine\ORM\Query\AST\PathExpression */
protected $first;
/** #var string */
protected $second;
/**
* #param SqlWalker $sqlWalker
*
* #return string
*/
public function getSql(SqlWalker $sqlWalker)
{
return sprintf("CAST(%s AS %s)",
$this->first->dispatch($sqlWalker),
$this->second
);
}
/**
* #param Parser $parser
*
* #return void
*/
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->first = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_AS);
$parser->match(Lexer::T_IDENTIFIER);
$this->second = $parser->getLexer()->token['value'];
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}
Now it should be possible to write DQL like this:
SELECT e, CAST(e.id AS integer) AS HIDDEN orderId FROM Namespace\Bla\MyEntity e ORDER BY orderId
Try this one by with out changing the data type
select (entity1 * 1) as display_value, entity1 as return_value
from Table_Name
order by 1 asc;
Think it's better to use some extra functional in such cases ( without trying "to circumvent" theirs). E.g. an excellent solution adding almost all necessary ( not supported from box ) stuff for Doctrine 2 is DoctrineExtensions by beberlei (github). With it it's possible to use directly CAST-statement like in OP's case:
("Symfony-example") E.g. in your config.xml add lines:
orm:
..
entity_managers:
....
dql:
....
string_functions:
CAST: DoctrineExtensions\Query\Mysql\Cast
Then U can use it like:
SELECT entity1, CAST(entity1.id AS integer) AS orderId
FROM Namespace\Bla\MyEntity
ORDER BY orderId
Not sure if this works, but to access an entity ID you need the IDENTITY() DQL function. Try this:
SELECT entity1 FROM Namespace\Bla\MyEntity ORDER BY IDENTITY(entity1)
I think you want order by entity1. if your entity1 data type is integer then no need to change it into integer or if it is not then you should do it. below is query for you.try this one.
select entity1,cast(entity1 as integer) as order_id from Table_Name order by 1 asc;
I just did something similar in my own code yesterday. I was able to do:
select cast(entity1 as int) as OrderID
from yourtablename
where yourconditions
I had to actually cast mine as money and then int, but if you don't have a decimal, you should not have that issue. You can also try casting as numeric or using convert instead of cast, but cast is better in this situation.
Why do you need entity1 as a column if you already have the same value in OrderID?