In DAML, how to get ContractId in a choice - daml

In DAML, can I save a contractId of B in contract A? I tried the followings but creating contract returns an update and I cannot save that update anywhere or even access to its data.
template OneContract
with
someParty : Party
someText : Text
someNumber : Int
where
signatory someParty
template Main
with
anyone : Party
cid : ContractId OneContract
where
signatory anyone
controller anyone can
nonconsuming CreateOneContracts : ()
with
p : Party
int : Int
do
-- cid is not bind to cid in the contract
cid <- create OneContract with someParty = p, someText = "same",
someNumber = int
-- let won't work since create returns an update
let x = create OneContract with someParty = p, someText = "same",
someNumber = int
pure()

You've got the right idea with cid <- ..., but that will create a new local variable cid with the contract id in it. All data in DAML is immutable, which means you can't write into this.cid. You have to archive the contract and recreate it to change the data stored on it:
template Main
with
anyone : Party
cid : ContractId OneContract
where
signatory anyone
controller anyone can
CreateOneContracts : ContractId Main
with
p : Party
int : Int
do
newCid <- create OneContract with someParty = p, someText = "same", someNumber = int
create this with cid = newCid
Note that this will only work with anyone == p, though. p's authority is needed to create a OneContract with someParty = p, and the only authority available in the context of the CreateOneContracts choice is that of anyone.

Related

modification of the content on a specific column in mysql. better in a single update?

The name of the card "Rascals" (line 55 of the excel) it will be modified by "Hal Roach's Rascals", and the rerity "Common" it will be "Proletari"
DROP TABLE IF EXISTS Carta;
CREATE TABLE Cards(
id_carta VARCHAR(255),
name VARCHAR(255),
rarity VARCHAR(255),
deck VARCHAR(255),
player VARCHAR(255),
level VARCHAR(255),
PRIMARY KEY(id_carta),
FOREIGN KEY(rarity)REFERENCES Rarity(id_rarity),
FOREIGN KEY(deck)REFERENCES Deck(id_deck),
);
My code is:
UPDATE cards SET cards_name = 'Hal Roachs Rascals' WHERE cards_name = 'Rascals';
UPDATE cards_rarity = 'Proletari' WHERE cards_rarity = 'Common;
My question is if it is correct or it could be do it in a single update?
"when the name of a card is like "RASCALS" I must change that name with "Hal Ro..." and then change only the rarity of this card not all the cards which have "common" rarity"
It is like that:
UPDATE cards SET cards_name = 'Hal Roachs Rascals' AND cards_rarity=’Proletari’ WHERE cards_name = 'Rascals'; ???

SQLAlchmemy — get related objects with reflected tables

I am quite new to sqlalchemy, I guess I am missing just a little piece here.
There is this Database (sql):
create table CEO (
id int not null auto_increment,
name char(255) not null,
primary key(id),
unique(name)
);
create table Company (
id int not null auto_increment,
name char (255) not null,
ceo int not null,
primary key(id),
foreign key(ceo) references CEO(id)
);
That code:
from sqlalchemy import create_engine, Table, Column, Integer, String, ForeignKey
from sqlalchemy.orm import registry, relationship, Session
engine = create_engine(
"mysql+pymysql:xxxxxxxx",
echo=True,
future=True
)
mapper_registry = registry()
Base = mapper_registry.generate_base()
#####################
## MAPPING CLASSES ##
#####################
class CEO(Base):
__table__ = Table('CEO', mapper_registry.metadata, autoload_with=engine)
companies = relationship('Company', lazy="joined")
class Company(Base):
__table__ = Table('Company', mapper_registry.metadata, autoload_with=engine)
##########################
## FINALLY THE QUESTION ##
##########################
with Session(engine, future=True) as session:
for row in session.query(CEO).all():
for company in row.companies:
## Just the id of the Ceo is yielded here
print(company.ceo)
So CEO.companies works as expected, but Company.ceo does not, even though the FOREIGN KEY is defined.
What is a proper setup for the Company Mapper class, such that Company.ceo yields the related object?
I could figure out, that the automatic setup did not work, because the column Company.ceo exists in the Database and represents the ID of a given row. To make everything work, I needed to rename Company.ceo to Company.ceo_id and add the relation manually like so:
CompanyTable = Table('Company', Base.metadata, autoload_with=engine)
class Company(Base):
__table__ = CompanyTable
ceo_id = CompanyTable.c.ceo
ceo = relationship('CEO')
I would like to know if it would be possible to rename the column within the Table(…) call, such that I could get rid of the extra CompanyTable thing.

