KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
mssql.php
Ir a la documentación de este archivo.
1 <?php
24 require_once CORE_PATH . 'libs/db/adapters/pdo.php';
25 
33 class DbPdoMsSQL extends DbPDO
34 {
35 
39  protected $db_rbdm = "odbc";
40 
45  const TYPE_INTEGER = "INTEGER";
46 
51  const TYPE_DATE = "SMALLDATETIME";
52 
57  const TYPE_VARCHAR = "VARCHAR";
58 
63  const TYPE_DECIMAL = "DECIMAL";
64 
69  const TYPE_DATETIME = "DATETIME";
70 
75  const TYPE_CHAR = "CHAR";
76 
81  public function initialize()
82  {
86  //$this->exec("SET IDENTITY_INSERT ON");
87  }
88 
95  public function table_exists($table, $schema='')
96  {
97  $table = addslashes("$table");
98  $num = $this->fetch_one("SELECT COUNT(*) FROM sysobjects WHERE type = 'U' AND name = '$table'");
99  return $num[0];
100  }
101 
108  public function limit($sql)
109  {
110  $params = Util::getParams(func_get_args());
111 
112  if(!isset($params['offset']) && isset($params['limit'])){
113  return str_ireplace("SELECT ", "SELECT TOP $params[limit] ", $sql);
114  }
115  $orderby = stristr($sql, 'ORDER BY');
116  if ($orderby !== false) {
117  $sort = (stripos($orderby, 'desc') !== false) ? 'desc' : 'asc';
118  $order = str_ireplace('ORDER BY', '', $orderby);
119  $order = trim(preg_replace('/ASC|DESC/i', '', $order));
120  }
121  $sql = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . $params[offset] . ' ', $sql);
122  $sql = 'SELECT * FROM (SELECT TOP ' . $params[limit] . ' * FROM (' . $sql . ') AS itable';
123  if ($orderby !== false) {
124  $sql.= ' ORDER BY ' . $order . ' ';
125  $sql.= ( stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
126  }
127  $sql.= ') AS otable';
128  if ($orderby !== false) {
129  $sql.=' ORDER BY ' . $order . ' ' . $sort;
130  }
131  return $sql;
132  }
133 
140  public function drop_table($table, $if_exists=true)
141  {
142  if ($if_exists) {
143  if ($this->table_exists($table)) {
144  return $this->query("DROP TABLE $table");
145  } else {
146  return true;
147  }
148  } else {
149  return $this->query("DROP TABLE $table");
150  }
151  }
152 
166  public function create_table($table, $definition, $index=array())
167  {
168  $create_sql = "CREATE TABLE $table (";
169  if (!is_array($definition)) {
170  new KumbiaException("Definici&oacute;n invalida para crear la tabla '$table'");
171  return false;
172  }
173  $create_lines = array();
174  $index = array();
175  $unique_index = array();
176  $primary = array();
177  //$not_null = "";
178  //$size = "";
179  foreach ($definition as $field => $field_def) {
180  if (isset($field_def['not_null'])) {
181  $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
182  } else {
183  $not_null = "";
184  }
185  if (isset($field_def['size'])) {
186  $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : '';
187  } else {
188  $size = "";
189  }
190  if (isset($field_def['index'])) {
191  if ($field_def['index']) {
192  $index[] = "INDEX($field)";
193  }
194  }
195  if (isset($field_def['unique_index'])) {
196  if ($field_def['unique_index']) {
197  $index[] = "UNIQUE($field)";
198  }
199  }
200  if (isset($field_def['primary'])) {
201  if ($field_def['primary']) {
202  $primary[] = "$field";
203  }
204  }
205  if (isset($field_def['auto'])) {
206  if ($field_def['auto']) {
207  $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra'] . " IDENTITY" : "IDENTITY";
208  }
209  }
210  if (isset($field_def['extra'])) {
211  $extra = $field_def['extra'];
212  } else {
213  $extra = "";
214  }
215  $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra;
216  }
217  $create_sql.= join(',', $create_lines);
218  $last_lines = array();
219  if (count($primary)) {
220  $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')';
221  }
222  if (count($index)) {
223  $last_lines[] = join(',', $index);
224  }
225  if (count($unique_index)) {
226  $last_lines[] = join(',', $unique_index);
227  }
228  if (count($last_lines)) {
229  $create_sql.= ',' . join(',', $last_lines) . ')';
230  }
231  return $this->query($create_sql);
232  }
233 
239  public function list_tables()
240  {
241  return $this->fetch_all("SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name");
242  }
243 
250  public function describe_table($table, $schema='')
251  {
252  $describe_table = $this->fetch_all("exec sp_columns @table_name = '$table'");
253  $final_describe = array();
254  foreach ($describe_table as $field) {
255  $final_describe[] = array(
256  "Field" => $field["COLUMN_NAME"],
257  "Type" => $field['LENGTH'] ? $field["TYPE_NAME"] : $field["TYPE_NAME"] . "(" . $field['LENGTH'] . ")",
258  "Null" => $field['NULLABLE'] == 1 ? "YES" : "NO"
259  );
260  }
261  $describe_keys = $this->fetch_all("exec sp_pkeys @table_name = '$table'");
262  foreach ($describe_keys as $field) {
263  for ($i = 0; $i <= count($final_describe) - 1; $i++) {
264  if ($final_describe[$i]['Field'] == $field['COLUMN_NAME']) {
265  $final_describe[$i]['Key'] = 'PRI';
266  } else {
267  $final_describe[$i]['Key'] = "";
268  }
269  }
270  }
271  return $final_describe;
272  }
273 
279  public function last_insert_id($table='', $primary_key='')
280  {
284  $num = $this->fetch_one("SELECT MAX($primary_key) FROM $table");
285  return (int) $num[0];
286  }
287 
288 }