summaryrefslogtreecommitdiffstats
path: root/backend/php-cubrid/cubrid_mysql_compat.php
blob: 3ae2a8eba7e90681f7166b27ef6ea9588719e0d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/*
* Description: A Cubrid implementation of the equivalent MySQL PHP API functions.
*
* Copyright (c) 2010 Cubrid.
* www.cubrid.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Ver 1.2.1
* Last update: November 2010
* 
*/

// Global variables
$calculate_insert_id;   // flag to indicate if calculate the last_insert_id

$cubrid_last_insert_id; // keeps the last_insert_id value
$cubrid_default_port;   // default port used to coonect to Cubrid, if not explicitely secified
$cubrid_database;       // keeps the current database name

// MySQL syntax:
// resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )  
function cubrid_mysql_connect($server, $username, $password, $database) {
  // Note: In Cubrid, the database name is mandatory
  // initialize globals
  $GLOBALS['calculate_last_insert_id'] = true;
  $GLOBALS['cubrid_last_insert_id']    = 0;
  $GLOBALS['cubrid_default_port']      = 30000; // or use 33000
  $GLOBALS['cubrid_database']          = "";

  if (strpos($server, ":") > 0) {
    list($host, $port)=explode(":", $server, 2);
  } else {
    $host=$server;
    $port=$GLOBALS['cubrid_default_port']; // default port
  }

  // save database name
  $GLOBALS['cubrid_database']=$database;

  return cubrid_connect($host, $port, $database, $username, $password);
}

// MySQL syntax:
// bool mysql_close ( [resource link_identifier] )
function cubrid_mysql_close($link_identifier) {
  return cubrid_disconnect($link_identifier);
}

// MySQL syntax:
function cubrid_mysql_errno($link_identifier = '') {
  // int mysql_errno ( [resource link_identifier] )
  return cubrid_error_code();
}

// MySQL syntax:
function cubrid_mysql_error($link_identifier = '') {
  // string mysql_error ( [resource link_identifier] )
  return cubrid_error_msg();
}

// MySQL syntax:
// resource mysql_query ( string query [, resource link_identifier] )
function cubrid_mysql_query_native($query, $link_identifier) {
  $result=cubrid_execute($link_identifier, $query);
  // Cubrid does not do commit automatically by default (auto-commit is off by default)
  cubrid_commit ($link_identifier);
  return $result;
}

// MySQL syntax:
// resource mysql_query ( string query [, resource link_identifier] )
function cubrid_mysql_query($query, $link_identifier, $last_insert_id_column = '') {
  $cubrid_query=trim($query); // need to trim to make sure we are handling properly the ";" terminator

  if ($GLOBALS['calculate_last_insert_id']) {
    // HACK - Cubrid does not support yet INSERT_ID()
    // We need to alter the INSERT query to be able to get last_insert_id
    if (startsWith($cubrid_query, "insert", false)) {
      if ($cubrid_query[strlen($cubrid_query) - 1] == ";") { // remove the ";" last char
        $cubrid_query=substr($cubrid_query, 0, strlen($cubrid_query) - 1) . " INTO :xyz;";
      } else {
        $cubrid_query=$cubrid_query . " INTO :xyz;";
      }
    }
  }

  $result=cubrid_execute($link_identifier, $cubrid_query);

  if (!$result) {
    return false;
  }

  if ($GLOBALS['calculate_last_insert_id']) {
    if (startsWith($cubrid_query, "insert", false)) {
      // get the last_insert_id for the specified column (In Cubrid can be multiple auto_increment columns)
      if (strlen($last_insert_id_column) > 0) {
        $GLOBALS['cubrid_last_insert_id']=cubrid_get_value(cubrid_last_insert_id_query($last_insert_id_column),
                                                           $link_identifier);
      }
    } else {
      $GLOBALS['cubrid_last_insert_id']=0; // (re)set to 0
    }
  }
  cubrid_commit ($link_identifier);
  return $result;
}

// MySQL syntax:
// array mysql_free_result ( resource $link_identifier )
function cubrid_mysql_free_result($link_identifier) {
  return cubrid_free_result($link_identifier);
}

// MySQL syntax:
// array mysql_fetch_row ( resource result )
function cubrid_mysql_fetch_row($result) {
  return cubrid_fetch_row($result);
}

// MySQL syntax:
// array mysql_fetch_array ( resource result [, int result_type] )
function cubrid_mysql_fetch_array($result, $result_type = 0) {
  return cubrid_fetch($result, $result_type);
}

// MySQL syntax:
// object mysql_fetch_object ( resource result )
function cubrid_mysql_fetch_object($result) {
  return cubrid_fetch_object($result);
}

// MySQL syntax:
// bool mysql_data_seek ( resource result, int row_number )
function cubrid_mysql_move_cursor($result, $offset) {
  return cubrid_move_cursor($result, $offset);
}

