How can we create users with the same email but not the same workflow? - sql-server

I want to make it possible to create several users, even with the same email address as long as they do not have the same "IdWorkflow" number.
But obviously it doesn't work... result.Succeeded is always false. & all I have is that message : "User name 'test#test.com' is already taken."
What can I do...?
Register.cshtml.cs
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, IdWorkflow = Input.IdWorkflow };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ConfirmEmail",
pageHandler: null,
values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
protocol: Request.Scheme);
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
if (_userManager.Options.SignIn.RequireConfirmedAccount)
{
return RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl });
}
else
{
await _signInManager.SignInAsync(user, isPersistent: false);
return LocalRedirect(returnUrl);
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
AspNetUsers.sql
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (450) NOT NULL,
[UserName] NVARCHAR (256) NULL,
[NormalizedUserName] NVARCHAR (256) NULL,
[Email] NVARCHAR (256) NULL,
[NormalizedEmail] NVARCHAR (256) NULL,
[EmailConfirmed] BIT NOT NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[SecurityStamp] NVARCHAR (MAX) NULL,
[ConcurrencyStamp] NVARCHAR (MAX) NULL,
[PhoneNumber] NVARCHAR (MAX) NULL,
[PhoneNumberConfirmed] BIT NOT NULL,
[TwoFactorEnabled] BIT NOT NULL,
[LockoutEnd] DATETIMEOFFSET (7) NULL,
[LockoutEnabled] BIT NOT NULL,
[AccessFailedCount] INT NOT NULL,
[DateCreate] DATETIME2 (7) DEFAULT ('0001-01-01T00:00:00.0000000') NOT NULL,
[DateUpdate] DATETIME2 (7) DEFAULT ('0001-01-01T00:00:00.0000000') NOT NULL,
[IdWorkflow] NVARCHAR (450) NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
ON [dbo].[AspNetUsers]([NormalizedUserName] ASC, [IdWorkflow] ASC) WHERE ([NormalizedUserName] IS NOT NULL);

Related

Produce XML from MS SQL, structure issues, namespace prefix:

I'm trying to run SQL for complex XML out of MS SQL Server 2016. I made a huge progress considering that I'm new to XML generation but still can't figure out how to do that nesting portion to make Export/Client structure matter what I tried putting into nested ROOT clause. Not sure if this issue also causing that I'm missing hmis: prefix for most Elements. I need them like on attached picture with Desired output/schema.
Also pasting self containing test Input and working code , I marked with ???? places which I think caused this trouble. Appreciate your hints. Do you think it's will be easy to do with other types FOR XML ? Explicit ???
Best Mario
SQL Version: Microsoft SQL Server 2017 (RTM-CU27)
Updated: added #export table
/* --- test data/table
DROP TABLE IF EXISTS #t;
SELECT * INTO #T FROM ( -- SELECT * FROM #T
SELECT 111 PersonalID, 'Alpha' first_name, 'Brown' last_name, '1/1/2000' birth_date, 'Manager Alpha' CaseManager_PH, 0 Female, '3/2/2022' ExportDate, 'AW3' user_updated UNION
SELECT 222 PersonalID, 'Bobby' , 'Dow' , '2/2/2002', 'Manager2222' , 0 , '3/3/2022' ExportDate, 'BBX3' ) A
SELECT * FROM #T
*/
DECLARE #export TABLE (
ExportDate date , StartDate DATE, EndDate date)
INSERT INTO #export (ExportDate, StartDate, EndDate)
VALUES ('3/22/2022', '1/1/2022', '4/4/2022')
; WITH XMLNAMESPACES ('https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd' as hmis,
'http://www.w3.org/2001/XMLSchema-instance' AS xsi )
SELECT
10 AS [hmis:SourceID],
111 AS [hmis:Export/ExportID]
,CAST(e.ExportDate AS VARCHAR(10)) AS [hmis:Export/ExportDate] ---<<< Change
,CAST(e.StartDate AS VARCHAR(10)) AS [hmis:Export/ExportPeriod/StartDate]
,CAST(e.EndDate AS VARCHAR(10)) AS [hmis:Export/ExportPeriod/EndDate]
, (
SELECT
ExportDate AS [hmis:Client/#DateCreated], ExportDate AS [hmis:Client/#dateUpdated],
PersonalID AS [hmis:Client/PersonalID],
first_name AS [hmis:Client/first_name],
last_name AS [hmis:Client/last_name],
birth_date AS [hmis:Client/birth_date],
CaseManager_PH AS [hmis:Client/CustomClientElements/CaseManager_PH],
'Unknown' AS [hmis:Client/CustomClientElements/Casemanager_ContactInfo],
user_updated AS [hmis:Client/user_updated]
FROM #t t
-- WHERE 1=1
FOR XML path , ROOT('Export'), TYPE) ---????
FROM #export e
FOR XML PATH('Source'), ROOT('Sources')
Desired output format:
<hmis:Sources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hmis="https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd">
<hmis:Source>
<hmis:SourceID>10</hmis:SourceID>
<hmis:Export>
<ExportID>111</ExportID>
<ExportDate>2022-03-22</ExportDate>
<ExportPeriod>
<StartDate>2022-01-01</StartDate>
<EndDate>2022-04-04</EndDate>
</ExportPeriod>
<hmis:Client DateCreated="3/2/2022" DateUpdated="3/2/2022">
<hmis:PersonalID>111</hmis:PersonalID>
<hmis:first_name>Alpha</hmis:first_name>
<hmis:last_name>Brown</hmis:last_name>
<hmis:birth_date>1/1/2000</hmis:birth_date>
<hmis:CustomClientElements>
<hmis:CaseManager_PH>Manager Alpha</hmis:CaseManager_PH>
<hmis:Casemanager_ContactInfo>Unknown</hmis:Casemanager_ContactInfo>
</hmis:CustomClientElements>
<hmis:user_updated>AW3</hmis:user_updated>
</hmis:Client>
<hmis:Client DateCreated="3/3/2022" DateUpdated="3/3/2022">
<hmis:PersonalID>222</hmis:PersonalID>
<hmis:first_name>Bobby</hmis:first_name>
<hmis:last_name>Dow</hmis:last_name>
<hmis:birth_date>2/2/2002</hmis:birth_date>
<hmis:CustomClientElements>
<hmis:CaseManager_PH>Manager2222</hmis:CaseManager_PH>
<hmis:Casemanager_ContactInfo>Unknown</hmis:Casemanager_ContactInfo>
</hmis:CustomClientElements>
<hmis:user_updated>BBX3</hmis:user_updated>
</hmis:Client>
</hmis:Export>
</hmis:Source>
</hmis:Sources>
Please try the following solution.
The desired output is produced in two steps:
Raw XML via FOR XML PATH('r'), TYPE, ROOT('root').
Fine tuned final XML via XQuery .query() method and FLWOR expression.
Because a minimal reproducible example is not provided, I hope I didn't miss anything.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (
PersonalID INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
birth_date DATE,
CaseManager_PH VARCHAR(30),
Female BIT,
ExportDate DATE,
user_updated VARCHAR(30)
);
INSERT INTO #tbl (
PersonalID,
first_name,
last_name,
birth_date,
CaseManager_PH,
Female,
ExportDate,
user_updated
)
VALUES
(111, 'Alpha', 'Brown', '2000-01-01', 'Manager Alpha', 0, '2022-03-02', 'AW3'),
(222, 'Bobby', 'Dow', '2002-02-02', 'Manager2222', 0 , '2022-03-03', 'BBX3');
DECLARE #export TABLE (ExportDate date , StartDate DATE, EndDate date);
INSERT INTO #export (ExportDate, StartDate, EndDate) VALUES
('2022-03-22', '2022-01-01', '2022-04-04');
-- DDL and sample data population, end
DECLARE #ExportDate date, #StartDate DATE, #EndDate DATE;
SELECT #ExportDate = ExportDate, #StartDate = StartDate, #EndDate = EndDate
FROM #export;
WITH XMLNAMESPACES ('https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd' as hmis,
'http://www.w3.org/2001/XMLSchema-instance' AS xsi )
SELECT (
SELECT * FROM #tbl
FOR XML PATH('r'), TYPE, ROOT('root'))
.query('<hmis:Sources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<hmis:Source>
<hmis:SourceID>10</hmis:SourceID>
<hmis:Export>
<ExportID>111</ExportID>
<ExportDate>{sql:variable("#ExportDate")}</ExportDate>
<ExportPeriod>
<StartDate>{sql:variable("#StartDate")}</StartDate>
<EndDate>{sql:variable("#EndDate")}</EndDate>
</ExportPeriod>
{
for $x in /root/r
return <hmis:Client DateCreated="{$x/ExportDate}" DateUpdated="{$x/ExportDate}">
<hmis:PersonalID>{data($x/PersonalID)}</hmis:PersonalID>
<hmis:first_name>{data($x/first_name)}</hmis:first_name>
<hmis:last_name>{data($x/last_name)}</hmis:last_name>
<hmis:birth_date>{data($x/birth_date)}</hmis:birth_date>
<hmis:CustomClientElements>
<hmis:CaseManager_PH>{data($x/CaseManager_PH)}</hmis:CaseManager_PH>
<hmis:Casemanager_ContactInfo>Unknown</hmis:Casemanager_ContactInfo>
</hmis:CustomClientElements>
<hmis:user_updated>{data($x/user_updated)}</hmis:user_updated>
</hmis:Client>
}
</hmis:Export>
</hmis:Source></hmis:Sources>');
Output
<hmis:Sources xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hmis="https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd">
<hmis:Source>
<hmis:SourceID>10</hmis:SourceID>
<hmis:Export>
<ExportID>111</ExportID>
<ExportDate>0</ExportDate>
<ExportPeriod>
<StartDate>0</StartDate>
<EndDate>0</EndDate>
</ExportPeriod>
<hmis:Client DateCreated="2022-03-02" DateUpdated="2022-03-02">
<hmis:PersonalID>111</hmis:PersonalID>
<hmis:first_name>Alpha</hmis:first_name>
<hmis:last_name>Brown</hmis:last_name>
<hmis:birth_date>2000-01-01</hmis:birth_date>
<hmis:CustomClientElements>
<hmis:CaseManager_PH>Manager Alpha</hmis:CaseManager_PH>
<hmis:Casemanager_ContactInfo>Unknown</hmis:Casemanager_ContactInfo>
</hmis:CustomClientElements>
<hmis:user_updated>AW3</hmis:user_updated>
</hmis:Client>
<hmis:Client DateCreated="2022-03-03" DateUpdated="2022-03-03">
<hmis:PersonalID>222</hmis:PersonalID>
<hmis:first_name>Bobby</hmis:first_name>
<hmis:last_name>Dow</hmis:last_name>
<hmis:birth_date>2000-01-01</hmis:birth_date>
<hmis:CustomClientElements>
<hmis:CaseManager_PH>Manager2222</hmis:CaseManager_PH>
<hmis:Casemanager_ContactInfo>Unknown</hmis:Casemanager_ContactInfo>
</hmis:CustomClientElements>
<hmis:user_updated>BBX3</hmis:user_updated>
</hmis:Client>
</hmis:Export>
</hmis:Source>
</hmis:Sources>
You mentioned Explicit, so here is solution generated with FOAM. Not that pretty for some but works OK though could have some limitation with super fancy formats and some casese with maxOccurs="0". It also might require some manual tweaking.
I made single table c just as my personal preference, which required me to add top 1 into few levels manually. But also works fine with join
/* --------------------TEST DATA
drop table if exists #tbl
Create table #tbl (
PersonalID INT , first_name VARCHAR(30), last_name VARCHAR(30), birth_date DATE, CaseManager_PH VARCHAR(30), Female BIT, ExportDate DATE, user_updated VARCHAR(30) );
INSERT INTO #tbl
VALUES
(111, 'Alpha', 'Brown', '2000-01-01', 'Manager Alpha', 0, '2022-03-02', 'AW3'),
(222, 'Bobby', 'Dow', '2002-02-02', 'Manager2222', 0 , '2022-03-03', 'BBX3');
create table #export (ExportDate date , StartDate DATE, EndDate date);
INSERT INTO #export (ExportDate, StartDate, EndDate) VALUES
('2022-03-22', '2022-01-01', '2022-04-04');
-- select * from #export
-- select * from #tbl
-- drop table c
Select * into c from (
select c.*, e.StartDate, e.EndDate
, hmis =cast('https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd' as varchar(255))
, ExportID = '111', SourceID = '10', ContactInfo = 'Unknown'
from #tbl c
join #export e on 1=1
) c
Select * from c
*/
/********************************************
* Output produced by Foam #
* 3/31/2022 12:12:03 PM
*
* digital nothing design
* http://www.digitalnothing.com
********************************************/
SELECT Tag = 1, Parent = NULL,
[hmis:Sources!1!xmlns:hmis] = 'https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd',
[hmis:Source!2!] = NULL,
[hmis:Source!2!hmis:SourceID!element] = NULL,
[hmis:Export!3!] = NULL,
[hmis:Export!3!ExportID!element] = NULL,
[hmis:Export!3!ExportDate!element] = NULL,
[ExportPeriod!4!] = NULL,
[ExportPeriod!4!StartDate!element] = NULL,
[ExportPeriod!4!EndDate!element] = NULL,
[hmis:Client!5!DateCreated] = NULL,
[hmis:Client!5!DateUpdated] = NULL,
[hmis:Client!5!hmis:PersonalID!element] = NULL,
[hmis:Client!5!hmis:first_name!element] = NULL,
[hmis:Client!5!hmis:last_name!element] = NULL,
[hmis:Client!5!hmis:birth_date!element] = NULL,
[hmis:CustomClientElements!6!] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_PH!element] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_ContactInfo!element] = NULL,
[hmis:Client!5!hmis:user_updated!element] = NULL
UNION ALL
SELECT top 1 Tag = 2, Parent = 1,
[hmis:Sources!1!xmlns:hmis] = NULL,
[hmis:Source!2!] = NULL,
[hmis:Source!2!hmis:SourceID!element] = c.SourceID,
[hmis:Export!3!] = NULL,
[hmis:Export!3!ExportID!element] = NULL,
[hmis:Export!3!ExportDate!element] = NULL,
[ExportPeriod!4!] = NULL,
[ExportPeriod!4!StartDate!element] = NULL,
[ExportPeriod!4!EndDate!element] = NULL,
[hmis:Client!5!DateCreated] = NULL,
[hmis:Client!5!DateUpdated] = NULL,
[hmis:Client!5!hmis:PersonalID!element] = NULL,
[hmis:Client!5!hmis:first_name!element] = NULL,
[hmis:Client!5!hmis:last_name!element] = NULL,
[hmis:Client!5!hmis:birth_date!element] = NULL,
[hmis:CustomClientElements!6!] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_PH!element] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_ContactInfo!element] = NULL,
[hmis:Client!5!hmis:user_updated!element] = NULL
from c
UNION ALL
SELECT top 1 Tag = 3, Parent = 2,
[hmis:Sources!1!xmlns:hmis] = NULL,
[hmis:Source!2!] = NULL,
[hmis:Source!2!hmis:SourceID!element] = NULL,
[hmis:Export!3!] = NULL,
[hmis:Export!3!ExportID!element] = c.ExportID,
[hmis:Export!3!ExportDate!element] = c.ExportDate,
[ExportPeriod!4!] = NULL,
[ExportPeriod!4!StartDate!element] = NULL,
[ExportPeriod!4!EndDate!element] = NULL,
[hmis:Client!5!DateCreated] = NULL,
[hmis:Client!5!DateUpdated] = NULL,
[hmis:Client!5!hmis:PersonalID!element] = NULL,
[hmis:Client!5!hmis:first_name!element] = NULL,
[hmis:Client!5!hmis:last_name!element] = NULL,
[hmis:Client!5!hmis:birth_date!element] = NULL,
[hmis:CustomClientElements!6!] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_PH!element] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_ContactInfo!element] = NULL,
[hmis:Client!5!hmis:user_updated!element] = NULL
from c
UNION ALL
SELECT top 1 Tag = 4, Parent = 3,
[hmis:Sources!1!xmlns:hmis] = NULL,
[hmis:Source!2!] = NULL,
[hmis:Source!2!hmis:SourceID!element] = NULL,
[hmis:Export!3!] = NULL,
[hmis:Export!3!ExportID!element] = NULL,
[hmis:Export!3!ExportDate!element] = NULL,
[ExportPeriod!4!] = NULL,
[ExportPeriod!4!StartDate!element] = c.StartDate,
[ExportPeriod!4!EndDate!element] = c.EndDate,
[hmis:Client!5!DateCreated] = NULL,
[hmis:Client!5!DateUpdated] = NULL,
[hmis:Client!5!hmis:PersonalID!element] = NULL,
[hmis:Client!5!hmis:first_name!element] = NULL,
[hmis:Client!5!hmis:last_name!element] = NULL,
[hmis:Client!5!hmis:birth_date!element] = NULL,
[hmis:CustomClientElements!6!] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_PH!element] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_ContactInfo!element] = NULL,
[hmis:Client!5!hmis:user_updated!element] = NULL
from c
UNION ALL
SELECT Tag = 5, Parent = 3,
[hmis:Sources!1!xmlns:hmis] = NULL,
[hmis:Source!2!] = NULL,
[hmis:Source!2!hmis:SourceID!element] = NULL,
[hmis:Export!3!] = NULL,
[hmis:Export!3!ExportID!element] = NULL,
[hmis:Export!3!ExportDate!element] = NULL,
[ExportPeriod!4!] = NULL,
[ExportPeriod!4!StartDate!element] = NULL,
[ExportPeriod!4!EndDate!element] = NULL,
[hmis:Client!5!DateCreated] = c.ExportDate,
[hmis:Client!5!DateUpdated] = c.ExportDate,
[hmis:Client!5!hmis:PersonalID!element] = c.PersonalID,
[hmis:Client!5!hmis:first_name!element] = c.first_name,
[hmis:Client!5!hmis:last_name!element] = c.last_name,
[hmis:Client!5!hmis:birth_date!element] = c.birth_date,
[hmis:CustomClientElements!6!] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_PH!element] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_ContactInfo!element] = NULL,
[hmis:Client!5!hmis:user_updated!element] = c.user_updated
from c
UNION ALL
SELECT Tag = 6, Parent = 5,
[hmis:Sources!1!xmlns:hmis] = NULL,
[hmis:Source!2!] = NULL,
[hmis:Source!2!hmis:SourceID!element] = NULL,
[hmis:Export!3!] = NULL,
[hmis:Export!3!ExportID!element] = NULL,
[hmis:Export!3!ExportDate!element] = NULL,
[ExportPeriod!4!] = NULL,
[ExportPeriod!4!StartDate!element] = NULL,
[ExportPeriod!4!EndDate!element] = NULL,
[hmis:Client!5!DateCreated] = NULL,
[hmis:Client!5!DateUpdated] = NULL,
[hmis:Client!5!hmis:PersonalID!element] = c.PersonalID,
[hmis:Client!5!hmis:first_name!element] = NULL,
[hmis:Client!5!hmis:last_name!element] = NULL,
[hmis:Client!5!hmis:birth_date!element] = NULL,
[hmis:CustomClientElements!6!] = NULL,
[hmis:CustomClientElements!6!hmis:Casemanager_PH!element] = ISNULL(c.Casemanager_PH,''),
[hmis:CustomClientElements!6!hmis:Casemanager_ContactInfo!element] = ISNULL(c.ContactInfo,''),
[hmis:Client!5!hmis:user_updated!element] = NULL
from c
ORDER BY
[hmis:Client!5!hmis:PersonalID!element] ASC,
Tag
FOR XML EXPLICIT
-- sample xml with column names as element values used in FOAM
/*
<hmis:Sources xmlns:hmis="https://www.hudhdx.info/Resources/Vendors/4_0/HUD_HMIS.xsd" >
<hmis:Source>
<hmis:SourceID>c.SourceID</hmis:SourceID>
<hmis:Export>
<ExportID>c.ExportID</ExportID>
<ExportDate>c.ExportDate</ExportDate>
<ExportPeriod>
<StartDate>c.StartDate</StartDate>
<EndDate>c.EndDate</EndDate>
</ExportPeriod>
<hmis:Client DateCreated = "c.ExportDate" DateUpdated="c.ExportDate" >
<hmis:PersonalID>c.PersonalID</hmis:PersonalID>
<hmis:first_name>c.first_name</hmis:first_name>
<hmis:last_name>c.last_name</hmis:last_name>
<hmis:birth_date>c.birth_date</hmis:birth_date>
<hmis:CustomClientElements>
<hmis:Casemanager_PH>c.Casemanager_PH</hmis:Casemanager_PH>
<hmis:Casemanager_ContactInfo>c.ContactInfo</hmis:Casemanager_ContactInfo>
</hmis:CustomClientElements>
<hmis:user_updated>c.user_updated</hmis:user_updated>
</hmis:Client>
</hmis:Export>
</hmis:Source>
</hmis:Sources>
*/

