Author: dr
Date: Mon Mar  3 15:47:15 2008
New Revision: 7498

Log:
- Some refactoring.

Added:
    trunk/Search/src/abstraction/implementations/
Removed:
    trunk/Search/src/abstraction/query.php
Modified:
    trunk/Search/design/class_diagram.png
    trunk/Search/src/abstraction/query_find.php
    trunk/Search/src/handlers/solr.php
    trunk/Search/src/managers/xml_manager.php
    trunk/Search/src/search_autoload.php
    trunk/Search/src/search_session.php
    trunk/Search/src/structs/document_definition.php
    trunk/Search/tests/managers/testfiles/article.xml
    trunk/Search/tests/managers/xml_test.php
    trunk/Search/tests/session_test.php

Modified: trunk/Search/design/class_diagram.png
==============================================================================
Binary files - no diff available.

Modified: trunk/Search/src/abstraction/query_find.php
==============================================================================
--- trunk/Search/src/abstraction/query_find.php [iso-8859-1] (original)
+++ trunk/Search/src/abstraction/query_find.php [iso-8859-1] Mon Mar  3 
15:47:15 2008
@@ -15,97 +15,8 @@
  * @version //autogentag//
  * @mainclass
  */
-class ezcSearchFindQuery extends ezcSearchQuery
+interface ezcSearchFindQuery extends ezcSearchQuery
 {
-    /**
-     * Sort the result ascending.
-     */
-    const ASC = 'ASC';
-
-    /**
-     * Sort the result descending.
-     */
-    const DESC = 'DESC';
-
-    /**
-     * Stores the SELECT part of the SQL.
-     *
-     * Everything from 'SELECT' until 'FROM' is stored.
-     * @var string
-     */
-    protected $selectString = null;
-
-    /**
-     * Stores the FROM part of the SQL.
-     *
-     * Everything from 'FROM' until 'WHERE' is stored.
-     * @var string
-     */
-    protected $fromString = null;
-
-    /**
-     * Stores the WHERE part of the SQL.
-     *
-     * Everything from 'WHERE' until 'GROUP', 'LIMIT', 'ORDER' or 'SORT' is 
stored.
-     * @var string
-     */
-    protected $whereString = null;
-
-    /**
-     * Stores the FACET part of SQL
-     *
-     * @var string
-     */
-    protected $facetString = null;
-
-    /**
-     * Stores the ORDER BY part of the SQL.
-     *
-     * @var string
-     */
-    protected $orderString = null;
-
-    /**
-     * Stores the LIMIT part of the SQL.
-     *
-     * @var string
-     */
-    protected $limitString = null;
-
-    /**
-     * Stores the name of last invoked SQL clause method.
-     *
-     * Could be 'select', 'from', 'where', 'group', 'having', 'order', 'limit'
-     * @var string
-     */
-    protected $lastInvokedMethod = null;
-
-    /**
-     * Constructs a new ezcSearchQuery object.
-     *
-     * @param ezcSearchHandler $handler a pointer to the search backend
-     */
-    public function __construct( ezcSearchHandler $handler )
-    {
-        parent::__construct( $handler );
-    }
-
-    /**
-     * Resets the query object for reuse.
-     *
-     * @return void
-     */
-    public function reset()
-    {
-        $this->selectString = null;
-        $this->fromString = null;
-        $this->whereString = null;
-        $this->facetString = null;
-        $this->orderString = null;
-        $this->limitString = null;
-        $this->lastInvokedClauseMethod = null;
-    }
-
     /**
      * Opens the query and selects which columns you want to return with
      * the query.
@@ -137,167 +48,7 @@
      * @param string|array(string) $... Either a string with a column name or 
an array of column names.
      * @return ezcQuery returns a pointer to $this.
      */
-    public function select()
-    {
-        if ( $this->selectString == null )
-        {
-            $this->selectString = 'SELECT ';
-        }
-
-        $args = func_get_args();
-        $cols = self::arrayFlatten( $args );
-
-        if ( count( $cols ) < 1 )
-        {
-            throw new ezcQueryVariableParameterException( 'select', count( 
$args ), 1 );
-        }
-        $this->lastInvokedMethod = 'select';
-        $cols = $this->getIdentifiers( $cols );
-
-        // glue string should be inserted each time but not before first entry
-        if ( ( $this->selectString !== 'SELECT ' ) &&
-             ( $this->selectString !== 'SELECT DISTINCT ' ) )
-        {
-            $this->selectString .= ', ';
-        }
-
-        $this->selectString .= join( ', ', $cols );
-        return $this;
-    }
-
-    /**
-     * Adds a where clause with logical expressions to the query.
-     *
-     * where() accepts an arbitrary number of parameters. Each parameter
-     * must contain a logical expression or an array with logical expressions.
-     * If you specify multiple logical expression they are connected using
-     * a logical and.
-     *
-     * Multiple calls to where() will join the expressions using a logical and.
-     *
-     * Example:
-     * <code>
-     * $q->select( '*' )->from( 'table' )->where( $q->expr->eq( 'id', 1 ) );
-     * </code>
-     *
-     * @throws ezcQueryVariableParameterException if called with no parameters.
-     * @param string|array(string) $... Either a string with a logical 
expression name
-     * or an array with logical expressions.
-     * @return ezcQuerySelect
-     */
-    public function where()
-    {
-        if ( $this->whereString == null )
-        {
-            $this->whereString = 'WHERE ';
-        }
-
-        $args = func_get_args();
-        $expressions = self::arrayFlatten( $args );
-        if ( count( $expressions ) < 1 )
-        {
-            throw new ezcQueryVariableParameterException( 'where', count( 
$args ), 1 );
-        }
-
-        $this->lastInvokedMethod = 'where';
-
-        // glue string should be inserted each time but not before first entry
-        if ( $this->whereString != 'WHERE ' )
-        {
-            $this->whereString .= ' AND ';
-        }
-
-        $this->whereString .= join( ' AND ', $expressions );
-        return $this;
-    }
-
-
-    /**
-     * Returns SQL that limits the result set.
-     *
-     * $limit controls the maximum number of rows that will be returned.
-     * $offset controls which row that will be the first in the result
-     * set from the total amount of matching rows.
-     *
-     * Example:
-     * <code>
-     * $q->select( '*' )->from( 'table' )
-     *                  ->limit( 10, 0 );
-     * </code>
-     *
-     * LIMIT is not part of SQL92. It is implemented here anyway since all
-     * databases support it one way or the other and because it is
-     * essential.
-     *
-     * @param string $limit integer expression
-     * @param string $offset integer expression
-     * @return ezcQuerySelect
-     */
-    public function limit( $limit, $offset = '' )
-    {
-        if ( $offset === '' )
-        {
-            $this->limitString = "LIMIT {$limit}";
-        }
-        else
-        {
-            $this->limitString = "LIMIT {$limit} OFFSET {$offset}";
-        }
-        $this->lastInvokedMethod = 'limit';
-
-        return $this;
-    }
-
-    /**
-     * Returns SQL that orders the result set by a given column.
-     *
-     * You can call orderBy multiple times. Each call will add a
-     * column to order by.
-     *
-     * Example:
-     * <code>
-     * $q->select( '*' )->from( 'table' )
-     *                  ->orderBy( 'id' );
-     * </code>
-     *
-     * @param string $column a column name in the result set
-     * @param string $type if the column should be sorted ascending or 
descending.
-     *        you can specify this using ezcQuerySelect::ASC or 
ezcQuerySelect::DESC
-     * @return ezcQuery a pointer to $this
-     */
-    public function orderBy( $column, $type = self::ASC )
-    {
-        $string = $this->getIdentifier( $column );
-        if ( $type == self::DESC )
-        {
-            $string .= ' DESC';
-        }
-        if ( $this->orderString == '' )
-        {
-            $this->orderString = "ORDER BY {$string}";
-        }
-        else
-        {
-            $this->orderString .= ", {$string}";
-        }
-        $this->lastInvokedMethod = 'order';
-
-        return $this;
-    }
-
-    /**
-     * Returns the complete select query string.
-     *
-     * This method uses the build methods to build the
-     * various parts of the select query.
-     *
-     * @todo add newlines? easier for debugging
-     * @throws ezcQueryInvalidException if it was not possible to build a 
valid query.
-     * @return string
-     */
-    public function getQuery()
-    {
-    }
+    public function select();
 }
 
 ?>

