Skip to content

Instantly share code, notes, and snippets.

@fastcodecoq
Created May 21, 2014 09:13
Show Gist options
  • Select an option

  • Save fastcodecoq/4266557f00b13c99f048 to your computer and use it in GitHub Desktop.

Select an option

Save fastcodecoq/4266557f00b13c99f048 to your computer and use it in GitHub Desktop.

Archivos desarrollados para las correciones de inmueblealaventa.com 21 de Mayo de 2014.

Gomosoft

<?php
require_once("../config.inc.php");
class db_controller_exception extends Exception{};
class mysqlity{
public $db;
protected $table;
protected $compiled;
function __construct($table = "inmueble"){
$this->db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$this->table = $table;
return $this;
}
public function exists($table_name){
$rs = $this->db->query("SELECT * FROM {$table_name} LIMIT 1");
return $this->db->affected_rows > 0;
}
public function create($table_name, $vars, $primary_key = NULL, $force = NULL, $engine = "InnoDB"){
if($primary_key === NULL)
$primary_key = "";
else
$primary_key = ", PRIMARY KEY ({$primary_key})";
$db = $this->db;
$force = "IF NOT EXISTS";
$query = "CREATE TABLE {$force} `{$table_name}` ({$vars} {{primary_key}}) engine={$engine}";
$query = str_replace("{{primary_key}}", $primary_key, $query);
return array("success" => $db->query($query), "table" => $table_name);
}
public function error(){
return $this->db->error;
}
public function save($varss, $table = "shorts"){
$db = $this->db;
$query = array();
$query[] = "INSERT INTO";
$query[] = "`{$table}`";
$vars = array();
$vals = array();
foreach ($varss as $row => $val)
{
$vars[] = "`" . $row . "`";
if(!$this->compile($val))
throw new db_controller_exception("Tipo de dato no soportado solo enteros y cadenas", 77);
else
$vals[] = $this->compiled;
}
$query[] = "(" . implode(",", $vars) . ")";
$query[] = "VALUES";
$query[] = "(" . implode(",", $vals) . ")";
$query = implode(" ", $query);
return $db->query($query);
}
public function compile($val){
if(preg_match("{{d}}", $val))
{ $this->compiled = (int) $val; return true;}
if(preg_match("{{s}}", $val))
{ $this->compiled = "'" . addslashes(strip_tags(str_replace("{{s}}", "", $val))) . "'"; return true;}
if(preg_match("{{c}}", $val))
{ $this->compiled = (strlen("'" . addslashes(strip_tags(str_replace("{{c}}", "", $val))) . "'") === 3) ? true : false; return true;}
if(preg_match("{{f}}", $val))
{ $this->compiled = (float) $val; return true;}
throw new db_controller_exception("Tipo de dato no soportado solo enteros y cadenas", 77);
}
public function find($params = NULL, $table = NULL, $pointer = NULL){
$db = $this->db;
$query = array();
$query[] = "SELECT";
$query[] = ( isset($params["vars"]) ) ? $params["vars"] : "*";
$query[] = ( $table != NULL ) ? "FROM `" . TB_PREFIX . "{$table}`" : "FROM `" . TB_PREFIX . "{$this->table}`";
$query[] = ( isset($params["where"]) ) ? "WHERE {$params["where"]}" : "";
$query = implode(" ", $query );
if($params === NULL)
{
if($query === NULL)
{
$rs = array();
$result = $db->query( $query );
}
else
{
$query .= $pointer;
$query = str_replace("?", $this->compiled , $query);
$result = $db->query($query);
}
$rs = array();
while($row = $result->fetch_array(MYSQLI_ASSOC))
$rs[] = $row;
mysqli_free_result($result);
return $rs;
}
else{
$stmt = $db->stmt_init();
if( $stmt->prepare( $query ) ){
return $stmt;
}
else
throw new db_controller_exception("No se pudo preparar el query [{$db->error}]", 52);
}
}
public function update($params = NULL, $table = NULL){
$db = $this->db;
$query = array();
$query[] = "UPDATE";
$query[] = ( $table != NULL ) ? TB_PREFIX . "{$table}" : TB_PREFIX . "{$this->table}";
$query[] = ( isset($params["vars"]) ) ? $params["vars"] : "*";
$query[] = ( isset($params["where"]) ) ? "WHERE {$params["where"]}" : "";
if($params === NULL)
return $db->query($query);
else{
if( $stmt = $db->prepare( implode(" ", $query )) ){
$stmt->bind_param($params["flags"], $params["params"]);
if($stmt->execute())
return $stmt->get_result();
else
throw new db_controller_exception("No se pudo hacer update [{$db->error}]", 101);
}
else
throw new db_controller_exception("No se pudo preparar el query [{$db->error}]", 52);
}
}
public function remove($params = NULL, $table = NULL){
$db = $this->db;
$query = array();
$query[] = "DELETE";
$query[] = ( $table != NULL ) ? "FROM " . TB_PREFIX . " {$table}" : TB_PREFIX . "{$this->table}";
$query[] = ( isset($params["where"]) ) ? "WHERE {$params["where"]}" : "";
if($params === NULL)
return $db->query(implode(" ", $query));
else{
if( $stmt = $db->prepare( implode(" ", $query )) ) {
$stmt->bind_param($params["flags"], $params["params"]);
if(!$stmt->execute())
throw new db_controller_exception("No se pudo hacer remove [{$db->error}]", 132);
}
else
throw new db_controller_exception("No se pudo preparar el query [{$db->error}]", 52);
}
}
}
<?php
require_once("../libs/db.controller.php");
class m2sql{
protected $db;
protected $col;
public function __construct(){
$this->db = new mysqlity;
$con = new MongoClient();
$db = $con->iav;
$this->col = $db->inmuebles;
}
public function merge(){
$inmbls = iterator_to_array($this->col->find());
$coun = 0;
foreach($inmbls as $inm){
$codigo = $this->db->compile( $inm["codigo"] . "{{d}}");
$query = " WHERE codigo = ? LIMIT 1";
$must_saved = (count($this->db->find(null, "inmueble", $query)) > 0) ? false : true;
if($must_saved )
{
$values = $vars = array();
foreach($inm as $key => $val)
{
$val = $val . "";
switch ($key) {
case 'codigo':
case 'tipo_neg':
case 'ciudad':
case 'barrio':
case 'tipo_inm':
case 'plan':
case 'numvisitas':
case 'estado':
case 'destacado':
$val = (empty($val)) ? 0 : (int) $val;
break;
default:
$val = "'{$val}'";
break;
}
if($key != "_id" AND $key != "id" AND $key != "image")
{
$vars[] = "`" . $key . "`";
$values[] = $val;
}
}
$vars = implode(",", $vars);
$values = implode(",", $values);
$query = "INSERT INTO `inmueble` ({{vars}}) VALUES ({{values}})";
$query = str_replace("{{vars}}", $vars, $query);
$query = str_replace("{{values}}", $values, $query);
echo addslashes($query);
$this->db->db->query($query) or die($this->db->db->error);
echo $this->db->db->affected_rows;
}
}
}
}
try{
$app = new m2sql;
$app->merge();
}
catch(Exception $e){
json_encode(array("error" => $e->getMessage()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment