|
|
@@ -104,38 +104,563 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
}
|
|
|
|
|
|
function show_tables($table=null) {
|
|
|
- $sql="SELECT tablename as \"Tables_in_".$this->getDatabaseName()."\", 'BASE TABLE' as \"Table_type\" FROM pg_catalog.pg_tables where schemaname='public' ";
|
|
|
- if(!empty($table)) $sql="SELECT tablename as \"Tables_in_".$this->getDatabaseName()."\", 'BASE TABLE' as \"Table_type\" FROM pg_catalog.pg_tables where schemaname='public' and tablename='".$table."' ";
|
|
|
- echo $sql;
|
|
|
+ $sql="SELECT table_name as \"Tables_in_".$this->getDatabaseName()."\", table_type as \"Table_type\" FROM information_schema.tables where table_schema NOT IN ('pg_catalog', 'information_schema'); ";
|
|
|
+ if(!empty($table)) $sql="SELECT table_name as \"Tables_in_".$this->getDatabaseName()."\", table_type as \"Table_type\" FROM information_schema.tables where table_schema NOT IN ('pg_catalog', 'information_schema') and table_name='".$table."' ";
|
|
|
+ DEBUG_S(-3,$sql,$sql,__FILE__,__FUNCTION__,__LINE__);
|
|
|
$res = $this->query($sql);
|
|
|
return $res;
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
function describe_table($table) {
|
|
|
$sql="select column_name as \"Field\" from INFORMATION_SCHEMA.COLUMNS where table_name = '".$table."'";
|
|
|
- $res = $this->query($sql);
|
|
|
- return $res;
|
|
|
+ $sql="SELECT
|
|
|
+ f.attname AS \"Field\",
|
|
|
+
|
|
|
+
|
|
|
+ -- pg_catalog.format_type(f.atttypid,f.atttypmod) AS type,
|
|
|
+
|
|
|
+ CASE
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'double precision' then 'double(8,2)'
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'smallint' then 'int(11)'
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'integer' then
|
|
|
+ ( select cast(replace(pg_catalog.format_type(f.atttypid,f.atttypmod), 'integer', 'int(11)') as char(255)) )
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like '%character varying%' then
|
|
|
+ ( select cast(replace(pg_catalog.format_type(f.atttypid,f.atttypmod), 'character varying', 'varchar') as char(255)) )
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'timestamp without time zone' then 'datetime'
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'date' then 'date'
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'text' then 'text'
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'real' then 'double'
|
|
|
+
|
|
|
+ when pg_catalog.format_type(f.atttypid,f.atttypmod) like 'geometry(%' then
|
|
|
+ ( select cast( split_part( lower(replace(replace(pg_catalog.format_type(f.atttypid,f.atttypmod), 'geometry(', ''),')','')) , ',',1) as char(255)) )
|
|
|
+
|
|
|
+
|
|
|
+ else ( select cast( 'enum('''||string_agg(enumlabel,''',''') || ''')' as char(255))
|
|
|
+ FROM pg_enum e
|
|
|
+ JOIN pg_type t ON e.enumtypid = t.oid
|
|
|
+ WHERE t.typname = replace(pg_catalog.format_type(f.atttypid,f.atttypmod ),'\"','' ) )
|
|
|
+ END as \"Type\" ,
|
|
|
+
|
|
|
+ CASE f.attnotnull
|
|
|
+ WHEN 't' THEN 'NO'
|
|
|
+ WHEN 'f' THEN 'YES'
|
|
|
+ END AS \"Null\",
|
|
|
+
|
|
|
+ CASE
|
|
|
+ WHEN p.contype='p' THEN 'PRI'
|
|
|
+ ELSE ''
|
|
|
+ END AS Key,
|
|
|
+ -- CASE
|
|
|
+ -- WHEN p.contype = 'u' THEN 't'
|
|
|
+ -- ELSE 'f'
|
|
|
+ -- END AS uniquekey,
|
|
|
+ -- CASE
|
|
|
+ -- WHEN p.contype = 'f' THEN g.relname
|
|
|
+ -- END AS foreignkey,
|
|
|
+ -- CASE
|
|
|
+ -- WHEN p.contype = 'f' THEN p.confkey
|
|
|
+ -- END AS foreignkey_fieldnum,
|
|
|
+ -- CASE
|
|
|
+ -- WHEN p.contype = 'f' THEN g.relname
|
|
|
+ -- END AS foreignkey,
|
|
|
+ -- CASE
|
|
|
+ -- WHEN p.contype = 'f' THEN p.conkey
|
|
|
+ -- END AS foreignkey_connnum,
|
|
|
+ CASE
|
|
|
+ WHEN f.atthasdef = 'n' THEN ''
|
|
|
+ WHEN f.atthasdef = 't' AND d.adsrc like 'nextval(%' THEN ''
|
|
|
+ WHEN f.atthasdef = 't' THEN d.adsrc
|
|
|
+ END AS default,
|
|
|
+ CASE
|
|
|
+
|
|
|
+ WHEN f.atthasdef = 't' AND d.adsrc like 'nextval(%' THEN 'auto_increment'
|
|
|
+
|
|
|
+ END AS Extra
|
|
|
+FROM pg_attribute f
|
|
|
+ JOIN pg_class c ON c.oid = f.attrelid
|
|
|
+ JOIN pg_type t ON t.oid = f.atttypid
|
|
|
+ LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
|
|
|
+ LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
|
+ LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
|
|
+ LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
|
|
+WHERE c.relkind = 'r'::char
|
|
|
+ AND n.nspname = 'public' -- Replace with Schema name
|
|
|
+ AND c.relname = '".$table."' -- Replace with table name
|
|
|
+ AND f.attnum > 0
|
|
|
+ -- ORDER BY number
|
|
|
+;
|
|
|
+";
|
|
|
+
|
|
|
+ // echo "<pre>".$sql."</pre>";
|
|
|
+ $result = $this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ DEBUG_S(-3,'describe table '.$table,array($sql,$res,$this->num_rows),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ function describe_table_value($table) {
|
|
|
+
|
|
|
+ $res = self::describe_table($table);
|
|
|
+ while($h=self::fetch($res)) {
|
|
|
+ $result[$h->Field]=$h;
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
}
|
|
|
|
|
|
|
|
|
+ function show_index($table,$only_primary_flag=false) {
|
|
|
+ $sql="select table_name as \"Table\"
|
|
|
+ ,case string_agg( cast( indisunique as char(255) ) ,'')
|
|
|
+ when 'true' then '0'
|
|
|
+ else '1'
|
|
|
+ END as \"Non_unique\"
|
|
|
+
|
|
|
+ , CASE string_agg( cast( indisprimary as char(255) ) ,'')
|
|
|
+ when 'true' then 'PRIMARY'
|
|
|
+ else column_name
|
|
|
+ END as \"Key_name\"
|
|
|
+
|
|
|
+ , CASE string_agg( cast( indisprimary as char(255) ) ,'')
|
|
|
+ when 'true' then 'PRIMARY'
|
|
|
+ -- else cast(string_agg(index_name) as char(255))
|
|
|
+ END
|
|
|
+ ,column_name as \"Column_name\"
|
|
|
+
|
|
|
+from (
|
|
|
+ select
|
|
|
+ t.relname as table_name,
|
|
|
+ i.relname as index_name,
|
|
|
+ a.attname as column_name,
|
|
|
+ ix.indnatts as indnatts,
|
|
|
+ unnest(ix.indkey) as unn,
|
|
|
+ a.attnum,
|
|
|
+ ix.indisunique as indisunique,
|
|
|
+ ix.indisprimary as indisprimary
|
|
|
+ ,ix.indkey
|
|
|
+ ,a.attrelid
|
|
|
+ ,i.oid
|
|
|
+ from
|
|
|
+ pg_class t,
|
|
|
+ pg_class i,
|
|
|
+ pg_index ix,
|
|
|
+ pg_attribute a
|
|
|
+ where
|
|
|
+ t.oid = ix.indrelid
|
|
|
+ and i.oid = ix.indexrelid
|
|
|
+ and a.attrelid = t.oid
|
|
|
+ and a.attnum = ANY(ix.indkey)
|
|
|
+ and t.relkind = 'r'
|
|
|
+ -- and t.relnamespace = 10
|
|
|
+ and t.relname = '".$table."'
|
|
|
+ ";
|
|
|
+ if($only_primary_flag) $sql.=" and ix.indisprimary is true ";
|
|
|
+ $sql.="
|
|
|
+ order by
|
|
|
+ t.relname,
|
|
|
+ i.relname
|
|
|
+ , generate_subscripts(ix.indkey,1)) sb
|
|
|
+where unn = attnum
|
|
|
+ group by table_name,column_name
|
|
|
+-- group by index_name
|
|
|
+";
|
|
|
+ DEBUG_S(-3,'Show index',array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $res = $this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ return $res;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function show_index_value($table) {
|
|
|
+ $res=self::show_index($table,true);
|
|
|
+ while($h=self::fetch($res)) {
|
|
|
+ return $h->Column_name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ function die1($txt) {
|
|
|
+ DEBUG_S(-3,'Died',$txt,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ die();
|
|
|
+
|
|
|
+ }
|
|
|
+ function show_triggers($table) {
|
|
|
+ $sql="SELECT
|
|
|
+trigger_name as \"Trigger\"
|
|
|
+,event_manipulation as \"Event\"
|
|
|
+
|
|
|
+,event_object_table as \"Table\"
|
|
|
+
|
|
|
+,action_statement as \"Statement\"
|
|
|
+,action_timing as \"Timing\"
|
|
|
+FROM information_schema.triggers
|
|
|
+ WHERE event_object_table='".$table."'
|
|
|
+ ORDER BY event_object_table,event_manipulation";
|
|
|
+ DEBUG_S(-3,'Show triggers',array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $res = $this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ return $res;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function show_create_trigger($table,$trigger) {
|
|
|
+ $sql="SELECT
|
|
|
+ trigger_name as \"Trigger\"
|
|
|
+
|
|
|
+ ,action_statement as \"SQL Original Statement\"
|
|
|
+ -- ,action_timing as \"Timing\"
|
|
|
+ -- character_set_client=utf8
|
|
|
+ -- collation_connection=utf8_general_ci
|
|
|
+ -- Database Collation=latin2_general_ci
|
|
|
+
|
|
|
+ FROM information_schema.triggers
|
|
|
+ WHERE trigger_name='".$trigger."' and event_object_table='".$table."'
|
|
|
+ ORDER BY event_object_table,event_manipulation";
|
|
|
+ DEBUG_S(-3,'Show triggers',array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $res = $this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ return $res;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
function from_mysql_struct($type) {
|
|
|
|
|
|
if(strstr($type, 'int(') ) return 'integer';
|
|
|
+ else if(strstr($type, 'double') ) return 'double precision';
|
|
|
else if(strstr($type, 'varchar(') ) return $type;
|
|
|
- else if(strstr($type, 'enum(') ) return 'text'; //todo
|
|
|
+ else if(strstr($type, 'char(') ) return $type;
|
|
|
+ else if(strstr($type, 'enum(') ) return $type; //todo
|
|
|
else if(strstr($type, 'text') ) return $type;
|
|
|
+ else if(strstr($type, 'datetime') ) return 'TIMESTAMP';
|
|
|
else if(strstr($type, 'date') ) return 'date';
|
|
|
else if(strstr($type, 'polygon') ) return 'geometry(Polygon)';
|
|
|
else if(strstr($type, 'linestring') ) return 'geometry(linestring)';
|
|
|
else if(strstr($type, 'point') ) return 'geometry(point)';
|
|
|
+ else if(strstr($type, 'timestamp') ) return 'timestamp';
|
|
|
|
|
|
else die('(TODO)Unknowns transl struct:'.$type);
|
|
|
|
|
|
}
|
|
|
+ //used mainly to translate enum values
|
|
|
+ function alter_add_struct($table,$column,$type) {
|
|
|
+
|
|
|
+
|
|
|
+ if(strstr($type, "enum('")) {
|
|
|
+ $sql="DROP TYPE IF EXISTS \"".$table."_".$column."\"";
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ $sql="CREATE TYPE \"".$table."_".$column."\" AS ".$type;
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ $sql='alter table "'.$table.'" add "'.$column.'" "'.$table.'_'.$column.'"';
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ } else if(strstr($type, "datetime('")) {
|
|
|
+ $sql='alter table "'.$table.'" add "'.$column.'" timestamp without time zone ';
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ } else {
|
|
|
+ $sql='alter table "'.$table.'" add "'.$column.'" '.$type;
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(strstr($type, "linestring'")) {
|
|
|
+ $sql="SELECT UpdateGeometrySRID('".$table."','".$column."',4326);";
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ } else if(strstr($type, "polygon'")) {
|
|
|
+ $sql="SELECT UpdateGeometrySRID('".$table."','".$column."',4326);";
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ } else if(strstr($type, "multipoint'")) {
|
|
|
+ $sql="SELECT UpdateGeometrySRID('".$table."','".$column."',4326);";
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** Returns formated array to parse it to SQL (especialy to get GIS data to keep as WKT)
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+ function GET_DB_OBJ_cols($table) {
|
|
|
+ $structure=self::describe_table_value($table);
|
|
|
+ foreach($structure as $Field=>$Struct) {
|
|
|
+ //echo $Struct->Type;
|
|
|
+ if((strstr($Struct->Type,"multipoint") )||strstr($Struct->Type,"linestring") ||strstr($Struct->Type,"polygon"))
|
|
|
+ $col[]=' ST_AsText("'.$Field.'") as "'.$Field.'"';
|
|
|
+ else $col[]='"'.$Field.'"';
|
|
|
+ }
|
|
|
+ return $col;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- function STRUCTURE_GENERATE_PARSE($config_base_structure_unserialized) {
|
|
|
+ function get_by_data_column($table,$date_column,$timestamp,$now,$remote_key) {
|
|
|
+ $structure=self::describe_table_value($table);
|
|
|
+ if(strlen($timestamp)==0) $timestamp='0';
|
|
|
+ DEBUG_S(-3,'Struct',$structure,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+
|
|
|
+ $col=self::GET_DB_OBJ_cols($table);
|
|
|
+
|
|
|
+ $sql='Select '.implode(',', $col).' from "'.$table.'" where extract(epoch from "'.$date_column.'") > '.$timestamp.' and extract(epoch from "'.$date_column.'") < '.$now;
|
|
|
+ //if(empty($timestamp))
|
|
|
+ $sql.=' or "'.$date_column.'" is null';
|
|
|
+ $sql.=' or "'.$remote_key.'" < 0 ';
|
|
|
+ // $sql.=' or "'.$remote_key.'"=\'\'';
|
|
|
+
|
|
|
+ DEBUG_S(-3,'biore dane ',$sql,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $res=self::query($sql) or die1($sql);
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ function get_current_time() {
|
|
|
+ self::query("set timezone to 'UTC'");
|
|
|
+
|
|
|
+ $sql=" select extract(epoch from now() at time zone 'utc') as time"; // bo nie dziala extract( epoch from CURRENT_TIMESTAMP at time zone 'UTC')
|
|
|
+ $res=self::query($sql);
|
|
|
+ while($h=self::fetch($res)) {
|
|
|
+ return $h->time;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ function get_current_timestamp() {
|
|
|
+ self::query("set timezone to 'UTC'"); //TODO jakas glupota nie dziala extract( epoch from CURRENT_TIMESTAMP at time zone 'UTC')
|
|
|
+ $sql=" select now() at time zone 'utc' as time ";
|
|
|
+ $res=self::query($sql);
|
|
|
+ while($h=self::fetch($res)) {
|
|
|
+ return $h->time;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function set_last_sync_date($table,$now,$remote_db_id,$local_db_id,$timestamp_column='A_RECORD_UPDATE_DATE') {
|
|
|
+ $sql="insert into \"_SYNC_DATABASE_TABLE_STATE\" ( \"TABLE_NAME\", \"LAST_SYNCED\" , \"DATABASE_SYNCED_ID\" , \"DATABASE_LOCAL_ID\" )
|
|
|
+ values ('".$table."' , TIMESTAMP WITH TIME ZONE 'epoch' + ".$now." * INTERVAL '1 second' , '".$remote_db_id."' , '".$local_db_id."')";
|
|
|
+ DEBUG_S(-3,'update sync date',$sql,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::query($sql);
|
|
|
+ $sql="update _SYNC_DATABASE_TABLE_STATE set LAST_SYNCED=from_unixtime('".$now."') where TABLE_NAME='".$table."'
|
|
|
+ and DATABASE_SYNCED_ID='".$remote_db_id."' and DATABASE_LOCAL_ID='".$local_db_id."' ";
|
|
|
+ $sql_c="update \"".$table."\" set \"".$timestamp_column."\"=TIMESTAMP WITH TIME ZONE 'epoch' + ".$now." * INTERVAL '1 second' where \"".$timestamp_column."\" is null ";
|
|
|
+ self::query($sql_c) or die('315Problem z '.$sql_c);
|
|
|
+
|
|
|
+ DEBUG_S(-3,'update sync date',array($sql,$sql_c),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ }
|
|
|
+
|
|
|
+ function get_last_sync_date($table,$remote_db_id,$local_db_id) {
|
|
|
+ $sql="select \"TABLE_NAME\", extract(epoch from \"LAST_SYNCED\" at time zone 'utc') as \"LAST_SYNCED\" from \"_SYNC_DATABASE_TABLE_STATE\"
|
|
|
+ where \"DATABASE_SYNCED_ID\"='".$remote_db_id."' and \"DATABASE_LOCAL_ID\"='".$local_db_id."' and \"TABLE_NAME\"='".$table."'
|
|
|
+ order by \"ID\" desc limit 1
|
|
|
+ ";
|
|
|
+ $res=self::query($sql);
|
|
|
+ DEBUG_S(-3,'Get date',$sql,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ while($h=self::fetch($res)) {
|
|
|
+ return $h->LAST_SYNCED;
|
|
|
+
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** Use it before pasting obj to UPDATE_OBJ or ADD_NEW_OBJ to maintain GIS data especially
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+ function MAKE_DB_OBJ($table,$obj) {
|
|
|
+ $structure=self::describe_table_value($table); //todo to cache optimize
|
|
|
+
|
|
|
+ foreach(get_object_vars($obj) as $k=>$v) {
|
|
|
+ if(trim($structure[$k]->Type)=='multipoint') {
|
|
|
+ if(empty($v)) $v='null';
|
|
|
+ else $v = "ST_GeomFromText('".$v."',4326)";
|
|
|
+ } else if(trim($structure[$k]->Type)=='linestring') {
|
|
|
+ if(empty($v)) $v='null';
|
|
|
+ else $v = "ST_GeomFromText('".$v."',4326)";
|
|
|
+ } else if(trim($structure[$k]->Type)=='polygon') {
|
|
|
+ if(empty($v)) $v='null';
|
|
|
+ else $v = "ST_GeomFromText('".$v."',4326)";
|
|
|
+ } else if(trim($structure[$k]->Type)=='multipolygon') {
|
|
|
+ if(empty($v)) $v='null';
|
|
|
+ else $v = "ST_GeomFromText('".$v."',4326)";
|
|
|
+ } else if(trim($structure[$k]->Type)=='datetime') {
|
|
|
+ if(empty($v)) continue;
|
|
|
+ if($v=='0000-00-00 00:00:00') continue;
|
|
|
+ } else if(strstr($structure[$k]->Type,'double')) {
|
|
|
+ if(empty($v)) continue;
|
|
|
+ } else if(trim($structure[$k]->Type)=='date') {
|
|
|
+ if(empty($v)) continue;
|
|
|
+ if($v=='0000-00-00') continue;
|
|
|
+ } else if(strstr(trim($structure[$k]->Type),'enum(')) {
|
|
|
+ if(empty($v)) continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $obj_ret->$k=$v;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ DEBUG_S(-3,'objs',array($obj,$obj_ret),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+
|
|
|
+ return $obj_ret;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ function STRUCTURE_GENERATE($TABLES,$null=null,$TABLES_STRUCTURE_DATA,$flags=array()){
|
|
|
+ DEBUG_S(-3,'flags',$flags,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ //global $this;
|
|
|
+ //! try to create info for functions and procedures -> ['__SCHEMA_FUNCTIONS__ARRAY__']
|
|
|
+ /*
|
|
|
+ $sql="show function status where Db='".$this->getDatabaseName()."';";
|
|
|
+ DEBUG_S(-3,'sql function detail :: ',$sql,__FILE__,__FUNCTION__,__LINE__) ;
|
|
|
+ $result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ while($h=mysql_fetch_object($result)) {
|
|
|
+ $sql_="show create function `{$h->Name}`" ;
|
|
|
+ DEBUG_S(-3,'sql function detail :: ',$sql_,__FILE__,__FUNCTION__,__LINE__) ;
|
|
|
+ $result_=$this->query($sql_) or self::die1('\nProblem z '.$sql);
|
|
|
+ while($h_=mysql_fetch_object($result_)) {
|
|
|
+ $TABLES_IMPORT['__SCHEMA_FUNCTIONS__ARRAY__']['FUNCTION'][$h->Name]=$h_;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ */
|
|
|
+
|
|
|
+ /*
|
|
|
+ $sql="show PROCEDURE status where Db='".$this->getDatabaseName()."';";
|
|
|
+ DEBUG_S(-3,'sql function detail :: ',$sql_,__FILE__,__FUNCTION__,__LINE__) ;
|
|
|
+ $result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ while($h=mysql_fetch_object($result)) {
|
|
|
+ $sql_="show create PROCEDURE `{$h->Name}`" ;
|
|
|
+ DEBUG_S(-3,'sql PROCEDURE detail :: ',$sql_,__FILE__,__FUNCTION__,__LINE__) ;
|
|
|
+ $result_=$this->query($sql_) or self::die1('\nProblem z '.$sql);
|
|
|
+ while($h_=mysql_fetch_object($result_)) {
|
|
|
+ $TABLES_IMPORT['__SCHEMA_FUNCTIONS__ARRAY__']['PROCEDURE'][$h->Name]=$h_;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ DEBUG_S(-3,'struktury funkcji: __SCHEMA_FUNCTIONS__ARRAY__ ',$TABLES_IMPORT['__SCHEMA_FUNCTIONS__ARRAY__'],__FILE__,__FUNCTION__,__LINE__) ;
|
|
|
+ //self::die1('stop function during dev.');
|
|
|
+ */
|
|
|
+ foreach($TABLES as $TABLE ){
|
|
|
+
|
|
|
+ //$sql="show tables like '".$TABLE."' ;" ;
|
|
|
+ DEBUG_S(-3,'//! Listing possible tables to write to config',$TABLE,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $result=$this->show_tables($TABLE) or self::die1('\nProblem z '.$sql);
|
|
|
+
|
|
|
+ if($this->num_rows($result)==0) {
|
|
|
+
|
|
|
+ // nie ma tabeli - trzeba zapisac
|
|
|
+ DEBUG_S(-3," BRAK TABELI ".$TABLE,null,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ //self::die1();
|
|
|
+ if(isset($TABLES_IMPORT[$TABLE])) {
|
|
|
+ DEBUG_S(-3," Table already imported - problem in twice declared table ".$TABLE,null,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::die1();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ DEBUG_S(-3,'We found rows'.$this->num_rows($result),$TABLE,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ //$sql="show full tables like '".$TABLE."' ;" ;
|
|
|
+ //$result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ $result=$this->show_tables($TABLE);
|
|
|
+ while($h=$this->fetch($result)) {
|
|
|
+ $TABLES_IMPORT[$TABLE]['Table_type']=$h->{'Table_type'};
|
|
|
+ DEBUG_S(-3,'//! Reading to conf if table is WIEV or not :sql/result',$h->{'Table_type'},__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ }
|
|
|
+
|
|
|
+ $FLAG_CONTABLE=false;
|
|
|
+
|
|
|
+ if( ($TABLES_IMPORT[$TABLE]['Table_type']=='BASE TABLE')
|
|
|
+ || ( in_array('with_view', $flags) && $TABLES_IMPORT[$TABLE]['Table_type']=='VIEW' )
|
|
|
+ ) {
|
|
|
+ $FLAG_CONTABLE=true;
|
|
|
+ //$sql=" describe `".$TABLE."` ;" ;
|
|
|
+ //echo "\n ".$sql;
|
|
|
+ // $result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ $result=$this->describe_table($TABLE);
|
|
|
+ while($h=$this->fetch($result)) {
|
|
|
+ //echo "found ";
|
|
|
+ $TABLES_IMPORT[$TABLE]['FIELDS'][]=$h;
|
|
|
+ }
|
|
|
+ //$sql=" show index from `".$TABLE."` ;" ;
|
|
|
+ //$result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ $result=self::show_index($TABLE);
|
|
|
+ $TMP_KEYNAME=array();
|
|
|
+ while($h=self::fetch($result)) {
|
|
|
+ if(isset($h->Column_name)) { $h->Column_name="`".$h->Column_name."`";
|
|
|
+ if(!isset($TMP_KEYNAME[$h->Key_name])) $TMP_KEYNAME[$h->Key_name]=$h->Column_name;
|
|
|
+ else $TMP_KEYNAME[$h->Key_name].=",".$h->Column_name;
|
|
|
+ }
|
|
|
+ //if(isset($TABLES_IMPORT[$TABLE]['INDEX'][$h->Key_name])) {
|
|
|
+ // $TABLES_IMPORT[$TABLE]['INDEX'][$h->Key_name]->Column_name.=", ".$h->Column_name." ";
|
|
|
+ //} else {
|
|
|
+ //}
|
|
|
+ $TABLES_IMPORT[$TABLE]['INDEX'][$h->Key_name]=$h;
|
|
|
+ $TABLES_IMPORT[$TABLE]['INDEX'][$h->Key_name]->Column_name=$TMP_KEYNAME[$h->Key_name];
|
|
|
+ }
|
|
|
+ if(in_array($TABLE,$TABLES_STRUCTURE_DATA)) { //bedziemy dodawac dane tej tabeli
|
|
|
+ $sql=" select * from `".$TABLE."` ;" ;
|
|
|
+ $result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ while($h=self::fetch($result)) {
|
|
|
+ $TABLES_IMPORT[$TABLE]['VALUES'][]=$h;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DEBUG_S(-3,"Dane tabeli KOMORKI ".$TABLE." ",$TABLES_IMPORT[$TABLE]['FIELDS'],__FILE__, __FUNCTION__, __LINE__);
|
|
|
+ DEBUG_S(-3,"Dane tabeli INDEKSY ".$TABLE." ",$TABLES_IMPORT[$TABLE]['INDEX'],__FILE__, __FUNCTION__, __LINE__);
|
|
|
+ if(isset($TABLES_IMPORT[$TABLE]['VALUES'])) DEBUG_S(-3,"Dane tabeli VALUES ".$TABLE." ",$TABLES_IMPORT[$TABLE]['VALUES'],__FILE__, __FUNCTION__, __LINE__);
|
|
|
+
|
|
|
+ //! reading triggers to conf -> $TABLES_IMPORT[$TABLE]['TRIGGERS']
|
|
|
+ //$sql=" show triggers like '".$TABLE."' ;" ;
|
|
|
+ //$result=$this->query($sql) or self::die1('\nProblem z '.$sql);
|
|
|
+ $result=self::show_triggers($TABLE);
|
|
|
+
|
|
|
+
|
|
|
+ while($h=self::fetch($result)) {
|
|
|
+ // $sql_="show create trigger {$h->Trigger} ;" ;
|
|
|
+ // echo $sql_;
|
|
|
+ // $result_=$this->query($sql_) or self::die1('\nProblem z '.$sql_);
|
|
|
+ $result_=self::show_create_trigger($TABLE,$h->Trigger);
|
|
|
+ while($h_=self::fetch($result_)) {
|
|
|
+ DEBUG_S(-3,"Dane tabeli h_ ".$TABLE." ",$h_,__FILE__, __FUNCTION__, __LINE__);
|
|
|
+ $TABLES_IMPORT[$TABLE]['TRIGGERS'][$h->Trigger]=$h_;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(isset($TABLES_IMPORT[$TABLE]['TRIGGERS'])) DEBUG_S(-3,"Dane tabeli TRIGGERS ".$TABLE." ",$TABLES_IMPORT[$TABLE]['TRIGGERS'],__FILE__, __FUNCTION__, __LINE__);
|
|
|
+
|
|
|
+ }
|
|
|
+ if(($TABLES_IMPORT[$TABLE]['Table_type']=='VIEW')) {
|
|
|
+ $FLAG_CONTABLE=true;
|
|
|
+ $sql_="show create view `{$TABLE}` ;" ;
|
|
|
+ //echo $sql_;
|
|
|
+ $result_=$this->query($sql_) or self::die1('\nProblem z '.$sql_);
|
|
|
+ while($h_=self::fetch($result_)) {
|
|
|
+ DEBUG_S(-3,"//! trying to fetch create view fom".$TABLE." sql/res",array($sql_,$h_),__FILE__, __FUNCTION__, __LINE__);
|
|
|
+ $TABLES_IMPORT[$TABLE]['VIEW']=$h_;
|
|
|
+ }
|
|
|
+
|
|
|
+ } if(!$FLAG_CONTABLE) {
|
|
|
+ DEBUG_S(-3,"Unknown table type ! ".$TABLE." ".$TABLES_IMPORT[$TABLE]['Table_type'],$TABLES_IMPORT[$TABLE],__FILE__, __FUNCTION__, __LINE__);
|
|
|
+ self::die1();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //print_r($TABLES_IMPORT);
|
|
|
+
|
|
|
+ if(!function_exists('func_sort_tables_import')) {
|
|
|
+ function func_sort_tables_import($a,$b){
|
|
|
+ if($a['Table_type']==$b['Table_type']) return 0;
|
|
|
+ else if($a['Table_type']=='VIEW') return 1;
|
|
|
+ else return -1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ uasort($TABLES_IMPORT,'func_sort_tables_import');
|
|
|
+
|
|
|
+ DEBUG_S(-3,'Wygenerowane struktury (TABLES_IMPORT)',$TABLES_IMPORT,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ return($TABLES_IMPORT);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ function STRUCTURE_GENERATE_PARSE($config_base_structure_unserialized,$options=array()) {
|
|
|
|
|
|
|
|
|
//budowa struktur w bazie
|
|
|
@@ -148,6 +673,10 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
//! try to sync __SCHEMA_FUNCTIONS__ARRAY__
|
|
|
// triggers sync disabled
|
|
|
/*
|
|
|
+ if(in_array('SKIP-PROCEDURE', $options)) {
|
|
|
+ DEBUG_S(-3,'SKIP-PROCEDURES option',null,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
if($TABLE=='__SCHEMA_FUNCTIONS__ARRAY__') {
|
|
|
DEBUG_S(-3,'Try to synchronize functions and procedures',$ARR_TABLE,__FILE__,__FUNCTION__,__LINE__);
|
|
|
|
|
|
@@ -307,8 +836,12 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
//DEBUG_S(-3,"\n BRAK TABELI dodajemy : ".$TABLE."!!!",null,__FILE__,__FUNCTION__,__LINE__);
|
|
|
|
|
|
// $sql="create table ".$TABLE." ( ".$ARR_TABLE['FIELDS'][0]->Field." ".$ARR_TABLE['FIELDS'][0]->Type ; // ID int(11) NOT NULL AUTO_INCREMENT,
|
|
|
- $sql="create table \"".$TABLE."\" ( \"".$ARR_TABLE['FIELDS'][0]->Field."\" ".$this->from_mysql_struct($ARR_TABLE['FIELDS'][0]->Type) ; // ID int(11) NOT NULL AUTO_INCREMENT,
|
|
|
-
|
|
|
+ if(trim($ARR_TABLE['FIELDS'][0]->Extra)=='auto_increment') {
|
|
|
+ if(in_array('SKIP-PRIMARY-KEY', $options)) {
|
|
|
+ $sql="create table \"".$TABLE."\" ( \"id_".$options['getDB']."\" serial
|
|
|
+ , CONSTRAINT \"".$TABLE."_pkey\" PRIMARY KEY (\"id_".$options['getDB']."\")" ;
|
|
|
+ } else $sql="create table \"".$TABLE."\" ( \"".$ARR_TABLE['FIELDS'][0]->Field."\" serial " ; // ID int(11) NOT NULL AUTO_INCREMENT,
|
|
|
+ } else $sql="create table \"".$TABLE."\" ( \"".$ARR_TABLE['FIELDS'][0]->Field."\" ".$this->from_mysql_struct($ARR_TABLE['FIELDS'][0]->Type) ; // ID int(11) NOT NULL AUTO_INCREMENT,
|
|
|
// if($ARR_TABLE['FIELDS'][0]->Null=='NO') $sql.=" not null "; else $sql.=" null ";
|
|
|
//if($ARR_TABLE['FIELDS'][0]->Extra=='auto_increment') $sql.=" AUTO_INCREMENT "; else $sql.=" ";
|
|
|
//$sql.=" ) DEFAULT CHARSET=latin2 ";
|
|
|
@@ -339,7 +872,7 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
$sql_="show create view `{$TABLE}` ;" ;
|
|
|
echo $sql_;
|
|
|
$result_=$this->query($sql_) or die('\nProblem z '.$sql_);
|
|
|
- while($h_=mysql_fetch_object($result_)) {
|
|
|
+ while($h_=self::fetch($result_)) {
|
|
|
DEBUG_S(-3,"//! get current view info ".$TABLE." sql/res",array($sql_,$h_),__FILE__, __FUNCTION__, __LINE__);
|
|
|
$FIELDS_CHECKED['VIEW']=$h_;
|
|
|
}
|
|
|
@@ -379,7 +912,7 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
DEBUG_S(-3,'FIELDS_CHECKED',$FIELDS_CHECKED,__FILE__, __FUNCTION__, __LINE__) ;
|
|
|
$var_passed=false;
|
|
|
while($h=$this->fetch($result)) {
|
|
|
- print_r($h);
|
|
|
+
|
|
|
//tu sprawdzamy czy sa wszystkie kolumny i maja takie same typy
|
|
|
$FIELDS_CHECKED['FIELD_FROM_DB'][$h->Field]=1;
|
|
|
if(!isset($FIELDS_CHECKED['FIELD_IN_CONFIG_THAT_EXISTS'][$h->Field])) {
|
|
|
@@ -416,17 +949,18 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
// $sql="create table ".$TABLE." ( ".$ARR_TABLE['FIELDS'][0]->Field." ".$ARR_TABLE['FIELDS'][0]->Type ; // ID int(11) NOT NULL AUTO_INCREMENT,
|
|
|
//if($ARR_TABLE['FIELDS'][0]->Extra=='auto_increment') $sql.=" AUTO_INCREMENT "; else $sql.=" ";
|
|
|
- $sql="alter table \"".$TABLE."\" add \"".$FIELD_TO_ADD."\" ".$this->from_mysql_struct($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type)." ";
|
|
|
- if(($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Default)) {
|
|
|
+ //* $sql="alter table \"".$TABLE."\" add \"".$FIELD_TO_ADD."\" ".$this->from_mysql_struct($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type)." ";
|
|
|
+ self::alter_add_struct($TABLE,$FIELD_TO_ADD,$this->from_mysql_struct($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type));
|
|
|
+ //if(($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Default)) {
|
|
|
/// if(($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Default=='NULL')||($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Default=='CURRENT_TIMESTAMP'))
|
|
|
/// $sql.=" DEFAULT ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Default." ";// (".$INDEX_OF_CONFIG_TABLE.")";
|
|
|
/// else $sql.=" DEFAULT '".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Default."' ";// (".$INDEX_OF_CONFIG_TABLE.")";
|
|
|
- }
|
|
|
+ // }
|
|
|
/// TODO !!! if($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Null=='NO') $sql.=" not null "; else $sql.=" null ";
|
|
|
- $sql.=" null ";
|
|
|
- $sql.=" ;";
|
|
|
- echo "\n ".$sql;
|
|
|
- $this->query($sql) or DEBUG_S(-3,'Problem z sql',$sql,__FILE__, __FUNCTION__, __LINE__) or die();
|
|
|
+ // $sql.=" null ";
|
|
|
+ // $sql.=" ;";
|
|
|
+ // echo "\n ".$sql;
|
|
|
+ // $this->query($sql) or DEBUG_S(-3,'Problem z sql',$sql,__FILE__, __FUNCTION__, __LINE__) or die();
|
|
|
|
|
|
|
|
|
}
|
|
|
@@ -439,7 +973,7 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
$result=$this->describe_table($TABLE) or die('\nProblem z '.$sql);
|
|
|
|
|
|
//$result=$this->query($sql) or die('\nProblem z '.$sql);
|
|
|
- while($h=mysql_fetch_object($result)) {
|
|
|
+ while($h=self::fetch($result)) {
|
|
|
set_time_limit(36000);
|
|
|
$FIELDS_CHECKED['FIELD_FOUND_IN_REMOTE_DB_PROFILE'][$h->Field]=$h;
|
|
|
|
|
|
@@ -449,7 +983,7 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
foreach($FIELDS_CHECKED['FIELD_FOUND_IN_REMOTE_DB_PROFILE'] as $FIELD_TO_CHK=>$VAL) {
|
|
|
if(isset($INDEX_OF_CONFIG_TABLE)) unset($INDEX_OF_CONFIG_TABLE);
|
|
|
foreach($ARR_TABLE['FIELDS'] as $TSTIND=>$TST_VAL) { //trzeba znalezc numer ID indeksu brakujacej komorki
|
|
|
- if($TST_VAL->Field==$FIELD_TO_CHK) $INDEX_OF_CONFIG_TABLE=$TSTIND;
|
|
|
+ if(trim($TST_VAL->Field)==trim($FIELD_TO_CHK)) $INDEX_OF_CONFIG_TABLE=$TSTIND;
|
|
|
}
|
|
|
if(isset($FIELDS_CHECKED['FIELD_IN_CONFIG_THAT_NOT_EXISTS'][$FIELD_TO_CHK])) continue; //jezeli komorki nie ma i jest tylko lokalnie - ignorujemy!
|
|
|
// to jakis glupi warunek ?
|
|
|
@@ -457,14 +991,14 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
// echo "\nsprawdzamy wszystkie wlasciwosci dla db ".$TABLE." ".$FIELD_TO_CHK." col ".$INDEX_OF_CONFIG_TABLE;
|
|
|
$CHK_TYPES_ARS=array('Type','Null','Default');
|
|
|
$sql="";
|
|
|
- // echo "\n if ".$VAL->Type." =? ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type ;
|
|
|
- if($VAL->Type!=$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type) {
|
|
|
+ // echo "\n if ".var_dump($VAL->Type)." =? ".var_dump($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type) ;
|
|
|
+ if(!strcmp(trim($VAL->Type),trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type))) {
|
|
|
//! aktualizujemy typy
|
|
|
- $sql=" alter table `".$TABLE."` CHANGE `".$FIELD_TO_CHK."` `".$FIELD_TO_CHK."` ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ; ";
|
|
|
+ $sql=" alter table \"".$TABLE."\" ALTER COLUMN \"".$FIELD_TO_CHK."\" ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ; ";
|
|
|
|
|
|
- if(strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'char(')&& strstr($VAL->Type, 'char(')){
|
|
|
- $src_char_len=ereg_replace('[^0-9]', '',$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type );
|
|
|
- $dst_char_len=ereg_replace('[^0-9]', '',$VAL->Type );
|
|
|
+ if(strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'char(')&& strstr(trim($VAL->Type), 'char(')){
|
|
|
+ $src_char_len=ereg_replace('[^0-9]', '',trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type) );
|
|
|
+ $dst_char_len=ereg_replace('[^0-9]', '',trim($VAL->Type) );
|
|
|
|
|
|
if($src_char_len>$dst_char_len) {
|
|
|
DEBUG_S(-3,'Aktualizacja rozszerzajaca zakres pola CHAR : CUR_CHAR_LEN ='.$src_char_len." and dest=".$dst_char_len ,$sql,__FILE__, __FUNCTION__, __LINE__);
|
|
|
@@ -474,10 +1008,10 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
DEBUG_S(-3,'celowe pominiecie - aktualizacja moze skasowac dane w bazie!!! :',$sql,__FILE__, __FUNCTION__, __LINE__);
|
|
|
// die();
|
|
|
}
|
|
|
- } else if(strstr($VAL->Type, 'enum(') && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'enum(' )) { //jezeli zmienia sie ENUM na inne ENUM
|
|
|
+ } else if(strstr(trim($VAL->Type), 'enum(') && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'enum(' )) { //jezeli zmienia sie ENUM na inne ENUM
|
|
|
|
|
|
- preg_match('/^enum\((.*)\)$/', $ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, $src_char_len);
|
|
|
- preg_match('/^enum\((.*)\)$/', $VAL->Type, $dst_char_len);
|
|
|
+ preg_match('/^enum\((.*)\)$/', trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), $src_char_len);
|
|
|
+ preg_match('/^enum\((.*)\)$/', trim($VAL->Type), $dst_char_len);
|
|
|
|
|
|
$src_char_len=explode(',', $src_char_len[1]);$dst_char_len=explode(',', $dst_char_len[1]);
|
|
|
foreach($src_char_len as $var) {
|
|
|
@@ -485,16 +1019,16 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
}
|
|
|
$dst_char_len="enum(".implode(",", $dst_char_len).")";
|
|
|
- $sql=" alter table `".$TABLE."` CHANGE `".$FIELD_TO_CHK."` `".$FIELD_TO_CHK."` ".$dst_char_len." ; ";
|
|
|
+ $sql=" alter table \"".$TABLE."\" CHANGE \"".$FIELD_TO_CHK."\" \"".$FIELD_TO_CHK."\" ".$dst_char_len." ; ";
|
|
|
|
|
|
DEBUG_S(-3,'Uzupelniamy strukture o ewentualnie brakujace elementy (sql,wynik polaczenia,import.strukt.,akt_strukt.)',array($sql,$dst_char_len,$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type,$VAL->Type),__FILE__, __FUNCTION__, __LINE__);
|
|
|
|
|
|
if(!empty($CONFIRM_SQL_UPDATE)) $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL:',$sql,__FILE__, __FUNCTION__, __LINE__) or die('\nProblem z '.$sql);
|
|
|
|
|
|
- } else if(strstr($VAL->Type, 'set(') && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'set(' )) { //jezeli zmienia sie ENUM na inne ENUM
|
|
|
+ } else if(strstr(trim($VAL->Type), 'set(') && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'set(' )) { //jezeli zmienia sie ENUM na inne ENUM
|
|
|
|
|
|
- preg_match('/^set\((.*)\)$/', $ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, $src_char_len);
|
|
|
- preg_match('/^set\((.*)\)$/', $VAL->Type, $dst_char_len);
|
|
|
+ preg_match('/^set\((.*)\)$/', trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), $src_char_len);
|
|
|
+ preg_match('/^set\((.*)\)$/', trim($VAL->Type), $dst_char_len);
|
|
|
|
|
|
$src_char_len=explode(',', $src_char_len[1]);$dst_char_len=explode(',', $dst_char_len[1]);
|
|
|
foreach($src_char_len as $var) {
|
|
|
@@ -502,31 +1036,32 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
}
|
|
|
$dst_char_len="set(".implode(",", $dst_char_len).")";
|
|
|
- $sql=" alter table `".$TABLE."` CHANGE `".$FIELD_TO_CHK."` `".$FIELD_TO_CHK."` ".$dst_char_len." ; ";
|
|
|
+ $sql=" alter table \"".$TABLE."\" CHANGE \"".$FIELD_TO_CHK."\" \"".$FIELD_TO_CHK."\" ".$dst_char_len." ; ";
|
|
|
|
|
|
DEBUG_S(-3,'Uzupelniamy strukture o ewentualnie brakujace elementy (sql,wynik polaczenia,import.strukt.,akt_strukt.)',array($sql,$dst_char_len,$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type,$VAL->Type),__FILE__, __FUNCTION__, __LINE__);
|
|
|
if(!empty($CONFIRM_SQL_UPDATE)) $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL:',$sql,__FILE__, __FUNCTION__, __LINE__) or die('\nProblem z '.$sql);
|
|
|
|
|
|
- } else if(strstr($VAL->Type, 'int(') && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'int(' )) {
|
|
|
+ } else if(strstr(trim($VAL->Type), 'int(') && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'int(' )) {
|
|
|
+ DEBUG_S(-3,'Te same typy INT',array($VAL->Type,$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type),__FILE__,__FUNCTION__,__LINE__);
|
|
|
preg_match('/^int\((.*)\)$/', $ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, $src_char_len);
|
|
|
preg_match('/^int\((.*)\)$/', $VAL->Type, $dst_char_len);
|
|
|
if($src_char_len[1]>$dst_char_len[1]) {
|
|
|
- $sql=" alter table `".$TABLE."` CHANGE `".$FIELD_TO_CHK."` `".$FIELD_TO_CHK."` ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ; ";
|
|
|
+ $sql=" alter table \"".$TABLE."\" ALTER COLUMN \"".$FIELD_TO_CHK."\" type ".self::from_mysql_struct($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type)." ; ";
|
|
|
DEBUG_S(-3,'Aktualizacja rozszerzajaca zakres pola INT ma byc ='.$src_char_len[1]." byla dest=".$dst_char_len[1] ,$sql,__FILE__, __FUNCTION__, __LINE__);
|
|
|
// if(!empty($CONFIRM_SQL_UPDATE)) $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL:',$sql,__FILE__, __FUNCTION__, __LINE__) or die('\nProblem z '.$sql);
|
|
|
} else {
|
|
|
DEBUG_S(-3,'Pominieta Aktualizacja zawiezajaca zakres pola INT ma byc ='.$src_char_len[1]." byla dest=".$dst_char_len[1] ,$sql,__FILE__, __FUNCTION__, __LINE__);
|
|
|
}
|
|
|
|
|
|
- } else if( ( strstr($VAL->Type, 'char(') || strstr($VAL->Type, 'varchar(') ) && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'text' )
|
|
|
- || ( strstr($VAL->Type, 'bigint(') ) && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'longblob' )
|
|
|
- || ( strstr($VAL->Type, 'mediumtext') ) && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'longblob' )
|
|
|
- || ( strstr($VAL->Type, 'decimal(42') ) && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'decimal(41' )
|
|
|
- || ( strstr($VAL->Type, 'decimal(43') ) && strstr($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type, 'decimal(41' )
|
|
|
+ } else if( ( strstr(trim($VAL->Type), 'char(') || strstr(trim($VAL->Type), 'varchar(') ) && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'text' )
|
|
|
+ || ( strstr(trim($VAL->Type), 'bigint(') ) && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'longblob' )
|
|
|
+ || ( strstr(trim($VAL->Type), 'mediumtext') ) && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'longblob' )
|
|
|
+ || ( strstr(trim($VAL->Type), 'decimal(42') ) && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'decimal(41' )
|
|
|
+ || ( strstr(trim($VAL->Type), 'decimal(43') ) && strstr(trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type), 'decimal(41' )
|
|
|
|
|
|
) {
|
|
|
|
|
|
- $sql=" alter table `".$TABLE."` CHANGE `".$FIELD_TO_CHK."` `".$FIELD_TO_CHK."` ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ; ";
|
|
|
+ $sql=" alter table \"".$TABLE."\" CHANGE \"".$FIELD_TO_CHK."\" \"".$FIELD_TO_CHK."\" ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ; ";
|
|
|
DEBUG_S(-3,'Aktualizacja rozszerzajaca zakres pola varchar/char() do text()',$sql,__FILE__, __FUNCTION__, __LINE__);
|
|
|
if(!empty($CONFIRM_SQL_UPDATE)) $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL:',$sql,__FILE__, __FUNCTION__, __LINE__) or die('\nProblem z '.$sql);
|
|
|
// DEBUG_S(-3,'Pominieta Aktualizacja zawiezajaca zakres pola INT ma byc ='.$src_char_len[1]." byla dest=".$dst_char_len[1] ,$sql,__FILE__, __FUNCTION__, __LINE__);
|
|
|
@@ -534,6 +1069,8 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
} else {
|
|
|
DEBUG_S(-3,'Celowe zabicie z uwagi na niebezpieczenstwo zmiany typu: sql/old/new',array($sql,$VAL->Type,$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type),__FILE__, __FUNCTION__, __LINE__);
|
|
|
+ echo "<font color=red> DO AKTAULIZACJI PHP BO TO JEST BLAD PHP!!!</font>";
|
|
|
+ continue;
|
|
|
die();
|
|
|
if(!empty($CONFIRM_SQL_UPDATE)) $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL:',$sql,__FILE__, __FUNCTION__, __LINE__) or die('\nProblem z '.$sql);
|
|
|
}
|
|
|
@@ -557,21 +1094,25 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
}
|
|
|
//autoincrement:
|
|
|
- if(($VAL->Extra!=$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Extra)&&$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Extra=='auto_increment' ) {
|
|
|
+ if(in_array('SKIP-PRIMARY-KEY', $options)) {
|
|
|
+ DEBUG_S(4,'SKIP-PRIMARY-KEY option',null,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+
|
|
|
+ } else if((trim($VAL->Extra)!=trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Extra))&&trim($ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Extra)=='auto_increment' ) {
|
|
|
//sprawdzmy czy nie ma w tabeli klucza z PRIMARY z ID
|
|
|
- $sql="show keys from `".$TABLE."`";
|
|
|
- $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL ',$sql,__FILE__, __FUNCTION__, __LINE__) or DIE();
|
|
|
+ //$sql="show keys from `".$TABLE."`";
|
|
|
+ //$result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL ',$sql,__FILE__, __FUNCTION__, __LINE__) or DIE();
|
|
|
+ $result=self::show_index($TABLE);
|
|
|
$TEST_IF_HAS_PRIMARY=false;
|
|
|
- while($tst=mysql_fetch_object($result)) { //dla kazdego klucza
|
|
|
- if($tst->Column_name==$FIELD_TO_CHK) //sprawdz czy jest klucz do tej kolumny
|
|
|
- if($tst->Key_name=='PRIMARY') $TEST_IF_HAS_PRIMARY=true; //jezeli jest primary, to flagujemy
|
|
|
+ while($tst=self::fetch($result)) { //dla kazdego klucza
|
|
|
+ if(trim($tst->Column_name)==$FIELD_TO_CHK) //sprawdz czy jest klucz do tej kolumny
|
|
|
+ if(trim($tst->Key_name)=='PRIMARY') $TEST_IF_HAS_PRIMARY=true; //jezeli jest primary, to flagujemy
|
|
|
}
|
|
|
|
|
|
if($TEST_IF_HAS_PRIMARY==false) { //bedziemy dodawac primary key
|
|
|
- $sql="ALTER TABLE `".$TABLE."` ADD PRIMARY KEY ( `".$FIELD_TO_CHK."` )";
|
|
|
+ $sql="ALTER TABLE \"".$TABLE."\" ADD PRIMARY KEY ( \"".$FIELD_TO_CHK."\" )";
|
|
|
$result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL ',$sql,__FILE__, __FUNCTION__, __LINE__) or DIE();
|
|
|
}
|
|
|
- $sql=" alter table `".$TABLE."` CHANGE `".$FIELD_TO_CHK."` `".$FIELD_TO_CHK."` ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Extra." ; ";
|
|
|
+ $sql=" alter table \"".$TABLE."\" ALTER COLUMN \"".$FIELD_TO_CHK."\" TYPE ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Type." ".$ARR_TABLE['FIELDS'][$INDEX_OF_CONFIG_TABLE]->Extra." ; ";
|
|
|
echo "\n SQL auto_increment ".$sql ;
|
|
|
if(!empty($CONFIRM_SQL_UPDATE)) $result=$this->query($sql) or DEBUG_S(-3,'Problem ze SQL ',$sql,__FILE__, __FUNCTION__, __LINE__) or DIE();
|
|
|
|
|
|
@@ -705,7 +1246,7 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
//tu bedziemy dodawac wartosci insert :
|
|
|
if(!strstr($TABLE, '__SCHEMA_FUNCTIONS__ARRAY__')) {
|
|
|
- $sql="select count(\"ID\") as ilosc from \"".$TABLE."\" ;" ;
|
|
|
+ $sql="select count(*) as ilosc from \"".$TABLE."\" ;" ;
|
|
|
DEBUG_S(-3,'SQL: licze czy cos jest w tabeli ',$sql,__FILE__, __FUNCTION__, __LINE__) ;
|
|
|
$result=$this->query($sql) or die('\nProblem z '.$sql);
|
|
|
$ilosc=0;
|
|
|
@@ -769,8 +1310,8 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
return $return;
|
|
|
}
|
|
|
|
|
|
- function affected_rows() {
|
|
|
- return pg_affected_rows( $this->_conn );
|
|
|
+ function affected_rows($res) {
|
|
|
+ return pg_affected_rows( $res );
|
|
|
}
|
|
|
|
|
|
function _( $str ) {
|
|
|
@@ -784,10 +1325,12 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
|
|
|
|
|
|
function get_by_id( $table, $id ) {
|
|
|
+ $primary=self::show_index_value($table); //TODO to optimalize cache
|
|
|
+
|
|
|
$null = null;
|
|
|
$sql = "select p.*
|
|
|
- from `".$table."` as p
|
|
|
- where p.`ID`='".$id."'
|
|
|
+ from \"".$table."\" as p
|
|
|
+ where p.\"".$primary."\"='".$id."'
|
|
|
";
|
|
|
$res = $this->query( $sql );
|
|
|
if ($r = $this->fetch( $res )) {
|
|
|
@@ -808,21 +1351,26 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
* TODO: sprawdzac czy w hist mozna odczytac aktualny stan, jesli nie to dodac caly rekord do HIST, jako 'procesy-fix-hist-data'
|
|
|
*/
|
|
|
public function UPDATE_OBJ($table, $sql_obj) {
|
|
|
- if (!isset($sql_obj->ID) || $sql_obj->ID <= 0) {
|
|
|
+ $structure=self::describe_table_value($table);
|
|
|
+ $primary=self::show_index_value($table);
|
|
|
+
|
|
|
+ if (!isset($sql_obj->$primary) || $sql_obj->$primary <= 0) {
|
|
|
return -3;
|
|
|
}
|
|
|
- $id = $sql_obj->ID;
|
|
|
+ $id = $sql_obj->$primary;
|
|
|
|
|
|
// check id record $id exists
|
|
|
- if (($curr_obj = $this->get_by_id( $table, $sql_obj->ID )) == null) {
|
|
|
+ if (($curr_obj = $this->get_by_id( $table, $sql_obj->$primary )) == null) {
|
|
|
return -2;
|
|
|
}
|
|
|
|
|
|
// check if enything changed
|
|
|
$changed = false;
|
|
|
$fields_to_change = get_object_vars($sql_obj);
|
|
|
+ //DEBUG_S(-3,'chk',array($fields_to_change),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+
|
|
|
foreach ($fields_to_change as $k => $v) {
|
|
|
- if ($k == 'ID') continue;
|
|
|
+ if ($k == $primary) continue;
|
|
|
if ($v == $curr_obj->$k) {// === ?
|
|
|
unset($sql_obj->$k);
|
|
|
} else {
|
|
|
@@ -839,10 +1387,18 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
$admin_col []= 'A_RECORD_CREATE_DATE';
|
|
|
$admin_col []= 'A_RECORD_CREATE_AUTHOR';
|
|
|
// ...
|
|
|
- $sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d-H:i');
|
|
|
+// $sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d H:i');
|
|
|
+ $sql_obj->A_RECORD_UPDATE_DATE = self::get_current_timestamp();
|
|
|
+
|
|
|
$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
|
|
|
foreach (get_object_vars($sql_obj) as $k => $v) {
|
|
|
- if (strtoupper($v) == 'NOW()') {
|
|
|
+ if($k=='A_RECORD_CREATE_DATE'&& $v=='0000-00-00 00:00:00') {
|
|
|
+ unset($k);
|
|
|
+ unset($v);
|
|
|
+ continue;
|
|
|
+ } else if (substr($v, 0, strlen('ST_GeomFromText')) == 'ST_GeomFromText') {
|
|
|
+
|
|
|
+ } else if (strtoupper($v) == 'NOW()') {
|
|
|
$v = 'NOW()';
|
|
|
} else if (strtoupper($v) == 'NULL') {
|
|
|
$v = 'NULL';
|
|
|
@@ -850,19 +1406,25 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
$v = $this->_($v);
|
|
|
$v = "'{$v}'";
|
|
|
}
|
|
|
- $sql_arr [] = "`{$k}`={$v}";
|
|
|
+ $sql_arr [] = '"'.$k.'"='.$v;
|
|
|
}
|
|
|
- $sql = "update `{$table}` set ".implode(",", $sql_arr)." where `ID`='{$id}' limit 1; ";
|
|
|
- $this->query($sql);
|
|
|
+
|
|
|
+ $sql = "update \"{$table}\" set ".implode(",", $sql_arr)." where \"".$primary."\"='{$id}' ; ";
|
|
|
+ // DEBUG_S(-3,'update',array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ // die();
|
|
|
+ $res=$this->query($sql);
|
|
|
|
|
|
if ($this->has_errors()) {
|
|
|
+ //DEBUG_S(-3,'errors',$this->has_errors,__FILE__,__FUNCTION__,__LINE__);
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- $ret = $this->affected_rows();
|
|
|
+ $ret = $this->affected_rows($res);
|
|
|
+ // DEBUG_S(-3,'affected',$ret,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+
|
|
|
if ($ret) {
|
|
|
- $sql_obj->ID_USERS2 = $sql_obj->ID;
|
|
|
- unset($sql_obj->ID);
|
|
|
+ $sql_obj->ID_USERS2 = $sql_obj->$primary;
|
|
|
+ unset($sql_obj->$primary);
|
|
|
$new_id = $this->ADD_NEW_OBJ("{$table}_HIST", $sql_obj);
|
|
|
if ($new_id) {
|
|
|
$ret += 1;
|
|
|
@@ -871,41 +1433,62 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
return $ret;
|
|
|
}
|
|
|
|
|
|
- function ADD_NEW_OBJ( $table, $sql_obj ) {
|
|
|
+ function ADD_NEW_OBJ( $table, $sql_obj,$dieonerror=null ) {
|
|
|
+ $structure=self::describe_table_value($table);
|
|
|
+ $primary=self::show_index_value($table);
|
|
|
$sql_arr = array();
|
|
|
// TODO: add admin columns if exists in table - search in session
|
|
|
$admin_col = array();
|
|
|
- $admin_col []= 'ID';
|
|
|
$admin_col []= 'A_RECORD_CREATE_DATE';
|
|
|
$admin_col []= 'A_RECORD_CREATE_AUTHOR';
|
|
|
$admin_col []= 'A_RECORD_UPDATE_DATE';
|
|
|
$admin_col []= 'A_RECORD_UPDATE_AUTHOR';
|
|
|
// ...
|
|
|
- $sql_arr["`ID`"] = "NULL";// add default value for ID, NULL in all inserts
|
|
|
if (substr($table, 0, -5) == '_HIST') {
|
|
|
- $sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d-H:i');
|
|
|
+ $sql_obj->A_RECORD_UPDATE_DATE = date('Y-m-d H:i');
|
|
|
$sql_obj->A_RECORD_UPDATE_AUTHOR = User::getName();
|
|
|
} else {
|
|
|
- $sql_obj->A_RECORD_CREATE_DATE = date('Y-m-d-H:i');
|
|
|
+ $sql_obj->A_RECORD_CREATE_DATE = date('Y-m-d H:i');
|
|
|
$sql_obj->A_RECORD_CREATE_AUTHOR = User::getName();
|
|
|
}
|
|
|
|
|
|
foreach (get_object_vars($sql_obj) as $k => $v) {
|
|
|
- if (strtoupper($v) == 'NOW()') {
|
|
|
+ if($k==$primary) {
|
|
|
+ unset($k);
|
|
|
+ unset($v);
|
|
|
+ continue;
|
|
|
+ } else if (substr($v, 0, strlen('ST_GeomFromText')) == 'ST_GeomFromText') {
|
|
|
+
|
|
|
+ } else if(strstr(trim($structure[$k]->Type),'int')) {
|
|
|
+ if(strlen($v)>0) {
|
|
|
+ $v=$v;
|
|
|
+ } else if(empty($v)) $v='null';
|
|
|
+ else $v = $v;
|
|
|
+ } else if(trim($structure[$k]->Type)=='datetime') {
|
|
|
+ if(empty($v)) $v='null';
|
|
|
+ else $v = "'".$v."'::timestamp";
|
|
|
+ } else if (strtoupper($v) == 'NOW()') {
|
|
|
$v = 'NOW()';
|
|
|
} else if (strtoupper($v) == 'NULL' && substr($table, -5) != '_HIST') {
|
|
|
$v = 'NULL';
|
|
|
+ } else if(strstr($structure[$k]->Type,'int(')) {
|
|
|
+ $v = $v ;
|
|
|
} else {
|
|
|
$v = $this->_($v);
|
|
|
$v = "'{$v}'";
|
|
|
}
|
|
|
- $sql_arr ["`{$k}`"] = $v;
|
|
|
+ $sql_arr ['"'.$k.'"'] = $v;
|
|
|
}
|
|
|
- $sql = "insert into `{$table}` (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr))."); ";
|
|
|
+ $sql = "insert into \"{$table}\" (".implode(",", array_keys($sql_arr)).") values (".implode(",", array_values($sql_arr))."); ";
|
|
|
+ //DEBUG_S(-3,' insert sql ',$sql,__FILE__,__FUNCTION__,__LINE__);
|
|
|
$this->query($sql);
|
|
|
|
|
|
if ($this->has_errors()) {
|
|
|
- //echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->get_errors());echo'</pre>';
|
|
|
+ if($dieonerror) {
|
|
|
+ DEBUG_S(-3,'Has errors died',$this->get_errors(),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ die();
|
|
|
+ }
|
|
|
+ // echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">db errors: (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($this->get_errors());print_r($sql);echo'</pre>';
|
|
|
}
|
|
|
|
|
|
$ret_id = $this->insert_id();
|
|
|
@@ -921,4 +1504,219 @@ class Core_Database_Pgsql extends Core_Database {
|
|
|
return $ret_id;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ function MAKE_SE_AND_HIST_FIELDS_FOR_TABLE($table){
|
|
|
+
|
|
|
+ $primary=self::show_index_value($table);
|
|
|
+
|
|
|
+ //TODO @2014-10-01 zrobic tabele z autoincrement + hist
|
|
|
+ //die('function incomplete');
|
|
|
+
|
|
|
+
|
|
|
+ //$sql="show tables like '".$table."';";
|
|
|
+ DEBUG_S(-3,'primary detected ',array($sql,$primary),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $USERS_COLUMN_INIT2['REQUEST_COLUMN']=array(
|
|
|
+ 'A_RECORD_CREATE_DATE'=>'datetime','A_RECORD_CREATE_AUTHOR'=>'varchar(100)','SYNC_SQIX_STATUS'=>'varchar(100) ',
|
|
|
+ 'A_RECORD_UPDATE_DATE'=>'datetime','A_RECORD_UPDATE_AUTHOR'=>'varchar(100) ',
|
|
|
+ 'L_APPOITMENT_DATE'=>'varchar(30) ',
|
|
|
+ 'L_APPOITMENT_USER'=>"varchar(100) ",
|
|
|
+ 'L_APPOITMENT_PERIOD'=>"varchar(4) ",
|
|
|
+ 'L_APPOITMENT_INFO'=>"varchar(255) ",
|
|
|
+ 'L_APPOITMENT_TYPE'=>"enum('', 'ARRANGED', 'TO_ARRANGE', 'CANCELLED', 'TO_CANCEL', 'RELATE', 'WAIT')",
|
|
|
+ 'A_STATUS'=>"enum('WAITING', 'NORMAL', 'MONITOR', 'WARNING', 'OFF_SOFT', 'OFF_HARD', 'DELETED')",
|
|
|
+ 'A_STATUS_INFO'=>"varchar(255) ",
|
|
|
+ 'A_ADM_COMPANY'=>'varchar(100) ','A_CLASSIFIED'=>'varchar(100) '
|
|
|
+
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+ //DEBUG_S(-3,'zapytanie',$sql,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ //$result=self::query($sql) or die1(' Problem ze sql '.$sql);
|
|
|
+ $result=self::show_tables($table);
|
|
|
+ while($h=self::fetch_array($result)) {
|
|
|
+
|
|
|
+ $res=self::show_tables($h[0].'_HIST');
|
|
|
+ if(self::num_rows($res)==0) {
|
|
|
+ //$CONNLOCAL->query($sql)
|
|
|
+ $sql='create table IF NOT EXISTS "'.$h[0].'_HIST"(
|
|
|
+ "ID" SERIAL PRIMARY KEY,
|
|
|
+ "ID_USERS2" integer NOT NULL,
|
|
|
+ "_action_time" timestamp without time zone,
|
|
|
+ "_action_type" character varying(16)
|
|
|
+ )';
|
|
|
+ DEBUG_S(-3,'create table hist '.$h[0],array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ $rescr=self::query($sql) or die1(' Problem ze sql '.$sql);
|
|
|
+
|
|
|
+
|
|
|
+ $sql=' CREATE INDEX "'.$h[0].'_ID_USERS2_index" ON "'.$h[0].'_HIST" ("ID_USERS2")';
|
|
|
+ $rescr=self::query($sql) or die1(' Problem ze sql '.$sql);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ } else DEBUG_S(-3,'Histable '.$h[0].'_HIST existied',null,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ //kolumny z bazy istniejace
|
|
|
+ // $sql_="describe `".$h[0]."`";
|
|
|
+ // $result_=self::query($sql_) or die1(' Problem ze sql '.$sql_);
|
|
|
+ $result_=self::describe_table($table);
|
|
|
+ $result__=self::describe_table($table."_HIST");
|
|
|
+ while($h__=self::fetch($result__)) {
|
|
|
+ $hist_exists[$h__->Field]=true;
|
|
|
+ }
|
|
|
+ while($h_=self::fetch($result_)) {
|
|
|
+ $table_exists[$h_->Field]=true;
|
|
|
+
|
|
|
+ if(empty($hist_exists[$h_->Field])) {
|
|
|
+ DEBUG_S(-3,'dane z tabeli',$h_,__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ //$sql="\n alter table \"".$h[0]."_HIST\" add \"".$h_->Field."\" ; " ;
|
|
|
+ //self::query($sql) or DEBUG_S(-3,' Problem ze sql '.$sql,array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::alter_add_struct($table."_HIST",$h_->Field," text default 'N/S;'");
|
|
|
+
|
|
|
+ $hist_exists[$h_->Field]=true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ foreach($USERS_COLUMN_INIT2['REQUEST_COLUMN'] as $tst_column=>$tst_type) {
|
|
|
+ if(empty($table_exists[$tst_column])) {
|
|
|
+ //$sql="\n alter table \"".$h[0]."\" add \"".$tst_column."\" ".$tst_type." ; " ;
|
|
|
+ //self::query($sql) or DEBUG_S(-3,' Problem ze sql '.$sql,array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::alter_add_struct($table,$tst_column,$tst_type);
|
|
|
+
|
|
|
+ if(empty($hist_exists[$tst_column])) {
|
|
|
+ // $sql="\n alter table \"".$h[0]."_HIST\" add \"".$tst_column."\" char(255) default 'N/S;' ; " ;
|
|
|
+ // self::query($sql) or DEBUG_S(-3,' Problem ze sql '.$sql,array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::alter_add_struct($table."_HIST",$tst_column,'char(255) default \'N/S;\'');
|
|
|
+ $hist_exists[$tst_column]=true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //Auto trigger funciton for history and edits
|
|
|
+
|
|
|
+
|
|
|
+ $sql = '
|
|
|
+ CREATE OR REPLACE FUNCTION "'.$table.'_HIST_func"() RETURNS TRIGGER AS $body$
|
|
|
+ DECLARE
|
|
|
+ v_old_data TEXT;
|
|
|
+ v_new_data TEXT;
|
|
|
+ v_currval INT;
|
|
|
+ BEGIN
|
|
|
+ /* If this actually for real auditing (where you need to log EVERY action),
|
|
|
+ then you would need to use something like dblink or plperl that could log outside the transaction,
|
|
|
+ regardless of whether the transaction committed or rolled back.
|
|
|
+ */
|
|
|
+
|
|
|
+ /* This dance with casting the NEW and OLD values to a ROW is not necessary in pg 9.0+ */
|
|
|
+
|
|
|
+ IF (TG_OP = \'UPDATE\') THEN
|
|
|
+ v_old_data := ROW(OLD.*);
|
|
|
+ v_new_data := ROW(NEW.*);
|
|
|
+
|
|
|
+ INSERT INTO "'.$table.'_HIST" ("ID_USERS2","_action_time","A_RECORD_UPDATE_AUTHOR","_action_type")
|
|
|
+ VALUES (NEW.'.$primary.',now(),session_user::TEXT,TG_OP);
|
|
|
+ v_currval := currval(\'"'.$table.'_HIST_ID_seq"\');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ';
|
|
|
+
|
|
|
+ foreach($hist_exists as $column=>$true) {
|
|
|
+ if($column=='ID') continue;
|
|
|
+ if($column=='ID_USERS2') continue;
|
|
|
+ if($column=='A_RECORD_UPDATE_AUTHOR') continue;
|
|
|
+ if($column=='A_RECORD_UPDATE_DATE') continue;
|
|
|
+ if($column=='A_RECORD_CREATE_DATE') continue;
|
|
|
+ if($column=='A_RECORD_CREATE_AUTHOR') continue;
|
|
|
+ if($column=='_action_time') continue;
|
|
|
+ if($column=='_action_type') continue;
|
|
|
+
|
|
|
+ $sql.='
|
|
|
+ IF cast(ROW(OLD."'.$column.'")as text)!=cast(ROW(NEW."'.$column.'")as text) THEN
|
|
|
+ UPDATE "'.$table.'_HIST" set "'.$column.'"=NEW."'.$column.'" where "ID"=v_currval;
|
|
|
+ -- ELSE
|
|
|
+ -- UPDATE "'.$table.'_HIST" set "'.$column.'"=\'N/S;\' where "ID"=v_currval;
|
|
|
+ END IF;
|
|
|
+ ';
|
|
|
+ }
|
|
|
+
|
|
|
+ $sql.='
|
|
|
+
|
|
|
+ -- UPDATE "'.$table.'" set "A_RECORD_UPDATE_DATE"=now(), "A_RECORD_UPDATE_AUTHOR"=session_user::TEXT where "'.$primary.'"=NEW."'.$primary.'";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ RETURN NEW;
|
|
|
+
|
|
|
+ ELSIF (TG_OP = \'INSERT\') THEN
|
|
|
+ v_new_data := ROW(NEW.*);
|
|
|
+
|
|
|
+ END IF;
|
|
|
+ END;
|
|
|
+ $body$
|
|
|
+ LANGUAGE plpgsql
|
|
|
+ SECURITY DEFINER
|
|
|
+
|
|
|
+ ';
|
|
|
+
|
|
|
+
|
|
|
+ DEBUG_S(-3,'Definied autohistory function',array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+
|
|
|
+
|
|
|
+ $res=self::show_create_trigger($table,$table.'_HIST_trigger');
|
|
|
+ if(self::num_rows($res)==0) {
|
|
|
+ $sql='CREATE TRIGGER "'.$table.'_HIST_trigger"
|
|
|
+ AFTER UPDATE ON "'.$table.'"
|
|
|
+ FOR EACH ROW EXECUTE PROCEDURE "'.$table.'_HIST_func"();';
|
|
|
+ DEBUG_S(-3,'Definied autohistory triggers',array($sql),__FILE__,__FUNCTION__,__LINE__);
|
|
|
+ self::query($sql) or die1($sql);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ self::query("CREATE OR REPLACE FUNCTION update_A_RECORD_UPDATE_DATE_column()
|
|
|
+ RETURNS TRIGGER AS $$
|
|
|
+ BEGIN
|
|
|
+ NEW.\"A_RECORD_UPDATE_DATE\" = now() at time zone 'utc';
|
|
|
+ NEW.\"A_RECORD_UPDATE_AUTHOR\" = session_user::TEXT;
|
|
|
+
|
|
|
+ RETURN NEW;
|
|
|
+ END;
|
|
|
+ $$ language 'plpgsql';");
|
|
|
+
|
|
|
+
|
|
|
+ $res=self::show_create_trigger($table,'update_A_RECORD_UPDATE_DATE');
|
|
|
+ if(self::num_rows($res)==0) {
|
|
|
+ self::query("CREATE TRIGGER \"update_A_RECORD_UPDATE_DATE\" BEFORE UPDATE
|
|
|
+ ON \"".$table."\" FOR EACH ROW EXECUTE PROCEDURE
|
|
|
+ update_A_RECORD_UPDATE_DATE_column();");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ self::query("CREATE OR REPLACE FUNCTION create_A_RECORD_UPDATE_DATE_column()
|
|
|
+ RETURNS TRIGGER AS $$
|
|
|
+ BEGIN
|
|
|
+ NEW.\"A_RECORD_CREATE_DATE\" = now() at time zone 'utc';
|
|
|
+ NEW.\"A_RECORD_CREATE_AUTHOR\" = session_user::TEXT;
|
|
|
+
|
|
|
+ RETURN NEW;
|
|
|
+ END;
|
|
|
+ $$ language 'plpgsql';");
|
|
|
+
|
|
|
+
|
|
|
+ $res=self::show_create_trigger($table,'create_A_RECORD_UPDATE_DATE');
|
|
|
+ if(self::num_rows($res)==0) {
|
|
|
+ self::query("CREATE TRIGGER \"create_A_RECORD_UPDATE_DATE\" BEFORE INSERT
|
|
|
+ ON \"".$table."\" FOR EACH ROW EXECUTE PROCEDURE
|
|
|
+ create_A_RECORD_UPDATE_DATE_column();");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //! IMPORT_SORT_DESC_INFO
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|