idx
int64
0
251k
question
stringlengths
53
3.53k
target
stringlengths
5
1.23k
len_question
int64
20
893
len_target
int64
3
238
224,900
def add_column ( self , name , type_name , options = None ) : column = Column ( name , type_name , options ) self . _add_column ( column ) return column
Adds a new column .
41
5
224,901
def change_column ( self , name , options ) : column = self . get_column ( name ) column . set_options ( options ) return self
Changes column details .
32
4
224,902
def drop_column ( self , name ) : name = self . _normalize_identifier ( name ) del self . _columns [ name ] return self
Drops a Column from the Table
34
7
224,903
def add_named_foreign_key_constraint ( self , name , foreign_table , local_columns , foreign_columns , options ) : if isinstance ( foreign_table , Table ) : for column in foreign_columns : if not foreign_table . has_column ( column ) : raise ColumnDoesNotExist ( column , foreign_table . get_name ( ) ) for column in local_columns : if not self . has_column ( column ) : raise ColumnDoesNotExist ( column , self . _name ) constraint = ForeignKeyConstraint ( local_columns , foreign_table , foreign_columns , name , options ) self . _add_foreign_key_constraint ( constraint ) return self
Adds a foreign key constraint with a given name .
161
10
224,904
def _add_index ( self , index ) : index_name = index . get_name ( ) index_name = self . _normalize_identifier ( index_name ) replaced_implicit_indexes = [ ] for name , implicit_index in self . _implicit_indexes . items ( ) : if implicit_index . is_fullfilled_by ( index ) and name in self . _indexes : replaced_implicit_indexes . append ( name ) already_exists = ( index_name in self . _indexes and index_name not in replaced_implicit_indexes or self . _primary_key_name is not False and index . is_primary ( ) ) if already_exists : raise IndexAlreadyExists ( index_name , self . _name ) for name in replaced_implicit_indexes : del self . _indexes [ name ] del self . _implicit_indexes [ name ] if index . is_primary ( ) : self . _primary_key_name = index_name self . _indexes [ index_name ] = index return self
Adds an index to the table .
241
7
224,905
def has_foreign_key ( self , name ) : name = self . _normalize_identifier ( name ) return name in self . _fk_constraints
Returns whether this table has a foreign key constraint with the given name .
37
14
224,906
def get_foreign_key ( self , name ) : name = self . _normalize_identifier ( name ) if not self . has_foreign_key ( name ) : raise ForeignKeyDoesNotExist ( name , self . _name ) return self . _fk_constraints [ name ]
Returns the foreign key constraint with the given name .
66
10
224,907
def remove_foreign_key ( self , name ) : name = self . _normalize_identifier ( name ) if not self . has_foreign_key ( name ) : raise ForeignKeyDoesNotExist ( name , self . _name ) del self . _fk_constraints [ name ]
Removes the foreign key constraint with the given name .
66
11
224,908
def get_primary_key_columns ( self ) : if not self . has_primary_key ( ) : raise DBALException ( 'Table "%s" has no primary key.' % self . get_name ( ) ) return self . get_primary_key ( ) . get_columns ( )
Returns the primary key columns .
66
6
224,909
def has_index ( self , name ) : name = self . _normalize_identifier ( name ) return name in self . _indexes
Returns whether this table has an Index with the given name .
31
12
224,910
def get_index ( self , name ) : name = self . _normalize_identifier ( name ) if not self . has_index ( name ) : raise IndexDoesNotExist ( name , self . _name ) return self . _indexes [ name ]
Returns the Index with the given name .
57
8
224,911
def without_global_scope ( self , scope ) : if isinstance ( scope , basestring ) : del self . _scopes [ scope ] return self keys = [ ] for key , value in self . _scopes . items ( ) : if scope == value . __class__ or isinstance ( scope , value . __class__ ) : keys . append ( key ) for key in keys : del self . _scopes [ key ] return self
Remove a registered global scope .
96
6
224,912
def find_or_fail ( self , id , columns = None ) : result = self . find ( id , columns ) if isinstance ( id , list ) : if len ( result ) == len ( set ( id ) ) : return result elif result : return result raise ModelNotFound ( self . _model . __class__ )
Find a model by its primary key or raise an exception
71
11
224,913
def first_or_fail ( self , columns = None ) : model = self . first ( columns ) if model is not None : return model raise ModelNotFound ( self . _model . __class__ )
Execute the query and get the first result or raise an exception
44
13
224,914
def pluck ( self , column ) : result = self . first ( [ column ] ) if result : return result [ column ]
Pluck a single column from the database .
27
9
224,915
def _add_updated_at_column ( self , values ) : if not self . _model . uses_timestamps ( ) : return values column = self . _model . get_updated_at_column ( ) if "updated_at" not in values : values . update ( { column : self . _model . fresh_timestamp_string ( ) } ) return values
Add the updated_at column to a dictionary of values .
82
12
224,916
def delete ( self ) : if self . _on_delete is not None : return self . _on_delete ( self ) return self . _query . delete ( )
Delete a record from the database .
36
7
224,917
def get_relation ( self , relation ) : from . relations import Relation with Relation . no_constraints ( True ) : rel = getattr ( self . get_model ( ) , relation ) ( ) nested = self . _nested_relations ( relation ) if len ( nested ) > 0 : rel . get_query ( ) . with_ ( nested ) return rel
Get the relation instance for the given relation name .
81
10
224,918
def _nested_relations ( self , relation ) : nested = { } for name , constraints in self . _eager_load . items ( ) : if self . _is_nested ( name , relation ) : nested [ name [ len ( relation + "." ) : ] ] = constraints return nested
Get the deeply nested relations for a given top - level relation .
65
13
224,919
def _is_nested ( self , name , relation ) : dots = name . find ( "." ) return dots and name . startswith ( relation + "." )
Determine if the relationship is nested .
37
9
224,920
def where_exists ( self , query , boolean = "and" , negate = False ) : if isinstance ( query , Builder ) : query = query . get_query ( ) self . get_query ( ) . where_exists ( query , boolean , negate ) return self
Add an exists clause to the query .
60
8
224,921
def has ( self , relation , operator = ">=" , count = 1 , boolean = "and" , extra = None ) : if relation . find ( "." ) >= 0 : return self . _has_nested ( relation , operator , count , boolean , extra ) relation = self . _get_has_relation_query ( relation ) query = relation . get_relation_count_query ( relation . get_related ( ) . new_query ( ) , self ) # TODO: extra query if extra : if callable ( extra ) : extra ( query ) return self . _add_has_where ( query . apply_scopes ( ) , relation , operator , count , boolean )
Add a relationship count condition to the query .
148
9
224,922
def _add_has_where ( self , has_query , relation , operator , count , boolean ) : self . _merge_model_defined_relation_wheres_to_has_query ( has_query , relation ) if isinstance ( count , basestring ) and count . isdigit ( ) : count = QueryExpression ( count ) return self . where ( QueryExpression ( "(%s)" % has_query . to_sql ( ) ) , operator , count , boolean )
Add the has condition where clause to the query .
107
10
224,923
def _merge_model_defined_relation_wheres_to_has_query ( self , has_query , relation ) : relation_query = relation . get_base_query ( ) has_query . merge_wheres ( relation_query . wheres , relation_query . get_bindings ( ) ) self . _query . add_binding ( has_query . get_query ( ) . get_bindings ( ) , "where" )
Merge the wheres from a relation query to a has query .
99
14
224,924
def apply_scopes ( self ) : if not self . _scopes : return self builder = copy . copy ( self ) query = builder . get_query ( ) # We will keep track of how many wheres are on the query before running the # scope so that we can properly group the added scope constraints in the # query as their own isolated nested where statement and avoid issues. original_where_count = len ( query . wheres ) where_counts = [ 0 , original_where_count ] for scope in self . _scopes . values ( ) : self . _apply_scope ( scope , builder ) # Again, we will keep track of the count each time we add where clauses so that # we will properly isolate each set of scope constraints inside of their own # nested where clause to avoid any conflicts or issues with logical order. where_counts . append ( len ( query . wheres ) ) if self . _should_nest_wheres_for_scope ( query , original_where_count ) : self . _nest_wheres_for_scope ( query , Collection ( where_counts ) . unique ( ) . all ( ) ) return builder
Get the underlying query builder instance with applied global scopes .
250
12
224,925
def _apply_scope ( self , scope , builder ) : if callable ( scope ) : scope ( builder ) elif isinstance ( scope , Scope ) : scope . apply ( builder , self . get_model ( ) )
Apply a single scope on the given builder instance .
48
10
224,926
def _nest_wheres_for_scope ( self , query , where_counts ) : # Here, we totally remove all of the where clauses since we are going to # rebuild them as nested queries by slicing the groups of wheres into # their own sections. This is to prevent any confusing logic order. wheres = query . wheres query . wheres = [ ] # We will take the first offset (typically 0) of where clauses and start # slicing out every scope's where clauses into their own nested where # groups for improved isolation of every scope's added constraints. previous_count = where_counts . pop ( 0 ) for where_count in where_counts : query . wheres . append ( self . _slice_where_conditions ( wheres , previous_count , where_count - previous_count ) ) previous_count = where_count
Nest where conditions of the builder and each global scope .
184
12
224,927
def _slice_where_conditions ( self , wheres , offset , length ) : where_group = self . get_query ( ) . for_nested_where ( ) where_group . wheres = wheres [ offset : ( offset + length ) ] return { "type" : "nested" , "query" : where_group , "boolean" : "and" }
Create a where list with sliced where conditions .
85
9
224,928
def set_model ( self , model ) : self . _model = model self . _query . from_ ( model . get_table ( ) ) return self
Set a model instance for the model being queried .
34
11
224,929
def compile_insert ( self , query , values ) : table = self . wrap_table ( query . from__ ) if not isinstance ( values , list ) : values = [ values ] # If there is only one row to insert, we just use the normal grammar if len ( values ) == 1 : return super ( SQLiteQueryGrammar , self ) . compile_insert ( query , values ) names = self . columnize ( values [ 0 ] . keys ( ) ) columns = [ ] # SQLite requires us to build the multi-row insert as a listing of select with # unions joining them together. So we'll build out this list of columns and # then join them all together with select unions to complete the queries. for column in values [ 0 ] . keys ( ) : columns . append ( "%s AS %s" % ( self . get_marker ( ) , self . wrap ( column ) ) ) columns = [ ", " . join ( columns ) ] * len ( values ) return "INSERT INTO %s (%s) SELECT %s" % ( table , names , " UNION ALL SELECT " . join ( columns ) , )
Compile insert statement into SQL
243
6
224,930
def compile_truncate ( self , query ) : sql = { "DELETE FROM sqlite_sequence WHERE name = %s" % self . get_marker ( ) : [ query . from__ ] } sql [ "DELETE FROM %s" % self . wrap_table ( query . from__ ) ] = [ ] return sql
Compile a truncate statement into SQL
75
8
224,931
def build ( self , connection , grammar ) : for statement in self . to_sql ( connection , grammar ) : connection . statement ( statement )
Execute the blueprint against the database .
30
8
224,932
def to_sql ( self , connection , grammar ) : self . _add_implied_commands ( ) statements = [ ] for command in self . _commands : method = "compile_%s" % command . name if hasattr ( grammar , method ) : sql = getattr ( grammar , method ) ( self , command , connection ) if sql is not None : if isinstance ( sql , list ) : statements += sql else : statements . append ( sql ) return statements
Get the raw SQL statements for the blueprint .
103
9
224,933
def _add_implied_commands ( self ) : if len ( self . get_added_columns ( ) ) and not self . _creating ( ) : self . _commands . insert ( 0 , self . _create_command ( "add" ) ) if len ( self . get_changed_columns ( ) ) and not self . _creating ( ) : self . _commands . insert ( 0 , self . _create_command ( "change" ) ) return self . _add_fluent_indexes ( )
Add the commands that are implied by the blueprint .
118
10
224,934
def drop_column ( self , * columns ) : columns = list ( columns ) return self . _add_command ( "drop_column" , columns = columns )
Indicates that the given columns should be dropped .
35
10
224,935
def integer ( self , column , auto_increment = False , unsigned = False ) : return self . _add_column ( "integer" , column , auto_increment = auto_increment , unsigned = unsigned )
Create a new integer column on the table .
47
9
224,936
def small_integer ( self , column , auto_increment = False , unsigned = False ) : return self . _add_column ( "small_integer" , column , auto_increment = auto_increment , unsigned = unsigned )
Create a new small integer column on the table .
51
10
224,937
def unsigned_integer ( self , column , auto_increment = False ) : return self . integer ( column , auto_increment , True )
Create a new unisgned integer column on the table .
31
13
224,938
def unsigned_big_integer ( self , column , auto_increment = False ) : return self . big_integer ( column , auto_increment , True )
Create a new unsigned big integer column on the table .
35
11
224,939
def float ( self , column , total = 8 , places = 2 ) : return self . _add_column ( "float" , column , total = total , places = places )
Create a new float column on the table .
38
9
224,940
def double ( self , column , total = None , places = None ) : return self . _add_column ( "double" , column , total = total , places = places )
Create a new double column on the table .
38
9
224,941
def decimal ( self , column , total = 8 , places = 2 ) : return self . _add_column ( "decimal" , column , total = total , places = places )
Create a new decimal column on the table .
39
9
224,942
def timestamps ( self , use_current = True ) : if use_current : self . timestamp ( "created_at" ) . use_current ( ) self . timestamp ( "updated_at" ) . use_current ( ) else : self . timestamp ( "created_at" ) self . timestamp ( "updated_at" )
Create creation and update timestamps to the table .
73
11
224,943
def morphs ( self , name , index_name = None ) : self . unsigned_integer ( "%s_id" % name ) self . string ( "%s_type" % name ) self . index ( [ "%s_id" % name , "%s_type" % name ] , index_name )
Add the proper columns for a polymorphic table .
67
10
224,944
def _drop_index_command ( self , command , type , index ) : columns = [ ] if isinstance ( index , list ) : columns = index index = self . _create_index_name ( type , columns ) return self . _index_command ( command , columns , index )
Create a new drop index command on the blueprint .
62
10
224,945
def _index_command ( self , type , columns , index ) : if not isinstance ( columns , list ) : columns = [ columns ] if not index : index = self . _create_index_name ( type , columns ) return self . _add_command ( type , index = index , columns = columns )
Add a new index command to the blueprint .
67
9
224,946
def _remove_column ( self , name ) : self . _columns = filter ( lambda c : c . name != name , self . _columns ) return self
Removes a column from the blueprint .
36
8
224,947
def _add_command ( self , name , * * parameters ) : command = self . _create_command ( name , * * parameters ) self . _commands . append ( command ) return command
Add a new command to the blueprint .
42
8
224,948
def get_quoted_columns ( self , platform ) : columns = [ ] for column in self . _columns . values ( ) : columns . append ( column . get_quoted_name ( platform ) ) return columns
Returns the quoted representation of the column names the constraint is associated with .
49
14
224,949
def spans_columns ( self , column_names ) : columns = self . get_columns ( ) number_of_columns = len ( columns ) same_columns = True for i in range ( number_of_columns ) : column = self . _trim_quotes ( columns [ i ] . lower ( ) ) if i >= len ( column_names ) or column != self . _trim_quotes ( column_names [ i ] . lower ( ) ) : same_columns = False return same_columns
Checks if this index exactly spans the given column names in the correct order .
117
16
224,950
def is_fullfilled_by ( self , other ) : # allow the other index to be equally large only. It being larger is an option # but it creates a problem with scenarios of the kind PRIMARY KEY(foo,bar) UNIQUE(foo) if len ( other . get_columns ( ) ) != len ( self . get_columns ( ) ) : return False # Check if columns are the same, and even in the same order if not self . spans_columns ( other . get_columns ( ) ) : return False if not self . same_partial_index ( other ) : return False if self . is_simple_index ( ) : # this is a special case: If the current key is neither primary or unique, # any unique or primary key will always have the same effect # for the index and there cannot be any constraint overlaps. # This means a primary or unique index can always fulfill # the requirements of just an index that has no constraints. return True if other . is_primary ( ) != self . is_primary ( ) : return False if other . is_unique ( ) != self . is_unique ( ) : return False return True
Checks if the other index already fulfills all the indexing and constraint needs of the current one .
251
21
224,951
def same_partial_index ( self , other ) : if ( self . has_option ( "where" ) and other . has_option ( "where" ) and self . get_option ( "where" ) == other . get_option ( "where" ) ) : return True if not self . has_option ( "where" ) and not other . has_option ( "where" ) : return True return False
Return whether the two indexes have the same partial index
90
10
224,952
def overrules ( self , other ) : if other . is_primary ( ) : return False elif self . is_simple_index ( ) and other . is_unique ( ) : return False same_columns = self . spans_columns ( other . get_columns ( ) ) if ( same_columns and ( self . is_primary ( ) or self . is_unique ( ) ) and self . same_partial_index ( other ) ) : return True return False
Detects if the other index is a non - unique non primary index that can be overwritten by this one .
104
23
224,953
def remove_flag ( self , flag ) : if self . has_flag ( flag ) : del self . _flags [ flag . lower ( ) ]
Removes a flag .
32
5
224,954
def _compile_create_encoding ( self , sql , connection , blueprint ) : charset = blueprint . charset or connection . get_config ( "charset" ) if charset : sql += " DEFAULT CHARACTER SET %s" % charset collation = blueprint . collation or connection . get_config ( "collation" ) if collation : sql += " COLLATE %s" % collation return sql
Append the character set specifications to a command .
95
10
224,955
def _get_path ( self , name ) : path = self . option ( "path" ) if path is None : path = self . _get_seeders_path ( ) return os . path . join ( path , "%s.py" % name )
Get the destination class path .
56
6
224,956
def update ( self , _attributes = None , * * attributes ) : if _attributes is not None : attributes . update ( _attributes ) if self . _related . uses_timestamps ( ) : attributes [ self . get_related_updated_at ( ) ] = self . _related . fresh_timestamp ( ) return self . _query . update ( attributes )
Perform an update on all the related models .
82
10
224,957
def get_dialect ( self ) : if "+" not in self . drivername : name = self . drivername else : name = self . drivername . replace ( "+" , "." ) cls = registry . load ( name ) # check for legacy dialects that # would return a module with 'dialect' as the # actual class if ( hasattr ( cls , "dialect" ) and isinstance ( cls . dialect , type ) and issubclass ( cls . dialect , Dialect ) ) : return cls . dialect else : return cls
Return the SQLAlchemy database dialect class corresponding to this URL s driver name .
125
16
224,958
def translate_connect_args ( self , names = [ ] , * * kw ) : translated = { } attribute_names = [ "host" , "database" , "username" , "password" , "port" ] for sname in attribute_names : if names : name = names . pop ( 0 ) elif sname in kw : name = kw [ sname ] else : name = sname if name is not None and getattr ( self , sname , False ) : translated [ name ] = getattr ( self , sname ) return translated
Translate url attributes into a dictionary of connection arguments .
122
11
224,959
def get_check_declaration_sql ( self , definition ) : constraints = [ ] for field , def_ in definition . items ( ) : if isinstance ( def_ , basestring ) : constraints . append ( "CHECK (%s)" % def_ ) else : if "min" in def_ : constraints . append ( "CHECK (%s >= %s)" % ( field , def_ [ "min" ] ) ) if "max" in def_ : constraints . append ( "CHECK (%s <= %s)" % ( field , def_ [ "max" ] ) ) return ", " . join ( constraints )
Obtains DBMS specific SQL code portion needed to set a CHECK constraint declaration to be used in statements like CREATE TABLE .
132
26
224,960
def get_unique_constraint_declaration_sql ( self , name , index ) : columns = index . get_quoted_columns ( self ) name = Identifier ( name ) if not columns : raise DBALException ( 'Incomplete definition. "columns" required.' ) return "CONSTRAINT %s UNIQUE (%s)%s" % ( name . get_quoted_name ( self ) , self . get_index_field_declaration_list_sql ( columns ) , self . get_partial_index_sql ( index ) , )
Obtains DBMS specific SQL code portion needed to set a unique constraint declaration to be used in statements like CREATE TABLE .
126
25
224,961
def get_foreign_key_declaration_sql ( self , foreign_key ) : sql = self . get_foreign_key_base_declaration_sql ( foreign_key ) sql += self . get_advanced_foreign_key_options_sql ( foreign_key ) return sql
Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint of a field declaration to be used in statements like CREATE TABLE .
63
31
224,962
def get_advanced_foreign_key_options_sql ( self , foreign_key ) : query = "" if self . supports_foreign_key_on_update ( ) and foreign_key . has_option ( "on_update" ) : query += " ON UPDATE %s" % self . get_foreign_key_referential_action_sql ( foreign_key . get_option ( "on_update" ) ) if foreign_key . has_option ( "on_delete" ) : query += " ON DELETE %s" % self . get_foreign_key_referential_action_sql ( foreign_key . get_option ( "on_delete" ) ) return query
Returns the FOREIGN KEY query section dealing with non - standard options as MATCH INITIALLY DEFERRED ON UPDATE ...
152
27
224,963
def get_foreign_key_referential_action_sql ( self , action ) : action = action . upper ( ) if action not in [ "CASCADE" , "SET NULL" , "NO ACTION" , "RESTRICT" , "SET DEFAULT" , ] : raise DBALException ( "Invalid foreign key action: %s" % action ) return action
Returns the given referential action in uppercase if valid otherwise throws an exception .
80
18
224,964
def get_foreign_key_base_declaration_sql ( self , foreign_key ) : sql = "" if foreign_key . get_name ( ) : sql += "CONSTRAINT %s " % foreign_key . get_quoted_name ( self ) sql += "FOREIGN KEY (" if not foreign_key . get_local_columns ( ) : raise DBALException ( 'Incomplete definition. "local" required.' ) if not foreign_key . get_foreign_columns ( ) : raise DBALException ( 'Incomplete definition. "foreign" required.' ) if not foreign_key . get_foreign_table_name ( ) : raise DBALException ( 'Incomplete definition. "foreign_table" required.' ) sql += "%s) REFERENCES %s (%s)" % ( ", " . join ( foreign_key . get_quoted_local_columns ( self ) ) , foreign_key . get_quoted_foreign_table_name ( self ) , ", " . join ( foreign_key . get_quoted_foreign_columns ( self ) ) , ) return sql
Obtains DBMS specific SQL code portion needed to set the FOREIGN KEY constraint of a field declaration to be used in statements like CREATE TABLE .
245
31
224,965
def get_column_declaration_list_sql ( self , fields ) : query_fields = [ ] for name , field in fields . items ( ) : query_fields . append ( self . get_column_declaration_sql ( name , field ) ) return ", " . join ( query_fields )
Gets declaration of a number of fields in bulk .
66
11
224,966
def get_create_index_sql ( self , index , table ) : if isinstance ( table , Table ) : table = table . get_quoted_name ( self ) name = index . get_quoted_name ( self ) columns = index . get_quoted_columns ( self ) if not columns : raise DBALException ( 'Incomplete definition. "columns" required.' ) if index . is_primary ( ) : return self . get_create_primary_key_sql ( index , table ) query = "CREATE %sINDEX %s ON %s" % ( self . get_create_index_sql_flags ( index ) , name , table , ) query += " (%s)%s" % ( self . get_index_field_declaration_list_sql ( columns ) , self . get_partial_index_sql ( index ) , ) return query
Returns the SQL to create an index on a table on this platform .
194
14
224,967
def get_create_primary_key_sql ( self , index , table ) : return "ALTER TABLE %s ADD PRIMARY KEY (%s)" % ( table , self . get_index_field_declaration_list_sql ( index . get_quoted_columns ( self ) ) , )
Returns the SQL to create an unnamed primary key constraint .
67
11
224,968
def get_create_foreign_key_sql ( self , foreign_key , table ) : if isinstance ( table , Table ) : table = table . get_quoted_name ( self ) query = "ALTER TABLE %s ADD %s" % ( table , self . get_foreign_key_declaration_sql ( foreign_key ) , ) return query
Returns the SQL to create a new foreign key .
79
10
224,969
def get_drop_table_sql ( self , table ) : if isinstance ( table , Table ) : table = table . get_quoted_name ( self ) return "DROP TABLE %s" % table
Returns the SQL snippet to drop an existing table .
46
10
224,970
def get_drop_index_sql ( self , index , table = None ) : if isinstance ( index , Index ) : index = index . get_quoted_name ( self ) return "DROP INDEX %s" % index
Returns the SQL to drop an index from a table .
51
11
224,971
def _get_create_table_sql ( self , table_name , columns , options = None ) : options = options or { } column_list_sql = self . get_column_declaration_list_sql ( columns ) if options . get ( "unique_constraints" ) : for name , definition in options [ "unique_constraints" ] . items ( ) : column_list_sql += ", %s" % self . get_unique_constraint_declaration_sql ( name , definition ) if options . get ( "primary" ) : column_list_sql += ", PRIMARY KEY(%s)" % ", " . join ( options [ "primary" ] ) if options . get ( "indexes" ) : for index , definition in options [ "indexes" ] : column_list_sql += ", %s" % self . get_index_declaration_sql ( index , definition ) query = "CREATE TABLE %s (%s" % ( table_name , column_list_sql ) check = self . get_check_declaration_sql ( columns ) if check : query += ", %s" % check query += ")" sql = [ query ] if options . get ( "foreign_keys" ) : for definition in options [ "foreign_keys" ] : sql . append ( self . get_create_foreign_key_sql ( definition , table_name ) ) return sql
Returns the SQL used to create a table .
309
9
224,972
def quote_identifier ( self , string ) : if "." in string : parts = list ( map ( self . quote_single_identifier , string . split ( "." ) ) ) return "." . join ( parts ) return self . quote_single_identifier ( string )
Quotes a string so that it can be safely used as a table or column name even if it is a reserved word of the platform . This also detects identifier chains separated by dot and quotes them independently .
61
40
224,973
def _detect_database_platform ( self ) : version = self . _get_database_platform_version ( ) if version is not None : self . _platform = self . _create_database_platform_for_version ( version ) else : self . _platform = self . get_dbal_platform ( )
Detects and sets the database platform .
69
8
224,974
def _check_for_more_pages ( self ) : self . _has_more = len ( self . _items ) > self . per_page self . _items = self . _items [ 0 : self . per_page ]
Check for more pages . The last item will be sliced off .
51
13
224,975
def diff_index ( self , index1 , index2 ) : if index1 . is_fullfilled_by ( index2 ) and index2 . is_fullfilled_by ( index1 ) : return False return True
Finds the difference between the indexes index1 and index2 .
47
13
224,976
def call ( self , klass ) : self . _resolve ( klass ) . run ( ) if self . _command : self . _command . line ( "<info>Seeded:</info> <fg=cyan>%s</>" % klass . __name__ )
Seed the given connection from the given class .
63
10
224,977
def _resolve ( self , klass ) : resolver = None if self . _resolver : resolver = self . _resolver elif self . _command : resolver = self . _command . resolver instance = klass ( ) instance . set_connection_resolver ( resolver ) if self . _command : instance . set_command ( self . _command ) return instance
Resolve an instance of the given seeder klass .
84
12
224,978
def select ( self , * columns ) : if not columns : columns = [ "*" ] self . columns = list ( columns ) return self
Set the columns to be selected
30
6
224,979
def select_raw ( self , expression , bindings = None ) : self . add_select ( QueryExpression ( expression ) ) if bindings : self . add_binding ( bindings , "select" ) return self
Add a new raw select expression to the query
44
9
224,980
def select_sub ( self , query , as_ ) : if isinstance ( query , QueryBuilder ) : bindings = query . get_bindings ( ) query = query . to_sql ( ) elif isinstance ( query , basestring ) : bindings = [ ] else : raise ArgumentError ( "Invalid subselect" ) return self . select_raw ( "(%s) AS %s" % ( query , self . _grammar . wrap ( as_ ) ) , bindings )
Add a subselect expression to the query
104
8
224,981
def add_select ( self , * column ) : if not column : column = [ ] self . columns += list ( column ) return self
Add a new select column to query
29
7
224,982
def left_join_where ( self , table , one , operator , two ) : return self . join_where ( table , one , operator , two , "left" )
Add a left join where clause to the query
37
9
224,983
def right_join ( self , table , one = None , operator = None , two = None ) : if isinstance ( table , JoinClause ) : table . type = "right" return self . join ( table , one , operator , two , "right" )
Add a right join to the query
57
7
224,984
def right_join_where ( self , table , one , operator , two ) : return self . join_where ( table , one , operator , two , "right" )
Add a right join where clause to the query
37
9
224,985
def group_by ( self , * columns ) : for column in columns : self . groups . append ( column ) return self
Add a group by clause to the query
26
8
224,986
def having_raw ( self , sql , bindings = None , boolean = "and" ) : type = "raw" self . havings . append ( { "type" : type , "sql" : sql , "boolean" : boolean } ) self . add_binding ( bindings , "having" ) return self
Add a raw having clause to the query
68
8
224,987
def order_by ( self , column , direction = "asc" ) : if self . unions : prop = "union_orders" else : prop = "orders" if direction . lower ( ) == "asc" : direction = "asc" else : direction = "desc" getattr ( self , prop ) . append ( { "column" : column , "direction" : direction } ) return self
Add a order by clause to the query
84
8
224,988
def order_by_raw ( self , sql , bindings = None ) : if bindings is None : bindings = [ ] type = "raw" self . orders . append ( { "type" : type , "sql" : sql } ) self . add_binding ( bindings , "order" ) return self
Add a raw order by clause to the query
64
9
224,989
def get ( self , columns = None ) : if not columns : columns = [ "*" ] original = self . columns if not original : self . columns = columns results = self . _processor . process_select ( self , self . _run_select ( ) ) self . columns = original return Collection ( results )
Execute the query as a select statement
67
8
224,990
def _run_select ( self ) : return self . _connection . select ( self . to_sql ( ) , self . get_bindings ( ) , not self . _use_write_connection )
Run the query as a select statement against the connection .
44
11
224,991
def exists ( self ) : limit = self . limit_ result = self . limit ( 1 ) . count ( ) > 0 self . limit ( limit ) return result
Determine if any rows exist for the current query .
34
12
224,992
def count ( self , * columns ) : if not columns and self . distinct_ : columns = self . columns if not columns : columns = [ "*" ] return int ( self . aggregate ( "count" , * columns ) )
Retrieve the count result of the query
49
8
224,993
def aggregate ( self , func , * columns ) : if not columns : columns = [ "*" ] self . aggregate_ = { "function" : func , "columns" : columns } previous_columns = self . columns results = self . get ( * columns ) . all ( ) self . aggregate_ = None self . columns = previous_columns if len ( results ) > 0 : return dict ( ( k . lower ( ) , v ) for k , v in results [ 0 ] . items ( ) ) [ "aggregate" ]
Execute an aggregate function against the database
116
8
224,994
def insert ( self , _values = None , * * values ) : if not values and not _values : return True if not isinstance ( _values , list ) : if _values is not None : values . update ( _values ) values = [ values ] else : values = _values for i , value in enumerate ( values ) : values [ i ] = OrderedDict ( sorted ( value . items ( ) ) ) bindings = [ ] for record in values : for value in record . values ( ) : bindings . append ( value ) sql = self . _grammar . compile_insert ( self , values ) bindings = self . _clean_bindings ( bindings ) return self . _connection . insert ( sql , bindings )
Insert a new record into the database
155
7
224,995
def insert_get_id ( self , values , sequence = None ) : values = OrderedDict ( sorted ( values . items ( ) ) ) sql = self . _grammar . compile_insert_get_id ( self , values , sequence ) values = self . _clean_bindings ( values . values ( ) ) return self . _processor . process_insert_get_id ( self , sql , values , sequence )
Insert a new record and get the value of the primary key
92
12
224,996
def truncate ( self ) : for sql , bindings in self . _grammar . compile_truncate ( self ) . items ( ) : self . _connection . statement ( sql , bindings )
Run a truncate statement on the table
42
8
224,997
def _clean_bindings ( self , bindings ) : return list ( filter ( lambda b : not isinstance ( b , QueryExpression ) , bindings ) )
Remove all of the expressions from bindings
34
7
224,998
def merge ( self , query ) : self . columns += query . columns self . joins += query . joins self . wheres += query . wheres self . groups += query . groups self . havings += query . havings self . orders += query . orders self . distinct_ = query . distinct_ if self . columns : self . columns = Collection ( self . columns ) . unique ( ) . all ( ) if query . limit_ : self . limit_ = query . limit_ if query . offset_ : self . offset_ = None self . unions += query . unions if query . union_limit : self . union_limit = query . union_limit if query . union_offset : self . union_offset = query . union_offset self . union_orders += query . union_orders self . merge_bindings ( query )
Merge current query with another .
178
7
224,999
def _set_name ( self , name ) : if self . _is_identifier_quoted ( name ) : self . _quoted = True name = self . _trim_quotes ( name ) if "." in name : parts = name . split ( "." , 1 ) self . _namespace = parts [ 0 ] name = parts [ 1 ] self . _name = name
Sets the name of this asset .
85
8