r66143 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66142‎ | r66143 | r66144 >
Date:16:33, 10 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r66141
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2_Queries.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2_Queries.php
@@ -175,6 +175,8 @@
176176 * step-wise execution of the query might lead to better performance, since
177177 * it exploits the tree-structure of the joins, which is important for fast
178178 * processing -- not all DBMS might be able in seeing this by themselves.
 179+ *
 180+ * @param SMWQuery $query
179181 */
180182 public function getQueryResult( SMWQuery $query ) {
181183 global $smwgIgnoreQueryErrors, $smwgQSortingSupport;
@@ -254,7 +256,7 @@
255257 * the proper debug output for the given query.
256258 *
257259 * @param SMWQuery $query
258 - * @param $rootid
 260+ * @param integer $rootid
259261 */
260262 protected function getDebugQueryResult( SMWQuery $query, $rootid ) {
261263 $qobj = $this->m_queries[$rootid];
@@ -307,8 +309,11 @@
308310 /**
309311 * Using a preprocessed internal query description referenced by $rootid, compute
310312 * the proper counting output for the given query.
 313+ *
 314+ * @param SMWQuery $query
 315+ * @param integer $rootid
311316 */
312 - protected function getCountQueryResult( $query, $rootid ) {
 317+ protected function getCountQueryResult( SMWQuery $query, $rootid ) {
313318 wfProfileIn( 'SMWSQLStore2Queries::getCountQueryResult (SMW)' );
314319 $qobj = $this->m_queries[$rootid];
315320 if ( $qobj->joinfield === '' ) { // empty result, no query needed
@@ -338,7 +343,7 @@
339344 * we want here. It would be nice if we could eliminate the bug in POSTGRES as well.
340345 *
341346 * @param SMWQuery $query
342 - * @param $rootid
 347+ * @param integer $rootid
343348 *
344349 * @return SMWQueryResult
345350 */
@@ -766,24 +771,31 @@
767772 * Process stored queries and change store accordingly. The query obj is modified
768773 * so that it contains non-recursive description of a select to execute for getting
769774 * the actual result.
 775+ *
 776+ * @param SMWSQLStore2Query $query
770777 */
771778 protected function executeQueries( SMWSQLStore2Query &$query ) {
772779 global $wgDBtype;
 780+
773781 switch ( $query->type ) {
774 - case SMW_SQL2_TABLE: // normal query with conjunctive subcondition
 782+ case SMW_SQL2_TABLE: // Normal query with conjunctive subcondition.
775783 foreach ( $query->components as $qid => $joinfield ) {
776784 $subquery = $this->m_queries[$qid];
777785 $this->executeQueries( $subquery );
778 - if ( $subquery->jointable != '' ) { // join with jointable.joinfield
 786+
 787+ if ( $subquery->jointable != '' ) { // Join with jointable.joinfield
779788 $query->from .= ' INNER JOIN ' . $this->m_dbs->tableName( $subquery->jointable ) . " AS $subquery->alias ON $joinfield=" . $subquery->joinfield;
780 - } elseif ( $subquery->joinfield !== '' ) { // require joinfield as "value" via WHERE
 789+ } elseif ( $subquery->joinfield !== '' ) { // Require joinfield as "value" via WHERE.
781790 $condition = '';
 791+
782792 foreach ( $subquery->joinfield as $value ) {
783793 $condition .= ( $condition ? ' OR ':'' ) . "$joinfield=" . $this->m_dbs->addQuotes( $value );
784794 }
 795+
785796 if ( count( $subquery->joinfield ) > 1 ) {
786797 $condition = "($condition)";
787798 }
 799+
788800 $query->where .= ( ( $query->where == '' ) ? '':' AND ' ) . $condition;
789801 } else { // interpret empty joinfields as impossible condition (empty result)
790802 $query->joinfield = ''; // make whole query false
@@ -792,36 +804,47 @@
793805 $query->from = '';
794806 break;
795807 }
 808+
796809 if ( $subquery->where != '' ) {
797810 $query->where .= ( ( $query->where == '' ) ? '':' AND ' ) . '(' . $subquery->where . ')';
798811 }
 812+
799813 $query->from .= $subquery->from;
800814 }
 815+
801816 $query->components = array();
802817 break;
803818 case SMW_SQL2_CONJUNCTION:
804819 // pick one subquery with jointable as anchor point ...
805820 reset( $query->components );
806821 $key = false;
 822+
807823 foreach ( $query->components as $qkey => $qid ) {
808824 if ( $this->m_queries[$qkey]->jointable != '' ) {
809825 $key = $qkey;
810826 break;
811827 }
812828 }
 829+
813830 if ( $key !== false ) {
814831 $result = $this->m_queries[$key];
815832 unset( $query->components[$key] );
816 - $this->executeQueries( $result ); // execute it first (may change jointable and joinfield, e.g. when making temporary tables)
817 - // ... and append to this query the remaining queries
 833+
 834+ // Execute it first (may change jointable and joinfield, e.g. when making temporary tables)
 835+ $this->executeQueries( $result );
 836+
 837+ // ... and append to this query the remaining queries.
818838 foreach ( $query->components as $qid => $joinfield ) {
819839 $result->components[$qid] = $result->joinfield;
820840 }
821 - $this->executeQueries( $result ); // second execute, now incorporating remaining conditions
822 - } else { // only fixed values in conjunction, make a new value without joining
 841+
 842+ // Second execute, now incorporating remaining conditions.
 843+ $this->executeQueries( $result );
 844+ } else { // Only fixed values in conjunction, make a new value without joining.
823845 $key = $qkey;
824846 $result = $this->m_queries[$key];
825847 unset( $query->components[$key] );
 848+
826849 foreach ( $query->components as $qid => $joinfield ) {
827850 if ( $result->joinfield != $this->m_queries[$qid]->joinfield ) {
828851 $result->joinfield = ''; // all other values should already be ''
@@ -835,11 +858,14 @@
836859 if ( $this->m_qmode !== SMWQuery::MODE_DEBUG ) {
837860 $this->m_dbs->query( $this->getCreateTempIDTableSQL( $this->m_dbs->tableName( $query->alias ) ), 'SMW::executeQueries' );
838861 }
 862+
839863 $this->m_querylog[$query->alias] = array();
 864+
840865 foreach ( $query->components as $qid => $joinfield ) {
841866 $subquery = $this->m_queries[$qid];
842867 $this->executeQueries( $subquery );
843868 $sql = '';
 869+
844870 if ( $subquery->jointable != '' ) {
845871 $sql = 'INSERT ' . ( ( $wgDBtype == 'postgres' ) ? '':'IGNORE ' ) . 'INTO ' .
846872 $this->m_dbs->tableName( $query->alias ) .
@@ -849,21 +875,25 @@
850876 // NOTE: this works only for single "unconditional" values without further
851877 // WHERE or FROM. The execution must take care of not creating any others.
852878 $values = '';
 879+
853880 foreach ( $subquery->joinfield as $value ) {
854881 $values .= ( $values ? ',':'' ) . '(' . $this->m_dbs->addQuotes( $value ) . ')';
855882 }
 883+
856884 $sql = 'INSERT ' . ( ( $wgDBtype == 'postgres' ) ? '':'IGNORE ' ) . 'INTO ' . $this->m_dbs->tableName( $query->alias ) . " (id) VALUES $values";
857885 } // else: // interpret empty joinfields as impossible condition (empty result), ignore
858886 if ( $sql ) {
859887 $this->m_querylog[$query->alias][] = $sql;
 888+
860889 if ( $this->m_qmode !== SMWQuery::MODE_DEBUG ) {
861890 $this->m_dbs->query( $sql , 'SMW::executeQueries' );
862891 }
863892 }
864893 }
 894+
865895 $query->jointable = $query->alias;
866896 $query->joinfield = "$query->alias.id";
867 - $query->sortfields = array(); // make sure we got no sortfields
 897+ $query->sortfields = array(); // Make sure we got no sortfields.
868898 // TODO: currently this eliminates sortkeys, possibly keep them (needs different temp table format though, maybe not such a good thing to do)
869899 break;
870900 case SMW_SQL2_PROP_HIERARCHY: case SMW_SQL2_CLASS_HIERARCHY: // make a saturated hierarchy
@@ -876,45 +906,58 @@
877907 /**
878908 * Find subproperties or subcategories. This may require iterative computation,
879909 * and temporary tables are used in many cases.
 910+ *
 911+ * @param SMWSQLStore2Query $query
880912 */
881 - protected function executeHierarchyQuery( &$query ) {
 913+ protected function executeHierarchyQuery( SMWSQLStore2Query &$query ) {
882914 global $wgDBtype;
 915+ global $smwgQSubpropertyDepth, $smwgQSubcategoryDepth;
 916+
883917 $fname = "SMWSQLStore2Queries::executeQueries-hierarchy-$query->type (SMW)";
884918 wfProfileIn( $fname );
885 - global $smwgQSubpropertyDepth, $smwgQSubcategoryDepth;
886 - $depth = ( $query->type == SMW_SQL2_PROP_HIERARCHY ) ? $smwgQSubpropertyDepth:$smwgQSubcategoryDepth;
 919+
 920+ $depth = ( $query->type == SMW_SQL2_PROP_HIERARCHY ) ? $smwgQSubpropertyDepth : $smwgQSubcategoryDepth;
 921+
887922 if ( $depth <= 0 ) { // treat as value, no recursion
888923 $query->type = SMW_SQL2_VALUE;
889924 wfProfileOut( $fname );
890925 return;
891926 }
 927+
892928 $values = '';
893929 $valuecond = '';
 930+
894931 foreach ( $query->joinfield as $value ) {
895932 $values .= ( $values ? ',':'' ) . '(' . $this->m_dbs->addQuotes( $value ) . ')';
896933 $valuecond .= ( $valuecond ? ' OR ':'' ) . 'o_id=' . $this->m_dbs->addQuotes( $value );
897934 }
 935+
898936 $smwtable = $this->m_dbs->tableName( ( $query->type == SMW_SQL2_PROP_HIERARCHY ) ? 'smw_subp2':'smw_subs2' );
899 - // try to safe time (SELECT is cheaper than creating/dropping 3 temp tables):
 937+
 938+ // Try to safe time (SELECT is cheaper than creating/dropping 3 temp tables):
900939 $res = $this->m_dbs->select( $smwtable, 's_id', $valuecond, array( 'LIMIT' => 1 ) );
 940+
901941 if ( !$this->m_dbs->fetchObject( $res ) ) { // no subobjects, we are done!
902942 $this->m_dbs->freeResult( $res );
903943 $query->type = SMW_SQL2_VALUE;
904944 wfProfileOut( $fname );
905945 return;
906946 }
 947+
907948 $this->m_dbs->freeResult( $res );
908949 $tablename = $this->m_dbs->tableName( $query->alias );
909950 $this->m_querylog[$query->alias] = array( "Recursively computed hierarchy for element(s) $values." );
910951 $query->jointable = $query->alias;
911952 $query->joinfield = "$query->alias.id";
 953+
912954 if ( $this->m_qmode == SMWQuery::MODE_DEBUG ) {
913955 wfProfileOut( $fname );
914 - return; // no real queries in debug mode
 956+ return; // No real queries in debug mode.
915957 }
 958+
916959 $this->m_dbs->query( $this->getCreateTempIDTableSQL( $tablename ), 'SMW::executeHierarchyQuery' );
917960
918 - if ( array_key_exists( $values, $this->m_hierarchies ) ) { // just copy known result
 961+ if ( array_key_exists( $values, $this->m_hierarchies ) ) { // Just copy known result.
919962 $this->m_dbs->query( "INSERT INTO $tablename (id) SELECT id" .
920963 ' FROM ' . $this->m_hierarchies[$values],
921964 'SMW::executeHierarchyQuery' );
@@ -930,28 +973,33 @@
931974 $tmpres = 'smw_res';
932975 $this->m_dbs->query( $this->getCreateTempIDTableSQL( $tmpnew ), 'SMW::executeQueries' );
933976 $this->m_dbs->query( $this->getCreateTempIDTableSQL( $tmpres ), 'SMW::executeQueries' );
934 - $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? "":"IGNORE" ) . " INTO $tablename (id) VALUES $values", 'SMW::executeHierarchyQuery' );
935 - $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? "":"IGNORE" ) . " INTO $tmpnew (id) VALUES $values", 'SMW::executeHierarchyQuery' );
 977+ $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? "" : "IGNORE" ) . " INTO $tablename (id) VALUES $values", 'SMW::executeHierarchyQuery' );
 978+ $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? "" : "IGNORE" ) . " INTO $tmpnew (id) VALUES $values", 'SMW::executeHierarchyQuery' );
936979
937980 for ( $i = 0; $i < $depth; $i++ ) {
938 - $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? '':'IGNORE ' ) . "INTO $tmpres (id) SELECT s_id" . ( $wgDBtype == 'postgres' ? '::integer':'' ) . " FROM $smwtable, $tmpnew WHERE o_id=id",
 981+ $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? '' : 'IGNORE ' ) . "INTO $tmpres (id) SELECT s_id" . ( $wgDBtype == 'postgres' ? '::integer':'' ) . " FROM $smwtable, $tmpnew WHERE o_id=id",
939982 'SMW::executeHierarchyQuery' );
940983 if ( $this->m_dbs->affectedRows() == 0 ) { // no change, exit loop
941984 break;
942985 }
943 - $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? '':'IGNORE ' ) . "INTO $tablename (id) SELECT $tmpres.id FROM $tmpres",
 986+
 987+ $this->m_dbs->query( "INSERT " . ( ( $wgDBtype == 'postgres' ) ? '' : 'IGNORE ' ) . "INTO $tablename (id) SELECT $tmpres.id FROM $tmpres",
944988 'SMW::executeHierarchyQuery' );
 989+
945990 if ( $this->m_dbs->affectedRows() == 0 ) { // no change, exit loop
946991 break;
947992 }
 993+
948994 $this->m_dbs->query( 'TRUNCATE TABLE ' . $tmpnew, 'SMW::executeHierarchyQuery' ); // empty "new" table
949995 $tmpname = $tmpnew;
950996 $tmpnew = $tmpres;
951997 $tmpres = $tmpname;
952998 }
 999+
9531000 $this->m_hierarchies[$values] = $tablename;
954 - $this->m_dbs->query( ( ( $wgDBtype == 'postgres' ) ? 'DROP TABLE IF EXISTS smw_new':'DROP TEMPORARY TABLE smw_new' ), 'SMW::executeHierarchyQuery' );
955 - $this->m_dbs->query( ( ( $wgDBtype == 'postgres' ) ? 'DROP TABLE IF EXISTS smw_res':'DROP TEMPORARY TABLE smw_res' ), 'SMW::executeHierarchyQuery' );
 1001+ $this->m_dbs->query( ( ( $wgDBtype == 'postgres' ) ? 'DROP TABLE IF EXISTS smw_new' : 'DROP TEMPORARY TABLE smw_new' ), 'SMW::executeHierarchyQuery' );
 1002+ $this->m_dbs->query( ( ( $wgDBtype == 'postgres' ) ? 'DROP TABLE IF EXISTS smw_res' : 'DROP TEMPORARY TABLE smw_res' ), 'SMW::executeHierarchyQuery' );
 1003+
9561004 wfProfileOut( $fname );
9571005 }
9581006
@@ -960,7 +1008,7 @@
9611009 * in the SMWQuery $query. It is always required that $qid is the id of a query that joins with
9621010 * smw_ids so that the field alias.smw_title is $available for default sorting.
9631011 *
964 - * @param $qid
 1012+ * @param integer $qid
9651013 */
9661014 protected function applyOrderConditions( $qid ) {
9671015 $qobj = $this->m_queries[$qid];
@@ -999,6 +1047,9 @@
10001048
10011049 /**
10021050 * Get a SQL option array for the given query and preprocessed query object at given id.
 1051+ *
 1052+ * @param SMWQuery $query
 1053+ * @param integer $rootid
10031054 */
10041055 protected function getSQLOptions( SMWQuery $query, $rootid ) {
10051056 global $smwgQSortingSupport, $smwgQRandSortingSupport;
@@ -1040,6 +1091,8 @@
10411092 * a table exists, so the code is ready to reuse existing tables if the code was modified to
10421093 * keep them after query answering. Also, PostgreSQL tables will use a RULE to achieve built-in
10431094 * duplicate elimination. The latter is done using INSERT IGNORE in MySQL.
 1095+ *
 1096+ * @param string $tablename
10441097 */
10451098 protected function getCreateTempIDTableSQL( $tablename ) {
10461099 global $wgDBtype;
@@ -1063,4 +1116,4 @@
10641117 }
10651118 }
10661119
1067 -}
 1120+}
\ No newline at end of file

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r66141Cleaned up code and improved code and doc stylesjeroendedauw16:17, 10 May 2010

Status & tagging log