Example #1
0
 /**
  * Constructs a new column object
  * @param string $name Name of the column
  * @param \Database\Resource $resource
  * @param boolean $check_existence If true, check to see if column exists
  *        before creating
  */
 public function __construct(\Database\Resource $resource, $name, $check_existence = null)
 {
     $check_existance = empty($check_existance) ? DATABASE_CHECK_COLUMNS : $check_existance;
     if (!\Database\DB::allowed($name)) {
         throw new \Exception(t('Bad column name'));
     }
     $this->name = new \Variable\Attribute($name, 'name');
     $this->resource = $resource;
     if ($check_existence && !$this->resource->columnExists($name)) {
         throw new \Exception(t('Column "%s" does not exist in %s "%s"', $name, get_class($resource), $this->resource->getFullName(false)));
     }
 }
Example #2
0
 /**
  * Транслитерация единичного системного маркера
  * @param type $marker
  * @return type
  */
 public static function systemMarker($marker)
 {
     $locale = \Components\Locale::getFavorite();
     $DB = \Database\DB::init(['adapter' => 'sqlite', 'dsn' => KIT_CORE_ETC . "/SystemLocale.sqlite"]);
     $result = $DB->query("SELECT * FROM `locale` WHERE `marker` = :marker", [$marker]);
     $data = $result->fetch();
     if (isset($data[$locale])) {
         return $data[$locale];
     } else {
         if (isset($data[KIT_LOCALE_DEF])) {
             return $data[KIT_LOCALE_DEF];
         }
     }
     return $marker;
 }
Example #3
0
<?php

include "../vendor/autoload.php";
use database\DB;
DB::setConfig(array("dsn" => "mysql:host=localhost;dbname=sakila", "username" => "root", "password" => "root"));
DB::registerExceptionCallback(function (Exception $e) {
    echo "Error callback: " . $e->getMessage();
});
DB::getInstance();
Example #4
0
 public function startTransaction()
 {
     if (!isset($this->_db)) {
         $this->_db = \Database\DB::init();
     }
     if ($this->_db->inTransaction()) {
         return true;
     }
     $this->_db->beginTransaction();
     return $this;
 }
Example #5
0
 public static function TableAsXML($table_name, $table = false)
 {
     if ($table === false) {
         $table = new XML('<table/>');
     }
     $table['name'] = $table_name;
     $indexes = self::GetIndexes($table_name);
     foreach ($indexes['index'] as $index_str) {
         $table->addChild('index', $index_str);
     }
     foreach ($indexes['unique'] as $index_str) {
         $table->addChild('unique', $index_str);
     }
     $table['primary_key'] = isset($indexes['primary']) ? $indexes['primary'] : false;
     $cols = DB::all("DESCRIBE " . DB::quoteField($table_name));
     foreach ($cols as $col) {
         $field = $table->addChild('field');
         $name = $col['Field'];
         $type = $col['Type'];
         $key = $col['Key'];
         $default = $col['Default'];
         $null = $col['Null'];
         $extra = $col['Extra'];
         $field['name'] = $name;
         // type, size
         if (preg_match('/^([a-z]+)\\(([0-9]+)\\) unsigned$/', $type, $match)) {
             $field['type'] = self::NormalizeType($match[1]);
             $field['size'] = $match[2];
             $field['signed'] = 'n';
         } elseif (preg_match('/^([a-z]+)\\(([0-9]+)\\)$/', $type, $match)) {
             $field['type'] = self::NormalizeType($match[1]);
             $field['size'] = $match[2];
         } elseif (preg_match('/^(decimal)\\(([0-9]+,[0-9]+)\\)$/', $type, $match)) {
             $field['type'] = self::NormalizeType($match[1]);
             $field['size'] = $match[2];
         } elseif (preg_match('/^(enum|set)\\((.+)\\)$/', $type, $match)) {
             $options = array();
             foreach (explode(',', $match[2]) as $option) {
                 if (preg_match("/^'(.+)'\$/", $option, $match2)) {
                     $options[] = $match2[1];
                 }
             }
             $field['type'] = self::NormalizeType($match[1]);
             $field['size'] = join(',', $options);
         } elseif (in_array($type, array('tinytext', 'text', 'blob', 'longblob'))) {
             $field['type'] = self::NormalizeType($type);
         } else {
             debug($type);
         }
         if ($field['type'] == 'int' && !isset($field['signed'])) {
             $field['signed'] = 'y';
         }
         // default
         if ($default !== NULL) {
             $field['default'] = $default;
         } else {
             $field['null'] = 'y';
         }
         if ($null == 'YES') {
             $field['null'] = 'y';
         } elseif ($null == 'NO') {
             $field['null'] = 'n';
         }
         // auto_increment
         if ($extra == 'auto_increment') {
             $field['auto_increment'] = 'y';
         } elseif ($extra) {
             debug($extra);
         }
     }
     return $table;
 }
Example #6
0
 /**
  * Connects to the database.
  *
  * @uses config.xml
  * @return bool Success
  */
 protected static function Database()
 {
     $config = static::DatabaseConfiguration();
     if (!$config) {
         return false;
     }
     try {
         return DB::connect($config);
     } catch (\Database\DBException $e) {
         $settings = DB::config($config);
         debug("Could not connect to the database {$settings['conn']}.");
         return false;
     }
 }
 private function processLimit()
 {
     $offset = ($this->current_page - 1) * $this->rows_per_page;
     $this->db->setLimit($this->rows_per_page, $offset);
 }
Example #8
0
<?php

