2010-04-21 12:01:32 +00:00
< ? php
/**
* @ author Jakub Cernohuby
* @ author Vladimir Stastka
* @ author Jakub Vrana
*/
2010-10-29 13:24:06 +00:00
$drivers [ " mssql " ] = " MS SQL " ;
2010-04-21 12:01:32 +00:00
if ( isset ( $_GET [ " mssql " ])) {
2010-10-29 13:24:06 +00:00
$possible_drivers = array ( " SQLSRV " , " MSSQL " );
2010-04-21 12:01:32 +00:00
define ( " DRIVER " , " mssql " );
if ( extension_loaded ( " sqlsrv " )) {
class Min_DB {
2013-01-10 06:19:38 +00:00
var $extension = " sqlsrv " , $_link , $_result , $server_info , $affected_rows , $errno , $error ;
2010-04-21 12:01:32 +00:00
function _get_error () {
$this -> error = " " ;
foreach ( sqlsrv_errors () as $error ) {
2013-01-10 06:19:38 +00:00
$this -> errno = $error [ " code " ];
2010-04-21 12:01:32 +00:00
$this -> error .= " $error[message] \n " ;
}
$this -> error = rtrim ( $this -> error );
}
function connect ( $server , $username , $password ) {
2012-06-29 22:23:35 +00:00
$this -> _link = @ sqlsrv_connect ( $server , array ( " UID " => $username , " PWD " => $password , " CharacterSet " => " UTF-8 " ));
2010-04-21 12:01:32 +00:00
if ( $this -> _link ) {
$info = sqlsrv_server_info ( $this -> _link );
$this -> server_info = $info [ 'SQLServerVersion' ];
} else {
$this -> _get_error ();
}
return ( bool ) $this -> _link ;
}
function quote ( $string ) {
return " ' " . str_replace ( " ' " , " '' " , $string ) . " ' " ;
}
function select_db ( $database ) {
2012-12-01 23:11:01 +00:00
return $this -> query ( " USE " . idf_escape ( $database ));
2010-04-21 12:01:32 +00:00
}
function query ( $query , $unbuffered = false ) {
$result = sqlsrv_query ( $this -> _link , $query ); //! , array(), ($unbuffered ? array() : array("Scrollable" => "keyset"))
2012-07-15 16:21:22 +00:00
$this -> error = " " ;
2010-04-21 12:01:32 +00:00
if ( ! $result ) {
$this -> _get_error ();
return false ;
}
return $this -> store_result ( $result );
}
function multi_query ( $query ) {
$this -> _result = sqlsrv_query ( $this -> _link , $query );
2012-07-15 16:21:22 +00:00
$this -> error = " " ;
2010-04-21 12:01:32 +00:00
if ( ! $this -> _result ) {
$this -> _get_error ();
return false ;
}
return true ;
}
function store_result ( $result = null ) {
if ( ! $result ) {
$result = $this -> _result ;
}
if ( sqlsrv_field_metadata ( $result )) {
return new Min_Result ( $result );
}
$this -> affected_rows = sqlsrv_rows_affected ( $result );
return true ;
}
function next_result () {
return sqlsrv_next_result ( $this -> _result );
}
function result ( $query , $field = 0 ) {
$result = $this -> query ( $query );
if ( ! is_object ( $result )) {
return false ;
}
$row = $result -> fetch_row ();
return $row [ $field ];
}
}
class Min_Result {
var $_result , $_offset = 0 , $_fields , $num_rows ;
function Min_Result ( $result ) {
$this -> _result = $result ;
2010-05-27 14:10:17 +00:00
// $this->num_rows = sqlsrv_num_rows($result); // available only in scrollable results
2010-04-21 12:01:32 +00:00
}
function _convert ( $row ) {
foreach (( array ) $row as $key => $val ) {
if ( is_a ( $val , 'DateTime' )) {
$row [ $key ] = $val -> format ( " Y-m-d H:i:s " );
}
//! stream
}
return $row ;
}
function fetch_assoc () {
return $this -> _convert ( sqlsrv_fetch_array ( $this -> _result , SQLSRV_FETCH_ASSOC , SQLSRV_SCROLL_NEXT ));
}
function fetch_row () {
return $this -> _convert ( sqlsrv_fetch_array ( $this -> _result , SQLSRV_FETCH_NUMERIC , SQLSRV_SCROLL_NEXT ));
}
function fetch_field () {
if ( ! $this -> _fields ) {
$this -> _fields = sqlsrv_field_metadata ( $this -> _result );
}
$field = $this -> _fields [ $this -> _offset ++ ];
$return = new stdClass ;
$return -> name = $field [ " Name " ];
$return -> orgname = $field [ " Name " ];
$return -> type = ( $field [ " Type " ] == 1 ? 254 : 0 );
return $return ;
}
2010-04-23 09:04:05 +00:00
function seek ( $offset ) {
for ( $i = 0 ; $i < $offset ; $i ++ ) {
sqlsrv_fetch ( $this -> _result ); // SQLSRV_SCROLL_ABSOLUTE added in sqlsrv 1.1
}
}
2010-04-21 12:01:32 +00:00
function __destruct () {
sqlsrv_free_stmt ( $this -> _result );
}
}
} elseif ( extension_loaded ( " mssql " )) {
class Min_DB {
var $extension = " MSSQL " , $_link , $_result , $server_info , $affected_rows , $error ;
function connect ( $server , $username , $password ) {
$this -> _link = @ mssql_connect ( $server , $username , $password );
if ( $this -> _link ) {
$result = $this -> query ( " SELECT SERVERPROPERTY('ProductLevel'), SERVERPROPERTY('Edition') " );
$row = $result -> fetch_row ();
2010-05-26 14:00:21 +00:00
$this -> server_info = $this -> result ( " sp_server_info 2 " , 2 ) . " [ $row[0] ] $row[1] " ;
2010-04-21 12:01:32 +00:00
} else {
$this -> error = mssql_get_last_message ();
}
return ( bool ) $this -> _link ;
}
function quote ( $string ) {
return " ' " . str_replace ( " ' " , " '' " , $string ) . " ' " ;
}
function select_db ( $database ) {
return mssql_select_db ( $database );
}
function query ( $query , $unbuffered = false ) {
$result = mssql_query ( $query , $this -> _link ); //! $unbuffered
2012-07-15 16:21:22 +00:00
$this -> error = " " ;
2010-04-21 12:01:32 +00:00
if ( ! $result ) {
$this -> error = mssql_get_last_message ();
return false ;
}
if ( $result === true ) {
$this -> affected_rows = mssql_rows_affected ( $this -> _link );
return true ;
}
return new Min_Result ( $result );
}
function multi_query ( $query ) {
return $this -> _result = $this -> query ( $query );
}
function store_result () {
return $this -> _result ;
}
function next_result () {
return mssql_next_result ( $this -> _result );
}
function result ( $query , $field = 0 ) {
$result = $this -> query ( $query );
if ( ! is_object ( $result )) {
return false ;
}
return mssql_result ( $result -> _result , 0 , $field );
}
}
class Min_Result {
var $_result , $_offset = 0 , $_fields , $num_rows ;
function Min_Result ( $result ) {
$this -> _result = $result ;
$this -> num_rows = mssql_num_rows ( $result );
}
function fetch_assoc () {
return mssql_fetch_assoc ( $this -> _result );
}
function fetch_row () {
return mssql_fetch_row ( $this -> _result );
}
function num_rows () {
return mssql_num_rows ( $this -> _result );
}
function fetch_field () {
$return = mssql_fetch_field ( $this -> _result );
$return -> orgtable = $return -> table ;
$return -> orgname = $return -> name ;
return $return ;
}
2010-04-23 09:04:05 +00:00
function seek ( $offset ) {
mssql_data_seek ( $this -> _result , $offset );
}
2010-04-21 12:01:32 +00:00
function __destruct () {
mssql_free_result ( $this -> _result );
}
}
}
2013-07-05 16:04:06 +00:00
class Min_Driver extends Min_SQL {
function insertUpdate ( $table , $set , $primary ) {
$update = array ();
$where = array ();
foreach ( $set as $key => $val ) {
$update [] = " $key = $val " ;
if ( isset ( $primary [ idf_unescape ( $key )])) {
$where [] = " $key = $val " ;
}
}
// can use only one query for all rows with different API
return queries ( " MERGE " . table ( $table ) . " USING (VALUES( " . implode ( " , " , $set ) . " )) AS source (c " . implode ( " , c " , range ( 1 , count ( $set ))) . " ) ON " . implode ( " AND " , $where ) //! source, c1 - possible conflict
. " WHEN MATCHED THEN UPDATE SET " . implode ( " , " , $update )
. " WHEN NOT MATCHED THEN INSERT ( " . implode ( " , " , array_keys ( $set )) . " ) VALUES ( " . implode ( " , " , $set ) . " ); " // ; is mandatory
);
}
}
2010-04-21 12:01:32 +00:00
function idf_escape ( $idf ) {
return " [ " . str_replace ( " ] " , " ]] " , $idf ) . " ] " ;
}
2010-05-11 14:45:04 +00:00
function table ( $idf ) {
return ( $_GET [ " ns " ] != " " ? idf_escape ( $_GET [ " ns " ]) . " . " : " " ) . idf_escape ( $idf );
}
2010-04-21 12:01:32 +00:00
function connect () {
global $adminer ;
$connection = new Min_DB ;
$credentials = $adminer -> credentials ();
if ( $connection -> connect ( $credentials [ 0 ], $credentials [ 1 ], $credentials [ 2 ])) {
return $connection ;
}
return $connection -> error ;
}
function get_databases () {
return get_vals ( " EXEC sp_databases " );
}
2010-05-14 13:51:54 +00:00
function limit ( $query , $where , $limit , $offset = 0 , $separator = " " ) {
2012-05-14 06:54:07 +00:00
return ( $limit !== null ? " TOP ( " . ( $limit + $offset ) . " ) " : " " ) . " $query $where " ; // seek later
2010-04-21 12:01:32 +00:00
}
2010-05-14 13:51:54 +00:00
function limit1 ( $query , $where ) {
return limit ( $query , $where , 1 );
2010-04-21 12:01:32 +00:00
}
function db_collation ( $db , $collations ) {
global $connection ;
2010-10-13 16:04:40 +00:00
return $connection -> result ( " SELECT collation_name FROM sys.databases WHERE name = " . q ( $db ));
2010-04-21 12:01:32 +00:00
}
function engines () {
return array ();
}
function logged_user () {
global $connection ;
return $connection -> result ( " SELECT SUSER_NAME() " );
}
function tables_list () {
2010-10-13 16:04:40 +00:00
return get_key_vals ( " SELECT name, type_desc FROM sys.all_objects WHERE schema_id = SCHEMA_ID( " . q ( get_schema ()) . " ) AND type IN ('S', 'U', 'V') ORDER BY name " );
2010-04-21 12:01:32 +00:00
}
function count_tables ( $databases ) {
global $connection ;
$return = array ();
foreach ( $databases as $db ) {
$connection -> select_db ( $db );
2010-10-16 14:38:20 +00:00
$return [ $db ] = $connection -> result ( " SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES " );
2010-04-21 12:01:32 +00:00
}
return $return ;
}
function table_status ( $name = " " ) {
$return = array ();
2013-05-28 18:12:25 +00:00
foreach ( get_rows ( " SELECT name AS Name, type_desc AS Engine FROM sys.all_objects WHERE schema_id = SCHEMA_ID( " . q ( get_schema ()) . " ) AND type IN ('S', 'U', 'V') " . ( $name != " " ? " AND name = " . q ( $name ) : " ORDER BY name " )) as $row ) {
2010-04-21 12:01:32 +00:00
if ( $name != " " ) {
return $row ;
}
$return [ $row [ " Name " ]] = $row ;
}
return $return ;
}
2010-05-27 11:31:08 +00:00
function is_view ( $table_status ) {
return $table_status [ " Engine " ] == " VIEW " ;
}
2010-04-21 12:01:32 +00:00
function fk_support ( $table_status ) {
return true ;
}
2010-11-11 09:47:32 +00:00
function fields ( $table ) {
2010-04-21 12:01:32 +00:00
$return = array ();
2010-10-13 15:53:59 +00:00
foreach ( get_rows ( " SELECT c.*, t.name type, d.definition [default]
2010-05-11 15:41:42 +00:00
FROM sys . all_columns c
JOIN sys . all_objects o ON c . object_id = o . object_id
JOIN sys . types t ON c . user_type_id = t . user_type_id
LEFT JOIN sys . default_constraints d ON c . default_object_id = d . parent_column_id
2010-10-13 16:04:40 +00:00
WHERE o . schema_id = SCHEMA_ID ( " . q(get_schema()) . " ) AND o . type IN ( 'S' , 'U' , 'V' ) AND o . name = " . q( $table )
2010-10-13 15:53:59 +00:00
) as $row ) {
2010-05-26 14:10:09 +00:00
$type = $row [ " type " ];
$length = ( ereg ( " char|binary " , $type ) ? $row [ " max_length " ] : ( $type == " decimal " ? " $row[precision] , $row[scale] " : " " ));
2010-05-11 15:41:42 +00:00
$return [ $row [ " name " ]] = array (
" field " => $row [ " name " ],
2010-05-26 14:10:09 +00:00
" full_type " => $type . ( $length ? " ( $length ) " : " " ),
" type " => $type ,
2010-05-11 21:17:11 +00:00
" length " => $length ,
2010-05-11 15:41:42 +00:00
" default " => $row [ " default " ],
" null " => $row [ " is_nullable " ],
2010-04-23 09:04:05 +00:00
" auto_increment " => $row [ " is_identity " ],
2010-05-11 15:41:42 +00:00
" collation " => $row [ " collation_name " ],
2010-04-21 12:01:32 +00:00
" privileges " => array ( " insert " => 1 , " select " => 1 , " update " => 1 ),
2010-04-23 09:04:05 +00:00
" primary " => $row [ " is_identity " ], //! or indexes.is_primary_key
2010-04-21 12:01:32 +00:00
);
}
return $return ;
}
function indexes ( $table , $connection2 = null ) {
$return = array ();
// sp_statistics doesn't return information about primary key
2013-06-25 16:42:47 +00:00
foreach ( get_rows ( " SELECT i.name, key_ordinal, is_unique, is_primary_key, c.name AS column_name, is_descending_key
2010-10-16 06:23:34 +00:00
FROM sys . indexes i
INNER JOIN sys . index_columns ic ON i . object_id = ic . object_id AND i . index_id = ic . index_id
INNER JOIN sys . columns c ON ic . object_id = c . object_id AND ic . column_id = c . column_id
WHERE OBJECT_NAME ( i . object_id ) = " . q( $table )
2010-10-13 16:59:15 +00:00
, $connection2 ) as $row ) {
2013-06-25 16:42:47 +00:00
$name = $row [ " name " ];
$return [ $name ][ " type " ] = ( $row [ " is_primary_key " ] ? " PRIMARY " : ( $row [ " is_unique " ] ? " UNIQUE " : " INDEX " ));
$return [ $name ][ " lengths " ] = array ();
$return [ $name ][ " columns " ][ $row [ " key_ordinal " ]] = $row [ " column_name " ];
$return [ $name ][ " descs " ][ $row [ " key_ordinal " ]] = ( $row [ " is_descending_key " ] ? '1' : null );
2010-04-21 12:01:32 +00:00
}
return $return ;
}
2010-05-27 11:25:04 +00:00
function view ( $name ) {
global $connection ;
2010-10-16 14:38:20 +00:00
return array ( " select " => preg_replace ( '~^(?:[^[]|\\[[^]]*])*\\s+AS\\s+~isU' , '' , $connection -> result ( " SELECT VIEW_DEFINITION FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = SCHEMA_NAME() AND TABLE_NAME = " . q ( $name ))));
2010-05-27 11:25:04 +00:00
}
2010-04-21 12:01:32 +00:00
function collations () {
$return = array ();
foreach ( get_vals ( " SELECT name FROM fn_helpcollations() " ) as $collation ) {
$return [ ereg_replace ( " _.* " , " " , $collation )][] = $collation ;
}
return $return ;
}
function information_schema ( $db ) {
return false ;
}
function error () {
global $connection ;
2010-04-22 14:38:05 +00:00
return nl_br ( h ( preg_replace ( '~^(\\[[^]]*])+~m' , '' , $connection -> error )));
2010-04-21 12:01:32 +00:00
}
2010-04-22 23:02:28 +00:00
function create_database ( $db , $collation ) {
2010-10-17 16:45:05 +00:00
return queries ( " CREATE DATABASE " . idf_escape ( $db ) . ( eregi ( '^[a-z0-9_]+$' , $collation ) ? " COLLATE $collation " : " " ));
2010-04-22 23:02:28 +00:00
}
function drop_databases ( $databases ) {
return queries ( " DROP DATABASE " . implode ( " , " , array_map ( 'idf_escape' , $databases )));
}
2010-04-21 12:01:32 +00:00
function rename_database ( $name , $collation ) {
2010-10-17 16:45:05 +00:00
if ( eregi ( '^[a-z0-9_]+$' , $collation )) {
2010-10-16 13:31:25 +00:00
queries ( " ALTER DATABASE " . idf_escape ( DB ) . " COLLATE $collation " );
2010-04-21 12:01:32 +00:00
}
2010-04-23 09:04:05 +00:00
queries ( " ALTER DATABASE " . idf_escape ( DB ) . " MODIFY NAME = " . idf_escape ( $name ));
return true ; //! false negative "The database name 'test2' has been set."
2010-04-21 12:01:32 +00:00
}
function auto_increment () {
2010-11-28 16:46:02 +00:00
return " IDENTITY " . ( $_POST [ " Auto_increment " ] != " " ? " ( " . ( + $_POST [ " Auto_increment " ]) . " ,1) " : " " ) . " PRIMARY KEY " ;
2010-05-26 14:00:21 +00:00
}
function alter_table ( $table , $name , $fields , $foreign , $comment , $engine , $collation , $auto_increment , $partitioning ) {
$alter = array ();
foreach ( $fields as $field ) {
$column = idf_escape ( $field [ 0 ]);
$val = $field [ 1 ];
if ( ! $val ) {
2010-11-28 16:39:07 +00:00
$alter [ " DROP " ][] = " COLUMN $column " ;
2010-05-26 14:00:21 +00:00
} else {
$val [ 1 ] = preg_replace ( " ~( COLLATE )'( \\ w+)'~ " , " \\ 1 \\ 2 " , $val [ 1 ]);
if ( $field [ 0 ] == " " ) {
2010-11-28 16:39:07 +00:00
$alter [ " ADD " ][] = " \n " . implode ( " " , $val ) . ( $table == " " ? substr ( $foreign [ $val [ 0 ]], 16 + strlen ( $val [ 0 ])) : " " ); // 16 - strlen(" FOREIGN KEY ()")
2010-05-26 14:00:21 +00:00
} else {
unset ( $val [ 6 ]); //! identity can't be removed
if ( $column != $val [ 0 ]) {
2010-10-13 16:04:40 +00:00
queries ( " EXEC sp_rename " . q ( table ( $table ) . " . $column " ) . " , " . q ( idf_unescape ( $val [ 0 ])) . " , 'COLUMN' " );
2010-05-26 14:00:21 +00:00
}
$alter [ " ALTER COLUMN " . implode ( " " , $val )][] = " " ;
}
}
}
if ( $table == " " ) {
return queries ( " CREATE TABLE " . table ( $name ) . " ( " . implode ( " , " , ( array ) $alter [ " ADD " ]) . " \n ) " );
}
if ( $table != $name ) {
2010-10-13 16:04:40 +00:00
queries ( " EXEC sp_rename " . q ( table ( $table )) . " , " . q ( $name ));
2010-05-26 14:00:21 +00:00
}
2010-11-28 15:28:15 +00:00
if ( $foreign ) {
$alter [ " " ] = $foreign ;
}
2010-05-26 14:00:21 +00:00
foreach ( $alter as $key => $val ) {
if ( ! queries ( " ALTER TABLE " . idf_escape ( $name ) . " $key " . implode ( " , " , $val ))) {
return false ;
}
}
return true ;
2010-04-21 12:01:32 +00:00
}
2010-07-14 14:18:12 +00:00
function alter_indexes ( $table , $alter ) {
$index = array ();
$drop = array ();
foreach ( $alter as $val ) {
2011-07-13 13:13:00 +00:00
if ( $val [ 2 ] == " DROP " ) {
2010-07-14 14:18:12 +00:00
if ( $val [ 0 ] == " PRIMARY " ) { //! sometimes used also for UNIQUE
2011-07-13 13:13:00 +00:00
$drop [] = idf_escape ( $val [ 1 ]);
2010-07-14 14:18:12 +00:00
} else {
2011-07-13 13:13:00 +00:00
$index [] = idf_escape ( $val [ 1 ]) . " ON " . table ( $table );
2010-07-14 14:18:12 +00:00
}
} elseif ( ! queries (( $val [ 0 ] != " PRIMARY "
2011-07-13 13:13:00 +00:00
? " CREATE $val[0] " . ( $val [ 0 ] != " INDEX " ? " INDEX " : " " ) . idf_escape ( $val [ 1 ] != " " ? $val [ 1 ] : uniqid ( $table . " _ " )) . " ON " . table ( $table )
2010-07-14 14:18:12 +00:00
: " ALTER TABLE " . table ( $table ) . " ADD PRIMARY KEY "
2011-07-13 13:13:00 +00:00
) . " $val[2] " )) {
2010-07-14 14:18:12 +00:00
return false ;
}
}
return ( ! $index || queries ( " DROP INDEX " . implode ( " , " , $index )))
&& ( ! $drop || queries ( " ALTER TABLE " . table ( $table ) . " DROP " . implode ( " , " , $drop )))
;
}
2010-04-23 23:49:21 +00:00
function begin () {
return queries ( " BEGIN TRANSACTION " );
}
2010-05-12 16:07:46 +00:00
function last_id () {
global $connection ;
return $connection -> result ( " SELECT SCOPE_IDENTITY() " ); // @@IDENTITY can return trigger INSERT
}
2010-04-21 12:01:32 +00:00
function explain ( $connection , $query ) {
$connection -> query ( " SET SHOWPLAN_ALL ON " );
$return = $connection -> query ( $query );
$connection -> query ( " SET SHOWPLAN_ALL OFF " ); // connection is used also for indexes
return $return ;
}
2011-07-29 15:08:06 +00:00
function found_rows ( $table_status , $where ) {
}
2010-04-22 14:38:05 +00:00
function foreign_keys ( $table ) {
$return = array ();
2010-10-13 16:04:40 +00:00
foreach ( get_rows ( " EXEC sp_fkeys @fktable_name = " . q ( $table )) as $row ) {
2010-04-22 14:38:05 +00:00
$foreign_key = & $return [ $row [ " FK_NAME " ]];
$foreign_key [ " table " ] = $row [ " PKTABLE_NAME " ];
$foreign_key [ " source " ][] = $row [ " FKCOLUMN_NAME " ];
$foreign_key [ " target " ][] = $row [ " PKCOLUMN_NAME " ];
}
return $return ;
}
function truncate_tables ( $tables ) {
2010-05-17 16:18:32 +00:00
return apply_queries ( " TRUNCATE TABLE " , $tables );
2010-04-22 14:38:05 +00:00
}
function drop_views ( $views ) {
2010-05-11 14:45:04 +00:00
return queries ( " DROP VIEW " . implode ( " , " , array_map ( 'table' , $views )));
2010-04-22 14:38:05 +00:00
}
function drop_tables ( $tables ) {
2010-05-11 14:45:04 +00:00
return queries ( " DROP TABLE " . implode ( " , " , array_map ( 'table' , $tables )));
2010-04-22 14:38:05 +00:00
}
2010-05-11 14:45:04 +00:00
function move_tables ( $tables , $views , $target ) {
2010-05-17 16:18:32 +00:00
return apply_queries ( " ALTER SCHEMA " . idf_escape ( $target ) . " TRANSFER " , array_merge ( $tables , $views ));
2010-05-11 14:45:04 +00:00
}
2010-04-22 14:38:05 +00:00
function trigger ( $name ) {
2011-08-08 16:18:16 +00:00
if ( $name == " " ) {
return array ();
}
2010-10-13 15:53:59 +00:00
$rows = get_rows ( " SELECT s.name [Trigger],
2010-04-22 14:38:05 +00:00
CASE WHEN OBJECTPROPERTY ( s . id , 'ExecIsInsertTrigger' ) = 1 THEN 'INSERT' WHEN OBJECTPROPERTY ( s . id , 'ExecIsUpdateTrigger' ) = 1 THEN 'UPDATE' WHEN OBJECTPROPERTY ( s . id , 'ExecIsDeleteTrigger' ) = 1 THEN 'DELETE' END [ Event ],
CASE WHEN OBJECTPROPERTY ( s . id , 'ExecIsInsteadOfTrigger' ) = 1 THEN 'INSTEAD OF' ELSE 'AFTER' END [ Timing ],
2010-04-22 15:53:42 +00:00
c . text
2010-04-22 14:38:05 +00:00
FROM sysobjects s
JOIN syscomments c ON s . id = c . id
2010-10-13 16:04:40 +00:00
WHERE s . xtype = 'TR' AND s . name = " . q( $name )
2010-05-11 14:45:04 +00:00
); // triggers are not schema-scoped
2010-10-13 15:53:59 +00:00
$return = reset ( $rows );
if ( $return ) {
$return [ " Statement " ] = preg_replace ( '~^.+\\s+AS\\s+~isU' , '' , $return [ " text " ]); //! identifiers, comments
}
return $return ;
2010-04-22 14:38:05 +00:00
}
function triggers ( $table ) {
$return = array ();
2010-10-13 15:53:59 +00:00
foreach ( get_rows ( " SELECT sys1.name,
2010-04-22 14:38:05 +00:00
CASE WHEN OBJECTPROPERTY ( sys1 . id , 'ExecIsInsertTrigger' ) = 1 THEN 'INSERT' WHEN OBJECTPROPERTY ( sys1 . id , 'ExecIsUpdateTrigger' ) = 1 THEN 'UPDATE' WHEN OBJECTPROPERTY ( sys1 . id , 'ExecIsDeleteTrigger' ) = 1 THEN 'DELETE' END [ Event ],
CASE WHEN OBJECTPROPERTY ( sys1 . id , 'ExecIsInsteadOfTrigger' ) = 1 THEN 'INSTEAD OF' ELSE 'AFTER' END [ Timing ]
FROM sysobjects sys1
JOIN sysobjects sys2 ON sys1 . parent_obj = sys2 . id
2010-10-13 16:04:40 +00:00
WHERE sys1 . xtype = 'TR' AND sys2 . name = " . q( $table )
2010-10-13 15:53:59 +00:00
) as $row ) { // triggers are not schema-scoped
2010-04-22 14:38:05 +00:00
$return [ $row [ " name " ]] = array ( $row [ " Timing " ], $row [ " Event " ]);
}
return $return ;
}
2010-04-22 15:53:42 +00:00
function trigger_options () {
return array (
" Timing " => array ( " AFTER " , " INSTEAD OF " ),
" Type " => array ( " AS " ),
);
}
2010-05-11 14:45:04 +00:00
function schemas () {
return get_vals ( " SELECT name FROM sys.schemas " );
}
function get_schema () {
global $connection ;
if ( $_GET [ " ns " ] != " " ) {
return $_GET [ " ns " ];
}
return $connection -> result ( " SELECT SCHEMA_NAME() " );
}
function set_schema ( $schema ) {
return true ; // ALTER USER is permanent
}
2010-05-05 11:11:33 +00:00
function use_sql ( $database ) {
return " USE " . idf_escape ( $database );
}
function show_variables () {
return array ();
}
function show_status () {
return array ();
}
2012-09-09 05:56:34 +00:00
function convert_field ( $field ) {
}
function unconvert_field ( $field , $return ) {
return $return ;
}
2010-04-21 12:01:32 +00:00
function support ( $feature ) {
2010-05-27 11:25:04 +00:00
return ereg ( '^(scheme|trigger|view|drop_col)$' , $feature ); //! routine|
2010-04-21 12:01:32 +00:00
}
2010-05-07 13:44:22 +00:00
$jush = " mssql " ;
2010-04-21 12:01:32 +00:00
$types = array ();
$structured_types = array ();
2010-05-11 15:41:42 +00:00
foreach ( array ( //! use sys.types
2010-04-21 12:01:32 +00:00
lang ( 'Numbers' ) => array ( " tinyint " => 3 , " smallint " => 5 , " int " => 10 , " bigint " => 20 , " bit " => 1 , " decimal " => 0 , " real " => 12 , " float " => 53 , " smallmoney " => 10 , " money " => 20 ),
lang ( 'Date and time' ) => array ( " date " => 10 , " smalldatetime " => 19 , " datetime " => 19 , " datetime2 " => 19 , " time " => 8 , " datetimeoffset " => 10 ),
lang ( 'Strings' ) => array ( " char " => 8000 , " varchar " => 8000 , " text " => 2147483647 , " nchar " => 4000 , " nvarchar " => 4000 , " ntext " => 1073741823 ),
lang ( 'Binary' ) => array ( " binary " => 8000 , " varbinary " => 8000 , " image " => 2147483647 ),
) as $key => $val ) {
$types += $val ;
$structured_types [ $key ] = array_keys ( $val );
}
$unsigned = array ();
2011-08-09 09:32:11 +00:00
$operators = array ( " = " , " < " , " > " , " <= " , " >= " , " != " , " LIKE " , " LIKE %% " , " IN " , " IS NULL " , " NOT LIKE " , " NOT IN " , " IS NOT NULL " );
2010-04-21 12:01:32 +00:00
$functions = array ( " len " , " lower " , " round " , " upper " );
$grouping = array ( " avg " , " count " , " count distinct " , " max " , " min " , " sum " );
$edit_functions = array (
array (
" date|time " => " getdate " ,
), array (
" int|decimal|real|float|money|datetime " => " +/- " ,
" char|text " => " + " ,
)
);
}