From 89b6fcd2a448d057163a8ba0eccb1c1bdeed86e7 Mon Sep 17 00:00:00 2001 From: David Lee Date: Tue, 16 Jan 2024 10:08:02 +0100 Subject: [PATCH] Added isTableExist Feature --- CHANGELOG.md | 12 ++++++ Helper/DbHelper.php | 90 ++++++++++++++++++++++++--------------------- composer.json | 2 +- etc/module.xml | 2 +- 4 files changed, 62 insertions(+), 44 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b740608 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +All notable changes to this extension will be documented in this file. + +## [0.3.2] +* Added isTableExits feature + +## Previous features + +- ... + + diff --git a/Helper/DbHelper.php b/Helper/DbHelper.php index cc54acd..72e5395 100644 --- a/Helper/DbHelper.php +++ b/Helper/DbHelper.php @@ -10,17 +10,17 @@ class DbHelper extends AbstractHelper { /** @var Monolog $logger **/ public $logger; - + /** @var Resource $resource **/ protected $resource; - + protected $debug = 0; - + protected $entityTypeProductId = null; protected $entityTypeCustomerId = null; protected $entityTypeAddressId = null; protected $attributeIds = []; - + protected $entityTypes = []; protected $eavAttributesWithType = []; @@ -35,7 +35,7 @@ public function __construct( $this->logger = $logger; $this->resource = $resource; } - + /** * Gets the full Table name, adds prefix if used... * @param string $tableName @@ -46,7 +46,13 @@ public function getTableName(string $tableName) : string $this->resource->getConnection('core_read'); return $this->resource->getTableName($tableName); } - + + public function isTableExists(string $tableName): bool + { + $connection = $this->resource->getConnection('core_read'); + return $connection->isTableExists($tableName); + } + /** * Use this for read operations on DB * @param string $sql @@ -65,7 +71,7 @@ public function sqlRead(string $sql) : array } return (array) $results; } - + /** * Use this for read operations on DB returning a single value from one row * @param string $sql @@ -84,7 +90,7 @@ public function sqlReadOne(string $sql) : string } return (string) $results; } - + /** * Do a simple write operation * @param string $sql @@ -96,7 +102,7 @@ public function sqlWrite(string $sql) } $connection = $this->resource->getConnection('core_write'); $results = $connection->query($sql); - + if ($this->debug === 1) { echo $sql."\n"; echo "sqlWrite(): ".date("H:i:s", time()-$startTime)."\n"; @@ -107,8 +113,8 @@ public function sqlWrite(string $sql) return false; } } - - public function sqlWriteArray(string $tableName, array $data) + + public function sqlWriteArray(string $tableName, array $data) { $connection = $this->resource->getConnection('core_write'); $results = $connection->insert($tableName, $data); @@ -118,14 +124,14 @@ public function sqlWriteArray(string $tableName, array $data) return false; } } - - public function sqlEscape($input) : string + + public function sqlEscape($input) : string { $connection = $this->resource->getConnection('core_write'); $ouput = $connection->quote($input); return $ouput; } - + /** * Fetches Id of Entity Type from "eav_entity_type" * @param string $code @@ -144,7 +150,7 @@ public function getEntityTypeId(string $code): int } return (int) $entityTypeId; } - + /** * Get attribute Id from "eav_attribute" * @param string $code @@ -165,9 +171,9 @@ public function getEavAttributeId(string $code, int $entityTypeId): int $this->eavAttributesWithType[$entityTypeId][$code] = $attributeId; return $attributeId; } - + /** - * Get Attribute id for product + * Get Attribute id for product * @param string $code * @return int */ @@ -176,9 +182,9 @@ public function getProductAttributeId(string $code) : int $entityTypeId = $this->getEntityTypeId("catalog_product"); return (int) $this->getEavAttributeId($code, $entityTypeId); } - + /** - * + * * @param string $code * @return int */ @@ -187,9 +193,9 @@ public function getCustomerAttributeId(string $code) : int $entityTypeId = $this->getEntityTypeId("customer"); return (int) $this->getEavAttributeId($code, $entityTypeId); } - + /** - * + * * @param string $code * @return int */ @@ -198,18 +204,18 @@ public function getAddressAttributeId(string $code) : int $entityTypeId = $this->getEntityTypeId("customer_address"); return (int) $this->getEavAttributeId($code, $entityTypeId); } - + /** - * + * * @param int $productId * @param string $attributeCode * @param string $attributeType int, varchar, text or datetime * @return string */ public function getCustomProductAttribute( - int $productId, - string $attributeCode, - string $attributeType = "varchar", + int $productId, + string $attributeCode, + string $attributeType = "varchar", $storeId = null ) : string { @@ -228,10 +234,10 @@ public function getCustomProductAttribute( if ((int) $storeId > 0 && !$value && $value !== "0") { return $this->getCustomProductAttribute($productId, $attributeCode, $attributeType, 0); } - + return (string) $value; } - + public function getAllStores() : array { $tableName = $this->getTableName('store'); @@ -390,7 +396,7 @@ public function setProductToCategory($arrCategoryProduct) . implode(", ", $arrCategoryProduct).";"; $this->sqlWrite($sql); } - + public function isProductInCategory(int $productId, int $categoryId) : bool { $table = $this->getTableName('catalog_category_product'); @@ -404,7 +410,7 @@ public function isProductInCategory(int $productId, int $categoryId) : bool } return false; } - + public function getCategoryIdsByPath(string $categoryPath, int $storeId = 0) : array { $arrIds = []; @@ -415,7 +421,7 @@ public function getCategoryIdsByPath(string $categoryPath, int $storeId = 0) : a $nameAttributeId = $this->getEavAttributeId("name", $entityTypeId); $arrParts = explode("/", $categoryPath); foreach ($arrParts as $category) { - $categoryName = str_replace("⁄", "\/", trim($category)); + $categoryName = str_replace("⁄", "\/", trim($category)); $sql = "SELECT * FROM `".$table."` WHERE `parent_id` = '".$parentId."' " . "AND `entity_id` IN (SELECT `entity_id` FROM `".$nameTable."` " . "WHERE `value` LIKE '".trim($category)."' " @@ -431,15 +437,15 @@ public function getCategoryIdsByPath(string $categoryPath, int $storeId = 0) : a } return $arrIds; } - + public function getCategoryIdByPath(string $categoryPath, int $storeId = 0) : int { $arrIds = $this->getCategoryIdsByPath($categoryPath, $storeId); return (int) array_pop($arrIds); } - - - + + + /** * Returns the id of the Default Category * @param int $storeId @@ -468,25 +474,25 @@ public function loadExistingAttributeSets() $existingSets = $this->sqlRead($sql); return $existingSets; } - - public function beginTransaction() + + public function beginTransaction() { $connection = $this->resource->getConnection('core_write'); $connection->beginTransaction(); } - - public function commitTransaction() + + public function commitTransaction() { $connection = $this->resource->getConnection('core_write'); $connection->commit(); } - - public function rollbackTransaction() + + public function rollbackTransaction() { $connection = $this->resource->getConnection('core_write'); $connection->rollback(); } - + /** * Do a simple write operation * @param string $sql diff --git a/composer.json b/composer.json index 7b2d194..a43d7a6 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "arcmedia/mage2-dbhelper", "description": "Methods to get Data from the Magento DB faster", "type": "magento2-module", - "version": "0.3.1", + "version": "0.3.2", "license": [ "MIT" ], diff --git a/etc/module.xml b/etc/module.xml index 755dd7a..047ca1e 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +