Skip to content

Commit 96b07a8

Browse files
committed
updating local files
0 parents  commit 96b07a8

File tree

466 files changed

+43908
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

466 files changed

+43908
-0
lines changed

LICENSE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This is **multi-licensed**. You may choose to use it under **one of the following licenses**:
2+
3+
- **BSD 3-Clause License** or
4+
- **Apache License 2.0** or
5+
- **GNU LGPL v3** or
6+
- **[HLNC License](http://bloxtor.com/LICENSE_HLNC.md)**
7+
8+
Select the license that best fits your needs.
9+
10+
**This is 100% open to your needs!**
11+
12+
© 2025 [Bloxtor](http://bloxtor.com) and [Joao Pinto](http://jplpinto.com)

README.md

Lines changed: 462 additions & 0 deletions
Large diffs are not rendered by default.

examples/config.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
include_once dirname(__DIR__) . "/lib/app.php";
3+
include_once get_lib("db.DB");
4+
5+
//GET DRIVER TYPE
6+
$type = !empty($_GET["type"]) ? $_GET["type"] : "mysql"; //values: mysql or pg or mssql
7+
$password = "";
8+
9+
//CREATE DRIVER OBJ
10+
$DBDriver = DB::createDriverByType($type); //to get all available types call: DB::getAvailableDriverClassNames(); and then for each item call: DB::getDriverTypeByClassName($DBDriver_class);
11+
12+
//PREPARE DRIVER CREDENTIALS
13+
//$dsn = "mysql:Server=localhost;dbname=test;";
14+
//$options = $DBDriver->parseDSN($dsn);
15+
//$options["username"] = "root";
16+
//$options["password"] = "";
17+
//or
18+
19+
switch ($type) {
20+
case "mysql":
21+
$options = array(
22+
"extension" => "mysqli", //to get all the extensions by type call: DB::getAllExtensionsByType();
23+
"host" => "localhost",
24+
"port" => "", //if empty, set port automatically by default
25+
"db_name" => "test",
26+
"username" => "root",
27+
"password" => $password,
28+
"persistent" => true, //create persistent connection
29+
"new_link" => false, //create new_link on connection
30+
"reconnect" => false, //reconnect if DB is disconnect automatically
31+
"encoding" => "utf8", //to get all the encodings by type call: DB::getAllDBConnectionEncodingsByType();
32+
"schema" => "", //Schema of DB - if apply
33+
"odbc_data_source" => "", //ODBC Data Source - only used if the extension is ODBC
34+
"odbc_driver" => "", //ODBC Driver - only used if the extension is ODBC and odbc_data_source is not defined
35+
"extra_dsn" => "", //Extra settings for the dsn - only used if the extension is ODBC or PDO
36+
"extra_settings" => "", //PDO settings - only used if the extension is PDO. If driver is MSSqlDB is also used if extension is sqlsrv
37+
); //to get the dsn based in options call: $DBDriver->getDSN($options);
38+
break;
39+
40+
case "pg":
41+
$options = array(
42+
"extension" => "pg", //to get all the extensions by type call: DB::getAllExtensionsByType();
43+
"host" => "localhost",
44+
"port" => "", //if empty, set port automatically by default
45+
"db_name" => "test",
46+
"username" => "jplpinto",
47+
"password" => $password,
48+
"persistent" => true, //create persistent connection
49+
"new_link" => false, //create new_link on connection
50+
"reconnect" => false, //reconnect if DB is disconnect automatically
51+
"encoding" => "utf8", //to get all the encodings by type call: DB::getAllDBConnectionEncodingsByType();
52+
"schema" => "", //Schema of DB - if apply
53+
"odbc_data_source" => "", //ODBC Data Source - only used if the extension is ODBC
54+
"odbc_driver" => "", //ODBC Driver - only used if the extension is ODBC and odbc_data_source is not defined
55+
"extra_dsn" => "", //Extra settings for the dsn - only used if the extension is ODBC or PDO
56+
"extra_settings" => "", //PDO settings - only used if the extension is PDO. If driver is MSSqlDB is also used if extension is sqlsrv
57+
); //to get the dsn based in options call: $DBDriver->getDSN($options);
58+
break;
59+
60+
case "mssql":
61+
$options = array(
62+
"extension" => "pdo", //to get all the extensions by type call: DB::getAllExtensionsByType();
63+
"host" => "localhost",
64+
"port" => "", //if empty, set port automatically by default
65+
"db_name" => "master",
66+
"username" => "jplpinto",
67+
"password" => $password,
68+
"persistent" => true, //create persistent connection
69+
"new_link" => false, //create new_link on connection
70+
"reconnect" => false, //reconnect if DB is disconnect automatically
71+
"encoding" => "utf8", //to get all the encodings by type call: DB::getAllDBConnectionEncodingsByType();
72+
"schema" => "", //Schema of DB - if apply
73+
"odbc_data_source" => "", //ODBC Data Source - only used if the extension is ODBC
74+
"odbc_driver" => "ODBC Driver 17 for SQL Server", //ODBC Driver - only used if the extension is ODBC and odbc_data_source is not defined
75+
"extra_dsn" => "TrustServerCertificate=yes;", //Extra settings for the dsn - only used if the extension is ODBC or PDO
76+
"extra_settings" => "", //PDO settings - only used if the extension is PDO. If driver is MSSqlDB is also used if extension is sqlsrv
77+
); //to get the dsn based in options call: $DBDriver->getDSN($options);
78+
break;
79+
}
80+
81+
//SET DRIVER CREDENTIALS
82+
$DBDriver->setOptions($options);
83+
84+
//SET SOME STYLING
85+
$style = '<style>
86+
select {background:#eee; border:1px solid #ccc; border-radius:3px; padding:3px 2px;}
87+
h1 {margin-bottom:0; text-align:center;}
88+
h4 {text-align:center;}
89+
h5 {font-size:1em; margin:40px 0 0; font-weight:bold;}
90+
p {margin:0 0 20px; text-align:center;}
91+
92+
.note {text-align:center;}
93+
.note span {text-align:center; margin:0 20px 20px; padding:10px; color:#aaa; border:1px solid #ccc; background:#eee; display:inline-block; border-radius:3px;}
94+
95+
.error {margin:20px 0; text-align:center; color:red;}
96+
97+
.code {display:block; margin:10px 0; padding:0; background:#eee; border:1px solid #ccc; border-radius:3px; position:relative;}
98+
.code:before {content:"php"; position:absolute; top:5px; left:5px; display:block; font-size:80%; opacity:.5;}
99+
.code.sql:before {content:"sql";}
100+
.code.status:before {content:"status";}
101+
.code.input:before {content:"input";}
102+
.code.output:before {content:"output";}
103+
.code textarea {width:100%; height:300px; padding:30px 10px 10px; display:inline-block; background:transparent; border:0; resize:vertical; font-family:monospace;}
104+
.code.short textarea {height:120px;}
105+
.code.one-line textarea {height:60px;}
106+
.code.sql textarea {height:200px;}
107+
.code.sql.short textarea {height:100px;}
108+
.code.sql.one-line textarea {height:60px;}
109+
</style>';
110+
?>

examples/crud_action.php

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
<?php
2+
include_once __DIR__ . "/config.php";
3+
4+
$table_name = "a_product_x";
5+
6+
echo $style;
7+
8+
echo "<h1>DB CRUD Actions</h1>
9+
<p>Executes CRUD actions through objects/arrays</p>";
10+
11+
echo '<h4>Choose a DB driver:
12+
<select onChange="document.location=\'?type=\' + this.value;">';
13+
14+
$types = DB::getAllDriverLabelsByType();
15+
foreach ($types as $id => $label)
16+
echo "<option value='$id'" . ($id == $type ? " selected" : "") . ">$label</option>";
17+
echo "</select></h4>";
18+
19+
echo '<div class="note">
20+
<span>
21+
The system will create 2 temp tables. Then will perform some CRUD actions on these tables and show the correspondent code and results.<br/>
22+
At the end will drop/remove the temp tables from your DB.
23+
</span>
24+
</div>';
25+
26+
try {
27+
if ($password) {
28+
$DBDriver->connect();
29+
$tables = $DBDriver->listTables();
30+
31+
//Create product table
32+
$tn = DB::getStaticTableInNamesList($tables, $table_name);
33+
34+
if (!$tn) {
35+
$drop_table = true;
36+
$sql = $DBDriver->getCreateTableStatement(array(
37+
"name" => $table_name,
38+
"attributes" => array(
39+
array("name" => "product_id", "type" => "bigint", "length" => 20, "primary_key" => true, "auto_increment" => true),
40+
array("name" => "client_id", "type" => "bigint", "length" => 20, "unsigned" => true, "null" => true),
41+
array("name" => "type_id", "type" => "bigint", "length" => 20, "unsigned" => true, "null" => true),
42+
array("name" => "name", "type" => "varchar", "length" => 200, "default" => "", "null" => false),
43+
array("name" => "description", "type" => "blob", "default" => "", "null" => false)
44+
)
45+
));
46+
$status = $DBDriver->setSQL($sql);
47+
//echo "sql: $sql<br/>status: $status<br/>";die();
48+
}
49+
50+
//create product_type table
51+
$tn = DB::getStaticTableInNamesList($tables, $table_name . "_type");
52+
53+
if (!$tn) {
54+
$drop_table_type = true;
55+
$sql = $DBDriver->getCreateTableStatement(array(
56+
"name" => $table_name . "_type",
57+
"attributes" => array(
58+
array("name" => "product_type_id", "type" => "bigint", "length" => 20, "primary_key" => true, "auto_increment" => true),
59+
array("name" => "name", "type" => "varchar", "length" => 200, "default" => "", "null" => false)
60+
)
61+
));
62+
$status = $DBDriver->setSQL($sql);
63+
//echo "sql: $sql<br/>status: $status<br/>";die();
64+
65+
if ($status) {
66+
$status = $DBDriver->insertObject($table_name . "_type", array("name" => "desert"));
67+
//echo "sql: $sql<br/>status: $status<br/>";die();
68+
$status = $DBDriver->insertObject($table_name . "_type", array("name" => "vegetal"));
69+
//echo "sql: $sql<br/>status: $status<br/>";die();
70+
}
71+
}
72+
}
73+
else
74+
throw new Exception("Please edit config.php file and define your DB credentials first!");
75+
}
76+
catch (Throwable $e) {
77+
$error = $e->getMessage() . (!empty($e->problem) ? "<br/>" . $e->problem : "");
78+
}
79+
80+
if (!empty($error))
81+
echo '<div class="error">' . $error . '</div>';
82+
else {
83+
echo "<h5>Insert object into DB:</h5>";
84+
$attributes = array(
85+
"client_id" => 10,
86+
"type_id" => 2,
87+
"name" => "onion",
88+
"description" => "simple onion",
89+
);
90+
$status = $DBDriver->insertObject($table_name, $attributes);
91+
echo "<div class=\"code short\"><textarea readonly>\$attributes = array(
92+
\"client_id\" => 10,
93+
\"type_id\" => 2,
94+
\"name\" => \"onion\",
95+
\"description\" => \"simple onion\",
96+
);
97+
\$status = \$DBDriver->insertObject(\"" . $table_name . "\", \$attributes);</textarea></div>
98+
<div class=\"code one-line\"><textarea readonly>$status</textarea></div>";
99+
100+
$id = $status ? $DBDriver->getInsertedId() : null;
101+
$id = $id ? $id : 13;
102+
103+
echo "<h5>Update object in DB:</h5>";
104+
$attributes = array(
105+
"name" => "onion",
106+
"description" => "new onion",
107+
);
108+
$conditions = array(
109+
"product_id" => $id,
110+
);
111+
$status = $DBDriver->updateObject($table_name, $attributes, $conditions);
112+
echo "<div class=\"code short\"><textarea readonly>\$attributes = array(
113+
\"name\" => \"onion\",
114+
\"description\" => \"new onion\",
115+
);
116+
\$conditions = array(
117+
\"product_id\" => " . $id . ",
118+
);
119+
\$status = \$DBDriver->updateObject(\"" . $table_name . "\", \$attributes, \$conditions);</textarea></div>
120+
<div class=\"code one-line\"><textarea readonly>$status</textarea></div>";
121+
122+
echo "<h5>Select objects from DB:</h5>";
123+
$attributes = array("product_id", "name");
124+
$conditions = array("product_id" => $id);
125+
$objects = $DBDriver->findObjects($table_name, $attributes, $conditions);
126+
echo "<div class=\"code short\"><textarea readonly>
127+
\$attributes = array(\"product_id\", \"name\");
128+
\$conditions = array(\"product_id\" => " . $id . ");
129+
\$objects = \$DBDriver->findObjects(\"" . $table_name . "\", \$attributes, \$conditions);</textarea></div>
130+
<div class=\"code short\"><textarea readonly>" . print_r($objects, true) . "</textarea></div>";
131+
132+
echo "<h5>Count objects in DB:</h5>";
133+
$total = $DBDriver->countObjects($table_name, array("product_id" => $id));
134+
echo "<div class=\"code one-line\"><textarea readonly>\$total = \$DBDriver->countObjects(\"" . $table_name . "\", array(\"product_id\" => " . $id . "));</textarea></div>
135+
<div class=\"code one-line\"><textarea readonly>$total</textarea></div>";
136+
137+
echo "<h5>Get maximum value from DB:</h5>";
138+
$max = $DBDriver->findObjectsColumnMax($table_name, "product_id");
139+
echo "<div class=\"code one-line\"><textarea readonly>\$max = \$DBDriver->findObjectsColumnMax(\"" . $table_name . "\", \"product_id\");</textarea></div>
140+
<div class=\"code one-line\"><textarea readonly>$max</textarea></div>";
141+
142+
echo "<h5>Get foreign objects:</h5>";
143+
$rel_elm = array(
144+
"keys" => array(
145+
array("pcolumn" => "type_id", "ftable" => $table_name . "_type pt", "fcolumn" => "product_type_id")
146+
)
147+
);
148+
$objects = $DBDriver->findRelationshipObjects($table_name, $rel_elm);
149+
echo "<div class=\"code short\"><textarea readonly>\$rel_elm = array(
150+
\"keys\" => array(
151+
array(\"pcolumn\" => \"type_id\", \"ftable\" => \"" . $table_name . "_type pt\", \"fcolumn\" => \"product_type_id\")
152+
)
153+
);
154+
\$objects = \$DBDriver->findRelationshipObjects(\"" . $table_name . "\", \$rel_elm);</textarea></div>
155+
<div class=\"code\"><textarea readonly>" . print_r($objects, true) . "</textarea></div>";
156+
157+
echo "<h5>Get foreign objects together with main table:</h5>";
158+
$rel_elm = array(
159+
"attributes" => array(
160+
array("table" => "pt", "column" => "product_type_id", "name" => "type_id"),
161+
array("table" => "pt", "column" => "name", "name" => "type"),
162+
),
163+
"keys" => array(
164+
array("pcolumn" => "type_id", "ftable" => $table_name . "_type pt", "fcolumn" => "product_type_id")
165+
),
166+
"conditions" => array(
167+
array("table" => $table_name, "column" => "type_id", "reftable" => "pt", "refcolumn" => "product_type_id"),
168+
array("table" => "pt", "column" => "product_type_id", "operator" => "<", "value" => 3)
169+
),
170+
"groups_by" => array(
171+
array("table" => "", "column" => "type_id", "having" => "count(product_type_id) > 1")
172+
)
173+
);
174+
$parent_conditions = array(
175+
"pt.name" => "vegetal"
176+
);
177+
$options = array(
178+
"sorts" => array(
179+
array("table" => "", "column" => "type", "order" => "desc")
180+
),
181+
"sql_conditions" => "pt.name = 'vegetal'",
182+
);
183+
$objects = $DBDriver->findRelationshipObjects($table_name, $rel_elm, $parent_conditions, $options);
184+
echo "<div class=\"code\"><textarea readonly>\$rel_elm = array(
185+
\"attributes\" => array(
186+
array(\"table\" => \"pt\", \"column\" => \"product_type_id\", \"name\" => \"type_id\"),
187+
array(\"table\" => \"pt\", \"column\" => \"name\", \"name\" => \"type\"),
188+
),
189+
\"keys\" => array(
190+
array(\"pcolumn\" => \"type_id\", \"ftable\" => \$table_name . \"_type pt\", \"fcolumn\" => \"product_type_id\")
191+
),
192+
\"conditions\" => array(
193+
array(\"table\" => \$table_name, \"column\" => \"type_id\", \"reftable\" => \"pt\", \"refcolumn\" => \"product_type_id\"),
194+
array(\"table\" => \"pt\", \"column\" => \"product_type_id\", \"operator\" => \"<\", \"value\" => 3)
195+
),
196+
\"groups_by\" => array(
197+
array(\"table\" => \"\", \"column\" => \"type_id\", \"having\" => \"count(product_type_id) > 1\")
198+
)
199+
);
200+
\$parent_conditions = array(
201+
\"pt.name\" => \"vegetal\"
202+
);
203+
\$options = array(
204+
\"sorts\" => array(
205+
array(\"table\" => \"\", \"column\" => \"type\", \"order\" => \"desc\")
206+
),
207+
\"sql_conditions\" => \"pt.name = 'vegetal'\",
208+
);
209+
\$objects = \$DBDriver->findRelationshipObjects(\"" . $table_name . "\", \$rel_elm, \$parent_conditions, \$options);</textarea></div>
210+
<div class=\"code short\"><textarea readonly>" . print_r($objects, true) . "</textarea></div>";
211+
212+
echo "<h5>Count foreign objects:</h5>";
213+
$rel_elm = array(
214+
"keys" => array(
215+
array("pcolumn" => "type_id", "ftable" => $table_name . "_type pt", "fcolumn" => "product_type_id")
216+
),
217+
"conditions" => array(
218+
"type_id" => array(
219+
"operator" => "<",
220+
"value" => 3
221+
)
222+
)
223+
);
224+
$total = $DBDriver->countRelationshipObjects($table_name, $rel_elm);
225+
echo "<div class=\"code\"><textarea readonly>\$rel_elm = array(
226+
\"keys\" => array(
227+
array(\"pcolumn\" => \"type_id\", \"ftable\" => \"" . $table_name . "_type pt\", \"fcolumn\" => \"product_type_id\")
228+
),
229+
\"conditions\" => array(
230+
\"type_id\" => array(
231+
\"operator\" => \"<\",
232+
\"value\" => 3
233+
)
234+
)
235+
);
236+
\$total = \$DBDriver->countRelationshipObjects(\"" . $table_name . "\", \$rel_elm);</textarea></div>
237+
<div class=\"code one-line\"><textarea readonly>$total</textarea></div>";
238+
239+
echo "<h5>Delete object in DB:</h5>";
240+
$status = $DBDriver->deleteObject($table_name, array(
241+
"product_id" => array(
242+
"operator" => ">=",
243+
"value" => $id,
244+
),
245+
));
246+
echo "<div class=\"code short\"><textarea readonly>\$status = \$DBDriver->deleteObject(\"" . $table_name . "\", array(
247+
\"product_id\" => array(
248+
\"operator\" => \">=\",
249+
\"value\" => " . $id . "
250+
),
251+
));</textarea></div>
252+
<div class=\"code one-line\"><textarea readonly>$status</textarea></div>";
253+
254+
echo "<br/>";
255+
256+
if (!empty($drop_table)) {
257+
$sql = $DBDriver->getDropTableStatement($table_name);
258+
$status = $DBDriver->setSQL($sql);
259+
//echo "sql: $sql<br/>status: $status<br/>";die();
260+
}
261+
262+
if (!empty($drop_table_type)) {
263+
$sql = $DBDriver->getDropTableStatement($table_name . "_type");
264+
$status = $DBDriver->setSQL($sql);
265+
//echo "sql: $sql<br/>status: $status<br/>";die();
266+
}
267+
}
268+
?>

0 commit comments

Comments
 (0)