php SilverStripe-ORM-DB类(方法)实例源码

下面列出了php SilverStripe-ORM-DB 类(方法)源码代码实例,从而了解它的用法。

作者:SpiritLeve    项目:silverstripe-framewor   
/**
  * Perform migration
  *
  * @param string $base Absolute base path (parent of assets folder). Will default to BASE_PATH
  * @return int Number of files successfully migrated
  */
 public function run($base = null)
 {
     if (empty($base)) {
         $base = BASE_PATH;
     }
     // Check if the File dataobject has a "Filename" field.
     // If not, cannot migrate
     /** @skipUpgrade */
     if (!DB::get_schema()->hasField('File', 'Filename')) {
         return 0;
     }
     // Set max time and memory limit
     increase_time_limit_to();
     increase_memory_limit_to();
     // Loop over all files
     $count = 0;
     $originalState = Versioned::get_reading_mode();
     Versioned::set_stage(Versioned::DRAFT);
     $filenameMap = $this->getFilenameArray();
     foreach ($this->getFileQuery() as $file) {
         // Get the name of the file to import
         $filename = $filenameMap[$file->ID];
         $success = $this->migrateFile($base, $file, $filename);
         if ($success) {
             $count++;
         }
     }
     Versioned::set_reading_mode($originalState);
     return $count;
 }

作者:jacobbuc    项目:silverstripe-framewor   
public function testFilter()
 {
     if (DB::get_conn() instanceof MySQLDatabase) {
         $baseQuery = FulltextFilterTest_DataObject::get();
         $this->assertEquals(3, $baseQuery->count(), "FulltextFilterTest_DataObject count does not match.");
         // First we'll text the 'SearchFields' which has been set using an array
         $search = $baseQuery->filter("SearchFields:fulltext", 'SilverStripe');
         $this->assertEquals(1, $search->count());
         $search = $baseQuery->exclude("SearchFields:fulltext", "SilverStripe");
         $this->assertEquals(2, $search->count());
         // Now we'll run the same tests on 'OtherSearchFields' which should yield the same resutls
         // but has been set using a string.
         $search = $baseQuery->filter("OtherSearchFields:fulltext", 'SilverStripe');
         $this->assertEquals(1, $search->count());
         $search = $baseQuery->exclude("OtherSearchFields:fulltext", "SilverStripe");
         $this->assertEquals(2, $search->count());
         // Search on a single field
         $search = $baseQuery->filter("ColumnE:fulltext", 'Dragons');
         $this->assertEquals(1, $search->count());
         $search = $baseQuery->exclude("ColumnE:fulltext", "Dragons");
         $this->assertEquals(2, $search->count());
     } else {
         $this->markTestSkipped("FulltextFilter only supports MySQL syntax.");
     }
 }

作者:jacobbuc    项目:silverstripe-framewor   
public function testMultipleRowInsert()
 {
     $query = SQLInsert::create('"SQLInsertTestBase"');
     $query->addRow(array('"Title"' => 'First Object', '"Age"' => 10, '"Description"' => 'First the worst'));
     $query->addRow(array('"Title"' => 'Second object', '"Age"' => 12));
     $sql = $query->sql($parameters);
     // Only test this case if using the default query builder
     if (get_class(DB::get_conn()->getQueryBuilder()) === 'SilverStripe\\ORM\\Connect\\DBQueryBuilder') {
         $this->assertSQLEquals('INSERT INTO "SQLInsertTestBase" ("Title", "Age", "Description") VALUES (?, ?, ?), (?, ?, ?)', $sql);
     }
     $this->assertEquals(array('First Object', 10, 'First the worst', 'Second object', 12, null), $parameters);
     $query->execute();
     $this->assertEquals(2, DB::affected_rows());
     // Check inserted objects are correct
     $firstObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'First Object'), false);
     $this->assertNotEmpty($firstObject);
     $this->assertEquals($firstObject->Title, 'First Object');
     $this->assertEquals($firstObject->Age, 10);
     $this->assertEquals($firstObject->Description, 'First the worst');
     $secondObject = DataObject::get_one('SQLInsertTestBase', array('"Title"' => 'Second object'), false);
     $this->assertNotEmpty($secondObject);
     $this->assertEquals($secondObject->Title, 'Second object');
     $this->assertEquals($secondObject->Age, 12);
     $this->assertEmpty($secondObject->Description);
 }

