binding/v8/jsi/dataaccess/CheckConstraint.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file CheckConstraint.cpp
22 
23  \brief Utility functions to register the CheckConstraint class into the Google JavaScript V8 engine.
24  */
25 
26 // TerraLib
27 #include "../../../../dataaccess/dataset/CheckConstraint.h"
28 #include "../../common/Utils.h"
29 #include "DataAccess.h"
30 
31 // STL
32 #include <memory>
33 
34 // Boost
35 #include <boost/cstdint.hpp>
36 
37 ::v8::Handle<::v8::Value> CheckConstraint_GetExpression(const ::v8::Arguments& args)
38 {
39  ::v8::HandleScope hs;
40 
41  if(args.Holder().IsEmpty())
42  return ::v8::ThrowException(::v8::String::New("In order to use the getExpression method you must use the object notation: \"exp = obj.getExpression();\""));
43 
44  te::da::CheckConstraint* cc = te::v8::common::Unwrap<te::da::CheckConstraint>(args.Holder());
45 
46  if(cc == 0)
47  return ::v8::ThrowException(::v8::String::New("Invalid check constraint in the getExpression method!"));
48 
49  const std::string& exp = cc->getExpression();
50 
51  ::v8::Local<::v8::String> jexp = ::v8::String::New(exp.c_str());
52 
53  return hs.Close(jexp);
54 }
55 
56 ::v8::Handle<::v8::Value> CheckConstraint_SetExpression(const ::v8::Arguments& args)
57 {
58  if(args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsString() || args.Holder().IsEmpty())
59  return ::v8::ThrowException(::v8::String::New("In order to use the setExpression method, you must use the object notation: \"obj.setExpression(\"x > 20\");\""));
60 
61  te::da::CheckConstraint* cc = te::v8::common::Unwrap<te::da::CheckConstraint>(args.Holder());
62 
63  if(cc == 0)
64  return ::v8::ThrowException(::v8::String::New("Invalid check constraint in the setExpression method!"));
65 
66  v8::String::Utf8Value jexp(args[0]->ToString());
67 
68  cc->setExpression(*jexp);
69 
70  return ::v8::Undefined();
71 }
72 
73 ::v8::Handle<::v8::Value> CheckConstraint_Constructor(const ::v8::Arguments& args)
74 {
75  //::v8::HandleScope hs;
76 
77  //if(!args.IsConstructCall())
78  // return ::v8::ThrowException(::v8::String::New("In order to create a check constraint, you need to call its constructor like: var c = new TeCheckConstraint(dt, id);"));
79 
80  //if(args.Holder().IsEmpty())
81  // return ::v8::ThrowException(::v8::String::New("The CheckConstraint constructor must use object notation!"));
82 
83  //if(args.Length() != 0)
84  // return ::v8::ThrowException(::v8::String::New("The CheckConstraint constructor has no parameters!"));
85 
86  //std::auto_ptr<te::da::CheckConstraint> catalog(new te::da::CheckConstraint());
87 
88  //::v8::Local<::v8::Object> jcatalog = te::v8::common::Make(catalog.get(), te::v8::jsi::GetCheckConstraintTemplate, true);
89 
90  //catalog.release();
91 
92  //return hs.Close(jcatalog);
93 
94  return ::v8::Undefined();
95 }
96 
97 void te::v8::jsi::RegisterCheckConstraint(::v8::Local<::v8::Object>& global)
98 {
99  ::v8::Local<::v8::FunctionTemplate> jscheckconstraint = ::v8::FunctionTemplate::New(CheckConstraint_Constructor);
100 
101  global->Set(::v8::String::New("TeCheckConstraint"), jscheckconstraint->GetFunction());
102 }
103 
104 static ::v8::Persistent<::v8::FunctionTemplate> sg_checkconstraint_template;
105 
106 ::v8::Persistent<::v8::FunctionTemplate>& te::v8::jsi::GetCheckConstraintTemplate()
107 {
108  if(sg_checkconstraint_template.IsEmpty())
109  {
110  ::v8::Persistent<::v8::FunctionTemplate>& cTpl = GetConstraintTemplate();
111  ::v8::Local<::v8::FunctionTemplate> result = ::v8::FunctionTemplate::New();
112  result->Inherit(cTpl);
113 
114  ::v8::Handle<::v8::ObjectTemplate> prototype = result->PrototypeTemplate();
115 
116  prototype->Set(::v8::String::NewSymbol("getExpression"), ::v8::FunctionTemplate::New(CheckConstraint_GetExpression));
117  prototype->Set(::v8::String::NewSymbol("setExpression"), ::v8::FunctionTemplate::New(CheckConstraint_SetExpression));
118 
119  sg_checkconstraint_template = ::v8::Persistent<::v8::FunctionTemplate>::New(result);
120  }
121 
123 }
void RegisterCheckConstraint(::v8::Local<::v8::Object > &global)
It registers the CheckConstraint class.
::v8::Handle<::v8::Value > CheckConstraint_SetExpression(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetCheckConstraintTemplate()
It returns a reference to the persistent template of a CheckConstraint object.
::v8::Handle<::v8::Value > CheckConstraint_GetExpression(const ::v8::Arguments &args)
JavaScript exporting routine for the TerraLib Data Access module.
A class that describes a check constraint.
const std::string & getExpression() const
It returns the check constraint expression.
static::v8::Persistent<::v8::FunctionTemplate > sg_checkconstraint_template
void setExpression(const std::string &e)
It sets the check constraint expression.
::v8::Handle<::v8::Value > CheckConstraint_Constructor(const ::v8::Arguments &args)
::v8::Persistent<::v8::FunctionTemplate > & GetConstraintTemplate()
It returns a reference to the persistent template of a Constraint object.
std::string ToString(const XMLCh *const value)
It converts the XML string to a standard C++ string.