Login   Register  
PHP Classes
elePHPant
Icontem

File: class_sqlite.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Ken Stanley  >  Class Calendar  >  class_sqlite.php  
File: class_sqlite.php
Role: Class source
Content type: text/plain
Description: Simple SQLite API
Class: Class Calendar
Generate month calendar arrays
 

Contents

Class file image Download
<?php
/******************************************************************************/
/* class_sqlite.php: Small class to easily use SQLite PHP extension.          */
/* Copyright (C) 2005 Ken Stanley <phpclasses@kennethpaul.com>                */
/*                                                                            */
/* This library is free software; you can redistribute it and/or              */
/* modify it under the terms of the GNU Lesser General Public                 */
/* License as published by the Free Software Foundation; either               */
/* version 2.1 of the License, or (at your option) any later version.         */
/*                                                                            */
/* This library is distributed in the hope that it will be useful,            */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of             */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          */
/* Lesser General Public License for more details.                            */
/*                                                                            */
/* You should have received a copy of the GNU Lesser General Public           */
/* License along with this library; if not, write to the Free Software        */
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
/******************************************************************************/
    
class sqlite {
        var 
$num_rows        0;
        var 
$resource        '';

        function 
sqlite($_sqlite_file$_sqlite_mode '0666') {
            if (!
extension_loaded('sqlite')) {
                if (
strtoupper(substr(PHP_OS03) == 'WIN')) {
                    
$extension "php_sqlite.dll";
                } else {
                    
$extension "sqlite.so";
                }
            
                @
dl($extension);
            }

            if (
is_readable($_sqlite_file) && is_writable($_sqlite_file)) {
                
$this->resource sqlite_open($_sqlite_file$_sqlite_mode);

                if (
is_resource($this->resource)) {
                    return 
TRUE;
                } else {
                    
$this->last_error();
                    return 
FALSE;
                }
            } else {
                
trigger_error("$_sqlite_file: Cannot read/write database file"E_USER_WARNING);
                return 
FALSE;
            }
        }

        function 
close() {
            if (
is_resource($this->resource)) {
                return 
sqlite_close($this->resource);
            } else {
                return 
FALSE;
            }
        }

        function 
query($_query_sql$_query_type SQLITE_BOTH) {
            if (
is_resource($this->resource)) {
                
$_query_result sqlite_query($_query_sql$this->resource$_query_type);

                if (
is_resource($_query_result)) {
                    
$this->num_rows sqlite_num_rows($_query_result);
                } else {
                    
$this->last_error();
                    return 
FALSE;
                }

                return 
$_query_result;
            } else {
                return 
FALSE;
            }
        }

        function 
fetch_assoc($_fetch_assoc_query) {
            if (
is_resource($this->resource)) {
                
$_fetch_assoc_qhandle $this->query($_fetch_assoc_querySQLITE_ASSOC);

                if (
is_resource($_fetch_assoc_qhandle)) {
                    while (
$_fetch_assoc_row sqlite_fetch_array($_fetch_assoc_qhandleSQLITE_ASSOC)) {
                        
$_fetch_assoc_result[] = $_fetch_assoc_row;
                    }
                } else {
                    return 
FALSE;
                }

                if (isset(
$_fetch_assoc_result) && is_array($_fetch_assoc_result)) {
                    return 
$_fetch_assoc_result;
                } else {
                    return 
FALSE;
                }
            } else {
                return 
FALSE;
            }
        }

        function 
table_exists($_table_exists_table) {
            if (
is_resource($this->resource)) {
                
$_table_exists_sql "SELECT COUNT(*) FROM sqlite_master";
                
$_table_exists_sql .= " WHERE type='table'";
                
$_table_exists_sql .= " AND name='$_table_exists_table'";

                
$_table_exists_result $this->query($_table_exists_sql);

                if (
is_resource($_table_exists_result)) {
                    
$_table_exists_count intval(sqlite_fetch_single($_table_exists_result));
                } else {
                    return 
FALSE;
                }

                return (
$_table_exists_count 0);
            } else {
                return 
FALSE;
            }
        }

        function 
create_table($_create_table_table) {
            if (
is_resource($this->resource)) {
                
$_create_table_sql "CREATE TABLE $_create_table_table";

                
$_create_table_result $this->query($_create_table_sql);

                if (
is_resource($_create_table_result)) {
                    return 
TRUE;
                } else {
                    return 
FALSE;
                }
            } else {
                return 
FALSE;
            }
        }

        function 
last_rowid() {
            if (
is_resource($this->resource)) {
                
$_last_rowid_result sqlite_last_insert_rowid($this->resource);

                if (
$_last_rowid_result 0) {
                    return 
$_last_rowid_result;
                } else {
                    return 
FALSE;
                }
            } else {
                return 
FALSE;
            }
        }

        function 
last_error($_last_error_return FALSE) {
            
$_last_error_mesg sprintf(
                
"<strong>SQLite Error:</strong> %s<br />",
                
sqlite_error_string(sqlite_last_error($this->resource))
            );

            if (
$_last_error_return) {
                return 
$_last_error_mesg;
            } else {
                echo 
$_last_error_mesg;
                return 
TRUE;
            }
        }
    }
?>