Pretty print SQL

This commit is contained in:
Alex Cabal 2022-12-24 15:44:42 -06:00
parent 31a8bc2d6c
commit 26a24cdbe2
17 changed files with 316 additions and 82 deletions

View file

@ -47,7 +47,14 @@ class NewsletterSubscription extends PropertiesBase{
$this->Created = new DateTime();
try{
Db::Query('INSERT into NewsletterSubscriptions (UserId, IsConfirmed, IsSubscribedToNewsletter, IsSubscribedToSummary, Created) values (?, ?, ?, ?, ?);', [$this->User->UserId, false, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->Created]);
Db::Query('
INSERT into NewsletterSubscriptions (UserId, IsConfirmed, IsSubscribedToNewsletter, IsSubscribedToSummary, Created)
values (?,
?,
?,
?,
?)
', [$this->User->UserId, false, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->Created]);
}
catch(PDOException $ex){
if($ex->errorInfo[1] == 1062){
@ -66,7 +73,13 @@ class NewsletterSubscription extends PropertiesBase{
public function Save(): void{
$this->Validate();
Db::Query('UPDATE NewsletterSubscriptions set IsConfirmed = ?, IsSubscribedToNewsletter = ?, IsSubscribedToSummary = ? where UserId = ?', [$this->IsConfirmed, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->UserId]);
Db::Query('
UPDATE NewsletterSubscriptions
set IsConfirmed = ?,
IsSubscribedToNewsletter = ?,
IsSubscribedToSummary = ?
where UserId = ?
', [$this->IsConfirmed, $this->IsSubscribedToNewsletter, $this->IsSubscribedToSummary, $this->UserId]);
}
public function SendConfirmationEmail(): void{
@ -83,11 +96,19 @@ class NewsletterSubscription extends PropertiesBase{
}
public function Confirm(): void{
Db::Query('UPDATE NewsletterSubscriptions set IsConfirmed = true where UserId = ?;', [$this->UserId]);
Db::Query('
UPDATE NewsletterSubscriptions
set IsConfirmed = true
where UserId = ?
', [$this->UserId]);
}
public function Delete(): void{
Db::Query('DELETE from NewsletterSubscriptions where UserId = ?;', [$this->UserId]);
Db::Query('
DELETE
from NewsletterSubscriptions
where UserId = ?
', [$this->UserId]);
}
public function Validate(): void{
@ -112,7 +133,12 @@ class NewsletterSubscription extends PropertiesBase{
// ***********
public static function Get(string $uuid): NewsletterSubscription{
$result = Db::Query('SELECT ns.* from NewsletterSubscriptions ns inner join Users u using(UserId) where u.Uuid = ?', [$uuid], 'NewsletterSubscription');
$result = Db::Query('
SELECT ns.*
from NewsletterSubscriptions ns
inner join Users u using(UserId)
where u.Uuid = ?
', [$uuid], 'NewsletterSubscription');
if(sizeof($result) == 0){
throw new Exceptions\InvalidNewsletterSubscriptionException();

View file

@ -20,14 +20,33 @@ class Patron extends PropertiesBase{
public function Create(): void{
$this->Created = new DateTime();
Db::Query('INSERT into Patrons (Created, UserId, IsAnonymous, AlternateName, IsSubscribedToEmails) values(?, ?, ?, ?, ?);', [$this->Created, $this->UserId, $this->IsAnonymous, $this->AlternateName, $this->IsSubscribedToEmails]);
Db::Query('
INSERT into Patrons (Created, UserId, IsAnonymous, AlternateName, IsSubscribedToEmails)
values(?,
?,
?,
?,
?)
', [$this->Created, $this->UserId, $this->IsAnonymous, $this->AlternateName, $this->IsSubscribedToEmails]);
Db::Query('INSERT into Benefits (UserId, CanVote, CanAccessFeeds, CanBulkDownload) values (?, true, true, true) on duplicate key update CanVote = true, CanAccessFeeds = true, CanBulkDownload = true', [$this->UserId]);
Db::Query('
INSERT into Benefits (UserId, CanVote, CanAccessFeeds, CanBulkDownload)
values (?,
true,
true,
true) on duplicate key
update CanVote = true,
CanAccessFeeds = true,
CanBulkDownload = true
', [$this->UserId]);
// If this is a patron for the first time, send the first-time patron email.
// Otherwise, send the returning patron email.
$isReturning = Db::QueryInt('SELECT count(*) from Patrons where UserId = ?', [$this->UserId]) > 1;
$isReturning = Db::QueryInt('
SELECT count(*)
from Patrons
where UserId = ?
', [$this->UserId]) > 1;
$this->SendWelcomeEmail($isReturning);
}
@ -60,7 +79,11 @@ class Patron extends PropertiesBase{
// ***********
public static function Get(?int $userId): Patron{
$result = Db::Query('SELECT * from Patrons where UserId = ?', [$userId], 'Patron');
$result = Db::Query('
SELECT *
from Patrons
where UserId = ?
', [$userId], 'Patron');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPatronException();
@ -70,7 +93,12 @@ class Patron extends PropertiesBase{
}
public static function GetByEmail(?string $email): Patron{
$result = Db::Query('SELECT p.* from Patrons p inner join Users u using(UserId) where u.Email = ?', [$email], 'Patron');
$result = Db::Query('
SELECT p.*
from Patrons p
inner join Users u using(UserId)
where u.Email = ?
', [$email], 'Patron');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPatronException();

View file

@ -33,7 +33,11 @@ class Payment extends PropertiesBase{
$this->User = $user;
// Update their name in case we have their email (but not name) recorded from a newsletter subscription
Db::Query('UPDATE Users set Name = ? where UserId = ?', [$this->User->Name, $this->User->UserId]);
Db::Query('
UPDATE Users
set Name = ?
where UserId = ?
', [$this->User->Name, $this->User->UserId]);
}
catch(Exceptions\InvalidUserException $ex){
// User doesn't exist, create it now
@ -45,7 +49,16 @@ class Payment extends PropertiesBase{
}
try{
Db::Query('INSERT into Payments (UserId, Created, ChannelId, TransactionId, Amount, Fee, IsRecurring) values(?, ?, ?, ?, ?, ?, ?);', [$this->UserId, $this->Created, $this->ChannelId, $this->TransactionId, $this->Amount, $this->Fee, $this->IsRecurring]);
Db::Query('
INSERT into Payments (UserId, Created, ChannelId, TransactionId, Amount, Fee, IsRecurring)
values(?,
?,
?,
?,
?,
?,
?)
', [$this->UserId, $this->Created, $this->ChannelId, $this->TransactionId, $this->Amount, $this->Fee, $this->IsRecurring]);
}
catch(PDOException $ex){
if($ex->errorInfo[1] == 1062){

View file

@ -36,7 +36,12 @@ class Poll extends PropertiesBase{
protected function GetVoteCount(): int{
if($this->_VoteCount === null){
$this->_VoteCount = Db::QueryInt('SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollId = ?', [$this->PollId]);
$this->_VoteCount = Db::QueryInt('
SELECT count(*)
from PollVotes pv
inner join PollItems pi using (PollItemId)
where pi.PollId = ?
', [$this->PollId]);
}
return $this->_VoteCount;
@ -47,7 +52,12 @@ class Poll extends PropertiesBase{
*/
protected function GetPollItems(): array{
if($this->_PollItems === null){
$this->_PollItems = Db::Query('SELECT * from PollItems where PollId = ? order by SortOrder asc', [$this->PollId], 'PollItem');
$this->_PollItems = Db::Query('
SELECT *
from PollItems
where PollId = ?
order by SortOrder asc
', [$this->PollId], 'PollItem');
}
return $this->_PollItems;
@ -91,7 +101,11 @@ class Poll extends PropertiesBase{
throw new Exceptions\InvalidPollException();
}
$result = Db::Query('SELECT * from Polls where PollId = ?', [$pollId], 'Poll');
$result = Db::Query('
SELECT *
from Polls
where PollId = ?
', [$pollId], 'Poll');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollException();
@ -105,7 +119,11 @@ class Poll extends PropertiesBase{
throw new Exceptions\InvalidPollException();
}
$result = Db::Query('SELECT * from Polls where UrlName = ?', [$urlName], 'Poll');
$result = Db::Query('
SELECT *
from Polls
where UrlName = ?
', [$urlName], 'Poll');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollException();

View file

@ -19,7 +19,12 @@ class PollItem extends PropertiesBase{
protected function GetVoteCount(): int{
if($this->_VoteCount === null){
$this->_VoteCount = Db::QueryInt('SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollItemId = ?', [$this->PollItemId]);
$this->_VoteCount = Db::QueryInt('
SELECT count(*)
from PollVotes pv
inner join PollItems pi using (PollItemId)
where pi.PollItemId = ?
', [$this->PollItemId]);
}
return $this->_VoteCount;
@ -35,7 +40,11 @@ class PollItem extends PropertiesBase{
throw new Exceptions\InvalidPollItemException();
}
$result = Db::Query('SELECT * from PollItems where PollItemId = ?', [$pollItemId], 'PollItem');
$result = Db::Query('
SELECT *
from PollItems
where PollItemId = ?
', [$pollItemId], 'PollItem');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollItemException();

View file

@ -93,7 +93,12 @@ class PollVote extends PropertiesBase{
$this->Validate();
$this->Created = new DateTime();
Db::Query('INSERT into PollVotes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]);
Db::Query('
INSERT into PollVotes (UserId, PollItemId, Created)
values (?,
?,
?)
', [$this->UserId, $this->PollItemId, $this->Created]);
}
public static function Get(?string $pollUrlName, ?int $userId): PollVote{
@ -101,10 +106,16 @@ class PollVote extends PropertiesBase{
throw new Exceptions\InvalidPollVoteException();
}
$result = Db::Query('SELECT pv.* from PollVotes pv inner join
(select pi.PollItemId from PollItems pi inner join Polls p using (PollId)
where p.UrlName = ?
) x using (PollItemId) where pv.UserId = ?', [$pollUrlName, $userId], 'PollVote');
$result = Db::Query('
SELECT pv.*
from PollVotes pv
inner join
(select pi.PollItemId
from PollItems pi
inner join Polls p using (PollId)
where p.UrlName = ? ) x using (PollItemId)
where pv.UserId = ?
', [$pollUrlName, $userId], 'PollVote');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollVoteException();

View file

@ -36,7 +36,12 @@ class Session extends PropertiesBase{
$this->User = User::GetIfRegistered($email);
$this->UserId = $this->User->UserId;
$existingSessions = Db::Query('SELECT SessionId, Created from Sessions where UserId = ?', [$this->UserId]);
$existingSessions = Db::Query('
SELECT SessionId,
Created
from Sessions
where UserId = ?
', [$this->UserId]);
if(sizeof($existingSessions) > 0){
$this->SessionId = $existingSessions[0]->SessionId;
@ -46,7 +51,12 @@ class Session extends PropertiesBase{
$uuid = Uuid::uuid4();
$this->SessionId = $uuid->toString();
$this->Created = new DateTime();
Db::Query('INSERT into Sessions (UserId, SessionId, Created) values (?, ?, ?)', [$this->UserId, $this->SessionId, $this->Created]);
Db::Query('
INSERT into Sessions (UserId, SessionId, Created)
values (?,
?,
?)
', [$this->UserId, $this->SessionId, $this->Created]);
}
$this->SetSessionCookie($this->SessionId);
@ -56,7 +66,12 @@ class Session extends PropertiesBase{
$sessionId = HttpInput::Str(COOKIE, 'sessionid');
if($sessionId !== null){
$result = Db::Query('SELECT u.* from Users u inner join Sessions s using (UserId) where s.SessionId = ?', [$sessionId], 'User');
$result = Db::Query('
SELECT u.*
from Users u
inner join Sessions s using (UserId)
where s.SessionId = ?
', [$sessionId], 'User');
if(sizeof($result) > 0){
self::SetSessionCookie($sessionId);
@ -76,7 +91,11 @@ class Session extends PropertiesBase{
throw new Exceptions\InvalidSessionException();
}
$result = Db::Query('SELECT * from Sessions where SessionId = ?', [$sessionId], 'Session');
$result = Db::Query('
SELECT *
from Sessions
where SessionId = ?
', [$sessionId], 'Session');
if(sizeof($result) == 0){
throw new Exceptions\InvalidSessionException();

View file

@ -26,7 +26,12 @@ class User extends PropertiesBase{
*/
protected function GetPayments(): array{
if($this->_Payments === null){
$this->_Payments = Db::Query('select * from Payments where UserId = ? order by Created desc', [$this->UserId], 'Payment');
$this->_Payments = Db::Query('
SELECT *
from Payments
where UserId = ?
order by Created desc
', [$this->UserId], 'Payment');
}
return $this->_Payments;
@ -34,7 +39,11 @@ class User extends PropertiesBase{
protected function GetBenefits(): Benefits{
if($this->_Benefits === null){
$result = Db::Query('select * from Benefits where UserId = ?', [$this->UserId], 'Benefits');
$result = Db::Query('
SELECT *
from Benefits
where UserId = ?
', [$this->UserId], 'Benefits');
if(sizeof($result) == 0){
$this->_Benefits = new Benefits();
@ -70,7 +79,13 @@ class User extends PropertiesBase{
$this->Created = new DateTime();
try{
Db::Query('INSERT into Users (Email, Name, Uuid, Created) values (?, ?, ?, ?);', [$this->Email, $this->Name, $this->Uuid, $this->Created]);
Db::Query('
INSERT into Users (Email, Name, Uuid, Created)
values (?,
?,
?,
?)
', [$this->Email, $this->Name, $this->Uuid, $this->Created]);
}
catch(PDOException $ex){
if($ex->errorInfo[1] == 1062){
@ -91,7 +106,11 @@ class User extends PropertiesBase{
// ***********
public static function Get(?int $userId): User{
$result = Db::Query('SELECT * from Users where UserId = ?', [$userId], 'User');
$result = Db::Query('
SELECT *
from Users
where UserId = ?
', [$userId], 'User');
if(sizeof($result) == 0){
throw new Exceptions\InvalidUserException();
@ -105,7 +124,11 @@ class User extends PropertiesBase{
throw new Exceptions\InvalidUserException();
}
$result = Db::Query('SELECT * from Users where Email = ?', [$email], 'User');
$result = Db::Query('
SELECT *
from Users
where Email = ?
', [$email], 'User');
if(sizeof($result) == 0){
throw new Exceptions\InvalidUserException();
@ -122,7 +145,13 @@ class User extends PropertiesBase{
throw new Exceptions\InvalidUserException();
}
$result = Db::Query('SELECT u.* from Users u inner join Benefits using (UserId) where u.Email = ? or u.Uuid = ?', [$identifier, $identifier], 'User');
$result = Db::Query('
SELECT u.*
from Users u
inner join Benefits using (UserId)
where u.Email = ?
or u.Uuid = ?
', [$identifier, $identifier], 'User');
if(sizeof($result) == 0){
throw new Exceptions\InvalidUserException();