How to use Last inserted id of a Table in Another Table - c++

I have searched and realized i can use SCOPE but am not sure how to use it. Any help will be appreciated
This is Options insert statement
char sql[256];
sprintf_s(sql, "INSERT INTO Options[Value],[ValuesCorrect],[QuestionId]) VALUES ('%s', '%d', '%d'); "
, choice->getValue()
, choice->getIsAnswer()
, choice->getQuestionId());
pRecordSet->Open(sql, pConnection.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
This is my my Question Table
char sql[256];
"DECLARE #ID = BIGINT";
sprintf_s(sql, "INSERT INTO Questions([Query],[CompetencyLevel],[TopicId]) VALUES('%s', %d, %d); "
,(const char*)question->getQuery()
, question->getCompetencyLevel()
,question->getTopicId());
pRecordSet->Open(sql, pConnection.GetInterfacePtr(), adOpenForwardOnly, adLockReadOnly, adCmdText);
"SELECT#ID = SCOPE_IDENTITY();";

The following query will return the inserted id
INSERT INTO Options (
[Value]
,[ValuesCorrect]
,[QuestionId]
)
OUTPUT inserted.[YourIdColumnName]
VALUES (
'%s'
,'%d'
,'%d'
)
A long time since I used ADO but the code could look something like
pRecordSet->Open(...);
auto id = pRecordSet->Fields->Item[0]->Value;

Related

Does QT have reflections for C++?

I want to create a SQL table in QT C++. So I have made this code.
And it is going to create a database for me, where the first argument tableName is the name of the table I want to create. Then the next argument is quite tricky.
Here, columns, specify the column name and it's data type. I think this is a bad way to do. Example
QVector<QPair<QString, QString>> myColumns = new QVector<QPair<QString, QString>>{{"ID", "BIGINT"}, {"logging_id", "INT"}};
Because If i have for example like 50 columns. The myColumns is going to be very long.
My question if QT C++ have some kind of reflections, so I can:
Get the name if every field
Get the data type of every field
If the field is an array, then I'm going to know how many elements there are inside that array
I was planning to have an entity class where I create a class, and use that class to get the information to create each columns in the database.
void Database::createTable(QString tableName, const QVector<QPair<QString, QString>> columns){
QSqlQuery query;
for (int i = 0; i < columns.length(); i++){
/* Get the Qpair values */
QString columnName = columns.at(i).first;
QString dataType = columns.at(i).second;
/* If key is ID, then try to create a new table */
if(columnName == "ID"){
query.exec("CREATE TABLE " + tableName + "(" + columnName + " " + dataType + " NOT NULL AUTO_INCREMENT PRIMARY KEY)");
continue;
}
/* If not, then try append new columns */
query.exec("ALTER TABLE " + tableName + " ADD " + columnName + " " + dataType);
}
}

Doctrine QueryBuilder - Struggling with table 'state' variable

I have the following query and the last part of it is to check the state of the item which will be 1 or 0;
My api calls:
http://example.com/api/search?keyword=someword&search_for=item&return_product
The query works as expected, except for one thing. Some of the stone items are disabled and I need to ignore where:
->where('S.state=:state')
->setParameter('state' , 1 )
I am not quite sure where to add this to the current query to get it to work:
$qb = $this->stoneRepository->createQueryBuilder('S');
//Get the image for the item
$qb->addSelect('I')
->leftJoin('S.image' , 'I');
//Check if we want products returned
if ( $return_product )
{
$qb->addSelect('P','PI')
->leftJoin('S.product' , 'P')
->leftJoin('P.image' , 'PI');
}
//Check is we want attributes returned
if ( $return_attribute )
{
$qb->addSelect('A','C')
->leftJoin('S.attribute' , 'A')
->leftJoin('A.category' , 'C');
}
//Check the fields for matches
$qb->add('where' , $qb->expr()->orX(
$qb->expr()->like('S.name' , ':keyword'),
$qb->expr()->like('S.description' , ':keyword')
)
);
//Set the search item
$qb->setParameter('keyword', '%'.$keyword.'%');
$qb->add('orderBy', 'S.name ASC');
Just after the createQueryBuilder call:
$qb = $this->stoneRepository
->createQueryBuilder('S')
->where('S.state = :state')
->setParameter('state', 1);
With the query builder the order is not important: you can add SQL pieces in different order and even override pieces already added.

How to Compare my sql columns dynamically

I am not able to get idea about the following requirement. The example table follows.
CREATE TABLE `test` (
`Id` INT NOT NULL,
`Name` VARCHAR(45) NULL,
`did_fk` INT NULL,
`adid_fk` INT NULL,
PRIMARY KEY (`Id`));
INSERT INTO test (id,name,did_fk,adid_fk)
VALUES
(1,'Rajesh',1,1),
(2,'Neeli',2,2),
(3,'Satish',3,3),
(4,'Ganesh',4,5),
(5,'Murali',9,10);
Here I need to compare the "id" with _fk columns i.e. did_fk & adid_fk. The "id" should be equal to did_fk & as well as adid_fk. If any of them is not true, then I should get that row.Here I need to get the rows 4 & 5.Since "_fk" columns are not equal to "id" value.Problem is "_fk" columns are not fixed. But "id" name is fixed.
SELECT * FROM `test` WHERE `Id` != `did_fk` OR `Id` != `adid_fk`
If your dynamic columns ends with _fk or some another suffix you can try to create SP like following
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetNonEqualFkValues`(IN tableName varchar(255))
BEGIN
DECLARE c_name VARCHAR(255);
DECLARE done INT DEFAULT FALSE;
DECLARE curs CURSOR FOR select column_name from information_schema.columns where column_name like '%_fk';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
SET #q = concat("SELECT * FROM ", tableName, " WHERE 1!=1 ");
get_col: LOOP
FETCH curs INTO c_name;
IF done THEN
LEAVE get_col;
END IF;
SET #q = CONCAT(#q, " OR ", c_name," != id");
END LOOP get_col;
PREPARE stmt1 FROM #q;
EXECUTE stmt1;
END
And then invoke for concrete table like
call GetNonEqualFkValues('test')
The code isn't perfect, but it works for me and I think idea should be clear.

Insert main table's primary key into child table multiple times

I have MS Access 2007 database with the following schema:
Main table Object< # Object_PK, ... >
Child table Electric_Consumption< $ Object_PK, # Electric_Consumption_PK, ... >
Child table Water_Consumption< $ Object_PK, # Water_Consumption_PK, ... >
Child table Educational_Object< $ Object_PK, # Educational_Object_PK, ... > which has child tables defined like this:
School< $ Educational_Object_PK, # School_PK, ... >
University< $ Educational_Object_PK, # University_PK, ... >
Here is the picture that should make things clearer:
I use ADO and C++ to insert data.
First I need to enter data for main table Object. I can successfully do that with INSERT query.
My problem is following:
After the above operation I need to insert Object's primary key into child tables, since it is their foreign key.
Allow me to describe exactly what I need so community can help me:
As I said, first I insert data into main table Object.
Then I need to insert data and Object's primary key into child tables.
Browsing through Internet I have found ##IDENTITY that might help me but I do not know if it works for my case.
To make things harder, this will be done in for loop ( the value of the Object_PK is the same in every INSERT and is equal to the value of the last inserted record for the Object ) , something like this:
for ( //... )
L"INSERT INTO Electric_Consumption ( Object_PK, field1, field2 ... )
values ( Object_pk // should I use here ##IDENTITY ? );
Then the same thing should be repeated for tables Water_Consumption and Educational_Object.
After I finish this, I need to add data in the Educational_Object's child tables.
The same as above, only instead of Object_PK I need to add Educational_Object_PK.
Here is the pseudo-code to clarify things better:
L"INSERT INTO Object ( ... ) values ( ... ); //this is OK
for ( ... )
L" INSERT INTO Electric_Consumption ( Object_PK, ... )
values ( Object_PK, ... )"; // should I use ##IDENTITY here
// to get Object_PK ??
for ( ... )
L" INSERT INTO Water_Consumption ( Object_PK, ... )
values ( Object_PK, ... )"; // should I use ##IDENTITY here
// to get Object_PK ??
for ( ... )
L" INSERT INTO Educational_Object ( Object_PK, ... )
values ( Object_PK, ... )"; // should I use ##IDENTITY here
// to get Object_PK ??
for ( ... )
L" INSERT INTO School ( Educational_Object_PK, ... )
values ( Educational_Object_PK, ... )";// should I use ##IDENTITY here
// to get Educational_Object_PK ??
for ( ... )
L" INSERT INTO University ( Educational_Object_PK, ... )
values ( Educational_Object_PK, ... )";// should I use ##IDENTITY here
// to get Educational_Object_PK ??
Can you please tell me which SQL statement to use for this, and demonstrate how to use it by providing a small pseudo code?
I understand that my description of the problem might be confusing so if you need further clarification leave a comment and I will edit my post.
Thank you.
Best regards.
Yes, you want to use SELECT ##IDENTITY as a multiuser-safe way to retrieve the most recently-created AutoNumber (sometimes called "IDENTITY") value. The things to remember are:
You execute a SELECT ##IDENTITY query immediately after you perform the INSERT on the parent table.
You store the returned value in a Long Integer variable.
You use the variable to populate the Foreign Key values in the child table(s).
The following is VBA code, but you can treat it as pseudo-code:
Dim lngObject_PK As Long, lngEducational_Object_PK As Long
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO [Object] ([Description]) VALUES (?)"
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new Object")
cmd.Execute
Set cmd = Nothing
Set rst = New ADODB.Recordset
rst.Open "SELECT ##IDENTITY", con, adOpenStatic, adLockOptimistic
lngObject_PK = rst(0).Value
rst.Close
Set rst = Nothing
Debug.Print "Object_PK of newly-created Object record: " & lngObject_PK
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO [Electric_Consumption] ([Object_PK],[Description]) VALUES (?,?)"
cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput, , lngObject_PK)
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new Electric_Consumption")
cmd.Execute
Set cmd = Nothing
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO [Educational_Object] ([Object_PK],[Description]) VALUES (?,?)"
cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput, , lngObject_PK)
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new Educational_Object")
cmd.Execute
Set cmd = Nothing
Set rst = New ADODB.Recordset
rst.Open "SELECT ##IDENTITY", con, adOpenStatic, adLockOptimistic
lngEducational_Object_PK = rst(0).Value
rst.Close
Set rst = Nothing
Debug.Print "Educational_Object_PK of newly-created Educational_Object record: " & lngEducational_Object_PK
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandText = "INSERT INTO [School] ([Educational_Object_PK],[Description]) VALUES (?,?)"
cmd.Parameters.Append cmd.CreateParameter("?", adInteger, adParamInput, , lngEducational_Object_PK)
cmd.Parameters.Append cmd.CreateParameter("?", adVarWChar, adParamInput, 255, "my new School")
cmd.Execute
Set cmd = Nothing
If the Object_PK is predictable, such as you are using an Autonumber field, you could first determine the next key by something like:
SELECT Max([Object_ID]+1) AS NewKey
FROM ObjectTable;
then use that for all of the other tables (or simply retrieve the MAX key value after storing the Object); How is the primary key defined?

PQexecParams in C++, query error

I'm using pqlib with postgresql version 9.1.11
I have the following code
const char *spid = std::to_string(pid).c_str();
PGresult *res;
const char *paramValues[2] = {u->getID().c_str(), spid};
std::string table;
table = table.append("public.\"").append(Constants::USER_PATTERNS_TABLE).append("\"");
std::string param_name_pid = Constants::RELATION_TABLE_PATTERN_ID;
std::string param_name_uid = Constants::RELATION_TABLE_USER_ID;
std::string command = Constants::INSERT_COMMAND + table + " (" + param_name_uid + ", " + param_name_pid + ") VALUES ($1, $2::int)";
std::cout << "command: " << command << std::endl;
res = PQexecParams(conn, command.c_str(), 2, NULL, paramValues, NULL, NULL,0);
Where
INSERT_COMMAND = "INSERT INTO " (string)
USER_PATTERN_TABLE = "User_Patterns" (string)
RELATION_TABLE_PATTERN_ID = "pattern_id" (string)
RELATION_TABLE_USER_ID = "user_id" (string)
pid = an int
u->getID() = a string
conn = the connection to the db
The table "User_Patterns" is defined as
CREATE TABLE "User_Patterns"(
user_id TEXT references public."User" (id) ON UPDATE CASCADE ON DELETE CASCADE
,pattern_id BIGSERIAL references public."Pattern" (id) ON UPDATE CASCADE
,CONSTRAINT user_patterns_pkey PRIMARY KEY (user_id,pattern_id) -- explicit pk
)WITH (
OIDS=FALSE
);
I already have a user and a pattern loaded into their respective tables.
The command generated is :
INSERT INTO public."User_Patterns" (user_id, pattern_id) VALUES ($1, $2::int)
I also tried with $2, $2::bigint, $2::int4
The problem is:
I receive the error :
ERROR: invalid input syntax for integer: "public.""
I already use PQexecParams to store users and patterns, the only difference is that they all have text/xml fields (the only int field on patterns is a serial one and I don't store that value myself) but because the user_patterns is a relation table I need to store and int for the pattern_id.
I already read the docs for pqlib and saw the examples, both are useless.
The problem is in the lines:
const char *spid = std::to_string(pid).c_str();
const char *paramValues[2] = {u->getID().c_str(), spid};
std::to_string(pid) creates temporary string and .c_str() returns a pointer to an internal representation of this string, which is destroyed at the end of the line, resulting in a dead pointer. You may also see answer to the question
stringstream::str copy lifetime