作者:SpiritLeve    项目:silverstripe-framewor   
public function testCreateWithTransaction()
 {
     if (DB::get_conn()->supportsTransactions() == true) {
         DB::get_conn()->transactionStart();
         $obj = new TransactionTest_Object();
         $obj->Title = 'First page';
         $obj->write();
         $obj = new TransactionTest_Object();
         $obj->Title = 'Second page';
         $obj->write();
         //Create a savepoint here:
         DB::get_conn()->transactionSavepoint('rollback');
         $obj = new TransactionTest_Object();
         $obj->Title = 'Third page';
         $obj->write();
         $obj = new TransactionTest_Object();
         $obj->Title = 'Fourth page';
         $obj->write();
         //Revert to a savepoint:
         DB::get_conn()->transactionRollback('rollback');
         DB::get_conn()->transactionEnd();
         $first = DataObject::get('TransactionTest_Object', "\"Title\"='First page'");
         $second = DataObject::get('TransactionTest_Object', "\"Title\"='Second page'");
         $third = DataObject::get('TransactionTest_Object', "\"Title\"='Third page'");
         $fourth = DataObject::get('TransactionTest_Object', "\"Title\"='Fourth page'");
         //These pages should be in the system
         $this->assertTrue(is_object($first) && $first->exists());
         $this->assertTrue(is_object($second) && $second->exists());
         //These pages should NOT exist, we reverted to a savepoint:
         $this->assertFalse(is_object($third) && $third->exists());
         $this->assertFalse(is_object($fourth) && $fourth->exists());
     } else {
         $this->markTestSkipped('Current database does not support transactions');
     }
 }

作者:SpiritLeve    项目:silverstripe-framewor   
/**
  * (non-PHPdoc)
  * @see DBField::requireField()
  */
 public function requireField()
 {
     $charset = Config::inst()->get('SilverStripe\\ORM\\Connect\\MySQLDatabase', 'charset');
     $collation = Config::inst()->get('SilverStripe\\ORM\\Connect\\MySQLDatabase', 'collation');
     $parts = array('datatype' => 'varchar', 'precision' => $this->size, 'character set' => $charset, 'collate' => $collation, 'arrayValue' => $this->arrayValue);
     $values = array('type' => 'varchar', 'parts' => $parts);
     DB::require_field($this->tableName, $this->name, $values);
 }

作者:jacobbuc    项目:silverstripe-framewor   
public function requireField()
 {
     // @todo: Remove mysql-centric logic from this
     $charset = Config::inst()->get('SilverStripe\\ORM\\Connect\\MySQLDatabase', 'charset');
     $collation = Config::inst()->get('SilverStripe\\ORM\\Connect\\MySQLDatabase', 'collation');
     $values = array('type' => 'set', 'parts' => array('enums' => $this->enum, 'character set' => $charset, 'collate' => $collation, 'default' => $this->default, 'table' => $this->tableName, 'arrayValue' => $this->arrayValue));
     DB::require_field($this->tableName, $this->name, $values);
 }

作者:SpiritLeve    项目:silverstripe-framewor   
/**
  * (non-PHPdoc)
  * @see DBField::requireField()
  */
 public function requireField()
 {
     $charset = Config::inst()->get('SilverStripe\\ORM\\Connect\\MySQLDatabase', 'charset');
     $collation = Config::inst()->get('SilverStripe\\ORM\\Connect\\MySQLDatabase', 'collation');
     $parts = ['datatype' => 'mediumtext', 'character set' => $charset, 'collate' => $collation, 'default' => $this->defaultVal, 'arrayValue' => $this->arrayValue];
     $values = ['type' => 'text', 'parts' => $parts];
     DB::require_field($this->tableName, $this->name, $values);
 }

作者:SpiritLeve    项目:silverstripe-framewor   
public function testTablesAreCreated()
 {
     $tables = DB::table_list();
     $check = array('versionableextensionstest_dataobject_test1_live', 'versionableextensionstest_dataobject_test2_live', 'versionableextensionstest_dataobject_test3_live', 'versionableextensionstest_dataobject_test1_versions', 'versionableextensionstest_dataobject_test2_versions', 'versionableextensionstest_dataobject_test3_versions');
     // Check that the right tables exist
     foreach ($check as $tableName) {
         $this->assertContains($tableName, array_keys($tables), 'Contains table: ' . $tableName);
     }
 }