// MySQL syntax:
// object mysql_fetch_field ( resource result [, int field_offset] )
function cubrid_mysql_fetch_field($result) {
  return cubrid_fetch_field($result);
}

// MySQL syntax:
// int mysql_insert_id ( [resource link_identifier] )
function cubrid_mysql_insert_id($link_identifier = -1) {
  return $GLOBALS['cubrid_last_insert_id'];
}

// MySQL syntax:
// int mysql_num_rows ( resource result )
function cubrid_mysql_num_rows($resultSet) {
  return cubrid_num_rows($resultSet);
}

// MySQL syntax:
// int mysql_num_fields ( resource result )
function cubrid_mysql_num_fields($resultSet) {
  return cubrid_num_cols($resultSet);
}

// MySQL syntax:
// bool mysql_data_seek ( resource result, int row_number )
function cubrid_mysql_data_seek($result, $row_number) {
  return cubrid_data_seek($result, $row_number);
}

// MySQL syntax:
// Zero if successful. Non-zero if an error occurred. 
function cubrid_mysql_commit($link_identifier) {
  return cubrid_commit($link_identifier);
}

// MySQL syntax:
// Zero if successful. Non-zero if an error occurred. 
function cubrid_mysql_rollback($link_identifier) {
  return cubrid_rollback($link_identifier);
}

// MySQL syntax:
// string mysql_real_escape_string ( string unescaped_string [, resource link_identifier] )
function cubrid_mysql_real_escape_string($unescaped_string, $link_identifier = '') {
  return str_replace("'", "''", $unescaped_string);
}

// MySQL syntax:
// string mysql_result  (  resource $result  ,  int $row  [,  mixed $field = 0  ] )
function cubrid_mysql_result($result, $row, $field = 0) {
  return cubrid_result($result, $row, $field);
}

// MySQL syntax:
// array mysql_fetch_assoc  (  resource $result  )
function cubrid_mysql_fetch_assoc($result) {
  return cubrid_fetch_assoc($result);
}

// implements SHOW TABLES
function cubrid_SHOW_TABLES($link_identifier) {
  return cubrid_mysql_query_native(get_SQL_SHOW_TABLES(), $link_identifier);
}

// implements DESCRIBE
function cubrid_DESCRIBE($table, $link_identifier) {
  return cubrid_mysql_query_native(get_SQL_describeTableSQL($table), $link_identifier);
}

// implements SHOW INDEX
function cubrid_SHOW_INDEX_FROM($table, $link_identifier) {
  return cubrid_mysql_query_native(get_SQL_SHOW_INDEX_FROM($table), $link_identifier);
}

// gets data type for a table
function cubrid_ColumnDataType($identifier, $table, $column) {
  $id_result       =cubrid_mysql_query_native(get_SQL_ColumnDataType($table, $column), $identifier);
  $id_row          =cubrid_fetch_row($id_result);
  cubrid_free_result ($id_result);
  $calculated_value=$id_row[0];
  return $calculated_value;
}

// gets a single value as a query result
function cubrid_get_value($query, $link_identifier) {
  $result=cubrid_execute($link_identifier, $query);

  if ($result) {
    $row  =cubrid_fetch_row($result);
    $value=$row[0];
  } else {
    return -1;
  }

  return $value;
}

// returns the last_insert_id value
function cubrid_last_insert_id_query($last_insert_id_column, $cubrid_var = "xyz") {
  return "SELECT :" . $cubrid_var . "." . $last_insert_id_column . " FROM db_root";
}

// returns the last_insert_id value
function cubrid_last_insert_id($last_insert_id_column, $link_identifier, $cubrid_var = "xyz") {
  return cubrid_get_value(cubrid_last_insert_id_query($last_insert_id_column), $link_identifier);
}

// return the current Cubrid database name
function cubrid_current_database() {
  return $GLOBALS['cubrid_database'];
}

// The following columns are returned:
// Field,  Type,  Null (YES,NO),  Key (PRI,MUL),  Default,  Extra
function get_SQL_describeTableSQL($table) {
  $sql="";
  $sql=$sql . "SELECT a.attr_name as \"Field\"," . " ";
  $sql=$sql . "a.data_type || '(' || a.prec || ',' || a.scale || ')' as \"Type\"," . " ";
  $sql=$sql . "a.prec || ',' || a.scale as \"Size\"," . " ";
  $sql=$sql . "a.is_nullable as \"Null\"," . " ";
  $sql=$sql . "'*' as \"Key\"," . " ";
  $sql=$sql . "a.default_value  as \"Default\"," . " ";
  $sql=$sql . "'' as \"Extra\"" . " ";
  $sql=$sql . "FROM db_attribute a" . " ";
  $sql=$sql . "WHERE a.class_name='" . $table . "'" . " ";
  $sql=$sql . "order by a.def_order ASC";

  return $sql;
}

