/** * Class constructor */ public function __construct() { if (!isset($this->db)) { $this->db = Database::create($this->conn); $this->cache = new \Core\Database\Cache(); } }
/** * Add data to configs and create all necessary files */ protected function addRelation() { if ($this->relationType == 'hasAndBelongsToMany') { $junction = $this->getJunction(); try { Validate::table($junction)->exists(); } catch (LazerException $e) { Database::create($junction, array($this->tables['local'] . '_id' => 'integer', $this->tables['foreign'] . '_id' => 'integer')); $this->insertRelationData($junction, $this->tables['local'], 'hasMany', array('local' => $this->tables['local'] . '_id', 'foreign' => $this->keys['local'])); $this->insertRelationData($junction, $this->tables['foreign'], 'hasMany', array('local' => $this->tables['foreign'] . '_id', 'foreign' => $this->keys['foreign'])); } } $this->insertRelationData($this->tables['local'], $this->tables['foreign'], $this->relationType, $this->keys); }
/** * 备份数据库 * @param String $tables 表名 * @param Integer $id 表ID * @param Integer $start 起始行数 */ public function export($tables = null, $id = null, $start = null) { import('ORG.Net.Database'); if (IS_POST && !empty($tables) && is_array($tables)) { //初始化 //读取备份配置 $config = array('path' => realpath(C('DATA_BACKUP_PATH')) . DIRECTORY_SEPARATOR, 'part' => C('DATA_BACKUP_PART_SIZE'), 'compress' => C('DATA_BACKUP_COMPRESS'), 'level' => C('DATA_BACKUP_COMPRESS_LEVEL')); //检查是否有正在执行的任务 $lock = "{$config['path']}backup.lock"; if (is_file($lock)) { $this->error('检测到有一个备份任务正在执行,请稍后再试!'); } else { //创建锁文件 file_put_contents($lock, NOW_TIME); } //检查备份目录是否可写 is_writeable($config['path']) || $this->error('备份目录不存在或不可写,请检查后重试!'); session('backup_config', $config); //生成备份文件信息 $file = array('name' => date('Ymd-His', NOW_TIME), 'part' => 1); session('backup_file', $file); //缓存要备份的表 session('backup_tables', $tables); //创建备份文件 $Database = new Database($file, $config); if (false !== $Database->create()) { $tab = array('id' => 0, 'start' => 0); $this->success('初始化成功!', '', array('tables' => $tables, 'tab' => $tab)); } else { $this->error('初始化失败,备份文件创建失败!'); } } elseif (IS_GET && is_numeric($id) && is_numeric($start)) { //备份数据 $tables = session('backup_tables'); //备份指定表 $Database = new Database(session('backup_file'), session('backup_config')); $start = $Database->backup($tables[$id], $start); if (false === $start) { //出错 $this->error('备份出错!'); } elseif (0 === $start) { //下一表 if (isset($tables[++$id])) { $tab = array('id' => $id, 'start' => 0); $this->success('备份完成!', '', array('tab' => $tab)); } else { //备份完成,清空缓存 unlink(session('backup_config.path') . 'backup.lock'); session('backup_tables', null); session('backup_file', null); session('backup_config', null); $this->success('备份完成!'); } } else { $tab = array('id' => $id, 'start' => $start[0]); $rate = floor(100 * ($start[0] / $start[1])); $this->success("正在备份...({$rate}%)", '', array('tab' => $tab)); } } else { //出错 $this->error('参数错误!'); } }
public static function create($tableName, $schema) { Database::create($tableName, $schema); }
/** * @covers Lazer\Classes\Database::create * @expectedException Lazer\Classes\LazerException * @expectedExceptionMessageRegExp #Table ".*" already exists# */ public function testCreateExistingTable() { $this->object->create('users', array('myInteger' => 'integer', 'myString' => 'string', 'myBool' => 'boolean')); }
/** * Test if Databases can still be created, if current is not _system */ public function testCreateDatabaseSwitchToItAndCreateAnotherOne() { $database = 'ArangoTestSuiteDatabaseTest01'; $database2 = 'ArangoTestSuiteDatabaseTest02'; try { $e = null; Database::delete($this->connection, $database); } catch (\Exception $e) { // don't bother us... } $response = Database::create($this->connection, $database); $this->assertTrue($response['error'] == false, 'result[\'error\'] Did not return false, instead returned: ' . print_r($response, 1)); // Try to create a database from within a non_system database $this->connection->setDatabase($database); $response = Database::getInfo($this->connection); $this->assertTrue($response['result']['name'] == $database); try { $e = null; $response = Database::create($this->connection, $database2); } catch (\Exception $e) { // don't bother us... just give us the $e } $this->assertInstanceOf('triagens\\ArangoDb\\ServerException', $e); $this->assertTrue($e->getCode() == 403, 'Should be 403, instead got: ' . $e->getCode()); $this->connection->setDatabase('_system'); $response = Database::getInfo($this->connection); $this->assertTrue($response['result']['name'] == '_system'); $response = Database::delete($this->connection, $database); $this->assertTrue($response['error'] == false, 'result[\'error\'] Did not return false, instead returned: ' . print_r($response, 1)); }
<head> <link rel="stylesheet" type="text/css" href="css/common.css" /> </head> <body style="background-color: gray"> <?php include 'header.html'; ?> </body> <?php require_once 'classes/Database.php'; $db = new Database(""); if ($db->getConnError()) { http_response_code(503); // Service is unavailable echo "<h1>The database service is currently unavailable. Please try again</h1>"; exit; } $db->create('sysc4504'); //Create the sysc4504 database $db2 = new Database("sysc4504"); //Connect to the sysc4504 database $dirContents = scandir('sql'); //Get an Array of the contents of the SQL folder. //We only want to process files that match *.sql foreach ($dirContents as $toProcess) { if (substr($toProcess, -4, 4) == ".sql") { $db2->import('./sql/' . $toProcess); } //Else do nothing } echo "<h1>Done importing sysc4504 database</h1>";
/** * SQL Command Performance * ทดสอบการเรียกข้อมูลและอัปเดทข้อมูลโดยใช้คำสั่ง SQL Command */ private function sql() { $db = \Database::create(); $db->query("UPDATE `world` SET `name`=''"); for ($i = 0; $i < 2; $i++) { $rnd = mt_rand(1, 10000); $result = $db->customQuery("SELECT * FROM `world` WHERE `id`=" . $rnd); $db->query("UPDATE `world` SET `name`='Hello World!' WHERE `id`=" . $result[0]['id']); } $result = $db->customQuery("SELECT * FROM `world` WHERE `id`=" . $result[0]['id']); echo $result[0]['name']; }
<?php /** * Created by PhpStorm. * User: essamamdani * Date: 26/04/16 * Time: 5:00 PM */ require "Database.php"; require "Form.php"; $aslam = new Database("localhost", "root", "root", "pos"); $data = array("name" => "aslam"); $tbl = "vendor"; $aslam->create($data, $tbl);
} } else { header('HTTP/1.1 500 Bad request'); echo 'Missing arguments for login'; } break; case 'create': if (isset($_POST['username']) && isset($_POST['password']) && isset($_POST['name']) && isset($_POST['email']) && isset($_POST['gender'])) { $g = $_POST['gender']; if ($g === 'male' || $g === 'female') { $g = $g === 'male' ? GENDER_MALE : GENDER_FEMALE; $u = $_POST['username']; $p = $_POST['password']; $n = $_POST['name']; $m = $_POST['email']; if ($db->create($u, $p, $n, $g, $m)) { header('HTTP/1.1 200 OK'); echo 'User created successfully'; } else { header('HTTP/1.1 500 Bad request'); echo 'Username exists'; } } else { header('HTTP/1.1 500 Bad request'); echo 'Gender should be either male or female'; } } else { header('HTTP/1.1 500 Bad request'); echo 'Missing arguments for user creation'; } break;