Passed
Push — master ( 2a5ce0...65bdac )
by Alxarafe
23:02
created
dolibarr/htdocs/core/db/mssql.class.php 3 patches
Indentation   +962 added lines, -962 removed lines patch added patch discarded remove patch
@@ -38,101 +38,101 @@  discard block
 block discarded – undo
38 38
  */
39 39
 class DoliDBMssql extends DoliDB
40 40
 {
41
-	//! Database type
42
-	public $type='mssql';
43
-	//! Database label
44
-	const LABEL='MSSQL';
45
-	//! Charset used to force charset when creating database
46
-	var $forcecharset='latin1';      // Can't be static as it may be forced with a dynamic value
47
-	//! Collate used to force collate when creating database
48
-	var $forcecollate='latin1_swedish_ci';      // Can't be static as it may be forced with a dynamic value
49
-	//! Version min database
50
-	const VERSIONMIN='2000';
51
-	/** @var resource Resultset of last query */
52
-	private $_results;
41
+    //! Database type
42
+    public $type='mssql';
43
+    //! Database label
44
+    const LABEL='MSSQL';
45
+    //! Charset used to force charset when creating database
46
+    var $forcecharset='latin1';      // Can't be static as it may be forced with a dynamic value
47
+    //! Collate used to force collate when creating database
48
+    var $forcecollate='latin1_swedish_ci';      // Can't be static as it may be forced with a dynamic value
49
+    //! Version min database
50
+    const VERSIONMIN='2000';
51
+    /** @var resource Resultset of last query */
52
+    private $_results;
53 53
 
54 54
     /**
55
-	 *	Constructor.
56
-	 *	This create an opened connexion to a database server and eventually to a database
57
-	 *
58
-	 *	@param      string	$type		Type of database (mysql, pgsql...)
59
-	 *	@param	    string	$host		Address of database server
60
-	 *	@param	    string	$user		Nom de l'utilisateur autorise
61
-	 *	@param	    string	$pass		Mot de passe
62
-	 *	@param	    string	$name		Nom de la database
63
-	 *	@param	    int		$port		Port of database server
55
+     *	Constructor.
56
+     *	This create an opened connexion to a database server and eventually to a database
57
+     *
58
+     *	@param      string	$type		Type of database (mysql, pgsql...)
59
+     *	@param	    string	$host		Address of database server
60
+     *	@param	    string	$user		Nom de l'utilisateur autorise
61
+     *	@param	    string	$pass		Mot de passe
62
+     *	@param	    string	$name		Nom de la database
63
+     *	@param	    int		$port		Port of database server
64 64
      */
65
-	function __construct($type, $host, $user, $pass, $name='', $port=0)
66
-	{
67
-		global $langs;
65
+    function __construct($type, $host, $user, $pass, $name='', $port=0)
66
+    {
67
+        global $langs;
68 68
 
69
-		$this->database_user=$user;
69
+        $this->database_user=$user;
70 70
         $this->database_host=$host;
71 71
         $this->database_port=$port;
72
-		$this->transaction_opened=0;
73
-
74
-		if (! function_exists("mssql_connect"))
75
-		{
76
-			$this->connected = false;
77
-			$this->ok = false;
78
-			$this->error="Mssql PHP functions for using MSSql driver are not available in this version of PHP";
79
-			dol_syslog(get_class($this)."::DoliDBMssql : MSsql PHP functions for using MSsql driver are not available in this version of PHP",LOG_ERR);
80
-			return $this->ok;
81
-		}
82
-
83
-		if (! $host)
84
-		{
85
-			$this->connected = false;
86
-			$this->ok = false;
87
-			$this->error=$langs->trans("ErrorWrongHostParameter");
88
-			dol_syslog(get_class($this)."::DoliDBMssql : Erreur Connect, wrong host parameters",LOG_ERR);
89
-			return $this->ok;
90
-		}
91
-
92
-		// Essai connexion serveur
93
-		$this->db = $this->connect($host, $user, $pass, $name, $port);
94
-		if ($this->db)
95
-		{
96
-			// Si client connecte avec charset different de celui de la base Dolibarr
97
-			// (La base Dolibarr a ete forcee en this->forcecharset a l'install)
98
-			$this->connected = true;
99
-			$this->ok = true;
100
-		}
101
-		else
102
-		{
103
-			// host, login ou password incorrect
104
-			$this->connected = false;
105
-			$this->ok = false;
106
-			$this->error=mssql_get_last_message();
107
-			dol_syslog(get_class($this)."::DoliDBMssql : Erreur Connect mssql_get_last_message=".$this->error,LOG_ERR);
108
-		}
109
-
110
-		// Si connexion serveur ok et si connexion base demandee, on essaie connexion base
111
-		if ($this->connected && $name)
112
-		{
113
-			if ($this->select_db($name))
114
-			{
115
-				$this->database_selected = true;
116
-				$this->database_name = $name;
117
-				$this->ok = true;
118
-			}
119
-			else
120
-			{
121
-				$this->database_selected = false;
122
-				$this->database_name = '';
123
-				$this->ok = false;
124
-				$this->error=$this->error();
125
-				dol_syslog(get_class($this)."::DoliDBMssql : Erreur Select_db ".$this->error,LOG_ERR);
126
-			}
127
-		}
128
-		else
129
-		{
130
-			// Pas de selection de base demandee, ok ou ko
131
-			$this->database_selected = false;
132
-		}
133
-
134
-		return $this->ok;
135
-	}
72
+        $this->transaction_opened=0;
73
+
74
+        if (! function_exists("mssql_connect"))
75
+        {
76
+            $this->connected = false;
77
+            $this->ok = false;
78
+            $this->error="Mssql PHP functions for using MSSql driver are not available in this version of PHP";
79
+            dol_syslog(get_class($this)."::DoliDBMssql : MSsql PHP functions for using MSsql driver are not available in this version of PHP",LOG_ERR);
80
+            return $this->ok;
81
+        }
82
+
83
+        if (! $host)
84
+        {
85
+            $this->connected = false;
86
+            $this->ok = false;
87
+            $this->error=$langs->trans("ErrorWrongHostParameter");
88
+            dol_syslog(get_class($this)."::DoliDBMssql : Erreur Connect, wrong host parameters",LOG_ERR);
89
+            return $this->ok;
90
+        }
91
+
92
+        // Essai connexion serveur
93
+        $this->db = $this->connect($host, $user, $pass, $name, $port);
94
+        if ($this->db)
95
+        {
96
+            // Si client connecte avec charset different de celui de la base Dolibarr
97
+            // (La base Dolibarr a ete forcee en this->forcecharset a l'install)
98
+            $this->connected = true;
99
+            $this->ok = true;
100
+        }
101
+        else
102
+        {
103
+            // host, login ou password incorrect
104
+            $this->connected = false;
105
+            $this->ok = false;
106
+            $this->error=mssql_get_last_message();
107
+            dol_syslog(get_class($this)."::DoliDBMssql : Erreur Connect mssql_get_last_message=".$this->error,LOG_ERR);
108
+        }
109
+
110
+        // Si connexion serveur ok et si connexion base demandee, on essaie connexion base
111
+        if ($this->connected && $name)
112
+        {
113
+            if ($this->select_db($name))
114
+            {
115
+                $this->database_selected = true;
116
+                $this->database_name = $name;
117
+                $this->ok = true;
118
+            }
119
+            else
120
+            {
121
+                $this->database_selected = false;
122
+                $this->database_name = '';
123
+                $this->ok = false;
124
+                $this->error=$this->error();
125
+                dol_syslog(get_class($this)."::DoliDBMssql : Erreur Select_db ".$this->error,LOG_ERR);
126
+            }
127
+        }
128
+        else
129
+        {
130
+            // Pas de selection de base demandee, ok ou ko
131
+            $this->database_selected = false;
132
+        }
133
+
134
+        return $this->ok;
135
+    }
136 136
 
137 137
     /**
138 138
      *  Convert a SQL request in Mysql syntax to native syntax
@@ -141,83 +141,83 @@  discard block
 block discarded – undo
141 141
      *  @param     string	$type	Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
142 142
      *  @return    string   		SQL request line converted
143 143
      */
144
-	static function convertSQLFromMysql($line,$type='ddl')
145
-	{
146
-		return $line;
147
-	}
144
+    static function convertSQLFromMysql($line,$type='ddl')
145
+    {
146
+        return $line;
147
+    }
148 148
 
149 149
     // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
150
-	/**
151
-	 *	Select a database
152
-	 *
153
-	 *	@param	    string	$database	Name of database
154
-	 *	@return	    boolean  		    true if OK, false if KO
155
-	 */
156
-	function select_db($database)
157
-	{
150
+    /**
151
+     *	Select a database
152
+     *
153
+     *	@param	    string	$database	Name of database
154
+     *	@return	    boolean  		    true if OK, false if KO
155
+     */
156
+    function select_db($database)
157
+    {
158 158
         // phpcs:enable
159
-		return @mssql_select_db($database, $this->db);
160
-	}
161
-
162
-	/**
163
-	 *	Connexion to server
164
-	 *
165
-	 *	@param	    string	$host		database server host
166
-	 *	@param	    string	$login		login
167
-	 *	@param	    string	$passwd		password
168
-	 *	@param		string	$name		name of database (not used for mysql, used for pgsql)
169
-	 *	@param		int		$port		Port of database server
170
-	 *	@return		false|resource|true	Database access handler
171
-	 *	@see		close
172
-	 */
173
-	function connect($host, $login, $passwd, $name, $port=0)
174
-	{
175
-		dol_syslog(get_class($this)."::connect host=$host, port=$port, login=$login, passwd=--hidden--, name=$name");
176
-		$newhost=$host;
177
-		if ($port) $newhost.=':'.$port;
178
-		$this->db  = @mssql_connect($newhost, $login, $passwd);
179
-		//force les enregistrement en latin1 si la base est en utf8 par defaut
180
-		// Supprime car plante sur mon PHP-Mysql. De plus, la base est forcement en latin1 avec
181
-		// les nouvelles version de Dolibarr car force par l'install Dolibarr.
182
-		//$this->query('SET NAMES '.$this->forcecharset);
183
-		//print "Resultat fonction connect: ".$this->db;
184
-		$set_options=array('SET ANSI_PADDING ON;',
185
-		    "SET ANSI_NULLS ON;",
186
-		    "SET ANSI_WARNINGS ON;",
187
-		    "SET ARITHABORT ON;",
188
-		    "SET CONCAT_NULL_YIELDS_NULL ON;",
189
-		    "SET QUOTED_IDENTIFIER ON;"
190
-		);
191
-		mssql_query(implode(' ',$set_options),$this->db);
192
-
193
-		return $this->db;
194
-	}
195
-
196
-	/**
197
-	 *	Return version of database server
198
-	 *
199
-	 *	@return	        string      Version string
200
-	 */
201
-	function getVersion()
202
-	{
203
-		$resql=$this->query("SELECT @@VERSION");
204
-		if ($resql)
205
-		{
159
+        return @mssql_select_db($database, $this->db);
160
+    }
161
+
162
+    /**
163
+     *	Connexion to server
164
+     *
165
+     *	@param	    string	$host		database server host
166
+     *	@param	    string	$login		login
167
+     *	@param	    string	$passwd		password
168
+     *	@param		string	$name		name of database (not used for mysql, used for pgsql)
169
+     *	@param		int		$port		Port of database server
170
+     *	@return		false|resource|true	Database access handler
171
+     *	@see		close
172
+     */
173
+    function connect($host, $login, $passwd, $name, $port=0)
174
+    {
175
+        dol_syslog(get_class($this)."::connect host=$host, port=$port, login=$login, passwd=--hidden--, name=$name");
176
+        $newhost=$host;
177
+        if ($port) $newhost.=':'.$port;
178
+        $this->db  = @mssql_connect($newhost, $login, $passwd);
179
+        //force les enregistrement en latin1 si la base est en utf8 par defaut
180
+        // Supprime car plante sur mon PHP-Mysql. De plus, la base est forcement en latin1 avec
181
+        // les nouvelles version de Dolibarr car force par l'install Dolibarr.
182
+        //$this->query('SET NAMES '.$this->forcecharset);
183
+        //print "Resultat fonction connect: ".$this->db;
184
+        $set_options=array('SET ANSI_PADDING ON;',
185
+            "SET ANSI_NULLS ON;",
186
+            "SET ANSI_WARNINGS ON;",
187
+            "SET ARITHABORT ON;",
188
+            "SET CONCAT_NULL_YIELDS_NULL ON;",
189
+            "SET QUOTED_IDENTIFIER ON;"
190
+        );
191
+        mssql_query(implode(' ',$set_options),$this->db);
192
+
193
+        return $this->db;
194
+    }
195
+
196
+    /**
197
+     *	Return version of database server
198
+     *
199
+     *	@return	        string      Version string
200
+     */
201
+    function getVersion()
202
+    {
203
+        $resql=$this->query("SELECT @@VERSION");
204
+        if ($resql)
205
+        {
206 206
             $version=$this->fetch_array($resql);
207 207
             return $version['computed'];
208
-		}
209
-		else return '';
210
-	}
211
-
212
-	/**
213
-	 *	Return version of database client driver
214
-	 *
215
-	 *	@return	        string      Version string
216
-	 */
217
-	function getDriverInfo()
218
-	{
219
-		return 'php mssql driver';
220
-	}
208
+        }
209
+        else return '';
210
+    }
211
+
212
+    /**
213
+     *	Return version of database client driver
214
+     *
215
+     *	@return	        string      Version string
216
+     */
217
+    function getDriverInfo()
218
+    {
219
+        return 'php mssql driver';
220
+    }
221 221
 
222 222
     /**
223 223
      *  Close database connexion
@@ -229,100 +229,100 @@  discard block
 block discarded – undo
229 229
     {
230 230
         if ($this->db)
231 231
         {
232
-          if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened,LOG_ERR);
233
-          $this->connected=false;
234
-          return mssql_close($this->db);
232
+            if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened,LOG_ERR);
233
+            $this->connected=false;
234
+            return mssql_close($this->db);
235 235
         }
236 236
         return false;
237 237
     }
238 238
 
239 239
 
240
-	/**
241
-	 * Start transaction
242
-	 *
243
-	 * @return	    bool         true if transaction successfuly opened or already opened, false if error
244
-	 */
245
-	function begin()
246
-	{
247
-
248
-	    $res=mssql_query('select @@TRANCOUNT');
249
-	    $this->transaction_opened=mssql_result($res, 0, 0);
250
-
251
-	    if ($this->transaction_opened == 0)
252
-		{
253
-		    //return 1; //There is a mess with auto_commit and 'SET IMPLICIT_TRANSACTIONS ON' generate also a mess
254
-			$ret=mssql_query("SET IMPLICIT_TRANSACTIONS OFF;BEGIN TRANSACTION;",$this->db);
255
-			if ($ret)
256
-			{
257
-				dol_syslog("BEGIN Transaction",LOG_DEBUG);
258
-			}
259
-			return $ret;
260
-		}
261
-		else
262
-		{
263
-			return true;
264
-		}
265
-	}
266
-
267
-	/**
240
+    /**
241
+     * Start transaction
242
+     *
243
+     * @return	    bool         true if transaction successfuly opened or already opened, false if error
244
+     */
245
+    function begin()
246
+    {
247
+
248
+        $res=mssql_query('select @@TRANCOUNT');
249
+        $this->transaction_opened=mssql_result($res, 0, 0);
250
+
251
+        if ($this->transaction_opened == 0)
252
+        {
253
+            //return 1; //There is a mess with auto_commit and 'SET IMPLICIT_TRANSACTIONS ON' generate also a mess
254
+            $ret=mssql_query("SET IMPLICIT_TRANSACTIONS OFF;BEGIN TRANSACTION;",$this->db);
255
+            if ($ret)
256
+            {
257
+                dol_syslog("BEGIN Transaction",LOG_DEBUG);
258
+            }
259
+            return $ret;
260
+        }
261
+        else
262
+        {
263
+            return true;
264
+        }
265
+    }
266
+
267
+    /**
268 268
      * Validate a database transaction
269 269
      *
270 270
      * @param	string	$log        Add more log to default log line
271 271
      * @return  bool         		true if validation is OK or transaction level no started, false if ERROR
272
-	 */
273
-	function commit($log='')
274
-	{
275
-	    $res=mssql_query('select @@TRANCOUNT');
276
-	    $this->transaction_opened=mssql_result($res, 0, 0);
277
-
278
-		if ($this->transaction_opened == 1)
279
-		{
280
-		    //return 1; //There is a mess with auto_commit and 'SET IMPLICIT_TRANSACTION ON' generate also a mess
281
-			$ret=mssql_query("COMMIT TRANSACTION",$this->db);
282
-			if ($ret)
283
-			{
284
-				dol_syslog("COMMIT Transaction",LOG_DEBUG);
285
-				return true;
286
-			}
287
-			else
288
-			{
289
-				return false;
290
-			}
291
-		}
292
-		elseif ($this->transaction_opened > 1)
293
-		{
294
-			return true;
295
-		}
296
-		trigger_error("Commit requested but no transaction remain");
297
-		return false;
298
-	}
299
-
300
-	/**
301
-	 * Annulation d'une transaction et retour aux anciennes valeurs
302
-	 *
303
-	 * @param	string	$log	Add more log to default log line
304
-	 * @return	bool             true si annulation ok ou transaction non ouverte, false en cas d'erreur
305
-	 */
306
-	function rollback($log='')
307
-	{
308
-	    $res=mssql_query('select @@TRANCOUNT');
309
-	    $this->transaction_opened=mssql_result($res, 0, 0);
310
-
311
-		if ($this->transaction_opened == 1)
312
-		{
313
-			$ret=mssql_query("ROLLBACK TRANSACTION",$this->db);
314
-			dol_syslog("ROLLBACK Transaction".($log?' '.$log:''),LOG_DEBUG);
315
-			return $ret;
316
-		}
317
-		elseif ($this->transaction_opened > 1)
318
-		{
319
-			return true;
320
-		}
321
-		trigger_error("Rollback requested but no transaction remain");
322
-		return false;
323
-	}
324
-
325
-	/**
272
+     */
273
+    function commit($log='')
274
+    {
275
+        $res=mssql_query('select @@TRANCOUNT');
276
+        $this->transaction_opened=mssql_result($res, 0, 0);
277
+
278
+        if ($this->transaction_opened == 1)
279
+        {
280
+            //return 1; //There is a mess with auto_commit and 'SET IMPLICIT_TRANSACTION ON' generate also a mess
281
+            $ret=mssql_query("COMMIT TRANSACTION",$this->db);
282
+            if ($ret)
283
+            {
284
+                dol_syslog("COMMIT Transaction",LOG_DEBUG);
285
+                return true;
286
+            }
287
+            else
288
+            {
289
+                return false;
290
+            }
291
+        }
292
+        elseif ($this->transaction_opened > 1)
293
+        {
294
+            return true;
295
+        }
296
+        trigger_error("Commit requested but no transaction remain");
297
+        return false;
298
+    }
299
+
300
+    /**
301
+     * Annulation d'une transaction et retour aux anciennes valeurs
302
+     *
303
+     * @param	string	$log	Add more log to default log line
304
+     * @return	bool             true si annulation ok ou transaction non ouverte, false en cas d'erreur
305
+     */
306
+    function rollback($log='')
307
+    {
308
+        $res=mssql_query('select @@TRANCOUNT');
309
+        $this->transaction_opened=mssql_result($res, 0, 0);
310
+
311
+        if ($this->transaction_opened == 1)
312
+        {
313
+            $ret=mssql_query("ROLLBACK TRANSACTION",$this->db);
314
+            dol_syslog("ROLLBACK Transaction".($log?' '.$log:''),LOG_DEBUG);
315
+            return $ret;
316
+        }
317
+        elseif ($this->transaction_opened > 1)
318
+        {
319
+            return true;
320
+        }
321
+        trigger_error("Rollback requested but no transaction remain");
322
+        return false;
323
+    }
324
+
325
+    /**
326 326
      *  Execute a SQL request and return the resultset
327 327
      *
328 328
      *  @param	string	$query          SQL query string
@@ -330,34 +330,34 @@  discard block
 block discarded – undo
330 330
      *                   		 		Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints.
331 331
      *  @param  string	$type           Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
332 332
      *  @return false|resource|true		Resultset of answer
333
-	 */
334
-	function query($query,$usesavepoint=0,$type='auto')
335
-	{
336
-		$query = trim($query);
333
+     */
334
+    function query($query,$usesavepoint=0,$type='auto')
335
+    {
336
+        $query = trim($query);
337 337
 
338 338
         Debug::addMessage('SQL', 'MSSQL: ' . $query);
339 339
 
340 340
         if (preg_match('/^--/',$query)) return true;
341 341
 
342
-		// Conversion syntaxe MySql vers MSDE.
343
-		$query = str_ireplace("now()", "getdate()", $query);
344
-		// Erreur SQL: cannot update timestamp field
345
-		$query = str_ireplace(", tms = tms", "", $query);
346
-
347
-		$query=preg_replace("/([. ,\t(])(percent|file|public)([. ,=\t)])/","$1[$2]$3",$query);
348
-
349
-		if ($type=="auto" || $type='dml')
350
-		{
351
-    		$query=preg_replace('/AUTO_INCREMENT/i','IDENTITY',$query);
352
-    		$query=preg_replace('/double/i','float',$query);
353
-    		$query=preg_replace('/float\((.*)\)/','numeric($1)',$query);
354
-    		$query=preg_replace('/([ \t])unsigned|IF NOT EXISTS[ \t]/i','$1',$query);
355
-    		$query=preg_replace('/([ \t])(MEDIUM|TINY|LONG){0,1}TEXT([ \t,])/i',"$1VARCHAR(MAX)$3",$query);
356
-
357
-    		$matches=array();
358
-    		$original_query='';
359
-    		if (preg_match('/ALTER TABLE\h+(\w+?)\h+ADD\h+(?:(UNIQUE)|INDEX)\h+(?:INDEX)?\h*(\w+?)\h*\((.+)\)/is', $query,$matches))
360
-    		{
342
+        // Conversion syntaxe MySql vers MSDE.
343
+        $query = str_ireplace("now()", "getdate()", $query);
344
+        // Erreur SQL: cannot update timestamp field
345
+        $query = str_ireplace(", tms = tms", "", $query);
346
+
347
+        $query=preg_replace("/([. ,\t(])(percent|file|public)([. ,=\t)])/","$1[$2]$3",$query);
348
+
349
+        if ($type=="auto" || $type='dml')
350
+        {
351
+            $query=preg_replace('/AUTO_INCREMENT/i','IDENTITY',$query);
352
+            $query=preg_replace('/double/i','float',$query);
353
+            $query=preg_replace('/float\((.*)\)/','numeric($1)',$query);
354
+            $query=preg_replace('/([ \t])unsigned|IF NOT EXISTS[ \t]/i','$1',$query);
355
+            $query=preg_replace('/([ \t])(MEDIUM|TINY|LONG){0,1}TEXT([ \t,])/i',"$1VARCHAR(MAX)$3",$query);
356
+
357
+            $matches=array();
358
+            $original_query='';
359
+            if (preg_match('/ALTER TABLE\h+(\w+?)\h+ADD\h+(?:(UNIQUE)|INDEX)\h+(?:INDEX)?\h*(\w+?)\h*\((.+)\)/is', $query,$matches))
360
+            {
361 361
                 $original_query=$query;
362 362
                 $query="CREATE ".trim($matches[2])." INDEX [".trim($matches[3])."] ON [".trim($matches[1])."] (".trim($matches[4]).")";
363 363
                 if ($matches[2]) {
@@ -374,658 +374,658 @@  discard block
 block discarded – undo
374 374
                     if (! empty($query_comp))
375 375
                         $query.=" WHERE ".implode(" AND ",$query_comp);
376 376
                 }
377
-    		}
378
-    		else
379
-    		{
380
-    		    if (preg_match('/ALTER TABLE\h+(\w+?)\h+ADD\h+PRIMARY\h+KEY\h+(\w+?)\h*\((.+)\)/is', $query, $matches))
381
-    		    {
377
+            }
378
+            else
379
+            {
380
+                if (preg_match('/ALTER TABLE\h+(\w+?)\h+ADD\h+PRIMARY\h+KEY\h+(\w+?)\h*\((.+)\)/is', $query, $matches))
381
+                {
382 382
                     $original_query=$query;
383 383
                     $query="ALTER TABLE [".$matches[1]."] ADD CONSTRAINT [".$matches[2]."] PRIMARY KEY CLUSTERED (".$matches[3].")";
384
-    		    }
385
-    		}
386
-		}
387
-
388
-		if ($type=="auto" || $type='ddl')
389
-		{
390
-    		$itemfound = stripos($query, " limit ");
391
-    		if ($itemfound !== false) {
392
-    			// Extraire le nombre limite
393
-    			$number = stristr($query, " limit ");
394
-    			$number = substr($number, 7);
395
-    			// Inserer l'instruction TOP et le nombre limite
396
-    			$query = str_ireplace("select ", "select top ".$number." ", $query);
397
-    			// Supprimer l'instruction MySql
398
-    			$query = str_ireplace(" limit ".$number, "", $query);
399
-    		}
400
-
401
-    		$itemfound = stripos($query, " week(");
402
-    		if ($itemfound !== false) {
403
-    			// Recreer une requete sans instruction Mysql
404
-    			$positionMySql = stripos($query, " week(");
405
-    			$newquery = substr($query, 0, $positionMySql);
406
-
407
-    			// Recuperer la date passee en parametre
408
-    			$extractvalue = stristr($query, " week(");
409
-    			$extractvalue = substr($extractvalue, 6);
410
-    			$positionMySql = stripos($extractvalue, ")");
411
-    			// Conserver la fin de la requete
412
-    			$endofquery = substr($extractvalue, $positionMySql);
413
-    			$extractvalue = substr($extractvalue, 0, $positionMySql);
414
-
415
-    			// Remplacer l'instruction MySql en Sql Server
416
-    			// Inserer la date en parametre et le reste de la requete
417
-    			$query = $newquery." DATEPART(week, ".$extractvalue.$endofquery;
418
-    		}
419
-    	   if (preg_match('/^insert\h+(?:INTO)?\h*(\w+?)\h*\(.*\b(?:row)?id\b.*\)\h+VALUES/i',$query,$matches))
420
-    	   {
421
-    	       //var_dump($query);
422
-    	       //var_dump($matches);
423
-    	       //if (stripos($query,'llx_c_departements') !== false) var_dump($query);
424
-    	       $sql='SET IDENTITY_INSERT ['.trim($matches[1]).'] ON;';
425
-    	       @mssql_query($sql, $this->db);
426
-    	       $post_query='SET IDENTITY_INSERT ['.trim($matches[1]).'] OFF;';
427
-    	   }
428
-		}
429
-		//print "<!--".$query."-->";
430
-
431
-		if (! in_array($query,array('BEGIN','COMMIT','ROLLBACK'))) dol_syslog('sql='.$query, LOG_DEBUG);
432
-
433
-		if (! $this->database_name)
434
-		{
435
-			// Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE)
436
-			$ret = mssql_query($query, $this->db);
437
-		}
438
-		else
439
-		{
440
-			$ret = mssql_query($query, $this->db);
441
-		}
442
-
443
-		if (!empty($post_query))
444
-		{
445
-		    @mssql_query($post_query, $this->db);
446
-		}
447
-
448
-		if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query))
449
-		{
450
-			// Si requete utilisateur, on la sauvegarde ainsi que son resultset
451
-			if (! $ret)
452
-			{
453
-				$result = mssql_query("SELECT @@ERROR as code", $this->db);
454
-				$row = mssql_fetch_array($result);
384
+                }
385
+            }
386
+        }
387
+
388
+        if ($type=="auto" || $type='ddl')
389
+        {
390
+            $itemfound = stripos($query, " limit ");
391
+            if ($itemfound !== false) {
392
+                // Extraire le nombre limite
393
+                $number = stristr($query, " limit ");
394
+                $number = substr($number, 7);
395
+                // Inserer l'instruction TOP et le nombre limite
396
+                $query = str_ireplace("select ", "select top ".$number." ", $query);
397
+                // Supprimer l'instruction MySql
398
+                $query = str_ireplace(" limit ".$number, "", $query);
399
+            }
400
+
401
+            $itemfound = stripos($query, " week(");
402
+            if ($itemfound !== false) {
403
+                // Recreer une requete sans instruction Mysql
404
+                $positionMySql = stripos($query, " week(");
405
+                $newquery = substr($query, 0, $positionMySql);
406
+
407
+                // Recuperer la date passee en parametre
408
+                $extractvalue = stristr($query, " week(");
409
+                $extractvalue = substr($extractvalue, 6);
410
+                $positionMySql = stripos($extractvalue, ")");
411
+                // Conserver la fin de la requete
412
+                $endofquery = substr($extractvalue, $positionMySql);
413
+                $extractvalue = substr($extractvalue, 0, $positionMySql);
414
+
415
+                // Remplacer l'instruction MySql en Sql Server
416
+                // Inserer la date en parametre et le reste de la requete
417
+                $query = $newquery." DATEPART(week, ".$extractvalue.$endofquery;
418
+            }
419
+            if (preg_match('/^insert\h+(?:INTO)?\h*(\w+?)\h*\(.*\b(?:row)?id\b.*\)\h+VALUES/i',$query,$matches))
420
+            {
421
+                //var_dump($query);
422
+                //var_dump($matches);
423
+                //if (stripos($query,'llx_c_departements') !== false) var_dump($query);
424
+                $sql='SET IDENTITY_INSERT ['.trim($matches[1]).'] ON;';
425
+                @mssql_query($sql, $this->db);
426
+                $post_query='SET IDENTITY_INSERT ['.trim($matches[1]).'] OFF;';
427
+            }
428
+        }
429
+        //print "<!--".$query."-->";
430
+
431
+        if (! in_array($query,array('BEGIN','COMMIT','ROLLBACK'))) dol_syslog('sql='.$query, LOG_DEBUG);
432
+
433
+        if (! $this->database_name)
434
+        {
435
+            // Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE)
436
+            $ret = mssql_query($query, $this->db);
437
+        }
438
+        else
439
+        {
440
+            $ret = mssql_query($query, $this->db);
441
+        }
442
+
443
+        if (!empty($post_query))
444
+        {
445
+            @mssql_query($post_query, $this->db);
446
+        }
447
+
448
+        if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query))
449
+        {
450
+            // Si requete utilisateur, on la sauvegarde ainsi que son resultset
451
+            if (! $ret)
452
+            {
453
+                $result = mssql_query("SELECT @@ERROR as code", $this->db);
454
+                $row = mssql_fetch_array($result);
455 455
 
456 456
                 $this->lastqueryerror = $query;
457
-				$this->lasterror = $this->error();
458
-				$this->lasterrno = $row["code"];
457
+                $this->lasterror = $this->error();
458
+                $this->lasterrno = $row["code"];
459 459
 
460
-				dol_syslog(get_class($this)."::query SQL Error query: ".$query, LOG_ERR);
461
-				if ($original_query) dol_syslog(get_class($this)."::query SQL Original query: ".$original_query, LOG_ERR);
462
-				dol_syslog(get_class($this)."::query SQL Error message: ".$this->lasterror." (".$this->lasterrno.")", LOG_ERR);
463
-			}
464
-			$this->lastquery=$query;
465
-			$this->_results = $ret;
466
-		}
460
+                dol_syslog(get_class($this)."::query SQL Error query: ".$query, LOG_ERR);
461
+                if ($original_query) dol_syslog(get_class($this)."::query SQL Original query: ".$original_query, LOG_ERR);
462
+                dol_syslog(get_class($this)."::query SQL Error message: ".$this->lasterror." (".$this->lasterrno.")", LOG_ERR);
463
+            }
464
+            $this->lastquery=$query;
465
+            $this->_results = $ret;
466
+        }
467 467
 
