Skip to content

Commit

Permalink
Merge pull request #6 from arcmedia/feature/isTableExist
Browse files Browse the repository at this point in the history
Added isTableExist Feature
  • Loading branch information
leedave authored Jan 16, 2024
2 parents 7845c02 + 89b6fcd commit a68b0f0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

- ...


90 changes: 48 additions & 42 deletions Helper/DbHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -84,7 +90,7 @@ public function sqlReadOne(string $sql) : string
}
return (string) $results;
}

/**
* Do a simple write operation
* @param string $sql
Expand All @@ -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";
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -144,7 +150,7 @@ public function getEntityTypeId(string $code): int
}
return (int) $entityTypeId;
}

/**
* Get attribute Id from "eav_attribute"
* @param string $code
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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
{
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand All @@ -404,7 +410,7 @@ public function isProductInCategory(int $productId, int $categoryId) : bool
}
return false;
}

public function getCategoryIdsByPath(string $categoryPath, int $storeId = 0) : array
{
$arrIds = [];
Expand All @@ -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)."' "
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Arcmedia_DbHelper" setup_version="0.3.0">
<module name="Arcmedia_DbHelper" setup_version="0.3.2">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
Expand Down

0 comments on commit a68b0f0

Please sign in to comment.