use sql statement

This commit is contained in:
Jean-Christian Denis 2023-04-22 11:18:07 +02:00
parent 78b0c4d577
commit 512738c98d
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951

View file

@ -17,6 +17,10 @@ namespace Dotclear\Plugin\filesAlias;
use dcCore; use dcCore;
use dcMedia; use dcMedia;
use dcRecord; use dcRecord;
use Dotclear\Database\Statement\{
DeleteStatement,
SelectStatement
};
use Exception; use Exception;
class Utils class Utils
@ -26,12 +30,19 @@ class Utils
// nullsafe // nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id; $blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
return new dcRecord(dcCore::app()->con->select( $sql = new SelectStatement();
'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' . $rs = $sql->from(dcCore::app()->prefix . My::ALIAS_TABLE_NAME)
'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . ->columns([
"WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " . 'filesalias_url',
'ORDER BY filesalias_url ASC ' 'filesalias_destination',
)); 'filesalias_password',
'filesalias_disposable',
])
->where('blog_id = ' . $sql->quote($blog_id))
->order('filesalias_url ASC')
->select();
return is_null($rs) ? dcRecord::newFromArray([]) : $rs;
} }
public static function getAlias(string $url): dcRecord public static function getAlias(string $url): dcRecord
@ -39,13 +50,20 @@ class Utils
// nullsafe // nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id; $blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
return new dcRecord(dcCore::app()->con->select( $sql = new SelectStatement();
'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' . $rs = $sql->from(dcCore::app()->prefix . My::ALIAS_TABLE_NAME)
'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . ->columns([
"WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " . 'filesalias_url',
"AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' " . 'filesalias_destination',
'ORDER BY filesalias_url ASC ' 'filesalias_password',
)); 'filesalias_disposable',
])
->where('blog_id = ' . $sql->quote($blog_id))
->and('filesalias_url = ' . $sql->quote($url))
->order('filesalias_url ASC')
->select();
return is_null($rs) ? dcRecord::newFromArray([]) : $rs;
} }
public static function updateAliases(array $aliases): void public static function updateAliases(array $aliases): void
@ -71,11 +89,11 @@ class Utils
public static function createAlias(string $url, string $destination, bool $disposable = false, ?string $password = null): void public static function createAlias(string $url, string $destination, bool $disposable = false, ?string $password = null): void
{ {
if (!$url) { if (empty($url)) {
throw new Exception(__('File URL is empty.')); throw new Exception(__('File URL is empty.'));
} }
if (!$destination) { if (empty($destination)) {
throw new Exception(__('File destination is empty.')); throw new Exception(__('File destination is empty.'));
} }
@ -96,10 +114,10 @@ class Utils
// nullsafe // nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id; $blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
dcCore::app()->con->execute( $sql = new DeleteStatement();
'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . $sql->from(dcCore::app()->prefix . My::ALIAS_TABLE_NAME)
"WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " ->where('blog_id = ' . $sql->quote($blog_id))
); ->delete();
} }
public static function deleteAlias(string $url): void public static function deleteAlias(string $url): void
@ -107,11 +125,11 @@ class Utils
// nullsafe // nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id; $blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
dcCore::app()->con->execute( $sql = new DeleteStatement();
'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . $sql->from(dcCore::app()->prefix . My::ALIAS_TABLE_NAME)
"WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " . ->where('blog_id = ' . $sql->quote($blog_id))
"AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' " ->and('filesalias_url = ' . $sql->quote($url))
); ->delete();
} }
public static function getMediaId(string $target): int public static function getMediaId(string $target): int
@ -121,12 +139,12 @@ class Utils
return 0; return 0;
} }
$strReq = 'SELECT media_id ' . $sql = new SelectStatement();
'FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' . $rs = $sql->from(dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME)
"WHERE media_path = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->settings->get('system')->get('public_path')) . "' " . ->column('media_id')
"AND media_file = '" . dcCore::app()->con->escapeStr($target) . "' "; ->where('media_path = ' . $sql->quote((string) dcCore::app()->blog->settings->get('system')->get('public_path')))
->and('media_file = ' . $sql->quote($target))
$rs = dcCore::app()->con->select($strReq); ->select();
return $rs->count() ? (int) $rs->f('media_id') : 0; return $rs->count() ? (int) $rs->f('media_id') : 0;
} }