468
-		return $ret;
469
-	}
468
+        return $ret;
469
+    }
470 470
 
471 471
     // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
472
-	/**
473
-	 *	Renvoie la ligne courante (comme un objet) pour le curseur resultset
474
-	 *
475
-	 *	@param	resource	$resultset  Curseur de la requete voulue
476
-	 *	@return	object|false			Object result line or false if KO or end of cursor
477
-	 */
478
-	function fetch_object($resultset)
479
-	{
472
+    /**
473
+     *	Renvoie la ligne courante (comme un objet) pour le curseur resultset
474
+     *
475
+     *	@param	resource	$resultset  Curseur de la requete voulue
476
+     *	@return	object|false			Object result line or false if KO or end of cursor
477
+     */
478
+    function fetch_object($resultset)
479
+    {
480 480
         // phpcs:enable
481
-		// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
482
-		if (! is_resource($resultset)) { $resultset=$this->_results; }
483
-		return mssql_fetch_object($resultset);
484
-	}
481
+        // Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
482
+        if (! is_resource($resultset)) { $resultset=$this->_results; }
483
+        return mssql_fetch_object($resultset);
484
+    }
485 485
 
486 486
     // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
487
-	/**
487
+    /**
488 488
      *	Return datas as an array
489 489
      *
490 490
      *	@param	resource	$resultset  Resultset of request
491 491
      *	@return	array|false				Array or false if KO or end of cursor
492
-	 */
493
-	function fetch_array($resultset)
494
-	{
492
+     */
493
+    function fetch_array($resultset)
494
+    {
495 495
         // phpcs:enable
496
-		// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
497
-		if (! is_resource($resultset)) { $resultset=$this->_results; }
498
-		return mssql_fetch_array($resultset);
499
-	}
496
+        // Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
497
+        if (! is_resource($resultset)) { $resultset=$this->_results; }
498
+        return mssql_fetch_array($resultset);
499
+    }
500 500
 
