24 require_once
CORE_PATH .
'libs/db/adapters/pdo.php';
97 $table = addslashes(
"$table");
98 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM sysobjects WHERE type = 'U' AND name = '$table'");
108 public function limit($sql, $number)
110 if (!is_numeric($number)) {
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));
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';
125 $sql.=
') AS otable';
126 if ($orderby !==
false) {
127 $sql.=
' ORDER BY ' . $order .
' ' . $sort;
142 return $this->
query(
"DROP TABLE $table");
147 return $this->
query(
"DROP TABLE $table");
166 $create_sql =
"CREATE TABLE $table (";
167 if (!is_array($definition)) {
168 new KumbiaException(
"Definición invalida para crear la tabla '$table'");
171 $create_lines = array();
173 $unique_index = array();
177 foreach ($definition as $field => $field_def) {
178 if (isset($field_def[
'not_null'])) {
179 $not_null = $field_def[
'not_null'] ?
'NOT NULL' :
'';
183 if (isset($field_def[
'size'])) {
184 $size = $field_def[
'size'] ?
'(' . $field_def[
'size'] .
')' :
'';
188 if (isset($field_def[
'index'])) {
189 if ($field_def[
'index']) {
190 $index[] =
"INDEX($field)";
193 if (isset($field_def[
'unique_index'])) {
194 if ($field_def[
'unique_index']) {
195 $index[] =
"UNIQUE($field)";
198 if (isset($field_def[
'primary'])) {
199 if ($field_def[
'primary']) {
200 $primary[] =
"$field";
203 if (isset($field_def[
'auto'])) {
204 if ($field_def[
'auto']) {
205 $field_def[
'extra'] = isset($field_def[
'extra']) ? $field_def[
'extra'] .
" IDENTITY" :
"IDENTITY";
208 if (isset($field_def[
'extra'])) {
209 $extra = $field_def[
'extra'];
213 $create_lines[] =
"$field " . $field_def[
'type'] . $size .
' ' . $not_null .
' ' . $extra;
215 $create_sql.= join(
',', $create_lines);
216 $last_lines = array();
217 if (count($primary)) {
218 $last_lines[] =
'PRIMARY KEY(' . join(
",", $primary) .
')';
221 $last_lines[] = join(
',', $index);
223 if (count($unique_index)) {
224 $last_lines[] = join(
',', $unique_index);
226 if (count($last_lines)) {
227 $create_sql.=
',' . join(
',', $last_lines) .
')';
229 return $this->
query($create_sql);
239 return $this->
fetch_all(
"SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name");
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"
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';
265 $final_describe[$i][
'Key'] =
"";
269 return $final_describe;
282 $num = $this->
fetch_one(
"SELECT MAX($primary_key) FROM $table");
283 return (
int) $num[0];