Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Baser {
- const DBNAME = 'fbdb8.dat';
- public static $db;
- private $sq;
- private $user;
- public $debug_mode=false;
- public $debug_array=array();
- //-----------------------------------------
- private function declear() {
- for ($i=0; $i<count($this->debug_array); $i++) {
- unset($this->debug_array[$i]);
- }
- }
- //-----------------------------------------
- private function dewr($s) {
- if ($this->debug_mode)
- $this->debug_array[]=$s;
- }
- //-----------------------------------------
- public function deshow() {
- $inf = '<pre>'.print_r($this->debug_array, true).'</pre>';
- $this->declear();
- return $inf;
- }
- //-----------------------------------------
- public static function getInstance($user) {
- if (!self::$db)
- self::$db = new Baser($user);
- return self::$db;
- }
- //-----------------------------------
- private function __construct($user) {
- $this->user= ($user) ? $user : '0000000';
- if (!$this->sq = new SQLiteDatabase(Baser::DBNAME)) {
- throw new Exception('Error opening SQLite', 5006);
- }
- $this->checkBase();
- }
- //-----------------------------------
- // check if base tables exist
- //-----------------------------------
- private function checkBase() {
- $rez = $this->sq->query('select * from fbs_likes_users');
- if (!$rez) {
- $crtabs=array();
- $crtabs[]='CREATE TABLE fbs_likes_users ('.
- 'fb_base_user_id varchar(100),'.
- 'fb_like_id varchar(100),'.
- 'fb_reluser_id varchar(100))';
- $this->processSqls($crtabs);
- }
- }
- //------------------------------------------------------------------
- // utils
- //------------------------------------------------------------------
- //-----------------------------------
- // retrieve table contents
- //-----------------------------------
- public function selectAll($table, $flds='', $curr_user=true) {
- $selflds='*';
- if ($flds!=='') {
- if (is_array($flds)) {
- $selflds=implode(',',$flds);
- } else {
- $selflds=$flds;
- }
- }
- $ufilter = $curr_user ? ' where fb_base_user_id=\''.$this->user.'\'' : '';
- $rez = $this->sq->arrayQuery("select $selflds from $table".$ufilter, SQLITE_ASSOC);
- if (!$rez) {
- $rez=array();
- $rez['err'] = sqlite_error_string($this->sq->lastError());
- }
- return $rez;
- }
- //-----------------------------------
- // get Query
- //-----------------------------------
- public function getQuery($sql) {
- $rez = $this->sq->arrayQuery($sql, SQLITE_ASSOC);
- if (!$rez) {
- $rez = array();
- $rez['err'] = sqlite_error_string($this->sq->lastError());
- }
- return $rez;
- }
- //-----------------------------------
- // check Query
- //-----------------------------------
- public function checkQuery($sql) {
- $rez = $this->getQuery($sql);
- return is_array($rez) && !isset($rez['err']);
- }
- //-----------------------------------
- // fetch field
- //-----------------------------------
- public function fetchField($table, $fld, $curr_user=true) {
- $inf = $this->selectAll($table, $fld, $curr_user);
- if (isset($inf['err'])) {
- return $inf;
- }
- $f = create_function('$val', 'return $val["'.$fld.'"];');
- $inf4 = array_map($f,$inf);
- return $inf4;
- }
- //-----------------------------------
- // fetch query field
- //-----------------------------------
- public function fetchQueryField($sql, $fld) {
- $inf = $this->getQuery($sql);
- if (isset($inf['err'])) {
- return $inf;
- }
- $f = create_function('$val', 'return $val["'.$fld.'"];');
- $inf4 = array_map($f,$inf);
- return $inf4;
- }
- //-----------------------------------
- // get table fields
- //-----------------------------------
- public function getTableFields($tabname) {
- $rez = $this->sq->query("select * from $tabname", SQLITE_ASSOC, $errstring);
- if (!$rez) {
- $s = array();
- $s['err'] = 'error: '.$errstring."\r\n";
- return $s;
- }
- $flds = array();
- for ($i=0; $i<$rez->numFields(); $i++) {
- $flds[] = $rez->fieldName($i);
- }
- return $flds;
- }
- //-----------------------------------
- // execute query
- //-----------------------------------
- public function execQuery($sql) {
- $errstr = array();
- if (!$this->sq->queryExec($sql, $error)) {
- $errstr['inf'] = $error.':'.sqlite_error_string($this->sq->lastError())."\r\n".
- str_repeat('=',30)."\r\n".$sql."\r\n";
- $errstr['err'] = 'error executing query';
- }
- else
- $errstr['inf'] = "successfully executed\r\n".
- "changes: ".$this->sq->changes()."\r\n".
- "last insert id: ".$this->sq->lastInsertRowid()."\r\n".
- str_repeat('=',30)."\r\n".$sql."\r\n";
- return $errstr;
- }
- //-----------------------------------
- // process sqls
- //-----------------------------------
- private function processSqls($sqls) {
- $errstr = array();
- $errstr['inf']='';
- for ($i=0; $i<count($sqls); $i++) {
- if (!$this->sq->queryExec($sqls[$i], $error)) {
- $errstr['inf'].=$error.':'.sqlite_error_string($this->sq->lastError())."\r\n".
- str_repeat('=',30)."\r\n".
- $sqls[$i]."\r\n";
- $errstr['err']='error executing query';
- }
- else
- $errstr['inf'].="successfully executed, changes:\r\n ".$this->sq->changes()."\r\n".
- 'last insert id: '.$this->sq->lastInsertRowid()."\r\n".
- str_repeat('=',30)."\r\n".
- $sqls[$i]."\r\n";
- }
- return $errstr;
- }
- //-----------------------------------
- // draw table
- //-----------------------------------
- public function drawTab($tabname, $curr_user=true) {
- $ufilter = $curr_user ? ' where fb_base_user_id=\''.$this->user.'\'' : '';
- $rez = $this->sq->query("select * from $tabname".$ufilter, SQLITE_BOTH, $errstring);
- if (!$rez) {
- $s = "drawTab error: $errstring\r\n";
- return $s;
- }
- $tab =
- "<table cellspacing=\"0\" cellpadding=\"3\" border=\"1\" bordercolor=\"#888\" >";
- $tab.='<tr>';
- for ($i=0; $i<$rez->numFields(); $i++) {
- $tab.='<td>'.$rez->fieldName($i).'</td>';
- }
- $tab.='</tr>';
- while ($rez->valid()) {
- $row = $rez->current();
- $tab.='<tr>';
- for ($i=0; $i<$rez->numFields(); $i++) {
- $tab.='<td>'.$row[$i].'</td>';
- }
- $tab.='</tr>';
- $rez->next();
- }
- $tab.='</table>';
- return $tab;
- }
- //-----------------------------------
- // draw list
- //-----------------------------------
- public function drawList($tabname, $curr_user=true) {
- $ufilter = $curr_user ? ' where fb_base_user_id=\''.$this->user.'\'' : '';
- $rez = $this->sq->query("select * from $tabname".$ufilter, SQLITE_BOTH, $errstring);
- if (!$rez) {
- $s = 'drawList error: '.$errstring."\r\n";
- return $s;
- }
- $inf="<h3>$tabname field list</h3><ul>";
- for ($i=0; $i<$rez->numFields(); $i++) {
- $inf.= '<li>'.$rez->fieldName($i).'</li>';
- }
- $inf.='</ul>';
- $inf.=str_repeat('-',30).'<br/>';
- while ($rez->valid()) {
- $row = $rez->current();
- $inf.='<ul>';
- for ($i=0; $i<$rez->numFields(); $i++) {
- $inf.= '<li>'.$row[$i].'</li>';
- }
- $inf.='</ul>';
- $inf.=str_repeat('-',30).'<br/>';
- $rez->next();
- }
- return $inf;
- }
- //-----------------------------------
- // show table info
- //-----------------------------------
- public function drawTableInfo($tabname) {
- $s = "table info:{$tabname}\r\n";
- $rez = $this->sq->query("select * from $tabname", SQLITE_BOTH, $errstring);
- if (!$rez) {
- $s.= "drawTableInfo error: $errstring\r\n";
- return $s;
- }
- $s.='number of fields: '.$rez->numFields()."\r\n";
- $s.='number of rows: '.$rez->numRows()."\r\n";
- return $s;
- }
- //------------------------------------------------------------------
- // main operations
- //------------------------------------------------------------------
- //-----------------------------------
- // fb record structure processing
- //-----------------------------------
- private function processFields($arr, $prefix, $fldtype=true, $keys=true) {
- $flds = array();
- foreach ($arr as $key=>$value) {
- //-------------multiple subentities
- if (is_integer($key)) {
- /*if ($keys)
- $flds[] = $prefix.'_external '.($fldtype?'int':'')."\r\n";
- else
- $flds[] = '254'."\r\n";*/
- break;
- }
- $type = gettype($value);
- if (($type=='object') || ($type=='resource') || ($type=='NULL'))
- continue;
- if ($type=='array') {
- $flds = array_merge($flds, $this->processFields($value, $prefix.'_'.$key,
- $fldtype, $keys));
- continue;
- }
- switch ($type) {
- case "boolean": $dbtype="boolean";break;
- case "integer": $dbtype="int";break;
- case "double": $dbtype="double";break;
- case "string": $dbtype="varchar(500)"; break;
- }
- if ($keys)
- $flds[]=$prefix.'_'.$key.' '.($fldtype ? $dbtype:'')."\r\n";
- else
- $flds[]=(($type=='string')? '\'':'').
- ((($type=='string') || $value) ?
- sqlite_escape_string($value) : '0').
- (($type=='string')? '\'':'')."\r\n";
- }
- return $flds;
- }
- //-----------------------------------
- // create fb structure table
- //-----------------------------------
- public function createTable($inf, $tabname, $checkifexists=false) {
- if (!is_array($inf)) {
- $z=array();
- $z['err']='cannot create table. argument is empty or not array';
- return $z;
- }
- $flds=array();
- if ($checkifexists) {
- $rez = $this->sq->query("select * from $tabname");
- if ($rez) {
- $z = array();
- $z['inf']='table '.$tabname.' already exists';
- return $z;
- }
- }
- if (array_key_exists('data', $inf)) {
- if (isset($inf['data'][0])){
- for ($i=0; $i<count($inf['data']);$i++) {
- $infz= $inf['data'][$i];
- $flds=array_merge($flds,
- $this->processFields($infz, 'fb'));
- }
- $flds = array_unique($flds);
- $inf=$inf['data'][0];
- } else {
- $z=array();
- $z['err']='fb query returns empty result';
- return $z;
- }
- } else {
- $flds = $this->processFields($inf, 'fb');
- }
- //---------------------------
- $dropstr = 'drop table '.$tabname;
- $tabstr = 'create table '.$tabname.'( fb_base_user_id varchar(100), '."\r\n".
- implode(',',$flds).')';
- $this->execQuery($dropstr);
- $errstr = $this->execQuery($tabstr);
- return $errstr;
- }
- //-----------------------------------
- // check if field exists
- //-----------------------------------
- private function field_exists ($val) {
- $val = trim($val);
- return in_array($val, $GLOBALS['gl_flds']);
- }
- //-----------------------------------
- // fill fb table
- //-----------------------------------
- public function fillTable($inf, $tabname, $clear=true) {
- if (!is_array($inf)) {
- $z = array();
- $z['err']='argument is empty or not array';
- return $z;
- }
- $rez = $this->sq->query("select * from $tabname");
- if (!$rez) {
- $crinf = $this->createTable($inf, $tabname);
- if (isset($crinf['err']))
- return $crinf;
- }
- //--------------------------------------------------
- $gl_flds = $this->getTableFields($tabname);
- if (isset($gl_flds['err']))
- return $gl_flds;
- $GLOBALS['gl_flds'] = $gl_flds;
- //--------------------------------------------------
- $sqls = array();
- if ($clear)
- $sqls[] = "delete from $tabname where fb_base_user_id='{$this->user}'";
- $zinf = array();
- //--------------------------- [node]->[data]->[0,1,2,3,4]
- if (array_key_exists('data', $inf)) {
- for ($i=0; $i<count($inf['data']); $i++) {
- $zinf[]= $inf['data'][$i];
- }
- } else {
- //------------------------[node]->[0,1,2,3,4]
- if (isset($inf[0])) {
- for ($i=0; $i<count($inf); $i++) {
- $zinf[]=$inf[$i];
- }
- }
- else
- $zinf[] = $inf;
- }
- //---------------------------
- for ($i=0; $i<count($zinf); $i++) {
- $f_flds = $this->processFields($zinf[$i],'fb', false);
- $f_values = $this->processFields($zinf[$i],'fb', false, false);
- if ((!is_array($f_flds)) || (!is_array($f_values)))
- continue;
- $f_check = array_combine($f_values, $f_flds );
- $f_check = array_flip( array_filter( $f_check, array($this,'field_exists')));
- $insstr = 'insert into '.$tabname.' (fb_base_user_id, '."\r\n".
- implode(',', array_keys($f_check)).
- ') values ( \''.$this->user.'\', '."\r\n".
- implode(',', array_values($f_check)).')';
- $sqls[]= $insstr;
- }
- $errstr = $this->processSqls($sqls);
- return $errstr;
- }
- //-----------------------------------
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement