33
44class DB
55{
6+ protected $ attributes = array ();
7+
68 private static $ connection = null ;
79
810 private $ data = null ;
@@ -57,9 +59,14 @@ protected function buildInsert(array $data, $modifiers=null, $upsert=false): str
5759 $ i = 0 ;
5860 foreach ($ data as $ column => $ value )
5961 {
62+ if ($ this ->attributes && !in_array ($ column , $ this ->attributes ))
63+ {
64+ continue ;
65+ }
66+
6067 if ($ value instanceof Expression)
6168 {
62- $ tmp [] = sprintf ("%s = %s " , $ column , $ value );
69+ $ tmp [] = sprintf ("%s = %s " , $ column , $ value-> getValue () );
6370 } else {
6471 $ tmp [] = sprintf ("%s = :set_ $ i " , $ column );
6572 $ this ->param (":set_ $ i " , $ value );
@@ -85,47 +92,47 @@ protected function buildJoin(): string
8592 $ tmp [] = sprintf ("%s JOIN %s ON %s " , $ item ['type ' ], $ item ['table ' ], $ item ['conditions ' ]);
8693 }
8794
88- return join (" " , $ tmp );
95+ return join ("\n " , $ tmp );
8996 }
9097
9198 protected function buildSelect (): string
9299 {
93100 $ statement = "SELECT " . ($ this ->select ? $ this ->select : '* ' );
94101
95- $ statement .= " FROM " . $ this ->table ;
102+ $ statement .= "\n FROM " . $ this ->table ;
96103
97104 if ($ this ->join )
98105 {
99- $ statement .= " " . $ this ->buildJoin ();
106+ $ statement .= "\n " . $ this ->buildJoin ();
100107 }
101108
102109 if ($ this ->where )
103110 {
104- $ statement .= " WHERE " . $ this ->buildWhere ();
111+ $ statement .= "\n WHERE " . $ this ->buildWhere ();
105112 }
106113
107114 if ($ this ->groupBy )
108115 {
109- $ statement .= " GROUP BY " . $ this ->groupBy ;
116+ $ statement .= "\n GROUP BY " . $ this ->groupBy ;
110117 }
111118
112119 if ($ this ->having )
113120 {
114- $ statement .= " HAVING " . $ this ->having ;
121+ $ statement .= "\n HAVING " . $ this ->having ;
115122 }
116123
117124 if ($ this ->orderBy )
118125 {
119- $ statement .= " ORDER BY " . $ this ->orderBy ;
126+ $ statement .= "\n ORDER BY " . $ this ->orderBy ;
120127 }
121128
122129 if (is_numeric ($ this ->rowCount ))
123130 {
124131 if (is_numeric ($ this ->offset ))
125132 {
126- $ statement .= sprintf (" LIMIT %u, %u " , $ this ->offset , $ this ->rowCount );
133+ $ statement .= sprintf ("\n LIMIT %u, %u " , $ this ->offset , $ this ->rowCount );
127134 } else {
128- $ statement .= sprintf (" LIMIT %u " , $ this ->rowCount );
135+ $ statement .= sprintf ("\n LIMIT %u " , $ this ->rowCount );
129136 }
130137 }
131138
@@ -165,9 +172,9 @@ protected function connect(): void
165172 return ;
166173 }
167174
168- $ dsn = getenv ( 'PHP_ORM_DSN ' , true ) ;
169- $ user = getenv ( 'PHP_ORM_USER ' , true ) ;
170- $ password = getenv ( 'PHP_ORM_PSWD ' , true ) ;
175+ $ dsn = $ _ENV [ 'PHP_ORM_DSN ' ] ;
176+ $ user = $ _ENV [ 'PHP_ORM_USER ' ] ;
177+ $ password = $ _ENV [ 'PHP_ORM_PSWD ' ] ;
171178
172179 try {
173180 $ this ->dbh = new \PDO ($ dsn , $ user , $ password );
@@ -258,9 +265,11 @@ public function delete($modifiers=null): ?int
258265
259266 if (is_numeric ($ this ->rowCount ))
260267 {
261- $ statement .= sprintf (" LIMIT %u; " , $ this ->rowCount );
268+ $ statement .= sprintf (" LIMIT %u " , $ this ->rowCount );
262269 }
263270
271+ $ statement .= "; " ;
272+
264273 if (!$ this ->fire ($ statement ))
265274 {
266275 return false ;
@@ -283,7 +292,7 @@ protected function dump()
283292
284293 public function find ($ value ): ?array
285294 {
286- $ this ->where (' id ' , $ value );
295+ $ this ->where ($ this -> table . ' . id ' , $ value );
287296 $ this ->limit (1 , 0 );
288297
289298 if (!$ this ->fire ($ this ->buildSelect ()))
@@ -293,7 +302,7 @@ public function find($value): ?array
293302
294303 $ this ->dump ();
295304
296- return $ this ->sth ->fetch (\PDO ::FETCH_ASSOC );
305+ return $ this ->sth ->fetch (\PDO ::FETCH_ASSOC ) ? : NULL ;
297306 }
298307
299308 protected function fire (string $ statement ): bool
@@ -321,7 +330,7 @@ public function first(): ?array
321330
322331 $ this ->dump ();
323332
324- return $ this ->sth ->fetch (\PDO ::FETCH_ASSOC );
333+ return $ this ->sth ->fetch (\PDO ::FETCH_ASSOC ) ? : NULL ;
325334 }
326335
327336 public function get (): ?array
@@ -357,6 +366,8 @@ public function insert(array $data, $modifiers=null): ?int
357366 return false ;
358367 }
359368
369+ $ this ->dump ();
370+
360371 return $ this ->sth ->rowCount ()
361372 ? $ this ->dbh ->lastInsertId ()
362373 : false ;
@@ -482,12 +493,17 @@ public function update(array $data, $modifiers=null): int
482493 $ i = 0 ;
483494 foreach ($ data as $ column => $ value )
484495 {
496+ if ($ this ->attributes && !in_array ($ column , $ this ->attributes ))
497+ {
498+ continue ;
499+ }
500+
485501 if ($ value instanceof Expression)
486502 {
487503 $ tmp [] = sprintf ("%s = %s " , $ column , $ value ->getValue ());
488504 } else {
489505 $ tmp [] = sprintf ("%s = :set_ $ i " , $ column );
490- $ this ->param (":set_ $ i " , $ value );
506+ $ this ->param (":set_ $ i " , $ value != '' ? $ value : NULL );
491507 }
492508 $ i += 1 ;
493509 }
0 commit comments