In oils_cstore.c: fix autojoin, i.e. the facility for constructing
authorscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 16 Apr 2009 12:48:54 +0000 (12:48 +0000)
committerscottmk <scottmk@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 16 Apr 2009 12:48:54 +0000 (12:48 +0000)
a join when the JSON query doesn't specify the columns involved.

When looking up a foreign key linkage in the IDL, ignore the parent
table's end of the link (where reltype = "has_many"), because the
parent doesn't know the name of the column at the other end.

Now an autojoin can work regardless of which table is subordinate
to which.

git-svn-id: svn://svn.open-ils.org/ILS/trunk@12886 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/c-apps/oils_cstore.c

index 7f4ab4b..8a1778e 100644 (file)
@@ -2247,7 +2247,6 @@ static char* searchJOIN ( const jsonObject* join_hash, osrfHash* leftmeta ) {
                                jsonIteratorFree(search_itr);
                                return NULL;
                        }
-                       field = field;
 
                } else if (!field && !fkey) {
                        osrfHash* _links = oilsIDL_links( leftclass );
@@ -2260,10 +2259,15 @@ static char* searchJOIN ( const jsonObject* join_hash, osrfHash* leftmeta ) {
                                const char* other_class = osrfHashGet( curr_link, "class" );
                                if( other_class && !strcmp( other_class, class ) ) {
 
-                                       // Found a link between the classes
-                                       fkey = osrfHashIteratorKey( itr );
-                                       field = osrfHashGet( curr_link, "key" );
-                                       break;
+                                       // In the IDL, the parent class doesn't know then names of the child
+                                       // columns that are pointing to it, so don't use that end of the link
+                                       const char* reltype = osrfHashGet( curr_link, "reltype" );
+                                       if( reltype && strcmp( reltype, "has_many" ) ) {
+                                               // Found a link between the classes
+                                               fkey = osrfHashIteratorKey( itr );
+                                               field = osrfHashGet( curr_link, "key" );
+                                               break;
+                                       }
                                }
                        }
                        osrfHashIteratorFree( itr );
@@ -2280,10 +2284,15 @@ static char* searchJOIN ( const jsonObject* join_hash, osrfHash* leftmeta ) {
                                        const char* other_class = osrfHashGet( curr_link, "class" );
                                        if( other_class && !strcmp( other_class, leftclass ) ) {
 
-                                               // Found a link between the classes
-                                               fkey = osrfHashIteratorKey( itr );
-                                               field = osrfHashGet( curr_link, "key" );
-                                               break;
+                                               // In the IDL, the parent class doesn't know then names of the child
+                                               // columns that are pointing to it, so don't use that end of the link
+                                               const char* reltype = osrfHashGet( curr_link, "reltype" );
+                                               if( reltype && strcmp( reltype, "has_many" ) ) {
+                                                       // Found a link between the classes
+                                                       field = osrfHashIteratorKey( itr );
+                                                       fkey = osrfHashGet( curr_link, "key" );
+                                                       break;
+                                               }
                                        }
                                }
                                osrfHashIteratorFree( itr );