Parameter index out of range with Multiple Join and Optional input parameter

I have an issue with the multiple Join using Spring JdbcTemplate if I need to make optional the input parameters.
This is the scenario.
The SQL table where i must perform the Join are:
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15),
password VARCHAR(20),
info VARCHAR(30),
active BOOLEAN,
locked BOOLEAN,
lockout_duration INT DEFAULT 0,
lockout_limit DATETIME,
login_attempts INT DEFAULT 0,
PRIMARY KEY(id)
);
CREATE TABLE profiles (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(15),
info VARCHAR(30),
PRIMARY KEY(id)
);
CREATE TABLE profiling(
user_id INT NOT NULL,
profile_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (profile_id) REFERENCES profiles(id) ON DELETE CASCADE,
PRIMARY KEY(user_id,profile_id)
);
Where profiling is the table to associate a user with his profile; the profile must be intended as the identification of what actions are permitted to users.
In one my precedent post, i ask how to make optional the sql parameters and I obtained a perfect response, that works and i have always used since then. So, if i need to make this, is put in the where:
WHERE (is null or variabile_name = ?)
And, using jdbctemplate, i write:
jdbcTemplate.query(SQL, new Object[]{variabile_name, variabile_name}, mapper_name);
where, of course, SQL is the String object where i make the Query.
So, i make this also in this case, but i got the error:
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
I report the full method here:
/**
* This metod return the join between users and profiles
* made using the profiling table
*
* #param userID the user id code
* #param profilesID the profile id code
* #return the join list
*/
public List<Profiling> joinWithUsersandProfiles(Integer userID, Integer profileID)
{
//This is the mapper for Profiling
RowMapper<Profiling> profilingMapper = new RowMapper<Profiling>()
{
//This method must be implemented when we use a row mapper
public Profiling mapRow(ResultSet rs, int rowNum) throws SQLException
{
Profiling profiling = new Profiling();
profiling.setUser_id(rs.getInt("profiling.user_id"));
profiling.setProfile_id(rs.getInt("profiling.profile_id"));
//mapping users variabiles
profiling.setUsersId(rs.getInt("users.id"));
profiling.setUsersActive(rs.getBoolean("users.active"));
profiling.setUsersInfo(rs.getString("users.info"));
profiling.setUsersLocked(rs.getBoolean("users.locked"));
profiling.setUsersLockoutDuration(rs.getInt("users.lockout_duration"));
profiling.setUsersLockoutLimit(rs.getTime("users.lockout_limit"));
profiling.setUsersLoginAttempts(rs.getInt("users.login_attempts"));
profiling.setUsersName(rs.getString("users.name"));
profiling.setUsersPassword(rs.getString("users.password"));
//mapping profiles variabiles
profiling.setProfilesId(rs.getInt("profiles.id"));
profiling.setProfilesInfo(rs.getString("profiles.info"));
profiling.setProfilesName(rs.getString("profiles.name"));
return profiling;
}
};
/**
* This is the string that contain the query to obtain the data from profiling table.
* Please note that the notation "? is null or x = ?" means that the x variabile is optional;
* it can be asked as no making the query.
* If we give alla input equals null, it means that we must perform a SQl Select * From table.
*/
String SQL = "SELECT * FROM profiling JOIN users ON profiling.user_id = users.id JOIN profiles ON profiling.profile_id = profiles.id WHERE (is null or profiling.user_id = ?) AND (is null or profiling.profile_id = ?)";
/**
* The list containing the results is obtained using the method query on jdcbtemplate, giving in in input to it the query string, the array of object
* containing the input variabile of the method and the rowmapper implemented.
*/
List<Profiling> theProfilings = jdbcTemplate.query(SQL, new Object[]{userID, userID, profileID, profileID}, profilingMapper);
return theProfilings;
}
I know that the problem is made by the optional variabile. Why? if i try to remove the optional code and pass from:
(is null or variabile_name = ?)
to
variabile_name = ?
The code work perfectly.
So, what's my error here?
Edit: solved myself. I forgot the ? BEFORE the "is null" code. So, passing to:
(? is null or variabile_name = ?)
The code works.