Modified: trunk/Search/src/handlers/solr.php
==============================================================================
--- trunk/Search/src/handlers/solr.php [iso-8859-1] (original)
+++ trunk/Search/src/handlers/solr.php [iso-8859-1] Mon Mar  3 15:47:15 2008
@@ -248,6 +248,7 @@
         }
 
         $result = $this->sendRawGetCommand( 'select', $queryFlags );
+        var_dump($result);
         $result = json_decode( $result );
         return ezcSearchResult::createFromResponse( $result );
     }
@@ -262,26 +263,85 @@
         return 'solr';
     }
 
-    public function createFindQuery( $type = false )
-    {
+    /**
+     * Creates a search query object with the fields from the definition 
filled in.
+     *
+     * @param string $type
+     * @return ezcSearchFindQuery
+     */
+    public function createFindQuery( $type = false, 
ezcSearchDocumentDefinition $definition = null )
+    {
+        $query = new ezcSearchFindQuerySolr( $this, $definition );
+        $query->select( 'score' );
+        if ( $type )
+        {
+            $selectFieldNames = array();
+            foreach ( $definition->getSelectFieldNames() as $docProp )
+            {
+                $selectFieldNames[] = $this->mapFieldType( $docProp, 
$definition->fields[$docProp]->type );
+            }
+            $query->select( $selectFieldNames );
+            $query->where( $query->eq( 'ezcsearch_type_s', $type ) );
+        }
+        return $query;
     }
 
     public function find( ezcSearchFindQuery $query )
     {
-    }
-
-    private function mapFieldType( $name, $type )
+        $queryWord = join( ' ', $query->whereClauses );
+        $resultFieldList = $query->resultFields;
+
+        return $this->search( $queryWord, '', array(), $resultFieldList );
+    }
+
+    public function mapFieldType( $name, $type )
     {
         $map = array(
             ezcSearchDocumentDefinition::STRING => '_s',
             ezcSearchDocumentDefinition::TEXT => '_t',
             ezcSearchDocumentDefinition::HTML => '_t',
-            ezcSearchDocumentDefinition::DATE => '_dt',
+            ezcSearchDocumentDefinition::DATE => '_l',
         );
         return $name . $map[$type];
     }
 
