Преглед изворни кода

fixed ref view to use charset

Piotr Labudda пре 8 година
родитељ
комит
0a8867f9a2
1 измењених фајлова са 25 додато и 12 уклоњено
  1. 25 12
      SE/se-lib/RefConfig.php

+ 25 - 12
SE/se-lib/RefConfig.php

@@ -32,7 +32,7 @@ Lib::loadClass('SchemaVersionUpgrade');
 
 class RefConfig {
 
-	static $REF_TABLE_VERSION = 2;
+	static $REF_TABLE_VERSION = 3;
 
 	static function isActive($objectNamespace, $childTypeName) {
 		$refInfo = self::fetch($objectNamespace, $childTypeName);
@@ -46,14 +46,13 @@ class RefConfig {
 		if (array_key_exists($cacheKey, $cacheRefConfigs)) return $cacheRefConfigs[$cacheKey];
 
 		$rootAcl = ACL::getAclByNamespace($rootObjectNamespace);
-		// $item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($rootObjectNamespace, [ 'propertyName' => '*,field' ]);
 		if (!($rootAcl instanceof AntAclBase)) throw new Exception("Ref allowed only for AntAcl objects");
 		$fieldInfo = $rootAcl->_getField($childTypeName); // throws Exception if field not exists
 
 		$refConfig = self::fetch($rootObjectNamespace, $childTypeName, $childNamespace);
 		if ('WAITING' == $refConfig->status || $refConfig->version < self::$REF_TABLE_VERSION) {
 			$typeField = Type_Field::build($fieldInfo);
-			self::update($objectNamespace, $childTypeName, $typeField, $refConfig);
+			self::update($rootObjectNamespace, $childTypeName, $typeField);
 			$refConfig = self::fetch($rootObjectNamespace, $childTypeName, $childNamespace);
 		}
 
@@ -96,7 +95,7 @@ class RefConfig {
 			':CHILD_NS' => $childNamespace,
 		]);
 		if (empty($refInfo)) {
-			$refInfo = [ 'ID' => 0, 'A_STATUS' => 'WAITING', 'VERSION' => self::$REF_TABLE_VERSION, 'SOURCE' => 'table',
+			$refInfo = [ 'ID' => 0, 'A_STATUS' => 'WAITING', 'VERSION' => 1, 'SOURCE' => 'table',
 				'ROOT_OBJECT_NS' => $rootObjectNamespace,
 				'CHILD_NAME' => $childName,
 				'CHILD_NS' => $childNamespace,
@@ -105,7 +104,7 @@ class RefConfig {
 				'ROOT_OBJECT_NS' => $rootObjectNamespace,
 				'CHILD_NAME' => $childName,
 				'CHILD_NS' => $childNamespace,
-				'VERSION' => self::$REF_TABLE_VERSION
+				'VERSION' => 1 // need update @see getRefConfig - require update SOURCE if needed
 			]);
 		}
 
@@ -231,12 +230,18 @@ class RefConfig {
 
 	static function installRefView($objectNamespace, $childTypeName, Type_Field $typeField, Type_RefConfig $refConfig = null) {
 		if (!$refConfig) $refConfig = self::fetch($objectNamespace, $childTypeName);
-		$viewSelectSql = RefConfig::generateRefSelectSqlByFlatRelationCache($objectNamespace, $childTypeName, $typeField);
-		DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refConfig->tableName}` AS {$viewSelectSql} ");
+		Lib::loadClass('SchemaFactory');
+		$item = SchemaFactory::loadDefaultObject('SystemObject')->getItem($objectNamespace, [ 'propertyName' => '*,field' ]);
+		$appInfo = (!empty($item['appInfo'])) ? @json_decode($item['appInfo'], $assoc = true) : null;
+		$charset = (!empty($appInfo) && !empty($appInfo['table_structure']['@charset'])) ? $appInfo['table_structure']['@charset'] : null;
+		$viewSelectSql = RefConfig::generateRefSelectSqlByFlatRelationCache($objectNamespace, $childTypeName, $typeField, $charset);
+		$refTableViewName = Type_RefConfig::generateTableName($refConfig->id, 'view');
+		DB::getPDO()->execSql(" CREATE OR REPLACE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `{$refTableViewName}` AS {$viewSelectSql} ");
 
 		$affected = DB::getPDO()->update("CRM_REF_CONFIG", 'ID', $refConfig->id, [
 			'SOURCE' => 'view',
 			'A_STATUS' => "NORMAL",
+			'VERSION' => self::$REF_TABLE_VERSION,
 		]);
 	}
 
@@ -287,7 +292,7 @@ class RefConfig {
 		return $sql;
 	}
 
-	static function generateRefSelectSqlByFlatRelationCache($rootObjectNamespace, $childName, Type_Field $typeField) { // CRM_REF_CONFIG
+	static function generateRefSelectSqlByFlatRelationCache($rootObjectNamespace, $childName, Type_Field $typeField, $charset = 'utf8') { // CRM_REF_CONFIG
 		$appInfo = $typeField->appInfo;
 		if (empty($appInfo)) throw new Exception("Empty app:info for field '{$rootObjectNamespace}/{$childName}'");
 
@@ -362,13 +367,17 @@ class RefConfig {
 			if (!empty($rootLocalFieldsWithRestrictions)) {
 				$sqlTablePrefix = 'root';
 				$sqlWhereFromRestrictions = array_reduce(
-					array_map(function ($field) use ($sqlTablePrefix) {
+					array_map(function ($field) use ($sqlTablePrefix, $charset) {
 						$sqlRestrictions = [];
 						// 'xsdRestrictions' => '{"enumeration":{"PROCES":"PROCES"}}',
 						$restrictions = @json_decode($field['xsdRestrictions'], $assoc = true);
 						if (!empty($restrictions)) {
 							if (!empty($restrictions['enumeration'])) {
-								$sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($restrictions['enumeration']))) . ")";
+								$sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map(function ($option) use ($charset) {
+									return ($charset && $charset !== 'utf8')
+									?	"CONVERT(" . DB::getPDO()->quote($option) . " using {$charset})"
+									:	DB::getPDO()->quote($option);
+								}, array_keys($restrictions['enumeration']))) . ")";
 							}
 						}
 						return $sqlRestrictions;
@@ -382,13 +391,17 @@ class RefConfig {
 			if (!empty($childLocalFieldsWithRestrictions)) {
 				$sqlTablePrefix = 'child';
 				$sqlWhereFromRestrictions = array_reduce(
-					array_map(function ($field) use ($sqlTablePrefix) {
+					array_map(function ($field) use ($sqlTablePrefix, $charset) {
 						$sqlRestrictions = [];
 						// 'xsdRestrictions' => '{"enumeration":{"PROCES":"PROCES"}}',
 						$restrictions = @json_decode($field['xsdRestrictions'], $assoc = true);
 						if (!empty($restrictions)) {
 							if (!empty($restrictions['enumeration'])) {
-								$sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map([DB::getPDO(), 'quote'], array_keys($restrictions['enumeration']))) . ")";
+								$sqlRestrictions[] = "{$sqlTablePrefix}.`{$field['fieldNamespace']}` in (" . implode(",", array_map(function ($option) use ($charset) {
+									return ($charset && $charset !== 'utf8')
+									?	"CONVERT(" . DB::getPDO()->quote($option) . " using {$charset})"
+									:	DB::getPDO()->quote($option);
+								}, array_keys($restrictions['enumeration']))) . ")";
 							}
 						}
 						return $sqlRestrictions;