// The column data type is returned
function get_SQL_ColumnDataType($table, $column) {
  $sql="";
  $sql=$sql . "SELECT a.data_type" . " ";
  $sql=$sql . "FROM db_attribute a" . " ";
  $sql=$sql . "WHERE a.class_name='" . $table . "'" . " ";
  $sql=$sql . "AND a.attr_name='" . $column . "'" . " ";
  $sql=$sql . "order by a.def_order ASC";

  return $sql;
}

// The following columns are returned:
// Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation,Cardinality,Sub_part,Packed,Index_type,Comment
function get_SQL_SHOW_INDEX_FROM($table) {
  $query="";
  $query=$query . "select distinct a.class_name as \"Table\"," . " ";
  $query=$query . "DECODE(a.is_unique, 'YES', 'True') as \"Non_unique\"," . " ";
  $query=$query . "a.index_name as \"Key_name\"," . " ";
  $query=$query . "b.key_order as \"Seq_in_index\"," . " ";
  $query=$query . "b.key_attr_name as \"Column_name\"," . " ";
  $query=$query . "'' as \"Collation\"," . " ";
  $query=$query . "'' as \"Cardinality\"," . " ";
  $query=$query . "'' as \"Sub_part\"," . " ";
  $query=$query . "'' as \"Packed\"," . " ";
  $query=$query . "'' as \"Index_type\"," . " ";
  $query=$query . "'' as \"Comment\"" . " ";
  $query=$query . "from \"db_index\" a, \"db_index_key\" b" . " ";
  $query=$query . "where a.index_name = b.index_name" . " ";
  $query=$query . "and b.key_attr_name in (select attr_name from db_attribute where class_name = '" . $table . "')" . " ";
  $query=$query . "and a.is_foreign_key = 'NO'" . " ";
  $query=$query . "and a.class_name = '" . $table . "'" . " ";
  $query=$query . "order by a.index_name, b.key_order,b.key_attr_name";

  return $query;
}

// returns the list of non-system tables
function get_SQL_SHOW_TABLES($pattern = '') {
  if (strlen($pattern) > 0) {
    return "select class_name from db_class where class_type='CLASS' and is_system_class='NO' WHERE class_name LIKE '"
      . $pattern . "' order by class_name ASC";
  } else {
    return "select class_name from db_class where class_type='CLASS' and is_system_class='NO' order by class_name ASC";
  }
}

// gets Primary Keys information
function columnIsPK($dbl, $table, $column) {
  $ret     = false;

  $sql     = "";
  $sql     = $sql . "select db_index_key.key_attr_name" . " ";
  $sql     = $sql . "from db_index, db_index_key" . " ";
  $sql     = $sql . "where db_index.index_name = db_index_key.index_name" . " ";
  $sql     = $sql . "and db_index.class_name='" . $table . "'" . " ";
  $sql     = $sql . "and db_index_key.key_attr_name='" . $column . "'" . " ";
  $sql     = $sql . "and db_index.is_primary_key='YES'";

  $tableSql = cubrid_execute($dbl, $sql);

  if (@cubrid_num_rows($tableSql) > 0) {
    $ret=true;
  }

  return $ret;
}

// emulates DROP TABLE IF EXISTS
function DROP_TABLE_IF_EXISTS($tablename, $link_identifier) {
  $id_result
       =cubrid_mysql_query_native("SELECT class_name FROM db_class WHERE class_name='" . $tablename . "'",
                                  $link_identifier);
  $rows=cubrid_num_rows($id_result);

  if ($rows > 0) {
    cubrid_execute($link_identifier, "DROP TABLE " . $tablename);
  }
}

// The following columns are returned:
// Field,  Type,  Null (YES,NO),  Key (PRI,MUL),  Default,  Extra
function get_SQL_SHOW_COLUMNS($table) {
  $sql="";
  $sql=$sql . "SELECT a.attr_name as \"Field\"," . " ";
  $sql=$sql . "a.data_type || '(' || a.prec || ',' || a.scale || ')' as \"Type\"," . " ";
  $sql=$sql . "a.prec || ',' || a.scale as \"Size\"," . " ";
  $sql=$sql . "a.is_nullable as \"Null\"," . " ";
  $sql=$sql . "'*' as \"Key\"," . " ";
  $sql=$sql . "a.default_value  as \"Default\"," . " ";
  $sql=$sql . "'' as \"Extra\"" . " ";
  $sql=$sql . "FROM db_attribute a" . " ";
  $sql=$sql . "WHERE a.class_name='" . $table . "'" . " ";
  $sql=$sql . "order by a.def_order ASC";

  return $sql;
}

function startsWith($haystack, $needle, $case = true) {
  if ($case) {
    return strpos($haystack, $needle, 0) == 0;
  } else {
    return stripos($haystack, $needle, 0) == 0;
  }
}

function endsWith($haystack, $needle, $case = true) {
  $expectedPosition=strlen($haystack) - strlen($needle);

  if ($case) {
    return strrpos($haystack, $needle, 0) == $expectedPosition;
  } else {
    return strripos($haystack, $needle, 0) == $expectedPosition;
  }
}

?>