include "src/database/Statement.php";
include "src/database/DB.php";
\Database\DB::setConfig(array("dsn" => "mysql:host=localhost;dbname=mobjizz", "username" => "root", "password" => "root"));
$db = \Database\DB::getInstance("set");
Example #9
0
 /**
  * Creates a new table based on the resource object. Returns table object
  * if successful
  * @param \Database\DB $db
  * @return \Database\Table
  */
 public function createTable(\Database\DB $db)
 {
     $resource_table = $db->buildTable($this->getTable());
     $datatypes = $this->getVariablesAsDatatypes($resource_table);
     if (!$datatypes) {
         throw new \Exception('Resource did not return any datatypes');
     }
     $resource_table->addPrimaryIndexId();
     $resource_table->create();
     return $resource_table;
 }
Example #10
0
 public static function PrintCompareTables()
 {
     $one = Framework::Model();
     $tmp = DBToXML::AsXML(DB::tables())->asXML();
     $two = new Config($tmp);
     $two = $two->get();
     $xml = new XMLToSQL($one);
     $compare = new CompareXML($one, $two);
     $total = $incorrect = 0;
     $one_empty = !count($one->xpath('//table'));
     $two_empty = !count($two->xpath('//table'));
     if ($one_empty || $two_empty) {
         return;
     }
     foreach ($one->table as $table) {
         $table_name = (string) $table['name'];
         if (!DB::tableExists($table_name)) {
             continue;
         }
         $total++;
         $problem = false;
         list($indexes_missing_one, $columns_missing_two) = $compare->CompareColumns($table_name);
         if (count($indexes_missing_one)) {
             $problem = true;
             foreach ($indexes_missing_one as $column) {
                 $field = $table->find("//field[@name='{$column}']");
                 $sql = $xml->FieldSQL($field);
                 $sql = "ALTER TABLE `{$table_name}` ADD {$sql};";
                 print self::output_sql($sql, "Column <em>{$column}</em> is missing from the <em>{$table_name}</em> table.", 'Add it');
             }
         }
         if (count($columns_missing_two)) {
             $problem = true;
             foreach ($columns_missing_two as $column) {
                 $sql = "ALTER TABLE `{$table_name}` DROP {$column};";
                 print self::output_sql($sql, "Existing column <em>{$column}</em> is not present in the configuration for the <em>{$table_name}</em> table.", 'Remove it');
             }
         }
         // compare column attributes
         foreach ($table->field as $column) {
             $column_name = (string) $column['name'];
             list($diff_one, $diff_two) = $compare->CompareAttributes($table_name, $column_name);
             if (count($diff_one) || count($diff_two)) {
                 $problem = true;
                 $text = self::column_diff_to_string($diff_two) . ' vs ' . self::column_diff_to_string($diff_one);
                 $sql = "ALTER TABLE `{$table['name']}` CHANGE `{$column_name}` " . $xml->FieldSQL($column);
                 //ALTER TABLE  `site_css_backup` CHANGE  `created`  `created` INT( 4 ) UNSIGNED NULL DEFAULT NULL
                 print self::output_sql($sql, "Different column attributes in XML/{$table_name}.{$column_name}: {$text}", 'Change it');
             }
         }
         list($pri_one, $pri_two) = $compare->ComparePrimaryKey($table_name);
         if ($pri_one != $pri_two) {
             $problem = true;
             print self::output_sql("DROP TABLE `{$table_name}`;", "Table <em>{$table_name}</em> has incorrect primary key \"{$pri_two}\" instead of \"{$pri_one}\":", 'Drop the table');
         }
         // indexes
         list($indexes_missing_one, $indexes_missing_two) = $compare->CompareIndexes($table_name);
         if (count($indexes_missing_one) || count($indexes_missing_two)) {
             $problem = true;
         }
         self::PrintMissingDBIndexes($xml, $table_name, $indexes_missing_one);
         self::PrintMissingXMLIndexes($xml, $table_name, $indexes_missing_two);
         // unique indexes
         list($indexes_missing_one, $indexes_missing_two) = $compare->CompareUniques($table_name);
         if (count($indexes_missing_one) || count($indexes_missing_two)) {
             $problem = true;
         }
         self::PrintMissingDBUniques($xml, $table_name, $indexes_missing_one);
         self::PrintMissingXMLUniques($xml, $table_name, $indexes_missing_two);
         if ($problem) {
             $incorrect++;
         }
     }
     $correct = $total - $incorrect;
     $type = $incorrect ? 'Fail' : 'Pass';
     $answer = $incorrect ? 'No' : 'Yes';
     $_t = $total == 1 ? 'table is correct' : 'tables are correct';
     $message = " - <em>{$answer}</em>, {$correct}/{$total} {$_t}.";
     print self::output($type, 'Are table definitions correct?', $message);
 }
Example #11
0
 /**
  * Calls getFieldCondtional and uses it within a DB::addConditional call. Note
  * that addConditional ONLY USES "AND" COMPARISONS. This is just a shortcut.
  * More intricate conditionals should not use this method.
  *
  * @see \Database\Table::getFieldConditional
  * @param string $field_name
  * @param string $value
  * @param string $operator
  */
 public function addFieldConditional($field_name, $value, $operator = null)
 {
     $this->db->addConditional($this->getFieldConditional($field_name, $value, $operator));
 }
Example #12
0
 public function __construct()
 {
     $this->conf = new \Core\Config();
     $this->sessionHandler = new \Libraries\SessionHandler();
     parent::__construct($this->conf->readDbConfig());
 }
Example #13
0
 public function delete()
 {
     $this->beforeDelete();
     $ok = (bool) DB::delete($this->table, $this->getIdCondition());
     return $this->id ? $this->afterDelete() : false;
 }
Example #14
0
 public function rollBack()
 {
     $this->transLevel--;
     if (!$this->nestable() || $this->transLevel == 0) {
         parent::rollBack();
     } else {
         $this->exec("ROLLBACK TO SAVEPOINT LEVEL{$this->transLevel}");
     }
 }