-    private function mapFieldValue( $field, $values )
+    public function mapFieldValue( $field, $value )
+    {
+        switch ( $field->type )
+        {
+            case ezcSearchDocumentDefinition::STRING:
+            case ezcSearchDocumentDefinition::TEXT:
+            case ezcSearchDocumentDefinition::HTML:
+                $value = trim( $value );
+                if ( strpbrk( $value, ' "' ) !== false )
+                {
+                    $value = '"' . str_replace( '"', '\"', $value ) . '"';
+                }
+                break;
+
+            case ezcSearchDocumentDefinition::DATE:
+                if ( is_numeric( $value ) )
+                {
+                    $d = new DateTime( "@$value" );
+                    $value = $d->format( 'U' );
+                }
+                else
+                {
+                    try
+                    {
+                        $d = new DateTime( $value );
+                    }
+                    catch ( Exception $e )
+                    {
+                        throw new ezcSearchInvalidValueException( $type, 
$value );
+                    }
+                    $value = $d->format( 'U' );
+                }
+        }
+        return $value;
+    }
+
+    public function mapFieldValues( $field, $values )
     {
         if ( !is_array( $values ) )
         {
@@ -289,27 +349,7 @@
         }
         foreach ( $values as &$value )
         {
-            switch( $field->type )
-            {
-                case ezcSearchDocumentDefinition::DATE:
-                    if ( is_numeric( $value ) )
-                    {
-                        $d = new DateTime( "@$value" );
-                        $value = $d->format( 'Y-m-d\TH:i:s\Z' );
-                    }
-                    else
-                    {
-                        try
-                        {
-                            $d = new DateTime( $value );
-                        }
-                        catch ( Exception $e )
-                        {
-                            throw new ezcSearchInvalidValueException( $type, 
$value );
-                        }
-                        $value = $d->format( 'Y-m-d\TH:i:s\Z' );
-                    }
-            }
+            $value = $this->mapFieldValue( $field, $value );
         }
         return $values;
     }