Creating an array of objects from a large unsorted object

I need to create an array of objects from some data in TypeScript. The data i get from an api is terribly formatted as it is just one big object.
It looks something like this.
KeyBinary00: null
KeyBinary01: null
KeyBinary02: null
KeyBinary03: null
KeyBinary04: null
KeyBoolean00: "deling"
KeyBoolean01: null
KeyBoolean02: null
KeyBoolean03: null
KeyBoolean04: null
KeyDateTime00: "start"
KeyDateTime01: "slutt"
KeyDateTime02: null
KeyDateTime03: null
KeyDateTime04: null
KeyInteger00: "verdi"
KeyInteger01: null
KeyInteger02: null
KeyInteger03: null
KeyInteger04: null
KeyString00: "status"
KeyString01: "beskrivelse"
KeyString02: "leverandør"
KeyString03: "prosjektleder"
KeyString04: "prosjektnavn"
KeyString05: "tekniskPL"
KeyString06: "klasse"
KeyString07: "type"
KeyString08: "statusBeskrivelse"
KeyString09: "id"
LabelBinary00: null
LabelBinary01: null
LabelBinary02: null
LabelBinary03: null
LabelBinary04: null
LabelBoolean00: "Ekstern Deling"
LabelBoolean01: null
LabelBoolean02: null
LabelBoolean03: null
LabelBoolean04: null
LabelDateTime00: "Startdato"
LabelDateTime01: "Sluttdato"
LabelDateTime02: null
LabelDateTime03: null
LabelDateTime04: null
LabelInteger00: "Verdi"
LabelInteger01: null
LabelInteger02: null
LabelInteger03: null
LabelInteger04: null
LabelString00: "Prosjektstatus"
LabelString01: "Beskrivelse"
LabelString02: "Leverandør"
LabelString03: "Prosjektleder / KAM"
LabelString04: "Prosjektnavn"
LabelString05: "Teknisk PL"
LabelString06: "Prosjektklasse"
LabelString07: "Prosjekttype"
LabelString08: "Prosjesktstatus beskrivelse"
LabelString09: "Prosjekt"
ValueBinary00: null
ValueBinary01: null
ValueBinary02: null
ValueBinary03: null
ValueBinary04: null
ValueBoolean00: false
ValueBoolean01: null
ValueBoolean02: null
ValueBoolean03: null
ValueBoolean04: null
ValueDateTime00: "2020-01-07T00:00:00Z"
ValueDateTime01: "2021-02-10T00:00:00Z"
ValueDateTime02: null
ValueDateTime03: null
ValueDateTime04: null
ValueInteger00: 10000000
ValueInteger01: null
ValueInteger02: null
ValueInteger03: null
ValueInteger04: null
ValueString00: "Godkjennt"
ValueString01: "Dette prosjektet skal kjøpe inn."
ValueString02: "Siemens"
ValueString03: "Tom Tommerson"
ValueString04: "Innkjøp av nye leker"
ValueString05: "Adele Adelerson"
ValueString06: "Stort prosjekt"
ValueString07: "Innkjøp"
ValueString08: "En beskrivelse om status"
ValueString09: "20-12"
what i need to do is to create an array of objects where all the values that correspond are put together as such.
[
{
KeyBoolean00: "deling",
LabelBoolean00: "Ekstern Deling",
ValueBoolean00: false
},
{
KeyDateTime00: "start",
LabelDateTime00: "Startdato",
ValueDateTime00: "2020-01-07T00:00:00Z"
},
{
KeyString00: "status"
LabelString00: "Prosjektstatus"
ValueString00: "Godkjennt"
}
]
if that makes any sense.
I have tried some things, but im not sure how to do it.
Use substring to get key number as 00, 01, ... and reduce to build the necessary object
const obj = {
KeyBinary00: null,
KeyBinary01: null,
KeyBinary02: null,
KeyBinary03: null,
KeyBinary04: null,
KeyBoolean00: 'deling',
KeyBoolean01: null,
KeyBoolean02: null,
KeyBoolean03: null,
KeyBoolean04: null,
KeyDateTime00: 'start',
KeyDateTime01: 'slutt',
KeyDateTime02: null,
KeyDateTime03: null,
KeyDateTime04: null,
KeyInteger00: 'verdi',
KeyInteger01: null,
KeyInteger02: null,
KeyInteger03: null,
KeyInteger04: null,
KeyString00: 'status',
KeyString01: 'beskrivelse',
KeyString02: 'leverandør',
KeyString03: 'prosjektleder',
KeyString04: 'prosjektnavn',
KeyString05: 'tekniskPL',
KeyString06: 'klasse',
KeyString07: 'type',
KeyString08: 'statusBeskrivelse',
KeyString09: 'id',
LabelBinary00: null,
LabelBinary01: null,
LabelBinary02: null,
LabelBinary03: null,
LabelBinary04: null,
LabelBoolean00: 'Ekstern Deling',
LabelBoolean01: null,
LabelBoolean02: null,
LabelBoolean03: null,
LabelBoolean04: null,
LabelDateTime00: 'Startdato',
LabelDateTime01: 'Sluttdato',
LabelDateTime02: null,
LabelDateTime03: null,
LabelDateTime04: null,
LabelInteger00: 'Verdi',
LabelInteger01: null,
LabelInteger02: null,
LabelInteger03: null,
LabelInteger04: null,
LabelString00: 'Prosjektstatus',
LabelString01: 'Beskrivelse',
LabelString02: 'Leverandør',
LabelString03: 'Prosjektleder / KAM',
LabelString04: 'Prosjektnavn',
LabelString05: 'Teknisk PL',
LabelString06: 'Prosjektklasse',
LabelString07: 'Prosjekttype',
LabelString08: 'Prosjesktstatus beskrivelse',
LabelString09: 'Prosjekt',
ValueBinary00: null,
ValueBinary01: null,
ValueBinary02: null,
ValueBinary03: null,
ValueBinary04: null,
ValueBoolean00: false,
ValueBoolean01: null,
ValueBoolean02: null,
ValueBoolean03: null,
ValueBoolean04: null,
ValueDateTime00: '2020-01-07T00:00:00Z',
ValueDateTime01: '2021-02-10T00:00:00Z',
ValueDateTime02: null,
ValueDateTime03: null,
ValueDateTime04: null,
ValueInteger00: 10000000,
ValueInteger01: null,
ValueInteger02: null,
ValueInteger03: null,
ValueInteger04: null,
ValueString00: 'Godkjennt',
ValueString01: 'Dette prosjektet skal kjøpe inn.',
ValueString02: 'Siemens',
ValueString03: 'Tom Tommerson',
ValueString04: 'Innkjøp av nye leker',
ValueString05: 'Adele Adelerson',
ValueString06: 'Stort prosjekt',
ValueString07: 'Innkjøp',
ValueString08: 'En beskrivelse om status',
ValueString09: '20-12',
};
const result = Object.values(
Object.keys(obj).reduce((acc, key) => {
const number = key.substring(key.length - 2, key.length); // 00
acc[number] = acc[number] ? { ...acc[number], [key]: obj[key] } : { [key]: obj[key] };
return acc;
}, {}),
);
console.log(result);

