Database Access
From Nephtali Documentation
Nephtali wraps the functionality of PDO, PHP's fantastic data-access abstraction layer. The functions are broken up into calls that alter data (actions) and calls that merely read data (sources) to provide better security, and prepared statements are used by default, which also enhances security.
Why No ORM or Database Abraction Layer
First, Nephtali takes most of its cues from Functional Programming(FP), not Object Oriented Programming (OOP). Many values in Nephtali are immutable (you can't change pipes, ports, vals, requests, or outputs once they've been set), the work is carried out by functions that return the work they've done (i.e., side effects are limited), and arrays are the data containers of choice, not objects.
Second, there's a simplicity to using Nephtali's PDO-powered functions that translates well across databases, and provides great bang for the buck. That is to say, the core of Nephtali all fits within one file, and yet it provides the ability to break up database work into calls that alter data and calls that merely read data for increased security, allows for easy implementation of transactions, facilitates simple updates, inserts, and deletes to tables, and more all through just a few function calls. The limited over-head also keeps performance at very high levels.
Additionally, more and more data operations are directed at web services, and an abstraction layer that functions well across the various DB and WS options must be quite large. Nephtali seeks simplicity for the developer AND for the general code-base to enhance experience and security.
SQL Source Functions
Nephtali's source functions provide convenient access to data whilst keeping you in complete control. The example below demonstrates how the Nephtali homepage retrieves the sites that use Nephtali. Of note, it also displays a contrived example of how one could implement an A/B view of the same data.
Here's the HTML:
<h2>Sites using Nephtali</h2>
<ul id="nephtali_sites">
<!--pipe:sites-->
<!--view:a-->
<!--data-->
<li>
<h4><a href="{link}">{title}</a></h4>
<p>{description}</p>
</li>
<!--data-->
<!--view:a-->
<!--view:b-->
<!--data-->
<li>
<h4 style="background-color:#CCC;"><a href="{link}">{title}</a></h4>
<p>{description}</p>
</li>
<!--data-->
<!--view:b-->
<!--view:empty-->
<li>
<h4>There are no Nephtali sites</h4>
<p>What a sad day :(</p>
</li>
<!--view:empty-->
<!--view:error-->
<li>
<h4>There has been an error</h4>
<p>Please try back later</p>
</li>
<!--view:error-->
<!--pipe:sites-->
</ul>
Here's the PHP:
n\pipe\register(
$name = 'sites',
$function = function($markup)
{
if (count($rows = n\sql\source\query($query = 'SELECT * FROM sites')))
{
return n\view\render($name = (rand(0,1)?('a'):('b')), $markup, $rows);
}
return n\view\render($name = 'empty', $markup);
}
);
Did you see the sql code, you might have missed it? The sql select occurs using the code n\sql\source\query($query = 'SELECT * FROM sites'), that's it.
If you don't send in any connection parameters, the connection info is automatically taken from nconfig.php, as is shown in the example below:
n\config\save(
$mode = 'dev',
$dev = array(
'sql_action_dsn' => 'sqlite:/home/lite.sqlite',
'sql_action_username' => 'username',
'sql_action_password' => 'password',
'sql_source_dsn' => 'sqlite:/home/lite.sqlite',
'sql_source_username' => 'username',
'sql_source_password' => 'password',
'encryption_key' => 'ENCRYPTION_KEY',
'encryption_salt' => 'ENCRYPTION_SALT'
),
$test = array(
'sql_action_dsn' => 'mysql:host=localhost;dbname=DBNAME',
'sql_action_username' => 'USERNAME',
'sql_action_password' => 'PASSWORD',
'sql_source_dsn' => 'mysql:host=localhost;dbname=DBNAME',
'sql_source_username' => 'USERNAME',
'sql_source_password' => 'PASSWORD',
'encryption_key' => 'ENCRYPTION_KEY',
'encryption_salt' => 'ENCRYPTION_SALT'
),
$prod = array(
'sql_action_dsn' => 'mysql:host=localhost;dbname=DBNAME',
'sql_action_username' => 'USERNAME',
'sql_action_password' => 'PASSWORD',
'sql_source_dsn' => 'mysql:host=localhost;dbname=DBNAME',
'sql_source_username' => 'USERNAME',
'sql_source_password' => 'PASSWORD',
'encryption_key' => 'ENCRYPTION_KEY',
'encryption_salt' => 'ENCRYPTION_SALT'
)
);
However, if you need to send in the connection info (maybe you're using more than one DB), then you merely pass it in as needed, as in the example below:
n\pipe\register(
$name = 'sites',
$function = function($markup)
{
if (count($rows = n\sql\source\query($query = 'SELECT * FROM sites', $inputs = array(), $dbh = connect($dsn = mysql:host=localhost;dbname=DBNAME, $username = 'username', $password = 'password'))))
{
return n\view\render($name = (rand(0,1)?('a'):('b')), $markup, $rows);
}
return n\view\render($name = 'empty', $markup);
}
);

