Jump to content
php.lv forumi

PDO with MySQL


777

Recommended Posts

Sveicinaati vecie un jaunie koderi, sen nebiju te radijies :)

 

Laikam jautaajums buus vecajiem guru.

 

Kadam varbuut ir aizmetaajies PDO klasse darbam ar mysql?

 

Atradu interneta shito, bet nebiju lidz shim nemies ar PDO, biju lietojis savu pierasto Mysql db klassi, tachu tagad grasos nemties ar vienu milzigo projektu, un lai nezaudeetu performance, domaju izmantot PDO.

 

Paldies jau ieprieksh, par Jusu komentariem, priekshlikumiem un iebildumiem.

 

class sdb
{
static private $PDOInstance;

public function __construct($dsn, $username=false, $password=false, $driver_options=false)
{
	if(!self::$PDOInstance)
	{
		try
		{
			self::$PDOInstance = new PDO($dsn, $username, $password, $driver_options);
		}
		catch(PDOException $e)
		{
			die("PDO CONNECTION ERROR: " . $e->getMessage() . "<br/>");
		}
	}
	return self::$PDOInstance;
}

public function beginTransaction()
{
	return self::$PDOInstance->beginTransaction();
}

public function commit()
{
	return self::$PDOInstance->commit();
}

public function errorCode()
{
	return self::$PDOInstance->errorCode();
}

public function errorInfo()
{
	return self::$PDOInstance->errorInfo();
}

public function exec($statement)
{
	return self::$PDOInstance->exec($statement);
}

public function getAttribute($attribute)
{
	return self::$PDOInstance->getAttribute($attribute);
}

public function getAvailableDrivers()
{
	return Self::$PDOInstance->getAvailableDrivers();
}

public function lastInsertId($name)
{
	return self::$PDOInstance->lastInsertId($name);
}

public function prepare ($statement, $driver_options=false)
{
	if(!$driver_options) $driver_options=array();
	return self::$PDOInstance->prepare($statement, $driver_options);
}

public function query($statement)
{
	return self::$PDOInstance->query($statement);
}

public function queryFetchAllAssoc($statement)
{
	return self::$PDOInstance->query($statement)->fetchAll(PDO::FETCH_ASSOC);
}

public function queryFetchRowAssoc($statement)
{
	return self::$PDOInstance->query($statement)->fetch(PDO::FETCH_ASSOC);
}

public function queryFetchColAssoc($statement)
{
	return self::$PDOInstance->query($statement)->fetchColumn();
}

public function quote ($input, $parameter_type=0)
{
	return self::$PDOInstance->quote($input, $parameter_type);
}

public function rollBack()
{
	return self::$PDOInstance->rollBack();
}

public function setAttribute($attribute, $value)
{
	return self::$PDOInstance->setAttribute($attribute, $value);
}
}

Link to comment
Share on other sites

Kāds registry ?

Ja izmantojam ietvaru, izmantojam ietvaru DB abstrakcijas klasi, a tā neredzu jēgu wrappot PDO - pie inicializācijas iebāžam konteinerī (aļa registry pattern) ar visu DSN un tālāk strādājam ar tiešu. Var uztaisīt delegāta / singleton miksējumu un rullēt tā.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...