/** * tries to get the create statement of a function */ public function testGetRoutineFunction() { $function = Routine::model()->findByPk(array('ROUTINE_SCHEMA' => 'routinetest', 'ROUTINE_NAME' => 'test_function')); $createRoutine = $function->getCreateRoutine(); $this->assertType('string', $createRoutine); $this->assertType('string', $createRoutine); $this->assertType('string', $function->delete()); $cmd = Routine::$db->createCommand($createRoutine); $this->assertEquals(0, $cmd->execute()); $function = Routine::model()->findByPk(array('ROUTINE_SCHEMA' => 'routinetest', 'ROUTINE_NAME' => 'test_function')); $this->assertType('Routine', $function); }
/** * Connects to the specified schema and assigns it to all models which need it. * * @param $schema schema * @return CDbConnection */ protected function connectDb($schema) { // Assign request $this->request = Yii::app()->getRequest(); // Check parameter if (is_null($schema)) { $this->db = null; return null; } // Connect to database $connectionString = 'mysql:host=' . Yii::app()->user->host . ';port=' . Yii::app()->user->port . ';dbname=' . $schema . '; charset=utf8'; $this->db = new CDbConnection($connectionString, utf8_decode(Yii::app()->user->name), utf8_decode(Yii::app()->user->password)); $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES \'utf8\''); $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET CHARACTER SET \'utf8\''); $this->db->charset = 'utf8'; $this->db->emulatePrepare = true; $this->db->active = true; // Schema name is set in connection string // $this->db->createCommand('USE ' . $this->db->quoteTableName($schema))->execute(); // Assign to all models which need it ActiveRecord::$db = Routine::$db = Row::$db = Trigger::$db = View::$db = $this->db; // Return connection return $this->db; }
/** * Entry point of routine work * * @static * @param string $workingDir * @param array $config * @param array $blackList * @return null */ public static function run($workingDir, $config, $blackList) { // various display parameters $workingDir = realpath($workingDir); self::$_workingDirLen = strlen($workingDir); self::$_errorsCount = 0; self::$_updatedCount = 0; self::$_skippedCount = 0; // set black list self::$skipFiles = array(); self::$skipDirectories = array(); self::_setSkippedPaths($workingDir, $blackList); $licenseInstances = array(); foreach ($config as $path => $types) { // whether to scan directory recursively $recursive = isset($types['_recursive']) ? $types['_recursive'] : true; unset($types['_recursive']); // update licenses foreach ($types as $fileType => $licenseType) { if (!isset($licenseInstances[$licenseType])) { $licenseInstances[$licenseType] = Routine::createLicenseInstance($licenseType); } Routine::updateLicense(array($workingDir . ($path ? DIRECTORY_SEPARATOR . $path : '')), Routine::$fileTypes[$fileType], $licenseInstances[$licenseType], $recursive); } } Routine::printLog(sprintf("\n" . 'Updated: %d; Skipped: %d; Errors: %d.' . "\n", self::$_updatedCount, self::$_skippedCount, self::$_errorsCount)); if (self::$_errorsCount || self::$_skippedCount) { throw new Exception('Failed: check skipped files or errors.' . "\n"); } Routine::printLog('Success.' . "\n"); }
/** * Exports all routines of the given array and writes the dump to the output buffer. * * @param array list of routines */ private function exportRoutines($routines) { // Get DbConnection object $db = Yii::app()->db; // Escape all routine names $routineNames = array(); foreach ($routines as $routine) { $routineNames[] = Yii::app()->db->quoteValue($routine); } // Find all routines $routines = Routine::model()->findAll('ROUTINE_NAME IN (' . implode(',', $routineNames) . ') ' . 'AND ROUTINE_SCHEMA = ' . $db->quoteValue($this->schema)); foreach ($routines as $routine) { $this->comment(ucfirst(strtolower($routine->ROUTINE_TYPE)) . ' ' . $db->quoteTableName($routine->ROUTINE_NAME)); echo "\n\n"; if ($this->settings['addDropObject']) { echo 'DROP ', strtoupper($routine->ROUTINE_TYPE), ' IF EXISTS ', $db->quoteTableName($routine->ROUTINE_NAME), ";\n"; } echo $routine->getCreateRoutine(), ";\n\n"; } }
/** * Setup test databases. */ protected function setUp() { $this->executeSqlFile('models/ForeignKeyTest.sql'); Column::$db = ForeignKey::$db = Index::$db = Routine::$db = Row::$db = Schema::$db = Table::$db = Trigger::$db = View::$db = $this->createDbConnection('tabletest'); }
/** * Lists all routines (procedures & functions). */ public function actionRoutines() { $schema = $this->loadSchema(); // Criteria $criteria = new CDbCriteria(); $criteria->condition = 'ROUTINE_SCHEMA = :schema'; $criteria->params = array(':schema' => $this->schema); // Pagination $pages = new Pagination(Routine::model()->count($criteria)); $pages->setupPageSize('pageSize', 'schema.routines'); $pages->applyLimit($criteria); $pages->route = '#routines'; // Sort $sort = new CSort('View'); $sort->attributes = array('ROUTINE_NAME' => 'name'); $sort->route = '#routines'; $sort->applyOrder($criteria); // Load data $schema->routines = Routine::model()->findAll($criteria); // Render $this->render('routines', array('schema' => $schema, 'routineCount' => count($schema->routines), 'pages' => $pages, 'sort' => $sort)); }
static function __static() { self::$SETACCESSIBLE_AVAILABLE = method_exists('ReflectionMethod', 'setAccessible'); }
/** * Creates a new constructor * * @param lang.mirrors.TypeMirror $mirror */ public function __construct($mirror) { parent::__construct($mirror, $mirror->reflect->constructor()); }
USAGE ); $options = getopt('e:w:vd0'); if (!isset($options['e'])) { print USAGE; exit(1); } if (isset($options['v'])) { Routine::$isVerbose = true; } $dryRun = false; if (isset($options['d'])) { Routine::$dryRun = true; } $workingDir = '.'; if (isset($options['w'])) { $workingDir = rtrim($options['w'], DIRECTORY_SEPARATOR); } if (!is_dir($workingDir)) { Routine::printLog("Directory '{$workingDir}' does not exist.\n"); exit(1); } $config = (require __DIR__ . "/conf/{$options['e']}.php"); $blackList = (require __DIR__ . '/../../../../dev/tools/license_placeholder/blacklist.php'); try { Routine::run($workingDir, $config, $blackList); } catch (Exception $e) { Routine::printLog($e->getMessage()); exit(isset($options['0']) ? 0 : 1); }
/** * Updates a routine. */ public function actionUpdate() { $routine = Routine::model()->findByPk(array('ROUTINE_SCHEMA' => $this->schema, 'ROUTINE_NAME' => $this->routine)); if (is_null($routine)) { $routine = new Routine(); $routine->ROUTINE_TYPE = $_POST['type']; } $type = strtolower($routine->ROUTINE_TYPE); if (isset($_POST['query'])) { $currentRoutine = $routine->getCreateRoutine(); $query = $_POST['query']; try { // Split queries $splitter = new SqlSplitter($query); $splitter->delimiter = self::$delimiter; $queries = $splitter->getQueries(); foreach ($queries as $query2) { $cmd = $this->db->createCommand($query2); $cmd->prepare(); $cmd->execute(); } $response = new AjaxResponse(); $response->addNotification('success', Yii::t('core', 'successAlterRoutine', array('{routine}' => $routine->ROUTINE_NAME)), null, $query); $response->refresh = true; $this->sendJSON($response); } catch (CDbException $ex) { $errorInfo = $cmd->getPdoStatement()->errorInfo(); $routine->addError(null, Yii::t('core', 'sqlErrorOccured', array('{errno}' => $errorInfo[1], '{errmsg}' => $errorInfo[2]))); $this->restoreCurrentRoutine($currentRoutine); } } else { $query = 'DROP ' . strtoupper($routine->ROUTINE_TYPE) . ' ' . $this->db->quoteTableName($routine->ROUTINE_NAME) . self::$delimiter . "\n" . $routine->getCreateRoutine(); } CHtml::generateRandomIdPrefix(); $this->render('form', array('routine' => $routine, 'type' => $type, 'query' => $query)); }