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