Explorar o código

updated PDO::insertOrUpdate

Piotr Labudda %!s(int64=9) %!d(string=hai) anos
pai
achega
bee0a22b69

+ 6 - 2
SE/se-lib/AntAclBase.php

@@ -1,11 +1,12 @@
 <?php
 
 /**
- * SE/schema/ant-object/default_db.{lower(rootTableName)}/{name}/build.xml
+ * SE/schema/ant-object/default_db.{rootTableName}/{name}/build.xml
  */
 class AntAclBase extends Core_AclBase {
 
   public $_zasobID = '';
+  public $_rootTableName = '';
 
   public function __construct($zasobID = 0) {
     $this->_zasobID = $zasobID;
@@ -13,7 +14,7 @@ class AntAclBase extends Core_AclBase {
   public function getName() { return $this->_name; }
   public function getNamespace() { return 'default_db/' . $this->getName(); }
   public function getSourceName() { return 'default_db'; }
-  public function getRootTableName() { throw new Exception("TODO: " . get_class($this) . "::F." . __FUNCTION__); }
+  public function getRootTableName() { $this->_rootTableName; }
 
   public static function buildInstance($idZasob, $conf = []) {
     static $_cache;
@@ -25,6 +26,9 @@ class AntAclBase extends Core_AclBase {
 		if (empty($conf)) throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
     DBG::nicePrint($conf, 'AntAclBase::buildInstance $conf');
     $_cache[$idZasob] = new AntAclBase($idZasob);
+    // TODO: define field by conf
+    // TODO: $this->_rootTableName - from namespace
+    // TODO: need file path
     return $_cache[$idZasob];
   }
 

+ 2 - 4
SE/se-lib/Core/Pdo.php

@@ -536,10 +536,8 @@ EOF_STRUCT_MYSQL;
 	// 	'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
 	// 	'A_RECORD_UPDATE_DATE' => 'NOW()',
 	// ]
-	public function insertOrUpdate($tableName, $uniqField, $uniqValue, $item, $sqlSchema = []) {
+	public function insertOrUpdate($tableName, $item, $sqlSchema = []) {
 		if (empty($tableName)) throw new Exception("Missing table name");
-		if (empty($uniqField)) throw new Exception("Missing uniq field name");
-		if (empty($uniqValue)) throw new Exception("Missing uniq field value");
 		if (empty($item) || !is_array($item)) throw new Exception("Missing item");
 		$sqlFields = [];
 		$sqlValues = [];
@@ -550,7 +548,7 @@ EOF_STRUCT_MYSQL;
 			$sqlVal = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
 			$sqlFields[] = "`{$field}`";
 			$sqlValues[] = $sqlVal;
-			if ($field !== $uniqField) $sqlUpdateSet[] = "`{$field}` = {$sqlVal}";
+			$sqlUpdateSet[] = "`{$field}` = {$sqlVal}";
 		}
 		if (!empty($item['@insert'])) {
 			foreach ($item['@insert'] as $field => $val) {

+ 8 - 110
SE/se-lib/Route/Storage.php

@@ -538,7 +538,7 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 			$sourceStorage = SchemaFactory::loadDefaultObject('SystemSource');
 			$sourceItem = $sourceStorage->getItem($idStorage);
 			if (!$sourceItem) throw new Exception("Storage id='{$idStorage}' not exists");
-			DBG::nicePrint($sourceItem, '$sourceItem');
+			DBG::log($sourceItem, 'array', '$sourceItem');
 
 			// TODO: fetch SystemObject childrens from $sourceItem:
 			// 1. 'SystemSource' -> getItems([ 'cols' => 'SystemObject/*', 'featureId' => $sourceItem['idZasob'] ])
@@ -550,6 +550,7 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 				$objectStorage->getTotal();
 			} catch (Exception $e) {
 				UI::alert('warning', $e->getMessage());
+				DBG::log($e);
 				$objectStorage->updateCache();
 				UI::alert('info', "Lista obiketów zaktualizowana");
 			}
@@ -602,108 +603,9 @@ jQuery(document).on('p5UIBtnAjax:Storage:checkObjectInstallAjax:ajaxLoaded', fun
 					jQuery.notify(payload.msg, payload.type);
 				});
 			");
-
-return;
-			echo '<hr>';
-
-			$storagePdo = DB::getStorage($idStorage);
-			$viewRealList = $storagePdo->getViewList();
-			$tableRealList = $storagePdo->getTableList();
-			$tableZasobList = array();
-			foreach (DB::getPDO()->fetchAllByKey("
-					select z.ID, z.`DESC`, z.A_STATUS
-					from CRM_LISTA_ZASOBOW z
-					where z.PARENT_ID = '{$idStorage}'
-				", $key = 'DESC') as $ind => $row) {
-				$tableZasobList[strtolower($ind)] = $row;
-			}
-
-			$emptyItem = array();
-			$emptyItem['nazwa'] = '';
-			$emptyItem['type'] = '';
-			$emptyItem['id_zasob'] = '';
-			$emptyItem['struktura'] = '';
-			$emptyItem['objectTest'] = '';
-			$emptyItem['xsd'] = '';
-			$emptyItem['isHist'] = '';
-			$emptyItem['uwagi'] = '';
-
-			$tableList = array();
-			foreach ($tableRealList as $row) {
-				$tblName = $row['table_name'];
-				$tblItem = V::cloneArray($emptyItem);
-				$tblItem['nazwa'] = $tblName;
-				$tblItem['type'] = $row['table_type'];
-				$tblItem['struktura'] = '<a href="index.php?_route=Storage&_task=tableStruct&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "struct" . '</a>';
-				$tblItem['xsd'] = '<a href="index.php?_route=Storage&_task=tableXsd&idStorage=' . $idStorage . '&table=' . $tblName . '" target="_blank">' . "xsd" . '</a>';
-				$tblItem['objectTest'] = '<a href="index.php?_route=Storage&_task=objectTest&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "objectTest" . '</a>';
-				$tblItem['isHist'] = ('_hist' == substr($tblName, -5));
-				$tblItem['uwagi'] = '';
-				$tblZasob = V::get($tblName, '', $tableZasobList);
-				if ($tblZasob) {
-					$tableZasobList[$tblName]['_checked'] = true;
-					$tblItem['id_zasob'] = $tblZasob['ID'];
-				} else {
-					$tblItem['uwagi'] .= 'TODO: ADD ZASOB';
-					$ajaxAddZasobLink = Request::getPathUri() . "index.php?_route=Storage&_task=addTableToZasoby&idStorage={$idStorage}&tblName={$tblName}";
-					$onClick = "return p5UI__ButtonAjax(this, 'p5UIBtnAjax:Storage:addTableToZasoby', { href: '{$ajaxAddZasobLink}' })";
-					$tblItem['id_zasob'] = '<a onclick="'.$onClick.'" class="btn btn-xs btn-primary" href="#">TODO: ADD ZASOB</a>';
-				}
-				$tableList[] = $tblItem;
-			}
-			foreach ($viewRealList as $row) {
-				$tblName = $row['table_name'];
-				$tblItem = V::cloneArray($emptyItem);
-				$tblItem['nazwa'] = $tblName;
-				$tblItem['type'] = $row['table_type'];
-				$tblItem['struktura'] = '<a href="index.php?_route=Storage&_task=tableStruct&idStorage=' . $idStorage . '&table=' . $tblName . '">' . "struct" . '</a>';
-				$tblItem['uwagi'] = '';
-				$tblZasob = V::get($tblName, '', $tableZasobList);
-				if ($tblZasob) {
-					$tableZasobList[$tblName]['_checked'] = true;
-					$tblItem['id_zasob'] = $tblZasob['ID'];
-				} else {
-					$tblItem['uwagi'] .= 'TODO: ADD ZASOB';
-					$tblItem['id_zasob'] = 'TODO: ADD ZASOB';
-				}
-				$tableList[] = $tblItem;
-			}
-			foreach ($tableZasobList as $tblName => $row) {
-				if (!$row['_checked']) {
-					$tblItem = V::cloneArray($emptyItem);
-					$tblItem['nazwa'] = $tblName;
-					$tblItem['id_zasob'] = $row['ID'];
-					$tblItem['type'] = 'unknown';
-					$tblItem['uwagi'] = 'TODO: nie istnieje w bazie danych';
-					$tableList[] = $tblItem;
-				}
-			}
-			usort($tableList, function($rowA, $rowB) {
-				$a = $rowA['nazwa']; $b = $rowB['nazwa'];
-				if ($a == $b) return 0;
-				$a1 = substr($a, 0, 1); $b1 = substr($b, 0, 1);
-				if (('_' == $a1 || '_' == $b1) && $a1 != $b1) {
-					return ($a1 < $b1) ? 1 : -1;
-				}
-				return ($a < $b) ? -1 : 1;
-			});
-			DBG::table("tableList", $tableList, __CLASS__, __FUNCTION__, __LINE__);
-			?>
-<script>
-jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:click', function(e, n, payload) {
-	console.log('event p5UIBtnAjax:Storage:addTableToZasoby:click', n, payload);
-});
-jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(e, n, payload) {
-	console.log('event p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', n, payload);
-	if ('success' == payload.type && payload.body && payload.body.id > 0) {
-		jQuery(n).parents('td').text(payload.body.id);
-	}
-	jQuery.notify(payload.msg, payload.type);
-});
-</script>
-			<?php
 		} catch (Exception $e) {
 			UI::alert('danger', "Error #" . $e->getCode() .  "|" . $e->getLine() .  ": " . $e->getMessage());
+			DBG::log($e);
 		}
 		UI::endContainer();
 		UI::dol();
@@ -1663,15 +1565,13 @@ jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(
 			UI::table([
 				'rows' => array_map(
 					function ($item) {
-						$exNs = explode('/', $item['namespace']);
-						$name = array_pop($exNs);
-						$typeName = implode('__x3A__', $exNs) . ':' . $name;
 						return [
 							'ns' => $item['namespace'],
-							'nazwa' => $name,
+							'nazwa' => $item['name'],
+							'type' => $item['_type'],
 							'edit' => '<a href="index.php?_route=ViewTableAjax&namespace=' . $item['namespace'] . '">edit</a>',
-							'wfs Describe' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=DescribeFeatureType&TYPENAME=' . $typeName . '">DescribeFeatureType</a>',
-							'wfs getFeature' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeature&TYPENAME=' . $typeName . '&MAXFEATURES=10">GetFeature</a> (max:10)'
+							'wfs Describe' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=DescribeFeatureType&TYPENAME=' . $item['typeName'] . '">DescribeFeatureType</a>',
+							'wfs getFeature' => '<a href="wfs-data.php/default_db/?SERVICE=WFS&VERSION=1.0.0&SRSNAME=EPSG:3003&REQUEST=GetFeature&TYPENAME=' . $item['typeName'] . '&MAXFEATURES=10">GetFeature</a> (max:10)'
 						];
 					}
 					, $acl->getItems([
@@ -1717,9 +1617,7 @@ jQuery(document).on('p5UIBtnAjax:Storage:addTableToZasoby:ajaxLoaded', function(
 
 	public function reinstallAntAcl($item) {
 		Lib::loadClass('AntAclBase');
-		$exNs = explode('/', $item['namespace']);
-		$name = array_pop($exNs);
-		$antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . implode('.', $exNs) . DS . $name;
+		$antAclPath = APP_PATH_SCHEMA . DS . 'ant-object' . DS . str_replace(['__x3A__', ':'], ['.', '/'], $item['typeName']);
 		if (!file_exists("{$antAclPath}/build.xml")) throw new Exception("Ant build file not exists");
 		DBG::log($antAclPath, 'string', '$antAclPath');
 		$antBin = APP_PATH_WWW . DS . 'stuff' . DS . 'dita-ot-2.3.3' . DS . 'bin' . DS . 'ant';

+ 19 - 24
SE/se-lib/Schema/SystemObjectStorageAcl.php

@@ -17,6 +17,8 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
       '_type' => [ '@type' => 'xsd:string' ],
       'isActive' => [ '@type' => 'xsd:integer' ], // installed
       'description' => [ '@type' => 'xsd:string' ],
+      'name' => [ '@type' => 'p5:string' ],
+      'typeName' => [ '@type' => 'p5:string' ],
       'reinstallLink' => [ '@type' => 'p5:www_link' ],
       // 'A_RECORD_CREATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'autor' ],
       // 'A_RECORD_CREATE_DATE' => [ '@type' => 'xsd:date' , '@label' => 'utworzono' ],
@@ -100,7 +102,6 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
   public function updateCache($idDatabase = null) {
     DBG::simpleLog('schema', "SystemObject::updateCache...");
     // DB::getPDO()->execSql(" drop table if exists `{$this->_rootTableName}` "); // TODO: DBG
-    DB::getPDO()->execSql(" update `{$this->_rootTableName}` set isActive = 0 ");
     DB::getPDO()->execSql("
       create table if not exists `{$this->_rootTableName}` (
         `idZasob` int(11) DEFAULT NULL,
@@ -115,6 +116,7 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
         KEY `isActive` (isActive)
       ) ENGINE=MyISAM  DEFAULT CHARSET=latin2
     ");
+    DB::getPDO()->execSql(" update `{$this->_rootTableName}` set isActive = 0 ");
     $idDefDB = DB::getPDO()->getZasobId();
 
     $sourceStorage = SchemaFactory::loadDefaultObject('SystemSource');
@@ -125,17 +127,12 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
 					try {
 						$acl = SchemaFactory::loadDefaultObject($clsName);
 						$namespace = $acl->getNamespace();
-						$name = $acl->getName();
-		        DB::getPDO()->execSql("
-		          insert ignore into `{$this->_rootTableName}` (namespace, idDatabase, _type, description, isActive)
-		            values (
-		              '{$source['nsPrefix']}/{$name}'
-									, '{$source['idZasob']}'
-                  , 'StorageAcl'
-									, '{$namespace}' -- test
-									, '1'
-		            )
-		        ");
+		        DB::getPDO()->insertOrUpdate($this->_rootTableName, [
+              'namespace' => $namespace,
+              'idDatabase' => $source['idZasob'],
+              '_type' => "StorageAcl",
+              'isActive' => 1
+            ]);
 					} catch (Exception $e) {
 						UI::alert('danger', $e->getMessage());
 					}
@@ -219,18 +216,13 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
         // $namespace = $acl->getNamespace();
         // $name = $acl->getName();
         $idDatabase = DB::getPDO($sourceName)->getZasobId();
-        $sqlNamespace = "{$sourceName}/{$rootTableName}/{$name}";
-        DB::getPDO()->execSql("
-          insert into `{$this->_rootTableName}` (namespace, idDatabase, _type, description, isActive)
-            values (
-              '{$sqlNamespace}'
-              , '{$idDatabase}'
-              , 'AntAcl'
-              , ''
-              , '1'
-            )
-          on duplicate key update idDatabase = '{$idDatabase}'
-        ");
+        $namespace = "{$sourceName}/{$rootTableName}/{$name}";
+        DB::getPDO()->insertOrUpdate($this->_rootTableName, [
+          'namespace' => $namespace,
+          'idDatabase' => $idDatabase,
+          '_type' => "AntAcl",
+          'isActive' => 1
+        ]);
       } catch (Exception $e) {
         UI::alert('danger', $e->getMessage());
       }
@@ -336,6 +328,9 @@ class Schema_SystemObjectStorageAcl extends Core_AclSimpleSchemaBase {
   }
 
   public function buildFeatureFromSqlRow($item) {
+    $exNs = explode('/', $item['namespace']);
+    $item['name'] = array_pop($exNs);
+    $item['typeName'] = implode('__x3A__', $exNs) . ':' . $item['name'];
     $item['reinstallLink'] = Router::getRoute('Storage')->getLink('objectReinstall', [ 'namespace' => $item['namespace'] ]);
     return $item;
   }

+ 3 - 3
SE/se-lib/Schema/SystemSourceStorageAcl.php

@@ -59,7 +59,7 @@ class Schema_SystemSourceStorageAcl extends Core_AclSimpleSchemaBase {
       $conf = parse_ini_file($file, true);
       if (!$conf) continue;
       if (in_array(V::get('type', '', $conf), ['mysql', 'pgsql', 'mssql'])) {
-        DB::getPDO()->insertOrUpdate($this->_rootTableName, 'idZasob', $idZasob, [
+        DB::getPDO()->insertOrUpdate($this->_rootTableName, [
           'idZasob' => $idZasob,
           'nsPrefix' => "zasob_{$idZasob}",
           'name' => V::get('database', $idZasob, $conf),
@@ -85,7 +85,7 @@ class Schema_SystemSourceStorageAcl extends Core_AclSimpleSchemaBase {
         and z.A_STATUS not in ('DELETED')
     ") as $item) {
       $desc = V::get('DESC', $item['ID'], $item);
-      DB::getPDO()->insertOrUpdate($this->_rootTableName, 'idZasob', $item['ID'], [
+      DB::getPDO()->insertOrUpdate($this->_rootTableName, [
         'idZasob' => $item['ID'],
         'nsPrefix' => "zasob_{$item['ID']}",
         'name' => $desc,
@@ -132,7 +132,7 @@ class Schema_SystemSourceStorageAcl extends Core_AclSimpleSchemaBase {
       }
     }
     if (!$id) throw new Exception("Missing default_objects ID");
-    DB::getPDO()->insertOrUpdate($this->_rootTableName, 'idZasob', $id, [
+    DB::getPDO()->insertOrUpdate($this->_rootTableName, [
       'idZasob' => $id,
       'nsPrefix' => 'default_objects',
       'name' => 'default_objects',