/** * @test */ public function it_provides_five_entry_points() { $instances = [it(null), this(null), these(null), those(null), that(null)]; foreach ($instances as $instance) { $this->assertInstanceOf("Essence\\Essence", $instance); } }
public function testPlurals() { $plurals = \Gini\Config::get('orm.plurals'); $plurals['users'] = 'user'; \Gini\Config::set('orm.plurals', $plurals); // e.g. those('users') \Gini\Those::reset(); $those = those('users')->whose('name')->beginsWith('COOL'); $those->makeSQL(); $this->assertAttributeEquals('SELECT DISTINCT "t0"."id" FROM "user" AS "t0" WHERE "t0"."name" LIKE \'COOL%\'', 'SQL', $those); }
public function actionCacheProducts() { $chemicals = those('chemical')->orderBy('cas_no', 'asc'); $cacher = \Gini\Cache::of('chemdb'); $timeout = 86400 * 30; foreach ($chemicals as $c) { $key = "chemical[{$c->cas_no}]"; $data = ['cas_no' => $c->cas_no, 'name' => $c->name, 'types' => $c->types(), 'state' => $c->state, 'en_name' => $c->en_name, 'aliases' => $c->aliases, 'en_aliases' => $c->en_aliases, 'einecs' => $c->einecs, 'mol_formula' => $c->mol_formula, 'mol_weight' => $c->mol_weight, 'inchi' => $c->inchi, 'melting_point' => $c->melting_point, 'boiling_point' => $c->boiling_point, 'flash_point' => $c->flash_point]; $cacher->set($key, $data, $timeout); } }
public function actionGetChemicalTypes($cas_nos) { if (!is_array($cas_nos)) { $cas_nos = [$cas_nos]; } $types = those('chemical/type')->whose('cas_no')->isIn($cas_nos); $data = []; foreach ($types as $type) { $data[$type->cas_no][] = $type->name; } return $data; }
public function actionInit() { $db = \Gini\Database::db(); $tableName = self::_getTableName(); $max = $db->query("SELECT max(order_mtime) FROM {$tableName}")->value() ?: 0; $start = 0; $perpage = 100; $schema = self::$schema; $keys = array_keys($schema['fields']); array_shift($keys); $keys = implode(',', $keys); while (true) { $rows = those('order')->whose('mtime')->isGreaterThan($max)->andWhose('customized')->is(0)->orderBy('mtime', 'asc')->limit($start, $perpage); if (!count($rows)) { break; } $start += $perpage; $values = []; foreach ($rows as $row) { $items = (array) $row->items; $qRowID = $db->quote($row->id); $qRowMd5 = $db->quote($this->getMd5($row)); // 检测有没有历史数据 $hisCount = $db->query("SELECT COUNT(*) FROM {$tableName} WHERE order_id={$qRowID}")->value() ?: 0; if ($hisCount) { // 检测历史数据是不是已经跟最新的数据不一致了 // 如果不一致,需要删除重建 // 如果一致,就不需要在重复添加了 $query = $db->query("DELETE FROM {$tableName} WHERE order_id={$qRowID} AND order_md5!={$qRowMd5}"); if (!$query || !$query->count()) { echo "\norder#{$row->id} 更新数据失败\n"; continue; } } foreach ($items as $i => $item) { // 如果商品价格是待询价, 当成无效订单处理 if ($item['unit_price'] < 0) { continue; } $values[] = '(' . implode(',', [$db->quote($row->id), $db->quote($row->mtime), $db->quote($this->getMd5($row)), $db->quote($item['id']), $db->quote(''), $db->quote(''), $db->quote($row->vendor->id), $db->quote($row->vendor->name), $db->quote($row->group->id), $db->quote($row->group->title), $db->quote(round($row->price, 2)), $db->quote(''), $db->quote($item['quantity']), $db->quote($item['unit_price']), $db->quote($item['price']), $db->quote($row->status), $db->quote('')]) . ')'; } } if (empty($values)) { continue; } $values = implode(',', $values); $db = \Gini\Database::db(); $sql = "INSERT INTO {$tableName}({$keys}) VALUES {$values};"; if ($db->query($sql)) { echo '.'; } else { echo 'x'; } } }
public function testInObjects() { // e.g. those('users') \Gini\Those::reset(); $those = those('users')->whose('gender')->is(1)->andWhose('father')->isIn(those('users')->whose('name')->is('A')); $those->makeSQL(); $this->assertAttributeEquals('SELECT DISTINCT "t0"."id" FROM "user" AS "t0" INNER JOIN "user" AS "t1" ON "t0"."father_id"="t1"."id" WHERE "t0"."gender"=1 AND "t1"."name"=\'A\'', 'SQL', $those); \Gini\Those::reset(); $those = those('users')->whose('gender')->is(1)->orWhose('father')->isIn(those('users')->whose('name')->is('A')); $those->makeSQL(); $this->assertAttributeEquals('SELECT DISTINCT "t0"."id" FROM "user" AS "t0" INNER JOIN "user" AS "t1" ON "t0"."father_id"="t1"."id" WHERE "t0"."gender"=1 OR "t1"."name"=\'A\'', 'SQL', $those); \Gini\Those::reset(); $those = those('users')->whose('gender')->is(1)->andWhose('father')->isNotIn(those('users')->whose('name')->is('A')); $those->makeSQL(); $this->assertAttributeEquals('SELECT DISTINCT "t0"."id" FROM "user" AS "t0" LEFT JOIN "user" AS "t1" ON "t0"."father_id"="t1"."id" WHERE "t0"."gender"=1 AND "t0"."father_id" IS NOT NULL AND "t1"."name"=\'A\'', 'SQL', $those); }
public function types() { return array_values(those('chemical/type')->whose('cas_no')->is($this->cas_no)->get('id', 'name')); }