501 501
 
502 502
     // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
503
-	/**
503
+    /**
504 504
      *	Return datas as an array
505 505
      *
506 506
      *	@param	resource	$resultset  Resultset of request
507 507
      *	@return	array|false				Array or false if KO or end of cursor
508
-	 */
509
-	function fetch_row($resultset)
510
-	{
508
+     */
509
+    function fetch_row($resultset)
510
+    {
511 511
         // phpcs:enable
512
-		// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
513
-		if (! is_resource($resultset)) { $resultset=$this->_results; }
514
-		return @mssql_fetch_row($resultset);
515
-	}
512
+        // Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
513
+        if (! is_resource($resultset)) { $resultset=$this->_results; }
514
+        return @mssql_fetch_row($resultset);
515
+    }
516 516
 
517 517
     // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
518
-	/**
518
+    /**
519 519
      *	Return number of lines for result of a SELECT
520 520
      *
521 521
      *	@param	resource	$resultset  Resulset of requests
522 522
      *	@return int		    			Nb of lines
523 523
      *	@see    affected_rows
524
-	 */
525
-	function num_rows($resultset)
526
-	{
524
+     */
525
+    function num_rows($resultset)
526
+    {
527 527
         // phpcs:enable
528
-		// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
529
-		if (! is_resource($resultset)) { $resultset=$this->_results; }
530
-		return mssql_num_rows($resultset);
531
-	}
528
+        // Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
529
+        if (! is_resource($resultset)) { $resultset=$this->_results; }
530
+        return mssql_num_rows($resultset);
531
+    }
532 532
 
