protected function add_column($table, $column, $data_type, $options = array())
 {
     $cmd = new SDBCommand('ALTER TABLE @table ADD @column ');
     $cmd->set('table', $table, SDB::TableName);
     $cmd->set('column', $column, SDB::FieldName);
     if (array_key_exists(strtolower($data_type), $this->_type_mappings)) {
         $data_type = $this->_type_mappings[strtolower($data_type)];
         if (is_array($data_type)) {
             $options = $options + $data_type[1];
             $data_type = $data_type[0];
         }
     }
     if (array_key_exists(strtolower($data_type), $this->_type_defaults)) {
         $options = $options + $this->_type_defaults[strtolower($data_type)];
     }
     $cmd->command .= strtoupper($data_type);
     if (strtolower($data_type) == 'enum') {
         $values = array();
         if (array_key_exists('values', $options)) {
             $values = $options['values'];
         } elseif (array_key_exists('options', $options)) {
             $values = $options['options'];
         }
         if (!is_array($values) || !count($values)) {
             throw new Exception('Invalid enum values');
         }
         foreach ($values as &$val) {
             $val = SDB::quote($val);
         }
         $cmd->command .= '(' . join(',', $values) . ')';
     } elseif (array_key_exists('size', $options) && $options['size'] > 0) {
         $cmd->command .= '(' . $options['size'] . ')';
     }
     if (array_key_exists('unsigned', $options) && $options['unsigned']) {
         $cmd->command .= ' UNSIGNED';
     }
     $cmd->command .= array_key_exists('null', $options) && $options['null'] ? ' NULL' : ' NOT NULL';
     $cmd->execute();
 }
 static function check_init()
 {
     if (!count(SDB::get_tables_list())) {
         $sql = file_get_contents(BASE . 'conf/simple.' . conf('db.type') . '.sql');
         if (conf('db.type') == 'mysql') {
             $spl = explode(';', $sql);
             foreach ($spl as $part) {
                 $cmd = new SDBCommand($part);
                 $cmd->execute();
             }
         } else {
             $cmd = new SDBCommand($sql);
             $cmd->execute();
         }
         SDB::reset_cached_data();
         $fl = new FileItem();
         $fl->parent_id = 0;
         $fl->name = 's';
         $fl->type = FileItem::Folder;
         $fl->save();
         Initiator::fill_db(S_BASE, $fl->id);
     }
 }
 protected function do_get_table_columns($table)
 {
     $cmd = new SDBCommand("PRAGMA table_info(@tbname)");
     $cmd->set('tbname', $table, SDB::TableName);
     $res = $this->get_all($cmd);
     $fields = array();
     foreach ($res as $row) {
         $type = $row['type'];
         if (strpos($type, '(') !== false) {
             $typename = substr($type, 0, strpos($type, '('));
             $type = substr($type, strpos($type, '(') + 1);
             if (strpos($type, ')') !== false) {
                 $size = intval(substr($type, 0, strpos($type, ')')));
             } else {
                 $size = 255;
             }
         } else {
             $typename = $type;
             $size = 255;
         }
         $typename = strtoupper($typename);
         /* real types */
         /* http://www.sqlite.org/datatype3.html */
         if (strpos($typename, 'INT') !== false) {
             $tp = SDB::Int;
         } else {
             if (strpos($typename, 'CHAR') !== false || strpos($typename, 'CLOB') !== false) {
                 $tp = SDB::String;
             } else {
                 if (strpos($typename, 'BLOB') !== false || strpos($typename, 'TEXT') !== false) {
                     $tp = SDB::Blob;
                 } else {
                     if (strpos($typename, 'REAL') !== false || strpos($typename, 'FLOA') !== false || strpos($typename, 'DOUB') !== false) {
                         $tp = SDB::Float;
                     } else {
                         switch ($typename) {
                             /* virtual types */
                             case 'DATE':
                                 $tp = SDB::Date;
                                 break;
                             case 'DATETIME':
                                 $tp = SDB::DateTime;
                                 break;
                             case 'DECIMAL':
                                 $tp = SDB::Float;
                                 break;
                                 /* no proper type found */
                             /* no proper type found */
                             default:
                                 throw new Exception("Unknown field type \"{$typename}\"");
                         }
                     }
                 }
             }
         }
         $fields[$row['name']] = array('t' => $tp, 's' => $size);
     }
     return $fields;
 }
