function makesql($condition, $onlyCount = false)
 {
     $tables = $wheres = array();
     $isarchive = $condition['tablename'] === 'archive' ? true : false;
     if ($condition['verify']) {
         foreach ($condition['verify'] as $key => $value) {
             $condition[$value] = 1;
         }
         unset($condition['verify']);
     }
     if ($condition['fid']) {
         $condition['level'] = '1,2,3,4';
     }
     if ($condition['tagid']) {
         $condition['idtype'] = 'uid';
     }
     $fields = membersearch::getfield();
     foreach ($fields as $key => $value) {
         $return = array();
         if (isset($condition[$key])) {
             $return = membersearch::makeset($key, $condition[$key], membersearch::gettype($key));
         } elseif (isset($condition[$key . '_low']) || isset($condition[$key . '_high'])) {
             $return = membersearch::makerange($key, $condition[$key . '_low'], $condition[$key . '_high'], membersearch::gettype($key));
         } elseif (isset($condition[$key . '_noempty'])) {
             $return = membersearch::makeset($key, $condition[$key . '_noempty'], membersearch::gettype($key));
         } elseif (isset($condition[$key . '_after']) || isset($condition[$key . '_before'])) {
             $condition[$key . '_after'] = dmktime($condition[$key . '_after']);
             $condition[$key . '_before'] = dmktime($condition[$key . '_before']);
             $return = membersearch::makerange($key, $condition[$key . '_after'], $condition[$key . '_before'], membersearch::gettype($key));
         }
         if ($return) {
             $tables[$return['table']] = true;
             $wheres[] = $return['where'];
         }
     }
     if ($tables && $wheres) {
         $parts = array();
         $table1 = $asuid = '';
         $uidfield = 'uid';
         foreach ($tables as $key => $value) {
             $value = membersearch::gettable($key, $isarchive);
             $parts[] = "{$value} as {$key}";
             if (!$table1) {
                 $table1 = $key;
                 if ($table1 == 'tag') {
                     $uidfield = 'itemid';
                     $asuid = ' as uid';
                 }
             } else {
                 if ($key == 'tag') {
                     $keyuid = 'itemid';
                 } else {
                     $keyuid = 'uid';
                 }
                 $wheres[] = $table1 . '.' . $uidfield . ' = ' . $key . '.' . $keyuid;
             }
         }
         $selectsql = $onlyCount ? 'SELECT COUNT(DISTINCT ' . $table1 . '.' . $uidfield . ') as cnt ' : 'SELECT DISTINCT ' . $table1 . '.' . $uidfield . $asuid;
         return $selectsql . ' FROM ' . implode(', ', $parts) . ' WHERE ' . implode(' AND ', $wheres);
     } else {
         $selectsql = $onlyCount ? 'SELECT COUNT(uid) as cnt ' : 'SELECT uid';
         return $selectsql . ' FROM ' . DB::table('common_member' . ($isarchive ? '_archive' : '')) . " WHERE 1";
     }
 }
示例#2
0
 function makesql($condition, $onlyCount = false)
 {
     $tables = $wheres = array();
     $fields = membersearch::getfield();
     foreach ($fields as $key => $value) {
         $return = array();
         if (isset($condition[$key])) {
             $return = membersearch::makeset($key, $condition[$key], membersearch::gettype($key));
         } elseif (isset($condition[$key . '_low']) || isset($condition[$key . '_high'])) {
             $return = membersearch::makerange($key, $condition[$key . '_low'], $condition[$key . '_high'], membersearch::gettype($key));
         } elseif (isset($condition[$key . '_after']) || isset($condition[$key . '_before'])) {
             $condition[$key . '_after'] = dmktime($condition[$key . '_after']);
             $condition[$key . '_before'] = dmktime($condition[$key . '_before']);
             $return = membersearch::makerange($key, $condition[$key . '_after'], $condition[$key . '_before'], membersearch::gettype($key));
         }
         if ($return) {
             $tables[$return['table']] = true;
             $wheres[] = $return['where'];
         }
     }
     if ($tables && $wheres) {
         $parts = array();
         $table1 = '';
         foreach ($tables as $key => $value) {
             $value = membersearch::gettable($key);
             $parts[] = "{$value} as {$key}";
             if (!$table1) {
                 $table1 = $key;
             } else {
                 $wheres[] = $table1 . '.uid = ' . $key . '.uid';
             }
         }
         $selectsql = $onlyCount ? 'SELECT COUNT(' . $table1 . '.uid) as cnt ' : 'SELECT ' . $table1 . '.uid';
         return $selectsql . ' FROM ' . implode(', ', $parts) . ' WHERE ' . implode(' AND ', $wheres);
     } else {
         $selectsql = $onlyCount ? 'SELECT COUNT(uid) as cnt ' : 'SELECT uid';
         return $selectsql . ' FROM ' . DB::table('common_member') . " WHERE 1";
     }
 }