'); if (!$name) { $name = parse_config($config, ''); $user = parse_config($config, ''); $passwd = parse_config($config, ''); $host = parse_config($config, ''); } else { $user = parse_config($config, ''); $passwd = parse_config($config, ''); $host = parse_config($config, ''); } if ($host == null) { $host = "localhost"; } $instance = new DbConn(); $retval = $instance->init_conn($user, $passwd, $host, $name); if (!$retval) return null; } return $instance; } static function escape_string($string) { $db = self::get(); return $db->base_escape_string($string); } } class BossaUser { static $cache; static function lookup_userid($id) { $db = BossaDb::get(); return $db->lookup('bossa_user', 'BossaUser', "user_id=$id"); } static function insert($clause) { $db = BossaDb::get(); return $db->insert('bossa_user', $clause); } static function lookup(&$user) { if (!$user) return; if (isset($user->bossa)) return; if (isset(self::$cache[$user->id])) { $bossa = self::$cache[$user->id]; } else { $bossa = self::lookup_userid($user->id); if (!$bossa) { self::insert("(user_id) values ($user->id)"); $bossa = self::lookup_userid($user->id); } self::$cache[$user->id] = $bossa; } $user->bossa = $bossa; } function update($clause) { $db = BossaDb::get(); $clause = "$clause where user_id=$this->user_id"; return $db->update_aux('bossa_user', $clause); } } class BossaApp { static function insert($clause) { $db = BossaDb::get(); return $db->insert('bossa_app', $clause); } static function lookup($clause) { $db = BossaDb::get(); return $db->lookup('bossa_app', 'BossaApp', $clause); } static function lookup_id($id) { $db = BossaDb::get(); return $db->lookup_id($id, 'bossa_app', 'BossaApp'); } static function enum() { $db = BossaDb::get(); return $db->enum('bossa_app', 'BossaApp'); } function update($clause) { $db = BossaDb::get(); return $db->update($this, 'bossa_app', $clause); } } class BossaJob { static function insert($clause) { $db = BossaDb::get(); $ret = $db->insert('bossa_job', $clause); if (!$ret) return 0; return $db->insert_id(); } function update($clause) { $db = BossaDb::get(); return $db->update($this, 'bossa_job', $clause); } static function lookup_id($id) { $db = BossaDb::get(); return $db->lookup_id($id, 'bossa_job', 'BossaJob'); } static function enum($clause) { $db = BossaDb::get(); return $db->enum('bossa_job', 'BossaJob', $clause); } } class BossaJobInst { function insert($clause) { $db = BossaDb::get(); $ret = $db->insert('bossa_job_inst', $clause); if (!$ret) return 0; return $db->insert_id(); } static function lookup_id($id) { $db = BossaDb::get(); return $db->lookup_id($id, 'bossa_job_inst', 'BossaJobInst'); } static function enum($clause) { $db = BossaDb::get(); return $db->enum('bossa_job_inst', 'BossaJobInst', $clause); } function update($clause) { $db = BossaDb::get(); return $db->update($this, 'bossa_job_inst', $clause); } // Assign a job from the given app to the given user. // Returns the job instance or NULL. // static function assign($app, $user) { $db = BossaDb::get(); // first look for unfinished jobs previously assigned to this user // $query = "select * from DBNAME.bossa_job_inst where app_id=$app->id and user_id=$user->id and finish_time=0 limit 1"; $result = $db->do_query($query); if ($result) { $ji = mysql_fetch_object($result, 'BossaJobInst'); mysql_free_result($result); if ($ji) return $ji; } // this query skips jobs for which this user // has already been assigned an instance // $prio = "priority_".$user->bossa->category; $query = "select * from DBNAME.bossa_job where app_id=$app->id and (select count(*) from DBNAME.bossa_job_inst where job_id=bossa_job.id and user_id=$user->id) = 0 and state=1 and is_calibration=0 order by $prio desc limit 1"; $result = $db->do_query($query); if (!$result) return null; $job = mysql_fetch_object($result, 'BossaJob'); if (!$job) return null; mysql_free_result($result); $now = time(); $clause = "(create_time, app_id, job_id, user_id) values ($now, $app->id, $job->id, $user->id)"; $id = BossaJobInst::insert($clause); return BossaJobInst::lookup_id($id); } } class BossaBatch { function insert($clause) { $db = BossaDb::get(); $ret = $db->insert('bossa_batch', $clause); if (!$ret) return 0; return $db->insert_id(); } } ?>