Requires that my function is implemented access to the database

Requires that my function is implemented access to the databaseand certain operations were carried out.
Such as:
1) Check or read the message.
2) Checking and issuing all communications between users by date.
3) Find the latest posts from all users.
But I though there is material in the tutorial I could not figure out how to do. All I could show you. Therefore, I want to ask you to see my code and see what's up there and help me out. You do not need to completely write code. Just if it's not hard for you to sink your sleeves and calmly give examples let me write these three functions on LINQ and EntityFramework
MY CODE:
public bool CheckIfUnreadMessagesArePresent(int userId)
{
IList<MessageInfo> messageInfo = new List<MessageInfo>();
MessageInfo messageIn = new MessageInfo();
var sql = #"SELECT * FROM MessageInfo
WHERE wasRead = 0 AND Id = #userId ORDER BY date DESC";
var meassage = dataBaseContext.Database.SqlQuery<MessageInfo>(sql, new { userId = messageIn.Id });
return messageInfo.Any(messageIn.WasRead == true);
}
public IList<UserInfo> FetchMessagesFromConversation(int userID1, int userID2)
{
IList<UserInfo> messageInfo = new List<UserInfo>();
MessageInfo messageIn = new MessageInfo();
var sql = #"SELECT * FROM MessageInfo
WHERE userID1 = 1 AND userID2 = 3 OR userID1 = 3 AND userID2 = 1 order by date DESC";
var meassage = dataBaseContext.Database.SqlQuery<MessageInfo>(sql, new { userID1 = messageIn.SenderId,userID2 = messageIn.ReceiverId});
messageInfo.Insert(message);
return messageInfo;
}
public IList<MessageInfo> FetchLastMessageFromEachConversation((int userID))
{
IList<UserInfo> messageInfo = new List<UserInfo>();
MessageInfo messageIn = new MessageInfo();
var sql = #"SELECT * FROM MessageInfo
WHERE date IN (#SELECT max(date)
FROM MessageInfo WHRERE date IN (
SELECT max(date)
FROM MessageInfo
GROUP BY messageIn.ID, messageIn.ReceiverId
ORDER BY date DESC, messageIn.ID, messageIn.ReceiverId)
GROUP BY if (messageIn.ID < messageIn.ReceiverId,
concat(messageIn.ID, " - ", messageIn.ReceiverId), concat(messageIn.ReceiverId, " - ", messageIn.ID)))";
var meassage = dataBaseContext.Database.SqlQuery<MessageInfo>(sql, new { userID1 = messageIn.SenderId, userID2 = messageIn.ReceiverId });
return messageInfo;
}
My DataBase
CREATE TABLE [dbo].[UserInfo](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Login] [varchar](50) NOT NULL,
[Password] [varchar](50) NOT NULL,
[FirstName] [nchar](10) NOT NULL,
[LastName] [nchar](10) NOT NULL,
[Email] [varchar](50) NOT NULL,
[RegistrationDate] [datetime] NOT NULL,
)
GO
CREATE TABLE [dbo].[Messages](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[SenderId] [int] NOT NULL,
[ReceiverId] [int] NOT NULL,
[Message] [varchar](500) NOT NULL,
[date] [datetime] NOT NULL,
[WasRead][bit] NOT NULL
CONSTRAINT FK_UserInfoSender FOREIGN KEY (SenderId)
REFERENCES [UserInfo](Id),
CONSTRAINT FK_UserInfoReceiver FOREIGN KEY (ReceiverId)
REFERENCES [UserInfo](Id)
)
GO

