24 require_once
CORE_PATH .
'libs/db/adapters/pdo.php';
99 return $this->pdo->lastInsertId(
"{$table}_{$primary_key}_seq");
110 $table = addslashes(strtolower($table));
111 if (strpos($table,
".")) {
112 list($schema, $table) = explode(
".", $table);
115 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'public' AND TABLE_NAME ='$table'");
117 $schema = addslashes(strtolower($schema));
118 $num = $this->
fetch_one(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$schema' AND TABLE_NAME ='$table'");
134 if (isset($params[
'limit']) && is_numeric($params[
'limit'])) {
135 $sql_new.=
" LIMIT $params[limit]";
138 if (isset($params[
'offset']) && is_numeric($params[
'offset'])) {
139 $sql_new.=
" OFFSET $params[offset]";
155 return $this->
query(
"DROP TABLE $table");
160 return $this->
query(
"DROP TABLE $table");
179 $create_sql =
"CREATE TABLE $table (";
180 if (!is_array($definition)) {
181 new KumbiaException(
"Definición invalida para crear la tabla '$table'");
184 $create_lines = array();
186 $unique_index = array();
190 foreach ($definition as $field => $field_def) {
191 if (isset($field_def[
'not_null'])) {
192 $not_null = $field_def[
'not_null'] ?
'NOT NULL' :
'';
196 if (isset($field_def[
'size'])) {
197 $size = $field_def[
'size'] ?
'(' . $field_def[
'size'] .
')' :
'';
201 if (isset($field_def[
'index'])) {
202 if ($field_def[
'index']) {
203 $index[] =
"INDEX($field)";
206 if (isset($field_def[
'unique_index'])) {
207 if ($field_def[
'unique_index']) {
208 $index[] =
"UNIQUE($field)";
211 if (isset($field_def[
'primary'])) {
212 if ($field_def[
'primary']) {
213 $primary[] =
"$field";
216 if (isset($field_def[
'auto'])) {
217 if ($field_def[
'auto']) {
218 $field_def[
'type'] =
"SERIAL";
221 if (isset($field_def[
'extra'])) {
222 $extra = $field_def[
'extra'];
226 $create_lines[] =
"$field " . $field_def[
'type'] . $size .
' ' . $not_null .
' ' . $extra;
228 $create_sql.= join(
',', $create_lines);
229 $last_lines = array();
230 if (count($primary)) {
231 $last_lines[] =
'PRIMARY KEY(' . join(
",", $primary) .
')';
234 $last_lines[] = join(
',', $index);
236 if (count($unique_index)) {
237 $last_lines[] = join(
',', $unique_index);
239 if (count($last_lines)) {
240 $create_sql.=
',' . join(
',', $last_lines) .
')';
242 return $this->
query($create_sql);
252 return $this->
fetch_all(
"SELECT c.relname AS table_name FROM pg_class c, pg_user u "
253 .
"WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
254 .
"AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
255 .
"AND c.relname !~ '^(pg_|sql_)' UNION "
256 .
"SELECT c.relname AS table_name FROM pg_class c "
257 .
"WHERE c.relkind = 'r' "
258 .
"AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
259 .
"AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
260 .
"AND c.relname !~ '^pg_'");
271 $describe = $this->
fetch_all(
"SELECT a.attname AS Field, t.typname AS Type,
272 CASE WHEN attnotnull=false THEN 'YES' ELSE 'NO' END AS Null,
273 CASE WHEN (select cc.contype FROM pg_catalog.pg_constraint cc WHERE
274 cc.conrelid = c.oid AND cc.conkey[1] = a.attnum)='p' THEN 'PRI' ELSE ''
275 END AS Key, CASE WHEN atthasdef=true THEN TRUE ELSE NULL END AS Default
276 FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a,
277 pg_catalog.pg_type t WHERE c.relname = '$table' AND c.oid = a.attrelid
278 AND a.attnum > 0 AND t.oid = a.atttypid order by a.attnum");
279 $final_describe = array();
280 foreach ($describe as $key => $value) {
281 $final_describe[] = array(
282 "Field" => $value[
"field"],
283 "Type" => $value[
"type"],
284 "Null" => $value[
"null"],
285 "Key" => $value[
"key"],
286 "Default" => $value[
"default"]
289 return $final_describe;