作者:SpiritLeve    项目:silverstripe-framewor   
public function run($request)
 {
     $migrated = FileMigrationHelper::singleton()->run();
     if ($migrated) {
         DB::alteration_message("{$migrated} File DataObjects upgraded", "changed");
     } else {
         DB::alteration_message("No File DataObjects need upgrading", "notice");
     }
 }

作者:jacobbuc    项目:silverstripe-framewor   
public function requireField()
 {
     // HACK: MSSQL does not support double so we're using float instead
     // @todo This should go into MSSQLDatabase ideally somehow
     if (DB::get_conn() instanceof MySQLDatabase) {
         DB::require_field($this->tableName, $this->name, "double");
     } else {
         DB::require_field($this->tableName, $this->name, "float");
     }
 }

作者:SpiritLeve    项目:silverstripe-framewor   
public function conditionSQL(&$parameters)
 {
     $parameters = array();
     // Ignore empty conditions
     $where = $this->whereQuery->getWhere();
     if (empty($where)) {
         return null;
     }
     // Allow database to manage joining of conditions
     $sql = DB::get_conn()->getQueryBuilder()->buildWhereFragment($this->whereQuery, $parameters);
     return preg_replace('/^\\s*WHERE\\s*/i', '', $sql);
 }

作者:SpiritLeve    项目:silverstripe-framewor   
function testValidAlternativeDatabaseName()
 {
     $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
     Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'dev');
     $this->assertTrue(DB::valid_alternative_database_name($prefix . 'tmpdb1234567'));
     $this->assertFalse(DB::valid_alternative_database_name($prefix . 'tmpdb12345678'));
     $this->assertFalse(DB::valid_alternative_database_name('tmpdb1234567'));
     $this->assertFalse(DB::valid_alternative_database_name('random'));
     $this->assertFalse(DB::valid_alternative_database_name(''));
     Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'live');
     $this->assertFalse(DB::valid_alternative_database_name($prefix . 'tmpdb1234567'));
     Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', 'dev');
 }

作者:jacobbuc    项目:silverstripe-framewor   
/**
  * Returns the manifest of all classes which are present in the database.
  *
  * @param string $class Class name to check enum values for ClassName field
  * @param boolean $includeUnbacked Flag indicating whether or not to include
  * types that don't exist as implemented classes. By default these are excluded.
  * @return array List of subclasses
  */
 public static function getValidSubClasses($class = 'SilverStripe\\CMS\\Model\\SiteTree', $includeUnbacked = false)
 {
     if (is_string($class) && !class_exists($class)) {
         return array();
     }
     $class = self::class_name($class);
     if ($includeUnbacked) {
         $table = DataObject::getSchema()->tableName($class);
         $classes = DB::get_schema()->enumValuesForField($table, 'ClassName');
     } else {
         $classes = static::subclassesFor($class);
     }
     return $classes;
 }

作者:SpiritLeve    项目:silverstripe-framewor   
protected function excludeMany(DataQuery $query)
 {
     $this->model = $query->applyRelation($this->relation);
     $values = $this->getValue();
     $comparisonClause = DB::get_conn()->comparisonClause($this->getDbName(), null, false, true, $this->getCaseSensitive(), true);
     $parameters = array();
     foreach ($values as $value) {
         $parameters[] = $this->getMatchPattern($value);
     }
     // Since query connective is ambiguous, use AND explicitly here
     $count = count($values);
     $predicate = implode(' AND ', array_fill(0, $count, $comparisonClause));
     return $query->where(array($predicate => $parameters));
 }

作者:SpiritLeve    项目:silverstripe-framewor   
public function testBasicUpdate()
 {
     $query = SQLUpdate::create()->setTable('"SQLUpdateTestBase"')->assign('"Description"', 'Description 1a')->addWhere(array('"Title" = ?' => 'Object 1'));
     $sql = $query->sql($parameters);
     // Check SQL
     $this->assertSQLEquals('UPDATE "SQLUpdateTestBase" SET "Description" = ? WHERE ("Title" = ?)', $sql);
     $this->assertEquals(array('Description 1a', 'Object 1'), $parameters);
     // Check affected rows
     $query->execute();
     $this->assertEquals(1, DB::affected_rows());
     // Check item updated
     $item = DataObject::get_one('SQLUpdateTestBase', array('"Title"' => 'Object 1'));
     $this->assertEquals('Description 1a', $item->Description);
 }