533 533
     // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
534
-	/**
535
-	 *	Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE
536
-	 *
537
-	 *	@param	resource	$resultset   Curseur de la requete voulue
538
-	 *	@return int		    Nombre de lignes
539
-	 *	@see    num_rows
540
-	 */
541
-	function affected_rows($resultset)
542
-	{
534
+    /**
535
+     *	Renvoie le nombre de lignes dans le resultat d'une requete INSERT, DELETE ou UPDATE
536
+     *
537
+     *	@param	resource	$resultset   Curseur de la requete voulue
538
+     *	@return int		    Nombre de lignes
539
+     *	@see    num_rows
540
+     */
541
+    function affected_rows($resultset)
542
+    {
543 543
         // phpcs:enable
544
-		// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
545
-		if (! is_resource($resultset)) { $resultset=$this->_results; }
546
-		// mssql necessite un link de base pour cette fonction contrairement
547
-		// a pqsql qui prend un resultset
548
-		$rsRows = mssql_query("select @@rowcount as rows", $this->db);
549
-		return mssql_result($rsRows, 0, "rows");
550
-		//return mssql_affected_rows($this->db);
551
-	}
552
-
553
-
554
-	/**
555
-	 *	Free last resultset used.
556
-	 *
557
-	 *	@param  resource	$resultset   Curseur de la requete voulue
558
-	 *	@return	bool
559
-	 */
560
-	function free($resultset=null)
561
-	{
562
-		// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
563
-		if (! is_resource($resultset)) { $resultset=$this->_results; }
564
-		// Si resultset en est un, on libere la memoire
565
-		if (is_resource($resultset)) mssql_free_result($resultset);
566
-	}
567
-
568
-	/**
569
-	 *	Escape a string to insert data
570
-	 *
571