wordpress/wp-content/plugins/sqlite-integration/query_alter.class.php
/**
	 * Function to handle ALTER COLUMN.
	 *
	 * @access private
	 * @param array of string $queries
	 * @return string|array of string
	 */
Function to handle ALTER COLUMN.
@access private
@param array of string $queries
@return string|array of string
Function to handle ALTER COLUMN . @access private @param array of string $queries @return string|array of string
private function handle_alter_command($queries) {
		$tokenized_query = $queries;
		$temp_table      = 'temp_'.$tokenized_query['table_name'];
		if (isset($tokenized_query['default_value'])) {
			$def_value = $this->convert_field_types($tokenized_query['column_name'], $tokenized_query['default_value']);
			$def_value = 'DEFAULT '.$def_value;
		} else {
			$def_value = null;
		}
		$_wpdb = new PDODB();
		$query_obj = $_wpdb->get_results("SELECT sql FROM sqlite_master WHERE tbl_name='{$tokenized_query['table_name']}'");
		$_wpdb = null;
		for ($i =0; $i < count($query_obj); $i++) {
			$index_queries[$i] = $query_obj[$i]->sql;
		}
		$create_query = array_shift($index_queries);
		if (stripos($create_query, $tokenized_query['column_name']) === false) {
			return 'SELECT 1=1';
		}
		if (preg_match("/\\s*({$tokenized_query['column_name']})\\s*(.*)?(DEFAULT\\s*.*)[,)]/im", $create_query, $match)) {
			$col_name        = trim($match[1]);
			$col_def         = trim($match[2]);
			$col_def_esc     = str_replace(array('(', ')'), array('\(', '\)'), $col_def);
			$checked_col_def = $this->convert_field_types($col_name, $col_def);
			$old_default     = trim($match[3]);
			$pattern         = "/$col_name\\s*$col_def_esc\\s*$old_default/im";
			if (is_null($def_value)) {
				$replacement = $col_name . ' ' . $checked_col_def;
			} else {
				$replacement = $col_name . ' ' . $checked_col_def . ' ' . $def_value;
			}
			$create_query = preg_replace($pattern, $replacement, $create_query);
			$create_query = str_ireplace($tokenized_query['table_name'], $temp_table, $create_query);
		} elseif (preg_match("/\\s*({$tokenized_query['column_name']})\\s*(.*)?[,)]/im", $create_query, $match)) {
			$col_name        = trim($match[1]);
			$col_def         = trim($match[2]);
			$col_def_esc     = str_replace(array('(', ')'), array('\(', '\)'), $col_def);
			$checked_col_def = $this->convert_field_types($col_name, $col_def);
			$pattern         = "/$col_name\\s*$col_def_esc/im";
			if (is_null($def_value)) {
				$replacement = $col_name . ' ' . $checked_col_def;
			} else {
				$replacement = $col_name . ' ' . $checked_col_def . ' ' . $def_value;
			}
			$create_query = preg_replace($pattern, $replacement, $create_query);
			$create_query = str_ireplace($tokenized_query['table_name'], $temp_table, $create_query);
		} else {
			return 'SELECT 1=1';
		}
		$query[] = $create_query;
		$query[] = "INSERT INTO $temp_table SELECT * FROM {$tokenized_query['table_name']}";
		$query[] = "DROP TABLE IF EXISTS {$tokenized_query['table_name']}";
		$query[] = "ALTER TABLE $temp_table RENAME TO {$tokenized_query['table_name']}";
			foreach ($index_queries as $index) {
			$query[] = $index;
		}
		return $query;
	}
private function handle_alter_command ( $ queries ) { $ tokenized_query = $ queries ; $ temp_table = 'temp_' . $ tokenized_query [ 'table_name' ] ; if ( isset ( $ tokenized_query [ 'default_value' ] ) ) { $ def_value = $ this -> convert_field_types ( $ tokenized_query [ 'column_name' ] , $ tokenized_query [ 'default_value' ] ) ; $ def_value = 'DEFAULT ' . $ def_value ; } else { $ def_value = null ; } $ _wpdb = new PDODB ( ) ; $ query_obj = $ _wpdb -> get_results ( " SELECT sql FROM sqlite_master WHERE tbl_name=' { $ tokenized_query [ 'table_name' ] } ' " ) ; $ _wpdb = null ; for ( $ i = 0 ; $ i < count ( $ query_obj ) ; $ i ++ ) { $ index_queries [ $ i ] = $ query_obj [ $ i ] -> sql ; } $ create_query = array_shift ( $ index_queries ) ; if ( stripos ( $ create_query , $ tokenized_query [ 'column_name' ] ) === false ) { return 'SELECT 1=1' ; } if ( preg_match ( " / \\ s*( { $ tokenized_query [ 'column_name' ] } ) \\ s*(.*)?(DEFAULT \\ s*.*)[,)]/im " , $ create_query , $ match ) ) { $ col_name = trim ( $ match [ 1 ] ) ; $ col_def = trim ( $ match [ 2 ] ) ; $ col_def_esc = str_replace ( array ( '(' , ')' ) , array ( '\(' , '\)' ) , $ col_def ) ; $ checked_col_def = $ this -> convert_field_types ( $ col_name , $ col_def ) ; $ old_default = trim ( $ match [ 3 ] ) ; $ pattern = " / $ col_name \\ s* $ col_def_esc \\ s* $ old_default /im " ; if ( is_null ( $ def_value ) ) { $ replacement = $ col_name . ' ' . $ checked_col_def ; } else { $ replacement = $ col_name . ' ' . $ checked_col_def . ' ' . $ def_value ; } $ create_query = preg_replace ( $ pattern , $ replacement , $ create_query ) ; $ create_query = str_ireplace ( $ tokenized_query [ 'table_name' ] , $ temp_table , $ create_query ) ; } elseif ( preg_match ( " / \\ s*( { $ tokenized_query [ 'column_name' ] } ) \\ s*(.*)?[,)]/im " , $ create_query , $ match ) ) { $ col_name = trim ( $ match [ 1 ] ) ; $ col_def = trim ( $ match [ 2 ] ) ; $ col_def_esc = str_replace ( array ( '(' , ')' ) , array ( '\(' , '\)' ) , $ col_def ) ; $ checked_col_def = $ this -> convert_field_types ( $ col_name , $ col_def ) ; $ pattern = " / $ col_name \\ s* $ col_def_esc /im " ; if ( is_null ( $ def_value ) ) { $ replacement = $ col_name . ' ' . $ checked_col_def ; } else { $ replacement = $ col_name . ' ' . $ checked_col_def . ' ' . $ def_value ; } $ create_query = preg_replace ( $ pattern , $ replacement , $ create_query ) ; $ create_query = str_ireplace ( $ tokenized_query [ 'table_name' ] , $ temp_table , $ create_query ) ; } else { return 'SELECT 1=1' ; } $ query [ ] = $ create_query ; $ query [ ] = " INSERT INTO  $ temp_table  SELECT * FROM  { $ tokenized_query [ 'table_name' ] } " ; $ query [ ] = " DROP TABLE IF EXISTS  { $ tokenized_query [ 'table_name' ] } " ; $ query [ ] = " ALTER TABLE  $ temp_table  RENAME TO  { $ tokenized_query [ 'table_name' ] } " ; foreach ( $ index_queries as $ index ) { $ query [ ] = $ index ; } return $ query ; }
Function to handle ALTER COLUMN.
Function to handle ALTER COLUMN .