function set_utf8_connection($db)
{
    $cmd = new SDBCommand('SET NAMES utf8', $db);
    $cmd->execute();
}
Esempio n. 5
0
 protected function apply_migration($id, $class_name, $method, $skip = false)
 {
     if ($skip) {
         $this->message("Skip {$method}: {$class_name}");
     } else {
         $this->message("Migrate {$method}: {$class_name}");
         require $this->get_migrations_dir() . "{$class_name}.php";
         $migration = new $class_name($this);
         $cmd = new SDBCommand("BEGIN");
         $cmd->execute();
         try {
             call_user_func(array($migration, $method));
         } catch (Exception $e) {
             if ($e->getPrevious()) {
                 $e = $e->getPrevious();
             }
             if ($e instanceof PDOException) {
                 $this->message($e->getMessage());
             } else {
                 $this->message('Exception "' . get_class($e) . '" in ' . $e->getFile() . ':' . $e->getLine());
                 $this->message($e->getMessage());
                 $this->message($e->getTraceAsString());
             }
             return false;
         }
         $cmd = new SDBCommand("COMMIT");
         $cmd->execute();
     }
     if ($method == 'down') {
         $cmd = new SDBCommand("DELETE FROM @schema_name WHERE `id`=@id");
     } else {
         $cmd = new SDBCommand("INSERT INTO @schema_name (`id`) VALUES(@id)");
     }
     $cmd->set('schema_name', self::SCHEMA_NAME, SDB::TableName);
     $cmd->set('id', $id, SDB::String);
     $cmd->execute();
     return true;
 }
 public static function remove_by_id($classname, $id)
 {
     $tmp = new $classname();
     $tmp->_init();
     $cmd = new SDBCommand("DELETE FROM @_db_table WHERE @_f_id=@id");
     $cmd->set('_db_table', $tmp->_db_table, SDB::TableName);
     $cmd->set('_f_id', $tmp->_db_key, SDB::FieldName);
     $cmd->set('id', $id, SDB::Int);
     $cmd->execute();
 }
<?php

if (!count(SDB::get_tables_list())) {
    $sql = file_get_contents(BASE . '../common/examples.' . conf('db.type') . '.sql');
    if (conf('db.type') == 'mysql') {
        $spl = explode(';', $sql);
        foreach ($spl as $part) {
            $cmd = new SDBCommand($part);
            $cmd->execute();
        }
    } else {
        $cmd = new SDBCommand($sql);
        $cmd->execute();
    }
    SDB::reset_cached_data();
}
 protected function do_get_table_columns($table)
 {
     $cmd = new SDBCommand("SHOW COLUMNS FROM @tbname");
     $cmd->set('tbname', $table, SDB::TableName);
     $res = $this->get_all($cmd);
     $fields = array();
     foreach ($res as $row) {
         $type = $row['Type'];
         if (strpos($type, '(') !== false) {
             $typename = substr($type, 0, strpos($type, '('));
             $type = substr($type, strpos($type, '(') + 1);
             if (strpos($type, ')') !== false) {
                 $size = intval(substr($type, 0, strpos($type, ')')));
             } else {
                 $size = 255;
             }
         } else {
             $typename = $type;
             $size = 255;
         }
         switch ($typename) {
             case 'varchar':
                 $tp = SDB::String;
                 break;
             case 'tinyint':
                 $tp = SDB::Int;
                 break;
             case 'text':
                 $tp = SDB::Blob;
                 break;
             case 'date':
                 $tp = SDB::Date;
                 break;
             case 'smallint':
                 $tp = SDB::Int;
                 break;
             case 'mediumint':
                 $tp = SDB::Int;
                 break;
             case 'int':
                 $tp = SDB::Int;
                 break;
             case 'bigint':
                 $tp = SDB::Int;
                 break;
             case 'float':
                 $tp = SDB::Float;
                 break;
             case 'double':
                 $tp = SDB::Float;
                 break;
             case 'decimal':
                 $tp = SDB::Float;
                 break;
             case 'datetime':
                 $tp = SDB::DateTime;
                 break;
             case 'timestamp':
                 throw new Exception('TODO: check mysql manual for "timestamp"');
                 break;
             case 'time':
                 throw new Exception('TODO: check mysql manual for "time"');
                 break;
             case 'year':
                 throw new Exception('TODO: check mysql manual for "year"');
                 break;
             case 'char':
                 $tp = SDB::String;
                 break;
             case 'tinyblob':
                 $tp = SDB::Blob;
                 break;
             case 'tinytext':
                 $tp = SDB::Blob;
                 break;
             case 'blob':
                 $tp = SDB::Blob;
                 break;
             case 'mediumblob':
                 $tp = SDB::Blob;
                 break;
             case 'mediumtext':
                 $tp = SDB::Blob;
                 break;
             case 'longblob':
                 $tp = SDB::Blob;
                 break;
             case 'longtext':
                 $tp = SDB::Blob;
                 break;
             case 'enum':
                 throw new Exception('Unsupported type "enum"');
                 break;
             case 'set':
                 throw new Exception('TODO: check mysql manual for "set"');
                 break;
             default:
                 throw new Exception("Unknown field type \"{$typename}\"");
         }
         $fields[$row['Field']] = array('t' => $tp, 's' => $size);
     }
     return $fields;
 }
Esempio n. 9
0
 public static function get_max_position($path)
 {
     $cmd = new SDBCommand("SELECT MAX(position) FROM nodes WHERE path=@path");
     $cmd->set('path', $path, SDB::String, self::PathSize);
     $pos = $cmd->get_one();
     return $pos === null ? 0 : $pos;
 }