File: sqlite3_srv.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (120 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * FALCON - The Falcon Programming Language.
 * FILE: sqlite3_srv.cpp
 *
 * SQLite3 Falcon service/driver
 * -------------------------------------------------------------------
 * Author: Giancarlo Niccolai
 * Begin: Sun, 23 May 2010 18:23:20 +0200
 *
 * -------------------------------------------------------------------
 * (C) Copyright 2010: the FALCON developers (see list in AUTHORS file)
 *
 * See LICENSE file for licensing details.
 */

#include <string.h>

#include <falcon/engine.h>
#include <falcon/sys.h>
#include "sqlite3_mod.h"
#include "sqlite3.h"

namespace Falcon
{

/******************************************************************************
 * Main service class
 *****************************************************************************/

DBIServiceSQLite3::DBIServiceSQLite3():
   DBIService( "DBI_sqlite3" )
{}


void DBIServiceSQLite3::init()
{
}

DBIHandle *DBIServiceSQLite3::connect( const String &parameters )
{
   // Parse the connection string.
   DBIConnParams connParams;

   if( ! connParams.parse( parameters ) || connParams.m_szDb == 0 )
   {
      throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CONNPARAMS, __LINE__)
         .extra( parameters )
      );
   }

   int flags = SQLITE_OPEN_READWRITE;
   if( connParams.m_sCreate == "always" )
   {
      flags |= SQLITE_OPEN_CREATE;

      // sqlite3 doesn't drop databases: delete files.
      int32 fsStatus;
      FileStat::e_fileType st;
      if ( Sys::fal_fileType( connParams.m_szDb, st ) &&
           ! Sys::fal_unlink( connParams.m_szDb, fsStatus ) )
      {
         throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CONNECT_CREATE, __LINE__)
                      .extra( parameters )
                   );
      }
   }
   else if ( connParams.m_sCreate == "cond" )
   {
      flags |= SQLITE_OPEN_CREATE;
   }
   else if( connParams.m_sCreate != "" )
   {
      throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CONNPARAMS, __LINE__)
              .extra( parameters )
           );
   }

   sqlite3 *conn;
   int result = sqlite3_open_v2( connParams.m_szDb, &conn, flags, NULL );

   if ( conn == NULL )
   {
      throw new DBIError( ErrorParam( FALCON_DBI_ERROR_NOMEM, __LINE__) );
   }
   else if ( result == SQLITE_CANTOPEN )
   {
      int er = connParams.m_sCreate == "cond" ?
               FALCON_DBI_ERROR_CONNECT_CREATE : FALCON_DBI_ERROR_DB_NOTFOUND;

      throw new DBIError( ErrorParam( er, __LINE__)
                    .extra( sqlite3_errmsg( conn ) )
                 );
   }
   else if ( result != SQLITE_OK )
   {
      throw new DBIError( ErrorParam( FALCON_DBI_ERROR_CONNECT, __LINE__)
              .extra( sqlite3_errmsg( conn ) )
           );
   }

   return new DBIHandleSQLite3( conn );
}

CoreObject *DBIServiceSQLite3::makeInstance( VMachine *vm, DBIHandle *dbh )
{
   Item *cl = vm->findWKI( "SQLite3" );
   if ( cl == 0 || ! cl->isClass() || cl->asClass()->symbol()->name() != "SQLite3" ) {
      throw new DBIError( ErrorParam( FALCON_DBI_ERROR_INVALID_DRIVER, __LINE__ ) );
   }

   CoreObject *obj = cl->asClass()->createInstance();
   obj->setUserData( dbh );

   return obj;
}

} /* namespace Falcon */

/* end of sqlite3_srv.cpp */