Dom Exception 18 open database - angularjs and cordova

How to initialize sqlite db when using only angular.js and cordova without ionic?
Below is the sequence of existing app initialization.
$(function() {
new init();
});
var init = function() {
// Check platform - web browser, iOS, android
if (navigator.userAgent.match(/(iOS|iPad|iPhone|Android)/)) {
document.addEventListener("deviceready", onDeviceReady, false);
} else {
initializeApp('browser');
}
// Device Ready event for cordova
function onDeviceReady() {
initializeApp('device');
}
// Bootstrapping Angular manually.
function initializeApp(platform) {
angular.element(document).ready(function() {
angular.bootstrap(document, ['myApp']);
});
}
}
// Angular Module and configurations
var myApp = angular.module('myApp', ['ngAnimate','ui.router', 'myApp.constants', 'myApp.helper','myApp.directives', 'myApp.utils', 'myApp.services','ngSanitize', 'ui.bootstrap', 'myApp.filter','chart.js']);
Yes you can use .run method for creating db. here i attach my code for sqlite for web and cordova also so you can sqlite db in browser when you use ionic serve
var db = null;
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ngCordova','timer','ionic-datepicker','ionic-timepicker'])
.run(function($ionicPlatform,$cordovaSQLite,$ionicPopup)
{
$ionicPlatform.ready(function()
{
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard)
{
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if (window.StatusBar)
{
StatusBar.styleLightContent();
}
if(window.cordova)
{
// App syntax
db = $cordovaSQLite.openDB("myapp.db");
}
else
{
// Ionic serve syntax
db = window.openDatabase("myapp.db", "1.0", "My app", -1);
}
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Users ( UserId INTEGER NOT NULL UNIQUE PRIMARY KEY, CloudId INTEGER, Email TEXT NOT NULL UNIQUE, IsEmailVerified TEXT DEFAULT 'N' NOT NULL, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, UserLanguage TEXT NOT NULL DEFAULT English, IsAutoSyncOn BOOL NOT NULL DEFAULT 1, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateDeleted TIMESTAMP)");
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Photos( PhotoId INTEGER NOT NULL UNIQUE PRIMARY KEY, Source TEXT 'Standard' NOT NULL, Category TEXT, ThumbnailURL TEXT NOT NULL, PhotoURL TEXT NOT NULL, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateDeleted TIMESTAMP)");
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Devices( DeviceId INTEGER NOT NULL UNIQUE PRIMARY KEY, UserId INTEGER NOT NULL, DbVersion INTEGER NOT NULL DEFAULT 1, DateLastLogin TIMESTAMP DEFAULT CURRENT_TIMESTAMP, NbrTimesSinceLastUpgradeNag INTEGER NOT NULL DEFAULT 1, DateLastSync TIMESTAMP, DateSyncServiceExpires TIMESTAMP, DateLastReviewRequest TIMESTAMPDEFAULT CURRENT_TIMESTAMP, NbrLogins INTEGER NOT NULL DEFAULT 1, DeviceName TEXT NOT NULL DEFAULT iPhone, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");
$cordovaSQLite.execute(db,"CREATE TABLE IF NOT EXISTS Events( EventId INTEGER NOT NULL UNIQUE PRIMARY KEY, CloudId INTEGER,UserId INTEGER NOT NULL, EventName TEXT NOT NULL, DateEvent TIMESTAMP, Category TEXT, ReminderIntervalNbr TEXT, ReminderIntervalUnits TEXT, RepeatInterval TEXT 'No Repeat' NOT NULL, PhotoId INTEGER, DateUpdated TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateAdded TIMESTAMP DEFAULT CURRENT_TIMESTAMP, DateDeleted TIMESTAMP)");
});
})

phonegap database error code 5 - non an error

I am using phonegap to access the phone database. But my create process calls everytime the error routine with error code: 5 and message: non an error?
Isn't it possible to execute multiple sql statements in one transaction?
SQLite Expert couldnt find an syntax error...
createDB: function(error, success) {
if(typeof error != 'function') error = this.errorDB;
if(typeof success != 'function') success = this.successDB;
sql = "DROP TABLE IF EXISTS `boiler`; "
+"CREATE TABLE IF NOT EXISTS `boiler` ( "
+" `id` int NOT NULL, `object` int NOT NULL, `number` varchar(100) NOT NULL, "
+" `volume` double DEFAULT NULL, `brand` varchar(100) DEFAULT NULL, `year` year(4) DEFAULT NULL, "
+" `price_before` float NOT NULL DEFAULT '0', `price_after` float NOT NULL DEFAULT '0', `description` TEXT DEFAULT NULL, "
+" `img1` varchar(200) DEFAULT NULL, `img2` varchar(200) DEFAULT NULL, `img3` varchar(200) DEFAULT NULL, "
+" `img4` varchar(200) DEFAULT NULL, `img5` varchar(200) DEFAULT NULL, `img6` varchar(200) DEFAULT NULL, "
+" `img7` varchar(200) DEFAULT NULL, `img8` varchar(200) DEFAULT NULL, `img9` varchar(200) DEFAULT NULL, "
+"PRIMARY KEY (`id`)); "
+"DROP TABLE IF EXISTS `counter`; "
+"CREATE TABLE IF NOT EXISTS `counter` ( "
+" `number` varchar(100) NOT NULL, `object` int NOT NULL, `type` tinyint NOT NULL DEFAULT '0', "
+" `value` double DEFAULT NULL, `access` varchar(100) DEFAULT NULL, "
+"PRIMARY KEY (`number`)); "
+"DROP TABLE IF EXISTS `link`; "
+"CREATE TABLE IF NOT EXISTS `link` ( "
+" `id` int NOT NULL, `boiler` int NOT NULL, `name` varchar(100) DEFAULT NULL, "
+" `units` tinyint DEFAULT NULL, `price` float NOT NULL DEFAULT '0', "
+"PRIMARY KEY (`id`)); "
+"DROP TABLE IF EXISTS `manager`; "
+"CREATE TABLE IF NOT EXISTS `manager` ( "
+" `id` int NOT NULL, `company` varchar(100) DEFAULT NULL, `name` varchar(100) NOT NULL, "
+" `phone` varchar(15) DEFAULT NULL, "
+"PRIMARY KEY (`id`)); "
+"DROP TABLE IF EXISTS `object`; "
+"CREATE TABLE IF NOT EXISTS `object` ( "
+" `id` int NOT NULL, `state` tinyint NOT NULL DEFAULT '0', `user` varchar(50) DEFAULT NULL, "
+" `date` char(15) DEFAULT NULL, `street` varchar(100) DEFAULT NULL, `number` varchar(16) DEFAULT NULL, "
+" `zip` char(5) DEFAULT NULL, `city` varchar(100) DEFAULT NULL, `manager` int NOT NULL DEFAULT '0', "
+" `units` int NOT NULL DEFAULT '0', "
+"PRIMARY KEY (`id`));";
console.log(sql);
this.DB.transaction(function (tx) { tx.executeSql(sql); }, error, success);
},
don't know the error code: 5 but you can execute the multiple sql statements
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is ready
function onDeviceReady() {
var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
db.transaction(populateDB, errorCB, successCB);
}
// Populate the database
function populateDB(tx) {
tx.executeSql('DROP TABLE IF EXISTS DEMO');
tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
//here you can do multiple sql statements.
}
// Transaction error callback
function errorCB(err) {
alert("Error processing SQL: "+err);
}
// Transaction success callback
function successCB() {
alert("success!");
}
link here
Please Check Your Database From File Explorer
there is problem only in the creation of table like you are entering four values but in your table there are only three columns so this error occours
tx.executeSql("CREATE TABLE IF NOT EXISTS quotes(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,name,flag INTEGER,address,description,phone_number,date INTEGER,quote_number,job,images)");
Error is due to if you are assigning directly integer value instead of var type value
like for flar i am using 1 or 0
but now i store this value in var type

Resources