How to update a part of JSON string in Oracle 12c?

I've a table in my oracle DB as below.
CREATE TABLE my_table(
id RAW(16) NOT NULL,
data CLOB,
CONSTRAINT my_table_pk PRIMARY KEY (id),
CONSTRAINT my_table_json_chk CHECK (data IS JSON)
);
INSERT INTO my_table (id, data)
VALUES (SYS_GUID(),
'{
"FirstName" : "aa",
"LastName" : "bb",
"Address" : {
"Street" : "99 My Street",
"City" : "My City",
"Country" : "UK",
"Postcode" : "A12 34B"
}');
Now I know, I can fetch value of a specific property like address of the JSON string using $.
Similarly, can I update Street property of the JSON string without providing the entire JSON structure in my update query?
Please help me on this.
This is supported via PL/SQL in 12.2.0.1.0. New PL/SQL objects enable fine grained manipulation of JSON content
JSON_OBJECT_T: for working with JSON objects
JSON_ARRAY_T: for working with JSON Arrays
JSON_OBJECT_T and JSON_ARRAY_T are subtypes of JSON_ELEMENT_T
These objects provide a set of methods for manipulating JSON similar to GSON.
enter code here
WITH FUNCTION updateTax(JSON_DOC in VARCHAR2 ) RETURN VARCHAR2
IS
jo JSON_OBJECT_T;
price NUMBER;
taxRate NUMBER;
BEGIN
jo := JSON_OBJECT_T(JSON_DOC);
taxRate := jo.get_Number('taxRate');
price := jo.get_Number('total');
jo.put('totalIncludingTax', price * (1+taxRate));
RETURN jo.to_string();
END;
ORDERS as (
select '{"taxRate":0.175,"total":10.00}' JSON_DOCUMENT
from dual
)
select JSON_DOCUMENT, updateTax(JSON_DOCUMENT)
from ORDERS;
JSON_DOCUMENT UPDATETAX(JSON_DOCUMENT)
------------------------------- --------------------------------------------------------
{"taxRate":0.175,"total":10.00} {"taxRate":0.175,"total":10.00,"totalIncludingTax":11.75}
https://docs.oracle.com/database/122/ADJSN/using-PLSQL-object-types-for-JSON.htm#ADJSN-GUID-F0561593-D0B9-44EA-9C8C-ACB6AA9474EE

How do I update a SQL row given an ID with LINQ to SQL?

Given this schema:
Fruits
- FruitID INT PK
- FruitName NVARCHAR(30)
- FruitStatusID INT NULL FK: Statuses
Statuses
- StatusID INT PK
- StatusName NVARCHAR(30)
If I have a FruitID in hand (just an int, not a Fruit object), how do I update the FruitName and null out FruitStatusID without loading the Fruit object from the database first?
Note: this solution gets me pretty far, but I can't figure out how to null out a FK column.
Answers in C# or VB, thanks!
This works but seems unnecessarily complicated:
''//initialize the values I'm going to null out to something
Dim Tag As Data_Tag = New Data_Tag() With {
.Data_Tag_ID = DataTagID,
.Last_Error_DateTime = New DateTime(),
.Last_Error_Message = "",
.Last_Error_Severity_Type_ID = -1 }
''//start change tracking
DB.Data_Tags.Attach(Tag)
''//record changes to these properties (must be initialized above)
Tag.Last_Error_DateTime = Nothing
Tag.Last_Error_Message = Nothing
Tag.Last_Error_Severity_Type_ID = Nothing
DB.SubmitChanges()
Surely there's a better way!
(note: the weird comment syntax is solely for the code highliger--it doesn't like VB-style comments)