作者:jacobbuc    项目:silverstripe-framewor   
protected function foreignIDFilter($id = null)
 {
     if ($id === null) {
         $id = $this->getForeignID();
     }
     // Apply relation filter
     $key = DataObject::getSchema()->sqlColumnForField($this->dataClass(), $this->getForeignKey());
     if (is_array($id)) {
         return array("{$key} IN (" . DB::placeholders($id) . ")" => $id);
     } else {
         if ($id !== null) {
             return array($key => $id);
         }
     }
     return null;
 }

作者:SpiritLeve    项目:silverstripe-framewor   
public function testAffectedRows()
 {
     if (!DB::get_connector() instanceof PDOConnector) {
         $this->markTestSkipped('This test requires the current DB connector is PDO');
     }
     $query = new SQLUpdate('MySQLDatabaseTest_Data');
     $query->setAssignments(array('Title' => 'New Title'));
     // Test update which affects no rows
     $query->setWhere(array('Title' => 'Bob'));
     $result = $query->execute();
     $this->assertInstanceOf('SilverStripe\\ORM\\Connect\\PDOQuery', $result);
     $this->assertEquals(0, DB::affected_rows());
     // Test update which affects some rows
     $query->setWhere(array('Title' => 'First Item'));
     $result = $query->execute();
     $this->assertInstanceOf('SilverStripe\\ORM\\Connect\\PDOQuery', $result);
     $this->assertEquals(1, DB::affected_rows());
 }

作者:SpiritLeve    项目:silverstripe-framewor   
/**
  * Link this group set to a specific member.
  *
  * Recursively selects all groups applied to this member, as well as any
  * parent groups of any applied groups
  *
  * @param array|integer $id (optional) An ID or an array of IDs - if not provided, will use the current
  * ids as per getForeignID
  * @return array Condition In array(SQL => parameters format)
  */
 public function foreignIDFilter($id = null)
 {
     if ($id === null) {
         $id = $this->getForeignID();
     }
     // Find directly applied groups
     $manyManyFilter = parent::foreignIDFilter($id);
     $query = new SQLSelect('"Group_Members"."GroupID"', '"Group_Members"', $manyManyFilter);
     $groupIDs = $query->execute()->column();
     // Get all ancestors, iteratively merging these into the master set
     $allGroupIDs = array();
     while ($groupIDs) {
         $allGroupIDs = array_merge($allGroupIDs, $groupIDs);
         $groupIDs = DataObject::get("SilverStripe\\Security\\Group")->byIDs($groupIDs)->column("ParentID");
         $groupIDs = array_filter($groupIDs);
     }
     // Add a filter to this DataList
     if (!empty($allGroupIDs)) {
         $allGroupIDsPlaceholders = DB::placeholders($allGroupIDs);
         return array("\"Group\".\"ID\" IN ({$allGroupIDsPlaceholders})" => $allGroupIDs);
     } else {
         return array('"Group"."ID"' => 0);
     }
 }

作者:jacobbuc    项目:silverstripe-framewor   
protected function overrideField($obj, $fieldName, $value, $fixtures = null)
 {
     $class = get_class($obj);
     $table = DataObject::getSchema()->tableForField($class, $fieldName);
     $value = $this->parseValue($value, $fixtures);
     DB::manipulate(array($table => array("command" => "update", "id" => $obj->ID, "class" => $class, "fields" => array($fieldName => $value))));
     $obj->{$fieldName} = $value;
 }

作者:jacobbuc    项目:silverstripe-framewor   
public function requireField()
 {
     $parts = array('datatype' => 'tinyint', 'precision' => 1, 'sign' => 'unsigned', 'null' => 'not null', 'default' => $this->defaultVal, 'arrayValue' => $this->arrayValue);
     $values = array('type' => 'boolean', 'parts' => $parts);
     DB::require_field($this->tableName, $this->name, $values);
 }


问题


面经


文章

微信
公众号

扫码关注公众号