24 require_once
CORE_PATH .
'libs/db/adapters/pdo.php';
97 $table = addslashes(
"$table");
98 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM systables WHERE tabname = '$table'");
108 public function limit($sql, $number)
113 $number = (int) $number;
114 return "$sql -- LIMIT $number\n";
127 return $this->
query(
"DROP TABLE $table");
133 return $this->
query(
"DROP TABLE $table");
152 $create_sql =
"CREATE TABLE $table (";
153 if (!is_array($definition)) {
154 new KumbiaException(
"Definición invalida para crear la tabla '$table'");
157 $create_lines = array();
159 $unique_index = array();
163 foreach ($definition as $field => $field_def) {
164 if (isset($field_def[
'not_null'])) {
165 $not_null = $field_def[
'not_null'] ?
'NOT NULL' :
'';
169 if (isset($field_def[
'size'])) {
170 $size = $field_def[
'size'] ?
'(' . $field_def[
'size'] .
')' :
'';
174 if (isset($field_def[
'index'])) {
175 if ($field_def[
'index']) {
176 $index[] =
"INDEX($field)";
179 if (isset($field_def[
'unique_index'])) {
180 if ($field_def[
'unique_index']) {
181 $index[] =
"UNIQUE($field)";
184 if (isset($field_def[
'primary'])) {
185 if ($field_def[
'primary']) {
186 $primary[] =
"$field";
189 if (isset($field_def[
'auto'])) {
190 if ($field_def[
'auto']) {
191 $field_def[
'type'] =
"SERIAL";
194 if (isset($field_def[
'extra'])) {
195 $extra = $field_def[
'extra'];
199 $create_lines[] =
"$field " . $field_def[
'type'] . $size .
' ' . $not_null .
' ' . $extra;
201 $create_sql.= join(
',', $create_lines);
202 $last_lines = array();
203 if (count($primary)) {
204 $last_lines[] =
'PRIMARY KEY(' . join(
",", $primary) .
')';
207 $last_lines[] = join(
',', $index);
209 if (count($unique_index)) {
210 $last_lines[] = join(
',', $unique_index);
212 if (count($last_lines)) {
213 $create_sql.=
',' . join(
',', $last_lines) .
')';
215 return $this->
query($create_sql);
225 return $this->
fetch_all(
"SELECT tabname FROM systables WHERE tabtype = 'T' AND version <> 65537");
242 $describe = $this->
fetch_all(
"SELECT c.colname AS Field, c.coltype AS Type,
243 'YES' AS NULL, c.collength as Length
244 FROM systables t, syscolumns c WHERE
245 c.tabid = t.tabid AND t.tabname = '$table' ORDER BY c.colno");
246 $final_describe = array();
247 foreach ($describe as $field) {
249 if ($field[
'field'] ==
'id') {
250 $field[
"key"] =
'PRI';
251 $field[
"null"] =
'NO';
255 if (substr($field[
'field'], -3) ==
'_id') {
256 $field[
"null"] =
'NO';
258 if ($field[
'type'] == 262) {
259 $field[
'type'] =
"integer";
261 if ($field[
'type'] == 13) {
262 $field[
'type'] =
"varchar(" . $field[
'length'] .
")";
264 if ($field[
'type'] == 2) {
265 $field[
'type'] =
"int(" . $field[
'length'] .
")";
267 if ($field[
'type'] == 7) {
268 $field[
'type'] =
"date";
270 $final_describe[] = array(
271 "Field" => $field[
"field"],
272 "Type" => $field[
"type"],
273 "Null" => $field[
"null"],
274 "Key" => $field[
"key"]
277 return $final_describe;