@@ -328,13 +368,20 @@
         $xml->openMemory();
         $xml->startElement( 'add' );
         $xml->startElement( 'doc' );
+
+        $xml->startElement( 'field' );
+        $xml->writeAttribute( 'name', 'ezcsearch_type_s' );
+        $xml->text( $definition->documentType );
+        $xml->endElement();
+
         $xml->startElement( 'field' );
         $xml->writeAttribute( 'name', 'id' );
         $xml->text( $document[$definition->idProperty] );
         $xml->endElement();
+
         foreach ( $definition->fields as $field )
         {
-            $value = $this->mapFieldValue( $field, $document[$field->field] );
+            $value = $this->mapFieldValues( $field, $document[$field->field] );
             foreach ( $value as $fieldValue )
             {
                 $xml->startElement( 'field' );

Modified: trunk/Search/src/managers/xml_manager.php
==============================================================================
--- trunk/Search/src/managers/xml_manager.php [iso-8859-1] (original)
+++ trunk/Search/src/managers/xml_manager.php [iso-8859-1] Mon Mar  3 15:47:15 
2008
@@ -70,7 +70,7 @@
 
     private function parseDefinitionXml( $documentType, $path, 
SimpleXMLElement $s )
     {
-        $def = new ezcSearchDocumentDefinition;
+        $def = new ezcSearchDocumentDefinition( $documentType );
 
         foreach ( $s->field as $field )
         {
@@ -88,7 +88,12 @@
                 throw new ezcSearchDefinitionInvalidException( 'XML', 
$documentType, $path, "Unknown type: {$type}" );
             }
             $type = $this->typeMap[$type];
-            $fields[(string) $field] = new ezcSearchDefinitionDocumentField( 
(string) $field, $type, (float) $field['boost'], (bool) $field['inResult'] );
+            $boost = (float) $field['boost'];
+            if ( $boost == 0 )
+            {
+                $boost = 1;
+            }
+            $fields[(string) $field] = new ezcSearchDefinitionDocumentField( 
(string) $field, $type, $boost, ((string) $field['inResult']) !== 'false' );
         }
         $def->fields = $fields;
 

Modified: trunk/Search/src/search_autoload.php
==============================================================================
--- trunk/Search/src/search_autoload.php [iso-8859-1] (original)
+++ trunk/Search/src/search_autoload.php [iso-8859-1] Mon Mar  3 15:47:15 2008
@@ -16,17 +16,19 @@
     'ezcSearchDefinitionMissingIdPropertyException' => 
'Search/exceptions/missing_id.php',
     'ezcSearchDefinitionNotFoundException'          => 
'Search/exceptions/definition_not_found.php',
     'ezcSearchTransactionException'                 => 
'Search/exceptions/transaction.php',
+    'ezcSearchQuery'                                => 
'Search/abstraction/query.php',
     'ezcSearchDefinitionManager'                    => 
'Search/interfaces/definition_manager.php',
+    'ezcSearchFindQuery'                            => 
'Search/abstraction/query_find.php',
     'ezcSearchHandler'                              => 
'Search/interfaces/handler.php',
     'ezcSearchIndexHandler'                         => 
'Search/interfaces/index_handler.php',
-    'ezcSearchQuery'                                => 
'Search/abstraction/query.php',
     'ezcSearchCodeManager'                          => 
'Search/managers/code_manager.php',
     'ezcSearchDefinitionDocumentField'              => 
'Search/structs/document_field_definition.php',
     'ezcSearchDeleteHandler'                        => 
'Search/handlers/delete_handler.php',
     'ezcSearchDocumentDefinition'                   => 
'Search/structs/document_definition.php',
     'ezcSearchFindHandler'                          => 
'Search/handlers/find_handler.php',
-    'ezcSearchFindQuery'                            => 
'Search/abstraction/query_find.php',
+    'ezcSearchFindQuerySolr'                        => 
'Search/abstraction/implementations/find_solr.php',
     'ezcSearchQueryInsert'                          => 
'Search/abstraction/query_index.php',
+    'ezcSearchQueryTools'                           => 
'Search/abstraction/query_tools.php',
     'ezcSearchResult'                               => 
'Search/search_result.php',
     'ezcSearchSession'                              => 
'Search/search_session.php',
     'ezcSearchSessionInstance'                      => 
'Search/search_session_instance.php',

Modified: trunk/Search/src/search_session.php
==============================================================================
--- trunk/Search/src/search_session.php [iso-8859-1] (original)
+++ trunk/Search/src/search_session.php [iso-8859-1] Mon Mar  3 15:47:15 2008
@@ -80,9 +80,9 @@
      *
      * @return array(object($class))
      */
-    public function find( ezcSearchQuery $query, $type )
-    {
-        return $this->findHandler->find( $query, $type );
+    public function find( ezcSearchQuery $query )
+    {
+        return $this->handler->find( $query );
     }
 
     /**
@@ -108,7 +108,7 @@
      */
     public function findIterator( ezcQuerySelect $query, $type )
     {
-        return $this->findHandler->findIterator( $query, $type );
+        return $this->handler->findIterator( $query, $type );
     }
 
     /**
@@ -127,11 +127,13 @@
      *
      * @param string $type
      *
-     * @return ezcQuerySelect
+     * @return ezcSearchFindQuery
      */
     public function createFindQuery( $type )
     {
-        return $this->findHandler->createFindQuery( $type );
+        $def = $this->definitionManager->fetchDefinition( $type );
+
+        return $this->handler->createFindQuery( $type, $def );
     }
 
     /**
@@ -267,11 +269,8 @@
     {
         switch ( $name )
         {
-            case 'database':
             case 'definitionManager':
-            case 'findHandler':
-            case 'indexHandler':
-            case 'deleteHandler':
+            case 'handler':
                 throw new ezcBasePropertyPermissionException( $name, 
ezcBasePropertyPermissionException::READ );
 
             default:

Modified: trunk/Search/src/structs/document_definition.php
==============================================================================
--- trunk/Search/src/structs/document_definition.php [iso-8859-1] (original)
+++ trunk/Search/src/structs/document_definition.php [iso-8859-1] Mon Mar  3 
15:47:15 2008
@@ -6,12 +6,31 @@
     const HTML = 3;
     const DATE = 4;
 
+    public $documentType;
     public $idProperty = null;
     public $fields = array();
+
+    public function __construct( $documentType )
+    {
+        $this->documentType = strtolower( $documentType );
+    }
 
     public function getFieldNames()
     {
         return array_keys( $this->fields );
     }
+
+    public function getSelectFieldNames()
+    {
+        $fields = array();
+        foreach ( $this->fields as $name => $def )
+        {
+            if ( $def->inResult )
+            {
+                $fields[] = $name;
+            }
+        }
+        return $fields;
+    }
 }
 ?>

Modified: trunk/Search/tests/managers/testfiles/article.xml
==============================================================================
--- trunk/Search/tests/managers/testfiles/article.xml [iso-8859-1] (original)
+++ trunk/Search/tests/managers/testfiles/article.xml [iso-8859-1] Mon Mar  3 
15:47:15 2008
@@ -2,7 +2,7 @@
 <document>
     <field type="id">id</field>
     <field type="string" boost="2">title</field>
-    <field type="text">summary</field>
-    <field type="html">body</field>
+    <field inResult="true" type="text">summary</field>
+    <field inResult="false" type="html">body</field>
     <field type="date">published</field>
 </document>

Modified: trunk/Search/tests/managers/xml_test.php
==============================================================================
--- trunk/Search/tests/managers/xml_test.php [iso-8859-1] (original)
+++ trunk/Search/tests/managers/xml_test.php [iso-8859-1] Mon Mar  3 15:47:15 
2008
@@ -104,6 +104,10 @@
         self::assertEquals( ezcSearchDocumentDefinition::HTML, 
$d->fields['body']->type );
         self::assertEquals( ezcSearchDocumentDefinition::DATE, 
$d->fields['published']->type );
         self::assertEquals( 2, $d->fields['title']->boost );
+
+        self::assertEquals( false, $d->fields['body']->inResult );
+        self::assertEquals( true, $d->fields['summary']->inResult );
+        self::assertEquals( true, $d->fields['title']->inResult );
     }
 
     public static function suite()

Modified: trunk/Search/tests/session_test.php
==============================================================================
--- trunk/Search/tests/session_test.php [iso-8859-1] (original)
+++ trunk/Search/tests/session_test.php [iso-8859-1] Mon Mar  3 15:47:15 2008
@@ -85,6 +85,61 @@
         $r = $this->backend->search( 'Rethans', 'author_t', array( 
'summary_t', 'title_t', 'body_t' ), array( 'author_t', 'title_t', 'score', 
'summary_t', 'published_dt' ), array( 'author_t', 'title_t', 'score', 
'summary_t' ) );
         self::assertEquals( 1, $r->resultCount );
     }
+
+    public function testCreateFindQuery1()
+    {
+        $session = new ezcSearchSession( $this->backend, new 
ezcSearchXmlManager( $this->testFilesDir ) );
+
+        $a = new Article( null, 'Test Article Eén', 'This is the first 
article to test', 'the body of the article', time() );
+        $session->index( $a );
+        $a = new Article( null, 'Test Article Twee', 'This is the second 
article to test', 'the body of the article', time() );
+        $session->index( $a );
+
+        $q = $session->createFindQuery( 'article' );
+        $q->where( $q->eq( 'body', 'article' ) )->where( $q->eq( 'title', 
'Test' ) );
+
+        $r = $session->find( $q );
+    }
+
+    public function testCreateFindQueryOr()
+    {
+        $session = new ezcSearchSession( $this->backend, new 
ezcSearchXmlManager( $this->testFilesDir ) );
+
+        $a = new Article( null, 'Test Article Eén', 'This is the first 
article to test', 'the body of the article', time() );
+        $session->index( $a );
+        $a = new Article( null, 'Test Article Twee', 'This is the second 
article to test', 'the body of the article', time() );
+        $session->index( $a );
+
+        $q = $session->createFindQuery( 'article' );
+        $q->where( $q->lOr( $q->eq( 'title', 'Nul' ), $q->eq( 'title', 'Drie' 
) ) );
+        $r = $session->find( $q );
+
+        $q = $session->createFindQuery( 'article' );
+        $q->where( $q->lOr( $q->eq( 'title', 'Eén' ), $q->eq( 'title', 'Drie' 
) ) );
+        $r = $session->find( $q );
+
+        $q = $session->createFindQuery( 'article' );
+        $q->where( $q->lOr( $q->eq( 'title', 'Twee' ), $q->eq( 'title', 'Drie' 
) ) );
+        $r = $session->find( $q );
+
+        $q = $session->createFindQuery( 'article' );
+        $q->where( $q->lOr( $q->eq( 'title', 'Eén' ), $q->eq( 'title', 'Twee' 
) ) );
+        $r = $session->find( $q );
+    }
+
+    public function testCreateFindQueryNot()
+    {
+        $session = new ezcSearchSession( $this->backend, new 
ezcSearchXmlManager( $this->testFilesDir ) );
+
+        $a = new Article( null, 'Test Article Eén', 'This is the first 
article to test', 'the body of the article', time() - 86400 );
+        $session->index( $a );
+        $a = new Article( null, 'Test Article Twee', 'This is the second 
article to test', 'the body of the article', time() );
+        $session->index( $a );
+
+        $q = $session->createFindQuery( 'article' );
+        $q->where( $q->not( $q->eq( 'title', 'Twee' ) ) );
+        $r = $session->find( $q );
+    }
 }
 
 ?>


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to